1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.10 2002/01/23 00:22:59 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.10 2002/01/23 00:22:59 jongfoster
46 * Adding new function cgi_edit_actions_section_swap(), to reorder
49 * Adding get_url_spec_param() to get a validated URL pattern.
51 * Moving edit_read_line() out of this file and into loaders.c.
53 * Adding missing html_encode() to many CGI functions.
55 * Moving the functions that #include actionlist.h to the end of the file,
56 * because the Visual C++ 97 debugger gets extremely confused if you try
57 * to debug any code that comes after them in the file.
59 * Major optimizations in cgi_edit_actions_list() to reduce the size of
60 * the generated HTML (down 40% from 550k to 304k), with major side-effects
61 * throughout the editor and templates. In particular, the length of the
62 * URLs throughout the editor has been drastically reduced, by cutting
63 * paramater names down to 1 character and CGI names down to 3-4
64 * characters, by removing all non-essential CGI paramaters even at the
65 * expense of having to re-read the actions file for the most trivial
66 * page, and by using relative rather than absolute URLs. This means
67 * that this (typical example):
69 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
70 * filename=ijb&ver=1011487572&section=12&pattern=13
71 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
75 * <a href="eau?f=ijb&v=1011487572&p=13">
77 * Revision 1.9 2002/01/17 20:56:22 jongfoster
78 * Replacing hard references to the URL of the config interface
79 * with #defines from project.h
81 * Revision 1.8 2001/11/30 23:35:51 jongfoster
82 * Renaming actionsfile to ijb.action
84 * Revision 1.7 2001/11/13 00:28:24 jongfoster
85 * - Renaming parameters from edit-actions-for-url so that they only
86 * contain legal JavaScript characters. If we wanted to write
87 * JavaScript that worked with Netscape 4, this is nessacery.
88 * (Note that at the moment the JavaScript doesn't actually work
89 * with Netscape 4, but now this is purely a template issue, not
90 * one affecting code).
91 * - Adding new CGIs for use by non-JavaScript browsers:
92 * edit-actions-url-form
93 * edit-actions-add-url-form
94 * edit-actions-remove-url-form
97 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
98 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
99 * by and __OS2__ ifdef.
101 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
102 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
103 * threads to call select() simultaneously. So, it's time to do a real, live,
104 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
105 * (native). Both versions will work, but using __OS2__ offers multi-threading.
107 * Revision 1.4 2001/10/23 21:48:19 jongfoster
108 * Cleaning up error handling in CGI functions - they now send back
109 * a HTML error page and should never cause a FATAL error. (Fixes one
110 * potential source of "denial of service" attacks).
112 * CGI actions file editor that works and is actually useful.
114 * Ability to toggle JunkBuster remotely using a CGI call.
116 * You can turn off both the above features in the main configuration
117 * file, e.g. if you are running a multi-user proxy.
119 * Revision 1.3 2001/10/14 22:12:49 jongfoster
120 * New version of CGI-based actionsfile editor.
121 * Major changes, including:
122 * - Completely new file parser and file output routines
123 * - edit-actions CGI renamed edit-actions-for-url
124 * - All CGIs now need a filename parameter, except for...
125 * - New CGI edit-actions which doesn't need a filename,
126 * to allow you to start the editor up.
127 * - edit-actions-submit now works, and now automatically
128 * redirects you back to the main edit-actions-list handler.
130 * Revision 1.2 2001/09/16 17:05:14 jongfoster
131 * Removing unused #include showarg.h
133 * Revision 1.1 2001/09/16 15:47:37 jongfoster
134 * First version of CGI-based edit interface. This is very much a
135 * work-in-progress, and you can't actually use it to edit anything
136 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
137 * to have any effect.
140 **********************************************************************/
146 * FIXME: Following includes copied from cgi.c - which are actually needed?
151 #include <sys/types.h>
156 #include <sys/stat.h>
159 #define snprintf _snprintf
160 #endif /* def _WIN32 */
165 #include "cgisimple.h"
169 #include "miscutil.h"
173 /* loadcfg.h is for g_bToggleIJB only */
174 #include "urlmatch.h"
176 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
179 #ifdef FEATURE_CGI_EDIT_ACTIONS
183 struct file_line * next;
191 struct action_spec action[1];
200 /* Add more data types here... e.g.
203 struct url_spec url[1];
207 struct action_spec action[1];
216 #define FILE_LINE_UNPROCESSED 1
217 #define FILE_LINE_BLANK 2
218 #define FILE_LINE_ALIAS_HEADER 3
219 #define FILE_LINE_ALIAS_ENTRY 4
220 #define FILE_LINE_ACTION 5
221 #define FILE_LINE_URL 6
222 #define FILE_LINE_SETTINGS_HEADER 7
223 #define FILE_LINE_SETTINGS_ENTRY 8
224 #define FILE_LINE_DESCRIPTION_HEADER 9
225 #define FILE_LINE_DESCRIPTION_ENTRY 10
230 struct file_line * lines;
231 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
232 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
233 /* Pre-encoded with url_encode() for ease of use. */
234 const char * version_str; /* Last modification time, as a string. For CGI param */
235 /* Can be used in URL without using url_param(). */
236 unsigned version; /* Last modification time - prevents chaos with
237 * the browser's "back" button. Note that this is a
238 * time_t cast to an unsigned. When comparing, always
239 * cast the time_t to an unsigned, and *NOT* vice-versa.
240 * This may lose the top few bits, but they're not
241 * significant anyway.
243 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
244 * Note that changing this after the file has been
245 * read in will cause a mess.
247 struct file_line * parse_error; /* On parse error, this is the offending line. */
248 const char * parse_error_text; /* On parse error, this is the problem.
249 * (Statically allocated) */
252 /* FIXME: Following non-static functions should be prototyped in .h or made static */
254 /* Functions to read and write arbitrary config files */
255 jb_err edit_read_file(struct client_state *csp,
256 const struct map *parameters,
259 struct editable_file **pfile);
260 jb_err edit_write_file(struct editable_file * file);
261 void edit_free_file(struct editable_file * file);
263 /* Functions to read and write actions files */
264 jb_err edit_parse_actions_file(struct editable_file * file);
265 jb_err edit_read_actions_file(struct client_state *csp,
266 struct http_response *rsp,
267 const struct map *parameters,
269 struct editable_file **pfile);
272 jb_err cgi_error_modified(struct client_state *csp,
273 struct http_response *rsp,
274 const char *filename);
275 jb_err cgi_error_parse(struct client_state *csp,
276 struct http_response *rsp,
277 struct editable_file *file);
278 jb_err cgi_error_file(struct client_state *csp,
279 struct http_response *rsp,
280 const char *filename);
281 jb_err cgi_error_disabled(struct client_state *csp,
282 struct http_response *rsp);
284 /* Internal arbitrary config file support functions */
285 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
286 static void edit_free_file_lines(struct file_line * first_line);
288 /* Internal actions file support functions */
289 static int match_actions_file_header_line(const char * line, const char * name);
290 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
292 /* Internal parameter parsing functions */
293 static jb_err get_file_name_param(struct client_state *csp,
294 const struct map *parameters,
295 const char *param_name,
298 const char **pparam);
299 static jb_err get_number_param(struct client_state *csp,
300 const struct map *parameters,
303 static jb_err get_url_spec_param(struct client_state *csp,
304 const struct map *parameters,
308 /* Internal actionsfile <==> HTML conversion functions */
309 static jb_err map_radio(struct map * exports,
310 const char * optionname,
313 static jb_err actions_to_radio(struct map * exports,
314 const struct action_spec *action);
315 static jb_err actions_from_radio(const struct map * parameters,
316 struct action_spec *action);
319 static jb_err map_copy_parameter_html(struct map *out,
320 const struct map *in,
322 #if 0 /* unused function */
323 static jb_err map_copy_parameter_url(struct map *out,
324 const struct map *in,
326 #endif /* unused function */
328 /*********************************************************************
330 * Function : map_copy_parameter_html
332 * Description : Copy a CGI parameter from one map to another, HTML
336 * 1 : out = target map
337 * 2 : in = source map
338 * 3 : name = name of cgi parameter to copy
340 * Returns : JB_ERR_OK on success
341 * JB_ERR_MEMORY on out-of-memory
342 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
345 *********************************************************************/
346 static jb_err map_copy_parameter_html(struct map *out,
347 const struct map *in,
357 value = lookup(in, name);
358 err = map(out, name, 1, html_encode(value), 0);
365 else if (*value == '\0')
367 return JB_ERR_CGI_PARAMS;
376 #if 0 /* unused function */
377 /*********************************************************************
379 * Function : map_copy_parameter_html
381 * Description : Copy a CGI parameter from one map to another, URL
385 * 1 : out = target map
386 * 2 : in = source map
387 * 3 : name = name of cgi parameter to copy
389 * Returns : JB_ERR_OK on success
390 * JB_ERR_MEMORY on out-of-memory
391 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
394 *********************************************************************/
395 static jb_err map_copy_parameter_url(struct map *out,
396 const struct map *in,
406 value = lookup(in, name);
407 err = map(out, name, 1, url_encode(value), 0);
414 else if (*value == '\0')
416 return JB_ERR_CGI_PARAMS;
423 #endif /* 0 - unused function */
425 /*********************************************************************
427 * Function : cgi_edit_actions_url_form
429 * Description : CGI function that displays a form for
433 * 1 : csp = Current client state (buffers, headers, etc...)
434 * 2 : rsp = http_response data structure for output
435 * 3 : parameters = map of cgi parameters
438 * f : (filename) Identifies the file to edit
439 * v : (version) File's last-modified time
440 * p : (pattern) Line number of pattern to edit
442 * Returns : JB_ERR_OK on success
443 * JB_ERR_MEMORY on out-of-memory
444 * JB_ERR_CGI_PARAMS if the CGI parameters are not
445 * specified or not valid.
447 *********************************************************************/
448 jb_err cgi_edit_actions_url_form(struct client_state *csp,
449 struct http_response *rsp,
450 const struct map *parameters)
452 struct map * exports;
454 struct editable_file * file;
455 struct file_line * cur_line;
456 unsigned line_number;
463 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
465 return cgi_error_disabled(csp, rsp);
468 err = get_number_param(csp, parameters, "p", &patternid);
474 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
477 /* No filename specified, can't read file, modified, or out of memory. */
478 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
481 cur_line = file->lines;
483 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
485 cur_line = cur_line->next;
488 if ( (cur_line == NULL)
489 || (line_number != patternid)
491 || (cur_line->type != FILE_LINE_URL))
493 /* Invalid "patternid" parameter */
494 edit_free_file(file);
495 return JB_ERR_CGI_PARAMS;
498 if (NULL == (exports = default_exports(csp, NULL)))
500 edit_free_file(file);
501 return JB_ERR_MEMORY;
504 err = map(exports, "f", 1, file->identifier, 1);
505 if (!err) err = map(exports, "v", 1, file->version_str, 1);
506 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
507 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
509 edit_free_file(file);
517 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
521 /*********************************************************************
523 * Function : cgi_edit_actions_add_url_form
525 * Description : CGI function that displays a form for
529 * 1 : csp = Current client state (buffers, headers, etc...)
530 * 2 : rsp = http_response data structure for output
531 * 3 : parameters = map of cgi parameters
534 * f : (filename) Identifies the file to edit
535 * v : (version) File's last-modified time
536 * s : (section) Line number of section to edit
538 * Returns : JB_ERR_OK on success
539 * JB_ERR_MEMORY on out-of-memory
540 * JB_ERR_CGI_PARAMS if the CGI parameters are not
541 * specified or not valid.
543 *********************************************************************/
544 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
545 struct http_response *rsp,
546 const struct map *parameters)
555 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
557 return cgi_error_disabled(csp, rsp);
560 if (NULL == (exports = default_exports(csp, NULL)))
562 return JB_ERR_MEMORY;
565 err = map_copy_parameter_html(exports, parameters, "f");
566 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
567 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
575 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
579 /*********************************************************************
581 * Function : cgi_edit_actions_remove_url_form
583 * Description : CGI function that displays a form for
587 * 1 : csp = Current client state (buffers, headers, etc...)
588 * 2 : rsp = http_response data structure for output
589 * 3 : parameters = map of cgi parameters
592 * f : (filename) Identifies the file to edit
593 * v : (version) File's last-modified time
594 * p : (pattern) Line number of pattern to edit
596 * Returns : JB_ERR_OK on success
597 * JB_ERR_MEMORY on out-of-memory
598 * JB_ERR_CGI_PARAMS if the CGI parameters are not
599 * specified or not valid.
601 *********************************************************************/
602 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
603 struct http_response *rsp,
604 const struct map *parameters)
606 struct map * exports;
608 struct editable_file * file;
609 struct file_line * cur_line;
610 unsigned line_number;
617 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
619 return cgi_error_disabled(csp, rsp);
622 err = get_number_param(csp, parameters, "p", &patternid);
628 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
631 /* No filename specified, can't read file, modified, or out of memory. */
632 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
635 cur_line = file->lines;
637 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
639 cur_line = cur_line->next;
642 if ( (cur_line == NULL)
643 || (line_number != patternid)
645 || (cur_line->type != FILE_LINE_URL))
647 /* Invalid "patternid" parameter */
648 edit_free_file(file);
649 return JB_ERR_CGI_PARAMS;
652 if (NULL == (exports = default_exports(csp, NULL)))
654 edit_free_file(file);
655 return JB_ERR_MEMORY;
658 err = map(exports, "f", 1, file->identifier, 1);
659 if (!err) err = map(exports, "v", 1, file->version_str, 1);
660 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
661 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
663 edit_free_file(file);
671 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
675 /*********************************************************************
677 * Function : edit_write_file
679 * Description : Write a complete file to disk.
682 * 1 : filename = File to write to.
683 * 2 : file = Data structure to write.
685 * Returns : JB_ERR_OK on success
686 * JB_ERR_FILE on error writing to file.
687 * JB_ERR_MEMORY on out of memory
689 *********************************************************************/
690 jb_err edit_write_file(struct editable_file * file)
693 struct file_line * cur_line;
694 struct stat statbuf[1];
695 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
696 digits in time_t, assuming this is a 64-bit
697 machine, plus null terminator, plus one
701 assert(file->filename);
703 if (NULL == (fp = fopen(file->filename, "wt")))
708 cur_line = file->lines;
709 while (cur_line != NULL)
713 if (fputs(cur_line->raw, fp) < 0)
721 if (cur_line->prefix)
723 if (fputs(cur_line->prefix, fp) < 0)
729 if (cur_line->unprocessed)
731 /* This should be a single line - sanity check. */
732 assert(NULL == strchr(cur_line->unprocessed, '\r'));
733 assert(NULL == strchr(cur_line->unprocessed, '\n'));
735 if (NULL != strchr(cur_line->unprocessed, '#'))
737 /* Must quote '#' characters */
744 /* Count number of # characters, so we know length of output string */
745 src = cur_line->unprocessed;
746 while (NULL != (src = strchr(src, '#')))
753 /* Allocate new memory for string */
754 len = strlen(cur_line->unprocessed);
755 if (NULL == (str = malloc(len + 1 + numhash)))
757 /* Uh oh, just trashed file! */
759 return JB_ERR_MEMORY;
762 /* Loop through string from end */
763 src = cur_line->unprocessed + len;
764 dest = str + len + numhash;
765 for ( ; len >= 0; len--)
767 if ((*dest-- = *src--) == '#')
771 assert(numhash >= 0);
774 assert(numhash == 0);
775 assert(src + 1 == cur_line->unprocessed);
776 assert(dest + 1 == str);
778 if (fputs(str, fp) < 0)
789 /* Can write without quoting '#' characters. */
790 if (fputs(cur_line->unprocessed, fp) < 0)
796 if (fputs(NEWLINE(file->newline), fp) < 0)
804 /* FIXME: Write data from file->data->whatever */
808 cur_line = cur_line->next;
814 /* Update the version stamp in the file structure, since we just
815 * wrote to the file & changed it's date.
817 if (stat(file->filename, statbuf) < 0)
819 /* Error, probably file not found. */
822 file->version = (unsigned)statbuf->st_mtime;
824 /* Correct file->version_str */
825 freez(file->version_str);
826 snprintf(version_buf, 22, "%u", file->version);
827 version_buf[21] = '\0';
828 file->version_str = strdup(version_buf);
829 if (version_buf == NULL)
831 return JB_ERR_MEMORY;
838 /*********************************************************************
840 * Function : edit_free_file
842 * Description : Free a complete file in memory.
845 * 1 : file = Data structure to free.
849 *********************************************************************/
850 void edit_free_file(struct editable_file * file)
854 /* Silently ignore NULL pointer */
858 edit_free_file_lines(file->lines);
859 freez(file->filename);
860 freez(file->identifier);
861 freez(file->version_str);
863 file->parse_error_text = NULL; /* Statically allocated */
864 file->parse_error = NULL;
870 /*********************************************************************
872 * Function : edit_free_file
874 * Description : Free an entire linked list of file lines.
877 * 1 : first_line = Data structure to free.
881 *********************************************************************/
882 static void edit_free_file_lines(struct file_line * first_line)
884 struct file_line * next_line;
886 while (first_line != NULL)
888 next_line = first_line->next;
889 first_line->next = NULL;
890 freez(first_line->raw);
891 freez(first_line->prefix);
892 freez(first_line->unprocessed);
893 switch(first_line->type)
895 case 0: /* special case if memory zeroed */
896 case FILE_LINE_UNPROCESSED:
897 case FILE_LINE_BLANK:
898 case FILE_LINE_ALIAS_HEADER:
899 case FILE_LINE_SETTINGS_HEADER:
900 case FILE_LINE_DESCRIPTION_HEADER:
901 case FILE_LINE_DESCRIPTION_ENTRY:
902 case FILE_LINE_ALIAS_ENTRY:
904 /* No data is stored for these */
907 case FILE_LINE_ACTION:
908 free_action(first_line->data.action);
911 case FILE_LINE_SETTINGS_ENTRY:
912 freez(first_line->data.setting.name);
913 freez(first_line->data.setting.svalue);
916 /* Should never happen */
920 first_line->type = 0; /* paranoia */
922 first_line = next_line;
927 /*********************************************************************
929 * Function : match_actions_file_header_line
931 * Description : Match an actions file {{header}} line
934 * 1 : line - String from file
935 * 2 : name - Header to match against
937 * Returns : 0 iff they match.
939 *********************************************************************/
940 static int match_actions_file_header_line(const char * line, const char * name)
948 if ((line[0] != '{') || (line[1] != '{'))
954 /* Look for optional whitespace */
955 while ( (*line == ' ') || (*line == '\t') )
960 /* Look for the specified name (case-insensitive) */
962 if (0 != strncmpic(line, name, len))
968 /* Look for optional whitespace */
969 while ( (*line == ' ') || (*line == '\t') )
974 /* Look for "}}" and end of string*/
975 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
985 /*********************************************************************
987 * Function : match_actions_file_header_line
989 * Description : Match an actions file {{header}} line
992 * 1 : line - String from file. Must not start with
993 * whitespace (else infinite loop!)
994 * 2 : name - Destination for name
995 * 2 : name - Destination for value
997 * Returns : JB_ERR_OK on success
998 * JB_ERR_MEMORY on out-of-memory
999 * JB_ERR_PARSE if there's no "=" sign, or if there's
1000 * nothing before the "=" sign (but empty
1001 * values *after* the "=" sign are legal).
1003 *********************************************************************/
1004 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1006 const char * name_end;
1007 const char * value_start;
1013 assert(*line != ' ');
1014 assert(*line != '\t');
1019 value_start = strchr(line, '=');
1020 if ((value_start == NULL) || (value_start == line))
1022 return JB_ERR_PARSE;
1025 name_end = value_start - 1;
1027 /* Eat any whitespace before the '=' */
1028 while ((*name_end == ' ') || (*name_end == '\t'))
1031 * we already know we must have at least 1 non-ws char
1032 * at start of buf - no need to check
1037 name_len = name_end - line + 1; /* Length excluding \0 */
1038 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1040 return JB_ERR_MEMORY;
1042 strncpy(*pname, line, name_len);
1043 (*pname)[name_len] = '\0';
1045 /* Eat any the whitespace after the '=' */
1047 while ((*value_start == ' ') || (*value_start == '\t'))
1052 if (NULL == (*pvalue = strdup(value_start)))
1056 return JB_ERR_MEMORY;
1063 /*********************************************************************
1065 * Function : edit_parse_actions_file
1067 * Description : Parse an actions file in memory.
1069 * Passed linked list must have the "data" member
1070 * zeroed, and must contain valid "next" and
1071 * "unprocessed" fields. The "raw" and "prefix"
1072 * fields are ignored, and "type" is just overwritten.
1074 * Note that on error the file may have been
1078 * 1 : file = Actions file to be parsed in-place.
1080 * Returns : JB_ERR_OK on success
1081 * JB_ERR_MEMORY on out-of-memory
1082 * JB_ERR_PARSE on error
1084 *********************************************************************/
1085 jb_err edit_parse_actions_file(struct editable_file * file)
1087 struct file_line * cur_line;
1089 const char * text; /* Text from a line */
1090 char * name; /* For lines of the form name=value */
1091 char * value; /* For lines of the form name=value */
1092 struct action_alias * alias_list = NULL;
1093 jb_err err = JB_ERR_OK;
1095 /* alias_list contains the aliases defined in this file.
1096 * It might be better to use the "file_line.data" fields
1097 * in the relavent places instead.
1100 cur_line = file->lines;
1102 /* A note about blank line support: Blank lines should only
1103 * ever occur as the last line in the file. This function
1104 * is more forgiving than that - FILE_LINE_BLANK can occur
1108 /* Skip leading blanks. Should only happen if file is
1109 * empty (which is valid, but pointless).
1111 while ( (cur_line != NULL)
1112 && (cur_line->unprocessed[0] == '\0') )
1115 cur_line->type = FILE_LINE_BLANK;
1116 cur_line = cur_line->next;
1119 if ( (cur_line != NULL)
1120 && (cur_line->unprocessed[0] != '{') )
1122 /* File doesn't start with a header */
1123 file->parse_error = cur_line;
1124 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1125 return JB_ERR_PARSE;
1128 if ( (cur_line != NULL) && (0 ==
1129 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1131 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1133 cur_line = cur_line->next;
1134 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1136 if (cur_line->unprocessed[0])
1138 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1140 err = split_line_on_equals(cur_line->unprocessed,
1141 &cur_line->data.setting.name,
1142 &cur_line->data.setting.svalue);
1143 if (err == JB_ERR_MEMORY)
1147 else if (err != JB_ERR_OK)
1149 /* Line does not contain a name=value pair */
1150 file->parse_error = cur_line;
1151 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1152 return JB_ERR_PARSE;
1157 cur_line->type = FILE_LINE_BLANK;
1159 cur_line = cur_line->next;
1163 if ( (cur_line != NULL) && (0 ==
1164 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1166 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1168 cur_line = cur_line->next;
1169 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1171 if (cur_line->unprocessed[0])
1173 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1177 cur_line->type = FILE_LINE_BLANK;
1179 cur_line = cur_line->next;
1183 if ( (cur_line != NULL) && (0 ==
1184 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1186 cur_line->type = FILE_LINE_ALIAS_HEADER;
1188 cur_line = cur_line->next;
1189 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1191 if (cur_line->unprocessed[0])
1193 /* define an alias */
1194 struct action_alias * new_alias;
1196 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1198 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1199 if (err == JB_ERR_MEMORY)
1203 else if (err != JB_ERR_OK)
1205 /* Line does not contain a name=value pair */
1206 file->parse_error = cur_line;
1207 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1208 return JB_ERR_PARSE;
1211 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1216 free_alias_list(alias_list);
1217 return JB_ERR_MEMORY;
1220 err = get_actions(value, alias_list, new_alias->action);
1223 /* Invalid action or out of memory */
1227 free_alias_list(alias_list);
1228 if (err == JB_ERR_MEMORY)
1234 /* Line does not contain a name=value pair */
1235 file->parse_error = cur_line;
1236 file->parse_error_text = "This alias does not specify a valid set of actions.";
1237 return JB_ERR_PARSE;
1243 new_alias->name = name;
1246 new_alias->next = alias_list;
1247 alias_list = new_alias;
1251 cur_line->type = FILE_LINE_BLANK;
1253 cur_line = cur_line->next;
1257 /* Header done, process the main part of the file */
1258 while (cur_line != NULL)
1260 /* At this point, (cur_line->unprocessed[0] == '{') */
1261 assert(cur_line->unprocessed[0] == '{');
1262 text = cur_line->unprocessed + 1;
1263 len = strlen(text) - 1;
1264 if (text[len] != '}')
1266 /* No closing } on header */
1267 free_alias_list(alias_list);
1268 file->parse_error = cur_line;
1269 file->parse_error_text = "Headers starting with '{' must have a "
1270 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1271 "must close with two brackets ('}}').";
1272 return JB_ERR_PARSE;
1277 /* An invalid {{ header. */
1278 free_alias_list(alias_list);
1279 file->parse_error = cur_line;
1280 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1281 "Please remember that the system (two-bracket) headers must "
1282 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1283 "and must appear before any actions (one-bracket) headers. "
1284 "Also note that system headers may not be repeated.";
1285 return JB_ERR_PARSE;
1288 while ( (*text == ' ') || (*text == '\t') )
1294 && ( (text[len - 1] == ' ')
1295 || (text[len - 1] == '\t') ) )
1300 cur_line->type = FILE_LINE_ACTION;
1302 /* Remove {} and make copy */
1303 if (NULL == (value = (char *) malloc(len + 1)))
1306 free_alias_list(alias_list);
1307 return JB_ERR_MEMORY;
1309 strncpy(value, text, len);
1313 err = get_actions(value, alias_list, cur_line->data.action);
1316 /* Invalid action or out of memory */
1318 free_alias_list(alias_list);
1319 if (err == JB_ERR_MEMORY)
1325 /* Line does not contain a name=value pair */
1326 file->parse_error = cur_line;
1327 file->parse_error_text = "This header does not specify a valid set of actions.";
1328 return JB_ERR_PARSE;
1332 /* Done with string - it was clobbered anyway */
1335 /* Process next line */
1336 cur_line = cur_line->next;
1338 /* Loop processing URL patterns */
1339 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1341 if (cur_line->unprocessed[0])
1343 /* Could parse URL here, but this isn't currently needed */
1345 cur_line->type = FILE_LINE_URL;
1349 cur_line->type = FILE_LINE_BLANK;
1351 cur_line = cur_line->next;
1353 } /* End main while(cur_line != NULL) loop */
1355 free_alias_list(alias_list);
1361 /*********************************************************************
1363 * Function : edit_read_file_lines
1365 * Description : Read all the lines of a file into memory.
1366 * Handles whitespace, comments and line continuation.
1369 * 1 : fp = File to read from. On return, this will be
1370 * at EOF but it will not have been closed.
1371 * 2 : pfile = Destination for a linked list of file_lines.
1372 * Will be set to NULL on error.
1374 * Returns : JB_ERR_OK on success
1375 * JB_ERR_MEMORY on out-of-memory
1377 *********************************************************************/
1378 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1380 struct file_line * first_line; /* Keep for return value or to free */
1381 struct file_line * cur_line; /* Current line */
1382 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1390 cur_line = first_line = zalloc(sizeof(struct file_line));
1391 if (cur_line == NULL)
1393 return JB_ERR_MEMORY;
1396 cur_line->type = FILE_LINE_UNPROCESSED;
1398 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1401 /* Out of memory or empty file. */
1402 /* Note that empty file is not an error we propogate up */
1404 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1409 prev_line = cur_line;
1410 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1411 if (cur_line == NULL)
1414 edit_free_file_lines(first_line);
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);
1421 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1424 edit_free_file_lines(first_line);
1425 return JB_ERR_MEMORY;
1429 while (rval != JB_ERR_FILE);
1433 /* We allocated one too many - free it */
1434 prev_line->next = NULL;
1437 *pfile = first_line;
1442 /*********************************************************************
1444 * Function : edit_read_file
1446 * Description : Read a complete file into memory.
1447 * Handles CGI parameter parsing. If requested, also
1448 * checks the file's modification timestamp.
1451 * 1 : csp = Current client state (buffers, headers, etc...)
1452 * 2 : parameters = map of cgi parameters.
1453 * 3 : require_version = true to check "ver" parameter.
1454 * 4 : suffix = File extension, e.g. ".action".
1455 * 5 : pfile = Destination for the file. Will be set
1459 * filename : The name of the file to read, without the
1460 * path or ".action" extension.
1461 * ver : (Only if require_version is nonzero)
1462 * Timestamp of the actions file. If wrong, this
1463 * function fails with JB_ERR_MODIFIED.
1465 * Returns : JB_ERR_OK on success
1466 * JB_ERR_MEMORY on out-of-memory
1467 * JB_ERR_CGI_PARAMS if "filename" was not specified
1469 * JB_ERR_FILE if the file cannot be opened or
1471 * JB_ERR_MODIFIED if version checking was requested and
1472 * failed - the file was modified outside
1473 * of this CGI editor instance.
1475 *********************************************************************/
1476 jb_err edit_read_file(struct client_state *csp,
1477 const struct map *parameters,
1478 int require_version,
1480 struct editable_file **pfile)
1482 struct file_line * lines;
1486 const char * identifier;
1487 struct editable_file * file;
1488 unsigned version = 0;
1489 struct stat statbuf[1];
1490 char version_buf[22];
1491 int newline = NEWLINE_UNKNOWN;
1499 err = get_file_name_param(csp, parameters, "f", suffix,
1500 &filename, &identifier);
1506 if (stat(filename, statbuf) < 0)
1508 /* Error, probably file not found. */
1512 version = (unsigned) statbuf->st_mtime;
1514 if (require_version)
1516 unsigned specified_version;
1517 err = get_number_param(csp, parameters, "v", &specified_version);
1524 if (version != specified_version)
1526 return JB_ERR_MODIFIED;
1530 if (NULL == (fp = fopen(filename,"rt")))
1536 err = edit_read_file_lines(fp, &lines, &newline);
1546 file = (struct editable_file *) zalloc(sizeof(*file));
1550 edit_free_file_lines(lines);
1554 file->lines = lines;
1555 file->newline = newline;
1556 file->filename = filename;
1557 file->version = version;
1558 file->identifier = url_encode(identifier);
1560 if (file->identifier == NULL)
1562 edit_free_file(file);
1563 return JB_ERR_MEMORY;
1566 /* Correct file->version_str */
1567 freez(file->version_str);
1568 snprintf(version_buf, 22, "%u", file->version);
1569 version_buf[21] = '\0';
1570 file->version_str = strdup(version_buf);
1571 if (version_buf == NULL)
1573 edit_free_file(file);
1574 return JB_ERR_MEMORY;
1582 /*********************************************************************
1584 * Function : edit_read_actions_file
1586 * Description : Read a complete actions file into memory.
1587 * Handles CGI parameter parsing. If requested, also
1588 * checks the file's modification timestamp.
1590 * If this function detects an error in the categories
1591 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1592 * then it handles it by filling in the specified
1593 * response structure and returning JB_ERR_FILE.
1596 * 1 : csp = Current client state (buffers, headers, etc...)
1597 * 2 : rsp = HTTP response. Only filled in on error.
1598 * 2 : parameters = map of cgi parameters.
1599 * 3 : require_version = true to check "ver" parameter.
1600 * 4 : pfile = Destination for the file. Will be set
1604 * filename : The name of the actions file to read, without the
1605 * path or ".action" extension.
1606 * ver : (Only if require_version is nonzero)
1607 * Timestamp of the actions file. If wrong, this
1608 * function fails with JB_ERR_MODIFIED.
1610 * Returns : JB_ERR_OK on success
1611 * JB_ERR_MEMORY on out-of-memory
1612 * JB_ERR_CGI_PARAMS if "filename" was not specified
1614 * JB_ERR_FILE if the file does not contain valid data,
1615 * or if file cannot be opened or
1616 * contains no data, or if version
1617 * checking was requested and failed.
1619 *********************************************************************/
1620 jb_err edit_read_actions_file(struct client_state *csp,
1621 struct http_response *rsp,
1622 const struct map *parameters,
1623 int require_version,
1624 struct editable_file **pfile)
1627 struct editable_file *file;
1635 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1638 /* Try to handle if possible */
1639 if (err == JB_ERR_FILE)
1641 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1643 else if (err == JB_ERR_MODIFIED)
1645 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1647 if (err == JB_ERR_OK)
1650 * Signal to higher-level CGI code that there was a problem but we
1651 * handled it, they should just return JB_ERR_OK.
1658 err = edit_parse_actions_file(file);
1661 if (err == JB_ERR_PARSE)
1663 err = cgi_error_parse(csp, rsp, file);
1664 if (err == JB_ERR_OK)
1667 * Signal to higher-level CGI code that there was a problem but we
1668 * handled it, they should just return JB_ERR_OK.
1673 edit_free_file(file);
1682 /*********************************************************************
1684 * Function : get_file_name_param
1686 * Description : Get the name of the file to edit from the parameters
1687 * passed to a CGI function. This function handles
1688 * security checks such as blocking urls containing
1689 * "/" or ".", prepending the config file directory,
1690 * and adding the specified suffix.
1692 * (This is an essential security check, otherwise
1693 * users may be able to pass "../../../etc/passwd"
1694 * and overwrite the password file [linux], "prn:"
1695 * and print random data [Windows], etc...)
1697 * This function only allows filenames contining the
1698 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1699 * That's probably too restrictive but at least it's
1703 * 1 : csp = Current client state (buffers, headers, etc...)
1704 * 2 : parameters = map of cgi parameters
1705 * 3 : param_name = The name of the parameter to read
1706 * 4 : suffix = File extension, e.g. ".actions"
1707 * 5 : pfilename = destination for full filename. Caller
1708 * free()s. Set to NULL on error.
1709 * 6 : pparam = destination for partial filename,
1710 * suitable for use in another URL. Allocated as part
1711 * of the map "parameters", so don't free it.
1712 * Set to NULL if not specified.
1714 * Returns : JB_ERR_OK on success
1715 * JB_ERR_MEMORY on out-of-memory
1716 * JB_ERR_CGI_PARAMS if "filename" was not specified
1719 *********************************************************************/
1720 static jb_err get_file_name_param(struct client_state *csp,
1721 const struct map *parameters,
1722 const char *param_name,
1725 const char **pparam)
1743 param = lookup(parameters, param_name);
1746 return JB_ERR_CGI_PARAMS;
1751 len = strlen(param);
1752 if (len >= FILENAME_MAX)
1755 return JB_ERR_CGI_PARAMS;
1758 /* Check every character to see if it's legal */
1760 while ((ch = *s++) != '\0')
1762 if ( ((ch < 'A') || (ch > 'Z'))
1763 && ((ch < 'a') || (ch > 'z'))
1764 && ((ch < '0') || (ch > '9'))
1768 /* Probable hack attempt. */
1769 return JB_ERR_CGI_PARAMS;
1773 /* Append extension */
1774 name = malloc(len + strlen(suffix) + 1);
1777 return JB_ERR_MEMORY;
1779 strcpy(name, param);
1780 strcpy(name + len, suffix);
1783 fullpath = make_path(csp->config->confdir, name);
1785 if (fullpath == NULL)
1787 return JB_ERR_MEMORY;
1791 *pfilename = fullpath;
1797 /*********************************************************************
1799 * Function : get_number_param
1801 * Description : Get a non-negative integer from the parameters
1802 * passed to a CGI function.
1805 * 1 : csp = Current client state (buffers, headers, etc...)
1806 * 2 : parameters = map of cgi parameters
1807 * 3 : name = Name of CGI parameter to read
1808 * 4 : pvalue = destination for value.
1809 * Set to -1 on error.
1811 * Returns : JB_ERR_OK on success
1812 * JB_ERR_MEMORY on out-of-memory
1813 * JB_ERR_CGI_PARAMS if the parameter was not specified
1816 *********************************************************************/
1817 static jb_err get_number_param(struct client_state *csp,
1818 const struct map *parameters,
1833 param = lookup(parameters, name);
1836 return JB_ERR_CGI_PARAMS;
1839 /* We don't use atoi because I want to check this carefully... */
1842 while ((ch = *param++) != '\0')
1844 if ((ch < '0') || (ch > '9'))
1846 return JB_ERR_CGI_PARAMS;
1853 * <limits.h> defines UINT_MAX
1855 * (UINT_MAX - ch) / 10 is the largest number that
1856 * can be safely multiplied by 10 then have ch added.
1858 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1860 return JB_ERR_CGI_PARAMS;
1863 value = value * 10 + ch;
1873 /*********************************************************************
1875 * Function : get_url_spec_param
1877 * Description : Get a URL pattern from the parameters
1878 * passed to a CGI function. Removes leading/trailing
1879 * spaces and validates it.
1882 * 1 : csp = Current client state (buffers, headers, etc...)
1883 * 2 : parameters = map of cgi parameters
1884 * 3 : name = Name of CGI parameter to read
1885 * 4 : pvalue = destination for value. Will be malloc()'d.
1886 * Set to NULL on error.
1888 * Returns : JB_ERR_OK on success
1889 * JB_ERR_MEMORY on out-of-memory
1890 * JB_ERR_CGI_PARAMS if the parameter was not specified
1893 *********************************************************************/
1894 static jb_err get_url_spec_param(struct client_state *csp,
1895 const struct map *parameters,
1899 const char *orig_param;
1902 struct url_spec compiled[1];
1912 orig_param = lookup(parameters, name);
1915 return JB_ERR_CGI_PARAMS;
1918 /* Copy and trim whitespace */
1919 param = strdup(orig_param);
1922 return JB_ERR_MEMORY;
1926 /* Must be non-empty, and can't allow 1st character to be '{' */
1927 if (param[0] == '\0' || param[0] == '{')
1930 return JB_ERR_CGI_PARAMS;
1933 /* Check for embedded newlines */
1934 for (s = param; *s != '\0'; s++)
1936 if ((*s == '\r') || (*s == '\n'))
1939 return JB_ERR_CGI_PARAMS;
1943 /* Check that regex is valid */
1948 return JB_ERR_MEMORY;
1950 err = create_url_spec(compiled, s);
1955 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1957 free_url_spec(compiled);
1959 if (param[strlen(param) - 1] == '\\')
1962 * Must protect trailing '\\' from becoming line continuation character.
1963 * Two methods: 1) If it's a domain only, add a trailing '/'.
1964 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1966 if (strchr(param, '/') == NULL)
1968 err = string_append(¶m, "/");
1972 err = string_append(¶m, "(?:)");
1979 /* Check that the modified regex is valid */
1984 return JB_ERR_MEMORY;
1986 err = create_url_spec(compiled, s);
1991 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1993 free_url_spec(compiled);
2000 /*********************************************************************
2002 * Function : map_radio
2004 * Description : Map a set of radio button values. E.g. if you have
2005 * 3 radio buttons, declare them as:
2006 * <option type="radio" name="xyz" @xyz-a@>
2007 * <option type="radio" name="xyz" @xyz-b@>
2008 * <option type="radio" name="xyz" @xyz-c@>
2009 * Then map one of the @xyz-?@ variables to "checked"
2010 * and all the others to empty by calling:
2011 * map_radio(exports, "xyz", "abc", sel)
2012 * Where 'sel' is 'a', 'b', or 'c'.
2015 * 1 : exports = Exports map to modify.
2016 * 2 : optionname = name for map
2017 * 3 : values = null-terminated list of values;
2018 * 4 : value = Selected value.
2020 * CGI Parameters : None
2022 * Returns : JB_ERR_OK on success
2023 * JB_ERR_MEMORY on out-of-memory
2025 *********************************************************************/
2026 static jb_err map_radio(struct map * exports,
2027 const char * optionname,
2028 const char * values,
2040 len = strlen(optionname);
2041 buf = malloc(len + 3);
2044 return JB_ERR_MEMORY;
2047 strcpy(buf, optionname);
2052 while ((c = *values++) != '\0')
2057 if (map(exports, buf, 1, "", 1))
2060 return JB_ERR_MEMORY;
2066 if (map(exports, buf, 0, "checked", 1))
2069 return JB_ERR_MEMORY;
2076 /*********************************************************************
2078 * Function : cgi_error_modified
2080 * Description : CGI function that is called when a file is modified
2081 * outside the CGI editor.
2084 * 1 : csp = Current client state (buffers, headers, etc...)
2085 * 2 : rsp = http_response data structure for output
2086 * 3 : filename = The file that was modified.
2088 * CGI Parameters : none
2090 * Returns : JB_ERR_OK on success
2091 * JB_ERR_MEMORY on out-of-memory error.
2093 *********************************************************************/
2094 jb_err cgi_error_modified(struct client_state *csp,
2095 struct http_response *rsp,
2096 const char *filename)
2098 struct map *exports;
2105 if (NULL == (exports = default_exports(csp, NULL)))
2107 return JB_ERR_MEMORY;
2110 err = map(exports, "f", 1, html_encode(filename), 0);
2117 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2121 /*********************************************************************
2123 * Function : cgi_error_parse
2125 * Description : CGI function that is called when a file cannot
2126 * be parsed by the CGI editor.
2129 * 1 : csp = Current client state (buffers, headers, etc...)
2130 * 2 : rsp = http_response data structure for output
2131 * 3 : file = The file that was modified.
2133 * CGI Parameters : none
2135 * Returns : JB_ERR_OK on success
2136 * JB_ERR_MEMORY on out-of-memory error.
2138 *********************************************************************/
2139 jb_err cgi_error_parse(struct client_state *csp,
2140 struct http_response *rsp,
2141 struct editable_file *file)
2143 struct map *exports;
2145 struct file_line *cur_line;
2151 if (NULL == (exports = default_exports(csp, NULL)))
2153 return JB_ERR_MEMORY;
2156 err = map(exports, "f", 1, file->identifier, 1);
2157 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2159 cur_line = file->parse_error;
2162 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2163 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2171 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2175 /*********************************************************************
2177 * Function : cgi_error_file
2179 * Description : CGI function that is called when a file cannot be
2180 * opened by the CGI editor.
2183 * 1 : csp = Current client state (buffers, headers, etc...)
2184 * 2 : rsp = http_response data structure for output
2185 * 3 : filename = The file that was modified.
2187 * CGI Parameters : none
2189 * Returns : JB_ERR_OK on success
2190 * JB_ERR_MEMORY on out-of-memory error.
2192 *********************************************************************/
2193 jb_err cgi_error_file(struct client_state *csp,
2194 struct http_response *rsp,
2195 const char *filename)
2197 struct map *exports;
2204 if (NULL == (exports = default_exports(csp, NULL)))
2206 return JB_ERR_MEMORY;
2209 err = map(exports, "f", 1, html_encode(filename), 0);
2216 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2220 /*********************************************************************
2222 * Function : cgi_error_bad_param
2224 * Description : CGI function that is called if the parameters
2225 * (query string) for a CGI were wrong.
2228 * 1 : csp = Current client state (buffers, headers, etc...)
2229 * 2 : rsp = http_response data structure for output
2231 * CGI Parameters : none
2233 * Returns : JB_ERR_OK on success
2234 * JB_ERR_MEMORY on out-of-memory error.
2236 *********************************************************************/
2237 jb_err cgi_error_disabled(struct client_state *csp,
2238 struct http_response *rsp)
2240 struct map *exports;
2245 if (NULL == (exports = default_exports(csp, NULL)))
2247 return JB_ERR_MEMORY;
2250 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2254 /*********************************************************************
2256 * Function : cgi_edit_actions
2258 * Description : CGI function that allows the user to choose which
2259 * actions file to edit.
2262 * 1 : csp = Current client state (buffers, headers, etc...)
2263 * 2 : rsp = http_response data structure for output
2264 * 3 : parameters = map of cgi parameters
2266 * CGI Parameters : None
2268 * Returns : JB_ERR_OK on success
2269 * JB_ERR_MEMORY on out-of-memory error
2271 *********************************************************************/
2272 jb_err cgi_edit_actions(struct client_state *csp,
2273 struct http_response *rsp,
2274 const struct map *parameters)
2277 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2279 return cgi_error_disabled(csp, rsp);
2282 /* FIXME: Incomplete */
2283 rsp->status = strdup("302 Local Redirect from Junkbuster");
2284 if (rsp->status == NULL)
2286 return JB_ERR_MEMORY;
2288 if (enlist_unique_header(rsp->headers, "Location",
2289 CGI_PREFIX "edit-actions-list?f=ijb"))
2293 return JB_ERR_MEMORY;
2300 /*********************************************************************
2302 * Function : cgi_edit_actions_list
2304 * Description : CGI function that edits the actions list.
2305 * FIXME: This function shouldn't FATAL ever.
2306 * FIXME: This function doesn't check the retval of map()
2308 * 1 : csp = Current client state (buffers, headers, etc...)
2309 * 2 : rsp = http_response data structure for output
2310 * 3 : parameters = map of cgi parameters
2312 * CGI Parameters : filename
2314 * Returns : JB_ERR_OK on success
2315 * JB_ERR_MEMORY on out-of-memory
2316 * JB_ERR_FILE if the file cannot be opened or
2318 * JB_ERR_CGI_PARAMS if "filename" was not specified
2321 *********************************************************************/
2322 jb_err cgi_edit_actions_list(struct client_state *csp,
2323 struct http_response *rsp,
2324 const struct map *parameters)
2326 char * section_template;
2327 char * url_template;
2332 struct map * exports;
2333 struct map * section_exports;
2334 struct map * url_exports;
2335 struct editable_file * file;
2336 struct file_line * cur_line;
2337 unsigned line_number = 0;
2338 unsigned prev_section_line_number = ((unsigned) (-1));
2342 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2344 return cgi_error_disabled(csp, rsp);
2347 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2350 /* No filename specified, can't read file, or out of memory. */
2351 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2354 if (NULL == (exports = default_exports(csp, NULL)))
2356 edit_free_file(file);
2357 return JB_ERR_MEMORY;
2360 err = map(exports, "f", 1, file->identifier, 1);
2361 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2365 edit_free_file(file);
2370 /* Should do all global exports above this point */
2372 err = template_load(csp, §ion_template, "edit-actions-list-section");
2375 edit_free_file(file);
2377 if (err == JB_ERR_FILE)
2379 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2384 err = template_load(csp, &url_template, "edit-actions-list-url");
2387 free(section_template);
2388 edit_free_file(file);
2390 if (err == JB_ERR_FILE)
2392 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2397 err = template_fill(§ion_template, exports);
2401 edit_free_file(file);
2407 err = template_fill(&url_template, exports);
2410 free(section_template);
2411 edit_free_file(file);
2416 /* Find start of actions in file */
2417 cur_line = file->lines;
2419 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2421 cur_line = cur_line->next;
2425 if (NULL == (sections = strdup("")))
2427 free(section_template);
2429 edit_free_file(file);
2431 return JB_ERR_MEMORY;
2434 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2436 if (NULL == (section_exports = new_map()))
2439 free(section_template);
2441 edit_free_file(file);
2443 return JB_ERR_MEMORY;
2446 snprintf(buf, 50, "%d", line_number);
2447 err = map(section_exports, "s", 1, buf, 1);
2448 if (!err) err = map(section_exports, "actions", 1,
2449 actions_to_html(cur_line->data.action), 0);
2452 && (cur_line->next != NULL)
2453 && (cur_line->next->type == FILE_LINE_URL))
2455 /* This section contains at least one URL, don't allow delete */
2456 err = map_block_killer(section_exports, "empty-section");
2460 if (!err) err = map_block_keep(section_exports, "empty-section");
2463 if (prev_section_line_number != ((unsigned)(-1)))
2465 /* Not last section */
2466 snprintf(buf, 50, "%d", prev_section_line_number);
2467 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2468 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2473 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2475 prev_section_line_number = line_number;
2480 free(section_template);
2482 edit_free_file(file);
2484 free_map(section_exports);
2488 /* Should do all section-specific exports above this point */
2490 if (NULL == (urls = strdup("")))
2493 free(section_template);
2495 edit_free_file(file);
2497 free_map(section_exports);
2498 return JB_ERR_MEMORY;
2503 cur_line = cur_line->next;
2506 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2508 if (NULL == (url_exports = new_map()))
2512 free(section_template);
2514 edit_free_file(file);
2516 free_map(section_exports);
2517 return JB_ERR_MEMORY;
2520 snprintf(buf, 50, "%d", line_number);
2521 err = map(url_exports, "p", 1, buf, 1);
2523 snprintf(buf, 50, "%d", url_1_2);
2524 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2526 if (!err) err = map(url_exports, "url-html", 1,
2527 html_encode(cur_line->unprocessed), 0);
2528 if (!err) err = map(url_exports, "url", 1,
2529 url_encode(cur_line->unprocessed), 0);
2535 free(section_template);
2537 edit_free_file(file);
2539 free_map(section_exports);
2540 free_map(url_exports);
2544 if (NULL == (s = strdup(url_template)))
2548 free(section_template);
2550 edit_free_file(file);
2552 free_map(section_exports);
2553 free_map(url_exports);
2554 return JB_ERR_MEMORY;
2557 err = template_fill(&s, section_exports);
2558 if (!err) err = template_fill(&s, url_exports);
2559 if (!err) err = string_append(&urls, s);
2561 free_map(url_exports);
2568 free(section_template);
2570 edit_free_file(file);
2572 free_map(section_exports);
2576 url_1_2 = 3 - url_1_2;
2578 cur_line = cur_line->next;
2582 err = map(section_exports, "urls", 1, urls, 0);
2584 /* Could also do section-specific exports here, but it wouldn't be as fast */
2586 if ( (cur_line != NULL)
2587 && (cur_line->type == FILE_LINE_ACTION))
2589 /* Not last section */
2590 snprintf(buf, 50, "%d", line_number);
2591 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2592 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2597 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2603 free(section_template);
2605 edit_free_file(file);
2607 free_map(section_exports);
2611 if (NULL == (s = strdup(section_template)))
2614 free(section_template);
2616 edit_free_file(file);
2618 free_map(section_exports);
2619 return JB_ERR_MEMORY;
2622 err = template_fill(&s, section_exports);
2623 if (!err) err = string_append(§ions, s);
2626 free_map(section_exports);
2631 free(section_template);
2633 edit_free_file(file);
2639 edit_free_file(file);
2640 free(section_template);
2643 err = map(exports, "sections", 1, sections, 0);
2650 /* Could also do global exports here, but it wouldn't be as fast */
2652 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2656 /*********************************************************************
2658 * Function : cgi_edit_actions
2660 * Description : CGI function that edits the Actions list.
2663 * 1 : csp = Current client state (buffers, headers, etc...)
2664 * 2 : rsp = http_response data structure for output
2665 * 3 : parameters = map of cgi parameters
2667 * CGI Parameters : None
2669 * Returns : JB_ERR_OK on success
2670 * JB_ERR_MEMORY on out-of-memory
2671 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2672 * specified or not valid.
2674 *********************************************************************/
2675 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2676 struct http_response *rsp,
2677 const struct map *parameters)
2679 struct map * exports;
2681 struct editable_file * file;
2682 struct file_line * cur_line;
2683 unsigned line_number;
2686 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2688 return cgi_error_disabled(csp, rsp);
2691 err = get_number_param(csp, parameters, "s", §ionid);
2697 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2700 /* No filename specified, can't read file, modified, or out of memory. */
2701 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2704 cur_line = file->lines;
2706 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2708 cur_line = cur_line->next;
2711 if ( (cur_line == NULL)
2712 || (line_number != sectionid)
2714 || (cur_line->type != FILE_LINE_ACTION))
2716 /* Invalid "sectionid" parameter */
2717 edit_free_file(file);
2718 return JB_ERR_CGI_PARAMS;
2721 if (NULL == (exports = default_exports(csp, NULL)))
2723 edit_free_file(file);
2724 return JB_ERR_MEMORY;
2727 err = map(exports, "f", 1, file->identifier, 1);
2728 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2729 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2731 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2733 edit_free_file(file);
2741 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2745 /*********************************************************************
2747 * Function : cgi_edit_actions_submit
2749 * Description : CGI function that actually edits the Actions list.
2752 * 1 : csp = Current client state (buffers, headers, etc...)
2753 * 2 : rsp = http_response data structure for output
2754 * 3 : parameters = map of cgi parameters
2756 * CGI Parameters : None
2758 * Returns : JB_ERR_OK on success
2759 * JB_ERR_MEMORY on out-of-memory
2760 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2761 * specified or not valid.
2763 *********************************************************************/
2764 jb_err cgi_edit_actions_submit(struct client_state *csp,
2765 struct http_response *rsp,
2766 const struct map *parameters)
2772 struct editable_file * file;
2773 struct file_line * cur_line;
2774 unsigned line_number;
2778 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2780 return cgi_error_disabled(csp, rsp);
2783 err = get_number_param(csp, parameters, "s", §ionid);
2789 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2792 /* No filename specified, can't read file, modified, or out of memory. */
2793 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2796 cur_line = file->lines;
2798 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2800 cur_line = cur_line->next;
2803 if ( (cur_line == NULL)
2804 || (line_number != sectionid)
2806 || (cur_line->type != FILE_LINE_ACTION))
2808 /* Invalid "sectionid" parameter */
2809 edit_free_file(file);
2810 return JB_ERR_CGI_PARAMS;
2813 err = actions_from_radio(parameters, cur_line->data.action);
2817 edit_free_file(file);
2821 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2824 edit_free_file(file);
2825 return JB_ERR_MEMORY;
2828 len = strlen(actiontext);
2832 * Empty action - must special-case this.
2833 * Simply setting len to 1 is sufficient...
2838 if (NULL == (newtext = malloc(len + 2)))
2842 edit_free_file(file);
2843 return JB_ERR_MEMORY;
2845 strcpy(newtext, actiontext);
2849 newtext[len + 1] = '\0';
2851 freez(cur_line->raw);
2852 freez(cur_line->unprocessed);
2853 cur_line->unprocessed = newtext;
2855 err = edit_write_file(file);
2858 /* Error writing file */
2859 edit_free_file(file);
2863 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2864 string_append(&target, file->identifier);
2866 edit_free_file(file);
2871 return JB_ERR_MEMORY;
2874 rsp->status = strdup("302 Local Redirect from Junkbuster");
2875 if (rsp->status == NULL)
2878 return JB_ERR_MEMORY;
2880 err = enlist_unique_header(rsp->headers, "Location", target);
2887 /*********************************************************************
2889 * Function : cgi_edit_actions_url
2891 * Description : CGI function that actually edits a URL pattern in
2895 * 1 : csp = Current client state (buffers, headers, etc...)
2896 * 2 : rsp = http_response data structure for output
2897 * 3 : parameters = map of cgi parameters
2900 * filename : Identifies the file to edit
2901 * ver : File's last-modified time
2902 * section : Line number of section to edit
2903 * pattern : Line number of pattern to edit
2904 * newval : New value for pattern
2906 * Returns : JB_ERR_OK on success
2907 * JB_ERR_MEMORY on out-of-memory
2908 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2909 * specified or not valid.
2911 *********************************************************************/
2912 jb_err cgi_edit_actions_url(struct client_state *csp,
2913 struct http_response *rsp,
2914 const struct map *parameters)
2918 struct editable_file * file;
2919 struct file_line * cur_line;
2920 unsigned line_number;
2924 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2926 return cgi_error_disabled(csp, rsp);
2929 err = get_number_param(csp, parameters, "p", &patternid);
2936 return JB_ERR_CGI_PARAMS;
2939 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
2945 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2948 /* No filename specified, can't read file, modified, or out of memory. */
2950 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2954 cur_line = file->lines;
2956 while ((cur_line != NULL) && (line_number < patternid))
2958 cur_line = cur_line->next;
2962 if ( (cur_line == NULL)
2963 || (cur_line->type != FILE_LINE_URL))
2965 /* Invalid "patternid" parameter */
2967 edit_free_file(file);
2968 return JB_ERR_CGI_PARAMS;
2971 /* At this point, the line to edit is in cur_line */
2973 freez(cur_line->raw);
2974 freez(cur_line->unprocessed);
2975 cur_line->unprocessed = new_pattern;
2977 err = edit_write_file(file);
2980 /* Error writing file */
2981 edit_free_file(file);
2985 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2986 string_append(&target, file->identifier);
2988 edit_free_file(file);
2993 return JB_ERR_MEMORY;
2996 rsp->status = strdup("302 Local Redirect from Junkbuster");
2997 if (rsp->status == NULL)
3000 return JB_ERR_MEMORY;
3002 err = enlist_unique_header(rsp->headers, "Location", target);
3009 /*********************************************************************
3011 * Function : cgi_edit_actions_add_url
3013 * Description : CGI function that actually adds a URL pattern to
3017 * 1 : csp = Current client state (buffers, headers, etc...)
3018 * 2 : rsp = http_response data structure for output
3019 * 3 : parameters = map of cgi parameters
3022 * filename : Identifies the file to edit
3023 * ver : File's last-modified time
3024 * section : Line number of section to edit
3025 * newval : New pattern
3027 * Returns : JB_ERR_OK on success
3028 * JB_ERR_MEMORY on out-of-memory
3029 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3030 * specified or not valid.
3032 *********************************************************************/
3033 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3034 struct http_response *rsp,
3035 const struct map *parameters)
3039 struct file_line * new_line;
3040 struct editable_file * file;
3041 struct file_line * cur_line;
3042 unsigned line_number;
3046 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3048 return cgi_error_disabled(csp, rsp);
3051 err = get_number_param(csp, parameters, "s", §ionid);
3058 return JB_ERR_CGI_PARAMS;
3061 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3067 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3070 /* No filename specified, can't read file, modified, or out of memory. */
3072 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3076 cur_line = file->lines;
3078 while ((cur_line != NULL) && (line_number < sectionid))
3080 cur_line = cur_line->next;
3084 if ( (cur_line == NULL)
3085 || (cur_line->type != FILE_LINE_ACTION))
3087 /* Invalid "sectionid" parameter */
3089 edit_free_file(file);
3090 return JB_ERR_CGI_PARAMS;
3093 /* At this point, the section header is in cur_line - add after this. */
3095 /* Allocate the new line */
3096 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3097 if (new_line == NULL)
3100 edit_free_file(file);
3101 return JB_ERR_MEMORY;
3104 /* Fill in the data members of the new line */
3105 new_line->raw = NULL;
3106 new_line->prefix = NULL;
3107 new_line->unprocessed = new_pattern;
3108 new_line->type = FILE_LINE_URL;
3110 /* Link new_line into the list, after cur_line */
3111 new_line->next = cur_line->next;
3112 cur_line->next = new_line;
3114 /* Done making changes, now commit */
3116 err = edit_write_file(file);
3119 /* Error writing file */
3120 edit_free_file(file);
3124 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3125 string_append(&target, file->identifier);
3127 edit_free_file(file);
3132 return JB_ERR_MEMORY;
3135 rsp->status = strdup("302 Local Redirect from Junkbuster");
3136 if (rsp->status == NULL)
3139 return JB_ERR_MEMORY;
3141 err = enlist_unique_header(rsp->headers, "Location", target);
3148 /*********************************************************************
3150 * Function : cgi_edit_actions_remove_url
3152 * Description : CGI function that actually removes a URL pattern from
3156 * 1 : csp = Current client state (buffers, headers, etc...)
3157 * 2 : rsp = http_response data structure for output
3158 * 3 : parameters = map of cgi parameters
3161 * f : (filename) Identifies the file to edit
3162 * v : (version) File's last-modified time
3163 * p : (pattern) Line number of pattern to remove
3165 * Returns : JB_ERR_OK on success
3166 * JB_ERR_MEMORY on out-of-memory
3167 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3168 * specified or not valid.
3170 *********************************************************************/
3171 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3172 struct http_response *rsp,
3173 const struct map *parameters)
3176 struct editable_file * file;
3177 struct file_line * cur_line;
3178 struct file_line * prev_line;
3179 unsigned line_number;
3183 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3185 return cgi_error_disabled(csp, rsp);
3188 err = get_number_param(csp, parameters, "p", &patternid);
3194 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3197 /* No filename specified, can't read file, modified, or out of memory. */
3198 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3203 cur_line = file->lines;
3205 while ((cur_line != NULL) && (line_number < patternid))
3207 prev_line = cur_line;
3208 cur_line = cur_line->next;
3212 if ( (cur_line == NULL)
3213 || (prev_line == NULL)
3214 || (cur_line->type != FILE_LINE_URL))
3216 /* Invalid "patternid" parameter */
3217 edit_free_file(file);
3218 return JB_ERR_CGI_PARAMS;
3221 /* At this point, the line to remove is in cur_line, and the previous
3222 * one is in prev_line
3225 /* Unlink cur_line */
3226 prev_line->next = cur_line->next;
3227 cur_line->next = NULL;
3230 edit_free_file_lines(cur_line);
3232 err = edit_write_file(file);
3235 /* Error writing file */
3236 edit_free_file(file);
3240 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3241 string_append(&target, file->identifier);
3243 edit_free_file(file);
3248 return JB_ERR_MEMORY;
3251 rsp->status = strdup("302 Local Redirect from Junkbuster");
3252 if (rsp->status == NULL)
3255 return JB_ERR_MEMORY;
3257 err = enlist_unique_header(rsp->headers, "Location", target);
3264 /*********************************************************************
3266 * Function : cgi_edit_actions_section_remove
3268 * Description : CGI function that actually removes a whole section from
3269 * the actions file. The section must be empty first
3270 * (else JB_ERR_CGI_PARAMS).
3273 * 1 : csp = Current client state (buffers, headers, etc...)
3274 * 2 : rsp = http_response data structure for output
3275 * 3 : parameters = map of cgi parameters
3278 * f : (filename) Identifies the file to edit
3279 * v : (version) File's last-modified time
3280 * s : (section) Line number of section to edit
3282 * Returns : JB_ERR_OK on success
3283 * JB_ERR_MEMORY on out-of-memory
3284 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3285 * specified or not valid.
3287 *********************************************************************/
3288 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3289 struct http_response *rsp,
3290 const struct map *parameters)
3293 struct editable_file * file;
3294 struct file_line * cur_line;
3295 struct file_line * prev_line;
3296 unsigned line_number;
3300 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3302 return cgi_error_disabled(csp, rsp);
3305 err = get_number_param(csp, parameters, "s", §ionid);
3311 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3314 /* No filename specified, can't read file, modified, or out of memory. */
3315 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3319 cur_line = file->lines;
3322 while ((cur_line != NULL) && (line_number < sectionid))
3324 prev_line = cur_line;
3325 cur_line = cur_line->next;
3329 if ( (cur_line == NULL)
3330 || (cur_line->type != FILE_LINE_ACTION) )
3332 /* Invalid "sectionid" parameter */
3333 edit_free_file(file);
3334 return JB_ERR_CGI_PARAMS;
3337 if ( (cur_line->next != NULL)
3338 && (cur_line->next->type == FILE_LINE_URL) )
3340 /* Section not empty. */
3341 edit_free_file(file);
3342 return JB_ERR_CGI_PARAMS;
3345 /* At this point, the line to remove is in cur_line, and the previous
3346 * one is in prev_line
3349 /* Unlink cur_line */
3350 if (prev_line == NULL)
3352 /* Removing the first line from the file */
3353 file->lines = cur_line->next;
3357 prev_line->next = cur_line->next;
3359 cur_line->next = NULL;
3362 edit_free_file_lines(cur_line);
3364 err = edit_write_file(file);
3367 /* Error writing file */
3368 edit_free_file(file);
3372 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3373 string_append(&target, file->identifier);
3375 edit_free_file(file);
3380 return JB_ERR_MEMORY;
3383 rsp->status = strdup("302 Local Redirect from Junkbuster");
3384 if (rsp->status == NULL)
3387 return JB_ERR_MEMORY;
3389 err = enlist_unique_header(rsp->headers, "Location", target);
3396 /*********************************************************************
3398 * Function : cgi_edit_actions_section_add
3400 * Description : CGI function that adds a new empty section to
3404 * 1 : csp = Current client state (buffers, headers, etc...)
3405 * 2 : rsp = http_response data structure for output
3406 * 3 : parameters = map of cgi parameters
3409 * f : (filename) Identifies the file to edit
3410 * v : (version) File's last-modified time
3411 * s : (section) Line number of section to add after, 0 for
3414 * Returns : JB_ERR_OK on success
3415 * JB_ERR_MEMORY on out-of-memory
3416 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3417 * specified or not valid.
3419 *********************************************************************/
3420 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3421 struct http_response *rsp,
3422 const struct map *parameters)
3425 struct file_line * new_line;
3427 struct editable_file * file;
3428 struct file_line * cur_line;
3429 unsigned line_number;
3433 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3435 return cgi_error_disabled(csp, rsp);
3438 err = get_number_param(csp, parameters, "s", §ionid);
3444 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3447 /* No filename specified, can't read file, modified, or out of memory. */
3448 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3452 cur_line = file->lines;
3456 /* Add to start of file */
3457 if (cur_line != NULL)
3459 /* There's something in the file, find the line before the first
3462 while ( (cur_line->next != NULL)
3463 && (cur_line->next->type != FILE_LINE_ACTION) )
3465 cur_line = cur_line->next;
3472 /* Add after stated section. */
3473 while ((cur_line != NULL) && (line_number < sectionid))
3475 cur_line = cur_line->next;
3479 if ( (cur_line == NULL)
3480 || (cur_line->type != FILE_LINE_ACTION))
3482 /* Invalid "sectionid" parameter */
3483 edit_free_file(file);
3484 return JB_ERR_CGI_PARAMS;
3487 /* Skip through the section to find the last line in it. */
3488 while ( (cur_line->next != NULL)
3489 && (cur_line->next->type != FILE_LINE_ACTION) )
3491 cur_line = cur_line->next;
3496 /* At this point, the last line in the previous section is in cur_line
3497 * - add after this. (Or if we need to add as the first line, cur_line
3501 new_text = strdup("{}");
3502 if (NULL == new_text)
3504 edit_free_file(file);
3505 return JB_ERR_MEMORY;
3508 /* Allocate the new line */
3509 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3510 if (new_line == NULL)
3513 edit_free_file(file);
3514 return JB_ERR_MEMORY;
3517 /* Fill in the data members of the new line */
3518 new_line->raw = NULL;
3519 new_line->prefix = NULL;
3520 new_line->unprocessed = new_text;
3521 new_line->type = FILE_LINE_ACTION;
3523 if (cur_line != NULL)
3525 /* Link new_line into the list, after cur_line */
3526 new_line->next = cur_line->next;
3527 cur_line->next = new_line;
3531 /* Link new_line into the list, as first line */
3532 new_line->next = file->lines;
3533 file->lines = new_line;
3536 /* Done making changes, now commit */
3538 err = edit_write_file(file);
3541 /* Error writing file */
3542 edit_free_file(file);
3546 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3547 string_append(&target, file->identifier);
3549 edit_free_file(file);
3554 return JB_ERR_MEMORY;
3557 rsp->status = strdup("302 Local Redirect from Junkbuster");
3558 if (rsp->status == NULL)
3561 return JB_ERR_MEMORY;
3563 err = enlist_unique_header(rsp->headers, "Location", target);
3570 /*********************************************************************
3572 * Function : cgi_edit_actions_section_swap
3574 * Description : CGI function that swaps the order of two sections
3575 * in the actions file. Note that this CGI can actually
3576 * swap any two arbitrary sections, but the GUI interface
3577 * currently only allows consecutive sections to be
3581 * 1 : csp = Current client state (buffers, headers, etc...)
3582 * 2 : rsp = http_response data structure for output
3583 * 3 : parameters = map of cgi parameters
3586 * f : (filename) Identifies the file to edit
3587 * v : (version) File's last-modified time
3588 * s1 : (section1) Line number of first section to swap
3589 * s2 : (section2) Line number of second section to swap
3591 * Returns : JB_ERR_OK on success
3592 * JB_ERR_MEMORY on out-of-memory
3593 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3594 * specified or not valid.
3596 *********************************************************************/
3597 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3598 struct http_response *rsp,
3599 const struct map *parameters)
3603 struct editable_file * file;
3604 struct file_line * cur_line;
3605 struct file_line * prev_line;
3606 struct file_line * line_before_section1;
3607 struct file_line * line_start_section1;
3608 struct file_line * line_end_section1;
3609 struct file_line * line_after_section1;
3610 struct file_line * line_before_section2;
3611 struct file_line * line_start_section2;
3612 struct file_line * line_end_section2;
3613 struct file_line * line_after_section2;
3614 unsigned line_number;
3618 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3620 return cgi_error_disabled(csp, rsp);
3623 err = get_number_param(csp, parameters, "s1", §ion1);
3624 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3630 if (section1 > section2)
3632 unsigned temp = section2;
3633 section2 = section1;
3637 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3640 /* No filename specified, can't read file, modified, or out of memory. */
3641 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3644 /* Start at the beginning... */
3646 cur_line = file->lines;
3649 /* ... find section1 ... */
3650 while ((cur_line != NULL) && (line_number < section1))
3652 prev_line = cur_line;
3653 cur_line = cur_line->next;
3657 if ( (cur_line == NULL)
3658 || (cur_line->type != FILE_LINE_ACTION) )
3660 /* Invalid "section1" parameter */
3661 edit_free_file(file);
3662 return JB_ERR_CGI_PARAMS;
3665 /* If no-op, we've validated params and can skip the rest. */
3666 if (section1 != section2)
3668 /* ... find the end of section1 ... */
3669 line_before_section1 = prev_line;
3670 line_start_section1 = cur_line;
3673 prev_line = cur_line;
3674 cur_line = cur_line->next;
3677 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3678 line_end_section1 = prev_line;
3679 line_after_section1 = cur_line;
3681 /* ... find section2 ... */
3682 while ((cur_line != NULL) && (line_number < section2))
3684 prev_line = cur_line;
3685 cur_line = cur_line->next;
3689 if ( (cur_line == NULL)
3690 || (cur_line->type != FILE_LINE_ACTION) )
3692 /* Invalid "section2" parameter */
3693 edit_free_file(file);
3694 return JB_ERR_CGI_PARAMS;
3697 /* ... find the end of section2 ... */
3698 line_before_section2 = prev_line;
3699 line_start_section2 = cur_line;
3702 prev_line = cur_line;
3703 cur_line = cur_line->next;
3706 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3707 line_end_section2 = prev_line;
3708 line_after_section2 = cur_line;
3710 /* Now have all the pointers we need. Do the swap. */
3712 /* Change the pointer to section1 to point to section2 instead */
3713 if (line_before_section1 == NULL)
3715 file->lines = line_start_section2;
3719 line_before_section1->next = line_start_section2;
3722 if (line_before_section2 == line_end_section1)
3724 /* Consecutive sections */
3725 line_end_section2->next = line_start_section1;
3729 line_end_section2->next = line_after_section1;
3730 line_before_section2->next = line_start_section1;
3733 /* Set the pointer from the end of section1 to the rest of the file */
3734 line_end_section1->next = line_after_section2;
3736 err = edit_write_file(file);
3739 /* Error writing file */
3740 edit_free_file(file);
3743 } /* END if (section1 != section2) */
3745 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3746 string_append(&target, file->identifier);
3748 edit_free_file(file);
3753 return JB_ERR_MEMORY;
3756 rsp->status = strdup("302 Local Redirect from Junkbuster");
3757 if (rsp->status == NULL)
3760 return JB_ERR_MEMORY;
3762 err = enlist_unique_header(rsp->headers, "Location", target);
3769 /*********************************************************************
3771 * Function : cgi_toggle
3773 * Description : CGI function that adds a new empty section to
3777 * 1 : csp = Current client state (buffers, headers, etc...)
3778 * 2 : rsp = http_response data structure for output
3779 * 3 : parameters = map of cgi parameters
3782 * set : If present, how to change toggle setting:
3783 * "enable", "disable", "toggle", or none (default).
3784 * mini : If present, use mini reply template.
3786 * Returns : JB_ERR_OK on success
3787 * JB_ERR_MEMORY on out-of-memory
3789 *********************************************************************/
3790 jb_err cgi_toggle(struct client_state *csp,
3791 struct http_response *rsp,
3792 const struct map *parameters)
3794 struct map *exports;
3796 const char *template_name;
3803 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3805 return cgi_error_disabled(csp, rsp);
3808 if (NULL == (exports = default_exports(csp, "toggle")))
3810 return JB_ERR_MEMORY;
3813 mode = *(lookup(parameters, "set"));
3820 else if (mode == 'd')
3825 else if (mode == 't')
3828 g_bToggleIJB = !g_bToggleIJB;
3831 err = map_conditional(exports, "enabled", g_bToggleIJB);
3838 template_name = (*(lookup(parameters, "mini"))
3842 return template_fill_for_cgi(csp, template_name, exports, rsp);
3846 /*********************************************************************
3848 * Function : actions_to_radio
3850 * Description : Converts a actionsfile entry into settings for
3851 * radio buttons and edit boxes on a HTML form.
3854 * 1 : exports = List of substitutions to add to.
3855 * 2 : action = Action to read
3857 * Returns : JB_ERR_OK on success
3858 * JB_ERR_MEMORY on out-of-memory
3860 *********************************************************************/
3861 static jb_err actions_to_radio(struct map * exports,
3862 const struct action_spec *action)
3864 unsigned mask = action->mask;
3865 unsigned add = action->add;
3873 mask = action->mask;
3876 /* sanity - prevents "-feature +feature" */
3880 #define DEFINE_ACTION_BOOL(name, bit) \
3881 if (!(mask & bit)) \
3883 current_mode = 'n'; \
3885 else if (add & bit) \
3887 current_mode = 'y'; \
3891 current_mode = 'x'; \
3893 if (map_radio(exports, name, "ynx", current_mode)) \
3895 return JB_ERR_MEMORY; \
3898 #define DEFINE_ACTION_STRING(name, bit, index) \
3899 DEFINE_ACTION_BOOL(name, bit); \
3902 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
3905 checked = !strcmp(action->string[index], value); \
3909 checked = is_default; \
3911 mapped_param |= checked; \
3912 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
3914 return JB_ERR_MEMORY; \
3917 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
3918 if (map(exports, name "-param-custom", 1, \
3919 ((!mapped_param) ? "checked" : ""), 1)) \
3921 return JB_ERR_MEMORY; \
3923 if (map(exports, name "-param", 1, \
3924 (((add & bit) && !mapped_param) ? \
3925 action->string[index] : default_val), 1)) \
3927 return JB_ERR_MEMORY; \
3930 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
3931 if (map(exports, name "-param", 1, \
3932 ((add & bit) ? action->string[index] : default_val), 1)) \
3934 return JB_ERR_MEMORY; \
3937 #define DEFINE_ACTION_MULTI(name, index) \
3938 if (action->multi_add[index]->first) \
3940 current_mode = 'y'; \
3942 else if (action->multi_remove_all[index]) \
3944 current_mode = 'n'; \
3946 else if (action->multi_remove[index]->first) \
3948 current_mode = 'y'; \
3952 current_mode = 'x'; \
3954 if (map_radio(exports, name, "ynx", current_mode)) \
3956 return JB_ERR_MEMORY; \
3959 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
3961 #include "actionlist.h"
3963 #undef DEFINE_ACTION_MULTI
3964 #undef DEFINE_ACTION_STRING
3965 #undef DEFINE_ACTION_BOOL
3966 #undef DEFINE_ACTION_ALIAS
3967 #undef DEFINE_CGI_PARAM_CUSTOM
3968 #undef DEFINE_CGI_PARAM_RADIO
3969 #undef DEFINE_CGI_PARAM_NO_RADIO
3975 /*********************************************************************
3977 * Function : javascriptify
3979 * Description : Converts a string into a form JavaScript will like.
3981 * Netscape 4's JavaScript sucks - it doesn't use
3982 * "id" parameters, so you have to set the "name"
3983 * used to submit a form element to something JavaScript
3984 * will like. (Or access the elements by index in an
3985 * array. That array contains >60 elements and will
3986 * be changed whenever we add a new action to the
3987 * editor, so I'm NOT going to use indexes that have
3988 * to be figured out by hand.)
3990 * Currently the only thing we have to worry about
3991 * is "-" ==> "_" conversion.
3993 * This is a length-preserving operation so it is
3994 * carried out in-place, no memory is allocated
3998 * 1 : identifier = String to make JavaScript-friendly.
4002 *********************************************************************/
4003 static void javascriptify(char * identifier)
4005 char * p = identifier;
4006 while (NULL != (p = strchr(p, '-')))
4013 /*********************************************************************
4015 * Function : actions_from_radio
4017 * Description : Converts a map of parameters passed to a CGI function
4018 * into an actionsfile entry.
4021 * 1 : parameters = parameters to the CGI call
4022 * 2 : action = Action to change. Must be valid before
4023 * the call, actions not specified will be
4026 * Returns : JB_ERR_OK on success
4027 * JB_ERR_MEMORY on out-of-memory
4029 *********************************************************************/
4030 static jb_err actions_from_radio(const struct map * parameters,
4031 struct action_spec *action)
4033 static int first_time = 1;
4037 const char * js_name;
4042 /* Statics are generally a potential race condition,
4043 * but in this case we're safe and don't need semaphores.
4044 * Be careful if you modify this function.
4048 #define JAVASCRIPTIFY(dest_var, string) \
4050 static char js_name_arr[] = string; \
4053 javascriptify(js_name_arr); \
4055 dest_var = js_name_arr; \
4058 #define DEFINE_ACTION_BOOL(name, bit) \
4059 JAVASCRIPTIFY(js_name, name); \
4060 param = lookup(parameters, js_name); \
4061 ch = ijb_toupper(param[0]); \
4064 action->add |= bit; \
4065 action->mask |= bit; \
4067 else if (ch == 'N') \
4069 action->add &= ~bit; \
4070 action->mask &= ~bit; \
4072 else if (ch == 'X') \
4074 action->add &= ~bit; \
4075 action->mask |= bit; \
4078 #define DEFINE_ACTION_STRING(name, bit, index) \
4079 JAVASCRIPTIFY(js_name, name); \
4080 param = lookup(parameters, js_name); \
4081 ch = ijb_toupper(param[0]); \
4084 JAVASCRIPTIFY(js_name, name "-mode"); \
4085 param = lookup(parameters, js_name); \
4086 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4088 JAVASCRIPTIFY(js_name, name "-param"); \
4089 param = lookup(parameters, js_name); \
4091 if (*param != '\0') \
4093 if (NULL == (param_dup = strdup(param))) \
4095 return JB_ERR_MEMORY; \
4097 freez(action->string[index]); \
4098 action->add |= bit; \
4099 action->mask |= bit; \
4100 action->string[index] = param_dup; \
4103 else if (ch == 'N') \
4105 if (action->add & bit) \
4107 freez(action->string[index]); \
4109 action->add &= ~bit; \
4110 action->mask &= ~bit; \
4112 else if (ch == 'X') \
4114 if (action->add & bit) \
4116 freez(action->string[index]); \
4118 action->add &= ~bit; \
4119 action->mask |= bit; \
4122 #define DEFINE_ACTION_MULTI(name, index) \
4123 JAVASCRIPTIFY(js_name, name); \
4124 param = lookup(parameters, js_name); \
4125 ch = ijb_toupper((int)param[0]); \
4130 else if (ch == 'N') \
4132 list_remove_all(action->multi_add[index]); \
4133 list_remove_all(action->multi_remove[index]); \
4134 action->multi_remove_all[index] = 1; \
4136 else if (ch == 'X') \
4138 list_remove_all(action->multi_add[index]); \
4139 list_remove_all(action->multi_remove[index]); \
4140 action->multi_remove_all[index] = 0; \
4143 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4145 #include "actionlist.h"
4147 #undef DEFINE_ACTION_MULTI
4148 #undef DEFINE_ACTION_STRING
4149 #undef DEFINE_ACTION_BOOL
4150 #undef DEFINE_ACTION_ALIAS
4151 #undef JAVASCRIPTIFY
4159 #endif /* def FEATURE_CGI_EDIT_ACTIONS */