1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.13 2002/03/04 02:07:59 david__schmidt 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.13 2002/03/04 02:07:59 david__schmidt
46 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
48 * Revision 1.12 2002/03/03 09:18:03 joergs
49 * Made jumbjuster work on AmigaOS again.
51 * Revision 1.11 2002/01/23 01:03:31 jongfoster
52 * Fixing gcc [CygWin] compiler warnings
54 * Revision 1.10 2002/01/23 00:22:59 jongfoster
55 * Adding new function cgi_edit_actions_section_swap(), to reorder
58 * Adding get_url_spec_param() to get a validated URL pattern.
60 * Moving edit_read_line() out of this file and into loaders.c.
62 * Adding missing html_encode() to many CGI functions.
64 * Moving the functions that #include actionlist.h to the end of the file,
65 * because the Visual C++ 97 debugger gets extremely confused if you try
66 * to debug any code that comes after them in the file.
68 * Major optimizations in cgi_edit_actions_list() to reduce the size of
69 * the generated HTML (down 40% from 550k to 304k), with major side-effects
70 * throughout the editor and templates. In particular, the length of the
71 * URLs throughout the editor has been drastically reduced, by cutting
72 * paramater names down to 1 character and CGI names down to 3-4
73 * characters, by removing all non-essential CGI paramaters even at the
74 * expense of having to re-read the actions file for the most trivial
75 * page, and by using relative rather than absolute URLs. This means
76 * that this (typical example):
78 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
79 * filename=ijb&ver=1011487572&section=12&pattern=13
80 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
84 * <a href="eau?f=ijb&v=1011487572&p=13">
86 * Revision 1.9 2002/01/17 20:56:22 jongfoster
87 * Replacing hard references to the URL of the config interface
88 * with #defines from project.h
90 * Revision 1.8 2001/11/30 23:35:51 jongfoster
91 * Renaming actionsfile to ijb.action
93 * Revision 1.7 2001/11/13 00:28:24 jongfoster
94 * - Renaming parameters from edit-actions-for-url so that they only
95 * contain legal JavaScript characters. If we wanted to write
96 * JavaScript that worked with Netscape 4, this is nessacery.
97 * (Note that at the moment the JavaScript doesn't actually work
98 * with Netscape 4, but now this is purely a template issue, not
99 * one affecting code).
100 * - Adding new CGIs for use by non-JavaScript browsers:
101 * edit-actions-url-form
102 * edit-actions-add-url-form
103 * edit-actions-remove-url-form
106 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
107 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
108 * by and __OS2__ ifdef.
110 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
111 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
112 * threads to call select() simultaneously. So, it's time to do a real, live,
113 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
114 * (native). Both versions will work, but using __OS2__ offers multi-threading.
116 * Revision 1.4 2001/10/23 21:48:19 jongfoster
117 * Cleaning up error handling in CGI functions - they now send back
118 * a HTML error page and should never cause a FATAL error. (Fixes one
119 * potential source of "denial of service" attacks).
121 * CGI actions file editor that works and is actually useful.
123 * Ability to toggle JunkBuster remotely using a CGI call.
125 * You can turn off both the above features in the main configuration
126 * file, e.g. if you are running a multi-user proxy.
128 * Revision 1.3 2001/10/14 22:12:49 jongfoster
129 * New version of CGI-based actionsfile editor.
130 * Major changes, including:
131 * - Completely new file parser and file output routines
132 * - edit-actions CGI renamed edit-actions-for-url
133 * - All CGIs now need a filename parameter, except for...
134 * - New CGI edit-actions which doesn't need a filename,
135 * to allow you to start the editor up.
136 * - edit-actions-submit now works, and now automatically
137 * redirects you back to the main edit-actions-list handler.
139 * Revision 1.2 2001/09/16 17:05:14 jongfoster
140 * Removing unused #include showarg.h
142 * Revision 1.1 2001/09/16 15:47:37 jongfoster
143 * First version of CGI-based edit interface. This is very much a
144 * work-in-progress, and you can't actually use it to edit anything
145 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
146 * to have any effect.
149 **********************************************************************/
155 * FIXME: Following includes copied from cgi.c - which are actually needed?
160 #include <sys/types.h>
165 #include <sys/stat.h>
168 #define snprintf _snprintf
169 #endif /* def _WIN32 */
174 #include "cgisimple.h"
178 #include "miscutil.h"
182 /* loadcfg.h is for g_bToggleIJB only */
183 #include "urlmatch.h"
185 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
188 #ifdef FEATURE_CGI_EDIT_ACTIONS
192 struct file_line * next;
200 struct action_spec action[1];
209 /* Add more data types here... e.g.
212 struct url_spec url[1];
216 struct action_spec action[1];
225 #define FILE_LINE_UNPROCESSED 1
226 #define FILE_LINE_BLANK 2
227 #define FILE_LINE_ALIAS_HEADER 3
228 #define FILE_LINE_ALIAS_ENTRY 4
229 #define FILE_LINE_ACTION 5
230 #define FILE_LINE_URL 6
231 #define FILE_LINE_SETTINGS_HEADER 7
232 #define FILE_LINE_SETTINGS_ENTRY 8
233 #define FILE_LINE_DESCRIPTION_HEADER 9
234 #define FILE_LINE_DESCRIPTION_ENTRY 10
239 struct file_line * lines;
240 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
241 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
242 /* Pre-encoded with url_encode() for ease of use. */
243 const char * version_str; /* Last modification time, as a string. For CGI param */
244 /* Can be used in URL without using url_param(). */
245 unsigned version; /* Last modification time - prevents chaos with
246 * the browser's "back" button. Note that this is a
247 * time_t cast to an unsigned. When comparing, always
248 * cast the time_t to an unsigned, and *NOT* vice-versa.
249 * This may lose the top few bits, but they're not
250 * significant anyway.
252 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
253 * Note that changing this after the file has been
254 * read in will cause a mess.
256 struct file_line * parse_error; /* On parse error, this is the offending line. */
257 const char * parse_error_text; /* On parse error, this is the problem.
258 * (Statically allocated) */
261 /* FIXME: Following non-static functions should be prototyped in .h or made static */
263 /* Functions to read and write arbitrary config files */
264 jb_err edit_read_file(struct client_state *csp,
265 const struct map *parameters,
268 struct editable_file **pfile);
269 jb_err edit_write_file(struct editable_file * file);
270 void edit_free_file(struct editable_file * file);
272 /* Functions to read and write actions files */
273 jb_err edit_parse_actions_file(struct editable_file * file);
274 jb_err edit_read_actions_file(struct client_state *csp,
275 struct http_response *rsp,
276 const struct map *parameters,
278 struct editable_file **pfile);
281 jb_err cgi_error_modified(struct client_state *csp,
282 struct http_response *rsp,
283 const char *filename);
284 jb_err cgi_error_parse(struct client_state *csp,
285 struct http_response *rsp,
286 struct editable_file *file);
287 jb_err cgi_error_file(struct client_state *csp,
288 struct http_response *rsp,
289 const char *filename);
290 jb_err cgi_error_disabled(struct client_state *csp,
291 struct http_response *rsp);
293 /* Internal arbitrary config file support functions */
294 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
295 static void edit_free_file_lines(struct file_line * first_line);
297 /* Internal actions file support functions */
298 static int match_actions_file_header_line(const char * line, const char * name);
299 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
301 /* Internal parameter parsing functions */
302 static jb_err get_file_name_param(struct client_state *csp,
303 const struct map *parameters,
304 const char *param_name,
307 const char **pparam);
308 static jb_err get_number_param(struct client_state *csp,
309 const struct map *parameters,
312 static jb_err get_url_spec_param(struct client_state *csp,
313 const struct map *parameters,
317 /* Internal actionsfile <==> HTML conversion functions */
318 static jb_err map_radio(struct map * exports,
319 const char * optionname,
322 static jb_err actions_to_radio(struct map * exports,
323 const struct action_spec *action);
324 static jb_err actions_from_radio(const struct map * parameters,
325 struct action_spec *action);
328 static jb_err map_copy_parameter_html(struct map *out,
329 const struct map *in,
331 #if 0 /* unused function */
332 static jb_err map_copy_parameter_url(struct map *out,
333 const struct map *in,
335 #endif /* unused function */
337 /*********************************************************************
339 * Function : map_copy_parameter_html
341 * Description : Copy a CGI parameter from one map to another, HTML
345 * 1 : out = target map
346 * 2 : in = source map
347 * 3 : name = name of cgi parameter to copy
349 * Returns : JB_ERR_OK on success
350 * JB_ERR_MEMORY on out-of-memory
351 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
354 *********************************************************************/
355 static jb_err map_copy_parameter_html(struct map *out,
356 const struct map *in,
366 value = lookup(in, name);
367 err = map(out, name, 1, html_encode(value), 0);
374 else if (*value == '\0')
376 return JB_ERR_CGI_PARAMS;
385 #if 0 /* unused function */
386 /*********************************************************************
388 * Function : map_copy_parameter_html
390 * Description : Copy a CGI parameter from one map to another, URL
394 * 1 : out = target map
395 * 2 : in = source map
396 * 3 : name = name of cgi parameter to copy
398 * Returns : JB_ERR_OK on success
399 * JB_ERR_MEMORY on out-of-memory
400 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
403 *********************************************************************/
404 static jb_err map_copy_parameter_url(struct map *out,
405 const struct map *in,
415 value = lookup(in, name);
416 err = map(out, name, 1, url_encode(value), 0);
423 else if (*value == '\0')
425 return JB_ERR_CGI_PARAMS;
432 #endif /* 0 - unused function */
434 /*********************************************************************
436 * Function : cgi_edit_actions_url_form
438 * Description : CGI function that displays a form for
442 * 1 : csp = Current client state (buffers, headers, etc...)
443 * 2 : rsp = http_response data structure for output
444 * 3 : parameters = map of cgi parameters
447 * f : (filename) Identifies the file to edit
448 * v : (version) File's last-modified time
449 * p : (pattern) Line number of pattern to edit
451 * Returns : JB_ERR_OK on success
452 * JB_ERR_MEMORY on out-of-memory
453 * JB_ERR_CGI_PARAMS if the CGI parameters are not
454 * specified or not valid.
456 *********************************************************************/
457 jb_err cgi_edit_actions_url_form(struct client_state *csp,
458 struct http_response *rsp,
459 const struct map *parameters)
461 struct map * exports;
463 struct editable_file * file;
464 struct file_line * cur_line;
465 unsigned line_number;
472 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
474 return cgi_error_disabled(csp, rsp);
477 err = get_number_param(csp, parameters, "p", &patternid);
483 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
486 /* No filename specified, can't read file, modified, or out of memory. */
487 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
490 cur_line = file->lines;
492 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
494 cur_line = cur_line->next;
497 if ( (cur_line == NULL)
498 || (line_number != patternid)
500 || (cur_line->type != FILE_LINE_URL))
502 /* Invalid "patternid" parameter */
503 edit_free_file(file);
504 return JB_ERR_CGI_PARAMS;
507 if (NULL == (exports = default_exports(csp, NULL)))
509 edit_free_file(file);
510 return JB_ERR_MEMORY;
513 err = map(exports, "f", 1, file->identifier, 1);
514 if (!err) err = map(exports, "v", 1, file->version_str, 1);
515 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
516 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
518 edit_free_file(file);
526 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
530 /*********************************************************************
532 * Function : cgi_edit_actions_add_url_form
534 * Description : CGI function that displays a form for
538 * 1 : csp = Current client state (buffers, headers, etc...)
539 * 2 : rsp = http_response data structure for output
540 * 3 : parameters = map of cgi parameters
543 * f : (filename) Identifies the file to edit
544 * v : (version) File's last-modified time
545 * s : (section) Line number of section to edit
547 * Returns : JB_ERR_OK on success
548 * JB_ERR_MEMORY on out-of-memory
549 * JB_ERR_CGI_PARAMS if the CGI parameters are not
550 * specified or not valid.
552 *********************************************************************/
553 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
554 struct http_response *rsp,
555 const struct map *parameters)
564 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
566 return cgi_error_disabled(csp, rsp);
569 if (NULL == (exports = default_exports(csp, NULL)))
571 return JB_ERR_MEMORY;
574 err = map_copy_parameter_html(exports, parameters, "f");
575 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
576 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
584 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
588 /*********************************************************************
590 * Function : cgi_edit_actions_remove_url_form
592 * Description : CGI function that displays a form for
596 * 1 : csp = Current client state (buffers, headers, etc...)
597 * 2 : rsp = http_response data structure for output
598 * 3 : parameters = map of cgi parameters
601 * f : (filename) Identifies the file to edit
602 * v : (version) File's last-modified time
603 * p : (pattern) Line number of pattern to edit
605 * Returns : JB_ERR_OK on success
606 * JB_ERR_MEMORY on out-of-memory
607 * JB_ERR_CGI_PARAMS if the CGI parameters are not
608 * specified or not valid.
610 *********************************************************************/
611 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
612 struct http_response *rsp,
613 const struct map *parameters)
615 struct map * exports;
617 struct editable_file * file;
618 struct file_line * cur_line;
619 unsigned line_number;
626 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
628 return cgi_error_disabled(csp, rsp);
631 err = get_number_param(csp, parameters, "p", &patternid);
637 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
640 /* No filename specified, can't read file, modified, or out of memory. */
641 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
644 cur_line = file->lines;
646 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
648 cur_line = cur_line->next;
651 if ( (cur_line == NULL)
652 || (line_number != patternid)
654 || (cur_line->type != FILE_LINE_URL))
656 /* Invalid "patternid" parameter */
657 edit_free_file(file);
658 return JB_ERR_CGI_PARAMS;
661 if (NULL == (exports = default_exports(csp, NULL)))
663 edit_free_file(file);
664 return JB_ERR_MEMORY;
667 err = map(exports, "f", 1, file->identifier, 1);
668 if (!err) err = map(exports, "v", 1, file->version_str, 1);
669 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
670 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
672 edit_free_file(file);
680 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
684 /*********************************************************************
686 * Function : edit_write_file
688 * Description : Write a complete file to disk.
691 * 1 : filename = File to write to.
692 * 2 : file = Data structure to write.
694 * Returns : JB_ERR_OK on success
695 * JB_ERR_FILE on error writing to file.
696 * JB_ERR_MEMORY on out of memory
698 *********************************************************************/
699 jb_err edit_write_file(struct editable_file * file)
702 struct file_line * cur_line;
703 struct stat statbuf[1];
704 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
705 digits in time_t, assuming this is a 64-bit
706 machine, plus null terminator, plus one
710 assert(file->filename);
712 #if defined(AMIGA) || defined(__OS2__)
713 if (NULL == (fp = fopen(file->filename, "w")))
715 if (NULL == (fp = fopen(file->filename, "wt")))
716 #endif /* def AMIGA */
721 cur_line = file->lines;
722 while (cur_line != NULL)
726 if (fputs(cur_line->raw, fp) < 0)
734 if (cur_line->prefix)
736 if (fputs(cur_line->prefix, fp) < 0)
742 if (cur_line->unprocessed)
744 /* This should be a single line - sanity check. */
745 assert(NULL == strchr(cur_line->unprocessed, '\r'));
746 assert(NULL == strchr(cur_line->unprocessed, '\n'));
748 if (NULL != strchr(cur_line->unprocessed, '#'))
750 /* Must quote '#' characters */
757 /* Count number of # characters, so we know length of output string */
758 src = cur_line->unprocessed;
759 while (NULL != (src = strchr(src, '#')))
766 /* Allocate new memory for string */
767 len = strlen(cur_line->unprocessed);
768 if (NULL == (str = malloc(len + 1 + numhash)))
770 /* Uh oh, just trashed file! */
772 return JB_ERR_MEMORY;
775 /* Loop through string from end */
776 src = cur_line->unprocessed + len;
777 dest = str + len + numhash;
778 for ( ; len >= 0; len--)
780 if ((*dest-- = *src--) == '#')
784 assert(numhash >= 0);
787 assert(numhash == 0);
788 assert(src + 1 == cur_line->unprocessed);
789 assert(dest + 1 == str);
791 if (fputs(str, fp) < 0)
802 /* Can write without quoting '#' characters. */
803 if (fputs(cur_line->unprocessed, fp) < 0)
809 if (fputs(NEWLINE(file->newline), fp) < 0)
817 /* FIXME: Write data from file->data->whatever */
821 cur_line = cur_line->next;
827 /* Update the version stamp in the file structure, since we just
828 * wrote to the file & changed it's date.
830 if (stat(file->filename, statbuf) < 0)
832 /* Error, probably file not found. */
835 file->version = (unsigned)statbuf->st_mtime;
837 /* Correct file->version_str */
838 freez(file->version_str);
839 snprintf(version_buf, 22, "%u", file->version);
840 version_buf[21] = '\0';
841 file->version_str = strdup(version_buf);
842 if (version_buf == NULL)
844 return JB_ERR_MEMORY;
851 /*********************************************************************
853 * Function : edit_free_file
855 * Description : Free a complete file in memory.
858 * 1 : file = Data structure to free.
862 *********************************************************************/
863 void edit_free_file(struct editable_file * file)
867 /* Silently ignore NULL pointer */
871 edit_free_file_lines(file->lines);
872 freez(file->filename);
873 freez(file->identifier);
874 freez(file->version_str);
876 file->parse_error_text = NULL; /* Statically allocated */
877 file->parse_error = NULL;
883 /*********************************************************************
885 * Function : edit_free_file
887 * Description : Free an entire linked list of file lines.
890 * 1 : first_line = Data structure to free.
894 *********************************************************************/
895 static void edit_free_file_lines(struct file_line * first_line)
897 struct file_line * next_line;
899 while (first_line != NULL)
901 next_line = first_line->next;
902 first_line->next = NULL;
903 freez(first_line->raw);
904 freez(first_line->prefix);
905 freez(first_line->unprocessed);
906 switch(first_line->type)
908 case 0: /* special case if memory zeroed */
909 case FILE_LINE_UNPROCESSED:
910 case FILE_LINE_BLANK:
911 case FILE_LINE_ALIAS_HEADER:
912 case FILE_LINE_SETTINGS_HEADER:
913 case FILE_LINE_DESCRIPTION_HEADER:
914 case FILE_LINE_DESCRIPTION_ENTRY:
915 case FILE_LINE_ALIAS_ENTRY:
917 /* No data is stored for these */
920 case FILE_LINE_ACTION:
921 free_action(first_line->data.action);
924 case FILE_LINE_SETTINGS_ENTRY:
925 freez(first_line->data.setting.name);
926 freez(first_line->data.setting.svalue);
929 /* Should never happen */
933 first_line->type = 0; /* paranoia */
935 first_line = next_line;
940 /*********************************************************************
942 * Function : match_actions_file_header_line
944 * Description : Match an actions file {{header}} line
947 * 1 : line - String from file
948 * 2 : name - Header to match against
950 * Returns : 0 iff they match.
952 *********************************************************************/
953 static int match_actions_file_header_line(const char * line, const char * name)
961 if ((line[0] != '{') || (line[1] != '{'))
967 /* Look for optional whitespace */
968 while ( (*line == ' ') || (*line == '\t') )
973 /* Look for the specified name (case-insensitive) */
975 if (0 != strncmpic(line, name, len))
981 /* Look for optional whitespace */
982 while ( (*line == ' ') || (*line == '\t') )
987 /* Look for "}}" and end of string*/
988 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
998 /*********************************************************************
1000 * Function : match_actions_file_header_line
1002 * Description : Match an actions file {{header}} line
1005 * 1 : line - String from file. Must not start with
1006 * whitespace (else infinite loop!)
1007 * 2 : name - Destination for name
1008 * 2 : name - Destination for value
1010 * Returns : JB_ERR_OK on success
1011 * JB_ERR_MEMORY on out-of-memory
1012 * JB_ERR_PARSE if there's no "=" sign, or if there's
1013 * nothing before the "=" sign (but empty
1014 * values *after* the "=" sign are legal).
1016 *********************************************************************/
1017 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1019 const char * name_end;
1020 const char * value_start;
1026 assert(*line != ' ');
1027 assert(*line != '\t');
1032 value_start = strchr(line, '=');
1033 if ((value_start == NULL) || (value_start == line))
1035 return JB_ERR_PARSE;
1038 name_end = value_start - 1;
1040 /* Eat any whitespace before the '=' */
1041 while ((*name_end == ' ') || (*name_end == '\t'))
1044 * we already know we must have at least 1 non-ws char
1045 * at start of buf - no need to check
1050 name_len = name_end - line + 1; /* Length excluding \0 */
1051 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1053 return JB_ERR_MEMORY;
1055 strncpy(*pname, line, name_len);
1056 (*pname)[name_len] = '\0';
1058 /* Eat any the whitespace after the '=' */
1060 while ((*value_start == ' ') || (*value_start == '\t'))
1065 if (NULL == (*pvalue = strdup(value_start)))
1069 return JB_ERR_MEMORY;
1076 /*********************************************************************
1078 * Function : edit_parse_actions_file
1080 * Description : Parse an actions file in memory.
1082 * Passed linked list must have the "data" member
1083 * zeroed, and must contain valid "next" and
1084 * "unprocessed" fields. The "raw" and "prefix"
1085 * fields are ignored, and "type" is just overwritten.
1087 * Note that on error the file may have been
1091 * 1 : file = Actions file to be parsed in-place.
1093 * Returns : JB_ERR_OK on success
1094 * JB_ERR_MEMORY on out-of-memory
1095 * JB_ERR_PARSE on error
1097 *********************************************************************/
1098 jb_err edit_parse_actions_file(struct editable_file * file)
1100 struct file_line * cur_line;
1102 const char * text; /* Text from a line */
1103 char * name; /* For lines of the form name=value */
1104 char * value; /* For lines of the form name=value */
1105 struct action_alias * alias_list = NULL;
1106 jb_err err = JB_ERR_OK;
1108 /* alias_list contains the aliases defined in this file.
1109 * It might be better to use the "file_line.data" fields
1110 * in the relavent places instead.
1113 cur_line = file->lines;
1115 /* A note about blank line support: Blank lines should only
1116 * ever occur as the last line in the file. This function
1117 * is more forgiving than that - FILE_LINE_BLANK can occur
1121 /* Skip leading blanks. Should only happen if file is
1122 * empty (which is valid, but pointless).
1124 while ( (cur_line != NULL)
1125 && (cur_line->unprocessed[0] == '\0') )
1128 cur_line->type = FILE_LINE_BLANK;
1129 cur_line = cur_line->next;
1132 if ( (cur_line != NULL)
1133 && (cur_line->unprocessed[0] != '{') )
1135 /* File doesn't start with a header */
1136 file->parse_error = cur_line;
1137 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1138 return JB_ERR_PARSE;
1141 if ( (cur_line != NULL) && (0 ==
1142 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1144 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1146 cur_line = cur_line->next;
1147 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1149 if (cur_line->unprocessed[0])
1151 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1153 err = split_line_on_equals(cur_line->unprocessed,
1154 &cur_line->data.setting.name,
1155 &cur_line->data.setting.svalue);
1156 if (err == JB_ERR_MEMORY)
1160 else if (err != JB_ERR_OK)
1162 /* Line does not contain a name=value pair */
1163 file->parse_error = cur_line;
1164 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1165 return JB_ERR_PARSE;
1170 cur_line->type = FILE_LINE_BLANK;
1172 cur_line = cur_line->next;
1176 if ( (cur_line != NULL) && (0 ==
1177 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1179 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1181 cur_line = cur_line->next;
1182 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1184 if (cur_line->unprocessed[0])
1186 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1190 cur_line->type = FILE_LINE_BLANK;
1192 cur_line = cur_line->next;
1196 if ( (cur_line != NULL) && (0 ==
1197 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1199 cur_line->type = FILE_LINE_ALIAS_HEADER;
1201 cur_line = cur_line->next;
1202 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1204 if (cur_line->unprocessed[0])
1206 /* define an alias */
1207 struct action_alias * new_alias;
1209 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1211 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1212 if (err == JB_ERR_MEMORY)
1216 else if (err != JB_ERR_OK)
1218 /* Line does not contain a name=value pair */
1219 file->parse_error = cur_line;
1220 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1221 return JB_ERR_PARSE;
1224 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1229 free_alias_list(alias_list);
1230 return JB_ERR_MEMORY;
1233 err = get_actions(value, alias_list, new_alias->action);
1236 /* Invalid action or out of memory */
1240 free_alias_list(alias_list);
1241 if (err == JB_ERR_MEMORY)
1247 /* Line does not contain a name=value pair */
1248 file->parse_error = cur_line;
1249 file->parse_error_text = "This alias does not specify a valid set of actions.";
1250 return JB_ERR_PARSE;
1256 new_alias->name = name;
1259 new_alias->next = alias_list;
1260 alias_list = new_alias;
1264 cur_line->type = FILE_LINE_BLANK;
1266 cur_line = cur_line->next;
1270 /* Header done, process the main part of the file */
1271 while (cur_line != NULL)
1273 /* At this point, (cur_line->unprocessed[0] == '{') */
1274 assert(cur_line->unprocessed[0] == '{');
1275 text = cur_line->unprocessed + 1;
1276 len = strlen(text) - 1;
1277 if (text[len] != '}')
1279 /* No closing } on header */
1280 free_alias_list(alias_list);
1281 file->parse_error = cur_line;
1282 file->parse_error_text = "Headers starting with '{' must have a "
1283 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1284 "must close with two brackets ('}}').";
1285 return JB_ERR_PARSE;
1290 /* An invalid {{ header. */
1291 free_alias_list(alias_list);
1292 file->parse_error = cur_line;
1293 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1294 "Please remember that the system (two-bracket) headers must "
1295 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1296 "and must appear before any actions (one-bracket) headers. "
1297 "Also note that system headers may not be repeated.";
1298 return JB_ERR_PARSE;
1301 while ( (*text == ' ') || (*text == '\t') )
1307 && ( (text[len - 1] == ' ')
1308 || (text[len - 1] == '\t') ) )
1313 cur_line->type = FILE_LINE_ACTION;
1315 /* Remove {} and make copy */
1316 if (NULL == (value = (char *) malloc(len + 1)))
1319 free_alias_list(alias_list);
1320 return JB_ERR_MEMORY;
1322 strncpy(value, text, len);
1326 err = get_actions(value, alias_list, cur_line->data.action);
1329 /* Invalid action or out of memory */
1331 free_alias_list(alias_list);
1332 if (err == JB_ERR_MEMORY)
1338 /* Line does not contain a name=value pair */
1339 file->parse_error = cur_line;
1340 file->parse_error_text = "This header does not specify a valid set of actions.";
1341 return JB_ERR_PARSE;
1345 /* Done with string - it was clobbered anyway */
1348 /* Process next line */
1349 cur_line = cur_line->next;
1351 /* Loop processing URL patterns */
1352 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1354 if (cur_line->unprocessed[0])
1356 /* Could parse URL here, but this isn't currently needed */
1358 cur_line->type = FILE_LINE_URL;
1362 cur_line->type = FILE_LINE_BLANK;
1364 cur_line = cur_line->next;
1366 } /* End main while(cur_line != NULL) loop */
1368 free_alias_list(alias_list);
1374 /*********************************************************************
1376 * Function : edit_read_file_lines
1378 * Description : Read all the lines of a file into memory.
1379 * Handles whitespace, comments and line continuation.
1382 * 1 : fp = File to read from. On return, this will be
1383 * at EOF but it will not have been closed.
1384 * 2 : pfile = Destination for a linked list of file_lines.
1385 * Will be set to NULL on error.
1387 * Returns : JB_ERR_OK on success
1388 * JB_ERR_MEMORY on out-of-memory
1390 *********************************************************************/
1391 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1393 struct file_line * first_line; /* Keep for return value or to free */
1394 struct file_line * cur_line; /* Current line */
1395 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1403 cur_line = first_line = zalloc(sizeof(struct file_line));
1404 if (cur_line == NULL)
1406 return JB_ERR_MEMORY;
1409 cur_line->type = FILE_LINE_UNPROCESSED;
1411 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1414 /* Out of memory or empty file. */
1415 /* Note that empty file is not an error we propogate up */
1417 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1422 prev_line = cur_line;
1423 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1424 if (cur_line == NULL)
1427 edit_free_file_lines(first_line);
1428 return JB_ERR_MEMORY;
1431 cur_line->type = FILE_LINE_UNPROCESSED;
1433 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1434 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1437 edit_free_file_lines(first_line);
1438 return JB_ERR_MEMORY;
1442 while (rval != JB_ERR_FILE);
1446 /* We allocated one too many - free it */
1447 prev_line->next = NULL;
1450 *pfile = first_line;
1455 /*********************************************************************
1457 * Function : edit_read_file
1459 * Description : Read a complete file into memory.
1460 * Handles CGI parameter parsing. If requested, also
1461 * checks the file's modification timestamp.
1464 * 1 : csp = Current client state (buffers, headers, etc...)
1465 * 2 : parameters = map of cgi parameters.
1466 * 3 : require_version = true to check "ver" parameter.
1467 * 4 : suffix = File extension, e.g. ".action".
1468 * 5 : pfile = Destination for the file. Will be set
1472 * filename : The name of the file to read, without the
1473 * path or ".action" extension.
1474 * ver : (Only if require_version is nonzero)
1475 * Timestamp of the actions file. If wrong, this
1476 * function fails with JB_ERR_MODIFIED.
1478 * Returns : JB_ERR_OK on success
1479 * JB_ERR_MEMORY on out-of-memory
1480 * JB_ERR_CGI_PARAMS if "filename" was not specified
1482 * JB_ERR_FILE if the file cannot be opened or
1484 * JB_ERR_MODIFIED if version checking was requested and
1485 * failed - the file was modified outside
1486 * of this CGI editor instance.
1488 *********************************************************************/
1489 jb_err edit_read_file(struct client_state *csp,
1490 const struct map *parameters,
1491 int require_version,
1493 struct editable_file **pfile)
1495 struct file_line * lines;
1499 const char * identifier;
1500 struct editable_file * file;
1501 unsigned version = 0;
1502 struct stat statbuf[1];
1503 char version_buf[22];
1504 int newline = NEWLINE_UNKNOWN;
1512 err = get_file_name_param(csp, parameters, "f", suffix,
1513 &filename, &identifier);
1519 if (stat(filename, statbuf) < 0)
1521 /* Error, probably file not found. */
1525 version = (unsigned) statbuf->st_mtime;
1527 if (require_version)
1529 unsigned specified_version;
1530 err = get_number_param(csp, parameters, "v", &specified_version);
1537 if (version != specified_version)
1539 return JB_ERR_MODIFIED;
1543 #if defined(AMIGA) || defined(__OS2__)
1544 if (NULL == (fp = fopen(filename,"r")))
1546 if (NULL == (fp = fopen(filename,"rt")))
1547 #endif /* def AMIGA */
1553 err = edit_read_file_lines(fp, &lines, &newline);
1563 file = (struct editable_file *) zalloc(sizeof(*file));
1567 edit_free_file_lines(lines);
1571 file->lines = lines;
1572 file->newline = newline;
1573 file->filename = filename;
1574 file->version = version;
1575 file->identifier = url_encode(identifier);
1577 if (file->identifier == NULL)
1579 edit_free_file(file);
1580 return JB_ERR_MEMORY;
1583 /* Correct file->version_str */
1584 freez(file->version_str);
1585 snprintf(version_buf, 22, "%u", file->version);
1586 version_buf[21] = '\0';
1587 file->version_str = strdup(version_buf);
1588 if (version_buf == NULL)
1590 edit_free_file(file);
1591 return JB_ERR_MEMORY;
1599 /*********************************************************************
1601 * Function : edit_read_actions_file
1603 * Description : Read a complete actions file into memory.
1604 * Handles CGI parameter parsing. If requested, also
1605 * checks the file's modification timestamp.
1607 * If this function detects an error in the categories
1608 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1609 * then it handles it by filling in the specified
1610 * response structure and returning JB_ERR_FILE.
1613 * 1 : csp = Current client state (buffers, headers, etc...)
1614 * 2 : rsp = HTTP response. Only filled in on error.
1615 * 2 : parameters = map of cgi parameters.
1616 * 3 : require_version = true to check "ver" parameter.
1617 * 4 : pfile = Destination for the file. Will be set
1621 * filename : The name of the actions file to read, without the
1622 * path or ".action" extension.
1623 * ver : (Only if require_version is nonzero)
1624 * Timestamp of the actions file. If wrong, this
1625 * function fails with JB_ERR_MODIFIED.
1627 * Returns : JB_ERR_OK on success
1628 * JB_ERR_MEMORY on out-of-memory
1629 * JB_ERR_CGI_PARAMS if "filename" was not specified
1631 * JB_ERR_FILE if the file does not contain valid data,
1632 * or if file cannot be opened or
1633 * contains no data, or if version
1634 * checking was requested and failed.
1636 *********************************************************************/
1637 jb_err edit_read_actions_file(struct client_state *csp,
1638 struct http_response *rsp,
1639 const struct map *parameters,
1640 int require_version,
1641 struct editable_file **pfile)
1644 struct editable_file *file;
1652 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1655 /* Try to handle if possible */
1656 if (err == JB_ERR_FILE)
1658 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1660 else if (err == JB_ERR_MODIFIED)
1662 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
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.
1675 err = edit_parse_actions_file(file);
1678 if (err == JB_ERR_PARSE)
1680 err = cgi_error_parse(csp, rsp, file);
1681 if (err == JB_ERR_OK)
1684 * Signal to higher-level CGI code that there was a problem but we
1685 * handled it, they should just return JB_ERR_OK.
1690 edit_free_file(file);
1699 /*********************************************************************
1701 * Function : get_file_name_param
1703 * Description : Get the name of the file to edit from the parameters
1704 * passed to a CGI function. This function handles
1705 * security checks such as blocking urls containing
1706 * "/" or ".", prepending the config file directory,
1707 * and adding the specified suffix.
1709 * (This is an essential security check, otherwise
1710 * users may be able to pass "../../../etc/passwd"
1711 * and overwrite the password file [linux], "prn:"
1712 * and print random data [Windows], etc...)
1714 * This function only allows filenames contining the
1715 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1716 * That's probably too restrictive but at least it's
1720 * 1 : csp = Current client state (buffers, headers, etc...)
1721 * 2 : parameters = map of cgi parameters
1722 * 3 : param_name = The name of the parameter to read
1723 * 4 : suffix = File extension, e.g. ".actions"
1724 * 5 : pfilename = destination for full filename. Caller
1725 * free()s. Set to NULL on error.
1726 * 6 : pparam = destination for partial filename,
1727 * suitable for use in another URL. Allocated as part
1728 * of the map "parameters", so don't free it.
1729 * Set to NULL if not specified.
1731 * Returns : JB_ERR_OK on success
1732 * JB_ERR_MEMORY on out-of-memory
1733 * JB_ERR_CGI_PARAMS if "filename" was not specified
1736 *********************************************************************/
1737 static jb_err get_file_name_param(struct client_state *csp,
1738 const struct map *parameters,
1739 const char *param_name,
1742 const char **pparam)
1746 #if 0 /* Patch to make 3.0.0 work properly. */
1748 #endif /* 0 - Patch to make 3.0.0 work properly. */
1762 param = lookup(parameters, param_name);
1765 return JB_ERR_CGI_PARAMS;
1770 len = strlen(param);
1771 if (len >= FILENAME_MAX)
1774 return JB_ERR_CGI_PARAMS;
1777 /* Check every character to see if it's legal */
1779 while ((ch = *s++) != '\0')
1781 if ( ((ch < 'A') || (ch > 'Z'))
1782 && ((ch < 'a') || (ch > 'z'))
1783 && ((ch < '0') || (ch > '9'))
1787 /* Probable hack attempt. */
1788 return JB_ERR_CGI_PARAMS;
1793 * FIXME Following is a hack to make 3.0.0 work properly.
1794 * Change "#if 0" --> "#if 1" below when we have modular action
1798 #if 0 /* Patch to make 3.0.0 work properly. */
1799 /* Append extension */
1800 name = malloc(len + strlen(suffix) + 1);
1803 return JB_ERR_MEMORY;
1805 strcpy(name, param);
1806 strcpy(name + len, suffix);
1809 fullpath = make_path(csp->config->confdir, name);
1811 #else /* 1 - Patch to make 3.0.0 work properly. */
1812 if ((csp->actions_list == NULL)
1813 || (csp->actions_list->filename == NULL))
1815 return JB_ERR_CGI_PARAMS;
1818 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1819 ? strdup(csp->actions_list->filename) : NULL);
1820 #endif /* 1 - Patch to make 3.0.0 work properly. */
1821 if (fullpath == NULL)
1823 return JB_ERR_MEMORY;
1827 *pfilename = fullpath;
1833 /*********************************************************************
1835 * Function : get_number_param
1837 * Description : Get a non-negative integer from the parameters
1838 * passed to a CGI function.
1841 * 1 : csp = Current client state (buffers, headers, etc...)
1842 * 2 : parameters = map of cgi parameters
1843 * 3 : name = Name of CGI parameter to read
1844 * 4 : pvalue = destination for value.
1845 * Set to -1 on error.
1847 * Returns : JB_ERR_OK on success
1848 * JB_ERR_MEMORY on out-of-memory
1849 * JB_ERR_CGI_PARAMS if the parameter was not specified
1852 *********************************************************************/
1853 static jb_err get_number_param(struct client_state *csp,
1854 const struct map *parameters,
1869 param = lookup(parameters, name);
1872 return JB_ERR_CGI_PARAMS;
1875 /* We don't use atoi because I want to check this carefully... */
1878 while ((ch = *param++) != '\0')
1880 if ((ch < '0') || (ch > '9'))
1882 return JB_ERR_CGI_PARAMS;
1889 * <limits.h> defines UINT_MAX
1891 * (UINT_MAX - ch) / 10 is the largest number that
1892 * can be safely multiplied by 10 then have ch added.
1894 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1896 return JB_ERR_CGI_PARAMS;
1899 value = value * 10 + ch;
1909 /*********************************************************************
1911 * Function : get_url_spec_param
1913 * Description : Get a URL pattern from the parameters
1914 * passed to a CGI function. Removes leading/trailing
1915 * spaces and validates it.
1918 * 1 : csp = Current client state (buffers, headers, etc...)
1919 * 2 : parameters = map of cgi parameters
1920 * 3 : name = Name of CGI parameter to read
1921 * 4 : pvalue = destination for value. Will be malloc()'d.
1922 * Set to NULL on error.
1924 * Returns : JB_ERR_OK on success
1925 * JB_ERR_MEMORY on out-of-memory
1926 * JB_ERR_CGI_PARAMS if the parameter was not specified
1929 *********************************************************************/
1930 static jb_err get_url_spec_param(struct client_state *csp,
1931 const struct map *parameters,
1935 const char *orig_param;
1938 struct url_spec compiled[1];
1948 orig_param = lookup(parameters, name);
1951 return JB_ERR_CGI_PARAMS;
1954 /* Copy and trim whitespace */
1955 param = strdup(orig_param);
1958 return JB_ERR_MEMORY;
1962 /* Must be non-empty, and can't allow 1st character to be '{' */
1963 if (param[0] == '\0' || param[0] == '{')
1966 return JB_ERR_CGI_PARAMS;
1969 /* Check for embedded newlines */
1970 for (s = param; *s != '\0'; s++)
1972 if ((*s == '\r') || (*s == '\n'))
1975 return JB_ERR_CGI_PARAMS;
1979 /* Check that 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);
1995 if (param[strlen(param) - 1] == '\\')
1998 * Must protect trailing '\\' from becoming line continuation character.
1999 * Two methods: 1) If it's a domain only, add a trailing '/'.
2000 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2002 if (strchr(param, '/') == NULL)
2004 err = string_append(¶m, "/");
2008 err = string_append(¶m, "(?:)");
2015 /* Check that the modified regex is valid */
2020 return JB_ERR_MEMORY;
2022 err = create_url_spec(compiled, s);
2027 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2029 free_url_spec(compiled);
2036 /*********************************************************************
2038 * Function : map_radio
2040 * Description : Map a set of radio button values. E.g. if you have
2041 * 3 radio buttons, declare them as:
2042 * <option type="radio" name="xyz" @xyz-a@>
2043 * <option type="radio" name="xyz" @xyz-b@>
2044 * <option type="radio" name="xyz" @xyz-c@>
2045 * Then map one of the @xyz-?@ variables to "checked"
2046 * and all the others to empty by calling:
2047 * map_radio(exports, "xyz", "abc", sel)
2048 * Where 'sel' is 'a', 'b', or 'c'.
2051 * 1 : exports = Exports map to modify.
2052 * 2 : optionname = name for map
2053 * 3 : values = null-terminated list of values;
2054 * 4 : value = Selected value.
2056 * CGI Parameters : None
2058 * Returns : JB_ERR_OK on success
2059 * JB_ERR_MEMORY on out-of-memory
2061 *********************************************************************/
2062 static jb_err map_radio(struct map * exports,
2063 const char * optionname,
2064 const char * values,
2076 len = strlen(optionname);
2077 buf = malloc(len + 3);
2080 return JB_ERR_MEMORY;
2083 strcpy(buf, optionname);
2088 while ((c = *values++) != '\0')
2093 if (map(exports, buf, 1, "", 1))
2096 return JB_ERR_MEMORY;
2102 if (map(exports, buf, 0, "checked", 1))
2105 return JB_ERR_MEMORY;
2112 /*********************************************************************
2114 * Function : cgi_error_modified
2116 * Description : CGI function that is called when a file is modified
2117 * outside the CGI editor.
2120 * 1 : csp = Current client state (buffers, headers, etc...)
2121 * 2 : rsp = http_response data structure for output
2122 * 3 : filename = The file that was modified.
2124 * CGI Parameters : none
2126 * Returns : JB_ERR_OK on success
2127 * JB_ERR_MEMORY on out-of-memory error.
2129 *********************************************************************/
2130 jb_err cgi_error_modified(struct client_state *csp,
2131 struct http_response *rsp,
2132 const char *filename)
2134 struct map *exports;
2141 if (NULL == (exports = default_exports(csp, NULL)))
2143 return JB_ERR_MEMORY;
2146 err = map(exports, "f", 1, html_encode(filename), 0);
2153 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2157 /*********************************************************************
2159 * Function : cgi_error_parse
2161 * Description : CGI function that is called when a file cannot
2162 * be parsed by the CGI editor.
2165 * 1 : csp = Current client state (buffers, headers, etc...)
2166 * 2 : rsp = http_response data structure for output
2167 * 3 : file = The file that was modified.
2169 * CGI Parameters : none
2171 * Returns : JB_ERR_OK on success
2172 * JB_ERR_MEMORY on out-of-memory error.
2174 *********************************************************************/
2175 jb_err cgi_error_parse(struct client_state *csp,
2176 struct http_response *rsp,
2177 struct editable_file *file)
2179 struct map *exports;
2181 struct file_line *cur_line;
2187 if (NULL == (exports = default_exports(csp, NULL)))
2189 return JB_ERR_MEMORY;
2192 err = map(exports, "f", 1, file->identifier, 1);
2193 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2195 cur_line = file->parse_error;
2198 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2199 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2207 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2211 /*********************************************************************
2213 * Function : cgi_error_file
2215 * Description : CGI function that is called when a file cannot be
2216 * opened by the CGI editor.
2219 * 1 : csp = Current client state (buffers, headers, etc...)
2220 * 2 : rsp = http_response data structure for output
2221 * 3 : filename = The file that was modified.
2223 * CGI Parameters : none
2225 * Returns : JB_ERR_OK on success
2226 * JB_ERR_MEMORY on out-of-memory error.
2228 *********************************************************************/
2229 jb_err cgi_error_file(struct client_state *csp,
2230 struct http_response *rsp,
2231 const char *filename)
2233 struct map *exports;
2240 if (NULL == (exports = default_exports(csp, NULL)))
2242 return JB_ERR_MEMORY;
2245 err = map(exports, "f", 1, html_encode(filename), 0);
2252 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2256 /*********************************************************************
2258 * Function : cgi_error_bad_param
2260 * Description : CGI function that is called if the parameters
2261 * (query string) for a CGI were wrong.
2264 * 1 : csp = Current client state (buffers, headers, etc...)
2265 * 2 : rsp = http_response data structure for output
2267 * CGI Parameters : none
2269 * Returns : JB_ERR_OK on success
2270 * JB_ERR_MEMORY on out-of-memory error.
2272 *********************************************************************/
2273 jb_err cgi_error_disabled(struct client_state *csp,
2274 struct http_response *rsp)
2276 struct map *exports;
2281 if (NULL == (exports = default_exports(csp, NULL)))
2283 return JB_ERR_MEMORY;
2286 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2290 /*********************************************************************
2292 * Function : cgi_edit_actions
2294 * Description : CGI function that allows the user to choose which
2295 * actions file to edit.
2298 * 1 : csp = Current client state (buffers, headers, etc...)
2299 * 2 : rsp = http_response data structure for output
2300 * 3 : parameters = map of cgi parameters
2302 * CGI Parameters : None
2304 * Returns : JB_ERR_OK on success
2305 * JB_ERR_MEMORY on out-of-memory error
2307 *********************************************************************/
2308 jb_err cgi_edit_actions(struct client_state *csp,
2309 struct http_response *rsp,
2310 const struct map *parameters)
2313 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2315 return cgi_error_disabled(csp, rsp);
2318 /* FIXME: Incomplete */
2319 rsp->status = strdup("302 Local Redirect from Junkbuster");
2320 if (rsp->status == NULL)
2322 return JB_ERR_MEMORY;
2324 if (enlist_unique_header(rsp->headers, "Location",
2325 CGI_PREFIX "edit-actions-list?f=ijb"))
2329 return JB_ERR_MEMORY;
2336 /*********************************************************************
2338 * Function : cgi_edit_actions_list
2340 * Description : CGI function that edits the actions list.
2341 * FIXME: This function shouldn't FATAL ever.
2342 * FIXME: This function doesn't check the retval of map()
2344 * 1 : csp = Current client state (buffers, headers, etc...)
2345 * 2 : rsp = http_response data structure for output
2346 * 3 : parameters = map of cgi parameters
2348 * CGI Parameters : filename
2350 * Returns : JB_ERR_OK on success
2351 * JB_ERR_MEMORY on out-of-memory
2352 * JB_ERR_FILE if the file cannot be opened or
2354 * JB_ERR_CGI_PARAMS if "filename" was not specified
2357 *********************************************************************/
2358 jb_err cgi_edit_actions_list(struct client_state *csp,
2359 struct http_response *rsp,
2360 const struct map *parameters)
2362 char * section_template;
2363 char * url_template;
2368 struct map * exports;
2369 struct map * section_exports;
2370 struct map * url_exports;
2371 struct editable_file * file;
2372 struct file_line * cur_line;
2373 unsigned line_number = 0;
2374 unsigned prev_section_line_number = ((unsigned) (-1));
2378 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2380 return cgi_error_disabled(csp, rsp);
2383 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2386 /* No filename specified, can't read file, or out of memory. */
2387 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2390 if (NULL == (exports = default_exports(csp, NULL)))
2392 edit_free_file(file);
2393 return JB_ERR_MEMORY;
2396 err = map(exports, "f", 1, file->identifier, 1);
2397 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2401 edit_free_file(file);
2406 /* Should do all global exports above this point */
2408 err = template_load(csp, §ion_template, "edit-actions-list-section");
2411 edit_free_file(file);
2413 if (err == JB_ERR_FILE)
2415 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2420 err = template_load(csp, &url_template, "edit-actions-list-url");
2423 free(section_template);
2424 edit_free_file(file);
2426 if (err == JB_ERR_FILE)
2428 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2433 err = template_fill(§ion_template, exports);
2437 edit_free_file(file);
2443 err = template_fill(&url_template, exports);
2446 free(section_template);
2447 edit_free_file(file);
2452 /* Find start of actions in file */
2453 cur_line = file->lines;
2455 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2457 cur_line = cur_line->next;
2461 if (NULL == (sections = strdup("")))
2463 free(section_template);
2465 edit_free_file(file);
2467 return JB_ERR_MEMORY;
2470 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2472 if (NULL == (section_exports = new_map()))
2475 free(section_template);
2477 edit_free_file(file);
2479 return JB_ERR_MEMORY;
2482 snprintf(buf, 50, "%d", line_number);
2483 err = map(section_exports, "s", 1, buf, 1);
2484 if (!err) err = map(section_exports, "actions", 1,
2485 actions_to_html(cur_line->data.action), 0);
2488 && (cur_line->next != NULL)
2489 && (cur_line->next->type == FILE_LINE_URL))
2491 /* This section contains at least one URL, don't allow delete */
2492 err = map_block_killer(section_exports, "empty-section");
2496 if (!err) err = map_block_keep(section_exports, "empty-section");
2499 if (prev_section_line_number != ((unsigned)(-1)))
2501 /* Not last section */
2502 snprintf(buf, 50, "%d", prev_section_line_number);
2503 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2504 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2509 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2511 prev_section_line_number = line_number;
2516 free(section_template);
2518 edit_free_file(file);
2520 free_map(section_exports);
2524 /* Should do all section-specific exports above this point */
2526 if (NULL == (urls = strdup("")))
2529 free(section_template);
2531 edit_free_file(file);
2533 free_map(section_exports);
2534 return JB_ERR_MEMORY;
2539 cur_line = cur_line->next;
2542 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2544 if (NULL == (url_exports = new_map()))
2548 free(section_template);
2550 edit_free_file(file);
2552 free_map(section_exports);
2553 return JB_ERR_MEMORY;
2556 snprintf(buf, 50, "%d", line_number);
2557 err = map(url_exports, "p", 1, buf, 1);
2559 snprintf(buf, 50, "%d", url_1_2);
2560 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2562 if (!err) err = map(url_exports, "url-html", 1,
2563 html_encode(cur_line->unprocessed), 0);
2564 if (!err) err = map(url_exports, "url", 1,
2565 url_encode(cur_line->unprocessed), 0);
2571 free(section_template);
2573 edit_free_file(file);
2575 free_map(section_exports);
2576 free_map(url_exports);
2580 if (NULL == (s = strdup(url_template)))
2584 free(section_template);
2586 edit_free_file(file);
2588 free_map(section_exports);
2589 free_map(url_exports);
2590 return JB_ERR_MEMORY;
2593 err = template_fill(&s, section_exports);
2594 if (!err) err = template_fill(&s, url_exports);
2595 if (!err) err = string_append(&urls, s);
2597 free_map(url_exports);
2604 free(section_template);
2606 edit_free_file(file);
2608 free_map(section_exports);
2612 url_1_2 = 3 - url_1_2;
2614 cur_line = cur_line->next;
2618 err = map(section_exports, "urls", 1, urls, 0);
2620 /* Could also do section-specific exports here, but it wouldn't be as fast */
2622 if ( (cur_line != NULL)
2623 && (cur_line->type == FILE_LINE_ACTION))
2625 /* Not last section */
2626 snprintf(buf, 50, "%d", line_number);
2627 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2628 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2633 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2639 free(section_template);
2641 edit_free_file(file);
2643 free_map(section_exports);
2647 if (NULL == (s = strdup(section_template)))
2650 free(section_template);
2652 edit_free_file(file);
2654 free_map(section_exports);
2655 return JB_ERR_MEMORY;
2658 err = template_fill(&s, section_exports);
2659 if (!err) err = string_append(§ions, s);
2662 free_map(section_exports);
2667 free(section_template);
2669 edit_free_file(file);
2675 edit_free_file(file);
2676 free(section_template);
2679 err = map(exports, "sections", 1, sections, 0);
2686 /* Could also do global exports here, but it wouldn't be as fast */
2688 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2692 /*********************************************************************
2694 * Function : cgi_edit_actions
2696 * Description : CGI function that edits the Actions list.
2699 * 1 : csp = Current client state (buffers, headers, etc...)
2700 * 2 : rsp = http_response data structure for output
2701 * 3 : parameters = map of cgi parameters
2703 * CGI Parameters : None
2705 * Returns : JB_ERR_OK on success
2706 * JB_ERR_MEMORY on out-of-memory
2707 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2708 * specified or not valid.
2710 *********************************************************************/
2711 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2712 struct http_response *rsp,
2713 const struct map *parameters)
2715 struct map * exports;
2717 struct editable_file * file;
2718 struct file_line * cur_line;
2719 unsigned line_number;
2722 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2724 return cgi_error_disabled(csp, rsp);
2727 err = get_number_param(csp, parameters, "s", §ionid);
2733 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2736 /* No filename specified, can't read file, modified, or out of memory. */
2737 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2740 cur_line = file->lines;
2742 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2744 cur_line = cur_line->next;
2747 if ( (cur_line == NULL)
2748 || (line_number != sectionid)
2750 || (cur_line->type != FILE_LINE_ACTION))
2752 /* Invalid "sectionid" parameter */
2753 edit_free_file(file);
2754 return JB_ERR_CGI_PARAMS;
2757 if (NULL == (exports = default_exports(csp, NULL)))
2759 edit_free_file(file);
2760 return JB_ERR_MEMORY;
2763 err = map(exports, "f", 1, file->identifier, 1);
2764 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2765 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2767 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2769 edit_free_file(file);
2777 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2781 /*********************************************************************
2783 * Function : cgi_edit_actions_submit
2785 * Description : CGI function that actually edits the Actions list.
2788 * 1 : csp = Current client state (buffers, headers, etc...)
2789 * 2 : rsp = http_response data structure for output
2790 * 3 : parameters = map of cgi parameters
2792 * CGI Parameters : None
2794 * Returns : JB_ERR_OK on success
2795 * JB_ERR_MEMORY on out-of-memory
2796 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2797 * specified or not valid.
2799 *********************************************************************/
2800 jb_err cgi_edit_actions_submit(struct client_state *csp,
2801 struct http_response *rsp,
2802 const struct map *parameters)
2808 struct editable_file * file;
2809 struct file_line * cur_line;
2810 unsigned line_number;
2814 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2816 return cgi_error_disabled(csp, rsp);
2819 err = get_number_param(csp, parameters, "s", §ionid);
2825 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2828 /* No filename specified, can't read file, modified, or out of memory. */
2829 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2832 cur_line = file->lines;
2834 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2836 cur_line = cur_line->next;
2839 if ( (cur_line == NULL)
2840 || (line_number != sectionid)
2842 || (cur_line->type != FILE_LINE_ACTION))
2844 /* Invalid "sectionid" parameter */
2845 edit_free_file(file);
2846 return JB_ERR_CGI_PARAMS;
2849 err = actions_from_radio(parameters, cur_line->data.action);
2853 edit_free_file(file);
2857 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2860 edit_free_file(file);
2861 return JB_ERR_MEMORY;
2864 len = strlen(actiontext);
2868 * Empty action - must special-case this.
2869 * Simply setting len to 1 is sufficient...
2874 if (NULL == (newtext = malloc(len + 2)))
2878 edit_free_file(file);
2879 return JB_ERR_MEMORY;
2881 strcpy(newtext, actiontext);
2885 newtext[len + 1] = '\0';
2887 freez(cur_line->raw);
2888 freez(cur_line->unprocessed);
2889 cur_line->unprocessed = newtext;
2891 err = edit_write_file(file);
2894 /* Error writing file */
2895 edit_free_file(file);
2899 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2900 string_append(&target, file->identifier);
2902 edit_free_file(file);
2907 return JB_ERR_MEMORY;
2910 rsp->status = strdup("302 Local Redirect from Junkbuster");
2911 if (rsp->status == NULL)
2914 return JB_ERR_MEMORY;
2916 err = enlist_unique_header(rsp->headers, "Location", target);
2923 /*********************************************************************
2925 * Function : cgi_edit_actions_url
2927 * Description : CGI function that actually edits a URL pattern in
2931 * 1 : csp = Current client state (buffers, headers, etc...)
2932 * 2 : rsp = http_response data structure for output
2933 * 3 : parameters = map of cgi parameters
2936 * filename : Identifies the file to edit
2937 * ver : File's last-modified time
2938 * section : Line number of section to edit
2939 * pattern : Line number of pattern to edit
2940 * newval : New value for pattern
2942 * Returns : JB_ERR_OK on success
2943 * JB_ERR_MEMORY on out-of-memory
2944 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2945 * specified or not valid.
2947 *********************************************************************/
2948 jb_err cgi_edit_actions_url(struct client_state *csp,
2949 struct http_response *rsp,
2950 const struct map *parameters)
2954 struct editable_file * file;
2955 struct file_line * cur_line;
2956 unsigned line_number;
2960 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2962 return cgi_error_disabled(csp, rsp);
2965 err = get_number_param(csp, parameters, "p", &patternid);
2972 return JB_ERR_CGI_PARAMS;
2975 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
2981 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2984 /* No filename specified, can't read file, modified, or out of memory. */
2986 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2990 cur_line = file->lines;
2992 while ((cur_line != NULL) && (line_number < patternid))
2994 cur_line = cur_line->next;
2998 if ( (cur_line == NULL)
2999 || (cur_line->type != FILE_LINE_URL))
3001 /* Invalid "patternid" parameter */
3003 edit_free_file(file);
3004 return JB_ERR_CGI_PARAMS;
3007 /* At this point, the line to edit is in cur_line */
3009 freez(cur_line->raw);
3010 freez(cur_line->unprocessed);
3011 cur_line->unprocessed = new_pattern;
3013 err = edit_write_file(file);
3016 /* Error writing file */
3017 edit_free_file(file);
3021 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3022 string_append(&target, file->identifier);
3024 edit_free_file(file);
3029 return JB_ERR_MEMORY;
3032 rsp->status = strdup("302 Local Redirect from Junkbuster");
3033 if (rsp->status == NULL)
3036 return JB_ERR_MEMORY;
3038 err = enlist_unique_header(rsp->headers, "Location", target);
3045 /*********************************************************************
3047 * Function : cgi_edit_actions_add_url
3049 * Description : CGI function that actually adds a URL pattern to
3053 * 1 : csp = Current client state (buffers, headers, etc...)
3054 * 2 : rsp = http_response data structure for output
3055 * 3 : parameters = map of cgi parameters
3058 * filename : Identifies the file to edit
3059 * ver : File's last-modified time
3060 * section : Line number of section to edit
3061 * newval : New pattern
3063 * Returns : JB_ERR_OK on success
3064 * JB_ERR_MEMORY on out-of-memory
3065 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3066 * specified or not valid.
3068 *********************************************************************/
3069 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3070 struct http_response *rsp,
3071 const struct map *parameters)
3075 struct file_line * new_line;
3076 struct editable_file * file;
3077 struct file_line * cur_line;
3078 unsigned line_number;
3082 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3084 return cgi_error_disabled(csp, rsp);
3087 err = get_number_param(csp, parameters, "s", §ionid);
3094 return JB_ERR_CGI_PARAMS;
3097 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3103 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3106 /* No filename specified, can't read file, modified, or out of memory. */
3108 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3112 cur_line = file->lines;
3114 while ((cur_line != NULL) && (line_number < sectionid))
3116 cur_line = cur_line->next;
3120 if ( (cur_line == NULL)
3121 || (cur_line->type != FILE_LINE_ACTION))
3123 /* Invalid "sectionid" parameter */
3125 edit_free_file(file);
3126 return JB_ERR_CGI_PARAMS;
3129 /* At this point, the section header is in cur_line - add after this. */
3131 /* Allocate the new line */
3132 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3133 if (new_line == NULL)
3136 edit_free_file(file);
3137 return JB_ERR_MEMORY;
3140 /* Fill in the data members of the new line */
3141 new_line->raw = NULL;
3142 new_line->prefix = NULL;
3143 new_line->unprocessed = new_pattern;
3144 new_line->type = FILE_LINE_URL;
3146 /* Link new_line into the list, after cur_line */
3147 new_line->next = cur_line->next;
3148 cur_line->next = new_line;
3150 /* Done making changes, now commit */
3152 err = edit_write_file(file);
3155 /* Error writing file */
3156 edit_free_file(file);
3160 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3161 string_append(&target, file->identifier);
3163 edit_free_file(file);
3168 return JB_ERR_MEMORY;
3171 rsp->status = strdup("302 Local Redirect from Junkbuster");
3172 if (rsp->status == NULL)
3175 return JB_ERR_MEMORY;
3177 err = enlist_unique_header(rsp->headers, "Location", target);
3184 /*********************************************************************
3186 * Function : cgi_edit_actions_remove_url
3188 * Description : CGI function that actually removes a URL pattern from
3192 * 1 : csp = Current client state (buffers, headers, etc...)
3193 * 2 : rsp = http_response data structure for output
3194 * 3 : parameters = map of cgi parameters
3197 * f : (filename) Identifies the file to edit
3198 * v : (version) File's last-modified time
3199 * p : (pattern) Line number of pattern to remove
3201 * Returns : JB_ERR_OK on success
3202 * JB_ERR_MEMORY on out-of-memory
3203 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3204 * specified or not valid.
3206 *********************************************************************/
3207 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3208 struct http_response *rsp,
3209 const struct map *parameters)
3212 struct editable_file * file;
3213 struct file_line * cur_line;
3214 struct file_line * prev_line;
3215 unsigned line_number;
3219 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3221 return cgi_error_disabled(csp, rsp);
3224 err = get_number_param(csp, parameters, "p", &patternid);
3230 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3233 /* No filename specified, can't read file, modified, or out of memory. */
3234 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3239 cur_line = file->lines;
3241 while ((cur_line != NULL) && (line_number < patternid))
3243 prev_line = cur_line;
3244 cur_line = cur_line->next;
3248 if ( (cur_line == NULL)
3249 || (prev_line == NULL)
3250 || (cur_line->type != FILE_LINE_URL))
3252 /* Invalid "patternid" parameter */
3253 edit_free_file(file);
3254 return JB_ERR_CGI_PARAMS;
3257 /* At this point, the line to remove is in cur_line, and the previous
3258 * one is in prev_line
3261 /* Unlink cur_line */
3262 prev_line->next = cur_line->next;
3263 cur_line->next = NULL;
3266 edit_free_file_lines(cur_line);
3268 err = edit_write_file(file);
3271 /* Error writing file */
3272 edit_free_file(file);
3276 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3277 string_append(&target, file->identifier);
3279 edit_free_file(file);
3284 return JB_ERR_MEMORY;
3287 rsp->status = strdup("302 Local Redirect from Junkbuster");
3288 if (rsp->status == NULL)
3291 return JB_ERR_MEMORY;
3293 err = enlist_unique_header(rsp->headers, "Location", target);
3300 /*********************************************************************
3302 * Function : cgi_edit_actions_section_remove
3304 * Description : CGI function that actually removes a whole section from
3305 * the actions file. The section must be empty first
3306 * (else JB_ERR_CGI_PARAMS).
3309 * 1 : csp = Current client state (buffers, headers, etc...)
3310 * 2 : rsp = http_response data structure for output
3311 * 3 : parameters = map of cgi parameters
3314 * f : (filename) Identifies the file to edit
3315 * v : (version) File's last-modified time
3316 * s : (section) Line number of section to edit
3318 * Returns : JB_ERR_OK on success
3319 * JB_ERR_MEMORY on out-of-memory
3320 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3321 * specified or not valid.
3323 *********************************************************************/
3324 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3325 struct http_response *rsp,
3326 const struct map *parameters)
3329 struct editable_file * file;
3330 struct file_line * cur_line;
3331 struct file_line * prev_line;
3332 unsigned line_number;
3336 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3338 return cgi_error_disabled(csp, rsp);
3341 err = get_number_param(csp, parameters, "s", §ionid);
3347 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3350 /* No filename specified, can't read file, modified, or out of memory. */
3351 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3355 cur_line = file->lines;
3358 while ((cur_line != NULL) && (line_number < sectionid))
3360 prev_line = cur_line;
3361 cur_line = cur_line->next;
3365 if ( (cur_line == NULL)
3366 || (cur_line->type != FILE_LINE_ACTION) )
3368 /* Invalid "sectionid" parameter */
3369 edit_free_file(file);
3370 return JB_ERR_CGI_PARAMS;
3373 if ( (cur_line->next != NULL)
3374 && (cur_line->next->type == FILE_LINE_URL) )
3376 /* Section not empty. */
3377 edit_free_file(file);
3378 return JB_ERR_CGI_PARAMS;
3381 /* At this point, the line to remove is in cur_line, and the previous
3382 * one is in prev_line
3385 /* Unlink cur_line */
3386 if (prev_line == NULL)
3388 /* Removing the first line from the file */
3389 file->lines = cur_line->next;
3393 prev_line->next = cur_line->next;
3395 cur_line->next = NULL;
3398 edit_free_file_lines(cur_line);
3400 err = edit_write_file(file);
3403 /* Error writing file */
3404 edit_free_file(file);
3408 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3409 string_append(&target, file->identifier);
3411 edit_free_file(file);
3416 return JB_ERR_MEMORY;
3419 rsp->status = strdup("302 Local Redirect from Junkbuster");
3420 if (rsp->status == NULL)
3423 return JB_ERR_MEMORY;
3425 err = enlist_unique_header(rsp->headers, "Location", target);
3432 /*********************************************************************
3434 * Function : cgi_edit_actions_section_add
3436 * Description : CGI function that adds a new empty section to
3440 * 1 : csp = Current client state (buffers, headers, etc...)
3441 * 2 : rsp = http_response data structure for output
3442 * 3 : parameters = map of cgi parameters
3445 * f : (filename) Identifies the file to edit
3446 * v : (version) File's last-modified time
3447 * s : (section) Line number of section to add after, 0 for
3450 * Returns : JB_ERR_OK on success
3451 * JB_ERR_MEMORY on out-of-memory
3452 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3453 * specified or not valid.
3455 *********************************************************************/
3456 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3457 struct http_response *rsp,
3458 const struct map *parameters)
3461 struct file_line * new_line;
3463 struct editable_file * file;
3464 struct file_line * cur_line;
3465 unsigned line_number;
3469 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3471 return cgi_error_disabled(csp, rsp);
3474 err = get_number_param(csp, parameters, "s", §ionid);
3480 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3483 /* No filename specified, can't read file, modified, or out of memory. */
3484 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3488 cur_line = file->lines;
3492 /* Add to start of file */
3493 if (cur_line != NULL)
3495 /* There's something in the file, find the line before the first
3498 while ( (cur_line->next != NULL)
3499 && (cur_line->next->type != FILE_LINE_ACTION) )
3501 cur_line = cur_line->next;
3508 /* Add after stated section. */
3509 while ((cur_line != NULL) && (line_number < sectionid))
3511 cur_line = cur_line->next;
3515 if ( (cur_line == NULL)
3516 || (cur_line->type != FILE_LINE_ACTION))
3518 /* Invalid "sectionid" parameter */
3519 edit_free_file(file);
3520 return JB_ERR_CGI_PARAMS;
3523 /* Skip through the section to find the last line in it. */
3524 while ( (cur_line->next != NULL)
3525 && (cur_line->next->type != FILE_LINE_ACTION) )
3527 cur_line = cur_line->next;
3532 /* At this point, the last line in the previous section is in cur_line
3533 * - add after this. (Or if we need to add as the first line, cur_line
3537 new_text = strdup("{}");
3538 if (NULL == new_text)
3540 edit_free_file(file);
3541 return JB_ERR_MEMORY;
3544 /* Allocate the new line */
3545 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3546 if (new_line == NULL)
3549 edit_free_file(file);
3550 return JB_ERR_MEMORY;
3553 /* Fill in the data members of the new line */
3554 new_line->raw = NULL;
3555 new_line->prefix = NULL;
3556 new_line->unprocessed = new_text;
3557 new_line->type = FILE_LINE_ACTION;
3559 if (cur_line != NULL)
3561 /* Link new_line into the list, after cur_line */
3562 new_line->next = cur_line->next;
3563 cur_line->next = new_line;
3567 /* Link new_line into the list, as first line */
3568 new_line->next = file->lines;
3569 file->lines = new_line;
3572 /* Done making changes, now commit */
3574 err = edit_write_file(file);
3577 /* Error writing file */
3578 edit_free_file(file);
3582 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3583 string_append(&target, file->identifier);
3585 edit_free_file(file);
3590 return JB_ERR_MEMORY;
3593 rsp->status = strdup("302 Local Redirect from Junkbuster");
3594 if (rsp->status == NULL)
3597 return JB_ERR_MEMORY;
3599 err = enlist_unique_header(rsp->headers, "Location", target);
3606 /*********************************************************************
3608 * Function : cgi_edit_actions_section_swap
3610 * Description : CGI function that swaps the order of two sections
3611 * in the actions file. Note that this CGI can actually
3612 * swap any two arbitrary sections, but the GUI interface
3613 * currently only allows consecutive sections to be
3617 * 1 : csp = Current client state (buffers, headers, etc...)
3618 * 2 : rsp = http_response data structure for output
3619 * 3 : parameters = map of cgi parameters
3622 * f : (filename) Identifies the file to edit
3623 * v : (version) File's last-modified time
3624 * s1 : (section1) Line number of first section to swap
3625 * s2 : (section2) Line number of second section to swap
3627 * Returns : JB_ERR_OK on success
3628 * JB_ERR_MEMORY on out-of-memory
3629 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3630 * specified or not valid.
3632 *********************************************************************/
3633 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3634 struct http_response *rsp,
3635 const struct map *parameters)
3639 struct editable_file * file;
3640 struct file_line * cur_line;
3641 struct file_line * prev_line;
3642 struct file_line * line_before_section1;
3643 struct file_line * line_start_section1;
3644 struct file_line * line_end_section1;
3645 struct file_line * line_after_section1;
3646 struct file_line * line_before_section2;
3647 struct file_line * line_start_section2;
3648 struct file_line * line_end_section2;
3649 struct file_line * line_after_section2;
3650 unsigned line_number;
3654 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3656 return cgi_error_disabled(csp, rsp);
3659 err = get_number_param(csp, parameters, "s1", §ion1);
3660 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3666 if (section1 > section2)
3668 unsigned temp = section2;
3669 section2 = section1;
3673 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3676 /* No filename specified, can't read file, modified, or out of memory. */
3677 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3680 /* Start at the beginning... */
3682 cur_line = file->lines;
3685 /* ... find section1 ... */
3686 while ((cur_line != NULL) && (line_number < section1))
3688 prev_line = cur_line;
3689 cur_line = cur_line->next;
3693 if ( (cur_line == NULL)
3694 || (cur_line->type != FILE_LINE_ACTION) )
3696 /* Invalid "section1" parameter */
3697 edit_free_file(file);
3698 return JB_ERR_CGI_PARAMS;
3701 /* If no-op, we've validated params and can skip the rest. */
3702 if (section1 != section2)
3704 /* ... find the end of section1 ... */
3705 line_before_section1 = prev_line;
3706 line_start_section1 = cur_line;
3709 prev_line = cur_line;
3710 cur_line = cur_line->next;
3713 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3714 line_end_section1 = prev_line;
3715 line_after_section1 = cur_line;
3717 /* ... find section2 ... */
3718 while ((cur_line != NULL) && (line_number < section2))
3720 prev_line = cur_line;
3721 cur_line = cur_line->next;
3725 if ( (cur_line == NULL)
3726 || (cur_line->type != FILE_LINE_ACTION) )
3728 /* Invalid "section2" parameter */
3729 edit_free_file(file);
3730 return JB_ERR_CGI_PARAMS;
3733 /* ... find the end of section2 ... */
3734 line_before_section2 = prev_line;
3735 line_start_section2 = cur_line;
3738 prev_line = cur_line;
3739 cur_line = cur_line->next;
3742 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3743 line_end_section2 = prev_line;
3744 line_after_section2 = cur_line;
3746 /* Now have all the pointers we need. Do the swap. */
3748 /* Change the pointer to section1 to point to section2 instead */
3749 if (line_before_section1 == NULL)
3751 file->lines = line_start_section2;
3755 line_before_section1->next = line_start_section2;
3758 if (line_before_section2 == line_end_section1)
3760 /* Consecutive sections */
3761 line_end_section2->next = line_start_section1;
3765 line_end_section2->next = line_after_section1;
3766 line_before_section2->next = line_start_section1;
3769 /* Set the pointer from the end of section1 to the rest of the file */
3770 line_end_section1->next = line_after_section2;
3772 err = edit_write_file(file);
3775 /* Error writing file */
3776 edit_free_file(file);
3779 } /* END if (section1 != section2) */
3781 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3782 string_append(&target, file->identifier);
3784 edit_free_file(file);
3789 return JB_ERR_MEMORY;
3792 rsp->status = strdup("302 Local Redirect from Junkbuster");
3793 if (rsp->status == NULL)
3796 return JB_ERR_MEMORY;
3798 err = enlist_unique_header(rsp->headers, "Location", target);
3805 /*********************************************************************
3807 * Function : cgi_toggle
3809 * Description : CGI function that adds a new empty section to
3813 * 1 : csp = Current client state (buffers, headers, etc...)
3814 * 2 : rsp = http_response data structure for output
3815 * 3 : parameters = map of cgi parameters
3818 * set : If present, how to change toggle setting:
3819 * "enable", "disable", "toggle", or none (default).
3820 * mini : If present, use mini reply template.
3822 * Returns : JB_ERR_OK on success
3823 * JB_ERR_MEMORY on out-of-memory
3825 *********************************************************************/
3826 jb_err cgi_toggle(struct client_state *csp,
3827 struct http_response *rsp,
3828 const struct map *parameters)
3830 struct map *exports;
3832 const char *template_name;
3839 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3841 return cgi_error_disabled(csp, rsp);
3844 if (NULL == (exports = default_exports(csp, "toggle")))
3846 return JB_ERR_MEMORY;
3849 mode = *(lookup(parameters, "set"));
3856 else if (mode == 'd')
3861 else if (mode == 't')
3864 g_bToggleIJB = !g_bToggleIJB;
3867 err = map_conditional(exports, "enabled", g_bToggleIJB);
3874 template_name = (*(lookup(parameters, "mini"))
3878 return template_fill_for_cgi(csp, template_name, exports, rsp);
3882 /*********************************************************************
3884 * Function : actions_to_radio
3886 * Description : Converts a actionsfile entry into settings for
3887 * radio buttons and edit boxes on a HTML form.
3890 * 1 : exports = List of substitutions to add to.
3891 * 2 : action = Action to read
3893 * Returns : JB_ERR_OK on success
3894 * JB_ERR_MEMORY on out-of-memory
3896 *********************************************************************/
3897 static jb_err actions_to_radio(struct map * exports,
3898 const struct action_spec *action)
3900 unsigned mask = action->mask;
3901 unsigned add = action->add;
3909 mask = action->mask;
3912 /* sanity - prevents "-feature +feature" */
3916 #define DEFINE_ACTION_BOOL(name, bit) \
3917 if (!(mask & bit)) \
3919 current_mode = 'n'; \
3921 else if (add & bit) \
3923 current_mode = 'y'; \
3927 current_mode = 'x'; \
3929 if (map_radio(exports, name, "ynx", current_mode)) \
3931 return JB_ERR_MEMORY; \
3934 #define DEFINE_ACTION_STRING(name, bit, index) \
3935 DEFINE_ACTION_BOOL(name, bit); \
3938 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
3941 checked = !strcmp(action->string[index], value); \
3945 checked = is_default; \
3947 mapped_param |= checked; \
3948 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
3950 return JB_ERR_MEMORY; \
3953 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
3954 if (map(exports, name "-param-custom", 1, \
3955 ((!mapped_param) ? "checked" : ""), 1)) \
3957 return JB_ERR_MEMORY; \
3959 if (map(exports, name "-param", 1, \
3960 (((add & bit) && !mapped_param) ? \
3961 action->string[index] : default_val), 1)) \
3963 return JB_ERR_MEMORY; \
3966 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
3967 if (map(exports, name "-param", 1, \
3968 ((add & bit) ? action->string[index] : default_val), 1)) \
3970 return JB_ERR_MEMORY; \
3973 #define DEFINE_ACTION_MULTI(name, index) \
3974 if (action->multi_add[index]->first) \
3976 current_mode = 'y'; \
3978 else if (action->multi_remove_all[index]) \
3980 current_mode = 'n'; \
3982 else if (action->multi_remove[index]->first) \
3984 current_mode = 'y'; \
3988 current_mode = 'x'; \
3990 if (map_radio(exports, name, "ynx", current_mode)) \
3992 return JB_ERR_MEMORY; \
3995 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
3997 #include "actionlist.h"
3999 #undef DEFINE_ACTION_MULTI
4000 #undef DEFINE_ACTION_STRING
4001 #undef DEFINE_ACTION_BOOL
4002 #undef DEFINE_ACTION_ALIAS
4003 #undef DEFINE_CGI_PARAM_CUSTOM
4004 #undef DEFINE_CGI_PARAM_RADIO
4005 #undef DEFINE_CGI_PARAM_NO_RADIO
4011 /*********************************************************************
4013 * Function : javascriptify
4015 * Description : Converts a string into a form JavaScript will like.
4017 * Netscape 4's JavaScript sucks - it doesn't use
4018 * "id" parameters, so you have to set the "name"
4019 * used to submit a form element to something JavaScript
4020 * will like. (Or access the elements by index in an
4021 * array. That array contains >60 elements and will
4022 * be changed whenever we add a new action to the
4023 * editor, so I'm NOT going to use indexes that have
4024 * to be figured out by hand.)
4026 * Currently the only thing we have to worry about
4027 * is "-" ==> "_" conversion.
4029 * This is a length-preserving operation so it is
4030 * carried out in-place, no memory is allocated
4034 * 1 : identifier = String to make JavaScript-friendly.
4038 *********************************************************************/
4039 static void javascriptify(char * identifier)
4041 char * p = identifier;
4042 while (NULL != (p = strchr(p, '-')))
4049 /*********************************************************************
4051 * Function : actions_from_radio
4053 * Description : Converts a map of parameters passed to a CGI function
4054 * into an actionsfile entry.
4057 * 1 : parameters = parameters to the CGI call
4058 * 2 : action = Action to change. Must be valid before
4059 * the call, actions not specified will be
4062 * Returns : JB_ERR_OK on success
4063 * JB_ERR_MEMORY on out-of-memory
4065 *********************************************************************/
4066 static jb_err actions_from_radio(const struct map * parameters,
4067 struct action_spec *action)
4069 static int first_time = 1;
4073 const char * js_name;
4078 /* Statics are generally a potential race condition,
4079 * but in this case we're safe and don't need semaphores.
4080 * Be careful if you modify this function.
4084 #define JAVASCRIPTIFY(dest_var, string) \
4086 static char js_name_arr[] = string; \
4089 javascriptify(js_name_arr); \
4091 dest_var = js_name_arr; \
4094 #define DEFINE_ACTION_BOOL(name, bit) \
4095 JAVASCRIPTIFY(js_name, name); \
4096 param = lookup(parameters, js_name); \
4097 ch = ijb_toupper(param[0]); \
4100 action->add |= bit; \
4101 action->mask |= bit; \
4103 else if (ch == 'N') \
4105 action->add &= ~bit; \
4106 action->mask &= ~bit; \
4108 else if (ch == 'X') \
4110 action->add &= ~bit; \
4111 action->mask |= bit; \
4114 #define DEFINE_ACTION_STRING(name, bit, index) \
4115 JAVASCRIPTIFY(js_name, name); \
4116 param = lookup(parameters, js_name); \
4117 ch = ijb_toupper(param[0]); \
4120 JAVASCRIPTIFY(js_name, name "-mode"); \
4121 param = lookup(parameters, js_name); \
4122 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4124 JAVASCRIPTIFY(js_name, name "-param"); \
4125 param = lookup(parameters, js_name); \
4127 if (*param != '\0') \
4129 if (NULL == (param_dup = strdup(param))) \
4131 return JB_ERR_MEMORY; \
4133 freez(action->string[index]); \
4134 action->add |= bit; \
4135 action->mask |= bit; \
4136 action->string[index] = param_dup; \
4139 else if (ch == 'N') \
4141 if (action->add & bit) \
4143 freez(action->string[index]); \
4145 action->add &= ~bit; \
4146 action->mask &= ~bit; \
4148 else if (ch == 'X') \
4150 if (action->add & bit) \
4152 freez(action->string[index]); \
4154 action->add &= ~bit; \
4155 action->mask |= bit; \
4158 #define DEFINE_ACTION_MULTI(name, index) \
4159 JAVASCRIPTIFY(js_name, name); \
4160 param = lookup(parameters, js_name); \
4161 ch = ijb_toupper((int)param[0]); \
4166 else if (ch == 'N') \
4168 list_remove_all(action->multi_add[index]); \
4169 list_remove_all(action->multi_remove[index]); \
4170 action->multi_remove_all[index] = 1; \
4172 else if (ch == 'X') \
4174 list_remove_all(action->multi_add[index]); \
4175 list_remove_all(action->multi_remove[index]); \
4176 action->multi_remove_all[index] = 0; \
4179 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4181 #include "actionlist.h"
4183 #undef DEFINE_ACTION_MULTI
4184 #undef DEFINE_ACTION_STRING
4185 #undef DEFINE_ACTION_BOOL
4186 #undef DEFINE_ACTION_ALIAS
4187 #undef JAVASCRIPTIFY
4195 #endif /* def FEATURE_CGI_EDIT_ACTIONS */