X-Git-Url: http://www.privoxy.org/gitweb/?a=blobdiff_plain;f=cgi.c;h=66776a7ee710633fefe71d0aeae18457660e7408;hb=d1d9361c3c809ea5a065de3c80fa0bf6113182f9;hp=383c33930b7710d003e0d505894a1d168b3518f4;hpb=83a36343155a2f17d29ff1c706809bf56382a9ec;p=privoxy.git diff --git a/cgi.c b/cgi.c index 383c3393..66776a7e 100644 --- a/cgi.c +++ b/cgi.c @@ -1,4 +1,4 @@ -const char cgi_rcs[] = "$Id: cgi.c,v 1.26 2001/09/16 15:47:37 jongfoster Exp $"; +const char cgi_rcs[] = "$Id: cgi.c,v 1.36 2001/10/26 17:33:27 oes Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/cgi.c,v $ @@ -38,6 +38,60 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.26 2001/09/16 15:47:37 jongfoster Exp $"; * * Revisions : * $Log: cgi.c,v $ + * Revision 1.36 2001/10/26 17:33:27 oes + * marginal bugfix + * + * Revision 1.35 2001/10/23 21:48:19 jongfoster + * Cleaning up error handling in CGI functions - they now send back + * a HTML error page and should never cause a FATAL error. (Fixes one + * potential source of "denial of service" attacks). + * + * CGI actions file editor that works and is actually useful. + * + * Ability to toggle JunkBuster remotely using a CGI call. + * + * You can turn off both the above features in the main configuration + * file, e.g. if you are running a multi-user proxy. + * + * Revision 1.34 2001/10/18 22:22:09 david__schmidt + * Only show "Local support" on templates conditionally: + * - if either 'admin-address' or 'proxy-info-url' are uncommented in config + * - if not, no Local support section appears + * + * Revision 1.33 2001/10/14 22:28:41 jongfoster + * Fixing stupid typo. + * + * Revision 1.32 2001/10/14 22:20:18 jongfoster + * - Changes to CGI dispatching method to match CGI names exactly, + * rather than doing a prefix match. + * - No longer need to count the length of the CGI handler names by hand. + * - Adding new handler for 404 error when disptching a CGI, if none of + * the handlers match. + * - Adding new handlers for CGI actionsfile editor. + * + * Revision 1.31 2001/10/10 10:56:39 oes + * Failiure to load template now fatal. Before, the user got a hard-to-understand assertion failure from cgi.c + * + * Revision 1.30 2001/10/02 15:30:57 oes + * Introduced show-request cgi + * + * Revision 1.29 2001/09/20 15:47:44 steudten + * + * Fix BUG: Modify int size to size_t size in fill_template() + * - removes big trouble on machines where sizeof(int) != sizeof(size_t). + * + * Revision 1.28 2001/09/19 18:00:37 oes + * - Deletef time() FIXME (Can't fail under Linux either, if + * the argument is guaranteed to be in out address space, + * which it is.) + * - Fixed comments + * - Pointer notation cosmetics + * - Fixed a minor bug in template_fill(): Failiure of + * pcrs_execute() now secure. + * + * Revision 1.27 2001/09/16 17:08:54 jongfoster + * Moving simple CGI functions from cgi.c to new file cgisimple.c + * * Revision 1.26 2001/09/16 15:47:37 jongfoster * First version of CGI-based edit interface. This is very much a * work-in-progress, and you can't actually use it to edit anything @@ -211,47 +265,75 @@ const char cgi_rcs[] = "$Id: cgi.c,v 1.26 2001/09/16 15:47:37 jongfoster Exp $"; #ifdef FEATURE_CGI_EDIT_ACTIONS #include "cgiedit.h" #endif /* def FEATURE_CGI_EDIT_ACTIONS */ +#include "loadcfg.h" +/* loadcfg.h is for g_bToggleIJB only */ const char cgi_h_rcs[] = CGI_H_VERSION; -const struct cgi_dispatcher cgi_dispatcher[] = { - { "robots.txt", - 10, cgi_robots_txt, - "HIDE Sends a robots.txt file to tell robots to go away." }, +static const struct cgi_dispatcher cgi_dispatchers[] = { + { "", + cgi_default, + "Junkbuster main page" }, { "show-status", - 11, cgi_show_status, + cgi_show_status, "Show information about the current configuration" }, - { "show-url-info", - 13, cgi_show_url_info, - "Show which actions apply to a URL and why" }, { "show-version", - 12, cgi_show_version, + cgi_show_version, "Show the source code version numbers" }, - { "send-banner", - 11, cgi_send_banner, - "HIDE Send the transparent or \"Junkbuster\" gif" }, + { "show-request", + cgi_show_request, + "Show the client's request headers." }, + { "show-url-info", + cgi_show_url_info, + "Show which actions apply to a URL and why" }, + { "toggle", + cgi_toggle, + "Toggle JunkBuster on or off" }, #ifdef FEATURE_CGI_EDIT_ACTIONS - { "edit-actions-list", - 17, cgi_edit_actions_list, + { "edit-actions", + cgi_edit_actions, "Edit the actions list" }, + { "edit-actions-for-url", + cgi_edit_actions_for_url, + NULL /* Edit the actions for (a) specified URL(s) */ }, + { "edit-actions-list", + cgi_edit_actions_list, + NULL /* Edit the actions list */ }, { "edit-actions-submit", - 19, cgi_edit_actions_submit, - "HIDE Change the actions for (a) specified URL(s)" }, - { "edit-actions", - 12, cgi_edit_actions, - "HIDE Edit the actions for (a) specified URL(s)" }, + cgi_edit_actions_submit, + NULL /* Change the actions for (a) specified URL(s) */ }, + { "edit-actions-url", + cgi_edit_actions_url, + NULL /* Change a URL pattern in the actionsfile */ }, + { "edit-actions-add-url", + cgi_edit_actions_add_url, + NULL /* Add a URL pattern to the actionsfile */ }, + { "edit-actions-remove-url", + cgi_edit_actions_remove_url, + NULL /* Add a URL pattern to the actionsfile */ }, + { "edit-actions-section-remove", + cgi_edit_actions_section_remove, + NULL /* Remove a section from the actionsfile */ }, + { "edit-actions-section-add", + cgi_edit_actions_section_add, + NULL /* Remove a section from the actionsfile */ }, #endif /* def FEATURE_CGI_EDIT_ACTIONS */ - { "", - 0, cgi_default, - "Junkbuster main page" }, - { NULL, 0, NULL, NULL } + { "robots.txt", + cgi_robots_txt, + NULL /* Sends a robots.txt file to tell robots to go away. */ }, + { "send-banner", + cgi_send_banner, + NULL /* Send the transparent or \"Junkbuster\" gif */ }, + { NULL, /* NULL Indicates end of list and default page */ + cgi_error_404, + NULL /* Unknown CGI page */ } }; /* * Some images * - * Hint: You can encode your own GIFs like that: + * Hint: You can encode your own GIFs like this: * perl -e 'while (read STDIN, $c, 1) { printf("\\%.3o,", unpack("C", $c)); }' */ @@ -276,16 +358,21 @@ const char image_blank_gif_data[] = const int image_blank_gif_length = sizeof(image_blank_gif_data) - 1; +static struct http_response cgi_error_memory_response[1]; + + +static struct http_response *dispatch_known_cgi(struct client_state * csp, + const char * path); + + /********************************************************************* * * Function : dispatch_cgi * * Description : Checks if a request URL has either the magical hostname - * i.j.b or matches HOME_PAGE_URL/config/. If so, it parses - * the (rest of the) path as a cgi name plus query string, - * prepares a map that maps CGI parameter names to their values, - * initializes the http_response struct, and calls the - * relevant CGI handler function. + * i.j.b or matches HOME_PAGE_URL/config/. If so, it passes + * the (rest of the) path onto dispatch_known_cgi, which + * calls the relevant CGI handler function. * * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) @@ -295,31 +382,40 @@ const int image_blank_gif_length = sizeof(image_blank_gif_data) - 1; *********************************************************************/ struct http_response *dispatch_cgi(struct client_state *csp) { - char *argstring = NULL; - const struct cgi_dispatcher *d; - struct map *param_list; - struct http_response *rsp; + const char *host = csp->http->host; + const char *path = csp->http->path; /* * Should we intercept ? */ /* Either the host matches CGI_PREFIX_HOST ..*/ - if (0 == strcmpic(csp->http->host, CGI_PREFIX_HOST)) + if ( (0 == strcmpic(host, CGI_PREFIX_HOST)) + && (path[0] == '/') ) { - /* ..then the path will all be for us */ - argstring = csp->http->path; + /* ..then the path will all be for us. Remove leading '/' */ + path++; } - /* Or it's the host part HOME_PAGE_URL, and the path /config ? */ - else if ( (0 == strcmpic(csp->http->host, HOME_PAGE_URL + 7 )) - && (0 == strncmpic(csp->http->path,"/config", 7)) - && ((csp->http->path[7] == '/') || (csp->http->path[7] == '\0'))) + /* Or it's the host part HOME_PAGE_URL, and the path /config/ */ + else if ( (0 == strcmpic(host, HOME_PAGE_URL + 7 )) + && (0 == strncmpic(path,"/config", 7)) ) { - /* then it's everything following "/config" */ - argstring = csp->http->path + 7; + /* take everything following "/config" */ + path += 7; + if (*path == '/') + { + /* skip the forward slash after "/config" */ + path++; + } + else if (*path != '\0') + { + /* wierdness: URL is /configXXX, where XXX is some string */ + return NULL; + } } else { + /* Not a CGI */ return NULL; } @@ -327,17 +423,74 @@ struct http_response *dispatch_cgi(struct client_state *csp) * This is a CGI call. */ - /* Get mem for response or fail*/ - if (NULL == (rsp = alloc_http_response())) + return dispatch_known_cgi(csp, path); +} + + +/********************************************************************* + * + * Function : dispatch_known_cgi + * + * Description : Processes a CGI once dispatch_cgi has determined that + * it matches one of the magic prefixes. Parses the path + * as a cgi name plus query string, prepares a map that + * maps CGI parameter names to their values, initializes + * the http_response struct, and calls the relevant CGI + * handler function. + * + * Parameters : + * 1 : csp = Current client state (buffers, headers, etc...) + * 2 : path = Path of CGI, with the CGI prefix removed. + * Should not have a leading "/". + * + * Returns : http_response, or NULL on handler failure or out of + * memory. + * + *********************************************************************/ +static struct http_response *dispatch_known_cgi(struct client_state * csp, + const char * path) +{ + const struct cgi_dispatcher *d; + struct map *param_list; + struct http_response *rsp; + char *query_args_start; + char *path_copy; + jb_err err; + + if (NULL == (path_copy = strdup(path))) { - return NULL; + return cgi_error_memory(); + } + + query_args_start = path_copy; + while (*query_args_start && *query_args_start != '?') + { + query_args_start++; + } + if (*query_args_start == '?') + { + *query_args_start++ = '\0'; + } + + if (NULL == (param_list = parse_cgi_parameters(query_args_start))) + { + free(path_copy); + return cgi_error_memory(); } - /* Remove leading slash */ - if (*argstring == '/') + /* + * At this point: + * path_copy = CGI call name + * param_list = CGI params, as map + */ + + /* Get mem for response or fail*/ + if (NULL == (rsp = alloc_http_response())) { - argstring++; + free(path_copy); + free_map(param_list); + return cgi_error_memory(); } log_error(LOG_LEVEL_GPC, "%s%s cgi call", csp->http->hostport, csp->http->path); @@ -345,32 +498,30 @@ struct http_response *dispatch_cgi(struct client_state *csp) csp->ip_addr_str, csp->http->cmd); /* Find and start the right CGI function*/ - for (d = cgi_dispatcher; d->handler; d++) + for (d = cgi_dispatchers; FOREVER; d++) { - if (strncmp(argstring, d->name, d->name_length) == 0) + if ((d->name == NULL) || (strcmp(path_copy, d->name) == 0)) { - if (NULL == (param_list = - parse_cgi_parameters(argstring + d->name_length))) + err = (d->handler)(csp, rsp, param_list); + free(path_copy); + free_map(param_list); + if (err == JB_ERR_CGI_PARAMS) { - free_map(param_list); - free_http_response(rsp); - return(NULL); + err = cgi_error_bad_param(csp, rsp); + } + if (!err) + { + /* It worked */ + return finish_http_response(rsp); } - if ((d->handler)(csp, rsp, param_list)) + else { - free_map(param_list); + /* Error in handler, probably out-of-memory */ free_http_response(rsp); - return(NULL); + return cgi_error_memory(); } - - free_map(param_list); - return(finish_http_response(rsp)); } } - - /* Can't get here, since cgi_default will match all requests */ - free_http_response(rsp); - return(NULL); } @@ -382,14 +533,14 @@ struct http_response *dispatch_cgi(struct client_state *csp) * pairs and store them in a struct map list. * * Parameters : - * 1 : string = string to be parsed + * 1 : string = string to be parsed. Will be trashed. * * Returns : pointer to param list, or NULL if out of memory. * *********************************************************************/ -struct map *parse_cgi_parameters(char *argstring) +static struct map *parse_cgi_parameters(char *argstring) { - char *tmp, *p; + char *p; char *vector[BUFFER_SIZE]; int pairs, i; struct map *cgi_params; @@ -399,29 +550,22 @@ struct map *parse_cgi_parameters(char *argstring) return NULL; } - if(*argstring == '?') - { - argstring++; - } - if (NULL == (tmp = strdup(argstring))) - { - free_map(cgi_params); - return NULL; - } - - pairs = ssplit(tmp, "&", vector, SZ(vector), 1, 1); + pairs = ssplit(argstring, "&", vector, SZ(vector), 1, 1); for (i = 0; i < pairs; i++) { if ((NULL != (p = strchr(vector[i], '='))) && (*(p+1) != '\0')) { *p = '\0'; - map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0); + if (map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0)) + { + free_map(cgi_params); + return NULL; + } } } - free(tmp); - return(cgi_params); + return cgi_params; } @@ -436,43 +580,248 @@ struct map *parse_cgi_parameters(char *argstring) * Parameters : * 1 : csp = Current client state (buffers, headers, etc...) * 2 : templatename = Which template should be used for the answer - * 3 : errno = system error number + * 3 : sys_err = system error number * - * Returns : NULL if no memory, else http_response + * Returns : A http_response. If we run out of memory, this + * will be cgi_error_memory(). * *********************************************************************/ -struct http_response *error_response(struct client_state *csp, const char *templatename, int err) +struct http_response *error_response(struct client_state *csp, + const char *templatename, + int sys_err) { + jb_err err; struct http_response *rsp; struct map * exports = default_exports(csp, NULL); + if (exports == NULL) + { + return cgi_error_memory(); + } if (NULL == (rsp = alloc_http_response())) { - return NULL; + free_map(exports); + return cgi_error_memory(); } - map(exports, "host-html", 1, html_encode(csp->http->host), 0); - map(exports, "hostport", 1, csp->http->hostport, 1); - map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0); - map(exports, "path", 1, csp->http->path, 1); - map(exports, "path-html", 1, html_encode(csp->http->path), 0); - map(exports, "error", 1, safe_strerror(err), 0); - map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1); + err = map(exports, "host-html", 1, html_encode(csp->http->host), 0) + || map(exports, "hostport", 1, csp->http->hostport, 1) + || map(exports, "hostport-html", 1, html_encode(csp->http->hostport), 0) + || map(exports, "path", 1, csp->http->path, 1) + || map(exports, "path-html", 1, html_encode(csp->http->path), 0) + || map(exports, "error", 1, safe_strerror(sys_err), 0) + || map(exports, "host-ip", 1, csp->http->host_ip_addr_str, 1); - rsp->body = template_load(csp, templatename); - template_fill(&rsp->body, exports); - free_map(exports); + if (err) + { + free_map(exports); + free_http_response(rsp); + return cgi_error_memory(); + } if (!strcmp(templatename, "no-such-domain")) { - rsp->status = strdup("404 No such domain"); + rsp->status = strdup("404 No such domain"); + if (rsp->status == NULL) + { + free_map(exports); + free_http_response(rsp); + return cgi_error_memory(); + } } else if (!strcmp(templatename, "connect-failed")) { rsp->status = strdup("503 Connect failed"); + if (rsp->status == NULL) + { + free_map(exports); + free_http_response(rsp); + return cgi_error_memory(); + } + } + + err = template_fill_for_cgi(csp, templatename, exports, rsp); + if (err) + { + free_http_response(rsp); + return cgi_error_memory(); } - return(finish_http_response(rsp)); + return finish_http_response(rsp); +} + + +/********************************************************************* + * + * Function : cgi_init_error_messages + * + * Description : Call at the start of the program to initialize + * the error message used by cgi_error_memory(). + * + * Parameters : N/A + * + * Returns : N/A + * + *********************************************************************/ +void cgi_init_error_messages(void) +{ + memset(cgi_error_memory_response, '\0', sizeof(*cgi_error_memory_response)); + cgi_error_memory_response->head = + "HTTP/1.0 500 Internal JunkBuster Proxy Error\r\n" + "Content-Type: text/html\r\n" + "\r\n"; + cgi_error_memory_response->body = + "\r\n" + "
JunkBuster ran out of memory whilst processing your request.
\r\n" + "Please contact your proxy administrator, or try again later
\r\n" + "\r\n" + "\r\n"; + + cgi_error_memory_response->head_length = + strlen(cgi_error_memory_response->head); + cgi_error_memory_response->content_length = + strlen(cgi_error_memory_response->body); +} + + +/********************************************************************* + * + * Function : cgi_error_memory + * + * Description : Called if a CGI function runs out of memory. + * Returns a statically-allocated error response. + * + * Parameters : + * 1 : csp = Current client state (buffers, headers, etc...) + * 2 : rsp = http_response data structure for output + * 3 : template_name = Name of template that could not + * be loaded. + * + * Returns : JB_ERR_OK on success + * JB_ERR_MEMORY on out-of-memory error. + * + *********************************************************************/ +struct http_response *cgi_error_memory(void) +{ + /* assert that it's been initialized. */ + assert(cgi_error_memory_response->head); + + return cgi_error_memory_response; +} + + +/********************************************************************* + * + * Function : cgi_error_no_template + * + * Description : Almost-CGI function that is called if a templae + * cannot be loaded. Note this is not a true CGI, + * it takes a template name rather than a map of + * parameters. + * + * Parameters : + * 1 : csp = Current client state (buffers, headers, etc...) + * 2 : rsp = http_response data structure for output + * 3 : template_name = Name of template that could not + * be loaded. + * + * Returns : JB_ERR_OK on success + * JB_ERR_MEMORY on out-of-memory error. + * + *********************************************************************/ +jb_err cgi_error_no_template(struct client_state *csp, + struct http_response *rsp, + const char *template_name) +{ + static const char status[] = + "500 Internal JunkBuster Proxy Error"; + static const char body_prefix[] = + "\r\n" + "JunkBuster encountered an error whilst processing your request:
\r\n" + "Could not load template file ";
+ static const char body_suffix[] =
+ "
Please contact your proxy administrator.
\r\n" + "If you are the proxy administrator, please put the required file "
+ "in the (confdir)/templates
directory. The "
+ "location of the (confdir)
directory "
+ "is specified in the main JunkBuster config
"
+ "file. (It's typically the JunkBuster install directory"
+#ifndef _WIN32
+ ", or /etc/junkbuster/
"
+#endif /* ndef _WIN32 */
+ ").