1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.11 2002/01/23 01:03:31 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.11 2002/01/23 01:03:31 jongfoster
46 * Fixing gcc [CygWin] compiler warnings
48 * Revision 1.10 2002/01/23 00:22:59 jongfoster
49 * Adding new function cgi_edit_actions_section_swap(), to reorder
52 * Adding get_url_spec_param() to get a validated URL pattern.
54 * Moving edit_read_line() out of this file and into loaders.c.
56 * Adding missing html_encode() to many CGI functions.
58 * Moving the functions that #include actionlist.h to the end of the file,
59 * because the Visual C++ 97 debugger gets extremely confused if you try
60 * to debug any code that comes after them in the file.
62 * Major optimizations in cgi_edit_actions_list() to reduce the size of
63 * the generated HTML (down 40% from 550k to 304k), with major side-effects
64 * throughout the editor and templates. In particular, the length of the
65 * URLs throughout the editor has been drastically reduced, by cutting
66 * paramater names down to 1 character and CGI names down to 3-4
67 * characters, by removing all non-essential CGI paramaters even at the
68 * expense of having to re-read the actions file for the most trivial
69 * page, and by using relative rather than absolute URLs. This means
70 * that this (typical example):
72 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
73 * filename=ijb&ver=1011487572&section=12&pattern=13
74 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
78 * <a href="eau?f=ijb&v=1011487572&p=13">
80 * Revision 1.9 2002/01/17 20:56:22 jongfoster
81 * Replacing hard references to the URL of the config interface
82 * with #defines from project.h
84 * Revision 1.8 2001/11/30 23:35:51 jongfoster
85 * Renaming actionsfile to ijb.action
87 * Revision 1.7 2001/11/13 00:28:24 jongfoster
88 * - Renaming parameters from edit-actions-for-url so that they only
89 * contain legal JavaScript characters. If we wanted to write
90 * JavaScript that worked with Netscape 4, this is nessacery.
91 * (Note that at the moment the JavaScript doesn't actually work
92 * with Netscape 4, but now this is purely a template issue, not
93 * one affecting code).
94 * - Adding new CGIs for use by non-JavaScript browsers:
95 * edit-actions-url-form
96 * edit-actions-add-url-form
97 * edit-actions-remove-url-form
100 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
101 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
102 * by and __OS2__ ifdef.
104 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
105 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
106 * threads to call select() simultaneously. So, it's time to do a real, live,
107 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
108 * (native). Both versions will work, but using __OS2__ offers multi-threading.
110 * Revision 1.4 2001/10/23 21:48:19 jongfoster
111 * Cleaning up error handling in CGI functions - they now send back
112 * a HTML error page and should never cause a FATAL error. (Fixes one
113 * potential source of "denial of service" attacks).
115 * CGI actions file editor that works and is actually useful.
117 * Ability to toggle JunkBuster remotely using a CGI call.
119 * You can turn off both the above features in the main configuration
120 * file, e.g. if you are running a multi-user proxy.
122 * Revision 1.3 2001/10/14 22:12:49 jongfoster
123 * New version of CGI-based actionsfile editor.
124 * Major changes, including:
125 * - Completely new file parser and file output routines
126 * - edit-actions CGI renamed edit-actions-for-url
127 * - All CGIs now need a filename parameter, except for...
128 * - New CGI edit-actions which doesn't need a filename,
129 * to allow you to start the editor up.
130 * - edit-actions-submit now works, and now automatically
131 * redirects you back to the main edit-actions-list handler.
133 * Revision 1.2 2001/09/16 17:05:14 jongfoster
134 * Removing unused #include showarg.h
136 * Revision 1.1 2001/09/16 15:47:37 jongfoster
137 * First version of CGI-based edit interface. This is very much a
138 * work-in-progress, and you can't actually use it to edit anything
139 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
140 * to have any effect.
143 **********************************************************************/
149 * FIXME: Following includes copied from cgi.c - which are actually needed?
154 #include <sys/types.h>
159 #include <sys/stat.h>
162 #define snprintf _snprintf
163 #endif /* def _WIN32 */
168 #include "cgisimple.h"
172 #include "miscutil.h"
176 /* loadcfg.h is for g_bToggleIJB only */
177 #include "urlmatch.h"
179 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
182 #ifdef FEATURE_CGI_EDIT_ACTIONS
186 struct file_line * next;
194 struct action_spec action[1];
203 /* Add more data types here... e.g.
206 struct url_spec url[1];
210 struct action_spec action[1];
219 #define FILE_LINE_UNPROCESSED 1
220 #define FILE_LINE_BLANK 2
221 #define FILE_LINE_ALIAS_HEADER 3
222 #define FILE_LINE_ALIAS_ENTRY 4
223 #define FILE_LINE_ACTION 5
224 #define FILE_LINE_URL 6
225 #define FILE_LINE_SETTINGS_HEADER 7
226 #define FILE_LINE_SETTINGS_ENTRY 8
227 #define FILE_LINE_DESCRIPTION_HEADER 9
228 #define FILE_LINE_DESCRIPTION_ENTRY 10
233 struct file_line * lines;
234 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
235 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
236 /* Pre-encoded with url_encode() for ease of use. */
237 const char * version_str; /* Last modification time, as a string. For CGI param */
238 /* Can be used in URL without using url_param(). */
239 unsigned version; /* Last modification time - prevents chaos with
240 * the browser's "back" button. Note that this is a
241 * time_t cast to an unsigned. When comparing, always
242 * cast the time_t to an unsigned, and *NOT* vice-versa.
243 * This may lose the top few bits, but they're not
244 * significant anyway.
246 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
247 * Note that changing this after the file has been
248 * read in will cause a mess.
250 struct file_line * parse_error; /* On parse error, this is the offending line. */
251 const char * parse_error_text; /* On parse error, this is the problem.
252 * (Statically allocated) */
255 /* FIXME: Following non-static functions should be prototyped in .h or made static */
257 /* Functions to read and write arbitrary config files */
258 jb_err edit_read_file(struct client_state *csp,
259 const struct map *parameters,
262 struct editable_file **pfile);
263 jb_err edit_write_file(struct editable_file * file);
264 void edit_free_file(struct editable_file * file);
266 /* Functions to read and write actions files */
267 jb_err edit_parse_actions_file(struct editable_file * file);
268 jb_err edit_read_actions_file(struct client_state *csp,
269 struct http_response *rsp,
270 const struct map *parameters,
272 struct editable_file **pfile);
275 jb_err cgi_error_modified(struct client_state *csp,
276 struct http_response *rsp,
277 const char *filename);
278 jb_err cgi_error_parse(struct client_state *csp,
279 struct http_response *rsp,
280 struct editable_file *file);
281 jb_err cgi_error_file(struct client_state *csp,
282 struct http_response *rsp,
283 const char *filename);
284 jb_err cgi_error_disabled(struct client_state *csp,
285 struct http_response *rsp);
287 /* Internal arbitrary config file support functions */
288 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
289 static void edit_free_file_lines(struct file_line * first_line);
291 /* Internal actions file support functions */
292 static int match_actions_file_header_line(const char * line, const char * name);
293 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
295 /* Internal parameter parsing functions */
296 static jb_err get_file_name_param(struct client_state *csp,
297 const struct map *parameters,
298 const char *param_name,
301 const char **pparam);
302 static jb_err get_number_param(struct client_state *csp,
303 const struct map *parameters,
306 static jb_err get_url_spec_param(struct client_state *csp,
307 const struct map *parameters,
311 /* Internal actionsfile <==> HTML conversion functions */
312 static jb_err map_radio(struct map * exports,
313 const char * optionname,
316 static jb_err actions_to_radio(struct map * exports,
317 const struct action_spec *action);
318 static jb_err actions_from_radio(const struct map * parameters,
319 struct action_spec *action);
322 static jb_err map_copy_parameter_html(struct map *out,
323 const struct map *in,
325 #if 0 /* unused function */
326 static jb_err map_copy_parameter_url(struct map *out,
327 const struct map *in,
329 #endif /* unused function */
331 /*********************************************************************
333 * Function : map_copy_parameter_html
335 * Description : Copy a CGI parameter from one map to another, HTML
339 * 1 : out = target map
340 * 2 : in = source map
341 * 3 : name = name of cgi parameter to copy
343 * Returns : JB_ERR_OK on success
344 * JB_ERR_MEMORY on out-of-memory
345 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
348 *********************************************************************/
349 static jb_err map_copy_parameter_html(struct map *out,
350 const struct map *in,
360 value = lookup(in, name);
361 err = map(out, name, 1, html_encode(value), 0);
368 else if (*value == '\0')
370 return JB_ERR_CGI_PARAMS;
379 #if 0 /* unused function */
380 /*********************************************************************
382 * Function : map_copy_parameter_html
384 * Description : Copy a CGI parameter from one map to another, URL
388 * 1 : out = target map
389 * 2 : in = source map
390 * 3 : name = name of cgi parameter to copy
392 * Returns : JB_ERR_OK on success
393 * JB_ERR_MEMORY on out-of-memory
394 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
397 *********************************************************************/
398 static jb_err map_copy_parameter_url(struct map *out,
399 const struct map *in,
409 value = lookup(in, name);
410 err = map(out, name, 1, url_encode(value), 0);
417 else if (*value == '\0')
419 return JB_ERR_CGI_PARAMS;
426 #endif /* 0 - unused function */
428 /*********************************************************************
430 * Function : cgi_edit_actions_url_form
432 * Description : CGI function that displays a form for
436 * 1 : csp = Current client state (buffers, headers, etc...)
437 * 2 : rsp = http_response data structure for output
438 * 3 : parameters = map of cgi parameters
441 * f : (filename) Identifies the file to edit
442 * v : (version) File's last-modified time
443 * p : (pattern) Line number of pattern to edit
445 * Returns : JB_ERR_OK on success
446 * JB_ERR_MEMORY on out-of-memory
447 * JB_ERR_CGI_PARAMS if the CGI parameters are not
448 * specified or not valid.
450 *********************************************************************/
451 jb_err cgi_edit_actions_url_form(struct client_state *csp,
452 struct http_response *rsp,
453 const struct map *parameters)
455 struct map * exports;
457 struct editable_file * file;
458 struct file_line * cur_line;
459 unsigned line_number;
466 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
468 return cgi_error_disabled(csp, rsp);
471 err = get_number_param(csp, parameters, "p", &patternid);
477 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
480 /* No filename specified, can't read file, modified, or out of memory. */
481 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
484 cur_line = file->lines;
486 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
488 cur_line = cur_line->next;
491 if ( (cur_line == NULL)
492 || (line_number != patternid)
494 || (cur_line->type != FILE_LINE_URL))
496 /* Invalid "patternid" parameter */
497 edit_free_file(file);
498 return JB_ERR_CGI_PARAMS;
501 if (NULL == (exports = default_exports(csp, NULL)))
503 edit_free_file(file);
504 return JB_ERR_MEMORY;
507 err = map(exports, "f", 1, file->identifier, 1);
508 if (!err) err = map(exports, "v", 1, file->version_str, 1);
509 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
510 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
512 edit_free_file(file);
520 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
524 /*********************************************************************
526 * Function : cgi_edit_actions_add_url_form
528 * Description : CGI function that displays a form for
532 * 1 : csp = Current client state (buffers, headers, etc...)
533 * 2 : rsp = http_response data structure for output
534 * 3 : parameters = map of cgi parameters
537 * f : (filename) Identifies the file to edit
538 * v : (version) File's last-modified time
539 * s : (section) Line number of section to edit
541 * Returns : JB_ERR_OK on success
542 * JB_ERR_MEMORY on out-of-memory
543 * JB_ERR_CGI_PARAMS if the CGI parameters are not
544 * specified or not valid.
546 *********************************************************************/
547 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
548 struct http_response *rsp,
549 const struct map *parameters)
558 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
560 return cgi_error_disabled(csp, rsp);
563 if (NULL == (exports = default_exports(csp, NULL)))
565 return JB_ERR_MEMORY;
568 err = map_copy_parameter_html(exports, parameters, "f");
569 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
570 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
578 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
582 /*********************************************************************
584 * Function : cgi_edit_actions_remove_url_form
586 * Description : CGI function that displays a form for
590 * 1 : csp = Current client state (buffers, headers, etc...)
591 * 2 : rsp = http_response data structure for output
592 * 3 : parameters = map of cgi parameters
595 * f : (filename) Identifies the file to edit
596 * v : (version) File's last-modified time
597 * p : (pattern) Line number of pattern to edit
599 * Returns : JB_ERR_OK on success
600 * JB_ERR_MEMORY on out-of-memory
601 * JB_ERR_CGI_PARAMS if the CGI parameters are not
602 * specified or not valid.
604 *********************************************************************/
605 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
606 struct http_response *rsp,
607 const struct map *parameters)
609 struct map * exports;
611 struct editable_file * file;
612 struct file_line * cur_line;
613 unsigned line_number;
620 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
622 return cgi_error_disabled(csp, rsp);
625 err = get_number_param(csp, parameters, "p", &patternid);
631 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
634 /* No filename specified, can't read file, modified, or out of memory. */
635 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
638 cur_line = file->lines;
640 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
642 cur_line = cur_line->next;
645 if ( (cur_line == NULL)
646 || (line_number != patternid)
648 || (cur_line->type != FILE_LINE_URL))
650 /* Invalid "patternid" parameter */
651 edit_free_file(file);
652 return JB_ERR_CGI_PARAMS;
655 if (NULL == (exports = default_exports(csp, NULL)))
657 edit_free_file(file);
658 return JB_ERR_MEMORY;
661 err = map(exports, "f", 1, file->identifier, 1);
662 if (!err) err = map(exports, "v", 1, file->version_str, 1);
663 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
664 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
666 edit_free_file(file);
674 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
678 /*********************************************************************
680 * Function : edit_write_file
682 * Description : Write a complete file to disk.
685 * 1 : filename = File to write to.
686 * 2 : file = Data structure to write.
688 * Returns : JB_ERR_OK on success
689 * JB_ERR_FILE on error writing to file.
690 * JB_ERR_MEMORY on out of memory
692 *********************************************************************/
693 jb_err edit_write_file(struct editable_file * file)
696 struct file_line * cur_line;
697 struct stat statbuf[1];
698 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
699 digits in time_t, assuming this is a 64-bit
700 machine, plus null terminator, plus one
704 assert(file->filename);
707 if (NULL == (fp = fopen(file->filename, "w")))
709 if (NULL == (fp = fopen(file->filename, "wt")))
710 #endif /* def AMIGA */
715 cur_line = file->lines;
716 while (cur_line != NULL)
720 if (fputs(cur_line->raw, fp) < 0)
728 if (cur_line->prefix)
730 if (fputs(cur_line->prefix, fp) < 0)
736 if (cur_line->unprocessed)
738 /* This should be a single line - sanity check. */
739 assert(NULL == strchr(cur_line->unprocessed, '\r'));
740 assert(NULL == strchr(cur_line->unprocessed, '\n'));
742 if (NULL != strchr(cur_line->unprocessed, '#'))
744 /* Must quote '#' characters */
751 /* Count number of # characters, so we know length of output string */
752 src = cur_line->unprocessed;
753 while (NULL != (src = strchr(src, '#')))
760 /* Allocate new memory for string */
761 len = strlen(cur_line->unprocessed);
762 if (NULL == (str = malloc(len + 1 + numhash)))
764 /* Uh oh, just trashed file! */
766 return JB_ERR_MEMORY;
769 /* Loop through string from end */
770 src = cur_line->unprocessed + len;
771 dest = str + len + numhash;
772 for ( ; len >= 0; len--)
774 if ((*dest-- = *src--) == '#')
778 assert(numhash >= 0);
781 assert(numhash == 0);
782 assert(src + 1 == cur_line->unprocessed);
783 assert(dest + 1 == str);
785 if (fputs(str, fp) < 0)
796 /* Can write without quoting '#' characters. */
797 if (fputs(cur_line->unprocessed, fp) < 0)
803 if (fputs(NEWLINE(file->newline), fp) < 0)
811 /* FIXME: Write data from file->data->whatever */
815 cur_line = cur_line->next;
821 /* Update the version stamp in the file structure, since we just
822 * wrote to the file & changed it's date.
824 if (stat(file->filename, statbuf) < 0)
826 /* Error, probably file not found. */
829 file->version = (unsigned)statbuf->st_mtime;
831 /* Correct file->version_str */
832 freez(file->version_str);
833 snprintf(version_buf, 22, "%u", file->version);
834 version_buf[21] = '\0';
835 file->version_str = strdup(version_buf);
836 if (version_buf == NULL)
838 return JB_ERR_MEMORY;
845 /*********************************************************************
847 * Function : edit_free_file
849 * Description : Free a complete file in memory.
852 * 1 : file = Data structure to free.
856 *********************************************************************/
857 void edit_free_file(struct editable_file * file)
861 /* Silently ignore NULL pointer */
865 edit_free_file_lines(file->lines);
866 freez(file->filename);
867 freez(file->identifier);
868 freez(file->version_str);
870 file->parse_error_text = NULL; /* Statically allocated */
871 file->parse_error = NULL;
877 /*********************************************************************
879 * Function : edit_free_file
881 * Description : Free an entire linked list of file lines.
884 * 1 : first_line = Data structure to free.
888 *********************************************************************/
889 static void edit_free_file_lines(struct file_line * first_line)
891 struct file_line * next_line;
893 while (first_line != NULL)
895 next_line = first_line->next;
896 first_line->next = NULL;
897 freez(first_line->raw);
898 freez(first_line->prefix);
899 freez(first_line->unprocessed);
900 switch(first_line->type)
902 case 0: /* special case if memory zeroed */
903 case FILE_LINE_UNPROCESSED:
904 case FILE_LINE_BLANK:
905 case FILE_LINE_ALIAS_HEADER:
906 case FILE_LINE_SETTINGS_HEADER:
907 case FILE_LINE_DESCRIPTION_HEADER:
908 case FILE_LINE_DESCRIPTION_ENTRY:
909 case FILE_LINE_ALIAS_ENTRY:
911 /* No data is stored for these */
914 case FILE_LINE_ACTION:
915 free_action(first_line->data.action);
918 case FILE_LINE_SETTINGS_ENTRY:
919 freez(first_line->data.setting.name);
920 freez(first_line->data.setting.svalue);
923 /* Should never happen */
927 first_line->type = 0; /* paranoia */
929 first_line = next_line;
934 /*********************************************************************
936 * Function : match_actions_file_header_line
938 * Description : Match an actions file {{header}} line
941 * 1 : line - String from file
942 * 2 : name - Header to match against
944 * Returns : 0 iff they match.
946 *********************************************************************/
947 static int match_actions_file_header_line(const char * line, const char * name)
955 if ((line[0] != '{') || (line[1] != '{'))
961 /* Look for optional whitespace */
962 while ( (*line == ' ') || (*line == '\t') )
967 /* Look for the specified name (case-insensitive) */
969 if (0 != strncmpic(line, name, len))
975 /* Look for optional whitespace */
976 while ( (*line == ' ') || (*line == '\t') )
981 /* Look for "}}" and end of string*/
982 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
992 /*********************************************************************
994 * Function : match_actions_file_header_line
996 * Description : Match an actions file {{header}} line
999 * 1 : line - String from file. Must not start with
1000 * whitespace (else infinite loop!)
1001 * 2 : name - Destination for name
1002 * 2 : name - Destination for value
1004 * Returns : JB_ERR_OK on success
1005 * JB_ERR_MEMORY on out-of-memory
1006 * JB_ERR_PARSE if there's no "=" sign, or if there's
1007 * nothing before the "=" sign (but empty
1008 * values *after* the "=" sign are legal).
1010 *********************************************************************/
1011 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1013 const char * name_end;
1014 const char * value_start;
1020 assert(*line != ' ');
1021 assert(*line != '\t');
1026 value_start = strchr(line, '=');
1027 if ((value_start == NULL) || (value_start == line))
1029 return JB_ERR_PARSE;
1032 name_end = value_start - 1;
1034 /* Eat any whitespace before the '=' */
1035 while ((*name_end == ' ') || (*name_end == '\t'))
1038 * we already know we must have at least 1 non-ws char
1039 * at start of buf - no need to check
1044 name_len = name_end - line + 1; /* Length excluding \0 */
1045 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1047 return JB_ERR_MEMORY;
1049 strncpy(*pname, line, name_len);
1050 (*pname)[name_len] = '\0';
1052 /* Eat any the whitespace after the '=' */
1054 while ((*value_start == ' ') || (*value_start == '\t'))
1059 if (NULL == (*pvalue = strdup(value_start)))
1063 return JB_ERR_MEMORY;
1070 /*********************************************************************
1072 * Function : edit_parse_actions_file
1074 * Description : Parse an actions file in memory.
1076 * Passed linked list must have the "data" member
1077 * zeroed, and must contain valid "next" and
1078 * "unprocessed" fields. The "raw" and "prefix"
1079 * fields are ignored, and "type" is just overwritten.
1081 * Note that on error the file may have been
1085 * 1 : file = Actions file to be parsed in-place.
1087 * Returns : JB_ERR_OK on success
1088 * JB_ERR_MEMORY on out-of-memory
1089 * JB_ERR_PARSE on error
1091 *********************************************************************/
1092 jb_err edit_parse_actions_file(struct editable_file * file)
1094 struct file_line * cur_line;
1096 const char * text; /* Text from a line */
1097 char * name; /* For lines of the form name=value */
1098 char * value; /* For lines of the form name=value */
1099 struct action_alias * alias_list = NULL;
1100 jb_err err = JB_ERR_OK;
1102 /* alias_list contains the aliases defined in this file.
1103 * It might be better to use the "file_line.data" fields
1104 * in the relavent places instead.
1107 cur_line = file->lines;
1109 /* A note about blank line support: Blank lines should only
1110 * ever occur as the last line in the file. This function
1111 * is more forgiving than that - FILE_LINE_BLANK can occur
1115 /* Skip leading blanks. Should only happen if file is
1116 * empty (which is valid, but pointless).
1118 while ( (cur_line != NULL)
1119 && (cur_line->unprocessed[0] == '\0') )
1122 cur_line->type = FILE_LINE_BLANK;
1123 cur_line = cur_line->next;
1126 if ( (cur_line != NULL)
1127 && (cur_line->unprocessed[0] != '{') )
1129 /* File doesn't start with a header */
1130 file->parse_error = cur_line;
1131 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1132 return JB_ERR_PARSE;
1135 if ( (cur_line != NULL) && (0 ==
1136 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1138 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1140 cur_line = cur_line->next;
1141 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1143 if (cur_line->unprocessed[0])
1145 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1147 err = split_line_on_equals(cur_line->unprocessed,
1148 &cur_line->data.setting.name,
1149 &cur_line->data.setting.svalue);
1150 if (err == JB_ERR_MEMORY)
1154 else if (err != JB_ERR_OK)
1156 /* Line does not contain a name=value pair */
1157 file->parse_error = cur_line;
1158 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1159 return JB_ERR_PARSE;
1164 cur_line->type = FILE_LINE_BLANK;
1166 cur_line = cur_line->next;
1170 if ( (cur_line != NULL) && (0 ==
1171 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1173 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1175 cur_line = cur_line->next;
1176 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1178 if (cur_line->unprocessed[0])
1180 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1184 cur_line->type = FILE_LINE_BLANK;
1186 cur_line = cur_line->next;
1190 if ( (cur_line != NULL) && (0 ==
1191 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1193 cur_line->type = FILE_LINE_ALIAS_HEADER;
1195 cur_line = cur_line->next;
1196 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1198 if (cur_line->unprocessed[0])
1200 /* define an alias */
1201 struct action_alias * new_alias;
1203 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1205 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1206 if (err == JB_ERR_MEMORY)
1210 else if (err != JB_ERR_OK)
1212 /* Line does not contain a name=value pair */
1213 file->parse_error = cur_line;
1214 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1215 return JB_ERR_PARSE;
1218 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1223 free_alias_list(alias_list);
1224 return JB_ERR_MEMORY;
1227 err = get_actions(value, alias_list, new_alias->action);
1230 /* Invalid action or out of memory */
1234 free_alias_list(alias_list);
1235 if (err == JB_ERR_MEMORY)
1241 /* Line does not contain a name=value pair */
1242 file->parse_error = cur_line;
1243 file->parse_error_text = "This alias does not specify a valid set of actions.";
1244 return JB_ERR_PARSE;
1250 new_alias->name = name;
1253 new_alias->next = alias_list;
1254 alias_list = new_alias;
1258 cur_line->type = FILE_LINE_BLANK;
1260 cur_line = cur_line->next;
1264 /* Header done, process the main part of the file */
1265 while (cur_line != NULL)
1267 /* At this point, (cur_line->unprocessed[0] == '{') */
1268 assert(cur_line->unprocessed[0] == '{');
1269 text = cur_line->unprocessed + 1;
1270 len = strlen(text) - 1;
1271 if (text[len] != '}')
1273 /* No closing } on header */
1274 free_alias_list(alias_list);
1275 file->parse_error = cur_line;
1276 file->parse_error_text = "Headers starting with '{' must have a "
1277 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1278 "must close with two brackets ('}}').";
1279 return JB_ERR_PARSE;
1284 /* An invalid {{ header. */
1285 free_alias_list(alias_list);
1286 file->parse_error = cur_line;
1287 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1288 "Please remember that the system (two-bracket) headers must "
1289 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1290 "and must appear before any actions (one-bracket) headers. "
1291 "Also note that system headers may not be repeated.";
1292 return JB_ERR_PARSE;
1295 while ( (*text == ' ') || (*text == '\t') )
1301 && ( (text[len - 1] == ' ')
1302 || (text[len - 1] == '\t') ) )
1307 cur_line->type = FILE_LINE_ACTION;
1309 /* Remove {} and make copy */
1310 if (NULL == (value = (char *) malloc(len + 1)))
1313 free_alias_list(alias_list);
1314 return JB_ERR_MEMORY;
1316 strncpy(value, text, len);
1320 err = get_actions(value, alias_list, cur_line->data.action);
1323 /* Invalid action or out of memory */
1325 free_alias_list(alias_list);
1326 if (err == JB_ERR_MEMORY)
1332 /* Line does not contain a name=value pair */
1333 file->parse_error = cur_line;
1334 file->parse_error_text = "This header does not specify a valid set of actions.";
1335 return JB_ERR_PARSE;
1339 /* Done with string - it was clobbered anyway */
1342 /* Process next line */
1343 cur_line = cur_line->next;
1345 /* Loop processing URL patterns */
1346 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1348 if (cur_line->unprocessed[0])
1350 /* Could parse URL here, but this isn't currently needed */
1352 cur_line->type = FILE_LINE_URL;
1356 cur_line->type = FILE_LINE_BLANK;
1358 cur_line = cur_line->next;
1360 } /* End main while(cur_line != NULL) loop */
1362 free_alias_list(alias_list);
1368 /*********************************************************************
1370 * Function : edit_read_file_lines
1372 * Description : Read all the lines of a file into memory.
1373 * Handles whitespace, comments and line continuation.
1376 * 1 : fp = File to read from. On return, this will be
1377 * at EOF but it will not have been closed.
1378 * 2 : pfile = Destination for a linked list of file_lines.
1379 * Will be set to NULL on error.
1381 * Returns : JB_ERR_OK on success
1382 * JB_ERR_MEMORY on out-of-memory
1384 *********************************************************************/
1385 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1387 struct file_line * first_line; /* Keep for return value or to free */
1388 struct file_line * cur_line; /* Current line */
1389 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1397 cur_line = first_line = zalloc(sizeof(struct file_line));
1398 if (cur_line == NULL)
1400 return JB_ERR_MEMORY;
1403 cur_line->type = FILE_LINE_UNPROCESSED;
1405 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1408 /* Out of memory or empty file. */
1409 /* Note that empty file is not an error we propogate up */
1411 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1416 prev_line = cur_line;
1417 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1418 if (cur_line == NULL)
1421 edit_free_file_lines(first_line);
1422 return JB_ERR_MEMORY;
1425 cur_line->type = FILE_LINE_UNPROCESSED;
1427 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1428 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1431 edit_free_file_lines(first_line);
1432 return JB_ERR_MEMORY;
1436 while (rval != JB_ERR_FILE);
1440 /* We allocated one too many - free it */
1441 prev_line->next = NULL;
1444 *pfile = first_line;
1449 /*********************************************************************
1451 * Function : edit_read_file
1453 * Description : Read a complete file into memory.
1454 * Handles CGI parameter parsing. If requested, also
1455 * checks the file's modification timestamp.
1458 * 1 : csp = Current client state (buffers, headers, etc...)
1459 * 2 : parameters = map of cgi parameters.
1460 * 3 : require_version = true to check "ver" parameter.
1461 * 4 : suffix = File extension, e.g. ".action".
1462 * 5 : pfile = Destination for the file. Will be set
1466 * filename : The name of the file to read, without the
1467 * path or ".action" extension.
1468 * ver : (Only if require_version is nonzero)
1469 * Timestamp of the actions file. If wrong, this
1470 * function fails with JB_ERR_MODIFIED.
1472 * Returns : JB_ERR_OK on success
1473 * JB_ERR_MEMORY on out-of-memory
1474 * JB_ERR_CGI_PARAMS if "filename" was not specified
1476 * JB_ERR_FILE if the file cannot be opened or
1478 * JB_ERR_MODIFIED if version checking was requested and
1479 * failed - the file was modified outside
1480 * of this CGI editor instance.
1482 *********************************************************************/
1483 jb_err edit_read_file(struct client_state *csp,
1484 const struct map *parameters,
1485 int require_version,
1487 struct editable_file **pfile)
1489 struct file_line * lines;
1493 const char * identifier;
1494 struct editable_file * file;
1495 unsigned version = 0;
1496 struct stat statbuf[1];
1497 char version_buf[22];
1498 int newline = NEWLINE_UNKNOWN;
1506 err = get_file_name_param(csp, parameters, "f", suffix,
1507 &filename, &identifier);
1513 if (stat(filename, statbuf) < 0)
1515 /* Error, probably file not found. */
1519 version = (unsigned) statbuf->st_mtime;
1521 if (require_version)
1523 unsigned specified_version;
1524 err = get_number_param(csp, parameters, "v", &specified_version);
1531 if (version != specified_version)
1533 return JB_ERR_MODIFIED;
1538 if (NULL == (fp = fopen(filename,"r")))
1540 if (NULL == (fp = fopen(filename,"rt")))
1541 #endif /* def AMIGA */
1547 err = edit_read_file_lines(fp, &lines, &newline);
1557 file = (struct editable_file *) zalloc(sizeof(*file));
1561 edit_free_file_lines(lines);
1565 file->lines = lines;
1566 file->newline = newline;
1567 file->filename = filename;
1568 file->version = version;
1569 file->identifier = url_encode(identifier);
1571 if (file->identifier == NULL)
1573 edit_free_file(file);
1574 return JB_ERR_MEMORY;
1577 /* Correct file->version_str */
1578 freez(file->version_str);
1579 snprintf(version_buf, 22, "%u", file->version);
1580 version_buf[21] = '\0';
1581 file->version_str = strdup(version_buf);
1582 if (version_buf == NULL)
1584 edit_free_file(file);
1585 return JB_ERR_MEMORY;
1593 /*********************************************************************
1595 * Function : edit_read_actions_file
1597 * Description : Read a complete actions file into memory.
1598 * Handles CGI parameter parsing. If requested, also
1599 * checks the file's modification timestamp.
1601 * If this function detects an error in the categories
1602 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1603 * then it handles it by filling in the specified
1604 * response structure and returning JB_ERR_FILE.
1607 * 1 : csp = Current client state (buffers, headers, etc...)
1608 * 2 : rsp = HTTP response. Only filled in on error.
1609 * 2 : parameters = map of cgi parameters.
1610 * 3 : require_version = true to check "ver" parameter.
1611 * 4 : pfile = Destination for the file. Will be set
1615 * filename : The name of the actions file to read, without the
1616 * path or ".action" extension.
1617 * ver : (Only if require_version is nonzero)
1618 * Timestamp of the actions file. If wrong, this
1619 * function fails with JB_ERR_MODIFIED.
1621 * Returns : JB_ERR_OK on success
1622 * JB_ERR_MEMORY on out-of-memory
1623 * JB_ERR_CGI_PARAMS if "filename" was not specified
1625 * JB_ERR_FILE if the file does not contain valid data,
1626 * or if file cannot be opened or
1627 * contains no data, or if version
1628 * checking was requested and failed.
1630 *********************************************************************/
1631 jb_err edit_read_actions_file(struct client_state *csp,
1632 struct http_response *rsp,
1633 const struct map *parameters,
1634 int require_version,
1635 struct editable_file **pfile)
1638 struct editable_file *file;
1646 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1649 /* Try to handle if possible */
1650 if (err == JB_ERR_FILE)
1652 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1654 else if (err == JB_ERR_MODIFIED)
1656 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1658 if (err == JB_ERR_OK)
1661 * Signal to higher-level CGI code that there was a problem but we
1662 * handled it, they should just return JB_ERR_OK.
1669 err = edit_parse_actions_file(file);
1672 if (err == JB_ERR_PARSE)
1674 err = cgi_error_parse(csp, rsp, file);
1675 if (err == JB_ERR_OK)
1678 * Signal to higher-level CGI code that there was a problem but we
1679 * handled it, they should just return JB_ERR_OK.
1684 edit_free_file(file);
1693 /*********************************************************************
1695 * Function : get_file_name_param
1697 * Description : Get the name of the file to edit from the parameters
1698 * passed to a CGI function. This function handles
1699 * security checks such as blocking urls containing
1700 * "/" or ".", prepending the config file directory,
1701 * and adding the specified suffix.
1703 * (This is an essential security check, otherwise
1704 * users may be able to pass "../../../etc/passwd"
1705 * and overwrite the password file [linux], "prn:"
1706 * and print random data [Windows], etc...)
1708 * This function only allows filenames contining the
1709 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1710 * That's probably too restrictive but at least it's
1714 * 1 : csp = Current client state (buffers, headers, etc...)
1715 * 2 : parameters = map of cgi parameters
1716 * 3 : param_name = The name of the parameter to read
1717 * 4 : suffix = File extension, e.g. ".actions"
1718 * 5 : pfilename = destination for full filename. Caller
1719 * free()s. Set to NULL on error.
1720 * 6 : pparam = destination for partial filename,
1721 * suitable for use in another URL. Allocated as part
1722 * of the map "parameters", so don't free it.
1723 * Set to NULL if not specified.
1725 * Returns : JB_ERR_OK on success
1726 * JB_ERR_MEMORY on out-of-memory
1727 * JB_ERR_CGI_PARAMS if "filename" was not specified
1730 *********************************************************************/
1731 static jb_err get_file_name_param(struct client_state *csp,
1732 const struct map *parameters,
1733 const char *param_name,
1736 const char **pparam)
1754 param = lookup(parameters, param_name);
1757 return JB_ERR_CGI_PARAMS;
1762 len = strlen(param);
1763 if (len >= FILENAME_MAX)
1766 return JB_ERR_CGI_PARAMS;
1769 /* Check every character to see if it's legal */
1771 while ((ch = *s++) != '\0')
1773 if ( ((ch < 'A') || (ch > 'Z'))
1774 && ((ch < 'a') || (ch > 'z'))
1775 && ((ch < '0') || (ch > '9'))
1779 /* Probable hack attempt. */
1780 return JB_ERR_CGI_PARAMS;
1784 /* Append extension */
1785 name = malloc(len + strlen(suffix) + 1);
1788 return JB_ERR_MEMORY;
1790 strcpy(name, param);
1791 strcpy(name + len, suffix);
1794 fullpath = make_path(csp->config->confdir, name);
1796 if (fullpath == NULL)
1798 return JB_ERR_MEMORY;
1802 *pfilename = fullpath;
1808 /*********************************************************************
1810 * Function : get_number_param
1812 * Description : Get a non-negative integer from the parameters
1813 * passed to a CGI function.
1816 * 1 : csp = Current client state (buffers, headers, etc...)
1817 * 2 : parameters = map of cgi parameters
1818 * 3 : name = Name of CGI parameter to read
1819 * 4 : pvalue = destination for value.
1820 * Set to -1 on error.
1822 * Returns : JB_ERR_OK on success
1823 * JB_ERR_MEMORY on out-of-memory
1824 * JB_ERR_CGI_PARAMS if the parameter was not specified
1827 *********************************************************************/
1828 static jb_err get_number_param(struct client_state *csp,
1829 const struct map *parameters,
1844 param = lookup(parameters, name);
1847 return JB_ERR_CGI_PARAMS;
1850 /* We don't use atoi because I want to check this carefully... */
1853 while ((ch = *param++) != '\0')
1855 if ((ch < '0') || (ch > '9'))
1857 return JB_ERR_CGI_PARAMS;
1864 * <limits.h> defines UINT_MAX
1866 * (UINT_MAX - ch) / 10 is the largest number that
1867 * can be safely multiplied by 10 then have ch added.
1869 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1871 return JB_ERR_CGI_PARAMS;
1874 value = value * 10 + ch;
1884 /*********************************************************************
1886 * Function : get_url_spec_param
1888 * Description : Get a URL pattern from the parameters
1889 * passed to a CGI function. Removes leading/trailing
1890 * spaces and validates it.
1893 * 1 : csp = Current client state (buffers, headers, etc...)
1894 * 2 : parameters = map of cgi parameters
1895 * 3 : name = Name of CGI parameter to read
1896 * 4 : pvalue = destination for value. Will be malloc()'d.
1897 * Set to NULL on error.
1899 * Returns : JB_ERR_OK on success
1900 * JB_ERR_MEMORY on out-of-memory
1901 * JB_ERR_CGI_PARAMS if the parameter was not specified
1904 *********************************************************************/
1905 static jb_err get_url_spec_param(struct client_state *csp,
1906 const struct map *parameters,
1910 const char *orig_param;
1913 struct url_spec compiled[1];
1923 orig_param = lookup(parameters, name);
1926 return JB_ERR_CGI_PARAMS;
1929 /* Copy and trim whitespace */
1930 param = strdup(orig_param);
1933 return JB_ERR_MEMORY;
1937 /* Must be non-empty, and can't allow 1st character to be '{' */
1938 if (param[0] == '\0' || param[0] == '{')
1941 return JB_ERR_CGI_PARAMS;
1944 /* Check for embedded newlines */
1945 for (s = param; *s != '\0'; s++)
1947 if ((*s == '\r') || (*s == '\n'))
1950 return JB_ERR_CGI_PARAMS;
1954 /* Check that regex is valid */
1959 return JB_ERR_MEMORY;
1961 err = create_url_spec(compiled, s);
1966 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1968 free_url_spec(compiled);
1970 if (param[strlen(param) - 1] == '\\')
1973 * Must protect trailing '\\' from becoming line continuation character.
1974 * Two methods: 1) If it's a domain only, add a trailing '/'.
1975 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1977 if (strchr(param, '/') == NULL)
1979 err = string_append(¶m, "/");
1983 err = string_append(¶m, "(?:)");
1990 /* Check that the modified regex is valid */
1995 return JB_ERR_MEMORY;
1997 err = create_url_spec(compiled, s);
2002 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2004 free_url_spec(compiled);
2011 /*********************************************************************
2013 * Function : map_radio
2015 * Description : Map a set of radio button values. E.g. if you have
2016 * 3 radio buttons, declare them as:
2017 * <option type="radio" name="xyz" @xyz-a@>
2018 * <option type="radio" name="xyz" @xyz-b@>
2019 * <option type="radio" name="xyz" @xyz-c@>
2020 * Then map one of the @xyz-?@ variables to "checked"
2021 * and all the others to empty by calling:
2022 * map_radio(exports, "xyz", "abc", sel)
2023 * Where 'sel' is 'a', 'b', or 'c'.
2026 * 1 : exports = Exports map to modify.
2027 * 2 : optionname = name for map
2028 * 3 : values = null-terminated list of values;
2029 * 4 : value = Selected value.
2031 * CGI Parameters : None
2033 * Returns : JB_ERR_OK on success
2034 * JB_ERR_MEMORY on out-of-memory
2036 *********************************************************************/
2037 static jb_err map_radio(struct map * exports,
2038 const char * optionname,
2039 const char * values,
2051 len = strlen(optionname);
2052 buf = malloc(len + 3);
2055 return JB_ERR_MEMORY;
2058 strcpy(buf, optionname);
2063 while ((c = *values++) != '\0')
2068 if (map(exports, buf, 1, "", 1))
2071 return JB_ERR_MEMORY;
2077 if (map(exports, buf, 0, "checked", 1))
2080 return JB_ERR_MEMORY;
2087 /*********************************************************************
2089 * Function : cgi_error_modified
2091 * Description : CGI function that is called when a file is modified
2092 * outside the CGI editor.
2095 * 1 : csp = Current client state (buffers, headers, etc...)
2096 * 2 : rsp = http_response data structure for output
2097 * 3 : filename = The file that was modified.
2099 * CGI Parameters : none
2101 * Returns : JB_ERR_OK on success
2102 * JB_ERR_MEMORY on out-of-memory error.
2104 *********************************************************************/
2105 jb_err cgi_error_modified(struct client_state *csp,
2106 struct http_response *rsp,
2107 const char *filename)
2109 struct map *exports;
2116 if (NULL == (exports = default_exports(csp, NULL)))
2118 return JB_ERR_MEMORY;
2121 err = map(exports, "f", 1, html_encode(filename), 0);
2128 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2132 /*********************************************************************
2134 * Function : cgi_error_parse
2136 * Description : CGI function that is called when a file cannot
2137 * be parsed by the CGI editor.
2140 * 1 : csp = Current client state (buffers, headers, etc...)
2141 * 2 : rsp = http_response data structure for output
2142 * 3 : file = The file that was modified.
2144 * CGI Parameters : none
2146 * Returns : JB_ERR_OK on success
2147 * JB_ERR_MEMORY on out-of-memory error.
2149 *********************************************************************/
2150 jb_err cgi_error_parse(struct client_state *csp,
2151 struct http_response *rsp,
2152 struct editable_file *file)
2154 struct map *exports;
2156 struct file_line *cur_line;
2162 if (NULL == (exports = default_exports(csp, NULL)))
2164 return JB_ERR_MEMORY;
2167 err = map(exports, "f", 1, file->identifier, 1);
2168 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2170 cur_line = file->parse_error;
2173 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2174 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2182 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2186 /*********************************************************************
2188 * Function : cgi_error_file
2190 * Description : CGI function that is called when a file cannot be
2191 * opened by the CGI editor.
2194 * 1 : csp = Current client state (buffers, headers, etc...)
2195 * 2 : rsp = http_response data structure for output
2196 * 3 : filename = The file that was modified.
2198 * CGI Parameters : none
2200 * Returns : JB_ERR_OK on success
2201 * JB_ERR_MEMORY on out-of-memory error.
2203 *********************************************************************/
2204 jb_err cgi_error_file(struct client_state *csp,
2205 struct http_response *rsp,
2206 const char *filename)
2208 struct map *exports;
2215 if (NULL == (exports = default_exports(csp, NULL)))
2217 return JB_ERR_MEMORY;
2220 err = map(exports, "f", 1, html_encode(filename), 0);
2227 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2231 /*********************************************************************
2233 * Function : cgi_error_bad_param
2235 * Description : CGI function that is called if the parameters
2236 * (query string) for a CGI were wrong.
2239 * 1 : csp = Current client state (buffers, headers, etc...)
2240 * 2 : rsp = http_response data structure for output
2242 * CGI Parameters : none
2244 * Returns : JB_ERR_OK on success
2245 * JB_ERR_MEMORY on out-of-memory error.
2247 *********************************************************************/
2248 jb_err cgi_error_disabled(struct client_state *csp,
2249 struct http_response *rsp)
2251 struct map *exports;
2256 if (NULL == (exports = default_exports(csp, NULL)))
2258 return JB_ERR_MEMORY;
2261 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2265 /*********************************************************************
2267 * Function : cgi_edit_actions
2269 * Description : CGI function that allows the user to choose which
2270 * actions file to edit.
2273 * 1 : csp = Current client state (buffers, headers, etc...)
2274 * 2 : rsp = http_response data structure for output
2275 * 3 : parameters = map of cgi parameters
2277 * CGI Parameters : None
2279 * Returns : JB_ERR_OK on success
2280 * JB_ERR_MEMORY on out-of-memory error
2282 *********************************************************************/
2283 jb_err cgi_edit_actions(struct client_state *csp,
2284 struct http_response *rsp,
2285 const struct map *parameters)
2288 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2290 return cgi_error_disabled(csp, rsp);
2293 /* FIXME: Incomplete */
2294 rsp->status = strdup("302 Local Redirect from Junkbuster");
2295 if (rsp->status == NULL)
2297 return JB_ERR_MEMORY;
2299 if (enlist_unique_header(rsp->headers, "Location",
2300 CGI_PREFIX "edit-actions-list?f=ijb"))
2304 return JB_ERR_MEMORY;
2311 /*********************************************************************
2313 * Function : cgi_edit_actions_list
2315 * Description : CGI function that edits the actions list.
2316 * FIXME: This function shouldn't FATAL ever.
2317 * FIXME: This function doesn't check the retval of map()
2319 * 1 : csp = Current client state (buffers, headers, etc...)
2320 * 2 : rsp = http_response data structure for output
2321 * 3 : parameters = map of cgi parameters
2323 * CGI Parameters : filename
2325 * Returns : JB_ERR_OK on success
2326 * JB_ERR_MEMORY on out-of-memory
2327 * JB_ERR_FILE if the file cannot be opened or
2329 * JB_ERR_CGI_PARAMS if "filename" was not specified
2332 *********************************************************************/
2333 jb_err cgi_edit_actions_list(struct client_state *csp,
2334 struct http_response *rsp,
2335 const struct map *parameters)
2337 char * section_template;
2338 char * url_template;
2343 struct map * exports;
2344 struct map * section_exports;
2345 struct map * url_exports;
2346 struct editable_file * file;
2347 struct file_line * cur_line;
2348 unsigned line_number = 0;
2349 unsigned prev_section_line_number = ((unsigned) (-1));
2353 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2355 return cgi_error_disabled(csp, rsp);
2358 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2361 /* No filename specified, can't read file, or out of memory. */
2362 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2365 if (NULL == (exports = default_exports(csp, NULL)))
2367 edit_free_file(file);
2368 return JB_ERR_MEMORY;
2371 err = map(exports, "f", 1, file->identifier, 1);
2372 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2376 edit_free_file(file);
2381 /* Should do all global exports above this point */
2383 err = template_load(csp, §ion_template, "edit-actions-list-section");
2386 edit_free_file(file);
2388 if (err == JB_ERR_FILE)
2390 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2395 err = template_load(csp, &url_template, "edit-actions-list-url");
2398 free(section_template);
2399 edit_free_file(file);
2401 if (err == JB_ERR_FILE)
2403 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2408 err = template_fill(§ion_template, exports);
2412 edit_free_file(file);
2418 err = template_fill(&url_template, exports);
2421 free(section_template);
2422 edit_free_file(file);
2427 /* Find start of actions in file */
2428 cur_line = file->lines;
2430 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2432 cur_line = cur_line->next;
2436 if (NULL == (sections = strdup("")))
2438 free(section_template);
2440 edit_free_file(file);
2442 return JB_ERR_MEMORY;
2445 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2447 if (NULL == (section_exports = new_map()))
2450 free(section_template);
2452 edit_free_file(file);
2454 return JB_ERR_MEMORY;
2457 snprintf(buf, 50, "%d", line_number);
2458 err = map(section_exports, "s", 1, buf, 1);
2459 if (!err) err = map(section_exports, "actions", 1,
2460 actions_to_html(cur_line->data.action), 0);
2463 && (cur_line->next != NULL)
2464 && (cur_line->next->type == FILE_LINE_URL))
2466 /* This section contains at least one URL, don't allow delete */
2467 err = map_block_killer(section_exports, "empty-section");
2471 if (!err) err = map_block_keep(section_exports, "empty-section");
2474 if (prev_section_line_number != ((unsigned)(-1)))
2476 /* Not last section */
2477 snprintf(buf, 50, "%d", prev_section_line_number);
2478 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2479 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2484 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2486 prev_section_line_number = line_number;
2491 free(section_template);
2493 edit_free_file(file);
2495 free_map(section_exports);
2499 /* Should do all section-specific exports above this point */
2501 if (NULL == (urls = strdup("")))
2504 free(section_template);
2506 edit_free_file(file);
2508 free_map(section_exports);
2509 return JB_ERR_MEMORY;
2514 cur_line = cur_line->next;
2517 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2519 if (NULL == (url_exports = new_map()))
2523 free(section_template);
2525 edit_free_file(file);
2527 free_map(section_exports);
2528 return JB_ERR_MEMORY;
2531 snprintf(buf, 50, "%d", line_number);
2532 err = map(url_exports, "p", 1, buf, 1);
2534 snprintf(buf, 50, "%d", url_1_2);
2535 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2537 if (!err) err = map(url_exports, "url-html", 1,
2538 html_encode(cur_line->unprocessed), 0);
2539 if (!err) err = map(url_exports, "url", 1,
2540 url_encode(cur_line->unprocessed), 0);
2546 free(section_template);
2548 edit_free_file(file);
2550 free_map(section_exports);
2551 free_map(url_exports);
2555 if (NULL == (s = strdup(url_template)))
2559 free(section_template);
2561 edit_free_file(file);
2563 free_map(section_exports);
2564 free_map(url_exports);
2565 return JB_ERR_MEMORY;
2568 err = template_fill(&s, section_exports);
2569 if (!err) err = template_fill(&s, url_exports);
2570 if (!err) err = string_append(&urls, s);
2572 free_map(url_exports);
2579 free(section_template);
2581 edit_free_file(file);
2583 free_map(section_exports);
2587 url_1_2 = 3 - url_1_2;
2589 cur_line = cur_line->next;
2593 err = map(section_exports, "urls", 1, urls, 0);
2595 /* Could also do section-specific exports here, but it wouldn't be as fast */
2597 if ( (cur_line != NULL)
2598 && (cur_line->type == FILE_LINE_ACTION))
2600 /* Not last section */
2601 snprintf(buf, 50, "%d", line_number);
2602 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2603 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2608 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2614 free(section_template);
2616 edit_free_file(file);
2618 free_map(section_exports);
2622 if (NULL == (s = strdup(section_template)))
2625 free(section_template);
2627 edit_free_file(file);
2629 free_map(section_exports);
2630 return JB_ERR_MEMORY;
2633 err = template_fill(&s, section_exports);
2634 if (!err) err = string_append(§ions, s);
2637 free_map(section_exports);
2642 free(section_template);
2644 edit_free_file(file);
2650 edit_free_file(file);
2651 free(section_template);
2654 err = map(exports, "sections", 1, sections, 0);
2661 /* Could also do global exports here, but it wouldn't be as fast */
2663 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2667 /*********************************************************************
2669 * Function : cgi_edit_actions
2671 * Description : CGI function that edits the Actions list.
2674 * 1 : csp = Current client state (buffers, headers, etc...)
2675 * 2 : rsp = http_response data structure for output
2676 * 3 : parameters = map of cgi parameters
2678 * CGI Parameters : None
2680 * Returns : JB_ERR_OK on success
2681 * JB_ERR_MEMORY on out-of-memory
2682 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2683 * specified or not valid.
2685 *********************************************************************/
2686 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2687 struct http_response *rsp,
2688 const struct map *parameters)
2690 struct map * exports;
2692 struct editable_file * file;
2693 struct file_line * cur_line;
2694 unsigned line_number;
2697 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2699 return cgi_error_disabled(csp, rsp);
2702 err = get_number_param(csp, parameters, "s", §ionid);
2708 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2711 /* No filename specified, can't read file, modified, or out of memory. */
2712 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2715 cur_line = file->lines;
2717 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2719 cur_line = cur_line->next;
2722 if ( (cur_line == NULL)
2723 || (line_number != sectionid)
2725 || (cur_line->type != FILE_LINE_ACTION))
2727 /* Invalid "sectionid" parameter */
2728 edit_free_file(file);
2729 return JB_ERR_CGI_PARAMS;
2732 if (NULL == (exports = default_exports(csp, NULL)))
2734 edit_free_file(file);
2735 return JB_ERR_MEMORY;
2738 err = map(exports, "f", 1, file->identifier, 1);
2739 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2740 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2742 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2744 edit_free_file(file);
2752 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2756 /*********************************************************************
2758 * Function : cgi_edit_actions_submit
2760 * Description : CGI function that actually edits the Actions list.
2763 * 1 : csp = Current client state (buffers, headers, etc...)
2764 * 2 : rsp = http_response data structure for output
2765 * 3 : parameters = map of cgi parameters
2767 * CGI Parameters : None
2769 * Returns : JB_ERR_OK on success
2770 * JB_ERR_MEMORY on out-of-memory
2771 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2772 * specified or not valid.
2774 *********************************************************************/
2775 jb_err cgi_edit_actions_submit(struct client_state *csp,
2776 struct http_response *rsp,
2777 const struct map *parameters)
2783 struct editable_file * file;
2784 struct file_line * cur_line;
2785 unsigned line_number;
2789 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2791 return cgi_error_disabled(csp, rsp);
2794 err = get_number_param(csp, parameters, "s", §ionid);
2800 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2803 /* No filename specified, can't read file, modified, or out of memory. */
2804 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2807 cur_line = file->lines;
2809 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2811 cur_line = cur_line->next;
2814 if ( (cur_line == NULL)
2815 || (line_number != sectionid)
2817 || (cur_line->type != FILE_LINE_ACTION))
2819 /* Invalid "sectionid" parameter */
2820 edit_free_file(file);
2821 return JB_ERR_CGI_PARAMS;
2824 err = actions_from_radio(parameters, cur_line->data.action);
2828 edit_free_file(file);
2832 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2835 edit_free_file(file);
2836 return JB_ERR_MEMORY;
2839 len = strlen(actiontext);
2843 * Empty action - must special-case this.
2844 * Simply setting len to 1 is sufficient...
2849 if (NULL == (newtext = malloc(len + 2)))
2853 edit_free_file(file);
2854 return JB_ERR_MEMORY;
2856 strcpy(newtext, actiontext);
2860 newtext[len + 1] = '\0';
2862 freez(cur_line->raw);
2863 freez(cur_line->unprocessed);
2864 cur_line->unprocessed = newtext;
2866 err = edit_write_file(file);
2869 /* Error writing file */
2870 edit_free_file(file);
2874 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2875 string_append(&target, file->identifier);
2877 edit_free_file(file);
2882 return JB_ERR_MEMORY;
2885 rsp->status = strdup("302 Local Redirect from Junkbuster");
2886 if (rsp->status == NULL)
2889 return JB_ERR_MEMORY;
2891 err = enlist_unique_header(rsp->headers, "Location", target);
2898 /*********************************************************************
2900 * Function : cgi_edit_actions_url
2902 * Description : CGI function that actually edits a URL pattern in
2906 * 1 : csp = Current client state (buffers, headers, etc...)
2907 * 2 : rsp = http_response data structure for output
2908 * 3 : parameters = map of cgi parameters
2911 * filename : Identifies the file to edit
2912 * ver : File's last-modified time
2913 * section : Line number of section to edit
2914 * pattern : Line number of pattern to edit
2915 * newval : New value for pattern
2917 * Returns : JB_ERR_OK on success
2918 * JB_ERR_MEMORY on out-of-memory
2919 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2920 * specified or not valid.
2922 *********************************************************************/
2923 jb_err cgi_edit_actions_url(struct client_state *csp,
2924 struct http_response *rsp,
2925 const struct map *parameters)
2929 struct editable_file * file;
2930 struct file_line * cur_line;
2931 unsigned line_number;
2935 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2937 return cgi_error_disabled(csp, rsp);
2940 err = get_number_param(csp, parameters, "p", &patternid);
2947 return JB_ERR_CGI_PARAMS;
2950 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
2956 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2959 /* No filename specified, can't read file, modified, or out of memory. */
2961 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2965 cur_line = file->lines;
2967 while ((cur_line != NULL) && (line_number < patternid))
2969 cur_line = cur_line->next;
2973 if ( (cur_line == NULL)
2974 || (cur_line->type != FILE_LINE_URL))
2976 /* Invalid "patternid" parameter */
2978 edit_free_file(file);
2979 return JB_ERR_CGI_PARAMS;
2982 /* At this point, the line to edit is in cur_line */
2984 freez(cur_line->raw);
2985 freez(cur_line->unprocessed);
2986 cur_line->unprocessed = new_pattern;
2988 err = edit_write_file(file);
2991 /* Error writing file */
2992 edit_free_file(file);
2996 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2997 string_append(&target, file->identifier);
2999 edit_free_file(file);
3004 return JB_ERR_MEMORY;
3007 rsp->status = strdup("302 Local Redirect from Junkbuster");
3008 if (rsp->status == NULL)
3011 return JB_ERR_MEMORY;
3013 err = enlist_unique_header(rsp->headers, "Location", target);
3020 /*********************************************************************
3022 * Function : cgi_edit_actions_add_url
3024 * Description : CGI function that actually adds a URL pattern to
3028 * 1 : csp = Current client state (buffers, headers, etc...)
3029 * 2 : rsp = http_response data structure for output
3030 * 3 : parameters = map of cgi parameters
3033 * filename : Identifies the file to edit
3034 * ver : File's last-modified time
3035 * section : Line number of section to edit
3036 * newval : New pattern
3038 * Returns : JB_ERR_OK on success
3039 * JB_ERR_MEMORY on out-of-memory
3040 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3041 * specified or not valid.
3043 *********************************************************************/
3044 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3045 struct http_response *rsp,
3046 const struct map *parameters)
3050 struct file_line * new_line;
3051 struct editable_file * file;
3052 struct file_line * cur_line;
3053 unsigned line_number;
3057 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3059 return cgi_error_disabled(csp, rsp);
3062 err = get_number_param(csp, parameters, "s", §ionid);
3069 return JB_ERR_CGI_PARAMS;
3072 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3078 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3081 /* No filename specified, can't read file, modified, or out of memory. */
3083 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3087 cur_line = file->lines;
3089 while ((cur_line != NULL) && (line_number < sectionid))
3091 cur_line = cur_line->next;
3095 if ( (cur_line == NULL)
3096 || (cur_line->type != FILE_LINE_ACTION))
3098 /* Invalid "sectionid" parameter */
3100 edit_free_file(file);
3101 return JB_ERR_CGI_PARAMS;
3104 /* At this point, the section header is in cur_line - add after this. */
3106 /* Allocate the new line */
3107 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3108 if (new_line == NULL)
3111 edit_free_file(file);
3112 return JB_ERR_MEMORY;
3115 /* Fill in the data members of the new line */
3116 new_line->raw = NULL;
3117 new_line->prefix = NULL;
3118 new_line->unprocessed = new_pattern;
3119 new_line->type = FILE_LINE_URL;
3121 /* Link new_line into the list, after cur_line */
3122 new_line->next = cur_line->next;
3123 cur_line->next = new_line;
3125 /* Done making changes, now commit */
3127 err = edit_write_file(file);
3130 /* Error writing file */
3131 edit_free_file(file);
3135 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3136 string_append(&target, file->identifier);
3138 edit_free_file(file);
3143 return JB_ERR_MEMORY;
3146 rsp->status = strdup("302 Local Redirect from Junkbuster");
3147 if (rsp->status == NULL)
3150 return JB_ERR_MEMORY;
3152 err = enlist_unique_header(rsp->headers, "Location", target);
3159 /*********************************************************************
3161 * Function : cgi_edit_actions_remove_url
3163 * Description : CGI function that actually removes a URL pattern from
3167 * 1 : csp = Current client state (buffers, headers, etc...)
3168 * 2 : rsp = http_response data structure for output
3169 * 3 : parameters = map of cgi parameters
3172 * f : (filename) Identifies the file to edit
3173 * v : (version) File's last-modified time
3174 * p : (pattern) Line number of pattern to remove
3176 * Returns : JB_ERR_OK on success
3177 * JB_ERR_MEMORY on out-of-memory
3178 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3179 * specified or not valid.
3181 *********************************************************************/
3182 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3183 struct http_response *rsp,
3184 const struct map *parameters)
3187 struct editable_file * file;
3188 struct file_line * cur_line;
3189 struct file_line * prev_line;
3190 unsigned line_number;
3194 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3196 return cgi_error_disabled(csp, rsp);
3199 err = get_number_param(csp, parameters, "p", &patternid);
3205 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3208 /* No filename specified, can't read file, modified, or out of memory. */
3209 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3214 cur_line = file->lines;
3216 while ((cur_line != NULL) && (line_number < patternid))
3218 prev_line = cur_line;
3219 cur_line = cur_line->next;
3223 if ( (cur_line == NULL)
3224 || (prev_line == NULL)
3225 || (cur_line->type != FILE_LINE_URL))
3227 /* Invalid "patternid" parameter */
3228 edit_free_file(file);
3229 return JB_ERR_CGI_PARAMS;
3232 /* At this point, the line to remove is in cur_line, and the previous
3233 * one is in prev_line
3236 /* Unlink cur_line */
3237 prev_line->next = cur_line->next;
3238 cur_line->next = NULL;
3241 edit_free_file_lines(cur_line);
3243 err = edit_write_file(file);
3246 /* Error writing file */
3247 edit_free_file(file);
3251 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3252 string_append(&target, file->identifier);
3254 edit_free_file(file);
3259 return JB_ERR_MEMORY;
3262 rsp->status = strdup("302 Local Redirect from Junkbuster");
3263 if (rsp->status == NULL)
3266 return JB_ERR_MEMORY;
3268 err = enlist_unique_header(rsp->headers, "Location", target);
3275 /*********************************************************************
3277 * Function : cgi_edit_actions_section_remove
3279 * Description : CGI function that actually removes a whole section from
3280 * the actions file. The section must be empty first
3281 * (else JB_ERR_CGI_PARAMS).
3284 * 1 : csp = Current client state (buffers, headers, etc...)
3285 * 2 : rsp = http_response data structure for output
3286 * 3 : parameters = map of cgi parameters
3289 * f : (filename) Identifies the file to edit
3290 * v : (version) File's last-modified time
3291 * s : (section) Line number of section to edit
3293 * Returns : JB_ERR_OK on success
3294 * JB_ERR_MEMORY on out-of-memory
3295 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3296 * specified or not valid.
3298 *********************************************************************/
3299 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3300 struct http_response *rsp,
3301 const struct map *parameters)
3304 struct editable_file * file;
3305 struct file_line * cur_line;
3306 struct file_line * prev_line;
3307 unsigned line_number;
3311 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3313 return cgi_error_disabled(csp, rsp);
3316 err = get_number_param(csp, parameters, "s", §ionid);
3322 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3325 /* No filename specified, can't read file, modified, or out of memory. */
3326 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3330 cur_line = file->lines;
3333 while ((cur_line != NULL) && (line_number < sectionid))
3335 prev_line = cur_line;
3336 cur_line = cur_line->next;
3340 if ( (cur_line == NULL)
3341 || (cur_line->type != FILE_LINE_ACTION) )
3343 /* Invalid "sectionid" parameter */
3344 edit_free_file(file);
3345 return JB_ERR_CGI_PARAMS;
3348 if ( (cur_line->next != NULL)
3349 && (cur_line->next->type == FILE_LINE_URL) )
3351 /* Section not empty. */
3352 edit_free_file(file);
3353 return JB_ERR_CGI_PARAMS;
3356 /* At this point, the line to remove is in cur_line, and the previous
3357 * one is in prev_line
3360 /* Unlink cur_line */
3361 if (prev_line == NULL)
3363 /* Removing the first line from the file */
3364 file->lines = cur_line->next;
3368 prev_line->next = cur_line->next;
3370 cur_line->next = NULL;
3373 edit_free_file_lines(cur_line);
3375 err = edit_write_file(file);
3378 /* Error writing file */
3379 edit_free_file(file);
3383 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3384 string_append(&target, file->identifier);
3386 edit_free_file(file);
3391 return JB_ERR_MEMORY;
3394 rsp->status = strdup("302 Local Redirect from Junkbuster");
3395 if (rsp->status == NULL)
3398 return JB_ERR_MEMORY;
3400 err = enlist_unique_header(rsp->headers, "Location", target);
3407 /*********************************************************************
3409 * Function : cgi_edit_actions_section_add
3411 * Description : CGI function that adds a new empty section to
3415 * 1 : csp = Current client state (buffers, headers, etc...)
3416 * 2 : rsp = http_response data structure for output
3417 * 3 : parameters = map of cgi parameters
3420 * f : (filename) Identifies the file to edit
3421 * v : (version) File's last-modified time
3422 * s : (section) Line number of section to add after, 0 for
3425 * Returns : JB_ERR_OK on success
3426 * JB_ERR_MEMORY on out-of-memory
3427 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3428 * specified or not valid.
3430 *********************************************************************/
3431 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3432 struct http_response *rsp,
3433 const struct map *parameters)
3436 struct file_line * new_line;
3438 struct editable_file * file;
3439 struct file_line * cur_line;
3440 unsigned line_number;
3444 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3446 return cgi_error_disabled(csp, rsp);
3449 err = get_number_param(csp, parameters, "s", §ionid);
3455 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3458 /* No filename specified, can't read file, modified, or out of memory. */
3459 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3463 cur_line = file->lines;
3467 /* Add to start of file */
3468 if (cur_line != NULL)
3470 /* There's something in the file, find the line before the first
3473 while ( (cur_line->next != NULL)
3474 && (cur_line->next->type != FILE_LINE_ACTION) )
3476 cur_line = cur_line->next;
3483 /* Add after stated section. */
3484 while ((cur_line != NULL) && (line_number < sectionid))
3486 cur_line = cur_line->next;
3490 if ( (cur_line == NULL)
3491 || (cur_line->type != FILE_LINE_ACTION))
3493 /* Invalid "sectionid" parameter */
3494 edit_free_file(file);
3495 return JB_ERR_CGI_PARAMS;
3498 /* Skip through the section to find the last line in it. */
3499 while ( (cur_line->next != NULL)
3500 && (cur_line->next->type != FILE_LINE_ACTION) )
3502 cur_line = cur_line->next;
3507 /* At this point, the last line in the previous section is in cur_line
3508 * - add after this. (Or if we need to add as the first line, cur_line
3512 new_text = strdup("{}");
3513 if (NULL == new_text)
3515 edit_free_file(file);
3516 return JB_ERR_MEMORY;
3519 /* Allocate the new line */
3520 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3521 if (new_line == NULL)
3524 edit_free_file(file);
3525 return JB_ERR_MEMORY;
3528 /* Fill in the data members of the new line */
3529 new_line->raw = NULL;
3530 new_line->prefix = NULL;
3531 new_line->unprocessed = new_text;
3532 new_line->type = FILE_LINE_ACTION;
3534 if (cur_line != NULL)
3536 /* Link new_line into the list, after cur_line */
3537 new_line->next = cur_line->next;
3538 cur_line->next = new_line;
3542 /* Link new_line into the list, as first line */
3543 new_line->next = file->lines;
3544 file->lines = new_line;
3547 /* Done making changes, now commit */
3549 err = edit_write_file(file);
3552 /* Error writing file */
3553 edit_free_file(file);
3557 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3558 string_append(&target, file->identifier);
3560 edit_free_file(file);
3565 return JB_ERR_MEMORY;
3568 rsp->status = strdup("302 Local Redirect from Junkbuster");
3569 if (rsp->status == NULL)
3572 return JB_ERR_MEMORY;
3574 err = enlist_unique_header(rsp->headers, "Location", target);
3581 /*********************************************************************
3583 * Function : cgi_edit_actions_section_swap
3585 * Description : CGI function that swaps the order of two sections
3586 * in the actions file. Note that this CGI can actually
3587 * swap any two arbitrary sections, but the GUI interface
3588 * currently only allows consecutive sections to be
3592 * 1 : csp = Current client state (buffers, headers, etc...)
3593 * 2 : rsp = http_response data structure for output
3594 * 3 : parameters = map of cgi parameters
3597 * f : (filename) Identifies the file to edit
3598 * v : (version) File's last-modified time
3599 * s1 : (section1) Line number of first section to swap
3600 * s2 : (section2) Line number of second section to swap
3602 * Returns : JB_ERR_OK on success
3603 * JB_ERR_MEMORY on out-of-memory
3604 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3605 * specified or not valid.
3607 *********************************************************************/
3608 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3609 struct http_response *rsp,
3610 const struct map *parameters)
3614 struct editable_file * file;
3615 struct file_line * cur_line;
3616 struct file_line * prev_line;
3617 struct file_line * line_before_section1;
3618 struct file_line * line_start_section1;
3619 struct file_line * line_end_section1;
3620 struct file_line * line_after_section1;
3621 struct file_line * line_before_section2;
3622 struct file_line * line_start_section2;
3623 struct file_line * line_end_section2;
3624 struct file_line * line_after_section2;
3625 unsigned line_number;
3629 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3631 return cgi_error_disabled(csp, rsp);
3634 err = get_number_param(csp, parameters, "s1", §ion1);
3635 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3641 if (section1 > section2)
3643 unsigned temp = section2;
3644 section2 = section1;
3648 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3651 /* No filename specified, can't read file, modified, or out of memory. */
3652 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3655 /* Start at the beginning... */
3657 cur_line = file->lines;
3660 /* ... find section1 ... */
3661 while ((cur_line != NULL) && (line_number < section1))
3663 prev_line = cur_line;
3664 cur_line = cur_line->next;
3668 if ( (cur_line == NULL)
3669 || (cur_line->type != FILE_LINE_ACTION) )
3671 /* Invalid "section1" parameter */
3672 edit_free_file(file);
3673 return JB_ERR_CGI_PARAMS;
3676 /* If no-op, we've validated params and can skip the rest. */
3677 if (section1 != section2)
3679 /* ... find the end of section1 ... */
3680 line_before_section1 = prev_line;
3681 line_start_section1 = cur_line;
3684 prev_line = cur_line;
3685 cur_line = cur_line->next;
3688 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3689 line_end_section1 = prev_line;
3690 line_after_section1 = cur_line;
3692 /* ... find section2 ... */
3693 while ((cur_line != NULL) && (line_number < section2))
3695 prev_line = cur_line;
3696 cur_line = cur_line->next;
3700 if ( (cur_line == NULL)
3701 || (cur_line->type != FILE_LINE_ACTION) )
3703 /* Invalid "section2" parameter */
3704 edit_free_file(file);
3705 return JB_ERR_CGI_PARAMS;
3708 /* ... find the end of section2 ... */
3709 line_before_section2 = prev_line;
3710 line_start_section2 = cur_line;
3713 prev_line = cur_line;
3714 cur_line = cur_line->next;
3717 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3718 line_end_section2 = prev_line;
3719 line_after_section2 = cur_line;
3721 /* Now have all the pointers we need. Do the swap. */
3723 /* Change the pointer to section1 to point to section2 instead */
3724 if (line_before_section1 == NULL)
3726 file->lines = line_start_section2;
3730 line_before_section1->next = line_start_section2;
3733 if (line_before_section2 == line_end_section1)
3735 /* Consecutive sections */
3736 line_end_section2->next = line_start_section1;
3740 line_end_section2->next = line_after_section1;
3741 line_before_section2->next = line_start_section1;
3744 /* Set the pointer from the end of section1 to the rest of the file */
3745 line_end_section1->next = line_after_section2;
3747 err = edit_write_file(file);
3750 /* Error writing file */
3751 edit_free_file(file);
3754 } /* END if (section1 != section2) */
3756 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3757 string_append(&target, file->identifier);
3759 edit_free_file(file);
3764 return JB_ERR_MEMORY;
3767 rsp->status = strdup("302 Local Redirect from Junkbuster");
3768 if (rsp->status == NULL)
3771 return JB_ERR_MEMORY;
3773 err = enlist_unique_header(rsp->headers, "Location", target);
3780 /*********************************************************************
3782 * Function : cgi_toggle
3784 * Description : CGI function that adds a new empty section to
3788 * 1 : csp = Current client state (buffers, headers, etc...)
3789 * 2 : rsp = http_response data structure for output
3790 * 3 : parameters = map of cgi parameters
3793 * set : If present, how to change toggle setting:
3794 * "enable", "disable", "toggle", or none (default).
3795 * mini : If present, use mini reply template.
3797 * Returns : JB_ERR_OK on success
3798 * JB_ERR_MEMORY on out-of-memory
3800 *********************************************************************/
3801 jb_err cgi_toggle(struct client_state *csp,
3802 struct http_response *rsp,
3803 const struct map *parameters)
3805 struct map *exports;
3807 const char *template_name;
3814 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3816 return cgi_error_disabled(csp, rsp);
3819 if (NULL == (exports = default_exports(csp, "toggle")))
3821 return JB_ERR_MEMORY;
3824 mode = *(lookup(parameters, "set"));
3831 else if (mode == 'd')
3836 else if (mode == 't')
3839 g_bToggleIJB = !g_bToggleIJB;
3842 err = map_conditional(exports, "enabled", g_bToggleIJB);
3849 template_name = (*(lookup(parameters, "mini"))
3853 return template_fill_for_cgi(csp, template_name, exports, rsp);
3857 /*********************************************************************
3859 * Function : actions_to_radio
3861 * Description : Converts a actionsfile entry into settings for
3862 * radio buttons and edit boxes on a HTML form.
3865 * 1 : exports = List of substitutions to add to.
3866 * 2 : action = Action to read
3868 * Returns : JB_ERR_OK on success
3869 * JB_ERR_MEMORY on out-of-memory
3871 *********************************************************************/
3872 static jb_err actions_to_radio(struct map * exports,
3873 const struct action_spec *action)
3875 unsigned mask = action->mask;
3876 unsigned add = action->add;
3884 mask = action->mask;
3887 /* sanity - prevents "-feature +feature" */
3891 #define DEFINE_ACTION_BOOL(name, bit) \
3892 if (!(mask & bit)) \
3894 current_mode = 'n'; \
3896 else if (add & bit) \
3898 current_mode = 'y'; \
3902 current_mode = 'x'; \
3904 if (map_radio(exports, name, "ynx", current_mode)) \
3906 return JB_ERR_MEMORY; \
3909 #define DEFINE_ACTION_STRING(name, bit, index) \
3910 DEFINE_ACTION_BOOL(name, bit); \
3913 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
3916 checked = !strcmp(action->string[index], value); \
3920 checked = is_default; \
3922 mapped_param |= checked; \
3923 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
3925 return JB_ERR_MEMORY; \
3928 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
3929 if (map(exports, name "-param-custom", 1, \
3930 ((!mapped_param) ? "checked" : ""), 1)) \
3932 return JB_ERR_MEMORY; \
3934 if (map(exports, name "-param", 1, \
3935 (((add & bit) && !mapped_param) ? \
3936 action->string[index] : default_val), 1)) \
3938 return JB_ERR_MEMORY; \
3941 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
3942 if (map(exports, name "-param", 1, \
3943 ((add & bit) ? action->string[index] : default_val), 1)) \
3945 return JB_ERR_MEMORY; \
3948 #define DEFINE_ACTION_MULTI(name, index) \
3949 if (action->multi_add[index]->first) \
3951 current_mode = 'y'; \
3953 else if (action->multi_remove_all[index]) \
3955 current_mode = 'n'; \
3957 else if (action->multi_remove[index]->first) \
3959 current_mode = 'y'; \
3963 current_mode = 'x'; \
3965 if (map_radio(exports, name, "ynx", current_mode)) \
3967 return JB_ERR_MEMORY; \
3970 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
3972 #include "actionlist.h"
3974 #undef DEFINE_ACTION_MULTI
3975 #undef DEFINE_ACTION_STRING
3976 #undef DEFINE_ACTION_BOOL
3977 #undef DEFINE_ACTION_ALIAS
3978 #undef DEFINE_CGI_PARAM_CUSTOM
3979 #undef DEFINE_CGI_PARAM_RADIO
3980 #undef DEFINE_CGI_PARAM_NO_RADIO
3986 /*********************************************************************
3988 * Function : javascriptify
3990 * Description : Converts a string into a form JavaScript will like.
3992 * Netscape 4's JavaScript sucks - it doesn't use
3993 * "id" parameters, so you have to set the "name"
3994 * used to submit a form element to something JavaScript
3995 * will like. (Or access the elements by index in an
3996 * array. That array contains >60 elements and will
3997 * be changed whenever we add a new action to the
3998 * editor, so I'm NOT going to use indexes that have
3999 * to be figured out by hand.)
4001 * Currently the only thing we have to worry about
4002 * is "-" ==> "_" conversion.
4004 * This is a length-preserving operation so it is
4005 * carried out in-place, no memory is allocated
4009 * 1 : identifier = String to make JavaScript-friendly.
4013 *********************************************************************/
4014 static void javascriptify(char * identifier)
4016 char * p = identifier;
4017 while (NULL != (p = strchr(p, '-')))
4024 /*********************************************************************
4026 * Function : actions_from_radio
4028 * Description : Converts a map of parameters passed to a CGI function
4029 * into an actionsfile entry.
4032 * 1 : parameters = parameters to the CGI call
4033 * 2 : action = Action to change. Must be valid before
4034 * the call, actions not specified will be
4037 * Returns : JB_ERR_OK on success
4038 * JB_ERR_MEMORY on out-of-memory
4040 *********************************************************************/
4041 static jb_err actions_from_radio(const struct map * parameters,
4042 struct action_spec *action)
4044 static int first_time = 1;
4048 const char * js_name;
4053 /* Statics are generally a potential race condition,
4054 * but in this case we're safe and don't need semaphores.
4055 * Be careful if you modify this function.
4059 #define JAVASCRIPTIFY(dest_var, string) \
4061 static char js_name_arr[] = string; \
4064 javascriptify(js_name_arr); \
4066 dest_var = js_name_arr; \
4069 #define DEFINE_ACTION_BOOL(name, bit) \
4070 JAVASCRIPTIFY(js_name, name); \
4071 param = lookup(parameters, js_name); \
4072 ch = ijb_toupper(param[0]); \
4075 action->add |= bit; \
4076 action->mask |= bit; \
4078 else if (ch == 'N') \
4080 action->add &= ~bit; \
4081 action->mask &= ~bit; \
4083 else if (ch == 'X') \
4085 action->add &= ~bit; \
4086 action->mask |= bit; \
4089 #define DEFINE_ACTION_STRING(name, bit, index) \
4090 JAVASCRIPTIFY(js_name, name); \
4091 param = lookup(parameters, js_name); \
4092 ch = ijb_toupper(param[0]); \
4095 JAVASCRIPTIFY(js_name, name "-mode"); \
4096 param = lookup(parameters, js_name); \
4097 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4099 JAVASCRIPTIFY(js_name, name "-param"); \
4100 param = lookup(parameters, js_name); \
4102 if (*param != '\0') \
4104 if (NULL == (param_dup = strdup(param))) \
4106 return JB_ERR_MEMORY; \
4108 freez(action->string[index]); \
4109 action->add |= bit; \
4110 action->mask |= bit; \
4111 action->string[index] = param_dup; \
4114 else if (ch == 'N') \
4116 if (action->add & bit) \
4118 freez(action->string[index]); \
4120 action->add &= ~bit; \
4121 action->mask &= ~bit; \
4123 else if (ch == 'X') \
4125 if (action->add & bit) \
4127 freez(action->string[index]); \
4129 action->add &= ~bit; \
4130 action->mask |= bit; \
4133 #define DEFINE_ACTION_MULTI(name, index) \
4134 JAVASCRIPTIFY(js_name, name); \
4135 param = lookup(parameters, js_name); \
4136 ch = ijb_toupper((int)param[0]); \
4141 else if (ch == 'N') \
4143 list_remove_all(action->multi_add[index]); \
4144 list_remove_all(action->multi_remove[index]); \
4145 action->multi_remove_all[index] = 1; \
4147 else if (ch == 'X') \
4149 list_remove_all(action->multi_add[index]); \
4150 list_remove_all(action->multi_remove[index]); \
4151 action->multi_remove_all[index] = 0; \
4154 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4156 #include "actionlist.h"
4158 #undef DEFINE_ACTION_MULTI
4159 #undef DEFINE_ACTION_STRING
4160 #undef DEFINE_ACTION_BOOL
4161 #undef DEFINE_ACTION_ALIAS
4162 #undef JAVASCRIPTIFY
4170 #endif /* def FEATURE_CGI_EDIT_ACTIONS */