1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.12 2002/03/03 09:18:03 joergs 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.12 2002/03/03 09:18:03 joergs
46 * Made jumbjuster work on AmigaOS again.
48 * Revision 1.11 2002/01/23 01:03:31 jongfoster
49 * Fixing gcc [CygWin] compiler warnings
51 * Revision 1.10 2002/01/23 00:22:59 jongfoster
52 * Adding new function cgi_edit_actions_section_swap(), to reorder
55 * Adding get_url_spec_param() to get a validated URL pattern.
57 * Moving edit_read_line() out of this file and into loaders.c.
59 * Adding missing html_encode() to many CGI functions.
61 * Moving the functions that #include actionlist.h to the end of the file,
62 * because the Visual C++ 97 debugger gets extremely confused if you try
63 * to debug any code that comes after them in the file.
65 * Major optimizations in cgi_edit_actions_list() to reduce the size of
66 * the generated HTML (down 40% from 550k to 304k), with major side-effects
67 * throughout the editor and templates. In particular, the length of the
68 * URLs throughout the editor has been drastically reduced, by cutting
69 * paramater names down to 1 character and CGI names down to 3-4
70 * characters, by removing all non-essential CGI paramaters even at the
71 * expense of having to re-read the actions file for the most trivial
72 * page, and by using relative rather than absolute URLs. This means
73 * that this (typical example):
75 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
76 * filename=ijb&ver=1011487572&section=12&pattern=13
77 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
81 * <a href="eau?f=ijb&v=1011487572&p=13">
83 * Revision 1.9 2002/01/17 20:56:22 jongfoster
84 * Replacing hard references to the URL of the config interface
85 * with #defines from project.h
87 * Revision 1.8 2001/11/30 23:35:51 jongfoster
88 * Renaming actionsfile to ijb.action
90 * Revision 1.7 2001/11/13 00:28:24 jongfoster
91 * - Renaming parameters from edit-actions-for-url so that they only
92 * contain legal JavaScript characters. If we wanted to write
93 * JavaScript that worked with Netscape 4, this is nessacery.
94 * (Note that at the moment the JavaScript doesn't actually work
95 * with Netscape 4, but now this is purely a template issue, not
96 * one affecting code).
97 * - Adding new CGIs for use by non-JavaScript browsers:
98 * edit-actions-url-form
99 * edit-actions-add-url-form
100 * edit-actions-remove-url-form
103 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
104 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
105 * by and __OS2__ ifdef.
107 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
108 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
109 * threads to call select() simultaneously. So, it's time to do a real, live,
110 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
111 * (native). Both versions will work, but using __OS2__ offers multi-threading.
113 * Revision 1.4 2001/10/23 21:48:19 jongfoster
114 * Cleaning up error handling in CGI functions - they now send back
115 * a HTML error page and should never cause a FATAL error. (Fixes one
116 * potential source of "denial of service" attacks).
118 * CGI actions file editor that works and is actually useful.
120 * Ability to toggle JunkBuster remotely using a CGI call.
122 * You can turn off both the above features in the main configuration
123 * file, e.g. if you are running a multi-user proxy.
125 * Revision 1.3 2001/10/14 22:12:49 jongfoster
126 * New version of CGI-based actionsfile editor.
127 * Major changes, including:
128 * - Completely new file parser and file output routines
129 * - edit-actions CGI renamed edit-actions-for-url
130 * - All CGIs now need a filename parameter, except for...
131 * - New CGI edit-actions which doesn't need a filename,
132 * to allow you to start the editor up.
133 * - edit-actions-submit now works, and now automatically
134 * redirects you back to the main edit-actions-list handler.
136 * Revision 1.2 2001/09/16 17:05:14 jongfoster
137 * Removing unused #include showarg.h
139 * Revision 1.1 2001/09/16 15:47:37 jongfoster
140 * First version of CGI-based edit interface. This is very much a
141 * work-in-progress, and you can't actually use it to edit anything
142 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
143 * to have any effect.
146 **********************************************************************/
152 * FIXME: Following includes copied from cgi.c - which are actually needed?
157 #include <sys/types.h>
162 #include <sys/stat.h>
165 #define snprintf _snprintf
166 #endif /* def _WIN32 */
171 #include "cgisimple.h"
175 #include "miscutil.h"
179 /* loadcfg.h is for g_bToggleIJB only */
180 #include "urlmatch.h"
182 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
185 #ifdef FEATURE_CGI_EDIT_ACTIONS
189 struct file_line * next;
197 struct action_spec action[1];
206 /* Add more data types here... e.g.
209 struct url_spec url[1];
213 struct action_spec action[1];
222 #define FILE_LINE_UNPROCESSED 1
223 #define FILE_LINE_BLANK 2
224 #define FILE_LINE_ALIAS_HEADER 3
225 #define FILE_LINE_ALIAS_ENTRY 4
226 #define FILE_LINE_ACTION 5
227 #define FILE_LINE_URL 6
228 #define FILE_LINE_SETTINGS_HEADER 7
229 #define FILE_LINE_SETTINGS_ENTRY 8
230 #define FILE_LINE_DESCRIPTION_HEADER 9
231 #define FILE_LINE_DESCRIPTION_ENTRY 10
236 struct file_line * lines;
237 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
238 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
239 /* Pre-encoded with url_encode() for ease of use. */
240 const char * version_str; /* Last modification time, as a string. For CGI param */
241 /* Can be used in URL without using url_param(). */
242 unsigned version; /* Last modification time - prevents chaos with
243 * the browser's "back" button. Note that this is a
244 * time_t cast to an unsigned. When comparing, always
245 * cast the time_t to an unsigned, and *NOT* vice-versa.
246 * This may lose the top few bits, but they're not
247 * significant anyway.
249 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
250 * Note that changing this after the file has been
251 * read in will cause a mess.
253 struct file_line * parse_error; /* On parse error, this is the offending line. */
254 const char * parse_error_text; /* On parse error, this is the problem.
255 * (Statically allocated) */
258 /* FIXME: Following non-static functions should be prototyped in .h or made static */
260 /* Functions to read and write arbitrary config files */
261 jb_err edit_read_file(struct client_state *csp,
262 const struct map *parameters,
265 struct editable_file **pfile);
266 jb_err edit_write_file(struct editable_file * file);
267 void edit_free_file(struct editable_file * file);
269 /* Functions to read and write actions files */
270 jb_err edit_parse_actions_file(struct editable_file * file);
271 jb_err edit_read_actions_file(struct client_state *csp,
272 struct http_response *rsp,
273 const struct map *parameters,
275 struct editable_file **pfile);
278 jb_err cgi_error_modified(struct client_state *csp,
279 struct http_response *rsp,
280 const char *filename);
281 jb_err cgi_error_parse(struct client_state *csp,
282 struct http_response *rsp,
283 struct editable_file *file);
284 jb_err cgi_error_file(struct client_state *csp,
285 struct http_response *rsp,
286 const char *filename);
287 jb_err cgi_error_disabled(struct client_state *csp,
288 struct http_response *rsp);
290 /* Internal arbitrary config file support functions */
291 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
292 static void edit_free_file_lines(struct file_line * first_line);
294 /* Internal actions file support functions */
295 static int match_actions_file_header_line(const char * line, const char * name);
296 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
298 /* Internal parameter parsing functions */
299 static jb_err get_file_name_param(struct client_state *csp,
300 const struct map *parameters,
301 const char *param_name,
304 const char **pparam);
305 static jb_err get_number_param(struct client_state *csp,
306 const struct map *parameters,
309 static jb_err get_url_spec_param(struct client_state *csp,
310 const struct map *parameters,
314 /* Internal actionsfile <==> HTML conversion functions */
315 static jb_err map_radio(struct map * exports,
316 const char * optionname,
319 static jb_err actions_to_radio(struct map * exports,
320 const struct action_spec *action);
321 static jb_err actions_from_radio(const struct map * parameters,
322 struct action_spec *action);
325 static jb_err map_copy_parameter_html(struct map *out,
326 const struct map *in,
328 #if 0 /* unused function */
329 static jb_err map_copy_parameter_url(struct map *out,
330 const struct map *in,
332 #endif /* unused function */
334 /*********************************************************************
336 * Function : map_copy_parameter_html
338 * Description : Copy a CGI parameter from one map to another, HTML
342 * 1 : out = target map
343 * 2 : in = source map
344 * 3 : name = name of cgi parameter to copy
346 * Returns : JB_ERR_OK on success
347 * JB_ERR_MEMORY on out-of-memory
348 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
351 *********************************************************************/
352 static jb_err map_copy_parameter_html(struct map *out,
353 const struct map *in,
363 value = lookup(in, name);
364 err = map(out, name, 1, html_encode(value), 0);
371 else if (*value == '\0')
373 return JB_ERR_CGI_PARAMS;
382 #if 0 /* unused function */
383 /*********************************************************************
385 * Function : map_copy_parameter_html
387 * Description : Copy a CGI parameter from one map to another, URL
391 * 1 : out = target map
392 * 2 : in = source map
393 * 3 : name = name of cgi parameter to copy
395 * Returns : JB_ERR_OK on success
396 * JB_ERR_MEMORY on out-of-memory
397 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
400 *********************************************************************/
401 static jb_err map_copy_parameter_url(struct map *out,
402 const struct map *in,
412 value = lookup(in, name);
413 err = map(out, name, 1, url_encode(value), 0);
420 else if (*value == '\0')
422 return JB_ERR_CGI_PARAMS;
429 #endif /* 0 - unused function */
431 /*********************************************************************
433 * Function : cgi_edit_actions_url_form
435 * Description : CGI function that displays a form for
439 * 1 : csp = Current client state (buffers, headers, etc...)
440 * 2 : rsp = http_response data structure for output
441 * 3 : parameters = map of cgi parameters
444 * f : (filename) Identifies the file to edit
445 * v : (version) File's last-modified time
446 * p : (pattern) Line number of pattern to edit
448 * Returns : JB_ERR_OK on success
449 * JB_ERR_MEMORY on out-of-memory
450 * JB_ERR_CGI_PARAMS if the CGI parameters are not
451 * specified or not valid.
453 *********************************************************************/
454 jb_err cgi_edit_actions_url_form(struct client_state *csp,
455 struct http_response *rsp,
456 const struct map *parameters)
458 struct map * exports;
460 struct editable_file * file;
461 struct file_line * cur_line;
462 unsigned line_number;
469 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
471 return cgi_error_disabled(csp, rsp);
474 err = get_number_param(csp, parameters, "p", &patternid);
480 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
483 /* No filename specified, can't read file, modified, or out of memory. */
484 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
487 cur_line = file->lines;
489 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
491 cur_line = cur_line->next;
494 if ( (cur_line == NULL)
495 || (line_number != patternid)
497 || (cur_line->type != FILE_LINE_URL))
499 /* Invalid "patternid" parameter */
500 edit_free_file(file);
501 return JB_ERR_CGI_PARAMS;
504 if (NULL == (exports = default_exports(csp, NULL)))
506 edit_free_file(file);
507 return JB_ERR_MEMORY;
510 err = map(exports, "f", 1, file->identifier, 1);
511 if (!err) err = map(exports, "v", 1, file->version_str, 1);
512 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
513 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
515 edit_free_file(file);
523 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
527 /*********************************************************************
529 * Function : cgi_edit_actions_add_url_form
531 * Description : CGI function that displays a form for
535 * 1 : csp = Current client state (buffers, headers, etc...)
536 * 2 : rsp = http_response data structure for output
537 * 3 : parameters = map of cgi parameters
540 * f : (filename) Identifies the file to edit
541 * v : (version) File's last-modified time
542 * s : (section) Line number of section to edit
544 * Returns : JB_ERR_OK on success
545 * JB_ERR_MEMORY on out-of-memory
546 * JB_ERR_CGI_PARAMS if the CGI parameters are not
547 * specified or not valid.
549 *********************************************************************/
550 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
551 struct http_response *rsp,
552 const struct map *parameters)
561 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
563 return cgi_error_disabled(csp, rsp);
566 if (NULL == (exports = default_exports(csp, NULL)))
568 return JB_ERR_MEMORY;
571 err = map_copy_parameter_html(exports, parameters, "f");
572 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
573 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
581 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
585 /*********************************************************************
587 * Function : cgi_edit_actions_remove_url_form
589 * Description : CGI function that displays a form for
593 * 1 : csp = Current client state (buffers, headers, etc...)
594 * 2 : rsp = http_response data structure for output
595 * 3 : parameters = map of cgi parameters
598 * f : (filename) Identifies the file to edit
599 * v : (version) File's last-modified time
600 * p : (pattern) Line number of pattern to edit
602 * Returns : JB_ERR_OK on success
603 * JB_ERR_MEMORY on out-of-memory
604 * JB_ERR_CGI_PARAMS if the CGI parameters are not
605 * specified or not valid.
607 *********************************************************************/
608 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
609 struct http_response *rsp,
610 const struct map *parameters)
612 struct map * exports;
614 struct editable_file * file;
615 struct file_line * cur_line;
616 unsigned line_number;
623 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
625 return cgi_error_disabled(csp, rsp);
628 err = get_number_param(csp, parameters, "p", &patternid);
634 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
637 /* No filename specified, can't read file, modified, or out of memory. */
638 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
641 cur_line = file->lines;
643 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
645 cur_line = cur_line->next;
648 if ( (cur_line == NULL)
649 || (line_number != patternid)
651 || (cur_line->type != FILE_LINE_URL))
653 /* Invalid "patternid" parameter */
654 edit_free_file(file);
655 return JB_ERR_CGI_PARAMS;
658 if (NULL == (exports = default_exports(csp, NULL)))
660 edit_free_file(file);
661 return JB_ERR_MEMORY;
664 err = map(exports, "f", 1, file->identifier, 1);
665 if (!err) err = map(exports, "v", 1, file->version_str, 1);
666 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
667 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
669 edit_free_file(file);
677 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
681 /*********************************************************************
683 * Function : edit_write_file
685 * Description : Write a complete file to disk.
688 * 1 : filename = File to write to.
689 * 2 : file = Data structure to write.
691 * Returns : JB_ERR_OK on success
692 * JB_ERR_FILE on error writing to file.
693 * JB_ERR_MEMORY on out of memory
695 *********************************************************************/
696 jb_err edit_write_file(struct editable_file * file)
699 struct file_line * cur_line;
700 struct stat statbuf[1];
701 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
702 digits in time_t, assuming this is a 64-bit
703 machine, plus null terminator, plus one
707 assert(file->filename);
709 #if defined(AMIGA) || defined(__OS2__)
710 if (NULL == (fp = fopen(file->filename, "w")))
712 if (NULL == (fp = fopen(file->filename, "wt")))
713 #endif /* def AMIGA */
718 cur_line = file->lines;
719 while (cur_line != NULL)
723 if (fputs(cur_line->raw, fp) < 0)
731 if (cur_line->prefix)
733 if (fputs(cur_line->prefix, fp) < 0)
739 if (cur_line->unprocessed)
741 /* This should be a single line - sanity check. */
742 assert(NULL == strchr(cur_line->unprocessed, '\r'));
743 assert(NULL == strchr(cur_line->unprocessed, '\n'));
745 if (NULL != strchr(cur_line->unprocessed, '#'))
747 /* Must quote '#' characters */
754 /* Count number of # characters, so we know length of output string */
755 src = cur_line->unprocessed;
756 while (NULL != (src = strchr(src, '#')))
763 /* Allocate new memory for string */
764 len = strlen(cur_line->unprocessed);
765 if (NULL == (str = malloc(len + 1 + numhash)))
767 /* Uh oh, just trashed file! */
769 return JB_ERR_MEMORY;
772 /* Loop through string from end */
773 src = cur_line->unprocessed + len;
774 dest = str + len + numhash;
775 for ( ; len >= 0; len--)
777 if ((*dest-- = *src--) == '#')
781 assert(numhash >= 0);
784 assert(numhash == 0);
785 assert(src + 1 == cur_line->unprocessed);
786 assert(dest + 1 == str);
788 if (fputs(str, fp) < 0)
799 /* Can write without quoting '#' characters. */
800 if (fputs(cur_line->unprocessed, fp) < 0)
806 if (fputs(NEWLINE(file->newline), fp) < 0)
814 /* FIXME: Write data from file->data->whatever */
818 cur_line = cur_line->next;
824 /* Update the version stamp in the file structure, since we just
825 * wrote to the file & changed it's date.
827 if (stat(file->filename, statbuf) < 0)
829 /* Error, probably file not found. */
832 file->version = (unsigned)statbuf->st_mtime;
834 /* Correct file->version_str */
835 freez(file->version_str);
836 snprintf(version_buf, 22, "%u", file->version);
837 version_buf[21] = '\0';
838 file->version_str = strdup(version_buf);
839 if (version_buf == NULL)
841 return JB_ERR_MEMORY;
848 /*********************************************************************
850 * Function : edit_free_file
852 * Description : Free a complete file in memory.
855 * 1 : file = Data structure to free.
859 *********************************************************************/
860 void edit_free_file(struct editable_file * file)
864 /* Silently ignore NULL pointer */
868 edit_free_file_lines(file->lines);
869 freez(file->filename);
870 freez(file->identifier);
871 freez(file->version_str);
873 file->parse_error_text = NULL; /* Statically allocated */
874 file->parse_error = NULL;
880 /*********************************************************************
882 * Function : edit_free_file
884 * Description : Free an entire linked list of file lines.
887 * 1 : first_line = Data structure to free.
891 *********************************************************************/
892 static void edit_free_file_lines(struct file_line * first_line)
894 struct file_line * next_line;
896 while (first_line != NULL)
898 next_line = first_line->next;
899 first_line->next = NULL;
900 freez(first_line->raw);
901 freez(first_line->prefix);
902 freez(first_line->unprocessed);
903 switch(first_line->type)
905 case 0: /* special case if memory zeroed */
906 case FILE_LINE_UNPROCESSED:
907 case FILE_LINE_BLANK:
908 case FILE_LINE_ALIAS_HEADER:
909 case FILE_LINE_SETTINGS_HEADER:
910 case FILE_LINE_DESCRIPTION_HEADER:
911 case FILE_LINE_DESCRIPTION_ENTRY:
912 case FILE_LINE_ALIAS_ENTRY:
914 /* No data is stored for these */
917 case FILE_LINE_ACTION:
918 free_action(first_line->data.action);
921 case FILE_LINE_SETTINGS_ENTRY:
922 freez(first_line->data.setting.name);
923 freez(first_line->data.setting.svalue);
926 /* Should never happen */
930 first_line->type = 0; /* paranoia */
932 first_line = next_line;
937 /*********************************************************************
939 * Function : match_actions_file_header_line
941 * Description : Match an actions file {{header}} line
944 * 1 : line - String from file
945 * 2 : name - Header to match against
947 * Returns : 0 iff they match.
949 *********************************************************************/
950 static int match_actions_file_header_line(const char * line, const char * name)
958 if ((line[0] != '{') || (line[1] != '{'))
964 /* Look for optional whitespace */
965 while ( (*line == ' ') || (*line == '\t') )
970 /* Look for the specified name (case-insensitive) */
972 if (0 != strncmpic(line, name, len))
978 /* Look for optional whitespace */
979 while ( (*line == ' ') || (*line == '\t') )
984 /* Look for "}}" and end of string*/
985 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
995 /*********************************************************************
997 * Function : match_actions_file_header_line
999 * Description : Match an actions file {{header}} line
1002 * 1 : line - String from file. Must not start with
1003 * whitespace (else infinite loop!)
1004 * 2 : name - Destination for name
1005 * 2 : name - Destination for value
1007 * Returns : JB_ERR_OK on success
1008 * JB_ERR_MEMORY on out-of-memory
1009 * JB_ERR_PARSE if there's no "=" sign, or if there's
1010 * nothing before the "=" sign (but empty
1011 * values *after* the "=" sign are legal).
1013 *********************************************************************/
1014 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1016 const char * name_end;
1017 const char * value_start;
1023 assert(*line != ' ');
1024 assert(*line != '\t');
1029 value_start = strchr(line, '=');
1030 if ((value_start == NULL) || (value_start == line))
1032 return JB_ERR_PARSE;
1035 name_end = value_start - 1;
1037 /* Eat any whitespace before the '=' */
1038 while ((*name_end == ' ') || (*name_end == '\t'))
1041 * we already know we must have at least 1 non-ws char
1042 * at start of buf - no need to check
1047 name_len = name_end - line + 1; /* Length excluding \0 */
1048 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1050 return JB_ERR_MEMORY;
1052 strncpy(*pname, line, name_len);
1053 (*pname)[name_len] = '\0';
1055 /* Eat any the whitespace after the '=' */
1057 while ((*value_start == ' ') || (*value_start == '\t'))
1062 if (NULL == (*pvalue = strdup(value_start)))
1066 return JB_ERR_MEMORY;
1073 /*********************************************************************
1075 * Function : edit_parse_actions_file
1077 * Description : Parse an actions file in memory.
1079 * Passed linked list must have the "data" member
1080 * zeroed, and must contain valid "next" and
1081 * "unprocessed" fields. The "raw" and "prefix"
1082 * fields are ignored, and "type" is just overwritten.
1084 * Note that on error the file may have been
1088 * 1 : file = Actions file to be parsed in-place.
1090 * Returns : JB_ERR_OK on success
1091 * JB_ERR_MEMORY on out-of-memory
1092 * JB_ERR_PARSE on error
1094 *********************************************************************/
1095 jb_err edit_parse_actions_file(struct editable_file * file)
1097 struct file_line * cur_line;
1099 const char * text; /* Text from a line */
1100 char * name; /* For lines of the form name=value */
1101 char * value; /* For lines of the form name=value */
1102 struct action_alias * alias_list = NULL;
1103 jb_err err = JB_ERR_OK;
1105 /* alias_list contains the aliases defined in this file.
1106 * It might be better to use the "file_line.data" fields
1107 * in the relavent places instead.
1110 cur_line = file->lines;
1112 /* A note about blank line support: Blank lines should only
1113 * ever occur as the last line in the file. This function
1114 * is more forgiving than that - FILE_LINE_BLANK can occur
1118 /* Skip leading blanks. Should only happen if file is
1119 * empty (which is valid, but pointless).
1121 while ( (cur_line != NULL)
1122 && (cur_line->unprocessed[0] == '\0') )
1125 cur_line->type = FILE_LINE_BLANK;
1126 cur_line = cur_line->next;
1129 if ( (cur_line != NULL)
1130 && (cur_line->unprocessed[0] != '{') )
1132 /* File doesn't start with a header */
1133 file->parse_error = cur_line;
1134 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1135 return JB_ERR_PARSE;
1138 if ( (cur_line != NULL) && (0 ==
1139 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1141 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1143 cur_line = cur_line->next;
1144 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1146 if (cur_line->unprocessed[0])
1148 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1150 err = split_line_on_equals(cur_line->unprocessed,
1151 &cur_line->data.setting.name,
1152 &cur_line->data.setting.svalue);
1153 if (err == JB_ERR_MEMORY)
1157 else if (err != JB_ERR_OK)
1159 /* Line does not contain a name=value pair */
1160 file->parse_error = cur_line;
1161 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1162 return JB_ERR_PARSE;
1167 cur_line->type = FILE_LINE_BLANK;
1169 cur_line = cur_line->next;
1173 if ( (cur_line != NULL) && (0 ==
1174 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1176 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1178 cur_line = cur_line->next;
1179 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1181 if (cur_line->unprocessed[0])
1183 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1187 cur_line->type = FILE_LINE_BLANK;
1189 cur_line = cur_line->next;
1193 if ( (cur_line != NULL) && (0 ==
1194 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1196 cur_line->type = FILE_LINE_ALIAS_HEADER;
1198 cur_line = cur_line->next;
1199 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1201 if (cur_line->unprocessed[0])
1203 /* define an alias */
1204 struct action_alias * new_alias;
1206 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1208 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1209 if (err == JB_ERR_MEMORY)
1213 else if (err != JB_ERR_OK)
1215 /* Line does not contain a name=value pair */
1216 file->parse_error = cur_line;
1217 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1218 return JB_ERR_PARSE;
1221 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1226 free_alias_list(alias_list);
1227 return JB_ERR_MEMORY;
1230 err = get_actions(value, alias_list, new_alias->action);
1233 /* Invalid action or out of memory */
1237 free_alias_list(alias_list);
1238 if (err == JB_ERR_MEMORY)
1244 /* Line does not contain a name=value pair */
1245 file->parse_error = cur_line;
1246 file->parse_error_text = "This alias does not specify a valid set of actions.";
1247 return JB_ERR_PARSE;
1253 new_alias->name = name;
1256 new_alias->next = alias_list;
1257 alias_list = new_alias;
1261 cur_line->type = FILE_LINE_BLANK;
1263 cur_line = cur_line->next;
1267 /* Header done, process the main part of the file */
1268 while (cur_line != NULL)
1270 /* At this point, (cur_line->unprocessed[0] == '{') */
1271 assert(cur_line->unprocessed[0] == '{');
1272 text = cur_line->unprocessed + 1;
1273 len = strlen(text) - 1;
1274 if (text[len] != '}')
1276 /* No closing } on header */
1277 free_alias_list(alias_list);
1278 file->parse_error = cur_line;
1279 file->parse_error_text = "Headers starting with '{' must have a "
1280 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1281 "must close with two brackets ('}}').";
1282 return JB_ERR_PARSE;
1287 /* An invalid {{ header. */
1288 free_alias_list(alias_list);
1289 file->parse_error = cur_line;
1290 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1291 "Please remember that the system (two-bracket) headers must "
1292 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1293 "and must appear before any actions (one-bracket) headers. "
1294 "Also note that system headers may not be repeated.";
1295 return JB_ERR_PARSE;
1298 while ( (*text == ' ') || (*text == '\t') )
1304 && ( (text[len - 1] == ' ')
1305 || (text[len - 1] == '\t') ) )
1310 cur_line->type = FILE_LINE_ACTION;
1312 /* Remove {} and make copy */
1313 if (NULL == (value = (char *) malloc(len + 1)))
1316 free_alias_list(alias_list);
1317 return JB_ERR_MEMORY;
1319 strncpy(value, text, len);
1323 err = get_actions(value, alias_list, cur_line->data.action);
1326 /* Invalid action or out of memory */
1328 free_alias_list(alias_list);
1329 if (err == JB_ERR_MEMORY)
1335 /* Line does not contain a name=value pair */
1336 file->parse_error = cur_line;
1337 file->parse_error_text = "This header does not specify a valid set of actions.";
1338 return JB_ERR_PARSE;
1342 /* Done with string - it was clobbered anyway */
1345 /* Process next line */
1346 cur_line = cur_line->next;
1348 /* Loop processing URL patterns */
1349 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1351 if (cur_line->unprocessed[0])
1353 /* Could parse URL here, but this isn't currently needed */
1355 cur_line->type = FILE_LINE_URL;
1359 cur_line->type = FILE_LINE_BLANK;
1361 cur_line = cur_line->next;
1363 } /* End main while(cur_line != NULL) loop */
1365 free_alias_list(alias_list);
1371 /*********************************************************************
1373 * Function : edit_read_file_lines
1375 * Description : Read all the lines of a file into memory.
1376 * Handles whitespace, comments and line continuation.
1379 * 1 : fp = File to read from. On return, this will be
1380 * at EOF but it will not have been closed.
1381 * 2 : pfile = Destination for a linked list of file_lines.
1382 * Will be set to NULL on error.
1384 * Returns : JB_ERR_OK on success
1385 * JB_ERR_MEMORY on out-of-memory
1387 *********************************************************************/
1388 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1390 struct file_line * first_line; /* Keep for return value or to free */
1391 struct file_line * cur_line; /* Current line */
1392 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1400 cur_line = first_line = zalloc(sizeof(struct file_line));
1401 if (cur_line == NULL)
1403 return JB_ERR_MEMORY;
1406 cur_line->type = FILE_LINE_UNPROCESSED;
1408 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1411 /* Out of memory or empty file. */
1412 /* Note that empty file is not an error we propogate up */
1414 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1419 prev_line = cur_line;
1420 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1421 if (cur_line == NULL)
1424 edit_free_file_lines(first_line);
1425 return JB_ERR_MEMORY;
1428 cur_line->type = FILE_LINE_UNPROCESSED;
1430 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1431 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1434 edit_free_file_lines(first_line);
1435 return JB_ERR_MEMORY;
1439 while (rval != JB_ERR_FILE);
1443 /* We allocated one too many - free it */
1444 prev_line->next = NULL;
1447 *pfile = first_line;
1452 /*********************************************************************
1454 * Function : edit_read_file
1456 * Description : Read a complete file into memory.
1457 * Handles CGI parameter parsing. If requested, also
1458 * checks the file's modification timestamp.
1461 * 1 : csp = Current client state (buffers, headers, etc...)
1462 * 2 : parameters = map of cgi parameters.
1463 * 3 : require_version = true to check "ver" parameter.
1464 * 4 : suffix = File extension, e.g. ".action".
1465 * 5 : pfile = Destination for the file. Will be set
1469 * filename : The name of the file to read, without the
1470 * path or ".action" extension.
1471 * ver : (Only if require_version is nonzero)
1472 * Timestamp of the actions file. If wrong, this
1473 * function fails with JB_ERR_MODIFIED.
1475 * Returns : JB_ERR_OK on success
1476 * JB_ERR_MEMORY on out-of-memory
1477 * JB_ERR_CGI_PARAMS if "filename" was not specified
1479 * JB_ERR_FILE if the file cannot be opened or
1481 * JB_ERR_MODIFIED if version checking was requested and
1482 * failed - the file was modified outside
1483 * of this CGI editor instance.
1485 *********************************************************************/
1486 jb_err edit_read_file(struct client_state *csp,
1487 const struct map *parameters,
1488 int require_version,
1490 struct editable_file **pfile)
1492 struct file_line * lines;
1496 const char * identifier;
1497 struct editable_file * file;
1498 unsigned version = 0;
1499 struct stat statbuf[1];
1500 char version_buf[22];
1501 int newline = NEWLINE_UNKNOWN;
1509 err = get_file_name_param(csp, parameters, "f", suffix,
1510 &filename, &identifier);
1516 if (stat(filename, statbuf) < 0)
1518 /* Error, probably file not found. */
1522 version = (unsigned) statbuf->st_mtime;
1524 if (require_version)
1526 unsigned specified_version;
1527 err = get_number_param(csp, parameters, "v", &specified_version);
1534 if (version != specified_version)
1536 return JB_ERR_MODIFIED;
1540 #if defined(AMIGA) || defined(__OS2__)
1541 if (NULL == (fp = fopen(filename,"r")))
1543 if (NULL == (fp = fopen(filename,"rt")))
1544 #endif /* def AMIGA */
1550 err = edit_read_file_lines(fp, &lines, &newline);
1560 file = (struct editable_file *) zalloc(sizeof(*file));
1564 edit_free_file_lines(lines);
1568 file->lines = lines;
1569 file->newline = newline;
1570 file->filename = filename;
1571 file->version = version;
1572 file->identifier = url_encode(identifier);
1574 if (file->identifier == NULL)
1576 edit_free_file(file);
1577 return JB_ERR_MEMORY;
1580 /* Correct file->version_str */
1581 freez(file->version_str);
1582 snprintf(version_buf, 22, "%u", file->version);
1583 version_buf[21] = '\0';
1584 file->version_str = strdup(version_buf);
1585 if (version_buf == NULL)
1587 edit_free_file(file);
1588 return JB_ERR_MEMORY;
1596 /*********************************************************************
1598 * Function : edit_read_actions_file
1600 * Description : Read a complete actions file into memory.
1601 * Handles CGI parameter parsing. If requested, also
1602 * checks the file's modification timestamp.
1604 * If this function detects an error in the categories
1605 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1606 * then it handles it by filling in the specified
1607 * response structure and returning JB_ERR_FILE.
1610 * 1 : csp = Current client state (buffers, headers, etc...)
1611 * 2 : rsp = HTTP response. Only filled in on error.
1612 * 2 : parameters = map of cgi parameters.
1613 * 3 : require_version = true to check "ver" parameter.
1614 * 4 : pfile = Destination for the file. Will be set
1618 * filename : The name of the actions file to read, without the
1619 * path or ".action" extension.
1620 * ver : (Only if require_version is nonzero)
1621 * Timestamp of the actions file. If wrong, this
1622 * function fails with JB_ERR_MODIFIED.
1624 * Returns : JB_ERR_OK on success
1625 * JB_ERR_MEMORY on out-of-memory
1626 * JB_ERR_CGI_PARAMS if "filename" was not specified
1628 * JB_ERR_FILE if the file does not contain valid data,
1629 * or if file cannot be opened or
1630 * contains no data, or if version
1631 * checking was requested and failed.
1633 *********************************************************************/
1634 jb_err edit_read_actions_file(struct client_state *csp,
1635 struct http_response *rsp,
1636 const struct map *parameters,
1637 int require_version,
1638 struct editable_file **pfile)
1641 struct editable_file *file;
1649 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1652 /* Try to handle if possible */
1653 if (err == JB_ERR_FILE)
1655 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1657 else if (err == JB_ERR_MODIFIED)
1659 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1661 if (err == JB_ERR_OK)
1664 * Signal to higher-level CGI code that there was a problem but we
1665 * handled it, they should just return JB_ERR_OK.
1672 err = edit_parse_actions_file(file);
1675 if (err == JB_ERR_PARSE)
1677 err = cgi_error_parse(csp, rsp, file);
1678 if (err == JB_ERR_OK)
1681 * Signal to higher-level CGI code that there was a problem but we
1682 * handled it, they should just return JB_ERR_OK.
1687 edit_free_file(file);
1696 /*********************************************************************
1698 * Function : get_file_name_param
1700 * Description : Get the name of the file to edit from the parameters
1701 * passed to a CGI function. This function handles
1702 * security checks such as blocking urls containing
1703 * "/" or ".", prepending the config file directory,
1704 * and adding the specified suffix.
1706 * (This is an essential security check, otherwise
1707 * users may be able to pass "../../../etc/passwd"
1708 * and overwrite the password file [linux], "prn:"
1709 * and print random data [Windows], etc...)
1711 * This function only allows filenames contining the
1712 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1713 * That's probably too restrictive but at least it's
1717 * 1 : csp = Current client state (buffers, headers, etc...)
1718 * 2 : parameters = map of cgi parameters
1719 * 3 : param_name = The name of the parameter to read
1720 * 4 : suffix = File extension, e.g. ".actions"
1721 * 5 : pfilename = destination for full filename. Caller
1722 * free()s. Set to NULL on error.
1723 * 6 : pparam = destination for partial filename,
1724 * suitable for use in another URL. Allocated as part
1725 * of the map "parameters", so don't free it.
1726 * Set to NULL if not specified.
1728 * Returns : JB_ERR_OK on success
1729 * JB_ERR_MEMORY on out-of-memory
1730 * JB_ERR_CGI_PARAMS if "filename" was not specified
1733 *********************************************************************/
1734 static jb_err get_file_name_param(struct client_state *csp,
1735 const struct map *parameters,
1736 const char *param_name,
1739 const char **pparam)
1757 param = lookup(parameters, param_name);
1760 return JB_ERR_CGI_PARAMS;
1765 len = strlen(param);
1766 if (len >= FILENAME_MAX)
1769 return JB_ERR_CGI_PARAMS;
1772 /* Check every character to see if it's legal */
1774 while ((ch = *s++) != '\0')
1776 if ( ((ch < 'A') || (ch > 'Z'))
1777 && ((ch < 'a') || (ch > 'z'))
1778 && ((ch < '0') || (ch > '9'))
1782 /* Probable hack attempt. */
1783 return JB_ERR_CGI_PARAMS;
1787 /* Append extension */
1788 name = malloc(len + strlen(suffix) + 1);
1791 return JB_ERR_MEMORY;
1793 strcpy(name, param);
1794 strcpy(name + len, suffix);
1797 fullpath = make_path(csp->config->confdir, name);
1799 if (fullpath == NULL)
1801 return JB_ERR_MEMORY;
1805 *pfilename = fullpath;
1811 /*********************************************************************
1813 * Function : get_number_param
1815 * Description : Get a non-negative integer from the parameters
1816 * passed to a CGI function.
1819 * 1 : csp = Current client state (buffers, headers, etc...)
1820 * 2 : parameters = map of cgi parameters
1821 * 3 : name = Name of CGI parameter to read
1822 * 4 : pvalue = destination for value.
1823 * Set to -1 on error.
1825 * Returns : JB_ERR_OK on success
1826 * JB_ERR_MEMORY on out-of-memory
1827 * JB_ERR_CGI_PARAMS if the parameter was not specified
1830 *********************************************************************/
1831 static jb_err get_number_param(struct client_state *csp,
1832 const struct map *parameters,
1847 param = lookup(parameters, name);
1850 return JB_ERR_CGI_PARAMS;
1853 /* We don't use atoi because I want to check this carefully... */
1856 while ((ch = *param++) != '\0')
1858 if ((ch < '0') || (ch > '9'))
1860 return JB_ERR_CGI_PARAMS;
1867 * <limits.h> defines UINT_MAX
1869 * (UINT_MAX - ch) / 10 is the largest number that
1870 * can be safely multiplied by 10 then have ch added.
1872 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1874 return JB_ERR_CGI_PARAMS;
1877 value = value * 10 + ch;
1887 /*********************************************************************
1889 * Function : get_url_spec_param
1891 * Description : Get a URL pattern from the parameters
1892 * passed to a CGI function. Removes leading/trailing
1893 * spaces and validates it.
1896 * 1 : csp = Current client state (buffers, headers, etc...)
1897 * 2 : parameters = map of cgi parameters
1898 * 3 : name = Name of CGI parameter to read
1899 * 4 : pvalue = destination for value. Will be malloc()'d.
1900 * Set to NULL on error.
1902 * Returns : JB_ERR_OK on success
1903 * JB_ERR_MEMORY on out-of-memory
1904 * JB_ERR_CGI_PARAMS if the parameter was not specified
1907 *********************************************************************/
1908 static jb_err get_url_spec_param(struct client_state *csp,
1909 const struct map *parameters,
1913 const char *orig_param;
1916 struct url_spec compiled[1];
1926 orig_param = lookup(parameters, name);
1929 return JB_ERR_CGI_PARAMS;
1932 /* Copy and trim whitespace */
1933 param = strdup(orig_param);
1936 return JB_ERR_MEMORY;
1940 /* Must be non-empty, and can't allow 1st character to be '{' */
1941 if (param[0] == '\0' || param[0] == '{')
1944 return JB_ERR_CGI_PARAMS;
1947 /* Check for embedded newlines */
1948 for (s = param; *s != '\0'; s++)
1950 if ((*s == '\r') || (*s == '\n'))
1953 return JB_ERR_CGI_PARAMS;
1957 /* Check that regex is valid */
1962 return JB_ERR_MEMORY;
1964 err = create_url_spec(compiled, s);
1969 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
1971 free_url_spec(compiled);
1973 if (param[strlen(param) - 1] == '\\')
1976 * Must protect trailing '\\' from becoming line continuation character.
1977 * Two methods: 1) If it's a domain only, add a trailing '/'.
1978 * 2) For path, add the do-nothing PCRE expression (?:) to the end
1980 if (strchr(param, '/') == NULL)
1982 err = string_append(¶m, "/");
1986 err = string_append(¶m, "(?:)");
1993 /* Check that the modified regex is valid */
1998 return JB_ERR_MEMORY;
2000 err = create_url_spec(compiled, s);
2005 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2007 free_url_spec(compiled);
2014 /*********************************************************************
2016 * Function : map_radio
2018 * Description : Map a set of radio button values. E.g. if you have
2019 * 3 radio buttons, declare them as:
2020 * <option type="radio" name="xyz" @xyz-a@>
2021 * <option type="radio" name="xyz" @xyz-b@>
2022 * <option type="radio" name="xyz" @xyz-c@>
2023 * Then map one of the @xyz-?@ variables to "checked"
2024 * and all the others to empty by calling:
2025 * map_radio(exports, "xyz", "abc", sel)
2026 * Where 'sel' is 'a', 'b', or 'c'.
2029 * 1 : exports = Exports map to modify.
2030 * 2 : optionname = name for map
2031 * 3 : values = null-terminated list of values;
2032 * 4 : value = Selected value.
2034 * CGI Parameters : None
2036 * Returns : JB_ERR_OK on success
2037 * JB_ERR_MEMORY on out-of-memory
2039 *********************************************************************/
2040 static jb_err map_radio(struct map * exports,
2041 const char * optionname,
2042 const char * values,
2054 len = strlen(optionname);
2055 buf = malloc(len + 3);
2058 return JB_ERR_MEMORY;
2061 strcpy(buf, optionname);
2066 while ((c = *values++) != '\0')
2071 if (map(exports, buf, 1, "", 1))
2074 return JB_ERR_MEMORY;
2080 if (map(exports, buf, 0, "checked", 1))
2083 return JB_ERR_MEMORY;
2090 /*********************************************************************
2092 * Function : cgi_error_modified
2094 * Description : CGI function that is called when a file is modified
2095 * outside the CGI editor.
2098 * 1 : csp = Current client state (buffers, headers, etc...)
2099 * 2 : rsp = http_response data structure for output
2100 * 3 : filename = The file that was modified.
2102 * CGI Parameters : none
2104 * Returns : JB_ERR_OK on success
2105 * JB_ERR_MEMORY on out-of-memory error.
2107 *********************************************************************/
2108 jb_err cgi_error_modified(struct client_state *csp,
2109 struct http_response *rsp,
2110 const char *filename)
2112 struct map *exports;
2119 if (NULL == (exports = default_exports(csp, NULL)))
2121 return JB_ERR_MEMORY;
2124 err = map(exports, "f", 1, html_encode(filename), 0);
2131 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2135 /*********************************************************************
2137 * Function : cgi_error_parse
2139 * Description : CGI function that is called when a file cannot
2140 * be parsed by the CGI editor.
2143 * 1 : csp = Current client state (buffers, headers, etc...)
2144 * 2 : rsp = http_response data structure for output
2145 * 3 : file = The file that was modified.
2147 * CGI Parameters : none
2149 * Returns : JB_ERR_OK on success
2150 * JB_ERR_MEMORY on out-of-memory error.
2152 *********************************************************************/
2153 jb_err cgi_error_parse(struct client_state *csp,
2154 struct http_response *rsp,
2155 struct editable_file *file)
2157 struct map *exports;
2159 struct file_line *cur_line;
2165 if (NULL == (exports = default_exports(csp, NULL)))
2167 return JB_ERR_MEMORY;
2170 err = map(exports, "f", 1, file->identifier, 1);
2171 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2173 cur_line = file->parse_error;
2176 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2177 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2185 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2189 /*********************************************************************
2191 * Function : cgi_error_file
2193 * Description : CGI function that is called when a file cannot be
2194 * opened by the CGI editor.
2197 * 1 : csp = Current client state (buffers, headers, etc...)
2198 * 2 : rsp = http_response data structure for output
2199 * 3 : filename = The file that was modified.
2201 * CGI Parameters : none
2203 * Returns : JB_ERR_OK on success
2204 * JB_ERR_MEMORY on out-of-memory error.
2206 *********************************************************************/
2207 jb_err cgi_error_file(struct client_state *csp,
2208 struct http_response *rsp,
2209 const char *filename)
2211 struct map *exports;
2218 if (NULL == (exports = default_exports(csp, NULL)))
2220 return JB_ERR_MEMORY;
2223 err = map(exports, "f", 1, html_encode(filename), 0);
2230 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2234 /*********************************************************************
2236 * Function : cgi_error_bad_param
2238 * Description : CGI function that is called if the parameters
2239 * (query string) for a CGI were wrong.
2242 * 1 : csp = Current client state (buffers, headers, etc...)
2243 * 2 : rsp = http_response data structure for output
2245 * CGI Parameters : none
2247 * Returns : JB_ERR_OK on success
2248 * JB_ERR_MEMORY on out-of-memory error.
2250 *********************************************************************/
2251 jb_err cgi_error_disabled(struct client_state *csp,
2252 struct http_response *rsp)
2254 struct map *exports;
2259 if (NULL == (exports = default_exports(csp, NULL)))
2261 return JB_ERR_MEMORY;
2264 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2268 /*********************************************************************
2270 * Function : cgi_edit_actions
2272 * Description : CGI function that allows the user to choose which
2273 * actions file to edit.
2276 * 1 : csp = Current client state (buffers, headers, etc...)
2277 * 2 : rsp = http_response data structure for output
2278 * 3 : parameters = map of cgi parameters
2280 * CGI Parameters : None
2282 * Returns : JB_ERR_OK on success
2283 * JB_ERR_MEMORY on out-of-memory error
2285 *********************************************************************/
2286 jb_err cgi_edit_actions(struct client_state *csp,
2287 struct http_response *rsp,
2288 const struct map *parameters)
2291 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2293 return cgi_error_disabled(csp, rsp);
2296 /* FIXME: Incomplete */
2297 rsp->status = strdup("302 Local Redirect from Junkbuster");
2298 if (rsp->status == NULL)
2300 return JB_ERR_MEMORY;
2302 if (enlist_unique_header(rsp->headers, "Location",
2303 CGI_PREFIX "edit-actions-list?f=ijb"))
2307 return JB_ERR_MEMORY;
2314 /*********************************************************************
2316 * Function : cgi_edit_actions_list
2318 * Description : CGI function that edits the actions list.
2319 * FIXME: This function shouldn't FATAL ever.
2320 * FIXME: This function doesn't check the retval of map()
2322 * 1 : csp = Current client state (buffers, headers, etc...)
2323 * 2 : rsp = http_response data structure for output
2324 * 3 : parameters = map of cgi parameters
2326 * CGI Parameters : filename
2328 * Returns : JB_ERR_OK on success
2329 * JB_ERR_MEMORY on out-of-memory
2330 * JB_ERR_FILE if the file cannot be opened or
2332 * JB_ERR_CGI_PARAMS if "filename" was not specified
2335 *********************************************************************/
2336 jb_err cgi_edit_actions_list(struct client_state *csp,
2337 struct http_response *rsp,
2338 const struct map *parameters)
2340 char * section_template;
2341 char * url_template;
2346 struct map * exports;
2347 struct map * section_exports;
2348 struct map * url_exports;
2349 struct editable_file * file;
2350 struct file_line * cur_line;
2351 unsigned line_number = 0;
2352 unsigned prev_section_line_number = ((unsigned) (-1));
2356 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2358 return cgi_error_disabled(csp, rsp);
2361 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2364 /* No filename specified, can't read file, or out of memory. */
2365 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2368 if (NULL == (exports = default_exports(csp, NULL)))
2370 edit_free_file(file);
2371 return JB_ERR_MEMORY;
2374 err = map(exports, "f", 1, file->identifier, 1);
2375 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2379 edit_free_file(file);
2384 /* Should do all global exports above this point */
2386 err = template_load(csp, §ion_template, "edit-actions-list-section");
2389 edit_free_file(file);
2391 if (err == JB_ERR_FILE)
2393 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2398 err = template_load(csp, &url_template, "edit-actions-list-url");
2401 free(section_template);
2402 edit_free_file(file);
2404 if (err == JB_ERR_FILE)
2406 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2411 err = template_fill(§ion_template, exports);
2415 edit_free_file(file);
2421 err = template_fill(&url_template, exports);
2424 free(section_template);
2425 edit_free_file(file);
2430 /* Find start of actions in file */
2431 cur_line = file->lines;
2433 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2435 cur_line = cur_line->next;
2439 if (NULL == (sections = strdup("")))
2441 free(section_template);
2443 edit_free_file(file);
2445 return JB_ERR_MEMORY;
2448 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2450 if (NULL == (section_exports = new_map()))
2453 free(section_template);
2455 edit_free_file(file);
2457 return JB_ERR_MEMORY;
2460 snprintf(buf, 50, "%d", line_number);
2461 err = map(section_exports, "s", 1, buf, 1);
2462 if (!err) err = map(section_exports, "actions", 1,
2463 actions_to_html(cur_line->data.action), 0);
2466 && (cur_line->next != NULL)
2467 && (cur_line->next->type == FILE_LINE_URL))
2469 /* This section contains at least one URL, don't allow delete */
2470 err = map_block_killer(section_exports, "empty-section");
2474 if (!err) err = map_block_keep(section_exports, "empty-section");
2477 if (prev_section_line_number != ((unsigned)(-1)))
2479 /* Not last section */
2480 snprintf(buf, 50, "%d", prev_section_line_number);
2481 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2482 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2487 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2489 prev_section_line_number = line_number;
2494 free(section_template);
2496 edit_free_file(file);
2498 free_map(section_exports);
2502 /* Should do all section-specific exports above this point */
2504 if (NULL == (urls = strdup("")))
2507 free(section_template);
2509 edit_free_file(file);
2511 free_map(section_exports);
2512 return JB_ERR_MEMORY;
2517 cur_line = cur_line->next;
2520 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2522 if (NULL == (url_exports = new_map()))
2526 free(section_template);
2528 edit_free_file(file);
2530 free_map(section_exports);
2531 return JB_ERR_MEMORY;
2534 snprintf(buf, 50, "%d", line_number);
2535 err = map(url_exports, "p", 1, buf, 1);
2537 snprintf(buf, 50, "%d", url_1_2);
2538 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2540 if (!err) err = map(url_exports, "url-html", 1,
2541 html_encode(cur_line->unprocessed), 0);
2542 if (!err) err = map(url_exports, "url", 1,
2543 url_encode(cur_line->unprocessed), 0);
2549 free(section_template);
2551 edit_free_file(file);
2553 free_map(section_exports);
2554 free_map(url_exports);
2558 if (NULL == (s = strdup(url_template)))
2562 free(section_template);
2564 edit_free_file(file);
2566 free_map(section_exports);
2567 free_map(url_exports);
2568 return JB_ERR_MEMORY;
2571 err = template_fill(&s, section_exports);
2572 if (!err) err = template_fill(&s, url_exports);
2573 if (!err) err = string_append(&urls, s);
2575 free_map(url_exports);
2582 free(section_template);
2584 edit_free_file(file);
2586 free_map(section_exports);
2590 url_1_2 = 3 - url_1_2;
2592 cur_line = cur_line->next;
2596 err = map(section_exports, "urls", 1, urls, 0);
2598 /* Could also do section-specific exports here, but it wouldn't be as fast */
2600 if ( (cur_line != NULL)
2601 && (cur_line->type == FILE_LINE_ACTION))
2603 /* Not last section */
2604 snprintf(buf, 50, "%d", line_number);
2605 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2606 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2611 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2617 free(section_template);
2619 edit_free_file(file);
2621 free_map(section_exports);
2625 if (NULL == (s = strdup(section_template)))
2628 free(section_template);
2630 edit_free_file(file);
2632 free_map(section_exports);
2633 return JB_ERR_MEMORY;
2636 err = template_fill(&s, section_exports);
2637 if (!err) err = string_append(§ions, s);
2640 free_map(section_exports);
2645 free(section_template);
2647 edit_free_file(file);
2653 edit_free_file(file);
2654 free(section_template);
2657 err = map(exports, "sections", 1, sections, 0);
2664 /* Could also do global exports here, but it wouldn't be as fast */
2666 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2670 /*********************************************************************
2672 * Function : cgi_edit_actions
2674 * Description : CGI function that edits the Actions list.
2677 * 1 : csp = Current client state (buffers, headers, etc...)
2678 * 2 : rsp = http_response data structure for output
2679 * 3 : parameters = map of cgi parameters
2681 * CGI Parameters : None
2683 * Returns : JB_ERR_OK on success
2684 * JB_ERR_MEMORY on out-of-memory
2685 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2686 * specified or not valid.
2688 *********************************************************************/
2689 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2690 struct http_response *rsp,
2691 const struct map *parameters)
2693 struct map * exports;
2695 struct editable_file * file;
2696 struct file_line * cur_line;
2697 unsigned line_number;
2700 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2702 return cgi_error_disabled(csp, rsp);
2705 err = get_number_param(csp, parameters, "s", §ionid);
2711 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2714 /* No filename specified, can't read file, modified, or out of memory. */
2715 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2718 cur_line = file->lines;
2720 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2722 cur_line = cur_line->next;
2725 if ( (cur_line == NULL)
2726 || (line_number != sectionid)
2728 || (cur_line->type != FILE_LINE_ACTION))
2730 /* Invalid "sectionid" parameter */
2731 edit_free_file(file);
2732 return JB_ERR_CGI_PARAMS;
2735 if (NULL == (exports = default_exports(csp, NULL)))
2737 edit_free_file(file);
2738 return JB_ERR_MEMORY;
2741 err = map(exports, "f", 1, file->identifier, 1);
2742 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2743 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2745 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2747 edit_free_file(file);
2755 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2759 /*********************************************************************
2761 * Function : cgi_edit_actions_submit
2763 * Description : CGI function that actually edits the Actions list.
2766 * 1 : csp = Current client state (buffers, headers, etc...)
2767 * 2 : rsp = http_response data structure for output
2768 * 3 : parameters = map of cgi parameters
2770 * CGI Parameters : None
2772 * Returns : JB_ERR_OK on success
2773 * JB_ERR_MEMORY on out-of-memory
2774 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2775 * specified or not valid.
2777 *********************************************************************/
2778 jb_err cgi_edit_actions_submit(struct client_state *csp,
2779 struct http_response *rsp,
2780 const struct map *parameters)
2786 struct editable_file * file;
2787 struct file_line * cur_line;
2788 unsigned line_number;
2792 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2794 return cgi_error_disabled(csp, rsp);
2797 err = get_number_param(csp, parameters, "s", §ionid);
2803 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2806 /* No filename specified, can't read file, modified, or out of memory. */
2807 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2810 cur_line = file->lines;
2812 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2814 cur_line = cur_line->next;
2817 if ( (cur_line == NULL)
2818 || (line_number != sectionid)
2820 || (cur_line->type != FILE_LINE_ACTION))
2822 /* Invalid "sectionid" parameter */
2823 edit_free_file(file);
2824 return JB_ERR_CGI_PARAMS;
2827 err = actions_from_radio(parameters, cur_line->data.action);
2831 edit_free_file(file);
2835 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2838 edit_free_file(file);
2839 return JB_ERR_MEMORY;
2842 len = strlen(actiontext);
2846 * Empty action - must special-case this.
2847 * Simply setting len to 1 is sufficient...
2852 if (NULL == (newtext = malloc(len + 2)))
2856 edit_free_file(file);
2857 return JB_ERR_MEMORY;
2859 strcpy(newtext, actiontext);
2863 newtext[len + 1] = '\0';
2865 freez(cur_line->raw);
2866 freez(cur_line->unprocessed);
2867 cur_line->unprocessed = newtext;
2869 err = edit_write_file(file);
2872 /* Error writing file */
2873 edit_free_file(file);
2877 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2878 string_append(&target, file->identifier);
2880 edit_free_file(file);
2885 return JB_ERR_MEMORY;
2888 rsp->status = strdup("302 Local Redirect from Junkbuster");
2889 if (rsp->status == NULL)
2892 return JB_ERR_MEMORY;
2894 err = enlist_unique_header(rsp->headers, "Location", target);
2901 /*********************************************************************
2903 * Function : cgi_edit_actions_url
2905 * Description : CGI function that actually edits a URL pattern in
2909 * 1 : csp = Current client state (buffers, headers, etc...)
2910 * 2 : rsp = http_response data structure for output
2911 * 3 : parameters = map of cgi parameters
2914 * filename : Identifies the file to edit
2915 * ver : File's last-modified time
2916 * section : Line number of section to edit
2917 * pattern : Line number of pattern to edit
2918 * newval : New value for pattern
2920 * Returns : JB_ERR_OK on success
2921 * JB_ERR_MEMORY on out-of-memory
2922 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2923 * specified or not valid.
2925 *********************************************************************/
2926 jb_err cgi_edit_actions_url(struct client_state *csp,
2927 struct http_response *rsp,
2928 const struct map *parameters)
2932 struct editable_file * file;
2933 struct file_line * cur_line;
2934 unsigned line_number;
2938 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2940 return cgi_error_disabled(csp, rsp);
2943 err = get_number_param(csp, parameters, "p", &patternid);
2950 return JB_ERR_CGI_PARAMS;
2953 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
2959 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2962 /* No filename specified, can't read file, modified, or out of memory. */
2964 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2968 cur_line = file->lines;
2970 while ((cur_line != NULL) && (line_number < patternid))
2972 cur_line = cur_line->next;
2976 if ( (cur_line == NULL)
2977 || (cur_line->type != FILE_LINE_URL))
2979 /* Invalid "patternid" parameter */
2981 edit_free_file(file);
2982 return JB_ERR_CGI_PARAMS;
2985 /* At this point, the line to edit is in cur_line */
2987 freez(cur_line->raw);
2988 freez(cur_line->unprocessed);
2989 cur_line->unprocessed = new_pattern;
2991 err = edit_write_file(file);
2994 /* Error writing file */
2995 edit_free_file(file);
2999 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3000 string_append(&target, file->identifier);
3002 edit_free_file(file);
3007 return JB_ERR_MEMORY;
3010 rsp->status = strdup("302 Local Redirect from Junkbuster");
3011 if (rsp->status == NULL)
3014 return JB_ERR_MEMORY;
3016 err = enlist_unique_header(rsp->headers, "Location", target);
3023 /*********************************************************************
3025 * Function : cgi_edit_actions_add_url
3027 * Description : CGI function that actually adds a URL pattern to
3031 * 1 : csp = Current client state (buffers, headers, etc...)
3032 * 2 : rsp = http_response data structure for output
3033 * 3 : parameters = map of cgi parameters
3036 * filename : Identifies the file to edit
3037 * ver : File's last-modified time
3038 * section : Line number of section to edit
3039 * newval : New pattern
3041 * Returns : JB_ERR_OK on success
3042 * JB_ERR_MEMORY on out-of-memory
3043 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3044 * specified or not valid.
3046 *********************************************************************/
3047 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3048 struct http_response *rsp,
3049 const struct map *parameters)
3053 struct file_line * new_line;
3054 struct editable_file * file;
3055 struct file_line * cur_line;
3056 unsigned line_number;
3060 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3062 return cgi_error_disabled(csp, rsp);
3065 err = get_number_param(csp, parameters, "s", §ionid);
3072 return JB_ERR_CGI_PARAMS;
3075 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3081 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3084 /* No filename specified, can't read file, modified, or out of memory. */
3086 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3090 cur_line = file->lines;
3092 while ((cur_line != NULL) && (line_number < sectionid))
3094 cur_line = cur_line->next;
3098 if ( (cur_line == NULL)
3099 || (cur_line->type != FILE_LINE_ACTION))
3101 /* Invalid "sectionid" parameter */
3103 edit_free_file(file);
3104 return JB_ERR_CGI_PARAMS;
3107 /* At this point, the section header is in cur_line - add after this. */
3109 /* Allocate the new line */
3110 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3111 if (new_line == NULL)
3114 edit_free_file(file);
3115 return JB_ERR_MEMORY;
3118 /* Fill in the data members of the new line */
3119 new_line->raw = NULL;
3120 new_line->prefix = NULL;
3121 new_line->unprocessed = new_pattern;
3122 new_line->type = FILE_LINE_URL;
3124 /* Link new_line into the list, after cur_line */
3125 new_line->next = cur_line->next;
3126 cur_line->next = new_line;
3128 /* Done making changes, now commit */
3130 err = edit_write_file(file);
3133 /* Error writing file */
3134 edit_free_file(file);
3138 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3139 string_append(&target, file->identifier);
3141 edit_free_file(file);
3146 return JB_ERR_MEMORY;
3149 rsp->status = strdup("302 Local Redirect from Junkbuster");
3150 if (rsp->status == NULL)
3153 return JB_ERR_MEMORY;
3155 err = enlist_unique_header(rsp->headers, "Location", target);
3162 /*********************************************************************
3164 * Function : cgi_edit_actions_remove_url
3166 * Description : CGI function that actually removes a URL pattern from
3170 * 1 : csp = Current client state (buffers, headers, etc...)
3171 * 2 : rsp = http_response data structure for output
3172 * 3 : parameters = map of cgi parameters
3175 * f : (filename) Identifies the file to edit
3176 * v : (version) File's last-modified time
3177 * p : (pattern) Line number of pattern to remove
3179 * Returns : JB_ERR_OK on success
3180 * JB_ERR_MEMORY on out-of-memory
3181 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3182 * specified or not valid.
3184 *********************************************************************/
3185 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3186 struct http_response *rsp,
3187 const struct map *parameters)
3190 struct editable_file * file;
3191 struct file_line * cur_line;
3192 struct file_line * prev_line;
3193 unsigned line_number;
3197 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3199 return cgi_error_disabled(csp, rsp);
3202 err = get_number_param(csp, parameters, "p", &patternid);
3208 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3211 /* No filename specified, can't read file, modified, or out of memory. */
3212 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3217 cur_line = file->lines;
3219 while ((cur_line != NULL) && (line_number < patternid))
3221 prev_line = cur_line;
3222 cur_line = cur_line->next;
3226 if ( (cur_line == NULL)
3227 || (prev_line == NULL)
3228 || (cur_line->type != FILE_LINE_URL))
3230 /* Invalid "patternid" parameter */
3231 edit_free_file(file);
3232 return JB_ERR_CGI_PARAMS;
3235 /* At this point, the line to remove is in cur_line, and the previous
3236 * one is in prev_line
3239 /* Unlink cur_line */
3240 prev_line->next = cur_line->next;
3241 cur_line->next = NULL;
3244 edit_free_file_lines(cur_line);
3246 err = edit_write_file(file);
3249 /* Error writing file */
3250 edit_free_file(file);
3254 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3255 string_append(&target, file->identifier);
3257 edit_free_file(file);
3262 return JB_ERR_MEMORY;
3265 rsp->status = strdup("302 Local Redirect from Junkbuster");
3266 if (rsp->status == NULL)
3269 return JB_ERR_MEMORY;
3271 err = enlist_unique_header(rsp->headers, "Location", target);
3278 /*********************************************************************
3280 * Function : cgi_edit_actions_section_remove
3282 * Description : CGI function that actually removes a whole section from
3283 * the actions file. The section must be empty first
3284 * (else JB_ERR_CGI_PARAMS).
3287 * 1 : csp = Current client state (buffers, headers, etc...)
3288 * 2 : rsp = http_response data structure for output
3289 * 3 : parameters = map of cgi parameters
3292 * f : (filename) Identifies the file to edit
3293 * v : (version) File's last-modified time
3294 * s : (section) Line number of section to edit
3296 * Returns : JB_ERR_OK on success
3297 * JB_ERR_MEMORY on out-of-memory
3298 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3299 * specified or not valid.
3301 *********************************************************************/
3302 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3303 struct http_response *rsp,
3304 const struct map *parameters)
3307 struct editable_file * file;
3308 struct file_line * cur_line;
3309 struct file_line * prev_line;
3310 unsigned line_number;
3314 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3316 return cgi_error_disabled(csp, rsp);
3319 err = get_number_param(csp, parameters, "s", §ionid);
3325 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3328 /* No filename specified, can't read file, modified, or out of memory. */
3329 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3333 cur_line = file->lines;
3336 while ((cur_line != NULL) && (line_number < sectionid))
3338 prev_line = cur_line;
3339 cur_line = cur_line->next;
3343 if ( (cur_line == NULL)
3344 || (cur_line->type != FILE_LINE_ACTION) )
3346 /* Invalid "sectionid" parameter */
3347 edit_free_file(file);
3348 return JB_ERR_CGI_PARAMS;
3351 if ( (cur_line->next != NULL)
3352 && (cur_line->next->type == FILE_LINE_URL) )
3354 /* Section not empty. */
3355 edit_free_file(file);
3356 return JB_ERR_CGI_PARAMS;
3359 /* At this point, the line to remove is in cur_line, and the previous
3360 * one is in prev_line
3363 /* Unlink cur_line */
3364 if (prev_line == NULL)
3366 /* Removing the first line from the file */
3367 file->lines = cur_line->next;
3371 prev_line->next = cur_line->next;
3373 cur_line->next = NULL;
3376 edit_free_file_lines(cur_line);
3378 err = edit_write_file(file);
3381 /* Error writing file */
3382 edit_free_file(file);
3386 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3387 string_append(&target, file->identifier);
3389 edit_free_file(file);
3394 return JB_ERR_MEMORY;
3397 rsp->status = strdup("302 Local Redirect from Junkbuster");
3398 if (rsp->status == NULL)
3401 return JB_ERR_MEMORY;
3403 err = enlist_unique_header(rsp->headers, "Location", target);
3410 /*********************************************************************
3412 * Function : cgi_edit_actions_section_add
3414 * Description : CGI function that adds a new empty section to
3418 * 1 : csp = Current client state (buffers, headers, etc...)
3419 * 2 : rsp = http_response data structure for output
3420 * 3 : parameters = map of cgi parameters
3423 * f : (filename) Identifies the file to edit
3424 * v : (version) File's last-modified time
3425 * s : (section) Line number of section to add after, 0 for
3428 * Returns : JB_ERR_OK on success
3429 * JB_ERR_MEMORY on out-of-memory
3430 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3431 * specified or not valid.
3433 *********************************************************************/
3434 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3435 struct http_response *rsp,
3436 const struct map *parameters)
3439 struct file_line * new_line;
3441 struct editable_file * file;
3442 struct file_line * cur_line;
3443 unsigned line_number;
3447 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3449 return cgi_error_disabled(csp, rsp);
3452 err = get_number_param(csp, parameters, "s", §ionid);
3458 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3461 /* No filename specified, can't read file, modified, or out of memory. */
3462 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3466 cur_line = file->lines;
3470 /* Add to start of file */
3471 if (cur_line != NULL)
3473 /* There's something in the file, find the line before the first
3476 while ( (cur_line->next != NULL)
3477 && (cur_line->next->type != FILE_LINE_ACTION) )
3479 cur_line = cur_line->next;
3486 /* Add after stated section. */
3487 while ((cur_line != NULL) && (line_number < sectionid))
3489 cur_line = cur_line->next;
3493 if ( (cur_line == NULL)
3494 || (cur_line->type != FILE_LINE_ACTION))
3496 /* Invalid "sectionid" parameter */
3497 edit_free_file(file);
3498 return JB_ERR_CGI_PARAMS;
3501 /* Skip through the section to find the last line in it. */
3502 while ( (cur_line->next != NULL)
3503 && (cur_line->next->type != FILE_LINE_ACTION) )
3505 cur_line = cur_line->next;
3510 /* At this point, the last line in the previous section is in cur_line
3511 * - add after this. (Or if we need to add as the first line, cur_line
3515 new_text = strdup("{}");
3516 if (NULL == new_text)
3518 edit_free_file(file);
3519 return JB_ERR_MEMORY;
3522 /* Allocate the new line */
3523 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3524 if (new_line == NULL)
3527 edit_free_file(file);
3528 return JB_ERR_MEMORY;
3531 /* Fill in the data members of the new line */
3532 new_line->raw = NULL;
3533 new_line->prefix = NULL;
3534 new_line->unprocessed = new_text;
3535 new_line->type = FILE_LINE_ACTION;
3537 if (cur_line != NULL)
3539 /* Link new_line into the list, after cur_line */
3540 new_line->next = cur_line->next;
3541 cur_line->next = new_line;
3545 /* Link new_line into the list, as first line */
3546 new_line->next = file->lines;
3547 file->lines = new_line;
3550 /* Done making changes, now commit */
3552 err = edit_write_file(file);
3555 /* Error writing file */
3556 edit_free_file(file);
3560 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3561 string_append(&target, file->identifier);
3563 edit_free_file(file);
3568 return JB_ERR_MEMORY;
3571 rsp->status = strdup("302 Local Redirect from Junkbuster");
3572 if (rsp->status == NULL)
3575 return JB_ERR_MEMORY;
3577 err = enlist_unique_header(rsp->headers, "Location", target);
3584 /*********************************************************************
3586 * Function : cgi_edit_actions_section_swap
3588 * Description : CGI function that swaps the order of two sections
3589 * in the actions file. Note that this CGI can actually
3590 * swap any two arbitrary sections, but the GUI interface
3591 * currently only allows consecutive sections to be
3595 * 1 : csp = Current client state (buffers, headers, etc...)
3596 * 2 : rsp = http_response data structure for output
3597 * 3 : parameters = map of cgi parameters
3600 * f : (filename) Identifies the file to edit
3601 * v : (version) File's last-modified time
3602 * s1 : (section1) Line number of first section to swap
3603 * s2 : (section2) Line number of second section to swap
3605 * Returns : JB_ERR_OK on success
3606 * JB_ERR_MEMORY on out-of-memory
3607 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3608 * specified or not valid.
3610 *********************************************************************/
3611 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3612 struct http_response *rsp,
3613 const struct map *parameters)
3617 struct editable_file * file;
3618 struct file_line * cur_line;
3619 struct file_line * prev_line;
3620 struct file_line * line_before_section1;
3621 struct file_line * line_start_section1;
3622 struct file_line * line_end_section1;
3623 struct file_line * line_after_section1;
3624 struct file_line * line_before_section2;
3625 struct file_line * line_start_section2;
3626 struct file_line * line_end_section2;
3627 struct file_line * line_after_section2;
3628 unsigned line_number;
3632 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3634 return cgi_error_disabled(csp, rsp);
3637 err = get_number_param(csp, parameters, "s1", §ion1);
3638 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3644 if (section1 > section2)
3646 unsigned temp = section2;
3647 section2 = section1;
3651 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3654 /* No filename specified, can't read file, modified, or out of memory. */
3655 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3658 /* Start at the beginning... */
3660 cur_line = file->lines;
3663 /* ... find section1 ... */
3664 while ((cur_line != NULL) && (line_number < section1))
3666 prev_line = cur_line;
3667 cur_line = cur_line->next;
3671 if ( (cur_line == NULL)
3672 || (cur_line->type != FILE_LINE_ACTION) )
3674 /* Invalid "section1" parameter */
3675 edit_free_file(file);
3676 return JB_ERR_CGI_PARAMS;
3679 /* If no-op, we've validated params and can skip the rest. */
3680 if (section1 != section2)
3682 /* ... find the end of section1 ... */
3683 line_before_section1 = prev_line;
3684 line_start_section1 = cur_line;
3687 prev_line = cur_line;
3688 cur_line = cur_line->next;
3691 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3692 line_end_section1 = prev_line;
3693 line_after_section1 = cur_line;
3695 /* ... find section2 ... */
3696 while ((cur_line != NULL) && (line_number < section2))
3698 prev_line = cur_line;
3699 cur_line = cur_line->next;
3703 if ( (cur_line == NULL)
3704 || (cur_line->type != FILE_LINE_ACTION) )
3706 /* Invalid "section2" parameter */
3707 edit_free_file(file);
3708 return JB_ERR_CGI_PARAMS;
3711 /* ... find the end of section2 ... */
3712 line_before_section2 = prev_line;
3713 line_start_section2 = cur_line;
3716 prev_line = cur_line;
3717 cur_line = cur_line->next;
3720 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3721 line_end_section2 = prev_line;
3722 line_after_section2 = cur_line;
3724 /* Now have all the pointers we need. Do the swap. */
3726 /* Change the pointer to section1 to point to section2 instead */
3727 if (line_before_section1 == NULL)
3729 file->lines = line_start_section2;
3733 line_before_section1->next = line_start_section2;
3736 if (line_before_section2 == line_end_section1)
3738 /* Consecutive sections */
3739 line_end_section2->next = line_start_section1;
3743 line_end_section2->next = line_after_section1;
3744 line_before_section2->next = line_start_section1;
3747 /* Set the pointer from the end of section1 to the rest of the file */
3748 line_end_section1->next = line_after_section2;
3750 err = edit_write_file(file);
3753 /* Error writing file */
3754 edit_free_file(file);
3757 } /* END if (section1 != section2) */
3759 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3760 string_append(&target, file->identifier);
3762 edit_free_file(file);
3767 return JB_ERR_MEMORY;
3770 rsp->status = strdup("302 Local Redirect from Junkbuster");
3771 if (rsp->status == NULL)
3774 return JB_ERR_MEMORY;
3776 err = enlist_unique_header(rsp->headers, "Location", target);
3783 /*********************************************************************
3785 * Function : cgi_toggle
3787 * Description : CGI function that adds a new empty section to
3791 * 1 : csp = Current client state (buffers, headers, etc...)
3792 * 2 : rsp = http_response data structure for output
3793 * 3 : parameters = map of cgi parameters
3796 * set : If present, how to change toggle setting:
3797 * "enable", "disable", "toggle", or none (default).
3798 * mini : If present, use mini reply template.
3800 * Returns : JB_ERR_OK on success
3801 * JB_ERR_MEMORY on out-of-memory
3803 *********************************************************************/
3804 jb_err cgi_toggle(struct client_state *csp,
3805 struct http_response *rsp,
3806 const struct map *parameters)
3808 struct map *exports;
3810 const char *template_name;
3817 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3819 return cgi_error_disabled(csp, rsp);
3822 if (NULL == (exports = default_exports(csp, "toggle")))
3824 return JB_ERR_MEMORY;
3827 mode = *(lookup(parameters, "set"));
3834 else if (mode == 'd')
3839 else if (mode == 't')
3842 g_bToggleIJB = !g_bToggleIJB;
3845 err = map_conditional(exports, "enabled", g_bToggleIJB);
3852 template_name = (*(lookup(parameters, "mini"))
3856 return template_fill_for_cgi(csp, template_name, exports, rsp);
3860 /*********************************************************************
3862 * Function : actions_to_radio
3864 * Description : Converts a actionsfile entry into settings for
3865 * radio buttons and edit boxes on a HTML form.
3868 * 1 : exports = List of substitutions to add to.
3869 * 2 : action = Action to read
3871 * Returns : JB_ERR_OK on success
3872 * JB_ERR_MEMORY on out-of-memory
3874 *********************************************************************/
3875 static jb_err actions_to_radio(struct map * exports,
3876 const struct action_spec *action)
3878 unsigned mask = action->mask;
3879 unsigned add = action->add;
3887 mask = action->mask;
3890 /* sanity - prevents "-feature +feature" */
3894 #define DEFINE_ACTION_BOOL(name, bit) \
3895 if (!(mask & bit)) \
3897 current_mode = 'n'; \
3899 else if (add & bit) \
3901 current_mode = 'y'; \
3905 current_mode = 'x'; \
3907 if (map_radio(exports, name, "ynx", current_mode)) \
3909 return JB_ERR_MEMORY; \
3912 #define DEFINE_ACTION_STRING(name, bit, index) \
3913 DEFINE_ACTION_BOOL(name, bit); \
3916 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
3919 checked = !strcmp(action->string[index], value); \
3923 checked = is_default; \
3925 mapped_param |= checked; \
3926 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
3928 return JB_ERR_MEMORY; \
3931 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
3932 if (map(exports, name "-param-custom", 1, \
3933 ((!mapped_param) ? "checked" : ""), 1)) \
3935 return JB_ERR_MEMORY; \
3937 if (map(exports, name "-param", 1, \
3938 (((add & bit) && !mapped_param) ? \
3939 action->string[index] : default_val), 1)) \
3941 return JB_ERR_MEMORY; \
3944 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
3945 if (map(exports, name "-param", 1, \
3946 ((add & bit) ? action->string[index] : default_val), 1)) \
3948 return JB_ERR_MEMORY; \
3951 #define DEFINE_ACTION_MULTI(name, index) \
3952 if (action->multi_add[index]->first) \
3954 current_mode = 'y'; \
3956 else if (action->multi_remove_all[index]) \
3958 current_mode = 'n'; \
3960 else if (action->multi_remove[index]->first) \
3962 current_mode = 'y'; \
3966 current_mode = 'x'; \
3968 if (map_radio(exports, name, "ynx", current_mode)) \
3970 return JB_ERR_MEMORY; \
3973 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
3975 #include "actionlist.h"
3977 #undef DEFINE_ACTION_MULTI
3978 #undef DEFINE_ACTION_STRING
3979 #undef DEFINE_ACTION_BOOL
3980 #undef DEFINE_ACTION_ALIAS
3981 #undef DEFINE_CGI_PARAM_CUSTOM
3982 #undef DEFINE_CGI_PARAM_RADIO
3983 #undef DEFINE_CGI_PARAM_NO_RADIO
3989 /*********************************************************************
3991 * Function : javascriptify
3993 * Description : Converts a string into a form JavaScript will like.
3995 * Netscape 4's JavaScript sucks - it doesn't use
3996 * "id" parameters, so you have to set the "name"
3997 * used to submit a form element to something JavaScript
3998 * will like. (Or access the elements by index in an
3999 * array. That array contains >60 elements and will
4000 * be changed whenever we add a new action to the
4001 * editor, so I'm NOT going to use indexes that have
4002 * to be figured out by hand.)
4004 * Currently the only thing we have to worry about
4005 * is "-" ==> "_" conversion.
4007 * This is a length-preserving operation so it is
4008 * carried out in-place, no memory is allocated
4012 * 1 : identifier = String to make JavaScript-friendly.
4016 *********************************************************************/
4017 static void javascriptify(char * identifier)
4019 char * p = identifier;
4020 while (NULL != (p = strchr(p, '-')))
4027 /*********************************************************************
4029 * Function : actions_from_radio
4031 * Description : Converts a map of parameters passed to a CGI function
4032 * into an actionsfile entry.
4035 * 1 : parameters = parameters to the CGI call
4036 * 2 : action = Action to change. Must be valid before
4037 * the call, actions not specified will be
4040 * Returns : JB_ERR_OK on success
4041 * JB_ERR_MEMORY on out-of-memory
4043 *********************************************************************/
4044 static jb_err actions_from_radio(const struct map * parameters,
4045 struct action_spec *action)
4047 static int first_time = 1;
4051 const char * js_name;
4056 /* Statics are generally a potential race condition,
4057 * but in this case we're safe and don't need semaphores.
4058 * Be careful if you modify this function.
4062 #define JAVASCRIPTIFY(dest_var, string) \
4064 static char js_name_arr[] = string; \
4067 javascriptify(js_name_arr); \
4069 dest_var = js_name_arr; \
4072 #define DEFINE_ACTION_BOOL(name, bit) \
4073 JAVASCRIPTIFY(js_name, name); \
4074 param = lookup(parameters, js_name); \
4075 ch = ijb_toupper(param[0]); \
4078 action->add |= bit; \
4079 action->mask |= bit; \
4081 else if (ch == 'N') \
4083 action->add &= ~bit; \
4084 action->mask &= ~bit; \
4086 else if (ch == 'X') \
4088 action->add &= ~bit; \
4089 action->mask |= bit; \
4092 #define DEFINE_ACTION_STRING(name, bit, index) \
4093 JAVASCRIPTIFY(js_name, name); \
4094 param = lookup(parameters, js_name); \
4095 ch = ijb_toupper(param[0]); \
4098 JAVASCRIPTIFY(js_name, name "-mode"); \
4099 param = lookup(parameters, js_name); \
4100 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4102 JAVASCRIPTIFY(js_name, name "-param"); \
4103 param = lookup(parameters, js_name); \
4105 if (*param != '\0') \
4107 if (NULL == (param_dup = strdup(param))) \
4109 return JB_ERR_MEMORY; \
4111 freez(action->string[index]); \
4112 action->add |= bit; \
4113 action->mask |= bit; \
4114 action->string[index] = param_dup; \
4117 else if (ch == 'N') \
4119 if (action->add & bit) \
4121 freez(action->string[index]); \
4123 action->add &= ~bit; \
4124 action->mask &= ~bit; \
4126 else if (ch == 'X') \
4128 if (action->add & bit) \
4130 freez(action->string[index]); \
4132 action->add &= ~bit; \
4133 action->mask |= bit; \
4136 #define DEFINE_ACTION_MULTI(name, index) \
4137 JAVASCRIPTIFY(js_name, name); \
4138 param = lookup(parameters, js_name); \
4139 ch = ijb_toupper((int)param[0]); \
4144 else if (ch == 'N') \
4146 list_remove_all(action->multi_add[index]); \
4147 list_remove_all(action->multi_remove[index]); \
4148 action->multi_remove_all[index] = 1; \
4150 else if (ch == 'X') \
4152 list_remove_all(action->multi_add[index]); \
4153 list_remove_all(action->multi_remove[index]); \
4154 action->multi_remove_all[index] = 0; \
4157 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4159 #include "actionlist.h"
4161 #undef DEFINE_ACTION_MULTI
4162 #undef DEFINE_ACTION_STRING
4163 #undef DEFINE_ACTION_BOOL
4164 #undef DEFINE_ACTION_ALIAS
4165 #undef JAVASCRIPTIFY
4173 #endif /* def FEATURE_CGI_EDIT_ACTIONS */