1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.16 2002/03/07 03:46:17 oes Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * IJBSWA team. http://ijbswa.sourceforge.net
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.16 2002/03/07 03:46:17 oes
46 * Fixed compiler warnings
48 * Revision 1.15 2002/03/06 22:54:35 jongfoster
49 * Automated function-comment nitpicking.
51 * Revision 1.14 2002/03/05 00:24:51 jongfoster
52 * Patch to always edit the current actions file.
54 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
55 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
57 * Revision 1.12 2002/03/03 09:18:03 joergs
58 * Made jumbjuster work on AmigaOS again.
60 * Revision 1.11 2002/01/23 01:03:31 jongfoster
61 * Fixing gcc [CygWin] compiler warnings
63 * Revision 1.10 2002/01/23 00:22:59 jongfoster
64 * Adding new function cgi_edit_actions_section_swap(), to reorder
67 * Adding get_url_spec_param() to get a validated URL pattern.
69 * Moving edit_read_line() out of this file and into loaders.c.
71 * Adding missing html_encode() to many CGI functions.
73 * Moving the functions that #include actionlist.h to the end of the file,
74 * because the Visual C++ 97 debugger gets extremely confused if you try
75 * to debug any code that comes after them in the file.
77 * Major optimizations in cgi_edit_actions_list() to reduce the size of
78 * the generated HTML (down 40% from 550k to 304k), with major side-effects
79 * throughout the editor and templates. In particular, the length of the
80 * URLs throughout the editor has been drastically reduced, by cutting
81 * paramater names down to 1 character and CGI names down to 3-4
82 * characters, by removing all non-essential CGI paramaters even at the
83 * expense of having to re-read the actions file for the most trivial
84 * page, and by using relative rather than absolute URLs. This means
85 * that this (typical example):
87 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
88 * filename=ijb&ver=1011487572&section=12&pattern=13
89 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
93 * <a href="eau?f=ijb&v=1011487572&p=13">
95 * Revision 1.9 2002/01/17 20:56:22 jongfoster
96 * Replacing hard references to the URL of the config interface
97 * with #defines from project.h
99 * Revision 1.8 2001/11/30 23:35:51 jongfoster
100 * Renaming actionsfile to ijb.action
102 * Revision 1.7 2001/11/13 00:28:24 jongfoster
103 * - Renaming parameters from edit-actions-for-url so that they only
104 * contain legal JavaScript characters. If we wanted to write
105 * JavaScript that worked with Netscape 4, this is nessacery.
106 * (Note that at the moment the JavaScript doesn't actually work
107 * with Netscape 4, but now this is purely a template issue, not
108 * one affecting code).
109 * - Adding new CGIs for use by non-JavaScript browsers:
110 * edit-actions-url-form
111 * edit-actions-add-url-form
112 * edit-actions-remove-url-form
115 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
116 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
117 * by and __OS2__ ifdef.
119 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
120 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
121 * threads to call select() simultaneously. So, it's time to do a real, live,
122 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
123 * (native). Both versions will work, but using __OS2__ offers multi-threading.
125 * Revision 1.4 2001/10/23 21:48:19 jongfoster
126 * Cleaning up error handling in CGI functions - they now send back
127 * a HTML error page and should never cause a FATAL error. (Fixes one
128 * potential source of "denial of service" attacks).
130 * CGI actions file editor that works and is actually useful.
132 * Ability to toggle JunkBuster remotely using a CGI call.
134 * You can turn off both the above features in the main configuration
135 * file, e.g. if you are running a multi-user proxy.
137 * Revision 1.3 2001/10/14 22:12:49 jongfoster
138 * New version of CGI-based actionsfile editor.
139 * Major changes, including:
140 * - Completely new file parser and file output routines
141 * - edit-actions CGI renamed edit-actions-for-url
142 * - All CGIs now need a filename parameter, except for...
143 * - New CGI edit-actions which doesn't need a filename,
144 * to allow you to start the editor up.
145 * - edit-actions-submit now works, and now automatically
146 * redirects you back to the main edit-actions-list handler.
148 * Revision 1.2 2001/09/16 17:05:14 jongfoster
149 * Removing unused #include showarg.h
151 * Revision 1.1 2001/09/16 15:47:37 jongfoster
152 * First version of CGI-based edit interface. This is very much a
153 * work-in-progress, and you can't actually use it to edit anything
154 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
155 * to have any effect.
158 **********************************************************************/
164 * FIXME: Following includes copied from cgi.c - which are actually needed?
169 #include <sys/types.h>
174 #include <sys/stat.h>
177 #define snprintf _snprintf
178 #endif /* def _WIN32 */
183 #include "cgisimple.h"
187 #include "miscutil.h"
191 /* loadcfg.h is for g_bToggleIJB only */
192 #include "urlmatch.h"
194 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
197 #ifdef FEATURE_CGI_EDIT_ACTIONS
201 struct file_line * next;
209 struct action_spec action[1];
218 /* Add more data types here... e.g.
221 struct url_spec url[1];
225 struct action_spec action[1];
234 #define FILE_LINE_UNPROCESSED 1
235 #define FILE_LINE_BLANK 2
236 #define FILE_LINE_ALIAS_HEADER 3
237 #define FILE_LINE_ALIAS_ENTRY 4
238 #define FILE_LINE_ACTION 5
239 #define FILE_LINE_URL 6
240 #define FILE_LINE_SETTINGS_HEADER 7
241 #define FILE_LINE_SETTINGS_ENTRY 8
242 #define FILE_LINE_DESCRIPTION_HEADER 9
243 #define FILE_LINE_DESCRIPTION_ENTRY 10
248 struct file_line * lines;
249 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
250 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
251 /* Pre-encoded with url_encode() for ease of use. */
252 const char * version_str; /* Last modification time, as a string. For CGI param */
253 /* Can be used in URL without using url_param(). */
254 unsigned version; /* Last modification time - prevents chaos with
255 * the browser's "back" button. Note that this is a
256 * time_t cast to an unsigned. When comparing, always
257 * cast the time_t to an unsigned, and *NOT* vice-versa.
258 * This may lose the top few bits, but they're not
259 * significant anyway.
261 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
262 * Note that changing this after the file has been
263 * read in will cause a mess.
265 struct file_line * parse_error; /* On parse error, this is the offending line. */
266 const char * parse_error_text; /* On parse error, this is the problem.
267 * (Statically allocated) */
270 /* FIXME: Following non-static functions should be prototyped in .h or made static */
272 /* Functions to read and write arbitrary config files */
273 jb_err edit_read_file(struct client_state *csp,
274 const struct map *parameters,
277 struct editable_file **pfile);
278 jb_err edit_write_file(struct editable_file * file);
279 void edit_free_file(struct editable_file * file);
281 /* Functions to read and write actions files */
282 jb_err edit_parse_actions_file(struct editable_file * file);
283 jb_err edit_read_actions_file(struct client_state *csp,
284 struct http_response *rsp,
285 const struct map *parameters,
287 struct editable_file **pfile);
290 jb_err cgi_error_modified(struct client_state *csp,
291 struct http_response *rsp,
292 const char *filename);
293 jb_err cgi_error_parse(struct client_state *csp,
294 struct http_response *rsp,
295 struct editable_file *file);
296 jb_err cgi_error_file(struct client_state *csp,
297 struct http_response *rsp,
298 const char *filename);
299 jb_err cgi_error_disabled(struct client_state *csp,
300 struct http_response *rsp);
302 /* Internal arbitrary config file support functions */
303 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
304 static void edit_free_file_lines(struct file_line * first_line);
306 /* Internal actions file support functions */
307 static int match_actions_file_header_line(const char * line, const char * name);
308 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
310 /* Internal parameter parsing functions */
311 static jb_err get_file_name_param(struct client_state *csp,
312 const struct map *parameters,
313 const char *param_name,
316 const char **pparam);
317 static jb_err get_number_param(struct client_state *csp,
318 const struct map *parameters,
321 static jb_err get_url_spec_param(struct client_state *csp,
322 const struct map *parameters,
326 /* Internal actionsfile <==> HTML conversion functions */
327 static jb_err map_radio(struct map * exports,
328 const char * optionname,
331 static jb_err actions_to_radio(struct map * exports,
332 const struct action_spec *action);
333 static jb_err actions_from_radio(const struct map * parameters,
334 struct action_spec *action);
337 static jb_err map_copy_parameter_html(struct map *out,
338 const struct map *in,
340 #if 0 /* unused function */
341 static jb_err map_copy_parameter_url(struct map *out,
342 const struct map *in,
344 #endif /* unused function */
346 /*********************************************************************
348 * Function : map_copy_parameter_html
350 * Description : Copy a CGI parameter from one map to another, HTML
354 * 1 : out = target map
355 * 2 : in = source map
356 * 3 : name = name of cgi parameter to copy
358 * Returns : JB_ERR_OK on success
359 * JB_ERR_MEMORY on out-of-memory
360 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
363 *********************************************************************/
364 static jb_err map_copy_parameter_html(struct map *out,
365 const struct map *in,
375 value = lookup(in, name);
376 err = map(out, name, 1, html_encode(value), 0);
383 else if (*value == '\0')
385 return JB_ERR_CGI_PARAMS;
394 #if 0 /* unused function */
395 /*********************************************************************
397 * Function : map_copy_parameter_html
399 * Description : Copy a CGI parameter from one map to another, URL
403 * 1 : out = target map
404 * 2 : in = source map
405 * 3 : name = name of cgi parameter to copy
407 * Returns : JB_ERR_OK on success
408 * JB_ERR_MEMORY on out-of-memory
409 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
412 *********************************************************************/
413 static jb_err map_copy_parameter_url(struct map *out,
414 const struct map *in,
424 value = lookup(in, name);
425 err = map(out, name, 1, url_encode(value), 0);
432 else if (*value == '\0')
434 return JB_ERR_CGI_PARAMS;
441 #endif /* 0 - unused function */
443 /*********************************************************************
445 * Function : cgi_edit_actions_url_form
447 * Description : CGI function that displays a form for
451 * 1 : csp = Current client state (buffers, headers, etc...)
452 * 2 : rsp = http_response data structure for output
453 * 3 : parameters = map of cgi parameters
456 * f : (filename) Identifies the file to edit
457 * v : (version) File's last-modified time
458 * p : (pattern) Line number of pattern to edit
460 * Returns : JB_ERR_OK on success
461 * JB_ERR_MEMORY on out-of-memory
462 * JB_ERR_CGI_PARAMS if the CGI parameters are not
463 * specified or not valid.
465 *********************************************************************/
466 jb_err cgi_edit_actions_url_form(struct client_state *csp,
467 struct http_response *rsp,
468 const struct map *parameters)
470 struct map * exports;
472 struct editable_file * file;
473 struct file_line * cur_line;
474 unsigned line_number;
481 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
483 return cgi_error_disabled(csp, rsp);
486 err = get_number_param(csp, parameters, "p", &patternid);
492 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
495 /* No filename specified, can't read file, modified, or out of memory. */
496 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
499 cur_line = file->lines;
501 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
503 cur_line = cur_line->next;
506 if ( (cur_line == NULL)
507 || (line_number != patternid)
509 || (cur_line->type != FILE_LINE_URL))
511 /* Invalid "patternid" parameter */
512 edit_free_file(file);
513 return JB_ERR_CGI_PARAMS;
516 if (NULL == (exports = default_exports(csp, NULL)))
518 edit_free_file(file);
519 return JB_ERR_MEMORY;
522 err = map(exports, "f", 1, file->identifier, 1);
523 if (!err) err = map(exports, "v", 1, file->version_str, 1);
524 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
525 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
527 edit_free_file(file);
535 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
539 /*********************************************************************
541 * Function : cgi_edit_actions_add_url_form
543 * Description : CGI function that displays a form for
547 * 1 : csp = Current client state (buffers, headers, etc...)
548 * 2 : rsp = http_response data structure for output
549 * 3 : parameters = map of cgi parameters
552 * f : (filename) Identifies the file to edit
553 * v : (version) File's last-modified time
554 * s : (section) Line number of section to edit
556 * Returns : JB_ERR_OK on success
557 * JB_ERR_MEMORY on out-of-memory
558 * JB_ERR_CGI_PARAMS if the CGI parameters are not
559 * specified or not valid.
561 *********************************************************************/
562 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
563 struct http_response *rsp,
564 const struct map *parameters)
573 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
575 return cgi_error_disabled(csp, rsp);
578 if (NULL == (exports = default_exports(csp, NULL)))
580 return JB_ERR_MEMORY;
583 err = map_copy_parameter_html(exports, parameters, "f");
584 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
585 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
593 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
597 /*********************************************************************
599 * Function : cgi_edit_actions_remove_url_form
601 * Description : CGI function that displays a form for
605 * 1 : csp = Current client state (buffers, headers, etc...)
606 * 2 : rsp = http_response data structure for output
607 * 3 : parameters = map of cgi parameters
610 * f : (filename) Identifies the file to edit
611 * v : (version) File's last-modified time
612 * p : (pattern) Line number of pattern to edit
614 * Returns : JB_ERR_OK on success
615 * JB_ERR_MEMORY on out-of-memory
616 * JB_ERR_CGI_PARAMS if the CGI parameters are not
617 * specified or not valid.
619 *********************************************************************/
620 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
621 struct http_response *rsp,
622 const struct map *parameters)
624 struct map * exports;
626 struct editable_file * file;
627 struct file_line * cur_line;
628 unsigned line_number;
635 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
637 return cgi_error_disabled(csp, rsp);
640 err = get_number_param(csp, parameters, "p", &patternid);
646 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
649 /* No filename specified, can't read file, modified, or out of memory. */
650 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
653 cur_line = file->lines;
655 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
657 cur_line = cur_line->next;
660 if ( (cur_line == NULL)
661 || (line_number != patternid)
663 || (cur_line->type != FILE_LINE_URL))
665 /* Invalid "patternid" parameter */
666 edit_free_file(file);
667 return JB_ERR_CGI_PARAMS;
670 if (NULL == (exports = default_exports(csp, NULL)))
672 edit_free_file(file);
673 return JB_ERR_MEMORY;
676 err = map(exports, "f", 1, file->identifier, 1);
677 if (!err) err = map(exports, "v", 1, file->version_str, 1);
678 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
679 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
681 edit_free_file(file);
689 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
693 /*********************************************************************
695 * Function : edit_write_file
697 * Description : Write a complete file to disk.
700 * 1 : filename = File to write to.
701 * 2 : file = Data structure to write.
703 * Returns : JB_ERR_OK on success
704 * JB_ERR_FILE on error writing to file.
705 * JB_ERR_MEMORY on out of memory
707 *********************************************************************/
708 jb_err edit_write_file(struct editable_file * file)
711 struct file_line * cur_line;
712 struct stat statbuf[1];
713 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
714 digits in time_t, assuming this is a 64-bit
715 machine, plus null terminator, plus one
719 assert(file->filename);
721 #if defined(AMIGA) || defined(__OS2__)
722 if (NULL == (fp = fopen(file->filename, "w")))
724 if (NULL == (fp = fopen(file->filename, "wt")))
725 #endif /* def AMIGA */
730 cur_line = file->lines;
731 while (cur_line != NULL)
735 if (fputs(cur_line->raw, fp) < 0)
743 if (cur_line->prefix)
745 if (fputs(cur_line->prefix, fp) < 0)
751 if (cur_line->unprocessed)
753 /* This should be a single line - sanity check. */
754 assert(NULL == strchr(cur_line->unprocessed, '\r'));
755 assert(NULL == strchr(cur_line->unprocessed, '\n'));
757 if (NULL != strchr(cur_line->unprocessed, '#'))
759 /* Must quote '#' characters */
766 /* Count number of # characters, so we know length of output string */
767 src = cur_line->unprocessed;
768 while (NULL != (src = strchr(src, '#')))
775 /* Allocate new memory for string */
776 len = strlen(cur_line->unprocessed);
777 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
779 /* Uh oh, just trashed file! */
781 return JB_ERR_MEMORY;
784 /* Loop through string from end */
785 src = cur_line->unprocessed + len;
786 dest = str + len + numhash;
787 for ( ; len >= 0; len--)
789 if ((*dest-- = *src--) == '#')
793 assert(numhash >= 0);
796 assert(numhash == 0);
797 assert(src + 1 == cur_line->unprocessed);
798 assert(dest + 1 == str);
800 if (fputs(str, fp) < 0)
811 /* Can write without quoting '#' characters. */
812 if (fputs(cur_line->unprocessed, fp) < 0)
818 if (fputs(NEWLINE(file->newline), fp) < 0)
826 /* FIXME: Write data from file->data->whatever */
830 cur_line = cur_line->next;
836 /* Update the version stamp in the file structure, since we just
837 * wrote to the file & changed it's date.
839 if (stat(file->filename, statbuf) < 0)
841 /* Error, probably file not found. */
844 file->version = (unsigned)statbuf->st_mtime;
846 /* Correct file->version_str */
847 freez(file->version_str);
848 snprintf(version_buf, 22, "%u", file->version);
849 version_buf[21] = '\0';
850 file->version_str = strdup(version_buf);
851 if (version_buf == NULL)
853 return JB_ERR_MEMORY;
860 /*********************************************************************
862 * Function : edit_free_file
864 * Description : Free a complete file in memory.
867 * 1 : file = Data structure to free.
871 *********************************************************************/
872 void edit_free_file(struct editable_file * file)
876 /* Silently ignore NULL pointer */
880 edit_free_file_lines(file->lines);
881 freez(file->filename);
882 freez(file->identifier);
883 freez(file->version_str);
885 file->parse_error_text = NULL; /* Statically allocated */
886 file->parse_error = NULL;
892 /*********************************************************************
894 * Function : edit_free_file
896 * Description : Free an entire linked list of file lines.
899 * 1 : first_line = Data structure to free.
903 *********************************************************************/
904 static void edit_free_file_lines(struct file_line * first_line)
906 struct file_line * next_line;
908 while (first_line != NULL)
910 next_line = first_line->next;
911 first_line->next = NULL;
912 freez(first_line->raw);
913 freez(first_line->prefix);
914 freez(first_line->unprocessed);
915 switch(first_line->type)
917 case 0: /* special case if memory zeroed */
918 case FILE_LINE_UNPROCESSED:
919 case FILE_LINE_BLANK:
920 case FILE_LINE_ALIAS_HEADER:
921 case FILE_LINE_SETTINGS_HEADER:
922 case FILE_LINE_DESCRIPTION_HEADER:
923 case FILE_LINE_DESCRIPTION_ENTRY:
924 case FILE_LINE_ALIAS_ENTRY:
926 /* No data is stored for these */
929 case FILE_LINE_ACTION:
930 free_action(first_line->data.action);
933 case FILE_LINE_SETTINGS_ENTRY:
934 freez(first_line->data.setting.name);
935 freez(first_line->data.setting.svalue);
938 /* Should never happen */
942 first_line->type = 0; /* paranoia */
944 first_line = next_line;
949 /*********************************************************************
951 * Function : match_actions_file_header_line
953 * Description : Match an actions file {{header}} line
956 * 1 : line = String from file
957 * 2 : name = Header to match against
959 * Returns : 0 iff they match.
961 *********************************************************************/
962 static int match_actions_file_header_line(const char * line, const char * name)
970 if ((line[0] != '{') || (line[1] != '{'))
976 /* Look for optional whitespace */
977 while ( (*line == ' ') || (*line == '\t') )
982 /* Look for the specified name (case-insensitive) */
984 if (0 != strncmpic(line, name, len))
990 /* Look for optional whitespace */
991 while ( (*line == ' ') || (*line == '\t') )
996 /* Look for "}}" and end of string*/
997 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1007 /*********************************************************************
1009 * Function : match_actions_file_header_line
1011 * Description : Match an actions file {{header}} line
1014 * 1 : line = String from file. Must not start with
1015 * whitespace (else infinite loop!)
1016 * 2 : name = Destination for name
1017 * 2 : name = Destination for value
1019 * Returns : JB_ERR_OK on success
1020 * JB_ERR_MEMORY on out-of-memory
1021 * JB_ERR_PARSE if there's no "=" sign, or if there's
1022 * nothing before the "=" sign (but empty
1023 * values *after* the "=" sign are legal).
1025 *********************************************************************/
1026 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1028 const char * name_end;
1029 const char * value_start;
1035 assert(*line != ' ');
1036 assert(*line != '\t');
1041 value_start = strchr(line, '=');
1042 if ((value_start == NULL) || (value_start == line))
1044 return JB_ERR_PARSE;
1047 name_end = value_start - 1;
1049 /* Eat any whitespace before the '=' */
1050 while ((*name_end == ' ') || (*name_end == '\t'))
1053 * we already know we must have at least 1 non-ws char
1054 * at start of buf - no need to check
1059 name_len = name_end - line + 1; /* Length excluding \0 */
1060 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1062 return JB_ERR_MEMORY;
1064 strncpy(*pname, line, name_len);
1065 (*pname)[name_len] = '\0';
1067 /* Eat any the whitespace after the '=' */
1069 while ((*value_start == ' ') || (*value_start == '\t'))
1074 if (NULL == (*pvalue = strdup(value_start)))
1078 return JB_ERR_MEMORY;
1085 /*********************************************************************
1087 * Function : edit_parse_actions_file
1089 * Description : Parse an actions file in memory.
1091 * Passed linked list must have the "data" member
1092 * zeroed, and must contain valid "next" and
1093 * "unprocessed" fields. The "raw" and "prefix"
1094 * fields are ignored, and "type" is just overwritten.
1096 * Note that on error the file may have been
1100 * 1 : file = Actions file to be parsed in-place.
1102 * Returns : JB_ERR_OK on success
1103 * JB_ERR_MEMORY on out-of-memory
1104 * JB_ERR_PARSE on error
1106 *********************************************************************/
1107 jb_err edit_parse_actions_file(struct editable_file * file)
1109 struct file_line * cur_line;
1111 const char * text; /* Text from a line */
1112 char * name; /* For lines of the form name=value */
1113 char * value; /* For lines of the form name=value */
1114 struct action_alias * alias_list = NULL;
1115 jb_err err = JB_ERR_OK;
1117 /* alias_list contains the aliases defined in this file.
1118 * It might be better to use the "file_line.data" fields
1119 * in the relavent places instead.
1122 cur_line = file->lines;
1124 /* A note about blank line support: Blank lines should only
1125 * ever occur as the last line in the file. This function
1126 * is more forgiving than that - FILE_LINE_BLANK can occur
1130 /* Skip leading blanks. Should only happen if file is
1131 * empty (which is valid, but pointless).
1133 while ( (cur_line != NULL)
1134 && (cur_line->unprocessed[0] == '\0') )
1137 cur_line->type = FILE_LINE_BLANK;
1138 cur_line = cur_line->next;
1141 if ( (cur_line != NULL)
1142 && (cur_line->unprocessed[0] != '{') )
1144 /* File doesn't start with a header */
1145 file->parse_error = cur_line;
1146 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1147 return JB_ERR_PARSE;
1150 if ( (cur_line != NULL) && (0 ==
1151 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1153 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1155 cur_line = cur_line->next;
1156 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1158 if (cur_line->unprocessed[0])
1160 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1162 err = split_line_on_equals(cur_line->unprocessed,
1163 &cur_line->data.setting.name,
1164 &cur_line->data.setting.svalue);
1165 if (err == JB_ERR_MEMORY)
1169 else if (err != JB_ERR_OK)
1171 /* Line does not contain a name=value pair */
1172 file->parse_error = cur_line;
1173 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1174 return JB_ERR_PARSE;
1179 cur_line->type = FILE_LINE_BLANK;
1181 cur_line = cur_line->next;
1185 if ( (cur_line != NULL) && (0 ==
1186 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1188 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1190 cur_line = cur_line->next;
1191 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1193 if (cur_line->unprocessed[0])
1195 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1199 cur_line->type = FILE_LINE_BLANK;
1201 cur_line = cur_line->next;
1205 if ( (cur_line != NULL) && (0 ==
1206 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1208 cur_line->type = FILE_LINE_ALIAS_HEADER;
1210 cur_line = cur_line->next;
1211 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1213 if (cur_line->unprocessed[0])
1215 /* define an alias */
1216 struct action_alias * new_alias;
1218 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1220 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1221 if (err == JB_ERR_MEMORY)
1225 else if (err != JB_ERR_OK)
1227 /* Line does not contain a name=value pair */
1228 file->parse_error = cur_line;
1229 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1230 return JB_ERR_PARSE;
1233 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1238 free_alias_list(alias_list);
1239 return JB_ERR_MEMORY;
1242 err = get_actions(value, alias_list, new_alias->action);
1245 /* Invalid action or out of memory */
1249 free_alias_list(alias_list);
1250 if (err == JB_ERR_MEMORY)
1256 /* Line does not contain a name=value pair */
1257 file->parse_error = cur_line;
1258 file->parse_error_text = "This alias does not specify a valid set of actions.";
1259 return JB_ERR_PARSE;
1265 new_alias->name = name;
1268 new_alias->next = alias_list;
1269 alias_list = new_alias;
1273 cur_line->type = FILE_LINE_BLANK;
1275 cur_line = cur_line->next;
1279 /* Header done, process the main part of the file */
1280 while (cur_line != NULL)
1282 /* At this point, (cur_line->unprocessed[0] == '{') */
1283 assert(cur_line->unprocessed[0] == '{');
1284 text = cur_line->unprocessed + 1;
1285 len = strlen(text) - 1;
1286 if (text[len] != '}')
1288 /* No closing } on header */
1289 free_alias_list(alias_list);
1290 file->parse_error = cur_line;
1291 file->parse_error_text = "Headers starting with '{' must have a "
1292 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1293 "must close with two brackets ('}}').";
1294 return JB_ERR_PARSE;
1299 /* An invalid {{ header. */
1300 free_alias_list(alias_list);
1301 file->parse_error = cur_line;
1302 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1303 "Please remember that the system (two-bracket) headers must "
1304 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1305 "and must appear before any actions (one-bracket) headers. "
1306 "Also note that system headers may not be repeated.";
1307 return JB_ERR_PARSE;
1310 while ( (*text == ' ') || (*text == '\t') )
1316 && ( (text[len - 1] == ' ')
1317 || (text[len - 1] == '\t') ) )
1322 cur_line->type = FILE_LINE_ACTION;
1324 /* Remove {} and make copy */
1325 if (NULL == (value = (char *) malloc(len + 1)))
1328 free_alias_list(alias_list);
1329 return JB_ERR_MEMORY;
1331 strncpy(value, text, len);
1335 err = get_actions(value, alias_list, cur_line->data.action);
1338 /* Invalid action or out of memory */
1340 free_alias_list(alias_list);
1341 if (err == JB_ERR_MEMORY)
1347 /* Line does not contain a name=value pair */
1348 file->parse_error = cur_line;
1349 file->parse_error_text = "This header does not specify a valid set of actions.";
1350 return JB_ERR_PARSE;
1354 /* Done with string - it was clobbered anyway */
1357 /* Process next line */
1358 cur_line = cur_line->next;
1360 /* Loop processing URL patterns */
1361 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1363 if (cur_line->unprocessed[0])
1365 /* Could parse URL here, but this isn't currently needed */
1367 cur_line->type = FILE_LINE_URL;
1371 cur_line->type = FILE_LINE_BLANK;
1373 cur_line = cur_line->next;
1375 } /* End main while(cur_line != NULL) loop */
1377 free_alias_list(alias_list);
1383 /*********************************************************************
1385 * Function : edit_read_file_lines
1387 * Description : Read all the lines of a file into memory.
1388 * Handles whitespace, comments and line continuation.
1391 * 1 : fp = File to read from. On return, this will be
1392 * at EOF but it will not have been closed.
1393 * 2 : pfile = Destination for a linked list of file_lines.
1394 * Will be set to NULL on error.
1396 * Returns : JB_ERR_OK on success
1397 * JB_ERR_MEMORY on out-of-memory
1399 *********************************************************************/
1400 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1402 struct file_line * first_line; /* Keep for return value or to free */
1403 struct file_line * cur_line; /* Current line */
1404 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1412 cur_line = first_line = zalloc(sizeof(struct file_line));
1413 if (cur_line == NULL)
1415 return JB_ERR_MEMORY;
1418 cur_line->type = FILE_LINE_UNPROCESSED;
1420 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1423 /* Out of memory or empty file. */
1424 /* Note that empty file is not an error we propogate up */
1426 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1431 prev_line = cur_line;
1432 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1433 if (cur_line == NULL)
1436 edit_free_file_lines(first_line);
1437 return JB_ERR_MEMORY;
1440 cur_line->type = FILE_LINE_UNPROCESSED;
1442 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1443 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1446 edit_free_file_lines(first_line);
1447 return JB_ERR_MEMORY;
1451 while (rval != JB_ERR_FILE);
1455 /* We allocated one too many - free it */
1456 prev_line->next = NULL;
1459 *pfile = first_line;
1464 /*********************************************************************
1466 * Function : edit_read_file
1468 * Description : Read a complete file into memory.
1469 * Handles CGI parameter parsing. If requested, also
1470 * checks the file's modification timestamp.
1473 * 1 : csp = Current client state (buffers, headers, etc...)
1474 * 2 : parameters = map of cgi parameters.
1475 * 3 : require_version = true to check "ver" parameter.
1476 * 4 : suffix = File extension, e.g. ".action".
1477 * 5 : pfile = Destination for the file. Will be set
1481 * filename : The name of the file to read, without the
1482 * path or ".action" extension.
1483 * ver : (Only if require_version is nonzero)
1484 * Timestamp of the actions file. If wrong, this
1485 * function fails with JB_ERR_MODIFIED.
1487 * Returns : JB_ERR_OK on success
1488 * JB_ERR_MEMORY on out-of-memory
1489 * JB_ERR_CGI_PARAMS if "filename" was not specified
1491 * JB_ERR_FILE if the file cannot be opened or
1493 * JB_ERR_MODIFIED if version checking was requested and
1494 * failed - the file was modified outside
1495 * of this CGI editor instance.
1497 *********************************************************************/
1498 jb_err edit_read_file(struct client_state *csp,
1499 const struct map *parameters,
1500 int require_version,
1502 struct editable_file **pfile)
1504 struct file_line * lines;
1508 const char * identifier;
1509 struct editable_file * file;
1510 unsigned version = 0;
1511 struct stat statbuf[1];
1512 char version_buf[22];
1513 int newline = NEWLINE_UNKNOWN;
1521 err = get_file_name_param(csp, parameters, "f", suffix,
1522 &filename, &identifier);
1528 if (stat(filename, statbuf) < 0)
1530 /* Error, probably file not found. */
1534 version = (unsigned) statbuf->st_mtime;
1536 if (require_version)
1538 unsigned specified_version;
1539 err = get_number_param(csp, parameters, "v", &specified_version);
1546 if (version != specified_version)
1548 return JB_ERR_MODIFIED;
1552 #if defined(AMIGA) || defined(__OS2__)
1553 if (NULL == (fp = fopen(filename,"r")))
1555 if (NULL == (fp = fopen(filename,"rt")))
1556 #endif /* def AMIGA */
1562 err = edit_read_file_lines(fp, &lines, &newline);
1572 file = (struct editable_file *) zalloc(sizeof(*file));
1576 edit_free_file_lines(lines);
1580 file->lines = lines;
1581 file->newline = newline;
1582 file->filename = filename;
1583 file->version = version;
1584 file->identifier = url_encode(identifier);
1586 if (file->identifier == NULL)
1588 edit_free_file(file);
1589 return JB_ERR_MEMORY;
1592 /* Correct file->version_str */
1593 freez(file->version_str);
1594 snprintf(version_buf, 22, "%u", file->version);
1595 version_buf[21] = '\0';
1596 file->version_str = strdup(version_buf);
1597 if (version_buf == NULL)
1599 edit_free_file(file);
1600 return JB_ERR_MEMORY;
1608 /*********************************************************************
1610 * Function : edit_read_actions_file
1612 * Description : Read a complete actions file into memory.
1613 * Handles CGI parameter parsing. If requested, also
1614 * checks the file's modification timestamp.
1616 * If this function detects an error in the categories
1617 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1618 * then it handles it by filling in the specified
1619 * response structure and returning JB_ERR_FILE.
1622 * 1 : csp = Current client state (buffers, headers, etc...)
1623 * 2 : rsp = HTTP response. Only filled in on error.
1624 * 2 : parameters = map of cgi parameters.
1625 * 3 : require_version = true to check "ver" parameter.
1626 * 4 : pfile = Destination for the file. Will be set
1630 * filename : The name of the actions file to read, without the
1631 * path or ".action" extension.
1632 * ver : (Only if require_version is nonzero)
1633 * Timestamp of the actions file. If wrong, this
1634 * function fails with JB_ERR_MODIFIED.
1636 * Returns : JB_ERR_OK on success
1637 * JB_ERR_MEMORY on out-of-memory
1638 * JB_ERR_CGI_PARAMS if "filename" was not specified
1640 * JB_ERR_FILE if the file does not contain valid data,
1641 * or if file cannot be opened or
1642 * contains no data, or if version
1643 * checking was requested and failed.
1645 *********************************************************************/
1646 jb_err edit_read_actions_file(struct client_state *csp,
1647 struct http_response *rsp,
1648 const struct map *parameters,
1649 int require_version,
1650 struct editable_file **pfile)
1653 struct editable_file *file;
1661 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1664 /* Try to handle if possible */
1665 if (err == JB_ERR_FILE)
1667 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1669 else if (err == JB_ERR_MODIFIED)
1671 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1673 if (err == JB_ERR_OK)
1676 * Signal to higher-level CGI code that there was a problem but we
1677 * handled it, they should just return JB_ERR_OK.
1684 err = edit_parse_actions_file(file);
1687 if (err == JB_ERR_PARSE)
1689 err = cgi_error_parse(csp, rsp, file);
1690 if (err == JB_ERR_OK)
1693 * Signal to higher-level CGI code that there was a problem but we
1694 * handled it, they should just return JB_ERR_OK.
1699 edit_free_file(file);
1708 /*********************************************************************
1710 * Function : get_file_name_param
1712 * Description : Get the name of the file to edit from the parameters
1713 * passed to a CGI function. This function handles
1714 * security checks such as blocking urls containing
1715 * "/" or ".", prepending the config file directory,
1716 * and adding the specified suffix.
1718 * (This is an essential security check, otherwise
1719 * users may be able to pass "../../../etc/passwd"
1720 * and overwrite the password file [linux], "prn:"
1721 * and print random data [Windows], etc...)
1723 * This function only allows filenames contining the
1724 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1725 * That's probably too restrictive but at least it's
1729 * 1 : csp = Current client state (buffers, headers, etc...)
1730 * 2 : parameters = map of cgi parameters
1731 * 3 : param_name = The name of the parameter to read
1732 * 4 : suffix = File extension, e.g. ".actions"
1733 * 5 : pfilename = destination for full filename. Caller
1734 * free()s. Set to NULL on error.
1735 * 6 : pparam = destination for partial filename,
1736 * suitable for use in another URL. Allocated as part
1737 * of the map "parameters", so don't free it.
1738 * Set to NULL if not specified.
1740 * Returns : JB_ERR_OK on success
1741 * JB_ERR_MEMORY on out-of-memory
1742 * JB_ERR_CGI_PARAMS if "filename" was not specified
1745 *********************************************************************/
1746 static jb_err get_file_name_param(struct client_state *csp,
1747 const struct map *parameters,
1748 const char *param_name,
1751 const char **pparam)
1755 #if 0 /* Patch to make 3.0.0 work properly. */
1757 #endif /* 0 - Patch to make 3.0.0 work properly. */
1771 param = lookup(parameters, param_name);
1774 return JB_ERR_CGI_PARAMS;
1779 len = strlen(param);
1780 if (len >= FILENAME_MAX)
1783 return JB_ERR_CGI_PARAMS;
1786 /* Check every character to see if it's legal */
1788 while ((ch = *s++) != '\0')
1790 if ( ((ch < 'A') || (ch > 'Z'))
1791 && ((ch < 'a') || (ch > 'z'))
1792 && ((ch < '0') || (ch > '9'))
1796 /* Probable hack attempt. */
1797 return JB_ERR_CGI_PARAMS;
1802 * FIXME Following is a hack to make 3.0.0 work properly.
1803 * Change "#if 0" --> "#if 1" below when we have modular action
1807 #if 0 /* Patch to make 3.0.0 work properly. */
1808 /* Append extension */
1809 name = malloc(len + strlen(suffix) + 1);
1812 return JB_ERR_MEMORY;
1814 strcpy(name, param);
1815 strcpy(name + len, suffix);
1818 fullpath = make_path(csp->config->confdir, name);
1820 #else /* 1 - Patch to make 3.0.0 work properly. */
1821 if ((csp->actions_list == NULL)
1822 || (csp->actions_list->filename == NULL))
1824 return JB_ERR_CGI_PARAMS;
1827 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1828 ? strdup(csp->actions_list->filename) : NULL);
1829 #endif /* 1 - Patch to make 3.0.0 work properly. */
1830 if (fullpath == NULL)
1832 return JB_ERR_MEMORY;
1836 *pfilename = fullpath;
1842 /*********************************************************************
1844 * Function : get_number_param
1846 * Description : Get a non-negative integer from the parameters
1847 * passed to a CGI function.
1850 * 1 : csp = Current client state (buffers, headers, etc...)
1851 * 2 : parameters = map of cgi parameters
1852 * 3 : name = Name of CGI parameter to read
1853 * 4 : pvalue = destination for value.
1854 * Set to -1 on error.
1856 * Returns : JB_ERR_OK on success
1857 * JB_ERR_MEMORY on out-of-memory
1858 * JB_ERR_CGI_PARAMS if the parameter was not specified
1861 *********************************************************************/
1862 static jb_err get_number_param(struct client_state *csp,
1863 const struct map *parameters,
1878 param = lookup(parameters, name);
1881 return JB_ERR_CGI_PARAMS;
1884 /* We don't use atoi because I want to check this carefully... */
1887 while ((ch = *param++) != '\0')
1889 if ((ch < '0') || (ch > '9'))
1891 return JB_ERR_CGI_PARAMS;
1898 * <limits.h> defines UINT_MAX
1900 * (UINT_MAX - ch) / 10 is the largest number that
1901 * can be safely multiplied by 10 then have ch added.
1903 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1905 return JB_ERR_CGI_PARAMS;
1908 value = value * 10 + ch;
1919 /*********************************************************************
1921 * Function : get_url_spec_param
1923 * Description : Get a URL pattern from the parameters
1924 * passed to a CGI function. Removes leading/trailing
1925 * spaces and validates it.
1928 * 1 : csp = Current client state (buffers, headers, etc...)
1929 * 2 : parameters = map of cgi parameters
1930 * 3 : name = Name of CGI parameter to read
1931 * 4 : pvalue = destination for value. Will be malloc()'d.
1932 * Set to NULL on error.
1934 * Returns : JB_ERR_OK on success
1935 * JB_ERR_MEMORY on out-of-memory
1936 * JB_ERR_CGI_PARAMS if the parameter was not specified
1939 *********************************************************************/
1940 static jb_err get_url_spec_param(struct client_state *csp,
1941 const struct map *parameters,
1945 const char *orig_param;
1948 struct url_spec compiled[1];
1958 orig_param = lookup(parameters, name);
1961 return JB_ERR_CGI_PARAMS;
1964 /* Copy and trim whitespace */
1965 param = strdup(orig_param);
1968 return JB_ERR_MEMORY;
1972 /* Must be non-empty, and can't allow 1st character to be '{' */
1973 if (param[0] == '\0' || param[0] == '{')
1976 return JB_ERR_CGI_PARAMS;
1979 /* Check for embedded newlines */
1980 for (s = param; *s != '\0'; s++)
1982 if ((*s == '\r') || (*s == '\n'))
1985 return JB_ERR_CGI_PARAMS;
1989 /* Check that regex is valid */
1994 return JB_ERR_MEMORY;
1996 err = create_url_spec(compiled, s);
2001 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2003 free_url_spec(compiled);
2005 if (param[strlen(param) - 1] == '\\')
2008 * Must protect trailing '\\' from becoming line continuation character.
2009 * Two methods: 1) If it's a domain only, add a trailing '/'.
2010 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2012 if (strchr(param, '/') == NULL)
2014 err = string_append(¶m, "/");
2018 err = string_append(¶m, "(?:)");
2025 /* Check that the modified regex is valid */
2030 return JB_ERR_MEMORY;
2032 err = create_url_spec(compiled, s);
2037 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2039 free_url_spec(compiled);
2046 /*********************************************************************
2048 * Function : map_radio
2050 * Description : Map a set of radio button values. E.g. if you have
2051 * 3 radio buttons, declare them as:
2052 * <option type="radio" name="xyz" @xyz-a@>
2053 * <option type="radio" name="xyz" @xyz-b@>
2054 * <option type="radio" name="xyz" @xyz-c@>
2055 * Then map one of the @xyz-?@ variables to "checked"
2056 * and all the others to empty by calling:
2057 * map_radio(exports, "xyz", "abc", sel)
2058 * Where 'sel' is 'a', 'b', or 'c'.
2061 * 1 : exports = Exports map to modify.
2062 * 2 : optionname = name for map
2063 * 3 : values = null-terminated list of values;
2064 * 4 : value = Selected value.
2066 * CGI Parameters : None
2068 * Returns : JB_ERR_OK on success
2069 * JB_ERR_MEMORY on out-of-memory
2071 *********************************************************************/
2072 static jb_err map_radio(struct map * exports,
2073 const char * optionname,
2074 const char * values,
2086 len = strlen(optionname);
2087 buf = malloc(len + 3);
2090 return JB_ERR_MEMORY;
2093 strcpy(buf, optionname);
2098 while ((c = *values++) != '\0')
2103 if (map(exports, buf, 1, "", 1))
2105 return JB_ERR_MEMORY;
2111 return map(exports, buf, 0, "checked", 1);
2115 /*********************************************************************
2117 * Function : cgi_error_modified
2119 * Description : CGI function that is called when a file is modified
2120 * outside the CGI editor.
2123 * 1 : csp = Current client state (buffers, headers, etc...)
2124 * 2 : rsp = http_response data structure for output
2125 * 3 : filename = The file that was modified.
2127 * CGI Parameters : none
2129 * Returns : JB_ERR_OK on success
2130 * JB_ERR_MEMORY on out-of-memory error.
2132 *********************************************************************/
2133 jb_err cgi_error_modified(struct client_state *csp,
2134 struct http_response *rsp,
2135 const char *filename)
2137 struct map *exports;
2144 if (NULL == (exports = default_exports(csp, NULL)))
2146 return JB_ERR_MEMORY;
2149 err = map(exports, "f", 1, html_encode(filename), 0);
2156 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2160 /*********************************************************************
2162 * Function : cgi_error_parse
2164 * Description : CGI function that is called when a file cannot
2165 * be parsed by the CGI editor.
2168 * 1 : csp = Current client state (buffers, headers, etc...)
2169 * 2 : rsp = http_response data structure for output
2170 * 3 : file = The file that was modified.
2172 * CGI Parameters : none
2174 * Returns : JB_ERR_OK on success
2175 * JB_ERR_MEMORY on out-of-memory error.
2177 *********************************************************************/
2178 jb_err cgi_error_parse(struct client_state *csp,
2179 struct http_response *rsp,
2180 struct editable_file *file)
2182 struct map *exports;
2184 struct file_line *cur_line;
2190 if (NULL == (exports = default_exports(csp, NULL)))
2192 return JB_ERR_MEMORY;
2195 err = map(exports, "f", 1, file->identifier, 1);
2196 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2198 cur_line = file->parse_error;
2201 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2202 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2210 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2214 /*********************************************************************
2216 * Function : cgi_error_file
2218 * Description : CGI function that is called when a file cannot be
2219 * opened by the CGI editor.
2222 * 1 : csp = Current client state (buffers, headers, etc...)
2223 * 2 : rsp = http_response data structure for output
2224 * 3 : filename = The file that was modified.
2226 * CGI Parameters : none
2228 * Returns : JB_ERR_OK on success
2229 * JB_ERR_MEMORY on out-of-memory error.
2231 *********************************************************************/
2232 jb_err cgi_error_file(struct client_state *csp,
2233 struct http_response *rsp,
2234 const char *filename)
2236 struct map *exports;
2243 if (NULL == (exports = default_exports(csp, NULL)))
2245 return JB_ERR_MEMORY;
2248 err = map(exports, "f", 1, html_encode(filename), 0);
2255 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2259 /*********************************************************************
2261 * Function : cgi_error_bad_param
2263 * Description : CGI function that is called if the parameters
2264 * (query string) for a CGI were wrong.
2267 * 1 : csp = Current client state (buffers, headers, etc...)
2268 * 2 : rsp = http_response data structure for output
2270 * CGI Parameters : none
2272 * Returns : JB_ERR_OK on success
2273 * JB_ERR_MEMORY on out-of-memory error.
2275 *********************************************************************/
2276 jb_err cgi_error_disabled(struct client_state *csp,
2277 struct http_response *rsp)
2279 struct map *exports;
2284 if (NULL == (exports = default_exports(csp, NULL)))
2286 return JB_ERR_MEMORY;
2289 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2293 /*********************************************************************
2295 * Function : cgi_edit_actions
2297 * Description : CGI function that allows the user to choose which
2298 * actions file to edit.
2301 * 1 : csp = Current client state (buffers, headers, etc...)
2302 * 2 : rsp = http_response data structure for output
2303 * 3 : parameters = map of cgi parameters
2305 * CGI Parameters : None
2307 * Returns : JB_ERR_OK on success
2308 * JB_ERR_MEMORY on out-of-memory error
2310 *********************************************************************/
2311 jb_err cgi_edit_actions(struct client_state *csp,
2312 struct http_response *rsp,
2313 const struct map *parameters)
2316 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2318 return cgi_error_disabled(csp, rsp);
2321 /* FIXME: Incomplete */
2322 rsp->status = strdup("302 Local Redirect from Junkbuster");
2323 if (rsp->status == NULL)
2325 return JB_ERR_MEMORY;
2327 if (enlist_unique_header(rsp->headers, "Location",
2328 CGI_PREFIX "edit-actions-list?f=ijb"))
2332 return JB_ERR_MEMORY;
2339 /*********************************************************************
2341 * Function : cgi_edit_actions_list
2343 * Description : CGI function that edits the actions list.
2344 * FIXME: This function shouldn't FATAL ever.
2345 * FIXME: This function doesn't check the retval of map()
2347 * 1 : csp = Current client state (buffers, headers, etc...)
2348 * 2 : rsp = http_response data structure for output
2349 * 3 : parameters = map of cgi parameters
2351 * CGI Parameters : filename
2353 * Returns : JB_ERR_OK on success
2354 * JB_ERR_MEMORY on out-of-memory
2355 * JB_ERR_FILE if the file cannot be opened or
2357 * JB_ERR_CGI_PARAMS if "filename" was not specified
2360 *********************************************************************/
2361 jb_err cgi_edit_actions_list(struct client_state *csp,
2362 struct http_response *rsp,
2363 const struct map *parameters)
2365 char * section_template;
2366 char * url_template;
2371 struct map * exports;
2372 struct map * section_exports;
2373 struct map * url_exports;
2374 struct editable_file * file;
2375 struct file_line * cur_line;
2376 unsigned line_number = 0;
2377 unsigned prev_section_line_number = ((unsigned) (-1));
2381 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2383 return cgi_error_disabled(csp, rsp);
2386 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2389 /* No filename specified, can't read file, or out of memory. */
2390 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2393 if (NULL == (exports = default_exports(csp, NULL)))
2395 edit_free_file(file);
2396 return JB_ERR_MEMORY;
2399 err = map(exports, "f", 1, file->identifier, 1);
2400 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2404 edit_free_file(file);
2409 /* Should do all global exports above this point */
2411 err = template_load(csp, §ion_template, "edit-actions-list-section");
2414 edit_free_file(file);
2416 if (err == JB_ERR_FILE)
2418 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2423 err = template_load(csp, &url_template, "edit-actions-list-url");
2426 free(section_template);
2427 edit_free_file(file);
2429 if (err == JB_ERR_FILE)
2431 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2436 err = template_fill(§ion_template, exports);
2440 edit_free_file(file);
2446 err = template_fill(&url_template, exports);
2449 free(section_template);
2450 edit_free_file(file);
2455 /* Find start of actions in file */
2456 cur_line = file->lines;
2458 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2460 cur_line = cur_line->next;
2464 if (NULL == (sections = strdup("")))
2466 free(section_template);
2468 edit_free_file(file);
2470 return JB_ERR_MEMORY;
2473 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2475 if (NULL == (section_exports = new_map()))
2478 free(section_template);
2480 edit_free_file(file);
2482 return JB_ERR_MEMORY;
2485 snprintf(buf, 50, "%d", line_number);
2486 err = map(section_exports, "s", 1, buf, 1);
2487 if (!err) err = map(section_exports, "actions", 1,
2488 actions_to_html(cur_line->data.action), 0);
2491 && (cur_line->next != NULL)
2492 && (cur_line->next->type == FILE_LINE_URL))
2494 /* This section contains at least one URL, don't allow delete */
2495 err = map_block_killer(section_exports, "empty-section");
2499 if (!err) err = map_block_keep(section_exports, "empty-section");
2502 if (prev_section_line_number != ((unsigned)(-1)))
2504 /* Not last section */
2505 snprintf(buf, 50, "%d", prev_section_line_number);
2506 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2507 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2512 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2514 prev_section_line_number = line_number;
2519 free(section_template);
2521 edit_free_file(file);
2523 free_map(section_exports);
2527 /* Should do all section-specific exports above this point */
2529 if (NULL == (urls = strdup("")))
2532 free(section_template);
2534 edit_free_file(file);
2536 free_map(section_exports);
2537 return JB_ERR_MEMORY;
2542 cur_line = cur_line->next;
2545 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2547 if (NULL == (url_exports = new_map()))
2551 free(section_template);
2553 edit_free_file(file);
2555 free_map(section_exports);
2556 return JB_ERR_MEMORY;
2559 snprintf(buf, 50, "%d", line_number);
2560 err = map(url_exports, "p", 1, buf, 1);
2562 snprintf(buf, 50, "%d", url_1_2);
2563 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2565 if (!err) err = map(url_exports, "url-html", 1,
2566 html_encode(cur_line->unprocessed), 0);
2567 if (!err) err = map(url_exports, "url", 1,
2568 url_encode(cur_line->unprocessed), 0);
2574 free(section_template);
2576 edit_free_file(file);
2578 free_map(section_exports);
2579 free_map(url_exports);
2583 if (NULL == (s = strdup(url_template)))
2587 free(section_template);
2589 edit_free_file(file);
2591 free_map(section_exports);
2592 free_map(url_exports);
2593 return JB_ERR_MEMORY;
2596 err = template_fill(&s, section_exports);
2597 if (!err) err = template_fill(&s, url_exports);
2598 if (!err) err = string_append(&urls, s);
2600 free_map(url_exports);
2607 free(section_template);
2609 edit_free_file(file);
2611 free_map(section_exports);
2615 url_1_2 = 3 - url_1_2;
2617 cur_line = cur_line->next;
2621 err = map(section_exports, "urls", 1, urls, 0);
2623 /* Could also do section-specific exports here, but it wouldn't be as fast */
2625 if ( (cur_line != NULL)
2626 && (cur_line->type == FILE_LINE_ACTION))
2628 /* Not last section */
2629 snprintf(buf, 50, "%d", line_number);
2630 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2631 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2636 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2642 free(section_template);
2644 edit_free_file(file);
2646 free_map(section_exports);
2650 if (NULL == (s = strdup(section_template)))
2653 free(section_template);
2655 edit_free_file(file);
2657 free_map(section_exports);
2658 return JB_ERR_MEMORY;
2661 err = template_fill(&s, section_exports);
2662 if (!err) err = string_append(§ions, s);
2665 free_map(section_exports);
2670 free(section_template);
2672 edit_free_file(file);
2678 edit_free_file(file);
2679 free(section_template);
2682 err = map(exports, "sections", 1, sections, 0);
2689 /* Could also do global exports here, but it wouldn't be as fast */
2691 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2695 /*********************************************************************
2697 * Function : cgi_edit_actions
2699 * Description : CGI function that edits the Actions list.
2702 * 1 : csp = Current client state (buffers, headers, etc...)
2703 * 2 : rsp = http_response data structure for output
2704 * 3 : parameters = map of cgi parameters
2706 * CGI Parameters : None
2708 * Returns : JB_ERR_OK on success
2709 * JB_ERR_MEMORY on out-of-memory
2710 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2711 * specified or not valid.
2713 *********************************************************************/
2714 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2715 struct http_response *rsp,
2716 const struct map *parameters)
2718 struct map * exports;
2720 struct editable_file * file;
2721 struct file_line * cur_line;
2722 unsigned line_number;
2724 struct file_list *filter_file;
2725 struct re_filterfile_spec *filter_group;
2727 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2729 return cgi_error_disabled(csp, rsp);
2732 err = get_number_param(csp, parameters, "s", §ionid);
2738 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2741 /* No filename specified, can't read file, modified, or out of memory. */
2742 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2745 cur_line = file->lines;
2747 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2749 cur_line = cur_line->next;
2752 if ( (cur_line == NULL)
2753 || (line_number != sectionid)
2755 || (cur_line->type != FILE_LINE_ACTION))
2757 /* Invalid "sectionid" parameter */
2758 edit_free_file(file);
2759 return JB_ERR_CGI_PARAMS;
2762 if (NULL == (exports = default_exports(csp, NULL)))
2764 edit_free_file(file);
2765 return JB_ERR_MEMORY;
2768 err = map(exports, "f", 1, file->identifier, 1);
2769 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2770 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2772 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2774 filter_file = csp->rlist;
2775 filter_group = ((filter_file != NULL) ? filter_file->f : NULL);
2777 if (!err) err = map_conditional(exports, "any-filters-defined", (filter_group != NULL));
2781 edit_free_file(file);
2786 if (filter_group == NULL)
2788 err = map(exports, "filter-params", 1, "", 1);
2792 /* We have some entries in the filter list */
2795 char * filter_template;
2797 err = template_load(csp, &filter_template, "edit-actions-for-url-filter");
2800 edit_free_file(file);
2802 if (err == JB_ERR_FILE)
2804 return cgi_error_no_template(csp, rsp, "edit-actions-for-url-filter");
2809 result = strdup("");
2811 for (;(!err) && (filter_group != NULL); filter_group = filter_group->next)
2813 char current_mode = 'x';
2814 struct list_entry *filter_name;
2816 struct map *line_exports;
2819 filter_name = cur_line->data.action->multi_add[ACTION_MULTI_FILTER]->first;
2820 while ((filter_name != NULL)
2821 && (0 != strcmp(filter_group->filtername, filter_name->str)))
2823 filter_name = filter_name->next;
2826 if (filter_name != NULL)
2832 filter_name = cur_line->data.action->multi_remove[ACTION_MULTI_FILTER]->first;
2833 while ((filter_name != NULL)
2834 && (0 != strcmp(filter_group->filtername, filter_name->str)))
2836 filter_name = filter_name->next;
2838 if (filter_name != NULL)
2844 /* Generate a unique serial number */
2845 snprintf(number, sizeof(number), "%x", index++);
2846 number[sizeof(number) - 1] = '\0';
2848 line_exports = new_map();
2849 if (line_exports == NULL)
2851 err = JB_ERR_MEMORY;
2856 if (!err) err = map(line_exports, "index", 1, number, 1);
2857 if (!err) err = map(line_exports, "name", 1, filter_group->filtername, 1);
2858 if (!err) err = map_radio(line_exports, "this-filter", "ynx", current_mode);
2863 this_line = strdup(filter_template);
2864 if (this_line == NULL) err = JB_ERR_MEMORY;
2866 if (!err) err = template_fill(&this_line, line_exports);
2867 string_join(&result, this_line);
2869 free_map(line_exports);
2874 err = map(exports, "filter-params", 1, result, 0);
2882 if (!err) err = map_radio(exports, "filter-all", "nx",
2883 (cur_line->data.action->multi_remove_all[ACTION_MULTI_FILTER] ? 'n' : 'x'));
2885 edit_free_file(file);
2893 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2897 /*********************************************************************
2899 * Function : cgi_edit_actions_submit
2901 * Description : CGI function that actually edits the Actions list.
2904 * 1 : csp = Current client state (buffers, headers, etc...)
2905 * 2 : rsp = http_response data structure for output
2906 * 3 : parameters = map of cgi parameters
2908 * CGI Parameters : None
2910 * Returns : JB_ERR_OK on success
2911 * JB_ERR_MEMORY on out-of-memory
2912 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2913 * specified or not valid.
2915 *********************************************************************/
2916 jb_err cgi_edit_actions_submit(struct client_state *csp,
2917 struct http_response *rsp,
2918 const struct map *parameters)
2924 struct editable_file * file;
2925 struct file_line * cur_line;
2926 unsigned line_number;
2930 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2932 return cgi_error_disabled(csp, rsp);
2935 err = get_number_param(csp, parameters, "s", §ionid);
2941 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2944 /* No filename specified, can't read file, modified, or out of memory. */
2945 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2948 cur_line = file->lines;
2950 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2952 cur_line = cur_line->next;
2955 if ( (cur_line == NULL)
2956 || (line_number != sectionid)
2958 || (cur_line->type != FILE_LINE_ACTION))
2960 /* Invalid "sectionid" parameter */
2961 edit_free_file(file);
2962 return JB_ERR_CGI_PARAMS;
2965 err = actions_from_radio(parameters, cur_line->data.action);
2969 edit_free_file(file);
2973 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2976 edit_free_file(file);
2977 return JB_ERR_MEMORY;
2980 len = strlen(actiontext);
2984 * Empty action - must special-case this.
2985 * Simply setting len to 1 is sufficient...
2990 if (NULL == (newtext = malloc(len + 2)))
2994 edit_free_file(file);
2995 return JB_ERR_MEMORY;
2997 strcpy(newtext, actiontext);
3001 newtext[len + 1] = '\0';
3003 freez(cur_line->raw);
3004 freez(cur_line->unprocessed);
3005 cur_line->unprocessed = newtext;
3007 err = edit_write_file(file);
3010 /* Error writing file */
3011 edit_free_file(file);
3015 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3016 string_append(&target, file->identifier);
3018 edit_free_file(file);
3023 return JB_ERR_MEMORY;
3026 rsp->status = strdup("302 Local Redirect from Junkbuster");
3027 if (rsp->status == NULL)
3030 return JB_ERR_MEMORY;
3032 err = enlist_unique_header(rsp->headers, "Location", target);
3039 /*********************************************************************
3041 * Function : cgi_edit_actions_url
3043 * Description : CGI function that actually edits a URL pattern in
3047 * 1 : csp = Current client state (buffers, headers, etc...)
3048 * 2 : rsp = http_response data structure for output
3049 * 3 : parameters = map of cgi parameters
3052 * filename : Identifies the file to edit
3053 * ver : File's last-modified time
3054 * section : Line number of section to edit
3055 * pattern : Line number of pattern to edit
3056 * newval : New value for pattern
3058 * Returns : JB_ERR_OK on success
3059 * JB_ERR_MEMORY on out-of-memory
3060 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3061 * specified or not valid.
3063 *********************************************************************/
3064 jb_err cgi_edit_actions_url(struct client_state *csp,
3065 struct http_response *rsp,
3066 const struct map *parameters)
3070 struct editable_file * file;
3071 struct file_line * cur_line;
3072 unsigned line_number;
3076 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3078 return cgi_error_disabled(csp, rsp);
3081 err = get_number_param(csp, parameters, "p", &patternid);
3088 return JB_ERR_CGI_PARAMS;
3091 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3097 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3100 /* No filename specified, can't read file, modified, or out of memory. */
3102 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3106 cur_line = file->lines;
3108 while ((cur_line != NULL) && (line_number < patternid))
3110 cur_line = cur_line->next;
3114 if ( (cur_line == NULL)
3115 || (cur_line->type != FILE_LINE_URL))
3117 /* Invalid "patternid" parameter */
3119 edit_free_file(file);
3120 return JB_ERR_CGI_PARAMS;
3123 /* At this point, the line to edit is in cur_line */
3125 freez(cur_line->raw);
3126 freez(cur_line->unprocessed);
3127 cur_line->unprocessed = new_pattern;
3129 err = edit_write_file(file);
3132 /* Error writing file */
3133 edit_free_file(file);
3137 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3138 string_append(&target, file->identifier);
3140 edit_free_file(file);
3145 return JB_ERR_MEMORY;
3148 rsp->status = strdup("302 Local Redirect from Junkbuster");
3149 if (rsp->status == NULL)
3152 return JB_ERR_MEMORY;
3154 err = enlist_unique_header(rsp->headers, "Location", target);
3161 /*********************************************************************
3163 * Function : cgi_edit_actions_add_url
3165 * Description : CGI function that actually adds a URL pattern to
3169 * 1 : csp = Current client state (buffers, headers, etc...)
3170 * 2 : rsp = http_response data structure for output
3171 * 3 : parameters = map of cgi parameters
3174 * filename : Identifies the file to edit
3175 * ver : File's last-modified time
3176 * section : Line number of section to edit
3177 * newval : New pattern
3179 * Returns : JB_ERR_OK on success
3180 * JB_ERR_MEMORY on out-of-memory
3181 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3182 * specified or not valid.
3184 *********************************************************************/
3185 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3186 struct http_response *rsp,
3187 const struct map *parameters)
3191 struct file_line * new_line;
3192 struct editable_file * file;
3193 struct file_line * cur_line;
3194 unsigned line_number;
3198 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3200 return cgi_error_disabled(csp, rsp);
3203 err = get_number_param(csp, parameters, "s", §ionid);
3210 return JB_ERR_CGI_PARAMS;
3213 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3219 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3222 /* No filename specified, can't read file, modified, or out of memory. */
3224 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3228 cur_line = file->lines;
3230 while ((cur_line != NULL) && (line_number < sectionid))
3232 cur_line = cur_line->next;
3236 if ( (cur_line == NULL)
3237 || (cur_line->type != FILE_LINE_ACTION))
3239 /* Invalid "sectionid" parameter */
3241 edit_free_file(file);
3242 return JB_ERR_CGI_PARAMS;
3245 /* At this point, the section header is in cur_line - add after this. */
3247 /* Allocate the new line */
3248 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3249 if (new_line == NULL)
3252 edit_free_file(file);
3253 return JB_ERR_MEMORY;
3256 /* Fill in the data members of the new line */
3257 new_line->raw = NULL;
3258 new_line->prefix = NULL;
3259 new_line->unprocessed = new_pattern;
3260 new_line->type = FILE_LINE_URL;
3262 /* Link new_line into the list, after cur_line */
3263 new_line->next = cur_line->next;
3264 cur_line->next = new_line;
3266 /* Done making changes, now commit */
3268 err = edit_write_file(file);
3271 /* Error writing file */
3272 edit_free_file(file);
3276 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3277 string_append(&target, file->identifier);
3279 edit_free_file(file);
3284 return JB_ERR_MEMORY;
3287 rsp->status = strdup("302 Local Redirect from Junkbuster");
3288 if (rsp->status == NULL)
3291 return JB_ERR_MEMORY;
3293 err = enlist_unique_header(rsp->headers, "Location", target);
3300 /*********************************************************************
3302 * Function : cgi_edit_actions_remove_url
3304 * Description : CGI function that actually removes a URL pattern from
3308 * 1 : csp = Current client state (buffers, headers, etc...)
3309 * 2 : rsp = http_response data structure for output
3310 * 3 : parameters = map of cgi parameters
3313 * f : (filename) Identifies the file to edit
3314 * v : (version) File's last-modified time
3315 * p : (pattern) Line number of pattern to remove
3317 * Returns : JB_ERR_OK on success
3318 * JB_ERR_MEMORY on out-of-memory
3319 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3320 * specified or not valid.
3322 *********************************************************************/
3323 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3324 struct http_response *rsp,
3325 const struct map *parameters)
3328 struct editable_file * file;
3329 struct file_line * cur_line;
3330 struct file_line * prev_line;
3331 unsigned line_number;
3335 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3337 return cgi_error_disabled(csp, rsp);
3340 err = get_number_param(csp, parameters, "p", &patternid);
3346 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3349 /* No filename specified, can't read file, modified, or out of memory. */
3350 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3355 cur_line = file->lines;
3357 while ((cur_line != NULL) && (line_number < patternid))
3359 prev_line = cur_line;
3360 cur_line = cur_line->next;
3364 if ( (cur_line == NULL)
3365 || (prev_line == NULL)
3366 || (cur_line->type != FILE_LINE_URL))
3368 /* Invalid "patternid" parameter */
3369 edit_free_file(file);
3370 return JB_ERR_CGI_PARAMS;
3373 /* At this point, the line to remove is in cur_line, and the previous
3374 * one is in prev_line
3377 /* Unlink cur_line */
3378 prev_line->next = cur_line->next;
3379 cur_line->next = NULL;
3382 edit_free_file_lines(cur_line);
3384 err = edit_write_file(file);
3387 /* Error writing file */
3388 edit_free_file(file);
3392 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3393 string_append(&target, file->identifier);
3395 edit_free_file(file);
3400 return JB_ERR_MEMORY;
3403 rsp->status = strdup("302 Local Redirect from Junkbuster");
3404 if (rsp->status == NULL)
3407 return JB_ERR_MEMORY;
3409 err = enlist_unique_header(rsp->headers, "Location", target);
3416 /*********************************************************************
3418 * Function : cgi_edit_actions_section_remove
3420 * Description : CGI function that actually removes a whole section from
3421 * the actions file. The section must be empty first
3422 * (else JB_ERR_CGI_PARAMS).
3425 * 1 : csp = Current client state (buffers, headers, etc...)
3426 * 2 : rsp = http_response data structure for output
3427 * 3 : parameters = map of cgi parameters
3430 * f : (filename) Identifies the file to edit
3431 * v : (version) File's last-modified time
3432 * s : (section) Line number of section to edit
3434 * Returns : JB_ERR_OK on success
3435 * JB_ERR_MEMORY on out-of-memory
3436 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3437 * specified or not valid.
3439 *********************************************************************/
3440 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3441 struct http_response *rsp,
3442 const struct map *parameters)
3445 struct editable_file * file;
3446 struct file_line * cur_line;
3447 struct file_line * prev_line;
3448 unsigned line_number;
3452 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3454 return cgi_error_disabled(csp, rsp);
3457 err = get_number_param(csp, parameters, "s", §ionid);
3463 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3466 /* No filename specified, can't read file, modified, or out of memory. */
3467 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3471 cur_line = file->lines;
3474 while ((cur_line != NULL) && (line_number < sectionid))
3476 prev_line = cur_line;
3477 cur_line = cur_line->next;
3481 if ( (cur_line == NULL)
3482 || (cur_line->type != FILE_LINE_ACTION) )
3484 /* Invalid "sectionid" parameter */
3485 edit_free_file(file);
3486 return JB_ERR_CGI_PARAMS;
3489 if ( (cur_line->next != NULL)
3490 && (cur_line->next->type == FILE_LINE_URL) )
3492 /* Section not empty. */
3493 edit_free_file(file);
3494 return JB_ERR_CGI_PARAMS;
3497 /* At this point, the line to remove is in cur_line, and the previous
3498 * one is in prev_line
3501 /* Unlink cur_line */
3502 if (prev_line == NULL)
3504 /* Removing the first line from the file */
3505 file->lines = cur_line->next;
3509 prev_line->next = cur_line->next;
3511 cur_line->next = NULL;
3514 edit_free_file_lines(cur_line);
3516 err = edit_write_file(file);
3519 /* Error writing file */
3520 edit_free_file(file);
3524 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3525 string_append(&target, file->identifier);
3527 edit_free_file(file);
3532 return JB_ERR_MEMORY;
3535 rsp->status = strdup("302 Local Redirect from Junkbuster");
3536 if (rsp->status == NULL)
3539 return JB_ERR_MEMORY;
3541 err = enlist_unique_header(rsp->headers, "Location", target);
3548 /*********************************************************************
3550 * Function : cgi_edit_actions_section_add
3552 * Description : CGI function that adds a new empty section to
3556 * 1 : csp = Current client state (buffers, headers, etc...)
3557 * 2 : rsp = http_response data structure for output
3558 * 3 : parameters = map of cgi parameters
3561 * f : (filename) Identifies the file to edit
3562 * v : (version) File's last-modified time
3563 * s : (section) Line number of section to add after, 0 for
3566 * Returns : JB_ERR_OK on success
3567 * JB_ERR_MEMORY on out-of-memory
3568 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3569 * specified or not valid.
3571 *********************************************************************/
3572 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3573 struct http_response *rsp,
3574 const struct map *parameters)
3577 struct file_line * new_line;
3579 struct editable_file * file;
3580 struct file_line * cur_line;
3581 unsigned line_number;
3585 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3587 return cgi_error_disabled(csp, rsp);
3590 err = get_number_param(csp, parameters, "s", §ionid);
3596 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3599 /* No filename specified, can't read file, modified, or out of memory. */
3600 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3604 cur_line = file->lines;
3608 /* Add to start of file */
3609 if (cur_line != NULL)
3611 /* There's something in the file, find the line before the first
3614 while ( (cur_line->next != NULL)
3615 && (cur_line->next->type != FILE_LINE_ACTION) )
3617 cur_line = cur_line->next;
3624 /* Add after stated section. */
3625 while ((cur_line != NULL) && (line_number < sectionid))
3627 cur_line = cur_line->next;
3631 if ( (cur_line == NULL)
3632 || (cur_line->type != FILE_LINE_ACTION))
3634 /* Invalid "sectionid" parameter */
3635 edit_free_file(file);
3636 return JB_ERR_CGI_PARAMS;
3639 /* Skip through the section to find the last line in it. */
3640 while ( (cur_line->next != NULL)
3641 && (cur_line->next->type != FILE_LINE_ACTION) )
3643 cur_line = cur_line->next;
3648 /* At this point, the last line in the previous section is in cur_line
3649 * - add after this. (Or if we need to add as the first line, cur_line
3653 new_text = strdup("{}");
3654 if (NULL == new_text)
3656 edit_free_file(file);
3657 return JB_ERR_MEMORY;
3660 /* Allocate the new line */
3661 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3662 if (new_line == NULL)
3665 edit_free_file(file);
3666 return JB_ERR_MEMORY;
3669 /* Fill in the data members of the new line */
3670 new_line->raw = NULL;
3671 new_line->prefix = NULL;
3672 new_line->unprocessed = new_text;
3673 new_line->type = FILE_LINE_ACTION;
3675 if (cur_line != NULL)
3677 /* Link new_line into the list, after cur_line */
3678 new_line->next = cur_line->next;
3679 cur_line->next = new_line;
3683 /* Link new_line into the list, as first line */
3684 new_line->next = file->lines;
3685 file->lines = new_line;
3688 /* Done making changes, now commit */
3690 err = edit_write_file(file);
3693 /* Error writing file */
3694 edit_free_file(file);
3698 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3699 string_append(&target, file->identifier);
3701 edit_free_file(file);
3706 return JB_ERR_MEMORY;
3709 rsp->status = strdup("302 Local Redirect from Junkbuster");
3710 if (rsp->status == NULL)
3713 return JB_ERR_MEMORY;
3715 err = enlist_unique_header(rsp->headers, "Location", target);
3722 /*********************************************************************
3724 * Function : cgi_edit_actions_section_swap
3726 * Description : CGI function that swaps the order of two sections
3727 * in the actions file. Note that this CGI can actually
3728 * swap any two arbitrary sections, but the GUI interface
3729 * currently only allows consecutive sections to be
3733 * 1 : csp = Current client state (buffers, headers, etc...)
3734 * 2 : rsp = http_response data structure for output
3735 * 3 : parameters = map of cgi parameters
3738 * f : (filename) Identifies the file to edit
3739 * v : (version) File's last-modified time
3740 * s1 : (section1) Line number of first section to swap
3741 * s2 : (section2) Line number of second section to swap
3743 * Returns : JB_ERR_OK on success
3744 * JB_ERR_MEMORY on out-of-memory
3745 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3746 * specified or not valid.
3748 *********************************************************************/
3749 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3750 struct http_response *rsp,
3751 const struct map *parameters)
3755 struct editable_file * file;
3756 struct file_line * cur_line;
3757 struct file_line * prev_line;
3758 struct file_line * line_before_section1;
3759 struct file_line * line_start_section1;
3760 struct file_line * line_end_section1;
3761 struct file_line * line_after_section1;
3762 struct file_line * line_before_section2;
3763 struct file_line * line_start_section2;
3764 struct file_line * line_end_section2;
3765 struct file_line * line_after_section2;
3766 unsigned line_number;
3770 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3772 return cgi_error_disabled(csp, rsp);
3775 err = get_number_param(csp, parameters, "s1", §ion1);
3776 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3782 if (section1 > section2)
3784 unsigned temp = section2;
3785 section2 = section1;
3789 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3792 /* No filename specified, can't read file, modified, or out of memory. */
3793 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3796 /* Start at the beginning... */
3798 cur_line = file->lines;
3801 /* ... find section1 ... */
3802 while ((cur_line != NULL) && (line_number < section1))
3804 prev_line = cur_line;
3805 cur_line = cur_line->next;
3809 if ( (cur_line == NULL)
3810 || (cur_line->type != FILE_LINE_ACTION) )
3812 /* Invalid "section1" parameter */
3813 edit_free_file(file);
3814 return JB_ERR_CGI_PARAMS;
3817 /* If no-op, we've validated params and can skip the rest. */
3818 if (section1 != section2)
3820 /* ... find the end of section1 ... */
3821 line_before_section1 = prev_line;
3822 line_start_section1 = cur_line;
3825 prev_line = cur_line;
3826 cur_line = cur_line->next;
3829 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3830 line_end_section1 = prev_line;
3831 line_after_section1 = cur_line;
3833 /* ... find section2 ... */
3834 while ((cur_line != NULL) && (line_number < section2))
3836 prev_line = cur_line;
3837 cur_line = cur_line->next;
3841 if ( (cur_line == NULL)
3842 || (cur_line->type != FILE_LINE_ACTION) )
3844 /* Invalid "section2" parameter */
3845 edit_free_file(file);
3846 return JB_ERR_CGI_PARAMS;
3849 /* ... find the end of section2 ... */
3850 line_before_section2 = prev_line;
3851 line_start_section2 = cur_line;
3854 prev_line = cur_line;
3855 cur_line = cur_line->next;
3858 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3859 line_end_section2 = prev_line;
3860 line_after_section2 = cur_line;
3862 /* Now have all the pointers we need. Do the swap. */
3864 /* Change the pointer to section1 to point to section2 instead */
3865 if (line_before_section1 == NULL)
3867 file->lines = line_start_section2;
3871 line_before_section1->next = line_start_section2;
3874 if (line_before_section2 == line_end_section1)
3876 /* Consecutive sections */
3877 line_end_section2->next = line_start_section1;
3881 line_end_section2->next = line_after_section1;
3882 line_before_section2->next = line_start_section1;
3885 /* Set the pointer from the end of section1 to the rest of the file */
3886 line_end_section1->next = line_after_section2;
3888 err = edit_write_file(file);
3891 /* Error writing file */
3892 edit_free_file(file);
3895 } /* END if (section1 != section2) */
3897 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3898 string_append(&target, file->identifier);
3900 edit_free_file(file);
3905 return JB_ERR_MEMORY;
3908 rsp->status = strdup("302 Local Redirect from Junkbuster");
3909 if (rsp->status == NULL)
3912 return JB_ERR_MEMORY;
3914 err = enlist_unique_header(rsp->headers, "Location", target);
3921 /*********************************************************************
3923 * Function : cgi_toggle
3925 * Description : CGI function that adds a new empty section to
3929 * 1 : csp = Current client state (buffers, headers, etc...)
3930 * 2 : rsp = http_response data structure for output
3931 * 3 : parameters = map of cgi parameters
3934 * set : If present, how to change toggle setting:
3935 * "enable", "disable", "toggle", or none (default).
3936 * mini : If present, use mini reply template.
3938 * Returns : JB_ERR_OK on success
3939 * JB_ERR_MEMORY on out-of-memory
3941 *********************************************************************/
3942 jb_err cgi_toggle(struct client_state *csp,
3943 struct http_response *rsp,
3944 const struct map *parameters)
3946 struct map *exports;
3948 const char *template_name;
3955 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3957 return cgi_error_disabled(csp, rsp);
3960 if (NULL == (exports = default_exports(csp, "toggle")))
3962 return JB_ERR_MEMORY;
3965 mode = *(lookup(parameters, "set"));
3972 else if (mode == 'd')
3977 else if (mode == 't')
3980 g_bToggleIJB = !g_bToggleIJB;
3983 err = map_conditional(exports, "enabled", g_bToggleIJB);
3990 template_name = (*(lookup(parameters, "mini"))
3994 return template_fill_for_cgi(csp, template_name, exports, rsp);
3998 /*********************************************************************
4000 * Function : javascriptify
4002 * Description : Converts a string into a form JavaScript will like.
4004 * Netscape 4's JavaScript sucks - it doesn't use
4005 * "id" parameters, so you have to set the "name"
4006 * used to submit a form element to something JavaScript
4007 * will like. (Or access the elements by index in an
4008 * array. That array contains >60 elements and will
4009 * be changed whenever we add a new action to the
4010 * editor, so I'm NOT going to use indexes that have
4011 * to be figured out by hand.)
4013 * Currently the only thing we have to worry about
4014 * is "-" ==> "_" conversion.
4016 * This is a length-preserving operation so it is
4017 * carried out in-place, no memory is allocated
4021 * 1 : identifier = String to make JavaScript-friendly.
4025 *********************************************************************/
4026 static void javascriptify(char * identifier)
4028 char * p = identifier;
4029 while (NULL != (p = strchr(p, '-')))
4036 /*********************************************************************
4038 * Function : actions_to_radio
4040 * Description : Converts a actionsfile entry into settings for
4041 * radio buttons and edit boxes on a HTML form.
4044 * 1 : exports = List of substitutions to add to.
4045 * 2 : action = Action to read
4047 * Returns : JB_ERR_OK on success
4048 * JB_ERR_MEMORY on out-of-memory
4050 *********************************************************************/
4051 static jb_err actions_to_radio(struct map * exports,
4052 const struct action_spec *action)
4054 unsigned mask = action->mask;
4055 unsigned add = action->add;
4063 mask = action->mask;
4066 /* sanity - prevents "-feature +feature" */
4070 #define DEFINE_ACTION_BOOL(name, bit) \
4071 if (!(mask & bit)) \
4073 current_mode = 'n'; \
4075 else if (add & bit) \
4077 current_mode = 'y'; \
4081 current_mode = 'x'; \
4083 if (map_radio(exports, name, "ynx", current_mode)) \
4085 return JB_ERR_MEMORY; \
4088 #define DEFINE_ACTION_STRING(name, bit, index) \
4089 DEFINE_ACTION_BOOL(name, bit); \
4092 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
4095 checked = !strcmp(action->string[index], value); \
4099 checked = is_default; \
4101 mapped_param |= checked; \
4102 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
4104 return JB_ERR_MEMORY; \
4107 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
4108 if (map(exports, name "-param-custom", 1, \
4109 ((!mapped_param) ? "checked" : ""), 1)) \
4111 return JB_ERR_MEMORY; \
4113 if (map(exports, name "-param", 1, \
4114 (((add & bit) && !mapped_param) ? \
4115 action->string[index] : default_val), 1)) \
4117 return JB_ERR_MEMORY; \
4120 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
4121 if (map(exports, name "-param", 1, \
4122 ((add & bit) ? action->string[index] : default_val), 1)) \
4124 return JB_ERR_MEMORY; \
4127 #define DEFINE_ACTION_MULTI(name, index) \
4128 if (action->multi_add[index]->first) \
4130 current_mode = 'y'; \
4132 else if (action->multi_remove_all[index]) \
4134 current_mode = 'n'; \
4136 else if (action->multi_remove[index]->first) \
4138 current_mode = 'y'; \
4142 current_mode = 'x'; \
4144 if (map_radio(exports, name, "ynx", current_mode)) \
4146 return JB_ERR_MEMORY; \
4149 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4151 #include "actionlist.h"
4153 #undef DEFINE_ACTION_MULTI
4154 #undef DEFINE_ACTION_STRING
4155 #undef DEFINE_ACTION_BOOL
4156 #undef DEFINE_ACTION_ALIAS
4157 #undef DEFINE_CGI_PARAM_CUSTOM
4158 #undef DEFINE_CGI_PARAM_RADIO
4159 #undef DEFINE_CGI_PARAM_NO_RADIO
4165 /*********************************************************************
4167 * Function : actions_from_radio
4169 * Description : Converts a map of parameters passed to a CGI function
4170 * into an actionsfile entry.
4173 * 1 : parameters = parameters to the CGI call
4174 * 2 : action = Action to change. Must be valid before
4175 * the call, actions not specified will be
4178 * Returns : JB_ERR_OK on success
4179 * JB_ERR_MEMORY on out-of-memory
4181 *********************************************************************/
4182 static jb_err actions_from_radio(const struct map * parameters,
4183 struct action_spec *action)
4185 static int first_time = 1;
4189 const char * js_name;
4194 /* Statics are generally a potential race condition,
4195 * but in this case we're safe and don't need semaphores.
4196 * Be careful if you modify this function.
4200 #define JAVASCRIPTIFY(dest_var, string) \
4202 static char js_name_arr[] = string; \
4205 javascriptify(js_name_arr); \
4207 dest_var = js_name_arr; \
4210 #define DEFINE_ACTION_BOOL(name, bit) \
4211 JAVASCRIPTIFY(js_name, name); \
4212 param = lookup(parameters, js_name); \
4213 ch = ijb_toupper(param[0]); \
4216 action->add |= bit; \
4217 action->mask |= bit; \
4219 else if (ch == 'N') \
4221 action->add &= ~bit; \
4222 action->mask &= ~bit; \
4224 else if (ch == 'X') \
4226 action->add &= ~bit; \
4227 action->mask |= bit; \
4230 #define DEFINE_ACTION_STRING(name, bit, index) \
4231 JAVASCRIPTIFY(js_name, name); \
4232 param = lookup(parameters, js_name); \
4233 ch = ijb_toupper(param[0]); \
4236 JAVASCRIPTIFY(js_name, name "-mode"); \
4237 param = lookup(parameters, js_name); \
4238 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4240 JAVASCRIPTIFY(js_name, name "-param"); \
4241 param = lookup(parameters, js_name); \
4243 if (*param != '\0') \
4245 if (NULL == (param_dup = strdup(param))) \
4247 return JB_ERR_MEMORY; \
4249 freez(action->string[index]); \
4250 action->add |= bit; \
4251 action->mask |= bit; \
4252 action->string[index] = param_dup; \
4255 else if (ch == 'N') \
4257 if (action->add & bit) \
4259 freez(action->string[index]); \
4261 action->add &= ~bit; \
4262 action->mask &= ~bit; \
4264 else if (ch == 'X') \
4266 if (action->add & bit) \
4268 freez(action->string[index]); \
4270 action->add &= ~bit; \
4271 action->mask |= bit; \
4274 #define DEFINE_ACTION_MULTI(name, index) \
4275 JAVASCRIPTIFY(js_name, name); \
4276 param = lookup(parameters, js_name); \
4277 ch = ijb_toupper((int)param[0]); \
4282 else if (ch == 'N') \
4284 list_remove_all(action->multi_add[index]); \
4285 list_remove_all(action->multi_remove[index]); \
4286 action->multi_remove_all[index] = 1; \
4288 else if (ch == 'X') \
4290 list_remove_all(action->multi_add[index]); \
4291 list_remove_all(action->multi_remove[index]); \
4292 action->multi_remove_all[index] = 0; \
4295 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4297 #include "actionlist.h"
4299 #undef DEFINE_ACTION_MULTI
4300 #undef DEFINE_ACTION_STRING
4301 #undef DEFINE_ACTION_BOOL
4302 #undef DEFINE_ACTION_ALIAS
4303 #undef JAVASCRIPTIFY
4311 #endif /* def FEATURE_CGI_EDIT_ACTIONS */