From 3fb1f03ddb3407a710371dcfed1494363aace957 Mon Sep 17 00:00:00 2001 From: Fabian Keil Date: Wed, 6 Dec 2006 19:41:40 +0000 Subject: [PATCH] Privoxy is now able to run as intercepting proxy in combination with any packet filter that does the port redirection. The destination is extracted from the "Host:" header which should be available for nearly all requests. Moved HTTP snipplets into jcc.c. Added error message for gopher proxy requests. --- jcc.c | 192 +++++++++++++++++++++++++++++++++++++++++++++------------- jcc.h | 22 ++++++- 2 files changed, 171 insertions(+), 43 deletions(-) diff --git a/jcc.c b/jcc.c index 911c5059..369d35a0 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.107 2006/11/13 19:05:51 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.108 2006/11/28 15:38:51 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,13 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.107 2006/11/13 19:05:51 fabiankeil Exp $" * * Revisions : * $Log: jcc.c,v $ + * Revision 1.108 2006/11/28 15:38:51 fabiankeil + * Only unlink the pidfile if it's actually used. + * + * Change order of interception checks to make + * it possible to block or redirect requests for + * the cgi pages. + * * Revision 1.107 2006/11/13 19:05:51 fabiankeil * Make pthread mutex locking more generic. Instead of * checking for OSX and OpenBSD, check for FEATURE_PTHREAD @@ -841,6 +848,36 @@ static const char VANILLA_WAFER[] = "Take_notice_that_I_refuse_to_be_bound_by_any_license_condition_" "(copyright_or_otherwise)_applying_to_any_cookie._"; +/* HTTP snipplets. */ +static const char CSUCCEED[] = + "HTTP/1.0 200 Connection established\n" + "Proxy-Agent: Privoxy/" VERSION "\r\n\r\n"; + +static const char CHEADER[] = + "HTTP/1.0 400 Invalid header received from browser\r\n" + "Connection: close\r\n\r\n" + "Invalid header received from browser."; + +static const char CFORBIDDEN[] = + "HTTP/1.0 403 Connection not allowable\r\n" + "X-Hint: If you read this message interactively, then you know why this happens ,-)\r\n" + "Connection: close\r\n\r\n"; + +static const char FTP_RESPONSE[] = + "HTTP/1.0 400 Invalid request received from browser\r\n" + "Connection: close\r\n\r\n" + "Invalid request. Privoxy doesn't support FTP.\r\n"; + +static const char GOPHER_RESPONSE[] = + "HTTP/1.0 400 Invalid request received from browser\r\n" + "Connection: close\r\n\r\n" + "Invalid request. Privoxy doesn't support gopher.\r\n"; + +static const char MISSING_DESTINATION_RESPONSE[] = + "HTTP/1.0 400 Bad request received from browser\r\n" + "Connection: close\r\n\r\n" + "Bad request. Privoxy was unable to extract the destination.\r\n"; + #if !defined(_WIN32) && !defined(__OS2__) && !defined(AMIGA) /********************************************************************* @@ -964,6 +1001,10 @@ static void chat(struct client_state *csp) /* Skeleton for HTTP response, if we should intercept the request */ struct http_response *rsp; + /* Temporary copy of the client's headers before they get enlisted in csp->headers */ + struct list header_list; + struct list *headers = &header_list; + http = csp->http; /* @@ -998,6 +1039,38 @@ static void chat(struct client_state *csp) continue; /* more to come! */ } + /* + * If it's a FTP or gopher request, we don't support it. + * + * These checks are better than nothing, but they might + * not work in all configurations and some clients might + * have problems digesting the answer. + * + * They should, however, never cause more problems than + * Privoxy's old behaviour (returning the misleading HTML error message: + * "Could not resolve http://(ftp|gopher)://example.org"). + */ + if (!strncmpic(req, "GET ftp://", 10) || !strncmpic(req, "GET gopher://", 13)) + { + if (!strncmpic(req, "GET ftp://", 10)) + { + strcpy(buf, FTP_RESPONSE); + log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as FTP proxy: %s", + csp->ip_addr_str, req); + } + else + { + strcpy(buf, GOPHER_RESPONSE); + log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as gopher proxy: %s", + csp->ip_addr_str, req); + } + log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, req); + freez(req); + write_socket(csp->cfd, buf, strlen(buf)); + free_http_request(http); + return; + } + #ifdef FEATURE_FORCE_LOAD /* If this request contains the FORCE_PREFIX, * better get rid of it now and set the force flag --oes @@ -1012,7 +1085,16 @@ static void chat(struct client_state *csp) #endif /* def FEATURE_FORCE_LOAD */ - parse_http_request(req, http, csp); + switch( parse_http_request(req, http, csp) ) + { + case JB_ERR_MEMORY: + log_error(LOG_LEVEL_ERROR, "Out of memory while parsing request."); + break; + case JB_ERR_PARSE: + log_error(LOG_LEVEL_ERROR, "Couldn't parse request: %s.", req); + break; + } + freez(req); break; } @@ -1028,16 +1110,71 @@ static void chat(struct client_state *csp) return; } - if (!strncmpic(http->cmd, "GET ftp://", 10)) + /* grab the rest of the client's headers */ + init_list(headers); + for (;;) { - strcpy(buf, FTP_RESPONSE); - write_socket(csp->cfd, buf, strlen(buf)); + if ( ( ( p = get_header(csp) ) != NULL) && ( *p == '\0' ) ) + { + len = read_socket(csp->cfd, buf, sizeof(buf)); + if (len <= 0) + { + log_error(LOG_LEVEL_ERROR, "read from client failed: %E"); + return; + } + + /* + * If there is no memory left for buffering the + * request, there is nothing we can do but hang up + */ + if (add_to_iob(csp, buf, len)) + { + return; + } + continue; + } - log_error(LOG_LEVEL_ERROR, "%s tried to use Privoxy as FTP proxy: %s", - csp->ip_addr_str, http->cmd); + if (p == NULL) break; - free_http_request(http); - return; + enlist(headers, p); + freez(p); + + } + + if (http->host == NULL) + { + /* + * Intercepted or invalid request without domain + * inside the request line. Try to get it another way. + */ + if (JB_ERR_OK == get_destination_from_headers(headers, http)) + { + /* Split the domain we just got for pattern matching */ + init_domain_components(http); + } + else + { + /* We can't work without destination. Go spread the news.*/ + + req = list_to_text(headers); + chomp(req); + log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, http->cmd); + log_error(LOG_LEVEL_ERROR, + "Privoxy was unable to get the destination for %s's request:\n%s\n%s", + csp->ip_addr_str, http->cmd, req); + freez(req); + + strcpy(buf, MISSING_DESTINATION_RESPONSE); + write_socket(csp->cfd, buf, strlen(buf)); + free_http_request(http); + destroy_list(headers); + return; + } + /* + * TODO: If available, use PF's ioctl DIOCNATLOOK as last resort + * to get the destination IP address, use it as host directly + * or do a reverse DNS lookup first. + */ } /* decide how to route the HTTP request */ @@ -1188,6 +1325,9 @@ static void chat(struct client_state *csp) } enlist(csp->headers, http->cmd); + /* Append the previously read headers */ + list_append_list_unique(csp->headers, headers); + destroy_list(headers); /* * If the user has not supplied any wafers, and the user has not @@ -1211,36 +1351,6 @@ static void chat(struct client_state *csp) jpeg_inspect = ((csp->action->flags & ACTION_JPEG_INSPECT) != 0); - /* grab the rest of the client's headers */ - - for (;;) - { - if ( ( ( p = get_header(csp) ) != NULL) && ( *p == '\0' ) ) - { - len = read_socket(csp->cfd, buf, sizeof(buf)); - if (len <= 0) - { - log_error(LOG_LEVEL_ERROR, "read from client failed: %E"); - return; - } - - /* - * If there is no memory left for buffering the - * request, there is nothing we can do but hang up - */ - if (add_to_iob(csp, buf, len)) - { - return; - } - continue; - } - - if (p == NULL) break; - - enlist(csp->headers, p); - freez(p); - } - /* * We have a request. Now, check to see if we need to * intercept it, i.e. If .. @@ -2013,7 +2123,7 @@ int main(int argc, const char *argv[]) #if defined(unix) - else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0) + else if (strcmp(argv[argc_pos], "--no-daemon" ) == 0) { no_daemon = 1; } @@ -2242,7 +2352,7 @@ int main(int argc, const char *argv[]) * to the user and group ID indicated by the --user option */ write_pid_file(); - + if (NULL != pw) { if (setgid((NULL != grp) ? grp->gr_gid : pw->pw_gid)) diff --git a/jcc.h b/jcc.h index a4eb214f..9da00cf1 100644 --- a/jcc.h +++ b/jcc.h @@ -1,6 +1,6 @@ #ifndef JCC_H_INCLUDED #define JCC_H_INCLUDED -#define JCC_H_VERSION "$Id: jcc.h,v 1.17 2006/11/06 19:58:23 fabiankeil Exp $" +#define JCC_H_VERSION "$Id: jcc.h,v 1.18 2006/11/13 19:05:51 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.h,v $ @@ -8,7 +8,7 @@ * Purpose : Main file. Contains main() method, main loop, and * the main connection-handling function. * - * Copyright : Written by and Copyright (C) 2001 the SourceForge + * Copyright : Written by and Copyright (C) 2001-2006 the SourceForge * Privoxy team. http://www.privoxy.org/ * * Based on the Internet Junkbuster originally written @@ -35,6 +35,16 @@ * * Revisions : * $Log: jcc.h,v $ + * Revision 1.18 2006/11/13 19:05:51 fabiankeil + * Make pthread mutex locking more generic. Instead of + * checking for OSX and OpenBSD, check for FEATURE_PTHREAD + * and use mutex locking unless there is an _r function + * available. Better safe than sorry. + * + * Fixes "./configure --disable-pthread" and should result + * in less threading-related problems on pthread-using platforms, + * but it still doesn't fix BR#1122404. + * * Revision 1.17 2006/11/06 19:58:23 fabiankeil * Move pthread.h inclusion from jcc.c to jcc.h. * Fixes build on x86-freebsd1 (FreeBSD 5.4-RELEASE). @@ -185,6 +195,14 @@ int main(int argc, const char *argv[]); extern const char jcc_rcs[]; extern const char jcc_h_rcs[]; +/* HTTP snippets */ +extern const char CSUCCEED[]; +extern const char CHEADER[]; +extern const char CFORBIDDEN[]; +extern const char FTP_RESPONSE[]; +extern const char GOPHER_RESPONSE[]; +extern const char MISSING_DESTINATION_RESPONSE[]; + #ifdef __cplusplus } /* extern "C" */ #endif -- 2.39.2