X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=cgiedit.c;h=750015cf665b0c3354542c61e3302d94998e0c84;hp=4b727c8f366e1b314ddd2ea721cbb7f1c1858377;hb=9dae3e9be4e16d17fe8ca8c5cb363e3b7b9896aa;hpb=2465a9102eb7d4f7a53bd454596e61edd16acb85 diff --git a/cgiedit.c b/cgiedit.c index 4b727c8f..750015cf 100644 --- a/cgiedit.c +++ b/cgiedit.c @@ -1,21 +1,25 @@ -const char cgiedit_rcs[] = "$Id: cgi.c,v 1.25 2001/09/16 15:02:35 jongfoster Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgiedit.c,v $ * * Purpose : CGI-based actionsfile editor. - * - * Functions declared include: - * * - * Copyright : Written by and Copyright (C) 2001 the SourceForge - * IJBSWA team. http://ijbswa.sourceforge.net + * NOTE: The CGIs in this file use parameter names + * such as "f" and "s" which are really *BAD* choices. + * However, I'm trying to save bytes in the + * edit-actions-list HTML page - the standard actions + * file generated a 550kbyte page, which is ridiculous. + * + * Stick to the short names in this file for consistency. + * + * Copyright : Written by and Copyright (C) 2001-2014 the + * Privoxy team. http://www.privoxy.org/ * * Based on the Internet Junkbuster originally written - * by and Copyright (C) 1997 Anonymous Coders and + * by and Copyright (C) 1997 Anonymous Coders and * Junkbusters Corporation. http://www.junkbusters.com * - * This program is free software; you can redistribute it + * This program is free software; you can redistribute it * and/or modify it under the terms of the GNU General * Public License as published by the Free Software * Foundation; either version 2 of the License, or (at @@ -33,11 +37,8 @@ const char cgiedit_rcs[] = "$Id: cgi.c,v 1.25 2001/09/16 15:02:35 jongfoster Exp * or write to the Free Software Foundation, Inc., 59 * Temple Place - Suite 330, Boston, MA 02111-1307, USA. * - * Revisions : - * $Log: cgi.c,v $ - * **********************************************************************/ - + #include "config.h" @@ -46,454 +47,4377 @@ const char cgiedit_rcs[] = "$Id: cgi.c,v 1.25 2001/09/16 15:02:35 jongfoster Exp */ #include -#include #include +#include #include #include #include - -#ifdef _WIN32 -#define snprintf _snprintf -#endif /* def _WIN32 */ +#include #include "project.h" #include "cgi.h" #include "cgiedit.h" +#include "cgisimple.h" #include "list.h" #include "encode.h" -#include "ssplit.h" -#include "jcc.h" -#include "filters.h" #include "actions.h" -#include "errlog.h" #include "miscutil.h" -#include "showargs.h" +#include "errlog.h" +#include "loaders.h" +#ifdef FEATURE_TOGGLE +/* loadcfg.h is for global_toggle_state only */ #include "loadcfg.h" - -const char cgiedit_h_rcs[] = CGIEDIT_H_VERSION; +#endif /* def FEATURE_TOGGLE */ +#include "urlmatch.h" #ifdef FEATURE_CGI_EDIT_ACTIONS - -/********************************************************************* - * - * Function : cgi_edit_actions_list - * - * Description : CGI function that edits the actions list. - * - * Parameters : - * 1 : csp = Current client state (buffers, headers, etc...) - * 2 : rsp = http_response data structure for output - * 3 : parameters = map of cgi parameters - * - * CGI Parameters : None - * - * Returns : 0 - * - *********************************************************************/ -int cgi_edit_actions_list(struct client_state *csp, struct http_response *rsp, - struct map *parameters) +/** + * A line in an editable_file. + */ +struct file_line { - struct file_list *fl; - struct url_actions *actions; - char * actions_html; - char * next_actions_html; - char * section_template; - char * url_template; - char * sections; - char * urls; - struct map * exports = default_exports(csp, NULL); - struct map * section_exports; - struct map * url_exports; - int urlid; - char buf[50]; - char * s; - int url_1_2; + /** Next entry in the linked list */ + struct file_line * next; + + /** The raw data, to write out if this line is unmodified. */ + char * raw; - if (((fl = csp->actions_list) == NULL) || ((actions = fl->f) == NULL)) + /** Comments and/or whitespace to put before this line if it's modified + and then written out. */ + char * prefix; + + /** The actual data, as a string. Line continuation and comment removal + are performed on the data read from file before it's stored here, so + it will be a single line of data. */ + char * unprocessed; + + /** The type of data on this line. One of the FILE_LINE_xxx constants. */ + int type; + + /** The actual data, processed into some sensible data type. */ + union { - /* FIXME: Oops, no file to edit */ - free_map(exports); - return cgi_default(csp, rsp, parameters); - } - /* Should do all global exports above this point */ + /** An action specification. */ + struct action_spec action[1]; - section_template = template_load(csp, "edit-actions-list-section"); - url_template = template_load(csp, "edit-actions-list-url"); + /** A name=value pair. */ + struct + { - template_fill(§ion_template, exports); - template_fill(&url_template, exports); + /** The name in the name=value pair. */ + char * name; - urlid = 0; - sections = strdup(""); + /** The value in the name=value pair, as a string. */ + char * svalue; - ++urlid; - actions = actions->next; - if (actions != NULL) - { - actions_html = actions_to_html(actions->action); - } + /** The value in the name=value pair, as an integer. */ + int ivalue; + + } setting; + + } data; + +}; + +/** This file_line has not been processed yet. */ +#define FILE_LINE_UNPROCESSED 1 + +/** This file_line is blank. Can only appear at the end of a file, due to + the way the parser works. */ +#define FILE_LINE_BLANK 2 + +/** This file_line says {{alias}}. */ +#define FILE_LINE_ALIAS_HEADER 3 + +/** This file_line defines an alias. */ +#define FILE_LINE_ALIAS_ENTRY 4 - while (actions != NULL) +/** This file_line defines an {action}. */ +#define FILE_LINE_ACTION 5 + +/** This file_line specifies a URL pattern. */ +#define FILE_LINE_URL 6 + +/** This file_line says {{settings}}. */ +#define FILE_LINE_SETTINGS_HEADER 7 + +/** This file_line is in a {{settings}} block. */ +#define FILE_LINE_SETTINGS_ENTRY 8 + +/** This file_line says {{description}}. */ +#define FILE_LINE_DESCRIPTION_HEADER 9 + +/** This file_line is in a {{description}} block. */ +#define FILE_LINE_DESCRIPTION_ENTRY 10 + +/* + * Number of file modification time mismatches + * before the CGI editor gets turned off. + */ +#define ACCEPTABLE_TIMESTAMP_MISMATCHES 3 + +/** + * A configuration file, in a format that can be edited and written back to + * disk. + */ +struct editable_file +{ + struct file_line * lines; /**< The contents of the file. A linked list of lines. */ + const char * filename; /**< Full pathname - e.g. "/etc/privoxy/wibble.action". */ + unsigned identifier; /**< The file name's position in csp->config->actions_file[]. */ + const char * version_str; /**< Last modification time, as a string. For CGI param. */ + /**< Can be used in URL without using url_param(). */ + unsigned version; /**< Last modification time - prevents chaos with + the browser's "back" button. Note that this is a + time_t cast to an unsigned. When comparing, always + cast the time_t to an unsigned, and *NOT* vice-versa. + This may lose the top few bits, but they're not + significant anyway. */ + int newline; /**< Newline convention - one of the NEWLINE_xxx constants. + Note that changing this after the file has been + read in will cause a mess. */ + struct file_line * parse_error; /**< On parse error, this is the offending line. */ + const char * parse_error_text; /**< On parse error, this is the problem. + (Statically allocated) */ +}; + +/** + * Information about the filter types. + * Used for macro replacement in cgi_edit_actions_for_url. + */ +struct filter_type_info +{ + const int multi_action_index; /**< The multi action index as defined in project.h */ + const char *macro_name; /**< Name of the macro that has to be replaced + with the prepared templates. + For example "content-filter-params" */ + const char *type; /**< Name of the filter type, + for example "server-header-filter". */ + /* XXX: check if these two can be combined. */ + const char *disable_all_option; /**< Name of the catch-all radio option that has + to be checked or unchecked for this filter type. */ + const char *disable_all_param; /**< Name of the parameter that causes all filters of + this type to be disabled. */ + const char *abbr_type; /**< Abbreviation of the filter type, usually the + first or second character capitalized */ + const char *anchor; /**< Anchor for the User Manual link, + for example "SERVER-HEADER-FILTER" */ +}; + +/* Accessed by index, keep the order in the way the FT_ macros are defined. */ +static const struct filter_type_info filter_type_info[] = +{ { - section_exports = new_map(); + ACTION_MULTI_FILTER, + "content-filter-params", "filter", + "filter-all", "filter_all", + "F", "FILTER" + }, + { + ACTION_MULTI_CLIENT_HEADER_FILTER, + "client-header-filter-params", "client-header-filter", + "client-header-filter-all", "client_header_filter_all", + "C", "CLIENT-HEADER-FILTER" + }, + { + ACTION_MULTI_SERVER_HEADER_FILTER, + "server-header-filter-params", "server-header-filter", + "server-header-filter-all", "server_header_filter_all", + "S", "SERVER-HEADER-FILTER" + }, + { + ACTION_MULTI_CLIENT_HEADER_TAGGER, + "client-header-tagger-params", "client-header-tagger", + "client-header-tagger-all", "client_header_tagger_all", + "L", "CLIENT-HEADER-TAGGER" + }, + { + ACTION_MULTI_SERVER_HEADER_TAGGER, + "server-header-tagger-params", "server-header-tagger", + "server-header-tagger-all", "server_header_tagger_all", + "E", "SERVER-HEADER-TAGGER" + }, +#ifdef FEATURE_EXTERNAL_FILTERS + { + ACTION_MULTI_EXTERNAL_FILTER, + "external-content-filter-params", "external-filter", + "external-content-filter-all", "external_content_filter_all", + "E", "EXTERNAL-CONTENT-FILTER" + }, +#endif +}; - snprintf(buf, 50, "%d", urlid); - map(section_exports, "sectionid", 1, buf, 1); +/* FIXME: Following non-static functions should be prototyped in .h or made static */ - map(section_exports, "actions", 1, actions_html, 1); +/* Functions to read and write arbitrary config files */ +jb_err edit_read_file(struct client_state *csp, + const struct map *parameters, + int require_version, + struct editable_file **pfile); +jb_err edit_write_file(struct editable_file * file); +void edit_free_file(struct editable_file * file); - /* Should do all section-specific exports above this point */ +/* Functions to read and write actions files */ +jb_err edit_parse_actions_file(struct editable_file * file); +jb_err edit_read_actions_file(struct client_state *csp, + struct http_response *rsp, + const struct map *parameters, + int require_version, + struct editable_file **pfile); - urls = strdup(""); - url_1_2 = 2; +/* Error handlers */ +jb_err cgi_error_modified(struct client_state *csp, + struct http_response *rsp, + const char *filename); +jb_err cgi_error_parse(struct client_state *csp, + struct http_response *rsp, + struct editable_file *file); +jb_err cgi_error_file(struct client_state *csp, + struct http_response *rsp, + const char *filename); +jb_err cgi_error_file_read_only(struct client_state *csp, + struct http_response *rsp, + const char *filename); - next_actions_html = NULL; - do - { - freez(next_actions_html); +/* Internal arbitrary config file support functions */ +static jb_err edit_read_file_lines(FILE *fp, struct file_line ** pfile, int *newline); +static void edit_free_file_lines(struct file_line * first_line); - url_exports = new_map(); +/* Internal actions file support functions */ +static int match_actions_file_header_line(const char * line, const char * name); +static jb_err split_line_on_equals(const char * line, char ** pname, char ** pvalue); - snprintf(buf, 50, "%d", urlid); - map(url_exports, "urlid", 1, buf, 1); +/* Internal parameter parsing functions */ +static jb_err get_url_spec_param(struct client_state *csp, + const struct map *parameters, + const char *name, + char **pvalue); - snprintf(buf, 50, "%d", url_1_2); - map(url_exports, "url-1-2", 1, buf, 1); - s = html_encode(actions->url->spec); - map(url_exports, "url", 1, s, 1); +/* Internal actionsfile <==> HTML conversion functions */ +static jb_err map_radio(struct map * exports, + const char * optionname, + const char * values, + int value); +static jb_err actions_to_radio(struct map * exports, + const struct action_spec *action); +static jb_err actions_from_radio(const struct map * parameters, + struct action_spec *action); - s = strdup(url_template); - template_fill(&s, section_exports); - template_fill(&s, url_exports); - urls = strsav(urls, s); - free_map(url_exports); - ++urlid; - url_1_2 = 3 - url_1_2; - actions = actions->next; - if (actions) - { - next_actions_html = actions_to_html(actions->action); - } - } - while (actions && (0 == strcmp(actions_html, next_actions_html))); +static jb_err map_copy_parameter_html(struct map *out, + const struct map *in, + const char *name); - map(section_exports, "urls", 1, urls, 0); +static jb_err get_file_name_param(struct client_state *csp, + const struct map *parameters, + const char *param_name, + const char **pfilename); - /* Could also do section-specific exports here, but it wouldn't be as fast */ +/* Internal convenience functions */ +static char *section_target(const unsigned sectionid); - s = strdup(section_template); - template_fill(&s, section_exports); - sections = strsav(sections, s); - free_map(section_exports); +/********************************************************************* + * + * Function : section_target + * + * Description : Given an unsigned (section id) n, produce a dynamically + * allocated string of the form #l, for use in link + * targets. + * + * XXX: The hash should be moved into the templates + * to make this function more generic and render + * stringify() obsolete. + * + * Parameters : + * 1 : sectionid = start line number of section + * + * Returns : String with link target, or NULL if out of + * memory + * + *********************************************************************/ +static char *section_target(const unsigned sectionid) +{ + char buf[30]; - freez(actions_html); - actions_html = next_actions_html; - } + snprintf(buf, sizeof(buf), "#l%u", sectionid); + return(strdup(buf)); - map(exports, "sections", 1, sections, 0); +} - /* Could also do global exports here, but it wouldn't be as fast */ - rsp->body = template_load(csp, "edit-actions-list"); - template_fill(&rsp->body, exports); - free_map(exports); +/********************************************************************* + * + * Function : stringify + * + * Description : Convert a number into a dynamically allocated string. + * + * Parameters : + * 1 : number = The number to convert. + * + * Returns : String with link target, or NULL if out of memory + * + *********************************************************************/ +static char *stringify(const unsigned number) +{ + char buf[6]; - return(0); + snprintf(buf, sizeof(buf), "%u", number); + return strdup(buf); } /********************************************************************* * - * Function : map_radio + * Function : map_copy_parameter_html * - * Description : Map a set of radio button values. E.g. if you have - * 3 radio buttons, declare them as: - *