1 const char cgiedit_rcs[] = "$Id: cgiedit.c,v 1.15 2002/03/06 22:54:35 jongfoster Exp $";
2 /*********************************************************************
4 * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $
6 * Purpose : CGI-based actionsfile editor.
8 * Functions declared include: cgi_edit_*
10 * NOTE: The CGIs in this file use parameter names
11 * such as "f" and "s" which are really *BAD* choices.
12 * However, I'm trying to save bytes in the
13 * edit-actions-list HTML page - the standard actions
14 * file generated a 550kbyte page, which is ridiculous.
16 * Stick to the short names in this file for consistency.
18 * Copyright : Written by and Copyright (C) 2001 the SourceForge
19 * IJBSWA team. http://ijbswa.sourceforge.net
21 * Based on the Internet Junkbuster originally written
22 * by and Copyright (C) 1997 Anonymous Coders and
23 * Junkbusters Corporation. http://www.junkbusters.com
25 * This program is free software; you can redistribute it
26 * and/or modify it under the terms of the GNU General
27 * Public License as published by the Free Software
28 * Foundation; either version 2 of the License, or (at
29 * your option) any later version.
31 * This program is distributed in the hope that it will
32 * be useful, but WITHOUT ANY WARRANTY; without even the
33 * implied warranty of MERCHANTABILITY or FITNESS FOR A
34 * PARTICULAR PURPOSE. See the GNU General Public
35 * License for more details.
37 * The GNU General Public License should be included with
38 * this file. If not, you can view it at
39 * http://www.gnu.org/copyleft/gpl.html
40 * or write to the Free Software Foundation, Inc., 59
41 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 * Revision 1.15 2002/03/06 22:54:35 jongfoster
46 * Automated function-comment nitpicking.
48 * Revision 1.14 2002/03/05 00:24:51 jongfoster
49 * Patch to always edit the current actions file.
51 * Revision 1.13 2002/03/04 02:07:59 david__schmidt
52 * Enable web editing of actions file on OS/2 (it had been broken all this time!)
54 * Revision 1.12 2002/03/03 09:18:03 joergs
55 * Made jumbjuster work on AmigaOS again.
57 * Revision 1.11 2002/01/23 01:03:31 jongfoster
58 * Fixing gcc [CygWin] compiler warnings
60 * Revision 1.10 2002/01/23 00:22:59 jongfoster
61 * Adding new function cgi_edit_actions_section_swap(), to reorder
64 * Adding get_url_spec_param() to get a validated URL pattern.
66 * Moving edit_read_line() out of this file and into loaders.c.
68 * Adding missing html_encode() to many CGI functions.
70 * Moving the functions that #include actionlist.h to the end of the file,
71 * because the Visual C++ 97 debugger gets extremely confused if you try
72 * to debug any code that comes after them in the file.
74 * Major optimizations in cgi_edit_actions_list() to reduce the size of
75 * the generated HTML (down 40% from 550k to 304k), with major side-effects
76 * throughout the editor and templates. In particular, the length of the
77 * URLs throughout the editor has been drastically reduced, by cutting
78 * paramater names down to 1 character and CGI names down to 3-4
79 * characters, by removing all non-essential CGI paramaters even at the
80 * expense of having to re-read the actions file for the most trivial
81 * page, and by using relative rather than absolute URLs. This means
82 * that this (typical example):
84 * <a href="http://ijbswa.sourceforge.net/config/edit-actions-url-form?
85 * filename=ijb&ver=1011487572&section=12&pattern=13
86 * &oldval=www.oesterhelt.org%2Fdeanimate-demo">
90 * <a href="eau?f=ijb&v=1011487572&p=13">
92 * Revision 1.9 2002/01/17 20:56:22 jongfoster
93 * Replacing hard references to the URL of the config interface
94 * with #defines from project.h
96 * Revision 1.8 2001/11/30 23:35:51 jongfoster
97 * Renaming actionsfile to ijb.action
99 * Revision 1.7 2001/11/13 00:28:24 jongfoster
100 * - Renaming parameters from edit-actions-for-url so that they only
101 * contain legal JavaScript characters. If we wanted to write
102 * JavaScript that worked with Netscape 4, this is nessacery.
103 * (Note that at the moment the JavaScript doesn't actually work
104 * with Netscape 4, but now this is purely a template issue, not
105 * one affecting code).
106 * - Adding new CGIs for use by non-JavaScript browsers:
107 * edit-actions-url-form
108 * edit-actions-add-url-form
109 * edit-actions-remove-url-form
112 * Revision 1.6 2001/10/29 03:48:09 david__schmidt
113 * OS/2 native needed a snprintf() routine. Added one to miscutil, brackedted
114 * by and __OS2__ ifdef.
116 * Revision 1.5 2001/10/25 03:40:48 david__schmidt
117 * Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
118 * threads to call select() simultaneously. So, it's time to do a real, live,
119 * native OS/2 port. See defines for __EMX__ (the porting layer) vs. __OS2__
120 * (native). Both versions will work, but using __OS2__ offers multi-threading.
122 * Revision 1.4 2001/10/23 21:48:19 jongfoster
123 * Cleaning up error handling in CGI functions - they now send back
124 * a HTML error page and should never cause a FATAL error. (Fixes one
125 * potential source of "denial of service" attacks).
127 * CGI actions file editor that works and is actually useful.
129 * Ability to toggle JunkBuster remotely using a CGI call.
131 * You can turn off both the above features in the main configuration
132 * file, e.g. if you are running a multi-user proxy.
134 * Revision 1.3 2001/10/14 22:12:49 jongfoster
135 * New version of CGI-based actionsfile editor.
136 * Major changes, including:
137 * - Completely new file parser and file output routines
138 * - edit-actions CGI renamed edit-actions-for-url
139 * - All CGIs now need a filename parameter, except for...
140 * - New CGI edit-actions which doesn't need a filename,
141 * to allow you to start the editor up.
142 * - edit-actions-submit now works, and now automatically
143 * redirects you back to the main edit-actions-list handler.
145 * Revision 1.2 2001/09/16 17:05:14 jongfoster
146 * Removing unused #include showarg.h
148 * Revision 1.1 2001/09/16 15:47:37 jongfoster
149 * First version of CGI-based edit interface. This is very much a
150 * work-in-progress, and you can't actually use it to edit anything
151 * yet. You must #define FEATURE_CGI_EDIT_ACTIONS for these changes
152 * to have any effect.
155 **********************************************************************/
161 * FIXME: Following includes copied from cgi.c - which are actually needed?
166 #include <sys/types.h>
171 #include <sys/stat.h>
174 #define snprintf _snprintf
175 #endif /* def _WIN32 */
180 #include "cgisimple.h"
184 #include "miscutil.h"
188 /* loadcfg.h is for g_bToggleIJB only */
189 #include "urlmatch.h"
191 const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION;
194 #ifdef FEATURE_CGI_EDIT_ACTIONS
198 struct file_line * next;
206 struct action_spec action[1];
215 /* Add more data types here... e.g.
218 struct url_spec url[1];
222 struct action_spec action[1];
231 #define FILE_LINE_UNPROCESSED 1
232 #define FILE_LINE_BLANK 2
233 #define FILE_LINE_ALIAS_HEADER 3
234 #define FILE_LINE_ALIAS_ENTRY 4
235 #define FILE_LINE_ACTION 5
236 #define FILE_LINE_URL 6
237 #define FILE_LINE_SETTINGS_HEADER 7
238 #define FILE_LINE_SETTINGS_ENTRY 8
239 #define FILE_LINE_DESCRIPTION_HEADER 9
240 #define FILE_LINE_DESCRIPTION_ENTRY 10
245 struct file_line * lines;
246 const char * filename; /* Full pathname - e.g. "/etc/junkbuster/wibble.action" */
247 const char * identifier; /* Filename stub - e.g. "wibble". Use for CGI param. */
248 /* Pre-encoded with url_encode() for ease of use. */
249 const char * version_str; /* Last modification time, as a string. For CGI param */
250 /* Can be used in URL without using url_param(). */
251 unsigned version; /* Last modification time - prevents chaos with
252 * the browser's "back" button. Note that this is a
253 * time_t cast to an unsigned. When comparing, always
254 * cast the time_t to an unsigned, and *NOT* vice-versa.
255 * This may lose the top few bits, but they're not
256 * significant anyway.
258 int newline; /* Newline convention - one of the NEWLINE_xxx constants.
259 * Note that changing this after the file has been
260 * read in will cause a mess.
262 struct file_line * parse_error; /* On parse error, this is the offending line. */
263 const char * parse_error_text; /* On parse error, this is the problem.
264 * (Statically allocated) */
267 /* FIXME: Following non-static functions should be prototyped in .h or made static */
269 /* Functions to read and write arbitrary config files */
270 jb_err edit_read_file(struct client_state *csp,
271 const struct map *parameters,
274 struct editable_file **pfile);
275 jb_err edit_write_file(struct editable_file * file);
276 void edit_free_file(struct editable_file * file);
278 /* Functions to read and write actions files */
279 jb_err edit_parse_actions_file(struct editable_file * file);
280 jb_err edit_read_actions_file(struct client_state *csp,
281 struct http_response *rsp,
282 const struct map *parameters,
284 struct editable_file **pfile);
287 jb_err cgi_error_modified(struct client_state *csp,
288 struct http_response *rsp,
289 const char *filename);
290 jb_err cgi_error_parse(struct client_state *csp,
291 struct http_response *rsp,
292 struct editable_file *file);
293 jb_err cgi_error_file(struct client_state *csp,
294 struct http_response *rsp,
295 const char *filename);
296 jb_err cgi_error_disabled(struct client_state *csp,
297 struct http_response *rsp);
299 /* Internal arbitrary config file support functions */
300 static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline);
301 static void edit_free_file_lines(struct file_line * first_line);
303 /* Internal actions file support functions */
304 static int match_actions_file_header_line(const char * line, const char * name);
305 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue);
307 /* Internal parameter parsing functions */
308 static jb_err get_file_name_param(struct client_state *csp,
309 const struct map *parameters,
310 const char *param_name,
313 const char **pparam);
314 static jb_err get_number_param(struct client_state *csp,
315 const struct map *parameters,
318 static jb_err get_url_spec_param(struct client_state *csp,
319 const struct map *parameters,
323 /* Internal actionsfile <==> HTML conversion functions */
324 static jb_err map_radio(struct map * exports,
325 const char * optionname,
328 static jb_err actions_to_radio(struct map * exports,
329 const struct action_spec *action);
330 static jb_err actions_from_radio(const struct map * parameters,
331 struct action_spec *action);
334 static jb_err map_copy_parameter_html(struct map *out,
335 const struct map *in,
337 #if 0 /* unused function */
338 static jb_err map_copy_parameter_url(struct map *out,
339 const struct map *in,
341 #endif /* unused function */
343 /*********************************************************************
345 * Function : map_copy_parameter_html
347 * Description : Copy a CGI parameter from one map to another, HTML
351 * 1 : out = target map
352 * 2 : in = source map
353 * 3 : name = name of cgi parameter to copy
355 * Returns : JB_ERR_OK on success
356 * JB_ERR_MEMORY on out-of-memory
357 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
360 *********************************************************************/
361 static jb_err map_copy_parameter_html(struct map *out,
362 const struct map *in,
372 value = lookup(in, name);
373 err = map(out, name, 1, html_encode(value), 0);
380 else if (*value == '\0')
382 return JB_ERR_CGI_PARAMS;
391 #if 0 /* unused function */
392 /*********************************************************************
394 * Function : map_copy_parameter_html
396 * Description : Copy a CGI parameter from one map to another, URL
400 * 1 : out = target map
401 * 2 : in = source map
402 * 3 : name = name of cgi parameter to copy
404 * Returns : JB_ERR_OK on success
405 * JB_ERR_MEMORY on out-of-memory
406 * JB_ERR_CGI_PARAMS if the parameter doesn't exist
409 *********************************************************************/
410 static jb_err map_copy_parameter_url(struct map *out,
411 const struct map *in,
421 value = lookup(in, name);
422 err = map(out, name, 1, url_encode(value), 0);
429 else if (*value == '\0')
431 return JB_ERR_CGI_PARAMS;
438 #endif /* 0 - unused function */
440 /*********************************************************************
442 * Function : cgi_edit_actions_url_form
444 * Description : CGI function that displays a form for
448 * 1 : csp = Current client state (buffers, headers, etc...)
449 * 2 : rsp = http_response data structure for output
450 * 3 : parameters = map of cgi parameters
453 * f : (filename) Identifies the file to edit
454 * v : (version) File's last-modified time
455 * p : (pattern) Line number of pattern to edit
457 * Returns : JB_ERR_OK on success
458 * JB_ERR_MEMORY on out-of-memory
459 * JB_ERR_CGI_PARAMS if the CGI parameters are not
460 * specified or not valid.
462 *********************************************************************/
463 jb_err cgi_edit_actions_url_form(struct client_state *csp,
464 struct http_response *rsp,
465 const struct map *parameters)
467 struct map * exports;
469 struct editable_file * file;
470 struct file_line * cur_line;
471 unsigned line_number;
478 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
480 return cgi_error_disabled(csp, rsp);
483 err = get_number_param(csp, parameters, "p", &patternid);
489 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
492 /* No filename specified, can't read file, modified, or out of memory. */
493 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
496 cur_line = file->lines;
498 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
500 cur_line = cur_line->next;
503 if ( (cur_line == NULL)
504 || (line_number != patternid)
506 || (cur_line->type != FILE_LINE_URL))
508 /* Invalid "patternid" parameter */
509 edit_free_file(file);
510 return JB_ERR_CGI_PARAMS;
513 if (NULL == (exports = default_exports(csp, NULL)))
515 edit_free_file(file);
516 return JB_ERR_MEMORY;
519 err = map(exports, "f", 1, file->identifier, 1);
520 if (!err) err = map(exports, "v", 1, file->version_str, 1);
521 if (!err) err = map(exports, "p", 1, url_encode(lookup(parameters, "p")), 0);
522 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
524 edit_free_file(file);
532 return template_fill_for_cgi(csp, "edit-actions-url-form", exports, rsp);
536 /*********************************************************************
538 * Function : cgi_edit_actions_add_url_form
540 * Description : CGI function that displays a form for
544 * 1 : csp = Current client state (buffers, headers, etc...)
545 * 2 : rsp = http_response data structure for output
546 * 3 : parameters = map of cgi parameters
549 * f : (filename) Identifies the file to edit
550 * v : (version) File's last-modified time
551 * s : (section) Line number of section to edit
553 * Returns : JB_ERR_OK on success
554 * JB_ERR_MEMORY on out-of-memory
555 * JB_ERR_CGI_PARAMS if the CGI parameters are not
556 * specified or not valid.
558 *********************************************************************/
559 jb_err cgi_edit_actions_add_url_form(struct client_state *csp,
560 struct http_response *rsp,
561 const struct map *parameters)
570 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
572 return cgi_error_disabled(csp, rsp);
575 if (NULL == (exports = default_exports(csp, NULL)))
577 return JB_ERR_MEMORY;
580 err = map_copy_parameter_html(exports, parameters, "f");
581 if (!err) err = map_copy_parameter_html(exports, parameters, "v");
582 if (!err) err = map_copy_parameter_html(exports, parameters, "s");
590 return template_fill_for_cgi(csp, "edit-actions-add-url-form", exports, rsp);
594 /*********************************************************************
596 * Function : cgi_edit_actions_remove_url_form
598 * Description : CGI function that displays a form for
602 * 1 : csp = Current client state (buffers, headers, etc...)
603 * 2 : rsp = http_response data structure for output
604 * 3 : parameters = map of cgi parameters
607 * f : (filename) Identifies the file to edit
608 * v : (version) File's last-modified time
609 * p : (pattern) Line number of pattern to edit
611 * Returns : JB_ERR_OK on success
612 * JB_ERR_MEMORY on out-of-memory
613 * JB_ERR_CGI_PARAMS if the CGI parameters are not
614 * specified or not valid.
616 *********************************************************************/
617 jb_err cgi_edit_actions_remove_url_form(struct client_state *csp,
618 struct http_response *rsp,
619 const struct map *parameters)
621 struct map * exports;
623 struct editable_file * file;
624 struct file_line * cur_line;
625 unsigned line_number;
632 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
634 return cgi_error_disabled(csp, rsp);
637 err = get_number_param(csp, parameters, "p", &patternid);
643 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
646 /* No filename specified, can't read file, modified, or out of memory. */
647 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
650 cur_line = file->lines;
652 for (line_number = 1; (cur_line != NULL) && (line_number < patternid); line_number++)
654 cur_line = cur_line->next;
657 if ( (cur_line == NULL)
658 || (line_number != patternid)
660 || (cur_line->type != FILE_LINE_URL))
662 /* Invalid "patternid" parameter */
663 edit_free_file(file);
664 return JB_ERR_CGI_PARAMS;
667 if (NULL == (exports = default_exports(csp, NULL)))
669 edit_free_file(file);
670 return JB_ERR_MEMORY;
673 err = map(exports, "f", 1, file->identifier, 1);
674 if (!err) err = map(exports, "v", 1, file->version_str, 1);
675 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
676 if (!err) err = map(exports, "u", 1, html_encode(cur_line->unprocessed), 0);
678 edit_free_file(file);
686 return template_fill_for_cgi(csp, "edit-actions-remove-url-form", exports, rsp);
690 /*********************************************************************
692 * Function : edit_write_file
694 * Description : Write a complete file to disk.
697 * 1 : filename = File to write to.
698 * 2 : file = Data structure to write.
700 * Returns : JB_ERR_OK on success
701 * JB_ERR_FILE on error writing to file.
702 * JB_ERR_MEMORY on out of memory
704 *********************************************************************/
705 jb_err edit_write_file(struct editable_file * file)
708 struct file_line * cur_line;
709 struct stat statbuf[1];
710 char version_buf[22]; /* 22 = ceil(log10(2^64)) + 2 = max number of
711 digits in time_t, assuming this is a 64-bit
712 machine, plus null terminator, plus one
716 assert(file->filename);
718 #if defined(AMIGA) || defined(__OS2__)
719 if (NULL == (fp = fopen(file->filename, "w")))
721 if (NULL == (fp = fopen(file->filename, "wt")))
722 #endif /* def AMIGA */
727 cur_line = file->lines;
728 while (cur_line != NULL)
732 if (fputs(cur_line->raw, fp) < 0)
740 if (cur_line->prefix)
742 if (fputs(cur_line->prefix, fp) < 0)
748 if (cur_line->unprocessed)
750 /* This should be a single line - sanity check. */
751 assert(NULL == strchr(cur_line->unprocessed, '\r'));
752 assert(NULL == strchr(cur_line->unprocessed, '\n'));
754 if (NULL != strchr(cur_line->unprocessed, '#'))
756 /* Must quote '#' characters */
763 /* Count number of # characters, so we know length of output string */
764 src = cur_line->unprocessed;
765 while (NULL != (src = strchr(src, '#')))
772 /* Allocate new memory for string */
773 len = strlen(cur_line->unprocessed);
774 if (NULL == (str = malloc((size_t) len + 1 + numhash)))
776 /* Uh oh, just trashed file! */
778 return JB_ERR_MEMORY;
781 /* Loop through string from end */
782 src = cur_line->unprocessed + len;
783 dest = str + len + numhash;
784 for ( ; len >= 0; len--)
786 if ((*dest-- = *src--) == '#')
790 assert(numhash >= 0);
793 assert(numhash == 0);
794 assert(src + 1 == cur_line->unprocessed);
795 assert(dest + 1 == str);
797 if (fputs(str, fp) < 0)
808 /* Can write without quoting '#' characters. */
809 if (fputs(cur_line->unprocessed, fp) < 0)
815 if (fputs(NEWLINE(file->newline), fp) < 0)
823 /* FIXME: Write data from file->data->whatever */
827 cur_line = cur_line->next;
833 /* Update the version stamp in the file structure, since we just
834 * wrote to the file & changed it's date.
836 if (stat(file->filename, statbuf) < 0)
838 /* Error, probably file not found. */
841 file->version = (unsigned)statbuf->st_mtime;
843 /* Correct file->version_str */
844 freez(file->version_str);
845 snprintf(version_buf, 22, "%u", file->version);
846 version_buf[21] = '\0';
847 file->version_str = strdup(version_buf);
848 if (version_buf == NULL)
850 return JB_ERR_MEMORY;
857 /*********************************************************************
859 * Function : edit_free_file
861 * Description : Free a complete file in memory.
864 * 1 : file = Data structure to free.
868 *********************************************************************/
869 void edit_free_file(struct editable_file * file)
873 /* Silently ignore NULL pointer */
877 edit_free_file_lines(file->lines);
878 freez(file->filename);
879 freez(file->identifier);
880 freez(file->version_str);
882 file->parse_error_text = NULL; /* Statically allocated */
883 file->parse_error = NULL;
889 /*********************************************************************
891 * Function : edit_free_file
893 * Description : Free an entire linked list of file lines.
896 * 1 : first_line = Data structure to free.
900 *********************************************************************/
901 static void edit_free_file_lines(struct file_line * first_line)
903 struct file_line * next_line;
905 while (first_line != NULL)
907 next_line = first_line->next;
908 first_line->next = NULL;
909 freez(first_line->raw);
910 freez(first_line->prefix);
911 freez(first_line->unprocessed);
912 switch(first_line->type)
914 case 0: /* special case if memory zeroed */
915 case FILE_LINE_UNPROCESSED:
916 case FILE_LINE_BLANK:
917 case FILE_LINE_ALIAS_HEADER:
918 case FILE_LINE_SETTINGS_HEADER:
919 case FILE_LINE_DESCRIPTION_HEADER:
920 case FILE_LINE_DESCRIPTION_ENTRY:
921 case FILE_LINE_ALIAS_ENTRY:
923 /* No data is stored for these */
926 case FILE_LINE_ACTION:
927 free_action(first_line->data.action);
930 case FILE_LINE_SETTINGS_ENTRY:
931 freez(first_line->data.setting.name);
932 freez(first_line->data.setting.svalue);
935 /* Should never happen */
939 first_line->type = 0; /* paranoia */
941 first_line = next_line;
946 /*********************************************************************
948 * Function : match_actions_file_header_line
950 * Description : Match an actions file {{header}} line
953 * 1 : line = String from file
954 * 2 : name = Header to match against
956 * Returns : 0 iff they match.
958 *********************************************************************/
959 static int match_actions_file_header_line(const char * line, const char * name)
967 if ((line[0] != '{') || (line[1] != '{'))
973 /* Look for optional whitespace */
974 while ( (*line == ' ') || (*line == '\t') )
979 /* Look for the specified name (case-insensitive) */
981 if (0 != strncmpic(line, name, len))
987 /* Look for optional whitespace */
988 while ( (*line == ' ') || (*line == '\t') )
993 /* Look for "}}" and end of string*/
994 if ((line[0] != '}') || (line[1] != '}') || (line[2] != '\0'))
1004 /*********************************************************************
1006 * Function : match_actions_file_header_line
1008 * Description : Match an actions file {{header}} line
1011 * 1 : line = String from file. Must not start with
1012 * whitespace (else infinite loop!)
1013 * 2 : name = Destination for name
1014 * 2 : name = Destination for value
1016 * Returns : JB_ERR_OK on success
1017 * JB_ERR_MEMORY on out-of-memory
1018 * JB_ERR_PARSE if there's no "=" sign, or if there's
1019 * nothing before the "=" sign (but empty
1020 * values *after* the "=" sign are legal).
1022 *********************************************************************/
1023 static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue)
1025 const char * name_end;
1026 const char * value_start;
1032 assert(*line != ' ');
1033 assert(*line != '\t');
1038 value_start = strchr(line, '=');
1039 if ((value_start == NULL) || (value_start == line))
1041 return JB_ERR_PARSE;
1044 name_end = value_start - 1;
1046 /* Eat any whitespace before the '=' */
1047 while ((*name_end == ' ') || (*name_end == '\t'))
1050 * we already know we must have at least 1 non-ws char
1051 * at start of buf - no need to check
1056 name_len = name_end - line + 1; /* Length excluding \0 */
1057 if (NULL == (*pname = (char *) malloc(name_len + 1)))
1059 return JB_ERR_MEMORY;
1061 strncpy(*pname, line, name_len);
1062 (*pname)[name_len] = '\0';
1064 /* Eat any the whitespace after the '=' */
1066 while ((*value_start == ' ') || (*value_start == '\t'))
1071 if (NULL == (*pvalue = strdup(value_start)))
1075 return JB_ERR_MEMORY;
1082 /*********************************************************************
1084 * Function : edit_parse_actions_file
1086 * Description : Parse an actions file in memory.
1088 * Passed linked list must have the "data" member
1089 * zeroed, and must contain valid "next" and
1090 * "unprocessed" fields. The "raw" and "prefix"
1091 * fields are ignored, and "type" is just overwritten.
1093 * Note that on error the file may have been
1097 * 1 : file = Actions file to be parsed in-place.
1099 * Returns : JB_ERR_OK on success
1100 * JB_ERR_MEMORY on out-of-memory
1101 * JB_ERR_PARSE on error
1103 *********************************************************************/
1104 jb_err edit_parse_actions_file(struct editable_file * file)
1106 struct file_line * cur_line;
1108 const char * text; /* Text from a line */
1109 char * name; /* For lines of the form name=value */
1110 char * value; /* For lines of the form name=value */
1111 struct action_alias * alias_list = NULL;
1112 jb_err err = JB_ERR_OK;
1114 /* alias_list contains the aliases defined in this file.
1115 * It might be better to use the "file_line.data" fields
1116 * in the relavent places instead.
1119 cur_line = file->lines;
1121 /* A note about blank line support: Blank lines should only
1122 * ever occur as the last line in the file. This function
1123 * is more forgiving than that - FILE_LINE_BLANK can occur
1127 /* Skip leading blanks. Should only happen if file is
1128 * empty (which is valid, but pointless).
1130 while ( (cur_line != NULL)
1131 && (cur_line->unprocessed[0] == '\0') )
1134 cur_line->type = FILE_LINE_BLANK;
1135 cur_line = cur_line->next;
1138 if ( (cur_line != NULL)
1139 && (cur_line->unprocessed[0] != '{') )
1141 /* File doesn't start with a header */
1142 file->parse_error = cur_line;
1143 file->parse_error_text = "First (non-comment) line of the file must contain a header.";
1144 return JB_ERR_PARSE;
1147 if ( (cur_line != NULL) && (0 ==
1148 match_actions_file_header_line(cur_line->unprocessed, "settings") ) )
1150 cur_line->type = FILE_LINE_SETTINGS_HEADER;
1152 cur_line = cur_line->next;
1153 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1155 if (cur_line->unprocessed[0])
1157 cur_line->type = FILE_LINE_SETTINGS_ENTRY;
1159 err = split_line_on_equals(cur_line->unprocessed,
1160 &cur_line->data.setting.name,
1161 &cur_line->data.setting.svalue);
1162 if (err == JB_ERR_MEMORY)
1166 else if (err != JB_ERR_OK)
1168 /* Line does not contain a name=value pair */
1169 file->parse_error = cur_line;
1170 file->parse_error_text = "Expected a name=value pair on this {{description}} line, but couldn't find one.";
1171 return JB_ERR_PARSE;
1176 cur_line->type = FILE_LINE_BLANK;
1178 cur_line = cur_line->next;
1182 if ( (cur_line != NULL) && (0 ==
1183 match_actions_file_header_line(cur_line->unprocessed, "description") ) )
1185 cur_line->type = FILE_LINE_DESCRIPTION_HEADER;
1187 cur_line = cur_line->next;
1188 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1190 if (cur_line->unprocessed[0])
1192 cur_line->type = FILE_LINE_DESCRIPTION_ENTRY;
1196 cur_line->type = FILE_LINE_BLANK;
1198 cur_line = cur_line->next;
1202 if ( (cur_line != NULL) && (0 ==
1203 match_actions_file_header_line(cur_line->unprocessed, "alias") ) )
1205 cur_line->type = FILE_LINE_ALIAS_HEADER;
1207 cur_line = cur_line->next;
1208 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1210 if (cur_line->unprocessed[0])
1212 /* define an alias */
1213 struct action_alias * new_alias;
1215 cur_line->type = FILE_LINE_ALIAS_ENTRY;
1217 err = split_line_on_equals(cur_line->unprocessed, &name, &value);
1218 if (err == JB_ERR_MEMORY)
1222 else if (err != JB_ERR_OK)
1224 /* Line does not contain a name=value pair */
1225 file->parse_error = cur_line;
1226 file->parse_error_text = "Expected a name=value pair on this {{alias}} line, but couldn't find one.";
1227 return JB_ERR_PARSE;
1230 if ((new_alias = zalloc(sizeof(*new_alias))) == NULL)
1235 free_alias_list(alias_list);
1236 return JB_ERR_MEMORY;
1239 err = get_actions(value, alias_list, new_alias->action);
1242 /* Invalid action or out of memory */
1246 free_alias_list(alias_list);
1247 if (err == JB_ERR_MEMORY)
1253 /* Line does not contain a name=value pair */
1254 file->parse_error = cur_line;
1255 file->parse_error_text = "This alias does not specify a valid set of actions.";
1256 return JB_ERR_PARSE;
1262 new_alias->name = name;
1265 new_alias->next = alias_list;
1266 alias_list = new_alias;
1270 cur_line->type = FILE_LINE_BLANK;
1272 cur_line = cur_line->next;
1276 /* Header done, process the main part of the file */
1277 while (cur_line != NULL)
1279 /* At this point, (cur_line->unprocessed[0] == '{') */
1280 assert(cur_line->unprocessed[0] == '{');
1281 text = cur_line->unprocessed + 1;
1282 len = strlen(text) - 1;
1283 if (text[len] != '}')
1285 /* No closing } on header */
1286 free_alias_list(alias_list);
1287 file->parse_error = cur_line;
1288 file->parse_error_text = "Headers starting with '{' must have a "
1289 "closing bracket ('}'). Headers starting with two brackets ('{{') "
1290 "must close with two brackets ('}}').";
1291 return JB_ERR_PARSE;
1296 /* An invalid {{ header. */
1297 free_alias_list(alias_list);
1298 file->parse_error = cur_line;
1299 file->parse_error_text = "Unknown or unexpected two-bracket header. "
1300 "Please remember that the system (two-bracket) headers must "
1301 "appear in the order {{settings}}, {{description}}, {{alias}}, "
1302 "and must appear before any actions (one-bracket) headers. "
1303 "Also note that system headers may not be repeated.";
1304 return JB_ERR_PARSE;
1307 while ( (*text == ' ') || (*text == '\t') )
1313 && ( (text[len - 1] == ' ')
1314 || (text[len - 1] == '\t') ) )
1319 cur_line->type = FILE_LINE_ACTION;
1321 /* Remove {} and make copy */
1322 if (NULL == (value = (char *) malloc(len + 1)))
1325 free_alias_list(alias_list);
1326 return JB_ERR_MEMORY;
1328 strncpy(value, text, len);
1332 err = get_actions(value, alias_list, cur_line->data.action);
1335 /* Invalid action or out of memory */
1337 free_alias_list(alias_list);
1338 if (err == JB_ERR_MEMORY)
1344 /* Line does not contain a name=value pair */
1345 file->parse_error = cur_line;
1346 file->parse_error_text = "This header does not specify a valid set of actions.";
1347 return JB_ERR_PARSE;
1351 /* Done with string - it was clobbered anyway */
1354 /* Process next line */
1355 cur_line = cur_line->next;
1357 /* Loop processing URL patterns */
1358 while ((cur_line != NULL) && (cur_line->unprocessed[0] != '{'))
1360 if (cur_line->unprocessed[0])
1362 /* Could parse URL here, but this isn't currently needed */
1364 cur_line->type = FILE_LINE_URL;
1368 cur_line->type = FILE_LINE_BLANK;
1370 cur_line = cur_line->next;
1372 } /* End main while(cur_line != NULL) loop */
1374 free_alias_list(alias_list);
1380 /*********************************************************************
1382 * Function : edit_read_file_lines
1384 * Description : Read all the lines of a file into memory.
1385 * Handles whitespace, comments and line continuation.
1388 * 1 : fp = File to read from. On return, this will be
1389 * at EOF but it will not have been closed.
1390 * 2 : pfile = Destination for a linked list of file_lines.
1391 * Will be set to NULL on error.
1393 * Returns : JB_ERR_OK on success
1394 * JB_ERR_MEMORY on out-of-memory
1396 *********************************************************************/
1397 jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline)
1399 struct file_line * first_line; /* Keep for return value or to free */
1400 struct file_line * cur_line; /* Current line */
1401 struct file_line * prev_line; /* Entry with prev_line->next = cur_line */
1409 cur_line = first_line = zalloc(sizeof(struct file_line));
1410 if (cur_line == NULL)
1412 return JB_ERR_MEMORY;
1415 cur_line->type = FILE_LINE_UNPROCESSED;
1417 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1420 /* Out of memory or empty file. */
1421 /* Note that empty file is not an error we propogate up */
1423 return ((rval == JB_ERR_FILE) ? JB_ERR_OK : rval);
1428 prev_line = cur_line;
1429 cur_line = prev_line->next = zalloc(sizeof(struct file_line));
1430 if (cur_line == NULL)
1433 edit_free_file_lines(first_line);
1434 return JB_ERR_MEMORY;
1437 cur_line->type = FILE_LINE_UNPROCESSED;
1439 rval = edit_read_line(fp, &cur_line->raw, &cur_line->prefix, &cur_line->unprocessed, newline, NULL);
1440 if ((rval != JB_ERR_OK) && (rval != JB_ERR_FILE))
1443 edit_free_file_lines(first_line);
1444 return JB_ERR_MEMORY;
1448 while (rval != JB_ERR_FILE);
1452 /* We allocated one too many - free it */
1453 prev_line->next = NULL;
1456 *pfile = first_line;
1461 /*********************************************************************
1463 * Function : edit_read_file
1465 * Description : Read a complete file into memory.
1466 * Handles CGI parameter parsing. If requested, also
1467 * checks the file's modification timestamp.
1470 * 1 : csp = Current client state (buffers, headers, etc...)
1471 * 2 : parameters = map of cgi parameters.
1472 * 3 : require_version = true to check "ver" parameter.
1473 * 4 : suffix = File extension, e.g. ".action".
1474 * 5 : pfile = Destination for the file. Will be set
1478 * filename : The name of the file to read, without the
1479 * path or ".action" extension.
1480 * ver : (Only if require_version is nonzero)
1481 * Timestamp of the actions file. If wrong, this
1482 * function fails with JB_ERR_MODIFIED.
1484 * Returns : JB_ERR_OK on success
1485 * JB_ERR_MEMORY on out-of-memory
1486 * JB_ERR_CGI_PARAMS if "filename" was not specified
1488 * JB_ERR_FILE if the file cannot be opened or
1490 * JB_ERR_MODIFIED if version checking was requested and
1491 * failed - the file was modified outside
1492 * of this CGI editor instance.
1494 *********************************************************************/
1495 jb_err edit_read_file(struct client_state *csp,
1496 const struct map *parameters,
1497 int require_version,
1499 struct editable_file **pfile)
1501 struct file_line * lines;
1505 const char * identifier;
1506 struct editable_file * file;
1507 unsigned version = 0;
1508 struct stat statbuf[1];
1509 char version_buf[22];
1510 int newline = NEWLINE_UNKNOWN;
1518 err = get_file_name_param(csp, parameters, "f", suffix,
1519 &filename, &identifier);
1525 if (stat(filename, statbuf) < 0)
1527 /* Error, probably file not found. */
1531 version = (unsigned) statbuf->st_mtime;
1533 if (require_version)
1535 unsigned specified_version;
1536 err = get_number_param(csp, parameters, "v", &specified_version);
1543 if (version != specified_version)
1545 return JB_ERR_MODIFIED;
1549 #if defined(AMIGA) || defined(__OS2__)
1550 if (NULL == (fp = fopen(filename,"r")))
1552 if (NULL == (fp = fopen(filename,"rt")))
1553 #endif /* def AMIGA */
1559 err = edit_read_file_lines(fp, &lines, &newline);
1569 file = (struct editable_file *) zalloc(sizeof(*file));
1573 edit_free_file_lines(lines);
1577 file->lines = lines;
1578 file->newline = newline;
1579 file->filename = filename;
1580 file->version = version;
1581 file->identifier = url_encode(identifier);
1583 if (file->identifier == NULL)
1585 edit_free_file(file);
1586 return JB_ERR_MEMORY;
1589 /* Correct file->version_str */
1590 freez(file->version_str);
1591 snprintf(version_buf, 22, "%u", file->version);
1592 version_buf[21] = '\0';
1593 file->version_str = strdup(version_buf);
1594 if (version_buf == NULL)
1596 edit_free_file(file);
1597 return JB_ERR_MEMORY;
1605 /*********************************************************************
1607 * Function : edit_read_actions_file
1609 * Description : Read a complete actions file into memory.
1610 * Handles CGI parameter parsing. If requested, also
1611 * checks the file's modification timestamp.
1613 * If this function detects an error in the categories
1614 * JB_ERR_FILE, JB_ERR_MODIFIED, or JB_ERR_PARSE,
1615 * then it handles it by filling in the specified
1616 * response structure and returning JB_ERR_FILE.
1619 * 1 : csp = Current client state (buffers, headers, etc...)
1620 * 2 : rsp = HTTP response. Only filled in on error.
1621 * 2 : parameters = map of cgi parameters.
1622 * 3 : require_version = true to check "ver" parameter.
1623 * 4 : pfile = Destination for the file. Will be set
1627 * filename : The name of the actions file to read, without the
1628 * path or ".action" extension.
1629 * ver : (Only if require_version is nonzero)
1630 * Timestamp of the actions file. If wrong, this
1631 * function fails with JB_ERR_MODIFIED.
1633 * Returns : JB_ERR_OK on success
1634 * JB_ERR_MEMORY on out-of-memory
1635 * JB_ERR_CGI_PARAMS if "filename" was not specified
1637 * JB_ERR_FILE if the file does not contain valid data,
1638 * or if file cannot be opened or
1639 * contains no data, or if version
1640 * checking was requested and failed.
1642 *********************************************************************/
1643 jb_err edit_read_actions_file(struct client_state *csp,
1644 struct http_response *rsp,
1645 const struct map *parameters,
1646 int require_version,
1647 struct editable_file **pfile)
1650 struct editable_file *file;
1658 err = edit_read_file(csp, parameters, require_version, ".action", &file);
1661 /* Try to handle if possible */
1662 if (err == JB_ERR_FILE)
1664 err = cgi_error_file(csp, rsp, lookup(parameters, "f"));
1666 else if (err == JB_ERR_MODIFIED)
1668 err = cgi_error_modified(csp, rsp, lookup(parameters, "f"));
1670 if (err == JB_ERR_OK)
1673 * Signal to higher-level CGI code that there was a problem but we
1674 * handled it, they should just return JB_ERR_OK.
1681 err = edit_parse_actions_file(file);
1684 if (err == JB_ERR_PARSE)
1686 err = cgi_error_parse(csp, rsp, file);
1687 if (err == JB_ERR_OK)
1690 * Signal to higher-level CGI code that there was a problem but we
1691 * handled it, they should just return JB_ERR_OK.
1696 edit_free_file(file);
1705 /*********************************************************************
1707 * Function : get_file_name_param
1709 * Description : Get the name of the file to edit from the parameters
1710 * passed to a CGI function. This function handles
1711 * security checks such as blocking urls containing
1712 * "/" or ".", prepending the config file directory,
1713 * and adding the specified suffix.
1715 * (This is an essential security check, otherwise
1716 * users may be able to pass "../../../etc/passwd"
1717 * and overwrite the password file [linux], "prn:"
1718 * and print random data [Windows], etc...)
1720 * This function only allows filenames contining the
1721 * characters '-', '_', 'A'-'Z', 'a'-'z', and '0'-'9'.
1722 * That's probably too restrictive but at least it's
1726 * 1 : csp = Current client state (buffers, headers, etc...)
1727 * 2 : parameters = map of cgi parameters
1728 * 3 : param_name = The name of the parameter to read
1729 * 4 : suffix = File extension, e.g. ".actions"
1730 * 5 : pfilename = destination for full filename. Caller
1731 * free()s. Set to NULL on error.
1732 * 6 : pparam = destination for partial filename,
1733 * suitable for use in another URL. Allocated as part
1734 * of the map "parameters", so don't free it.
1735 * Set to NULL if not specified.
1737 * Returns : JB_ERR_OK on success
1738 * JB_ERR_MEMORY on out-of-memory
1739 * JB_ERR_CGI_PARAMS if "filename" was not specified
1742 *********************************************************************/
1743 static jb_err get_file_name_param(struct client_state *csp,
1744 const struct map *parameters,
1745 const char *param_name,
1748 const char **pparam)
1752 #if 0 /* Patch to make 3.0.0 work properly. */
1754 #endif /* 0 - Patch to make 3.0.0 work properly. */
1768 param = lookup(parameters, param_name);
1771 return JB_ERR_CGI_PARAMS;
1776 len = strlen(param);
1777 if (len >= FILENAME_MAX)
1780 return JB_ERR_CGI_PARAMS;
1783 /* Check every character to see if it's legal */
1785 while ((ch = *s++) != '\0')
1787 if ( ((ch < 'A') || (ch > 'Z'))
1788 && ((ch < 'a') || (ch > 'z'))
1789 && ((ch < '0') || (ch > '9'))
1793 /* Probable hack attempt. */
1794 return JB_ERR_CGI_PARAMS;
1799 * FIXME Following is a hack to make 3.0.0 work properly.
1800 * Change "#if 0" --> "#if 1" below when we have modular action
1804 #if 0 /* Patch to make 3.0.0 work properly. */
1805 /* Append extension */
1806 name = malloc(len + strlen(suffix) + 1);
1809 return JB_ERR_MEMORY;
1811 strcpy(name, param);
1812 strcpy(name + len, suffix);
1815 fullpath = make_path(csp->config->confdir, name);
1817 #else /* 1 - Patch to make 3.0.0 work properly. */
1818 if ((csp->actions_list == NULL)
1819 || (csp->actions_list->filename == NULL))
1821 return JB_ERR_CGI_PARAMS;
1824 fullpath = ( (csp->actions_list && csp->actions_list->filename)
1825 ? strdup(csp->actions_list->filename) : NULL);
1826 #endif /* 1 - Patch to make 3.0.0 work properly. */
1827 if (fullpath == NULL)
1829 return JB_ERR_MEMORY;
1833 *pfilename = fullpath;
1839 /*********************************************************************
1841 * Function : get_number_param
1843 * Description : Get a non-negative integer from the parameters
1844 * passed to a CGI function.
1847 * 1 : csp = Current client state (buffers, headers, etc...)
1848 * 2 : parameters = map of cgi parameters
1849 * 3 : name = Name of CGI parameter to read
1850 * 4 : pvalue = destination for value.
1851 * Set to -1 on error.
1853 * Returns : JB_ERR_OK on success
1854 * JB_ERR_MEMORY on out-of-memory
1855 * JB_ERR_CGI_PARAMS if the parameter was not specified
1858 *********************************************************************/
1859 static jb_err get_number_param(struct client_state *csp,
1860 const struct map *parameters,
1875 param = lookup(parameters, name);
1878 return JB_ERR_CGI_PARAMS;
1881 /* We don't use atoi because I want to check this carefully... */
1884 while ((ch = *param++) != '\0')
1886 if ((ch < '0') || (ch > '9'))
1888 return JB_ERR_CGI_PARAMS;
1895 * <limits.h> defines UINT_MAX
1897 * (UINT_MAX - ch) / 10 is the largest number that
1898 * can be safely multiplied by 10 then have ch added.
1900 if (value > ((UINT_MAX - (unsigned)ch) / 10U))
1902 return JB_ERR_CGI_PARAMS;
1905 value = value * 10 + ch;
1916 /*********************************************************************
1918 * Function : get_url_spec_param
1920 * Description : Get a URL pattern from the parameters
1921 * passed to a CGI function. Removes leading/trailing
1922 * spaces and validates it.
1925 * 1 : csp = Current client state (buffers, headers, etc...)
1926 * 2 : parameters = map of cgi parameters
1927 * 3 : name = Name of CGI parameter to read
1928 * 4 : pvalue = destination for value. Will be malloc()'d.
1929 * Set to NULL on error.
1931 * Returns : JB_ERR_OK on success
1932 * JB_ERR_MEMORY on out-of-memory
1933 * JB_ERR_CGI_PARAMS if the parameter was not specified
1936 *********************************************************************/
1937 static jb_err get_url_spec_param(struct client_state *csp,
1938 const struct map *parameters,
1942 const char *orig_param;
1945 struct url_spec compiled[1];
1955 orig_param = lookup(parameters, name);
1958 return JB_ERR_CGI_PARAMS;
1961 /* Copy and trim whitespace */
1962 param = strdup(orig_param);
1965 return JB_ERR_MEMORY;
1969 /* Must be non-empty, and can't allow 1st character to be '{' */
1970 if (param[0] == '\0' || param[0] == '{')
1973 return JB_ERR_CGI_PARAMS;
1976 /* Check for embedded newlines */
1977 for (s = param; *s != '\0'; s++)
1979 if ((*s == '\r') || (*s == '\n'))
1982 return JB_ERR_CGI_PARAMS;
1986 /* Check that regex is valid */
1991 return JB_ERR_MEMORY;
1993 err = create_url_spec(compiled, s);
1998 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2000 free_url_spec(compiled);
2002 if (param[strlen(param) - 1] == '\\')
2005 * Must protect trailing '\\' from becoming line continuation character.
2006 * Two methods: 1) If it's a domain only, add a trailing '/'.
2007 * 2) For path, add the do-nothing PCRE expression (?:) to the end
2009 if (strchr(param, '/') == NULL)
2011 err = string_append(¶m, "/");
2015 err = string_append(¶m, "(?:)");
2022 /* Check that the modified regex is valid */
2027 return JB_ERR_MEMORY;
2029 err = create_url_spec(compiled, s);
2034 return (err == JB_ERR_MEMORY) ? JB_ERR_MEMORY : JB_ERR_CGI_PARAMS;
2036 free_url_spec(compiled);
2043 /*********************************************************************
2045 * Function : map_radio
2047 * Description : Map a set of radio button values. E.g. if you have
2048 * 3 radio buttons, declare them as:
2049 * <option type="radio" name="xyz" @xyz-a@>
2050 * <option type="radio" name="xyz" @xyz-b@>
2051 * <option type="radio" name="xyz" @xyz-c@>
2052 * Then map one of the @xyz-?@ variables to "checked"
2053 * and all the others to empty by calling:
2054 * map_radio(exports, "xyz", "abc", sel)
2055 * Where 'sel' is 'a', 'b', or 'c'.
2058 * 1 : exports = Exports map to modify.
2059 * 2 : optionname = name for map
2060 * 3 : values = null-terminated list of values;
2061 * 4 : value = Selected value.
2063 * CGI Parameters : None
2065 * Returns : JB_ERR_OK on success
2066 * JB_ERR_MEMORY on out-of-memory
2068 *********************************************************************/
2069 static jb_err map_radio(struct map * exports,
2070 const char * optionname,
2071 const char * values,
2083 len = strlen(optionname);
2084 buf = malloc(len + 3);
2087 return JB_ERR_MEMORY;
2090 strcpy(buf, optionname);
2095 while ((c = *values++) != '\0')
2100 if (map(exports, buf, 1, "", 1))
2103 return JB_ERR_MEMORY;
2109 if (map(exports, buf, 0, "checked", 1))
2112 return JB_ERR_MEMORY;
2119 /*********************************************************************
2121 * Function : cgi_error_modified
2123 * Description : CGI function that is called when a file is modified
2124 * outside the CGI editor.
2127 * 1 : csp = Current client state (buffers, headers, etc...)
2128 * 2 : rsp = http_response data structure for output
2129 * 3 : filename = The file that was modified.
2131 * CGI Parameters : none
2133 * Returns : JB_ERR_OK on success
2134 * JB_ERR_MEMORY on out-of-memory error.
2136 *********************************************************************/
2137 jb_err cgi_error_modified(struct client_state *csp,
2138 struct http_response *rsp,
2139 const char *filename)
2141 struct map *exports;
2148 if (NULL == (exports = default_exports(csp, NULL)))
2150 return JB_ERR_MEMORY;
2153 err = map(exports, "f", 1, html_encode(filename), 0);
2160 return template_fill_for_cgi(csp, "cgi-error-modified", exports, rsp);
2164 /*********************************************************************
2166 * Function : cgi_error_parse
2168 * Description : CGI function that is called when a file cannot
2169 * be parsed by the CGI editor.
2172 * 1 : csp = Current client state (buffers, headers, etc...)
2173 * 2 : rsp = http_response data structure for output
2174 * 3 : file = The file that was modified.
2176 * CGI Parameters : none
2178 * Returns : JB_ERR_OK on success
2179 * JB_ERR_MEMORY on out-of-memory error.
2181 *********************************************************************/
2182 jb_err cgi_error_parse(struct client_state *csp,
2183 struct http_response *rsp,
2184 struct editable_file *file)
2186 struct map *exports;
2188 struct file_line *cur_line;
2194 if (NULL == (exports = default_exports(csp, NULL)))
2196 return JB_ERR_MEMORY;
2199 err = map(exports, "f", 1, file->identifier, 1);
2200 if (!err) err = map(exports, "parse-error", 1, html_encode(file->parse_error_text), 0);
2202 cur_line = file->parse_error;
2205 if (!err) err = map(exports, "line-raw", 1, html_encode(cur_line->raw), 0);
2206 if (!err) err = map(exports, "line-data", 1, html_encode(cur_line->unprocessed), 0);
2214 return template_fill_for_cgi(csp, "cgi-error-parse", exports, rsp);
2218 /*********************************************************************
2220 * Function : cgi_error_file
2222 * Description : CGI function that is called when a file cannot be
2223 * opened by the CGI editor.
2226 * 1 : csp = Current client state (buffers, headers, etc...)
2227 * 2 : rsp = http_response data structure for output
2228 * 3 : filename = The file that was modified.
2230 * CGI Parameters : none
2232 * Returns : JB_ERR_OK on success
2233 * JB_ERR_MEMORY on out-of-memory error.
2235 *********************************************************************/
2236 jb_err cgi_error_file(struct client_state *csp,
2237 struct http_response *rsp,
2238 const char *filename)
2240 struct map *exports;
2247 if (NULL == (exports = default_exports(csp, NULL)))
2249 return JB_ERR_MEMORY;
2252 err = map(exports, "f", 1, html_encode(filename), 0);
2259 return template_fill_for_cgi(csp, "cgi-error-file", exports, rsp);
2263 /*********************************************************************
2265 * Function : cgi_error_bad_param
2267 * Description : CGI function that is called if the parameters
2268 * (query string) for a CGI were wrong.
2271 * 1 : csp = Current client state (buffers, headers, etc...)
2272 * 2 : rsp = http_response data structure for output
2274 * CGI Parameters : none
2276 * Returns : JB_ERR_OK on success
2277 * JB_ERR_MEMORY on out-of-memory error.
2279 *********************************************************************/
2280 jb_err cgi_error_disabled(struct client_state *csp,
2281 struct http_response *rsp)
2283 struct map *exports;
2288 if (NULL == (exports = default_exports(csp, NULL)))
2290 return JB_ERR_MEMORY;
2293 return template_fill_for_cgi(csp, "cgi-error-disabled", exports, rsp);
2297 /*********************************************************************
2299 * Function : cgi_edit_actions
2301 * Description : CGI function that allows the user to choose which
2302 * actions file to edit.
2305 * 1 : csp = Current client state (buffers, headers, etc...)
2306 * 2 : rsp = http_response data structure for output
2307 * 3 : parameters = map of cgi parameters
2309 * CGI Parameters : None
2311 * Returns : JB_ERR_OK on success
2312 * JB_ERR_MEMORY on out-of-memory error
2314 *********************************************************************/
2315 jb_err cgi_edit_actions(struct client_state *csp,
2316 struct http_response *rsp,
2317 const struct map *parameters)
2320 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2322 return cgi_error_disabled(csp, rsp);
2325 /* FIXME: Incomplete */
2326 rsp->status = strdup("302 Local Redirect from Junkbuster");
2327 if (rsp->status == NULL)
2329 return JB_ERR_MEMORY;
2331 if (enlist_unique_header(rsp->headers, "Location",
2332 CGI_PREFIX "edit-actions-list?f=ijb"))
2336 return JB_ERR_MEMORY;
2343 /*********************************************************************
2345 * Function : cgi_edit_actions_list
2347 * Description : CGI function that edits the actions list.
2348 * FIXME: This function shouldn't FATAL ever.
2349 * FIXME: This function doesn't check the retval of map()
2351 * 1 : csp = Current client state (buffers, headers, etc...)
2352 * 2 : rsp = http_response data structure for output
2353 * 3 : parameters = map of cgi parameters
2355 * CGI Parameters : filename
2357 * Returns : JB_ERR_OK on success
2358 * JB_ERR_MEMORY on out-of-memory
2359 * JB_ERR_FILE if the file cannot be opened or
2361 * JB_ERR_CGI_PARAMS if "filename" was not specified
2364 *********************************************************************/
2365 jb_err cgi_edit_actions_list(struct client_state *csp,
2366 struct http_response *rsp,
2367 const struct map *parameters)
2369 char * section_template;
2370 char * url_template;
2375 struct map * exports;
2376 struct map * section_exports;
2377 struct map * url_exports;
2378 struct editable_file * file;
2379 struct file_line * cur_line;
2380 unsigned line_number = 0;
2381 unsigned prev_section_line_number = ((unsigned) (-1));
2385 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2387 return cgi_error_disabled(csp, rsp);
2390 err = edit_read_actions_file(csp, rsp, parameters, 0, &file);
2393 /* No filename specified, can't read file, or out of memory. */
2394 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2397 if (NULL == (exports = default_exports(csp, NULL)))
2399 edit_free_file(file);
2400 return JB_ERR_MEMORY;
2403 err = map(exports, "f", 1, file->identifier, 1);
2404 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2408 edit_free_file(file);
2413 /* Should do all global exports above this point */
2415 err = template_load(csp, §ion_template, "edit-actions-list-section");
2418 edit_free_file(file);
2420 if (err == JB_ERR_FILE)
2422 return cgi_error_no_template(csp, rsp, "edit-actions-list-section");
2427 err = template_load(csp, &url_template, "edit-actions-list-url");
2430 free(section_template);
2431 edit_free_file(file);
2433 if (err == JB_ERR_FILE)
2435 return cgi_error_no_template(csp, rsp, "edit-actions-list-url");
2440 err = template_fill(§ion_template, exports);
2444 edit_free_file(file);
2450 err = template_fill(&url_template, exports);
2453 free(section_template);
2454 edit_free_file(file);
2459 /* Find start of actions in file */
2460 cur_line = file->lines;
2462 while ((cur_line != NULL) && (cur_line->type != FILE_LINE_ACTION))
2464 cur_line = cur_line->next;
2468 if (NULL == (sections = strdup("")))
2470 free(section_template);
2472 edit_free_file(file);
2474 return JB_ERR_MEMORY;
2477 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_ACTION))
2479 if (NULL == (section_exports = new_map()))
2482 free(section_template);
2484 edit_free_file(file);
2486 return JB_ERR_MEMORY;
2489 snprintf(buf, 50, "%d", line_number);
2490 err = map(section_exports, "s", 1, buf, 1);
2491 if (!err) err = map(section_exports, "actions", 1,
2492 actions_to_html(cur_line->data.action), 0);
2495 && (cur_line->next != NULL)
2496 && (cur_line->next->type == FILE_LINE_URL))
2498 /* This section contains at least one URL, don't allow delete */
2499 err = map_block_killer(section_exports, "empty-section");
2503 if (!err) err = map_block_keep(section_exports, "empty-section");
2506 if (prev_section_line_number != ((unsigned)(-1)))
2508 /* Not last section */
2509 snprintf(buf, 50, "%d", prev_section_line_number);
2510 if (!err) err = map(section_exports, "s-prev", 1, buf, 1);
2511 if (!err) err = map_block_keep(section_exports, "s-prev-exists");
2516 if (!err) err = map_block_killer(section_exports, "s-prev-exists");
2518 prev_section_line_number = line_number;
2523 free(section_template);
2525 edit_free_file(file);
2527 free_map(section_exports);
2531 /* Should do all section-specific exports above this point */
2533 if (NULL == (urls = strdup("")))
2536 free(section_template);
2538 edit_free_file(file);
2540 free_map(section_exports);
2541 return JB_ERR_MEMORY;
2546 cur_line = cur_line->next;
2549 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL))
2551 if (NULL == (url_exports = new_map()))
2555 free(section_template);
2557 edit_free_file(file);
2559 free_map(section_exports);
2560 return JB_ERR_MEMORY;
2563 snprintf(buf, 50, "%d", line_number);
2564 err = map(url_exports, "p", 1, buf, 1);
2566 snprintf(buf, 50, "%d", url_1_2);
2567 if (!err) err = map(url_exports, "url-1-2", 1, buf, 1);
2569 if (!err) err = map(url_exports, "url-html", 1,
2570 html_encode(cur_line->unprocessed), 0);
2571 if (!err) err = map(url_exports, "url", 1,
2572 url_encode(cur_line->unprocessed), 0);
2578 free(section_template);
2580 edit_free_file(file);
2582 free_map(section_exports);
2583 free_map(url_exports);
2587 if (NULL == (s = strdup(url_template)))
2591 free(section_template);
2593 edit_free_file(file);
2595 free_map(section_exports);
2596 free_map(url_exports);
2597 return JB_ERR_MEMORY;
2600 err = template_fill(&s, section_exports);
2601 if (!err) err = template_fill(&s, url_exports);
2602 if (!err) err = string_append(&urls, s);
2604 free_map(url_exports);
2611 free(section_template);
2613 edit_free_file(file);
2615 free_map(section_exports);
2619 url_1_2 = 3 - url_1_2;
2621 cur_line = cur_line->next;
2625 err = map(section_exports, "urls", 1, urls, 0);
2627 /* Could also do section-specific exports here, but it wouldn't be as fast */
2629 if ( (cur_line != NULL)
2630 && (cur_line->type == FILE_LINE_ACTION))
2632 /* Not last section */
2633 snprintf(buf, 50, "%d", line_number);
2634 if (!err) err = map(section_exports, "s-next", 1, buf, 1);
2635 if (!err) err = map_block_keep(section_exports, "s-next-exists");
2640 if (!err) err = map_block_killer(section_exports, "s-next-exists");
2646 free(section_template);
2648 edit_free_file(file);
2650 free_map(section_exports);
2654 if (NULL == (s = strdup(section_template)))
2657 free(section_template);
2659 edit_free_file(file);
2661 free_map(section_exports);
2662 return JB_ERR_MEMORY;
2665 err = template_fill(&s, section_exports);
2666 if (!err) err = string_append(§ions, s);
2669 free_map(section_exports);
2674 free(section_template);
2676 edit_free_file(file);
2682 edit_free_file(file);
2683 free(section_template);
2686 err = map(exports, "sections", 1, sections, 0);
2693 /* Could also do global exports here, but it wouldn't be as fast */
2695 return template_fill_for_cgi(csp, "edit-actions-list", exports, rsp);
2699 /*********************************************************************
2701 * Function : cgi_edit_actions
2703 * Description : CGI function that edits the Actions list.
2706 * 1 : csp = Current client state (buffers, headers, etc...)
2707 * 2 : rsp = http_response data structure for output
2708 * 3 : parameters = map of cgi parameters
2710 * CGI Parameters : None
2712 * Returns : JB_ERR_OK on success
2713 * JB_ERR_MEMORY on out-of-memory
2714 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2715 * specified or not valid.
2717 *********************************************************************/
2718 jb_err cgi_edit_actions_for_url(struct client_state *csp,
2719 struct http_response *rsp,
2720 const struct map *parameters)
2722 struct map * exports;
2724 struct editable_file * file;
2725 struct file_line * cur_line;
2726 unsigned line_number;
2729 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2731 return cgi_error_disabled(csp, rsp);
2734 err = get_number_param(csp, parameters, "s", §ionid);
2740 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2743 /* No filename specified, can't read file, modified, or out of memory. */
2744 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2747 cur_line = file->lines;
2749 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2751 cur_line = cur_line->next;
2754 if ( (cur_line == NULL)
2755 || (line_number != sectionid)
2757 || (cur_line->type != FILE_LINE_ACTION))
2759 /* Invalid "sectionid" parameter */
2760 edit_free_file(file);
2761 return JB_ERR_CGI_PARAMS;
2764 if (NULL == (exports = default_exports(csp, NULL)))
2766 edit_free_file(file);
2767 return JB_ERR_MEMORY;
2770 err = map(exports, "f", 1, file->identifier, 1);
2771 if (!err) err = map(exports, "v", 1, file->version_str, 1);
2772 if (!err) err = map(exports, "s", 1, url_encode(lookup(parameters, "s")), 0);
2774 if (!err) err = actions_to_radio(exports, cur_line->data.action);
2776 edit_free_file(file);
2784 return template_fill_for_cgi(csp, "edit-actions-for-url", exports, rsp);
2788 /*********************************************************************
2790 * Function : cgi_edit_actions_submit
2792 * Description : CGI function that actually edits the Actions list.
2795 * 1 : csp = Current client state (buffers, headers, etc...)
2796 * 2 : rsp = http_response data structure for output
2797 * 3 : parameters = map of cgi parameters
2799 * CGI Parameters : None
2801 * Returns : JB_ERR_OK on success
2802 * JB_ERR_MEMORY on out-of-memory
2803 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2804 * specified or not valid.
2806 *********************************************************************/
2807 jb_err cgi_edit_actions_submit(struct client_state *csp,
2808 struct http_response *rsp,
2809 const struct map *parameters)
2815 struct editable_file * file;
2816 struct file_line * cur_line;
2817 unsigned line_number;
2821 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2823 return cgi_error_disabled(csp, rsp);
2826 err = get_number_param(csp, parameters, "s", §ionid);
2832 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2835 /* No filename specified, can't read file, modified, or out of memory. */
2836 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2839 cur_line = file->lines;
2841 for (line_number = 1; (cur_line != NULL) && (line_number < sectionid); line_number++)
2843 cur_line = cur_line->next;
2846 if ( (cur_line == NULL)
2847 || (line_number != sectionid)
2849 || (cur_line->type != FILE_LINE_ACTION))
2851 /* Invalid "sectionid" parameter */
2852 edit_free_file(file);
2853 return JB_ERR_CGI_PARAMS;
2856 err = actions_from_radio(parameters, cur_line->data.action);
2860 edit_free_file(file);
2864 if (NULL == (actiontext = actions_to_text(cur_line->data.action)))
2867 edit_free_file(file);
2868 return JB_ERR_MEMORY;
2871 len = strlen(actiontext);
2875 * Empty action - must special-case this.
2876 * Simply setting len to 1 is sufficient...
2881 if (NULL == (newtext = malloc(len + 2)))
2885 edit_free_file(file);
2886 return JB_ERR_MEMORY;
2888 strcpy(newtext, actiontext);
2892 newtext[len + 1] = '\0';
2894 freez(cur_line->raw);
2895 freez(cur_line->unprocessed);
2896 cur_line->unprocessed = newtext;
2898 err = edit_write_file(file);
2901 /* Error writing file */
2902 edit_free_file(file);
2906 target = strdup(CGI_PREFIX "edit-actions-list?f=");
2907 string_append(&target, file->identifier);
2909 edit_free_file(file);
2914 return JB_ERR_MEMORY;
2917 rsp->status = strdup("302 Local Redirect from Junkbuster");
2918 if (rsp->status == NULL)
2921 return JB_ERR_MEMORY;
2923 err = enlist_unique_header(rsp->headers, "Location", target);
2930 /*********************************************************************
2932 * Function : cgi_edit_actions_url
2934 * Description : CGI function that actually edits a URL pattern in
2938 * 1 : csp = Current client state (buffers, headers, etc...)
2939 * 2 : rsp = http_response data structure for output
2940 * 3 : parameters = map of cgi parameters
2943 * filename : Identifies the file to edit
2944 * ver : File's last-modified time
2945 * section : Line number of section to edit
2946 * pattern : Line number of pattern to edit
2947 * newval : New value for pattern
2949 * Returns : JB_ERR_OK on success
2950 * JB_ERR_MEMORY on out-of-memory
2951 * JB_ERR_CGI_PARAMS if the CGI parameters are not
2952 * specified or not valid.
2954 *********************************************************************/
2955 jb_err cgi_edit_actions_url(struct client_state *csp,
2956 struct http_response *rsp,
2957 const struct map *parameters)
2961 struct editable_file * file;
2962 struct file_line * cur_line;
2963 unsigned line_number;
2967 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
2969 return cgi_error_disabled(csp, rsp);
2972 err = get_number_param(csp, parameters, "p", &patternid);
2979 return JB_ERR_CGI_PARAMS;
2982 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
2988 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
2991 /* No filename specified, can't read file, modified, or out of memory. */
2993 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
2997 cur_line = file->lines;
2999 while ((cur_line != NULL) && (line_number < patternid))
3001 cur_line = cur_line->next;
3005 if ( (cur_line == NULL)
3006 || (cur_line->type != FILE_LINE_URL))
3008 /* Invalid "patternid" parameter */
3010 edit_free_file(file);
3011 return JB_ERR_CGI_PARAMS;
3014 /* At this point, the line to edit is in cur_line */
3016 freez(cur_line->raw);
3017 freez(cur_line->unprocessed);
3018 cur_line->unprocessed = new_pattern;
3020 err = edit_write_file(file);
3023 /* Error writing file */
3024 edit_free_file(file);
3028 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3029 string_append(&target, file->identifier);
3031 edit_free_file(file);
3036 return JB_ERR_MEMORY;
3039 rsp->status = strdup("302 Local Redirect from Junkbuster");
3040 if (rsp->status == NULL)
3043 return JB_ERR_MEMORY;
3045 err = enlist_unique_header(rsp->headers, "Location", target);
3052 /*********************************************************************
3054 * Function : cgi_edit_actions_add_url
3056 * Description : CGI function that actually adds a URL pattern to
3060 * 1 : csp = Current client state (buffers, headers, etc...)
3061 * 2 : rsp = http_response data structure for output
3062 * 3 : parameters = map of cgi parameters
3065 * filename : Identifies the file to edit
3066 * ver : File's last-modified time
3067 * section : Line number of section to edit
3068 * newval : New pattern
3070 * Returns : JB_ERR_OK on success
3071 * JB_ERR_MEMORY on out-of-memory
3072 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3073 * specified or not valid.
3075 *********************************************************************/
3076 jb_err cgi_edit_actions_add_url(struct client_state *csp,
3077 struct http_response *rsp,
3078 const struct map *parameters)
3082 struct file_line * new_line;
3083 struct editable_file * file;
3084 struct file_line * cur_line;
3085 unsigned line_number;
3089 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3091 return cgi_error_disabled(csp, rsp);
3094 err = get_number_param(csp, parameters, "s", §ionid);
3101 return JB_ERR_CGI_PARAMS;
3104 err = get_url_spec_param(csp, parameters, "u", &new_pattern);
3110 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3113 /* No filename specified, can't read file, modified, or out of memory. */
3115 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3119 cur_line = file->lines;
3121 while ((cur_line != NULL) && (line_number < sectionid))
3123 cur_line = cur_line->next;
3127 if ( (cur_line == NULL)
3128 || (cur_line->type != FILE_LINE_ACTION))
3130 /* Invalid "sectionid" parameter */
3132 edit_free_file(file);
3133 return JB_ERR_CGI_PARAMS;
3136 /* At this point, the section header is in cur_line - add after this. */
3138 /* Allocate the new line */
3139 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3140 if (new_line == NULL)
3143 edit_free_file(file);
3144 return JB_ERR_MEMORY;
3147 /* Fill in the data members of the new line */
3148 new_line->raw = NULL;
3149 new_line->prefix = NULL;
3150 new_line->unprocessed = new_pattern;
3151 new_line->type = FILE_LINE_URL;
3153 /* Link new_line into the list, after cur_line */
3154 new_line->next = cur_line->next;
3155 cur_line->next = new_line;
3157 /* Done making changes, now commit */
3159 err = edit_write_file(file);
3162 /* Error writing file */
3163 edit_free_file(file);
3167 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3168 string_append(&target, file->identifier);
3170 edit_free_file(file);
3175 return JB_ERR_MEMORY;
3178 rsp->status = strdup("302 Local Redirect from Junkbuster");
3179 if (rsp->status == NULL)
3182 return JB_ERR_MEMORY;
3184 err = enlist_unique_header(rsp->headers, "Location", target);
3191 /*********************************************************************
3193 * Function : cgi_edit_actions_remove_url
3195 * Description : CGI function that actually removes a URL pattern from
3199 * 1 : csp = Current client state (buffers, headers, etc...)
3200 * 2 : rsp = http_response data structure for output
3201 * 3 : parameters = map of cgi parameters
3204 * f : (filename) Identifies the file to edit
3205 * v : (version) File's last-modified time
3206 * p : (pattern) Line number of pattern to remove
3208 * Returns : JB_ERR_OK on success
3209 * JB_ERR_MEMORY on out-of-memory
3210 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3211 * specified or not valid.
3213 *********************************************************************/
3214 jb_err cgi_edit_actions_remove_url(struct client_state *csp,
3215 struct http_response *rsp,
3216 const struct map *parameters)
3219 struct editable_file * file;
3220 struct file_line * cur_line;
3221 struct file_line * prev_line;
3222 unsigned line_number;
3226 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3228 return cgi_error_disabled(csp, rsp);
3231 err = get_number_param(csp, parameters, "p", &patternid);
3237 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3240 /* No filename specified, can't read file, modified, or out of memory. */
3241 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3246 cur_line = file->lines;
3248 while ((cur_line != NULL) && (line_number < patternid))
3250 prev_line = cur_line;
3251 cur_line = cur_line->next;
3255 if ( (cur_line == NULL)
3256 || (prev_line == NULL)
3257 || (cur_line->type != FILE_LINE_URL))
3259 /* Invalid "patternid" parameter */
3260 edit_free_file(file);
3261 return JB_ERR_CGI_PARAMS;
3264 /* At this point, the line to remove is in cur_line, and the previous
3265 * one is in prev_line
3268 /* Unlink cur_line */
3269 prev_line->next = cur_line->next;
3270 cur_line->next = NULL;
3273 edit_free_file_lines(cur_line);
3275 err = edit_write_file(file);
3278 /* Error writing file */
3279 edit_free_file(file);
3283 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3284 string_append(&target, file->identifier);
3286 edit_free_file(file);
3291 return JB_ERR_MEMORY;
3294 rsp->status = strdup("302 Local Redirect from Junkbuster");
3295 if (rsp->status == NULL)
3298 return JB_ERR_MEMORY;
3300 err = enlist_unique_header(rsp->headers, "Location", target);
3307 /*********************************************************************
3309 * Function : cgi_edit_actions_section_remove
3311 * Description : CGI function that actually removes a whole section from
3312 * the actions file. The section must be empty first
3313 * (else JB_ERR_CGI_PARAMS).
3316 * 1 : csp = Current client state (buffers, headers, etc...)
3317 * 2 : rsp = http_response data structure for output
3318 * 3 : parameters = map of cgi parameters
3321 * f : (filename) Identifies the file to edit
3322 * v : (version) File's last-modified time
3323 * s : (section) Line number of section to edit
3325 * Returns : JB_ERR_OK on success
3326 * JB_ERR_MEMORY on out-of-memory
3327 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3328 * specified or not valid.
3330 *********************************************************************/
3331 jb_err cgi_edit_actions_section_remove(struct client_state *csp,
3332 struct http_response *rsp,
3333 const struct map *parameters)
3336 struct editable_file * file;
3337 struct file_line * cur_line;
3338 struct file_line * prev_line;
3339 unsigned line_number;
3343 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3345 return cgi_error_disabled(csp, rsp);
3348 err = get_number_param(csp, parameters, "s", §ionid);
3354 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3357 /* No filename specified, can't read file, modified, or out of memory. */
3358 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3362 cur_line = file->lines;
3365 while ((cur_line != NULL) && (line_number < sectionid))
3367 prev_line = cur_line;
3368 cur_line = cur_line->next;
3372 if ( (cur_line == NULL)
3373 || (cur_line->type != FILE_LINE_ACTION) )
3375 /* Invalid "sectionid" parameter */
3376 edit_free_file(file);
3377 return JB_ERR_CGI_PARAMS;
3380 if ( (cur_line->next != NULL)
3381 && (cur_line->next->type == FILE_LINE_URL) )
3383 /* Section not empty. */
3384 edit_free_file(file);
3385 return JB_ERR_CGI_PARAMS;
3388 /* At this point, the line to remove is in cur_line, and the previous
3389 * one is in prev_line
3392 /* Unlink cur_line */
3393 if (prev_line == NULL)
3395 /* Removing the first line from the file */
3396 file->lines = cur_line->next;
3400 prev_line->next = cur_line->next;
3402 cur_line->next = NULL;
3405 edit_free_file_lines(cur_line);
3407 err = edit_write_file(file);
3410 /* Error writing file */
3411 edit_free_file(file);
3415 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3416 string_append(&target, file->identifier);
3418 edit_free_file(file);
3423 return JB_ERR_MEMORY;
3426 rsp->status = strdup("302 Local Redirect from Junkbuster");
3427 if (rsp->status == NULL)
3430 return JB_ERR_MEMORY;
3432 err = enlist_unique_header(rsp->headers, "Location", target);
3439 /*********************************************************************
3441 * Function : cgi_edit_actions_section_add
3443 * Description : CGI function that adds a new empty section to
3447 * 1 : csp = Current client state (buffers, headers, etc...)
3448 * 2 : rsp = http_response data structure for output
3449 * 3 : parameters = map of cgi parameters
3452 * f : (filename) Identifies the file to edit
3453 * v : (version) File's last-modified time
3454 * s : (section) Line number of section to add after, 0 for
3457 * Returns : JB_ERR_OK on success
3458 * JB_ERR_MEMORY on out-of-memory
3459 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3460 * specified or not valid.
3462 *********************************************************************/
3463 jb_err cgi_edit_actions_section_add(struct client_state *csp,
3464 struct http_response *rsp,
3465 const struct map *parameters)
3468 struct file_line * new_line;
3470 struct editable_file * file;
3471 struct file_line * cur_line;
3472 unsigned line_number;
3476 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3478 return cgi_error_disabled(csp, rsp);
3481 err = get_number_param(csp, parameters, "s", §ionid);
3487 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3490 /* No filename specified, can't read file, modified, or out of memory. */
3491 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3495 cur_line = file->lines;
3499 /* Add to start of file */
3500 if (cur_line != NULL)
3502 /* There's something in the file, find the line before the first
3505 while ( (cur_line->next != NULL)
3506 && (cur_line->next->type != FILE_LINE_ACTION) )
3508 cur_line = cur_line->next;
3515 /* Add after stated section. */
3516 while ((cur_line != NULL) && (line_number < sectionid))
3518 cur_line = cur_line->next;
3522 if ( (cur_line == NULL)
3523 || (cur_line->type != FILE_LINE_ACTION))
3525 /* Invalid "sectionid" parameter */
3526 edit_free_file(file);
3527 return JB_ERR_CGI_PARAMS;
3530 /* Skip through the section to find the last line in it. */
3531 while ( (cur_line->next != NULL)
3532 && (cur_line->next->type != FILE_LINE_ACTION) )
3534 cur_line = cur_line->next;
3539 /* At this point, the last line in the previous section is in cur_line
3540 * - add after this. (Or if we need to add as the first line, cur_line
3544 new_text = strdup("{}");
3545 if (NULL == new_text)
3547 edit_free_file(file);
3548 return JB_ERR_MEMORY;
3551 /* Allocate the new line */
3552 new_line = (struct file_line *)zalloc(sizeof(*new_line));
3553 if (new_line == NULL)
3556 edit_free_file(file);
3557 return JB_ERR_MEMORY;
3560 /* Fill in the data members of the new line */
3561 new_line->raw = NULL;
3562 new_line->prefix = NULL;
3563 new_line->unprocessed = new_text;
3564 new_line->type = FILE_LINE_ACTION;
3566 if (cur_line != NULL)
3568 /* Link new_line into the list, after cur_line */
3569 new_line->next = cur_line->next;
3570 cur_line->next = new_line;
3574 /* Link new_line into the list, as first line */
3575 new_line->next = file->lines;
3576 file->lines = new_line;
3579 /* Done making changes, now commit */
3581 err = edit_write_file(file);
3584 /* Error writing file */
3585 edit_free_file(file);
3589 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3590 string_append(&target, file->identifier);
3592 edit_free_file(file);
3597 return JB_ERR_MEMORY;
3600 rsp->status = strdup("302 Local Redirect from Junkbuster");
3601 if (rsp->status == NULL)
3604 return JB_ERR_MEMORY;
3606 err = enlist_unique_header(rsp->headers, "Location", target);
3613 /*********************************************************************
3615 * Function : cgi_edit_actions_section_swap
3617 * Description : CGI function that swaps the order of two sections
3618 * in the actions file. Note that this CGI can actually
3619 * swap any two arbitrary sections, but the GUI interface
3620 * currently only allows consecutive sections to be
3624 * 1 : csp = Current client state (buffers, headers, etc...)
3625 * 2 : rsp = http_response data structure for output
3626 * 3 : parameters = map of cgi parameters
3629 * f : (filename) Identifies the file to edit
3630 * v : (version) File's last-modified time
3631 * s1 : (section1) Line number of first section to swap
3632 * s2 : (section2) Line number of second section to swap
3634 * Returns : JB_ERR_OK on success
3635 * JB_ERR_MEMORY on out-of-memory
3636 * JB_ERR_CGI_PARAMS if the CGI parameters are not
3637 * specified or not valid.
3639 *********************************************************************/
3640 jb_err cgi_edit_actions_section_swap(struct client_state *csp,
3641 struct http_response *rsp,
3642 const struct map *parameters)
3646 struct editable_file * file;
3647 struct file_line * cur_line;
3648 struct file_line * prev_line;
3649 struct file_line * line_before_section1;
3650 struct file_line * line_start_section1;
3651 struct file_line * line_end_section1;
3652 struct file_line * line_after_section1;
3653 struct file_line * line_before_section2;
3654 struct file_line * line_start_section2;
3655 struct file_line * line_end_section2;
3656 struct file_line * line_after_section2;
3657 unsigned line_number;
3661 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_EDIT_ACTIONS))
3663 return cgi_error_disabled(csp, rsp);
3666 err = get_number_param(csp, parameters, "s1", §ion1);
3667 if (!err) err = get_number_param(csp, parameters, "s2", §ion2);
3673 if (section1 > section2)
3675 unsigned temp = section2;
3676 section2 = section1;
3680 err = edit_read_actions_file(csp, rsp, parameters, 1, &file);
3683 /* No filename specified, can't read file, modified, or out of memory. */
3684 return (err == JB_ERR_FILE ? JB_ERR_OK : err);
3687 /* Start at the beginning... */
3689 cur_line = file->lines;
3692 /* ... find section1 ... */
3693 while ((cur_line != NULL) && (line_number < section1))
3695 prev_line = cur_line;
3696 cur_line = cur_line->next;
3700 if ( (cur_line == NULL)
3701 || (cur_line->type != FILE_LINE_ACTION) )
3703 /* Invalid "section1" parameter */
3704 edit_free_file(file);
3705 return JB_ERR_CGI_PARAMS;
3708 /* If no-op, we've validated params and can skip the rest. */
3709 if (section1 != section2)
3711 /* ... find the end of section1 ... */
3712 line_before_section1 = prev_line;
3713 line_start_section1 = 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_section1 = prev_line;
3722 line_after_section1 = cur_line;
3724 /* ... find section2 ... */
3725 while ((cur_line != NULL) && (line_number < section2))
3727 prev_line = cur_line;
3728 cur_line = cur_line->next;
3732 if ( (cur_line == NULL)
3733 || (cur_line->type != FILE_LINE_ACTION) )
3735 /* Invalid "section2" parameter */
3736 edit_free_file(file);
3737 return JB_ERR_CGI_PARAMS;
3740 /* ... find the end of section2 ... */
3741 line_before_section2 = prev_line;
3742 line_start_section2 = cur_line;
3745 prev_line = cur_line;
3746 cur_line = cur_line->next;
3749 while ((cur_line != NULL) && (cur_line->type == FILE_LINE_URL));
3750 line_end_section2 = prev_line;
3751 line_after_section2 = cur_line;
3753 /* Now have all the pointers we need. Do the swap. */
3755 /* Change the pointer to section1 to point to section2 instead */
3756 if (line_before_section1 == NULL)
3758 file->lines = line_start_section2;
3762 line_before_section1->next = line_start_section2;
3765 if (line_before_section2 == line_end_section1)
3767 /* Consecutive sections */
3768 line_end_section2->next = line_start_section1;
3772 line_end_section2->next = line_after_section1;
3773 line_before_section2->next = line_start_section1;
3776 /* Set the pointer from the end of section1 to the rest of the file */
3777 line_end_section1->next = line_after_section2;
3779 err = edit_write_file(file);
3782 /* Error writing file */
3783 edit_free_file(file);
3786 } /* END if (section1 != section2) */
3788 target = strdup(CGI_PREFIX "edit-actions-list?f=");
3789 string_append(&target, file->identifier);
3791 edit_free_file(file);
3796 return JB_ERR_MEMORY;
3799 rsp->status = strdup("302 Local Redirect from Junkbuster");
3800 if (rsp->status == NULL)
3803 return JB_ERR_MEMORY;
3805 err = enlist_unique_header(rsp->headers, "Location", target);
3812 /*********************************************************************
3814 * Function : cgi_toggle
3816 * Description : CGI function that adds a new empty section to
3820 * 1 : csp = Current client state (buffers, headers, etc...)
3821 * 2 : rsp = http_response data structure for output
3822 * 3 : parameters = map of cgi parameters
3825 * set : If present, how to change toggle setting:
3826 * "enable", "disable", "toggle", or none (default).
3827 * mini : If present, use mini reply template.
3829 * Returns : JB_ERR_OK on success
3830 * JB_ERR_MEMORY on out-of-memory
3832 *********************************************************************/
3833 jb_err cgi_toggle(struct client_state *csp,
3834 struct http_response *rsp,
3835 const struct map *parameters)
3837 struct map *exports;
3839 const char *template_name;
3846 if (0 == (csp->config->feature_flags & RUNTIME_FEATURE_CGI_TOGGLE))
3848 return cgi_error_disabled(csp, rsp);
3851 if (NULL == (exports = default_exports(csp, "toggle")))
3853 return JB_ERR_MEMORY;
3856 mode = *(lookup(parameters, "set"));
3863 else if (mode == 'd')
3868 else if (mode == 't')
3871 g_bToggleIJB = !g_bToggleIJB;
3874 err = map_conditional(exports, "enabled", g_bToggleIJB);
3881 template_name = (*(lookup(parameters, "mini"))
3885 return template_fill_for_cgi(csp, template_name, exports, rsp);
3889 /*********************************************************************
3891 * Function : actions_to_radio
3893 * Description : Converts a actionsfile entry into settings for
3894 * radio buttons and edit boxes on a HTML form.
3897 * 1 : exports = List of substitutions to add to.
3898 * 2 : action = Action to read
3900 * Returns : JB_ERR_OK on success
3901 * JB_ERR_MEMORY on out-of-memory
3903 *********************************************************************/
3904 static jb_err actions_to_radio(struct map * exports,
3905 const struct action_spec *action)
3907 unsigned mask = action->mask;
3908 unsigned add = action->add;
3916 mask = action->mask;
3919 /* sanity - prevents "-feature +feature" */
3923 #define DEFINE_ACTION_BOOL(name, bit) \
3924 if (!(mask & bit)) \
3926 current_mode = 'n'; \
3928 else if (add & bit) \
3930 current_mode = 'y'; \
3934 current_mode = 'x'; \
3936 if (map_radio(exports, name, "ynx", current_mode)) \
3938 return JB_ERR_MEMORY; \
3941 #define DEFINE_ACTION_STRING(name, bit, index) \
3942 DEFINE_ACTION_BOOL(name, bit); \
3945 #define DEFINE_CGI_PARAM_RADIO(name, bit, index, value, is_default) \
3948 checked = !strcmp(action->string[index], value); \
3952 checked = is_default; \
3954 mapped_param |= checked; \
3955 if (map(exports, name "-param-" value, 1, (checked ? "checked" : ""), 1)) \
3957 return JB_ERR_MEMORY; \
3960 #define DEFINE_CGI_PARAM_CUSTOM(name, bit, index, default_val) \
3961 if (map(exports, name "-param-custom", 1, \
3962 ((!mapped_param) ? "checked" : ""), 1)) \
3964 return JB_ERR_MEMORY; \
3966 if (map(exports, name "-param", 1, \
3967 (((add & bit) && !mapped_param) ? \
3968 action->string[index] : default_val), 1)) \
3970 return JB_ERR_MEMORY; \
3973 #define DEFINE_CGI_PARAM_NO_RADIO(name, bit, index, default_val) \
3974 if (map(exports, name "-param", 1, \
3975 ((add & bit) ? action->string[index] : default_val), 1)) \
3977 return JB_ERR_MEMORY; \
3980 #define DEFINE_ACTION_MULTI(name, index) \
3981 if (action->multi_add[index]->first) \
3983 current_mode = 'y'; \
3985 else if (action->multi_remove_all[index]) \
3987 current_mode = 'n'; \
3989 else if (action->multi_remove[index]->first) \
3991 current_mode = 'y'; \
3995 current_mode = 'x'; \
3997 if (map_radio(exports, name, "ynx", current_mode)) \
3999 return JB_ERR_MEMORY; \
4002 #define DEFINE_ACTION_ALIAS 0 /* No aliases for output */
4004 #include "actionlist.h"
4006 #undef DEFINE_ACTION_MULTI
4007 #undef DEFINE_ACTION_STRING
4008 #undef DEFINE_ACTION_BOOL
4009 #undef DEFINE_ACTION_ALIAS
4010 #undef DEFINE_CGI_PARAM_CUSTOM
4011 #undef DEFINE_CGI_PARAM_RADIO
4012 #undef DEFINE_CGI_PARAM_NO_RADIO
4018 /*********************************************************************
4020 * Function : javascriptify
4022 * Description : Converts a string into a form JavaScript will like.
4024 * Netscape 4's JavaScript sucks - it doesn't use
4025 * "id" parameters, so you have to set the "name"
4026 * used to submit a form element to something JavaScript
4027 * will like. (Or access the elements by index in an
4028 * array. That array contains >60 elements and will
4029 * be changed whenever we add a new action to the
4030 * editor, so I'm NOT going to use indexes that have
4031 * to be figured out by hand.)
4033 * Currently the only thing we have to worry about
4034 * is "-" ==> "_" conversion.
4036 * This is a length-preserving operation so it is
4037 * carried out in-place, no memory is allocated
4041 * 1 : identifier = String to make JavaScript-friendly.
4045 *********************************************************************/
4046 static void javascriptify(char * identifier)
4048 char * p = identifier;
4049 while (NULL != (p = strchr(p, '-')))
4056 /*********************************************************************
4058 * Function : actions_from_radio
4060 * Description : Converts a map of parameters passed to a CGI function
4061 * into an actionsfile entry.
4064 * 1 : parameters = parameters to the CGI call
4065 * 2 : action = Action to change. Must be valid before
4066 * the call, actions not specified will be
4069 * Returns : JB_ERR_OK on success
4070 * JB_ERR_MEMORY on out-of-memory
4072 *********************************************************************/
4073 static jb_err actions_from_radio(const struct map * parameters,
4074 struct action_spec *action)
4076 static int first_time = 1;
4080 const char * js_name;
4085 /* Statics are generally a potential race condition,
4086 * but in this case we're safe and don't need semaphores.
4087 * Be careful if you modify this function.
4091 #define JAVASCRIPTIFY(dest_var, string) \
4093 static char js_name_arr[] = string; \
4096 javascriptify(js_name_arr); \
4098 dest_var = js_name_arr; \
4101 #define DEFINE_ACTION_BOOL(name, bit) \
4102 JAVASCRIPTIFY(js_name, name); \
4103 param = lookup(parameters, js_name); \
4104 ch = ijb_toupper(param[0]); \
4107 action->add |= bit; \
4108 action->mask |= bit; \
4110 else if (ch == 'N') \
4112 action->add &= ~bit; \
4113 action->mask &= ~bit; \
4115 else if (ch == 'X') \
4117 action->add &= ~bit; \
4118 action->mask |= bit; \
4121 #define DEFINE_ACTION_STRING(name, bit, index) \
4122 JAVASCRIPTIFY(js_name, name); \
4123 param = lookup(parameters, js_name); \
4124 ch = ijb_toupper(param[0]); \
4127 JAVASCRIPTIFY(js_name, name "-mode"); \
4128 param = lookup(parameters, js_name); \
4129 if ((*param == '\0') || (0 == strcmp(param, "CUSTOM"))) \
4131 JAVASCRIPTIFY(js_name, name "-param"); \
4132 param = lookup(parameters, js_name); \
4134 if (*param != '\0') \
4136 if (NULL == (param_dup = strdup(param))) \
4138 return JB_ERR_MEMORY; \
4140 freez(action->string[index]); \
4141 action->add |= bit; \
4142 action->mask |= bit; \
4143 action->string[index] = param_dup; \
4146 else if (ch == 'N') \
4148 if (action->add & bit) \
4150 freez(action->string[index]); \
4152 action->add &= ~bit; \
4153 action->mask &= ~bit; \
4155 else if (ch == 'X') \
4157 if (action->add & bit) \
4159 freez(action->string[index]); \
4161 action->add &= ~bit; \
4162 action->mask |= bit; \
4165 #define DEFINE_ACTION_MULTI(name, index) \
4166 JAVASCRIPTIFY(js_name, name); \
4167 param = lookup(parameters, js_name); \
4168 ch = ijb_toupper((int)param[0]); \
4173 else if (ch == 'N') \
4175 list_remove_all(action->multi_add[index]); \
4176 list_remove_all(action->multi_remove[index]); \
4177 action->multi_remove_all[index] = 1; \
4179 else if (ch == 'X') \
4181 list_remove_all(action->multi_add[index]); \
4182 list_remove_all(action->multi_remove[index]); \
4183 action->multi_remove_all[index] = 0; \
4186 #define DEFINE_ACTION_ALIAS 0 /* No aliases for URL parsing */
4188 #include "actionlist.h"
4190 #undef DEFINE_ACTION_MULTI
4191 #undef DEFINE_ACTION_STRING
4192 #undef DEFINE_ACTION_BOOL
4193 #undef DEFINE_ACTION_ALIAS
4194 #undef JAVASCRIPTIFY
4202 #endif /* def FEATURE_CGI_EDIT_ACTIONS */