1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.14 2002/03/05 00:24:51 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * IJBSWA team. http://ijbswa.sourceforge.net
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.14 2002/03/05 00:24:51 jongfoster
46 * Patch to always edit the current actions file.
48 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
49 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
51 * Revision 1.12 2002/03/03 09:18:03 joergs
52 * Made jumbjuster work on AmigaOS again.
54 * Revision 1.11 2002/01/23 01:03:31 jongfoster
55 * Fixing gcc [CygWin] compiler warnings
57 * Revision 1.10 2002/01/23 00:22:59 jongfoster
58 * Adding new function cgi_edit_actions_section_swap(), to reorder
61 * Adding get_url_spec_param() to get a validated URL pattern.
63 * Moving edit_read_line() out of this file and into loaders.c.
65 * Adding missing html_encode() to many CGI functions.
67 * Moving the functions that #include actionlist.h to the end of the file,
68 * because the Visual C++ 97 debugger gets extremely confused if you try
69 * to debug any code that comes after them in the file.
71 * Major optimizations in cgi_edit_actions_list() to reduce the size of
72 * the generated HTML (down 40% from 550k to 304k), with major side-effects
73 * throughout the editor and templates. In particular, the length of the
74 * URLs throughout the editor has been drastically reduced, by cutting
75 * paramater names down to 1 character and CGI names down to 3-4
76 * characters, by removing all non-essential CGI paramaters even at the
77 * expense of having to re-read the actions file for the most trivial
78 * page, and by using relative rather than absolute URLs. This means
79 * that this (typical example):
81 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
82 * filename=ijb&ver=1011487572&section=12&pattern=13
83 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
87 * <a href="eau?f=ijb&v=1011487572&p=13">
89 * Revision 1.9 2002/01/17 20:56:22 jongfoster
90 * Replacing hard references to the URL of the config interface
91 * with #defines from project.h
93 * Revision 1.8 2001/11/30 23:35:51 jongfoster
94 * Renaming actionsfile to ijb.action
96 * Revision 1.7 2001/11/13 00:28:24 jongfoster
97 * - Renaming parameters from edit-actions-for-url so that they only
98 * contain legal JavaScript characters. If we wanted to write
99 * JavaScript that worked with Netscape 4, this is nessacery.
100 * (Note that at the moment the JavaScript doesn't actually work
101 * with Netscape 4, but now this is purely a template issue, not
102 * one affecting code).
103 * - Adding new CGIs for use by non-JavaScript browsers:
104 * edit-actions-url-form
105 * edit-actions-add-url-form
106 * edit-actions-remove-url-form
109 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
110 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
111 * by and __OS2__ ifdef.
113 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
114 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
115 * threads to call select() simultaneously. So, it's time to do a real, live,
116 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
117 * (native). Both versions will work, but using __OS2__ offers multi-threading.
119 * Revision 1.4 2001/10/23 21:48:19 jongfoster
120 * Cleaning up error handling in CGI functions - they now send back
121 * a HTML error page and should never cause a FATAL error. (Fixes one
122 * potential source of "denial of service" attacks).
124 * CGI actions file editor that works and is actually useful.
126 * Ability to toggle JunkBuster remotely using a CGI call.
128 * You can turn off both the above features in the main configuration
129 * file, e.g. if you are running a multi-user proxy.
131 * Revision 1.3 2001/10/14 22:12:49 jongfoster
132 * New version of CGI-based actionsfile editor.
133 * Major changes, including:
134 * - Completely new file parser and file output routines
135 * - edit-actions CGI renamed edit-actions-for-url
136 * - All CGIs now need a filename parameter, except for...
137 * - New CGI edit-actions which doesn't need a filename,
138 * to allow you to start the editor up.
139 * - edit-actions-submit now works, and now automatically
140 * redirects you back to the main edit-actions-list handler.
142 * Revision 1.2 2001/09/16 17:05:14 jongfoster
143 * Removing unused #include showarg.h
145 * Revision 1.1 2001/09/16 15:47:37 jongfoster
146 * First version of CGI-based edit interface. This is very much a
147 * work-in-progress, and you can't actually use it to edit anything
148 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
149 * to have any effect.
152 **********************************************************************/
158 * FIXME: Following includes copied from cgi.c - which are actually needed?
163 #include <sys/types.h>
168 #include <sys/stat.h>
171 #define snprintf _snprintf
172 #endif /* def _WIN32 */
177 #include "cgisimple.h"
181 #include "miscutil.h"
185 /* loadcfg.h is for g_bToggleIJB only */
186 #include "urlmatch.h"
188 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
191 #ifdef FEATURE_CGI_EDIT_ACTIONS
195 struct file_line * next;
203 struct action_spec action[1];
212 /* Add more data types here... e.g.
215 struct url_spec url[1];
219 struct action_spec action[1];
228 #define FILE_LINE_UNPROCESSED 1
229 #define FILE_LINE_BLANK 2
230 #define FILE_LINE_ALIAS_HEADER 3
231 #define FILE_LINE_ALIAS_ENTRY 4
232 #define FILE_LINE_ACTION 5
233 #define FILE_LINE_URL 6
234 #define FILE_LINE_SETTINGS_HEADER 7
235 #define FILE_LINE_SETTINGS_ENTRY 8
236 #define FILE_LINE_DESCRIPTION_HEADER 9
237 #define FILE_LINE_DESCRIPTION_ENTRY 10
242 struct file_line * lines;
243 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
244 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
245 /* Pre-encoded with url_encode() for ease of use. */
246 const char * version_str; /* Last modification time, as a string. For CGI param */
247 /* Can be used in URL without using url_param(). */
248 unsigned version; /* Last modification time - prevents chaos with
249 * the browser's "back" button. Note that this is a
250 * time_t cast to an unsigned. When comparing, always
251 * cast the time_t to an unsigned, and *NOT* vice-versa.
252 * This may lose the top few bits, but they're not
253 * significant anyway.
255 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
256 * Note that changing this after the file has been
257 * read in will cause a mess.
259 struct file_line * parse_error; /* On parse error, this is the offending line. */
260 const char * parse_error_text; /* On parse error, this is the problem.
261 * (Statically allocated) */
264 /* FIXME: Following non-static functions should be prototyped in .h or made static */
266 /* Functions to read and write arbitrary config files */
267 jb_err edit_read_file(struct client_state *csp,
268 const struct map *parameters,
271 struct editable_file **pfile);
272 jb_err edit_write_file(struct editable_file * file);
273 void edit_free_file(struct editable_file * file);
275 /* Functions to read and write actions files */
276 jb_err edit_parse_actions_file(struct editable_file * file);
277 jb_err edit_read_actions_file(struct client_state *csp,
278 struct http_response *rsp,
279 const struct map *parameters,
281 struct editable_file **pfile);
284 jb_err cgi_error_modified(struct client_state *csp,
285 struct http_response *rsp,
286 const char *filename);
287 jb_err cgi_error_parse(struct client_state *csp,
288 struct http_response *rsp,
289 struct editable_file *file);
290 jb_err cgi_error_file(struct client_state *csp,
291 struct http_response *rsp,
292 const char *filename);
293 jb_err cgi_error_disabled(struct client_state *csp,
294 struct http_response *rsp);
296 /* Internal arbitrary config file support functions */
297 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
298 static void edit_free_file_lines(struct file_line * first_line);
300 /* Internal actions file support functions */
301 static int match_actions_file_header_line(const char * line, const char * name);
302 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
304 /* Internal parameter parsing functions */
305 static jb_err get_file_name_param(struct client_state *csp,
306 const struct map *parameters,
307 const char *param_name,
310 const char **pparam);
311 static jb_err get_number_param(struct client_state *csp,
312 const struct map *parameters,
315 static jb_err get_url_spec_param(struct client_state *csp,
316 const struct map *parameters,
320 /* Internal actionsfile <==> HTML conversion functions */
321 static jb_err map_radio(struct map * exports,
322 const char * optionname,
325 static jb_err actions_to_radio(struct map * exports,
326 const struct action_spec *action);
327 static jb_err actions_from_radio(const struct map * parameters,
328 struct action_spec *action);
331 static jb_err map_copy_parameter_html(struct map *out,
332 const struct map *in,
334 #if 0 /* unused function */
335 static jb_err map_copy_parameter_url(struct map *out,
336 const struct map *in,
338 #endif /* unused function */
340 /*********************************************************************
342 * Function : map_copy_parameter_html
344 * Description : Copy a CGI parameter from one map to another, HTML
348 * 1 : out = target map
349 * 2 : in = source map
350 * 3 : name = name of cgi parameter to copy
352 * Returns : JB_ERR_OK on success
353 * JB_ERR_MEMORY on out-of-memory
354 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
357 *********************************************************************/
358 static jb_err map_copy_parameter_html(struct map *out,
359 const struct map *in,
369 value = lookup(in, name);
370 err = map(out, name, 1, html_encode(value), 0);
377 else if (*value == '\0')
379 return JB_ERR_CGI_PARAMS;
388 #if 0 /* unused function */
389 /*********************************************************************
391 * Function : map_copy_parameter_html
393 * Description : Copy a CGI parameter from one map to another, URL
397 * 1 : out = target map
398 * 2 : in = source map
399 * 3 : name = name of cgi parameter to copy
401 * Returns : JB_ERR_OK on success
402 * JB_ERR_MEMORY on out-of-memory
403 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
406 *********************************************************************/
407 static jb_err map_copy_parameter_url(struct map *out,
408 const struct map *in,
418 value = lookup(in, name);
419 err = map(out, name, 1, url_encode(value), 0);
426 else if (*value == '\0')
428 return JB_ERR_CGI_PARAMS;
435 #endif /* 0 - unused function */
437 /*********************************************************************
439 * Function : cgi_edit_actions_url_form
441 * Description : CGI function that displays a form for
445 * 1 : csp = Current client state (buffers, headers, etc...)
446 * 2 : rsp = http_response data structure for output
447 * 3 : parameters = map of cgi parameters
450 * f : (filename) Identifies the file to edit
451 * v : (version) File's last-modified time
452 * p : (pattern) Line number of pattern to edit
454 * Returns : JB_ERR_OK on success
455 * JB_ERR_MEMORY on out-of-memory
456 * JB_ERR_CGI_PARAMS if the CGI parameters are not
457 * specified or not valid.
459 *********************************************************************/
460 jb_err cgi_edit_actions_url_form(struct client_state *csp,
461 struct http_response *rsp,
462 const struct map *parameters)
464 struct map * exports;
466 struct editable_file * file;
467 struct file_line * cur_line;
468 unsigned line_number;
475 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
477 return cgi_error_disabled(csp, rsp);
480 err = get_number_param(csp, parameters, "p", &patternid);
486 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
489 /* No filename specified, can't read file, modified, or out of memory. */
490 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
493 cur_line = file->lines;
495 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
497 cur_line = cur_line->next;
500 if ( (cur_line == NULL)
501 || (line_number != patternid)
503 || (cur_line->type != FILE_LINE_URL))
505 /* Invalid "patternid" parameter */
506 edit_free_file(file);
507 return JB_ERR_CGI_PARAMS;
510 if (NULL == (exports = default_exports(csp, NULL)))
512 edit_free_file(file);
513 return JB_ERR_MEMORY;
516 err = map(exports, "f", 1, file->identifier, 1);
517 if (!err) err = map(exports, "v", 1, file->version_str, 1);
518 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
519 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
521 edit_free_file(file);
529 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
533 /*********************************************************************
535 * Function : cgi_edit_actions_add_url_form
537 * Description : CGI function that displays a form for
541 * 1 : csp = Current client state (buffers, headers, etc...)
542 * 2 : rsp = http_response data structure for output
543 * 3 : parameters = map of cgi parameters
546 * f : (filename) Identifies the file to edit
547 * v : (version) File's last-modified time
548 * s : (section) Line number of section to edit
550 * Returns : JB_ERR_OK on success
551 * JB_ERR_MEMORY on out-of-memory
552 * JB_ERR_CGI_PARAMS if the CGI parameters are not
553 * specified or not valid.
555 *********************************************************************/
556 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
557 struct http_response *rsp,
558 const struct map *parameters)
567 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
569 return cgi_error_disabled(csp, rsp);
572 if (NULL == (exports = default_exports(csp, NULL)))
574 return JB_ERR_MEMORY;
577 err = map_copy_parameter_html(exports, parameters, "f");
578 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
579 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
587 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
591 /*********************************************************************
593 * Function : cgi_edit_actions_remove_url_form
595 * Description : CGI function that displays a form for
599 * 1 : csp = Current client state (buffers, headers, etc...)
600 * 2 : rsp = http_response data structure for output
601 * 3 : parameters = map of cgi parameters
604 * f : (filename) Identifies the file to edit
605 * v : (version) File's last-modified time
606 * p : (pattern) Line number of pattern to edit
608 * Returns : JB_ERR_OK on success
609 * JB_ERR_MEMORY on out-of-memory
610 * JB_ERR_CGI_PARAMS if the CGI parameters are not
611 * specified or not valid.
613 *********************************************************************/
614 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
615 struct http_response *rsp,
616 const struct map *parameters)
618 struct map * exports;
620 struct editable_file * file;
621 struct file_line * cur_line;
622 unsigned line_number;
629 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
631 return cgi_error_disabled(csp, rsp);
634 err = get_number_param(csp, parameters, "p", &patternid);
640 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
643 /* No filename specified, can't read file, modified, or out of memory. */
644 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
647 cur_line = file->lines;
649 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
651 cur_line = cur_line->next;
654 if ( (cur_line == NULL)
655 || (line_number != patternid)
657 || (cur_line->type != FILE_LINE_URL))
659 /* Invalid "patternid" parameter */
660 edit_free_file(file);
661 return JB_ERR_CGI_PARAMS;
664 if (NULL == (exports = default_exports(csp, NULL)))
666 edit_free_file(file);
667 return JB_ERR_MEMORY;
670 err = map(exports, "f", 1, file->identifier, 1);
671 if (!err) err = map(exports, "v", 1, file->version_str, 1);
672 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
673 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
675 edit_free_file(file);
683 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
687 /*********************************************************************
689 * Function : edit_write_file
691 * Description : Write a complete file to disk.
694 * 1 : filename = File to write to.
695 * 2 : file = Data structure to write.
697 * Returns : JB_ERR_OK on success
698 * JB_ERR_FILE on error writing to file.
699 * JB_ERR_MEMORY on out of memory
701 *********************************************************************/
702 jb_err edit_write_file(struct editable_file * file)
705 struct file_line * cur_line;
706 struct stat statbuf[1];
707 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
708 digits in time_t, assuming this is a 64-bit
709 machine, plus null terminator, plus one
713 assert(file->filename);
715 #if defined(AMIGA) || defined(__OS2__)
716 if (NULL == (fp = fopen(file->filename, "w")))
718 if (NULL == (fp = fopen(file->filename, "wt")))
719 #endif /* def AMIGA */
724 cur_line = file->lines;
725 while (cur_line != NULL)
729 if (fputs(cur_line->raw, fp) < 0)
737 if (cur_line->prefix)
739 if (fputs(cur_line->prefix, fp) < 0)
745 if (cur_line->unprocessed)
747 /* This should be a single line - sanity check. */
748 assert(NULL == strchr(cur_line->unprocessed, '\r'));
749 assert(NULL == strchr(cur_line->unprocessed, '\n'));
751 if (NULL != strchr(cur_line->unprocessed, '#'))
753 /* Must quote '#' characters */
760 /* Count number of # characters, so we know length of output string */
761 src = cur_line->unprocessed;
762 while (NULL != (src = strchr(src, '#')))
769 /* Allocate new memory for string */
770 len = strlen(cur_line->unprocessed);
771 if (NULL == (str = malloc(len + 1 + numhash)))
773 /* Uh oh, just trashed file! */
775 return JB_ERR_MEMORY;
778 /* Loop through string from end */
779 src = cur_line->unprocessed + len;
780 dest = str + len + numhash;
781 for ( ; len >= 0; len--)
783 if ((*dest-- = *src--) == '#')
787 assert(numhash >= 0);
790 assert(numhash == 0);
791 assert(src + 1 == cur_line->unprocessed);
792 assert(dest + 1 == str);
794 if (fputs(str, fp) < 0)
805 /* Can write without quoting '#' characters. */
806 if (fputs(cur_line->unprocessed, fp) < 0)
812 if (fputs(NEWLINE(file->newline), fp) < 0)
820 /* FIXME: Write data from file->data->whatever */
824 cur_line = cur_line->next;
830 /* Update the version stamp in the file structure, since we just
831 * wrote to the file & changed it's date.
833 if (stat(file->filename, statbuf) < 0)
835 /* Error, probably file not found. */
838 file->version = (unsigned)statbuf->st_mtime;
840 /* Correct file->version_str */
841 freez(file->version_str);
842 snprintf(version_buf, 22, "%u", file->version);
843 version_buf[21] = '\0';
844 file->version_str = strdup(version_buf);
845 if (version_buf == NULL)
847 return JB_ERR_MEMORY;
854 /*********************************************************************
856 * Function : edit_free_file
858 * Description : Free a complete file in memory.
861 * 1 : file = Data structure to free.
865 *********************************************************************/
866 void edit_free_file(struct editable_file * file)
870 /* Silently ignore NULL pointer */
874 edit_free_file_lines(file->lines);
875 freez(file->filename);
876 freez(file->identifier);
877 freez(file->version_str);
879 file->parse_error_text = NULL; /* Statically allocated */
880 file->parse_error = NULL;
886 /*********************************************************************
888 * Function : edit_free_file
890 * Description : Free an entire linked list of file lines.
893 * 1 : first_line = Data structure to free.
897 *********************************************************************/
898 static void edit_free_file_lines(struct file_line * first_line)
900 struct file_line * next_line;
902 while (first_line != NULL)
904 next_line = first_line->next;
905 first_line->next = NULL;
906 freez(first_line->raw);
907 freez(first_line->prefix);
908 freez(first_line->unprocessed);
909 switch(first_line->type)
911 case 0: /* special case if memory zeroed */
912 case FILE_LINE_UNPROCESSED:
913 case FILE_LINE_BLANK:
914 case FILE_LINE_ALIAS_HEADER:
915 case FILE_LINE_SETTINGS_HEADER:
916 case FILE_LINE_DESCRIPTION_HEADER:
917 case FILE_LINE_DESCRIPTION_ENTRY:
918 case FILE_LINE_ALIAS_ENTRY:
920 /* No data is stored for these */
923 case FILE_LINE_ACTION:
924 free_action(first_line->data.action);
927 case FILE_LINE_SETTINGS_ENTRY:
928 freez(first_line->data.setting.name);
929 freez(first_line->data.setting.svalue);
932 /* Should never happen */
936 first_line->type = 0; /* paranoia */
938 first_line = next_line;
943 /*********************************************************************
945 * Function : match_actions_file_header_line
947 * Description : Match an actions file {{header}} line
950 * 1 : line = String from file
951 * 2 : name = Header to match against
953 * Returns : 0 iff they match.
955 *********************************************************************/
956 static int match_actions_file_header_line(const char * line, const char * name)
964 if ((line[0] != '{') || (line[1] != '{'))
970 /* Look for optional whitespace */
971 while ( (*line == ' ') || (*line == '\t') )
976 /* Look for the specified name (case-insensitive) */
978 if (0 != strncmpic(line, name, len))
984 /* Look for optional whitespace */
985 while ( (*line == ' ') || (*line == '\t') )
990 /* Look for "}}" and end of string*/
991 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1001 /*********************************************************************
1003 * Function : match_actions_file_header_line
1005 * Description : Match an actions file {{header}} line
1008 * 1 : line = String from file. Must not start with
1009 * whitespace (else infinite loop!)
1010 * 2 : name = Destination for name
1011 * 2 : name = Destination for value
1013 * Returns : JB_ERR_OK on success
1014 * JB_ERR_MEMORY on out-of-memory
1015 * JB_ERR_PARSE if there's no "=" sign, or if there's
1016 * nothing before the "=" sign (but empty
1017 * values *after* the "=" sign are legal).
1019 *********************************************************************/
1020 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1022 const char * name_end;
1023 const char * value_start;
1029 assert(*line != ' ');
1030 assert(*line != '\t');
1035 value_start = strchr(line, '=');
1036 if ((value_start == NULL) || (value_start == line))
1038 return JB_ERR_PARSE;
1041 name_end = value_start - 1;
1043 /* Eat any whitespace before the '=' */
1044 while ((*name_end == ' ') || (*name_end == '\t'))
1047 * we already know we must have at least 1 non-ws char
1048 * at start of buf - no need to check
1053 name_len = name_end - line + 1; /* Length excluding \0 */
1054 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1056 return JB_ERR_MEMORY;
1058 strncpy(*pname, line, name_len);
1059 (*pname)[name_len] = '\0';
1061 /* Eat any the whitespace after the '=' */
1063 while ((*value_start == ' ') || (*value_start == '\t'))
1068 if (NULL == (*pvalue = strdup(value_start)))
1072 return JB_ERR_MEMORY;
1079 /*********************************************************************
1081 * Function : edit_parse_actions_file
1083 * Description : Parse an actions file in memory.
1085 * Passed linked list must have the "data" member
1086 * zeroed, and must contain valid "next" and
1087 * "unprocessed" fields. The "raw" and "prefix"
1088 * fields are ignored, and "type" is just overwritten.
1090 * Note that on error the file may have been
1094 * 1 : file = Actions file to be parsed in-place.
1096 * Returns : JB_ERR_OK on success
1097 * JB_ERR_MEMORY on out-of-memory
1098 * JB_ERR_PARSE on error
1100 *********************************************************************/
1101 jb_err edit_parse_actions_file(struct editable_file * file)
1103 struct file_line * cur_line;
1105 const char * text; /* Text from a line */
1106 char * name; /* For lines of the form name=value */
1107 char * value; /* For lines of the form name=value */
1108 struct action_alias * alias_list = NULL;
1109 jb_err err = JB_ERR_OK;
1111 /* alias_list contains the aliases defined in this file.
1112 * It might be better to use the "file_line.data" fields
1113 * in the relavent places instead.
1116 cur_line = file->lines;
1118 /* A note about blank line support: Blank lines should only
1119 * ever occur as the last line in the file. This function
1120 * is more forgiving than that - FILE_LINE_BLANK can occur
1124 /* Skip leading blanks. Should only happen if file is
1125 * empty (which is valid, but pointless).
1127 while ( (cur_line != NULL)
1128 && (cur_line->unprocessed[0] == '\0') )
1131 cur_line->type = FILE_LINE_BLANK;
1132 cur_line = cur_line->next;
1135 if ( (cur_line != NULL)
1136 && (cur_line->unprocessed[0] != '{') )
1138 /* File doesn't start with a header */
1139 file->parse_error = cur_line;
1140 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1141 return JB_ERR_PARSE;
1144 if ( (cur_line != NULL) && (0 ==
1145 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1147 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1149 cur_line = cur_line->next;
1150 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1152 if (cur_line->unprocessed[0])
1154 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1156 err = split_line_on_equals(cur_line->unprocessed,
1157 &cur_line->data.setting.name,
1158 &cur_line->data.setting.svalue);
1159 if (err == JB_ERR_MEMORY)
1163 else if (err != JB_ERR_OK)
1165 /* Line does not contain a name=value pair */
1166 file->parse_error = cur_line;
1167 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1168 return JB_ERR_PARSE;
1173 cur_line->type = FILE_LINE_BLANK;
1175 cur_line = cur_line->next;
1179 if ( (cur_line != NULL) && (0 ==
1180 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1182 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1184 cur_line = cur_line->next;
1185 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1187 if (cur_line->unprocessed[0])
1189 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1193 cur_line->type = FILE_LINE_BLANK;
1195 cur_line = cur_line->next;
1199 if ( (cur_line != NULL) && (0 ==
1200 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1202 cur_line->type = FILE_LINE_ALIAS_HEADER;
1204 cur_line = cur_line->next;
1205 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1207 if (cur_line->unprocessed[0])
1209 /* define an alias */
1210 struct action_alias * new_alias;
1212 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1214 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1215 if (err == JB_ERR_MEMORY)
1219 else if (err != JB_ERR_OK)
1221 /* Line does not contain a name=value pair */
1222 file->parse_error = cur_line;
1223 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1224 return JB_ERR_PARSE;
1227 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1232 free_alias_list(alias_list);
1233 return JB_ERR_MEMORY;
1236 err = get_actions(value, alias_list, new_alias->action);
1239 /* Invalid action or out of memory */
1243 free_alias_list(alias_list);
1244 if (err == JB_ERR_MEMORY)
1250 /* Line does not contain a name=value pair */
1251 file->parse_error = cur_line;
1252 file->parse_error_text = "This alias does not specify a valid set of actions.";
1253 return JB_ERR_PARSE;
1259 new_alias->name = name;
1262 new_alias->next = alias_list;
1263 alias_list = new_alias;
1267 cur_line->type = FILE_LINE_BLANK;
1269 cur_line = cur_line->next;
1273 /* Header done, process the main part of the file */
1274 while (cur_line != NULL)
1276 /* At this point, (cur_line->unprocessed[0] == '{') */
1277 assert(cur_line->unprocessed[0] == '{');
1278 text = cur_line->unprocessed + 1;
1279 len = strlen(text) - 1;
1280 if (text[len] != '}')
1282 /* No closing } on header */
1283 free_alias_list(alias_list);
1284 file->parse_error = cur_line;
1285 file->parse_error_text = "Headers starting with '{' must have a "
1286 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1287 "must close with two brackets ('}}').";
1288 return JB_ERR_PARSE;
1293 /* An invalid {{ header. */
1294 free_alias_list(alias_list);
1295 file->parse_error = cur_line;
1296 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1297 "Please remember that the system (two-bracket) headers must "
1298 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1299 "and must appear before any actions (one-bracket) headers. "
1300 "Also note that system headers may not be repeated.";
1301 return JB_ERR_PARSE;
1304 while ( (*text == ' ') || (*text == '\t') )
1310 && ( (text[len - 1] == ' ')
1311 || (text[len - 1] == '\t') ) )
1316 cur_line->type = FILE_LINE_ACTION;
1318 /* Remove {} and make copy */
1319 if (NULL == (value = (char *) malloc(len + 1)))
1322 free_alias_list(alias_list);
1323 return JB_ERR_MEMORY;
1325 strncpy(value, text, len);
1329 err = get_actions(value, alias_list, cur_line->data.action);
1332 /* Invalid action or out of memory */
1334 free_alias_list(alias_list);
1335 if (err == JB_ERR_MEMORY)
1341 /* Line does not contain a name=value pair */
1342 file->parse_error = cur_line;
1343 file->parse_error_text = "This header does not specify a valid set of actions.";
1344 return JB_ERR_PARSE;
1348 /* Done with string - it was clobbered anyway */
1351 /* Process next line */
1352 cur_line = cur_line->next;
1354 /* Loop processing URL patterns */
1355 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1357 if (cur_line->unprocessed[0])
1359 /* Could parse URL here, but this isn't currently needed */
1361 cur_line->type = FILE_LINE_URL;
1365 cur_line->type = FILE_LINE_BLANK;
1367 cur_line = cur_line->next;
1369 } /* End main while(cur_line != NULL) loop */
1371 free_alias_list(alias_list);
1377 /*********************************************************************
1379 * Function : edit_read_file_lines
1381 * Description : Read all the lines of a file into memory.
1382 * Handles whitespace, comments and line continuation.
1385 * 1 : fp = File to read from. On return, this will be
1386 * at EOF but it will not have been closed.
1387 * 2 : pfile = Destination for a linked list of file_lines.
1388 * Will be set to NULL on error.
1390 * Returns : JB_ERR_OK on success
1391 * JB_ERR_MEMORY on out-of-memory
1393 *********************************************************************/
1394 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1396 struct file_line * first_line; /* Keep for return value or to free */
1397 struct file_line * cur_line; /* Current line */
1398 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1406 cur_line = first_line = zalloc(sizeof(struct file_line));
1407 if (cur_line == NULL)
1409 return JB_ERR_MEMORY;
1412 cur_line->type = FILE_LINE_UNPROCESSED;
1414 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1417 /* Out of memory or empty file. */
1418 /* Note that empty file is not an error we propogate up */
1420 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1425 prev_line = cur_line;
1426 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1427 if (cur_line == NULL)
1430 edit_free_file_lines(first_line);
1431 return JB_ERR_MEMORY;
1434 cur_line->type = FILE_LINE_UNPROCESSED;
1436 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1437 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1440 edit_free_file_lines(first_line);
1441 return JB_ERR_MEMORY;
1445 while (rval != JB_ERR_FILE);
1449 /* We allocated one too many - free it */
1450 prev_line->next = NULL;
1453 *pfile = first_line;
1458 /*********************************************************************
1460 * Function : edit_read_file
1462 * Description : Read a complete file into memory.
1463 * Handles CGI parameter parsing. If requested, also
1464 * checks the file's modification timestamp.
1467 * 1 : csp = Current client state (buffers, headers, etc...)
1468 * 2 : parameters = map of cgi parameters.
1469 * 3 : require_version = true to check "ver" parameter.
1470 * 4 : suffix = File extension, e.g. ".action".
1471 * 5 : pfile = Destination for the file. Will be set
1475 * filename : The name of the file to read, without the
1476 * path or ".action" extension.
1477 * ver : (Only if require_version is nonzero)
1478 * Timestamp of the actions file. If wrong, this
1479 * function fails with JB_ERR_MODIFIED.
1481 * Returns : JB_ERR_OK on success
1482 * JB_ERR_MEMORY on out-of-memory
1483 * JB_ERR_CGI_PARAMS if "filename" was not specified
1485 * JB_ERR_FILE if the file cannot be opened or
1487 * JB_ERR_MODIFIED if version checking was requested and
1488 * failed - the file was modified outside
1489 * of this CGI editor instance.
1491 *********************************************************************/
1492 jb_err edit_read_file(struct client_state *csp,
1493 const struct map *parameters,
1494 int require_version,
1496 struct editable_file **pfile)
1498 struct file_line * lines;
1502 const char * identifier;
1503 struct editable_file * file;
1504 unsigned version = 0;
1505 struct stat statbuf[1];
1506 char version_buf[22];
1507 int newline = NEWLINE_UNKNOWN;
1515 err = get_file_name_param(csp, parameters, "f", suffix,
1516 &filename, &identifier);
1522 if (stat(filename, statbuf) < 0)
1524 /* Error, probably file not found. */
1528 version = (unsigned) statbuf->st_mtime;
1530 if (require_version)
1532 unsigned specified_version;
1533 err = get_number_param(csp, parameters, "v", &specified_version);
1540 if (version != specified_version)
1542 return JB_ERR_MODIFIED;
1546 #if defined(AMIGA) || defined(__OS2__)
1547 if (NULL == (fp = fopen(filename,"r")))
1549 if (NULL == (fp = fopen(filename,"rt")))
1550 #endif /* def AMIGA */
1556 err = edit_read_file_lines(fp, &lines, &newline);
1566 file = (struct editable_file *) zalloc(sizeof(*file));
1570 edit_free_file_lines(lines);
1574 file->lines = lines;
1575 file->newline = newline;
1576 file->filename = filename;
1577 file->version = version;
1578 file->identifier = url_encode(identifier);
1580 if (file->identifier == NULL)
1582 edit_free_file(file);
1583 return JB_ERR_MEMORY;
1586 /* Correct file->version_str */
1587 freez(file->version_str);
1588 snprintf(version_buf, 22, "%u", file->version);
1589 version_buf[21] = '\0';
1590 file->version_str = strdup(version_buf);
1591 if (version_buf == NULL)
1593 edit_free_file(file);
1594 return JB_ERR_MEMORY;
1602 /*********************************************************************
1604 * Function : edit_read_actions_file
1606 * Description : Read a complete actions file into memory.
1607 * Handles CGI parameter parsing. If requested, also
1608 * checks the file's modification timestamp.
1610 * If this function detects an error in the categories
1611 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1612 * then it handles it by filling in the specified
1613 * response structure and returning JB_ERR_FILE.
1616 * 1 : csp = Current client state (buffers, headers, etc...)
1617 * 2 : rsp = HTTP response. Only filled in on error.
1618 * 2 : parameters = map of cgi parameters.
1619 * 3 : require_version = true to check "ver" parameter.
1620 * 4 : pfile = Destination for the file. Will be set
1624 * filename : The name of the actions file to read, without the
1625 * path or ".action" extension.
1626 * ver : (Only if require_version is nonzero)
1627 * Timestamp of the actions file. If wrong, this
1628 * function fails with JB_ERR_MODIFIED.
1630 * Returns : JB_ERR_OK on success
1631 * JB_ERR_MEMORY on out-of-memory
1632 * JB_ERR_CGI_PARAMS if "filename" was not specified
1634 * JB_ERR_FILE if the file does not contain valid data,
1635 * or if file cannot be opened or
1636 * contains no data, or if version
1637 * checking was requested and failed.
1639 *********************************************************************/
1640 jb_err edit_read_actions_file(struct client_state *csp,
1641 struct http_response *rsp,
1642 const struct map *parameters,
1643 int require_version,
1644 struct editable_file **pfile)
1647 struct editable_file *file;
1655 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1658 /* Try to handle if possible */
1659 if (err == JB_ERR_FILE)
1661 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1663 else if (err == JB_ERR_MODIFIED)
1665 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1667 if (err == JB_ERR_OK)
1670 * Signal to higher-level CGI code that there was a problem but we
1671 * handled it, they should just return JB_ERR_OK.
1678 err = edit_parse_actions_file(file);
1681 if (err == JB_ERR_PARSE)
1683 err = cgi_error_parse(csp, rsp, file);
1684 if (err == JB_ERR_OK)
1687 * Signal to higher-level CGI code that there was a problem but we
1688 * handled it, they should just return JB_ERR_OK.
1693 edit_free_file(file);
1702 /*********************************************************************
1704 * Function : get_file_name_param
1706 * Description : Get the name of the file to edit from the parameters
1707 * passed to a CGI function. This function handles
1708 * security checks such as blocking urls containing
1709 * "/" or ".", prepending the config file directory,
1710 * and adding the specified suffix.
1712 * (This is an essential security check, otherwise
1713 * users may be able to pass "../../../etc/passwd"
1714 * and overwrite the password file [linux], "prn:"
1715 * and print random data [Windows], etc...)
1717 * This function only allows filenames contining the
1718 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1719 * That's probably too restrictive but at least it's
1723 * 1 : csp = Current client state (buffers, headers, etc...)
1724 * 2 : parameters = map of cgi parameters
1725 * 3 : param_name = The name of the parameter to read
1726 * 4 : suffix = File extension, e.g. ".actions"
1727 * 5 : pfilename = destination for full filename. Caller
1728 * free()s. Set to NULL on error.
1729 * 6 : pparam = destination for partial filename,
1730 * suitable for use in another URL. Allocated as part
1731 * of the map "parameters", so don't free it.
1732 * Set to NULL if not specified.
1734 * Returns : JB_ERR_OK on success
1735 * JB_ERR_MEMORY on out-of-memory
1736 * JB_ERR_CGI_PARAMS if "filename" was not specified
1739 *********************************************************************/
1740 static jb_err get_file_name_param(struct client_state *csp,
1741 const struct map *parameters,
1742 const char *param_name,
1745 const char **pparam)
1749 #if 0 /* Patch to make 3.0.0 work properly. */
1751 #endif /* 0 - Patch to make 3.0.0 work properly. */
1765 param = lookup(parameters, param_name);
1768 return JB_ERR_CGI_PARAMS;
1773 len = strlen(param);
1774 if (len >= FILENAME_MAX)
1777 return JB_ERR_CGI_PARAMS;
1780 /* Check every character to see if it's legal */
1782 while ((ch = *s++) != '\0')
1784 if ( ((ch < 'A') || (ch > 'Z'))
1785 && ((ch < 'a') || (ch > 'z'))
1786 && ((ch < '0') || (ch > '9'))
1790 /* Probable hack attempt. */
1791 return JB_ERR_CGI_PARAMS;
1796 * FIXME Following is a hack to make 3.0.0 work properly.
1797 * Change "#if 0" --> "#if 1" below when we have modular action
1801 #if 0 /* Patch to make 3.0.0 work properly. */
1802 /* Append extension */
1803 name = malloc(len + strlen(suffix) + 1);
1806 return JB_ERR_MEMORY;
1808 strcpy(name, param);
1809 strcpy(name + len, suffix);
1812 fullpath = make_path(csp->config->confdir, name);
1814 #else /* 1 - Patch to make 3.0.0 work properly. */
1815 if ((csp->actions_list == NULL)
1816 || (csp->actions_list->filename == NULL))
1818 return JB_ERR_CGI_PARAMS;
1821 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1822 ? strdup(csp->actions_list->filename) : NULL);
1823 #endif /* 1 - Patch to make 3.0.0 work properly. */
1824 if (fullpath == NULL)
1826 return JB_ERR_MEMORY;
1830 *pfilename = fullpath;
1836 /*********************************************************************
1838 * Function : get_number_param
1840 * Description : Get a non-negative integer from the parameters
1841 * passed to a CGI function.
1844 * 1 : csp = Current client state (buffers, headers, etc...)
1845 * 2 : parameters = map of cgi parameters
1846 * 3 : name = Name of CGI parameter to read
1847 * 4 : pvalue = destination for value.
1848 * Set to -1 on error.
1850 * Returns : JB_ERR_OK on success
1851 * JB_ERR_MEMORY on out-of-memory
1852 * JB_ERR_CGI_PARAMS if the parameter was not specified
1855 *********************************************************************/
1856 static jb_err get_number_param(struct client_state *csp,
1857 const struct map *parameters,
1872 param = lookup(parameters, name);
1875 return JB_ERR_CGI_PARAMS;
1878 /* We don't use atoi because I want to check this carefully... */
1881 while ((ch = *param++) != '\0')
1883 if ((ch < '0') || (ch > '9'))
1885 return JB_ERR_CGI_PARAMS;
1892 * <limits.h> defines UINT_MAX
1894 * (UINT_MAX - ch) / 10 is the largest number that
1895 * can be safely multiplied by 10 then have ch added.
1897 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1899 return JB_ERR_CGI_PARAMS;
1902 value = value * 10 + ch;
1912 /*********************************************************************
1914 * Function : get_url_spec_param
1916 * Description : Get a URL pattern from the parameters
1917 * passed to a CGI function. Removes leading/trailing
1918 * spaces and validates it.
1921 * 1 : csp = Current client state (buffers, headers, etc...)
1922 * 2 : parameters = map of cgi parameters
1923 * 3 : name = Name of CGI parameter to read
1924 * 4 : pvalue = destination for value. Will be malloc()'d.
1925 * Set to NULL on error.
1927 * Returns : JB_ERR_OK on success
1928 * JB_ERR_MEMORY on out-of-memory
1929 * JB_ERR_CGI_PARAMS if the parameter was not specified
1932 *********************************************************************/
1933 static jb_err get_url_spec_param(struct client_state *csp,
1934 const struct map *parameters,
1938 const char *orig_param;
1941 struct url_spec compiled[1];
1951 orig_param = lookup(parameters, name);
1954 return JB_ERR_CGI_PARAMS;
1957 /* Copy and trim whitespace */
1958 param = strdup(orig_param);
1961 return JB_ERR_MEMORY;
1965 /* Must be non-empty, and can't allow 1st character to be '{' */
1966 if (param[0] == '\0' || param[0] == '{')
1969 return JB_ERR_CGI_PARAMS;
1972 /* Check for embedded newlines */
1973 for (s = param; *s != '\0'; s++)
1975 if ((*s == '\r') || (*s == '\n'))
1978 return JB_ERR_CGI_PARAMS;
1982 /* Check that regex is valid */
1987 return JB_ERR_MEMORY;
1989 err = create_url_spec(compiled, s);
1994 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1996 free_url_spec(compiled);
1998 if (param[strlen(param) - 1] == '\\')
2001 * Must protect trailing '\\' from becoming line continuation character.
2002 * Two methods: 1) If it's a domain only, add a trailing '/'.
2003 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2005 if (strchr(param, '/') == NULL)
2007 err = string_append(¶m, "/");
2011 err = string_append(¶m, "(?:)");
2018 /* Check that the modified regex is valid */
2023 return JB_ERR_MEMORY;
2025 err = create_url_spec(compiled, s);
2030 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2032 free_url_spec(compiled);
2039 /*********************************************************************
2041 * Function : map_radio
2043 * Description : Map a set of radio button values. E.g. if you have
2044 * 3 radio buttons, declare them as:
2045 * <option type="radio" name="xyz" @xyz-a@>
2046 * <option type="radio" name="xyz" @xyz-b@>
2047 * <option type="radio" name="xyz" @xyz-c@>
2048 * Then map one of the @xyz-?@ variables to "checked"
2049 * and all the others to empty by calling:
2050 * map_radio(exports, "xyz", "abc", sel)
2051 * Where 'sel' is 'a', 'b', or 'c'.
2054 * 1 : exports = Exports map to modify.
2055 * 2 : optionname = name for map
2056 * 3 : values = null-terminated list of values;
2057 * 4 : value = Selected value.
2059 * CGI Parameters : None
2061 * Returns : JB_ERR_OK on success
2062 * JB_ERR_MEMORY on out-of-memory
2064 *********************************************************************/
2065 static jb_err map_radio(struct map * exports,
2066 const char * optionname,
2067 const char * values,
2079 len = strlen(optionname);
2080 buf = malloc(len + 3);
2083 return JB_ERR_MEMORY;
2086 strcpy(buf, optionname);
2091 while ((c = *values++) != '\0')
2096 if (map(exports, buf, 1, "", 1))
2099 return JB_ERR_MEMORY;
2105 if (map(exports, buf, 0, "checked", 1))
2108 return JB_ERR_MEMORY;
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;
2725 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2727 return cgi_error_disabled(csp, rsp);
2730 err = get_number_param(csp, parameters, "s", §ionid);
2736 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2739 /* No filename specified, can't read file, modified, or out of memory. */
2740 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2743 cur_line = file->lines;
2745 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2747 cur_line = cur_line->next;
2750 if ( (cur_line == NULL)
2751 || (line_number != sectionid)
2753 || (cur_line->type != FILE_LINE_ACTION))
2755 /* Invalid "sectionid" parameter */
2756 edit_free_file(file);
2757 return JB_ERR_CGI_PARAMS;
2760 if (NULL == (exports = default_exports(csp, NULL)))
2762 edit_free_file(file);
2763 return JB_ERR_MEMORY;
2766 err = map(exports, "f", 1, file->identifier, 1);
2767 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2768 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2770 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2772 edit_free_file(file);
2780 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2784 /*********************************************************************
2786 * Function : cgi_edit_actions_submit
2788 * Description : CGI function that actually edits the Actions list.
2791 * 1 : csp = Current client state (buffers, headers, etc...)
2792 * 2 : rsp = http_response data structure for output
2793 * 3 : parameters = map of cgi parameters
2795 * CGI Parameters : None
2797 * Returns : JB_ERR_OK on success
2798 * JB_ERR_MEMORY on out-of-memory
2799 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2800 * specified or not valid.
2802 *********************************************************************/
2803 jb_err cgi_edit_actions_submit(struct client_state *csp,
2804 struct http_response *rsp,
2805 const struct map *parameters)
2811 struct editable_file * file;
2812 struct file_line * cur_line;
2813 unsigned line_number;
2817 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2819 return cgi_error_disabled(csp, rsp);
2822 err = get_number_param(csp, parameters, "s", §ionid);
2828 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2831 /* No filename specified, can't read file, modified, or out of memory. */
2832 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2835 cur_line = file->lines;
2837 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2839 cur_line = cur_line->next;
2842 if ( (cur_line == NULL)
2843 || (line_number != sectionid)
2845 || (cur_line->type != FILE_LINE_ACTION))
2847 /* Invalid "sectionid" parameter */
2848 edit_free_file(file);
2849 return JB_ERR_CGI_PARAMS;
2852 err = actions_from_radio(parameters, cur_line->data.action);
2856 edit_free_file(file);
2860 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2863 edit_free_file(file);
2864 return JB_ERR_MEMORY;
2867 len = strlen(actiontext);
2871 * Empty action - must special-case this.
2872 * Simply setting len to 1 is sufficient...
2877 if (NULL == (newtext = malloc(len + 2)))
2881 edit_free_file(file);
2882 return JB_ERR_MEMORY;
2884 strcpy(newtext, actiontext);
2888 newtext[len + 1] = '\0';
2890 freez(cur_line->raw);
2891 freez(cur_line->unprocessed);
2892 cur_line->unprocessed = newtext;
2894 err = edit_write_file(file);
2897 /* Error writing file */
2898 edit_free_file(file);
2902 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2903 string_append(&target, file->identifier);
2905 edit_free_file(file);
2910 return JB_ERR_MEMORY;
2913 rsp->status = strdup("302 Local Redirect from Junkbuster");
2914 if (rsp->status == NULL)
2917 return JB_ERR_MEMORY;
2919 err = enlist_unique_header(rsp->headers, "Location", target);
2926 /*********************************************************************
2928 * Function : cgi_edit_actions_url
2930 * Description : CGI function that actually edits a URL pattern in
2934 * 1 : csp = Current client state (buffers, headers, etc...)
2935 * 2 : rsp = http_response data structure for output
2936 * 3 : parameters = map of cgi parameters
2939 * filename : Identifies the file to edit
2940 * ver : File's last-modified time
2941 * section : Line number of section to edit
2942 * pattern : Line number of pattern to edit
2943 * newval : New value for pattern
2945 * Returns : JB_ERR_OK on success
2946 * JB_ERR_MEMORY on out-of-memory
2947 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2948 * specified or not valid.
2950 *********************************************************************/
2951 jb_err cgi_edit_actions_url(struct client_state *csp,
2952 struct http_response *rsp,
2953 const struct map *parameters)
2957 struct editable_file * file;
2958 struct file_line * cur_line;
2959 unsigned line_number;
2963 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2965 return cgi_error_disabled(csp, rsp);
2968 err = get_number_param(csp, parameters, "p", &patternid);
2975 return JB_ERR_CGI_PARAMS;
2978 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
2984 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2987 /* No filename specified, can't read file, modified, or out of memory. */
2989 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2993 cur_line = file->lines;
2995 while ((cur_line != NULL) && (line_number < patternid))
2997 cur_line = cur_line->next;
3001 if ( (cur_line == NULL)
3002 || (cur_line->type != FILE_LINE_URL))
3004 /* Invalid "patternid" parameter */
3006 edit_free_file(file);
3007 return JB_ERR_CGI_PARAMS;
3010 /* At this point, the line to edit is in cur_line */
3012 freez(cur_line->raw);
3013 freez(cur_line->unprocessed);
3014 cur_line->unprocessed = new_pattern;
3016 err = edit_write_file(file);
3019 /* Error writing file */
3020 edit_free_file(file);
3024 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3025 string_append(&target, file->identifier);
3027 edit_free_file(file);
3032 return JB_ERR_MEMORY;
3035 rsp->status = strdup("302 Local Redirect from Junkbuster");
3036 if (rsp->status == NULL)
3039 return JB_ERR_MEMORY;
3041 err = enlist_unique_header(rsp->headers, "Location", target);
3048 /*********************************************************************
3050 * Function : cgi_edit_actions_add_url
3052 * Description : CGI function that actually adds a URL pattern to
3056 * 1 : csp = Current client state (buffers, headers, etc...)
3057 * 2 : rsp = http_response data structure for output
3058 * 3 : parameters = map of cgi parameters
3061 * filename : Identifies the file to edit
3062 * ver : File's last-modified time
3063 * section : Line number of section to edit
3064 * newval : New pattern
3066 * Returns : JB_ERR_OK on success
3067 * JB_ERR_MEMORY on out-of-memory
3068 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3069 * specified or not valid.
3071 *********************************************************************/
3072 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3073 struct http_response *rsp,
3074 const struct map *parameters)
3078 struct file_line * new_line;
3079 struct editable_file * file;
3080 struct file_line * cur_line;
3081 unsigned line_number;
3085 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3087 return cgi_error_disabled(csp, rsp);
3090 err = get_number_param(csp, parameters, "s", §ionid);
3097 return JB_ERR_CGI_PARAMS;
3100 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3106 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3109 /* No filename specified, can't read file, modified, or out of memory. */
3111 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3115 cur_line = file->lines;
3117 while ((cur_line != NULL) && (line_number < sectionid))
3119 cur_line = cur_line->next;
3123 if ( (cur_line == NULL)
3124 || (cur_line->type != FILE_LINE_ACTION))
3126 /* Invalid "sectionid" parameter */
3128 edit_free_file(file);
3129 return JB_ERR_CGI_PARAMS;
3132 /* At this point, the section header is in cur_line - add after this. */
3134 /* Allocate the new line */
3135 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3136 if (new_line == NULL)
3139 edit_free_file(file);
3140 return JB_ERR_MEMORY;
3143 /* Fill in the data members of the new line */
3144 new_line->raw = NULL;
3145 new_line->prefix = NULL;
3146 new_line->unprocessed = new_pattern;
3147 new_line->type = FILE_LINE_URL;
3149 /* Link new_line into the list, after cur_line */
3150 new_line->next = cur_line->next;
3151 cur_line->next = new_line;
3153 /* Done making changes, now commit */
3155 err = edit_write_file(file);
3158 /* Error writing file */
3159 edit_free_file(file);
3163 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3164 string_append(&target, file->identifier);
3166 edit_free_file(file);
3171 return JB_ERR_MEMORY;
3174 rsp->status = strdup("302 Local Redirect from Junkbuster");
3175 if (rsp->status == NULL)
3178 return JB_ERR_MEMORY;
3180 err = enlist_unique_header(rsp->headers, "Location", target);
3187 /*********************************************************************
3189 * Function : cgi_edit_actions_remove_url
3191 * Description : CGI function that actually removes a URL pattern from
3195 * 1 : csp = Current client state (buffers, headers, etc...)
3196 * 2 : rsp = http_response data structure for output
3197 * 3 : parameters = map of cgi parameters
3200 * f : (filename) Identifies the file to edit
3201 * v : (version) File's last-modified time
3202 * p : (pattern) Line number of pattern to remove
3204 * Returns : JB_ERR_OK on success
3205 * JB_ERR_MEMORY on out-of-memory
3206 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3207 * specified or not valid.
3209 *********************************************************************/
3210 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3211 struct http_response *rsp,
3212 const struct map *parameters)
3215 struct editable_file * file;
3216 struct file_line * cur_line;
3217 struct file_line * prev_line;
3218 unsigned line_number;
3222 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3224 return cgi_error_disabled(csp, rsp);
3227 err = get_number_param(csp, parameters, "p", &patternid);
3233 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3236 /* No filename specified, can't read file, modified, or out of memory. */
3237 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3242 cur_line = file->lines;
3244 while ((cur_line != NULL) && (line_number < patternid))
3246 prev_line = cur_line;
3247 cur_line = cur_line->next;
3251 if ( (cur_line == NULL)
3252 || (prev_line == NULL)
3253 || (cur_line->type != FILE_LINE_URL))
3255 /* Invalid "patternid" parameter */
3256 edit_free_file(file);
3257 return JB_ERR_CGI_PARAMS;
3260 /* At this point, the line to remove is in cur_line, and the previous
3261 * one is in prev_line
3264 /* Unlink cur_line */
3265 prev_line->next = cur_line->next;
3266 cur_line->next = NULL;
3269 edit_free_file_lines(cur_line);
3271 err = edit_write_file(file);
3274 /* Error writing file */
3275 edit_free_file(file);
3279 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3280 string_append(&target, file->identifier);
3282 edit_free_file(file);
3287 return JB_ERR_MEMORY;
3290 rsp->status = strdup("302 Local Redirect from Junkbuster");
3291 if (rsp->status == NULL)
3294 return JB_ERR_MEMORY;
3296 err = enlist_unique_header(rsp->headers, "Location", target);
3303 /*********************************************************************
3305 * Function : cgi_edit_actions_section_remove
3307 * Description : CGI function that actually removes a whole section from
3308 * the actions file. The section must be empty first
3309 * (else JB_ERR_CGI_PARAMS).
3312 * 1 : csp = Current client state (buffers, headers, etc...)
3313 * 2 : rsp = http_response data structure for output
3314 * 3 : parameters = map of cgi parameters
3317 * f : (filename) Identifies the file to edit
3318 * v : (version) File's last-modified time
3319 * s : (section) Line number of section to edit
3321 * Returns : JB_ERR_OK on success
3322 * JB_ERR_MEMORY on out-of-memory
3323 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3324 * specified or not valid.
3326 *********************************************************************/
3327 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3328 struct http_response *rsp,
3329 const struct map *parameters)
3332 struct editable_file * file;
3333 struct file_line * cur_line;
3334 struct file_line * prev_line;
3335 unsigned line_number;
3339 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3341 return cgi_error_disabled(csp, rsp);
3344 err = get_number_param(csp, parameters, "s", §ionid);
3350 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3353 /* No filename specified, can't read file, modified, or out of memory. */
3354 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3358 cur_line = file->lines;
3361 while ((cur_line != NULL) && (line_number < sectionid))
3363 prev_line = cur_line;
3364 cur_line = cur_line->next;
3368 if ( (cur_line == NULL)
3369 || (cur_line->type != FILE_LINE_ACTION) )
3371 /* Invalid "sectionid" parameter */
3372 edit_free_file(file);
3373 return JB_ERR_CGI_PARAMS;
3376 if ( (cur_line->next != NULL)
3377 && (cur_line->next->type == FILE_LINE_URL) )
3379 /* Section not empty. */
3380 edit_free_file(file);
3381 return JB_ERR_CGI_PARAMS;
3384 /* At this point, the line to remove is in cur_line, and the previous
3385 * one is in prev_line
3388 /* Unlink cur_line */
3389 if (prev_line == NULL)
3391 /* Removing the first line from the file */
3392 file->lines = cur_line->next;
3396 prev_line->next = cur_line->next;
3398 cur_line->next = NULL;
3401 edit_free_file_lines(cur_line);
3403 err = edit_write_file(file);
3406 /* Error writing file */
3407 edit_free_file(file);
3411 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3412 string_append(&target, file->identifier);
3414 edit_free_file(file);
3419 return JB_ERR_MEMORY;
3422 rsp->status = strdup("302 Local Redirect from Junkbuster");
3423 if (rsp->status == NULL)
3426 return JB_ERR_MEMORY;
3428 err = enlist_unique_header(rsp->headers, "Location", target);
3435 /*********************************************************************
3437 * Function : cgi_edit_actions_section_add
3439 * Description : CGI function that adds a new empty section to
3443 * 1 : csp = Current client state (buffers, headers, etc...)
3444 * 2 : rsp = http_response data structure for output
3445 * 3 : parameters = map of cgi parameters
3448 * f : (filename) Identifies the file to edit
3449 * v : (version) File's last-modified time
3450 * s : (section) Line number of section to add after, 0 for
3453 * Returns : JB_ERR_OK on success
3454 * JB_ERR_MEMORY on out-of-memory
3455 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3456 * specified or not valid.
3458 *********************************************************************/
3459 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3460 struct http_response *rsp,
3461 const struct map *parameters)
3464 struct file_line * new_line;
3466 struct editable_file * file;
3467 struct file_line * cur_line;
3468 unsigned line_number;
3472 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3474 return cgi_error_disabled(csp, rsp);
3477 err = get_number_param(csp, parameters, "s", §ionid);
3483 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3486 /* No filename specified, can't read file, modified, or out of memory. */
3487 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3491 cur_line = file->lines;
3495 /* Add to start of file */
3496 if (cur_line != NULL)
3498 /* There's something in the file, find the line before the first
3501 while ( (cur_line->next != NULL)
3502 && (cur_line->next->type != FILE_LINE_ACTION) )
3504 cur_line = cur_line->next;
3511 /* Add after stated section. */
3512 while ((cur_line != NULL) && (line_number < sectionid))
3514 cur_line = cur_line->next;
3518 if ( (cur_line == NULL)
3519 || (cur_line->type != FILE_LINE_ACTION))
3521 /* Invalid "sectionid" parameter */
3522 edit_free_file(file);
3523 return JB_ERR_CGI_PARAMS;
3526 /* Skip through the section to find the last line in it. */
3527 while ( (cur_line->next != NULL)
3528 && (cur_line->next->type != FILE_LINE_ACTION) )
3530 cur_line = cur_line->next;
3535 /* At this point, the last line in the previous section is in cur_line
3536 * - add after this. (Or if we need to add as the first line, cur_line
3540 new_text = strdup("{}");
3541 if (NULL == new_text)
3543 edit_free_file(file);
3544 return JB_ERR_MEMORY;
3547 /* Allocate the new line */
3548 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3549 if (new_line == NULL)
3552 edit_free_file(file);
3553 return JB_ERR_MEMORY;
3556 /* Fill in the data members of the new line */
3557 new_line->raw = NULL;
3558 new_line->prefix = NULL;
3559 new_line->unprocessed = new_text;
3560 new_line->type = FILE_LINE_ACTION;
3562 if (cur_line != NULL)
3564 /* Link new_line into the list, after cur_line */
3565 new_line->next = cur_line->next;
3566 cur_line->next = new_line;
3570 /* Link new_line into the list, as first line */
3571 new_line->next = file->lines;
3572 file->lines = new_line;
3575 /* Done making changes, now commit */
3577 err = edit_write_file(file);
3580 /* Error writing file */
3581 edit_free_file(file);
3585 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3586 string_append(&target, file->identifier);
3588 edit_free_file(file);
3593 return JB_ERR_MEMORY;
3596 rsp->status = strdup("302 Local Redirect from Junkbuster");
3597 if (rsp->status == NULL)
3600 return JB_ERR_MEMORY;
3602 err = enlist_unique_header(rsp->headers, "Location", target);
3609 /*********************************************************************
3611 * Function : cgi_edit_actions_section_swap
3613 * Description : CGI function that swaps the order of two sections
3614 * in the actions file. Note that this CGI can actually
3615 * swap any two arbitrary sections, but the GUI interface
3616 * currently only allows consecutive sections to be
3620 * 1 : csp = Current client state (buffers, headers, etc...)
3621 * 2 : rsp = http_response data structure for output
3622 * 3 : parameters = map of cgi parameters
3625 * f : (filename) Identifies the file to edit
3626 * v : (version) File's last-modified time
3627 * s1 : (section1) Line number of first section to swap
3628 * s2 : (section2) Line number of second section to swap
3630 * Returns : JB_ERR_OK on success
3631 * JB_ERR_MEMORY on out-of-memory
3632 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3633 * specified or not valid.
3635 *********************************************************************/
3636 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3637 struct http_response *rsp,
3638 const struct map *parameters)
3642 struct editable_file * file;
3643 struct file_line * cur_line;
3644 struct file_line * prev_line;
3645 struct file_line * line_before_section1;
3646 struct file_line * line_start_section1;
3647 struct file_line * line_end_section1;
3648 struct file_line * line_after_section1;
3649 struct file_line * line_before_section2;
3650 struct file_line * line_start_section2;
3651 struct file_line * line_end_section2;
3652 struct file_line * line_after_section2;
3653 unsigned line_number;
3657 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3659 return cgi_error_disabled(csp, rsp);
3662 err = get_number_param(csp, parameters, "s1", §ion1);
3663 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3669 if (section1 > section2)
3671 unsigned temp = section2;
3672 section2 = section1;
3676 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3679 /* No filename specified, can't read file, modified, or out of memory. */
3680 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3683 /* Start at the beginning... */
3685 cur_line = file->lines;
3688 /* ... find section1 ... */
3689 while ((cur_line != NULL) && (line_number < section1))
3691 prev_line = cur_line;
3692 cur_line = cur_line->next;
3696 if ( (cur_line == NULL)
3697 || (cur_line->type != FILE_LINE_ACTION) )
3699 /* Invalid "section1" parameter */
3700 edit_free_file(file);
3701 return JB_ERR_CGI_PARAMS;
3704 /* If no-op, we've validated params and can skip the rest. */
3705 if (section1 != section2)
3707 /* ... find the end of section1 ... */
3708 line_before_section1 = prev_line;
3709 line_start_section1 = cur_line;
3712 prev_line = cur_line;
3713 cur_line = cur_line->next;
3716 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3717 line_end_section1 = prev_line;
3718 line_after_section1 = cur_line;
3720 /* ... find section2 ... */
3721 while ((cur_line != NULL) && (line_number < section2))
3723 prev_line = cur_line;
3724 cur_line = cur_line->next;
3728 if ( (cur_line == NULL)
3729 || (cur_line->type != FILE_LINE_ACTION) )
3731 /* Invalid "section2" parameter */
3732 edit_free_file(file);
3733 return JB_ERR_CGI_PARAMS;
3736 /* ... find the end of section2 ... */
3737 line_before_section2 = prev_line;
3738 line_start_section2 = cur_line;
3741 prev_line = cur_line;
3742 cur_line = cur_line->next;
3745 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3746 line_end_section2 = prev_line;
3747 line_after_section2 = cur_line;
3749 /* Now have all the pointers we need. Do the swap. */
3751 /* Change the pointer to section1 to point to section2 instead */
3752 if (line_before_section1 == NULL)
3754 file->lines = line_start_section2;
3758 line_before_section1->next = line_start_section2;
3761 if (line_before_section2 == line_end_section1)
3763 /* Consecutive sections */
3764 line_end_section2->next = line_start_section1;
3768 line_end_section2->next = line_after_section1;
3769 line_before_section2->next = line_start_section1;
3772 /* Set the pointer from the end of section1 to the rest of the file */
3773 line_end_section1->next = line_after_section2;
3775 err = edit_write_file(file);
3778 /* Error writing file */
3779 edit_free_file(file);
3782 } /* END if (section1 != section2) */
3784 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3785 string_append(&target, file->identifier);
3787 edit_free_file(file);
3792 return JB_ERR_MEMORY;
3795 rsp->status = strdup("302 Local Redirect from Junkbuster");
3796 if (rsp->status == NULL)
3799 return JB_ERR_MEMORY;
3801 err = enlist_unique_header(rsp->headers, "Location", target);
3808 /*********************************************************************
3810 * Function : cgi_toggle
3812 * Description : CGI function that adds a new empty section to
3816 * 1 : csp = Current client state (buffers, headers, etc...)
3817 * 2 : rsp = http_response data structure for output
3818 * 3 : parameters = map of cgi parameters
3821 * set : If present, how to change toggle setting:
3822 * "enable", "disable", "toggle", or none (default).
3823 * mini : If present, use mini reply template.
3825 * Returns : JB_ERR_OK on success
3826 * JB_ERR_MEMORY on out-of-memory
3828 *********************************************************************/
3829 jb_err cgi_toggle(struct client_state *csp,
3830 struct http_response *rsp,
3831 const struct map *parameters)
3833 struct map *exports;
3835 const char *template_name;
3842 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3844 return cgi_error_disabled(csp, rsp);
3847 if (NULL == (exports = default_exports(csp, "toggle")))
3849 return JB_ERR_MEMORY;
3852 mode = *(lookup(parameters, "set"));
3859 else if (mode == 'd')
3864 else if (mode == 't')
3867 g_bToggleIJB = !g_bToggleIJB;
3870 err = map_conditional(exports, "enabled", g_bToggleIJB);
3877 template_name = (*(lookup(parameters, "mini"))
3881 return template_fill_for_cgi(csp, template_name, exports, rsp);
3885 /*********************************************************************
3887 * Function : actions_to_radio
3889 * Description : Converts a actionsfile entry into settings for
3890 * radio buttons and edit boxes on a HTML form.
3893 * 1 : exports = List of substitutions to add to.
3894 * 2 : action = Action to read
3896 * Returns : JB_ERR_OK on success
3897 * JB_ERR_MEMORY on out-of-memory
3899 *********************************************************************/
3900 static jb_err actions_to_radio(struct map * exports,
3901 const struct action_spec *action)
3903 unsigned mask = action->mask;
3904 unsigned add = action->add;
3912 mask = action->mask;
3915 /* sanity - prevents "-feature +feature" */
3919 #define DEFINE_ACTION_BOOL(name, bit) \
3920 if (!(mask & bit)) \
3922 current_mode = 'n'; \
3924 else if (add & bit) \
3926 current_mode = 'y'; \
3930 current_mode = 'x'; \
3932 if (map_radio(exports, name, "ynx", current_mode)) \
3934 return JB_ERR_MEMORY; \
3937 #define DEFINE_ACTION_STRING(name, bit, index) \
3938 DEFINE_ACTION_BOOL(name, bit); \
3941 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
3944 checked = !strcmp(action->string[index], value); \
3948 checked = is_default; \
3950 mapped_param |= checked; \
3951 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
3953 return JB_ERR_MEMORY; \
3956 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
3957 if (map(exports, name "-param-custom", 1, \
3958 ((!mapped_param) ? "checked" : ""), 1)) \
3960 return JB_ERR_MEMORY; \
3962 if (map(exports, name "-param", 1, \
3963 (((add & bit) && !mapped_param) ? \
3964 action->string[index] : default_val), 1)) \
3966 return JB_ERR_MEMORY; \
3969 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
3970 if (map(exports, name "-param", 1, \
3971 ((add & bit) ? action->string[index] : default_val), 1)) \
3973 return JB_ERR_MEMORY; \
3976 #define DEFINE_ACTION_MULTI(name, index) \
3977 if (action->multi_add[index]->first) \
3979 current_mode = 'y'; \
3981 else if (action->multi_remove_all[index]) \
3983 current_mode = 'n'; \
3985 else if (action->multi_remove[index]->first) \
3987 current_mode = 'y'; \
3991 current_mode = 'x'; \
3993 if (map_radio(exports, name, "ynx", current_mode)) \
3995 return JB_ERR_MEMORY; \
3998 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4000 #include "actionlist.h"
4002 #undef DEFINE_ACTION_MULTI
4003 #undef DEFINE_ACTION_STRING
4004 #undef DEFINE_ACTION_BOOL
4005 #undef DEFINE_ACTION_ALIAS
4006 #undef DEFINE_CGI_PARAM_CUSTOM
4007 #undef DEFINE_CGI_PARAM_RADIO
4008 #undef DEFINE_CGI_PARAM_NO_RADIO
4014 /*********************************************************************
4016 * Function : javascriptify
4018 * Description : Converts a string into a form JavaScript will like.
4020 * Netscape 4's JavaScript sucks - it doesn't use
4021 * "id" parameters, so you have to set the "name"
4022 * used to submit a form element to something JavaScript
4023 * will like. (Or access the elements by index in an
4024 * array. That array contains >60 elements and will
4025 * be changed whenever we add a new action to the
4026 * editor, so I'm NOT going to use indexes that have
4027 * to be figured out by hand.)
4029 * Currently the only thing we have to worry about
4030 * is "-" ==> "_" conversion.
4032 * This is a length-preserving operation so it is
4033 * carried out in-place, no memory is allocated
4037 * 1 : identifier = String to make JavaScript-friendly.
4041 *********************************************************************/
4042 static void javascriptify(char * identifier)
4044 char * p = identifier;
4045 while (NULL != (p = strchr(p, '-')))
4052 /*********************************************************************
4054 * Function : actions_from_radio
4056 * Description : Converts a map of parameters passed to a CGI function
4057 * into an actionsfile entry.
4060 * 1 : parameters = parameters to the CGI call
4061 * 2 : action = Action to change. Must be valid before
4062 * the call, actions not specified will be
4065 * Returns : JB_ERR_OK on success
4066 * JB_ERR_MEMORY on out-of-memory
4068 *********************************************************************/
4069 static jb_err actions_from_radio(const struct map * parameters,
4070 struct action_spec *action)
4072 static int first_time = 1;
4076 const char * js_name;
4081 /* Statics are generally a potential race condition,
4082 * but in this case we're safe and don't need semaphores.
4083 * Be careful if you modify this function.
4087 #define JAVASCRIPTIFY(dest_var, string) \
4089 static char js_name_arr[] = string; \
4092 javascriptify(js_name_arr); \
4094 dest_var = js_name_arr; \
4097 #define DEFINE_ACTION_BOOL(name, bit) \
4098 JAVASCRIPTIFY(js_name, name); \
4099 param = lookup(parameters, js_name); \
4100 ch = ijb_toupper(param[0]); \
4103 action->add |= bit; \
4104 action->mask |= bit; \
4106 else if (ch == 'N') \
4108 action->add &= ~bit; \
4109 action->mask &= ~bit; \
4111 else if (ch == 'X') \
4113 action->add &= ~bit; \
4114 action->mask |= bit; \
4117 #define DEFINE_ACTION_STRING(name, bit, index) \
4118 JAVASCRIPTIFY(js_name, name); \
4119 param = lookup(parameters, js_name); \
4120 ch = ijb_toupper(param[0]); \
4123 JAVASCRIPTIFY(js_name, name "-mode"); \
4124 param = lookup(parameters, js_name); \
4125 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4127 JAVASCRIPTIFY(js_name, name "-param"); \
4128 param = lookup(parameters, js_name); \
4130 if (*param != '\0') \
4132 if (NULL == (param_dup = strdup(param))) \
4134 return JB_ERR_MEMORY; \
4136 freez(action->string[index]); \
4137 action->add |= bit; \
4138 action->mask |= bit; \
4139 action->string[index] = param_dup; \
4142 else if (ch == 'N') \
4144 if (action->add & bit) \
4146 freez(action->string[index]); \
4148 action->add &= ~bit; \
4149 action->mask &= ~bit; \
4151 else if (ch == 'X') \
4153 if (action->add & bit) \
4155 freez(action->string[index]); \
4157 action->add &= ~bit; \
4158 action->mask |= bit; \
4161 #define DEFINE_ACTION_MULTI(name, index) \
4162 JAVASCRIPTIFY(js_name, name); \
4163 param = lookup(parameters, js_name); \
4164 ch = ijb_toupper((int)param[0]); \
4169 else if (ch == 'N') \
4171 list_remove_all(action->multi_add[index]); \
4172 list_remove_all(action->multi_remove[index]); \
4173 action->multi_remove_all[index] = 1; \
4175 else if (ch == 'X') \
4177 list_remove_all(action->multi_add[index]); \
4178 list_remove_all(action->multi_remove[index]); \
4179 action->multi_remove_all[index] = 0; \
4182 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4184 #include "actionlist.h"
4186 #undef DEFINE_ACTION_MULTI
4187 #undef DEFINE_ACTION_STRING
4188 #undef DEFINE_ACTION_BOOL
4189 #undef DEFINE_ACTION_ALIAS
4190 #undef JAVASCRIPTIFY
4198 #endif /* def FEATURE_CGI_EDIT_ACTIONS */