1 /*********************************************************************
3 * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $
5 * Purpose : Main file. Contains main() method, main loop, and
6 * the main connection-handling function.
8 * Copyright : Written by and Copyright (C) 2001-2020 the
9 * Privoxy team. https://www.privoxy.org/
11 * Based on the Internet Junkbuster originally written
12 * by and Copyright (C) 1997 Anonymous Coders and
13 * Junkbusters Corporation. http://www.junkbusters.com
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 *********************************************************************/
39 #include <sys/types.h>
48 # ifndef FEATURE_PTHREAD
52 # include <winsock2.h>
55 # endif /* ndef FEATURE_PTHREAD */
60 # endif /* ndef _WIN_CONSOLE */
61 # include "w32svrapi.h"
63 #else /* ifndef _WIN32 */
65 # if !defined (__OS2__)
67 # include <sys/wait.h>
68 # endif /* ndef __OS2__ */
69 # include <sys/time.h>
70 # include <sys/stat.h>
71 # include <sys/ioctl.h>
74 #include <sys/termios.h>
85 # include <socket.h> /* BeOS has select() for sockets only. */
86 # include <OS.h> /* declarations for threads and stuff. */
89 # if defined(__EMX__) || defined(__OS2__)
90 # include <sys/select.h> /* OS/2/EMX needs a little help with select */
102 #endif /* def __GLIBC__ */
107 #warning poll() appears to be unavailable. Your platform will become unsupported in the future.
108 #endif /* HAVE_POLL */
115 #ifdef FEATURE_HTTPS_INSPECTION
121 #include "miscutil.h"
123 #include "jbsockets.h"
128 #include "urlmatch.h"
129 #ifdef FEATURE_CLIENT_TAGS
130 #include "client-tags.h"
134 struct client_states clients[1];
135 struct file_list files[1];
137 #ifdef FEATURE_STATISTICS
138 int urls_read = 0; /* total nr of urls read inc rejected */
139 int urls_rejected = 0; /* total nr of urls rejected */
140 #endif /* def FEATURE_STATISTICS */
142 #ifdef FEATURE_GRACEFUL_TERMINATION
146 #if !defined(_WIN32) && !defined(__OS2__)
147 static void sig_handler(int the_signal);
149 static int client_protocol_is_unsupported(struct client_state *csp, char *req);
150 static jb_err get_request_destination_elsewhere(struct client_state *csp, struct list *headers);
151 static jb_err get_server_headers(struct client_state *csp);
152 static const char *crunch_reason(const struct http_response *rsp);
153 static void send_crunch_response(struct client_state *csp, struct http_response *rsp);
154 static char *get_request_line(struct client_state *csp);
155 static jb_err receive_client_request(struct client_state *csp);
156 static jb_err parse_client_request(struct client_state *csp);
157 static void build_request_line(struct client_state *csp, const struct forward_spec *fwd, char **request_line);
158 static jb_err change_request_destination(struct client_state *csp);
159 static void chat(struct client_state *csp);
160 static void serve(struct client_state *csp);
161 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
162 static void usage(const char *myname);
164 static void initialize_mutexes(void);
165 static jb_socket bind_port_helper(const char *haddr, int hport, int backlog);
166 static void bind_ports_helper(struct configuration_spec *config, jb_socket sockets[]);
167 static void close_ports_helper(jb_socket sockets[]);
168 static void listen_loop(void);
169 static void serve(struct client_state *csp);
172 static int32 server_thread(void *data);
173 #endif /* def __BEOS__ */
176 #define sleep(N) Sleep(((N) * 1000))
180 #define sleep(N) DosSleep(((N) * 100))
184 int process_fuzzed_input(char *fuzz_input_type, char *fuzz_input_file);
185 void show_fuzz_usage(const char *name);
188 #ifdef MUTEX_LOCKS_AVAILABLE
190 * XXX: Does the locking stuff really belong in this file?
192 privoxy_mutex_t log_mutex;
193 privoxy_mutex_t log_init_mutex;
194 privoxy_mutex_t connection_reuse_mutex;
196 #ifdef FEATURE_HTTPS_INSPECTION
197 privoxy_mutex_t certificate_mutex;
198 privoxy_mutex_t rng_mutex;
201 #ifdef FEATURE_EXTERNAL_FILTERS
202 privoxy_mutex_t external_filter_mutex;
204 #ifdef FEATURE_CLIENT_TAGS
205 privoxy_mutex_t client_tags_mutex;
208 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R)
209 privoxy_mutex_t resolver_mutex;
210 #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */
212 #ifndef HAVE_GMTIME_R
213 privoxy_mutex_t gmtime_mutex;
214 #endif /* ndef HAVE_GMTIME_R */
216 #ifndef HAVE_LOCALTIME_R
217 privoxy_mutex_t localtime_mutex;
218 #endif /* ndef HAVE_GMTIME_R */
220 #if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM)
221 privoxy_mutex_t rand_mutex;
222 #endif /* !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM) */
224 #endif /* def MUTEX_LOCKS_AVAILABLE */
227 const char *basedir = NULL;
228 const char *pidfile = NULL;
229 static int received_hup_signal = 0;
230 #endif /* defined unix */
232 /* HTTP snipplets. */
233 static const char CSUCCEED[] =
234 "HTTP/1.1 200 Connection established\r\n\r\n";
236 static const char CHEADER[] =
237 "HTTP/1.1 400 Invalid header received from client\r\n"
238 "Content-Type: text/plain\r\n"
239 "Connection: close\r\n\r\n"
240 "Invalid header received from client.\r\n";
242 static const char FTP_RESPONSE[] =
243 "HTTP/1.1 400 Invalid request received from client\r\n"
244 "Content-Type: text/plain\r\n"
245 "Connection: close\r\n\r\n"
246 "Invalid request. Privoxy doesn't support FTP.\r\n";
248 static const char GOPHER_RESPONSE[] =
249 "HTTP/1.1 400 Invalid request received from client\r\n"
250 "Content-Type: text/plain\r\n"
251 "Connection: close\r\n\r\n"
252 "Invalid request. Privoxy doesn't support gopher.\r\n";
254 /* XXX: should be a template */
255 static const char MISSING_DESTINATION_RESPONSE[] =
256 "HTTP/1.1 400 Bad request received from client\r\n"
257 "Content-Type: text/plain\r\n"
258 "Connection: close\r\n\r\n"
259 "Bad request. Privoxy was unable to extract the destination.\r\n";
261 /* XXX: should be a template */
262 static const char INVALID_SERVER_HEADERS_RESPONSE[] =
263 "HTTP/1.1 502 Server or forwarder response invalid\r\n"
264 "Content-Type: text/plain\r\n"
265 "Connection: close\r\n\r\n"
266 "Bad response. The server or forwarder response doesn't look like HTTP.\r\n";
268 /* XXX: should be a template */
269 static const char MESSED_UP_REQUEST_RESPONSE[] =
270 "HTTP/1.1 400 Malformed request after rewriting\r\n"
271 "Content-Type: text/plain\r\n"
272 "Connection: close\r\n\r\n"
273 "Bad request. Messed up with header filters.\r\n";
275 static const char TOO_MANY_CONNECTIONS_RESPONSE[] =
276 "HTTP/1.1 503 Too many open connections\r\n"
277 "Content-Type: text/plain\r\n"
278 "Connection: close\r\n\r\n"
279 "Maximum number of open connections reached.\r\n";
281 static const char CLIENT_CONNECTION_TIMEOUT_RESPONSE[] =
282 "HTTP/1.1 504 Connection timeout\r\n"
283 "Content-Type: text/plain\r\n"
284 "Connection: close\r\n\r\n"
285 "The connection timed out because the client request didn't arrive in time.\r\n";
287 static const char CLIENT_BODY_PARSE_ERROR_RESPONSE[] =
288 "HTTP/1.1 400 Failed reading client body\r\n"
289 "Content-Type: text/plain\r\n"
290 "Connection: close\r\n\r\n"
291 "Failed parsing or buffering the chunk-encoded client body.\r\n";
293 static const char UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE[] =
294 "HTTP/1.1 417 Expecting too much\r\n"
295 "Content-Type: text/plain\r\n"
296 "Connection: close\r\n\r\n"
297 "Privoxy detected an unsupported Expect header value.\r\n";
299 /* A function to crunch a response */
300 typedef struct http_response *(*crunch_func_ptr)(struct client_state *);
302 /* Crunch function flags */
303 #define CF_NO_FLAGS 0
304 /* Cruncher applies to forced requests as well */
305 #define CF_IGNORE_FORCE 1
306 /* Crunched requests are counted for the block statistics */
307 #define CF_COUNT_AS_REJECT 2
309 /* A crunch function and its flags */
312 const crunch_func_ptr cruncher;
316 static int crunch_response_triggered(struct client_state *csp, const struct cruncher crunchers[]);
318 /* Complete list of cruncher functions */
319 static const struct cruncher crunchers_all[] = {
320 { direct_response, CF_COUNT_AS_REJECT|CF_IGNORE_FORCE},
321 { block_url, CF_COUNT_AS_REJECT },
323 { trust_url, CF_COUNT_AS_REJECT },
324 #endif /* def FEATURE_TRUST */
325 { redirect_url, CF_NO_FLAGS },
326 { dispatch_cgi, CF_IGNORE_FORCE},
330 /* Light version, used after tags are applied */
331 static const struct cruncher crunchers_light[] = {
332 { block_url, CF_COUNT_AS_REJECT },
333 { redirect_url, CF_NO_FLAGS },
339 * XXX: Don't we really mean
345 #if !defined(_WIN32) && !defined(__OS2__)
346 /*********************************************************************
348 * Function : sig_handler
350 * Description : Signal handler for different signals.
351 * Exit gracefully on TERM and INT
352 * or set a flag that will cause the errlog
353 * to be reopened by the main thread on HUP.
356 * 1 : the_signal = the signal cause this function to call
360 *********************************************************************/
361 static void sig_handler(int the_signal)
367 log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
379 received_hup_signal = 1;
385 * We shouldn't be here, unless we catch signals
386 * in main() that we can't handle here!
388 log_error(LOG_LEVEL_FATAL, "sig_handler: exiting on unexpected signal %d", the_signal);
396 /*********************************************************************
398 * Function : get_write_delay
400 * Description : Parse the delay-response parameter.
403 * 1 : csp = Current client state (buffers, headers, etc...)
405 * Returns : Number of milliseconds to delay writes.
407 *********************************************************************/
408 static unsigned int get_write_delay(const struct client_state *csp)
414 if ((csp->action->flags & ACTION_DELAY_RESPONSE) == 0)
418 newval = csp->action->string[ACTION_STRING_DELAY_RESPONSE];
420 delay = (unsigned)strtol(newval, &endptr, 0);
423 log_error(LOG_LEVEL_FATAL,
424 "Invalid delay-response{} parameter: '%s'", newval);
432 /*********************************************************************
434 * Function : client_protocol_is_unsupported
436 * Description : Checks if the client used a known unsupported
437 * protocol and deals with it by sending an error
441 * 1 : csp = Current client state (buffers, headers, etc...)
442 * 2 : req = the first request line send by the client
444 * Returns : TRUE if an error response has been generated, or
445 * FALSE if the request doesn't look invalid.
447 *********************************************************************/
448 static int client_protocol_is_unsupported(struct client_state *csp, char *req)
451 * If it's a FTP or gopher request, we don't support it.
453 * These checks are better than nothing, but they might
454 * not work in all configurations and some clients might
455 * have problems digesting the answer.
457 * They should, however, never cause more problems than
458 * Privoxy's old behaviour (returning the misleading HTML
461 * "Could not resolve http://(ftp|gopher)://example.org").
463 if (!strncmpic(req, "GET ftp://", 10) || !strncmpic(req, "GET gopher://", 13))
465 const char *response = NULL;
466 const char *protocol = NULL;
468 if (!strncmpic(req, "GET ftp://", 10))
470 response = FTP_RESPONSE;
475 response = GOPHER_RESPONSE;
478 log_error(LOG_LEVEL_ERROR,
479 "%s tried to use Privoxy as %s proxy: %s",
480 csp->ip_addr_str, protocol, req);
481 log_error(LOG_LEVEL_CLF,
482 "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, req);
485 #ifdef FEATURE_HTTPS_INSPECTION
486 if (client_use_ssl(csp))
488 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
489 (const unsigned char *)response, strlen(response),
490 get_write_delay(csp));
495 write_socket_delayed(csp->cfd, response, strlen(response),
496 get_write_delay(csp));
506 /*********************************************************************
508 * Function : client_has_unsupported_expectations
510 * Description : Checks if the client used an unsupported expectation
511 * in which case an error message is delivered.
514 * 1 : csp = Current client state (buffers, headers, etc...)
516 * Returns : TRUE if an error response has been generated, or
517 * FALSE if the request doesn't look invalid.
519 *********************************************************************/
520 static int client_has_unsupported_expectations(const struct client_state *csp)
522 if ((csp->flags & CSP_FLAG_UNSUPPORTED_CLIENT_EXPECTATION))
524 log_error(LOG_LEVEL_ERROR,
525 "Rejecting request from client %s with unsupported Expect header value",
527 log_error(LOG_LEVEL_CLF,
528 "%s - - [%T] \"%s\" 417 0", csp->ip_addr_str, csp->http->cmd);
529 write_socket_delayed(csp->cfd,
530 UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE,
531 strlen(UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE),
532 get_write_delay(csp));
542 /*********************************************************************
544 * Function : get_request_destination_elsewhere
546 * Description : If the client's request was redirected into
547 * Privoxy without the client's knowledge,
548 * the request line lacks the destination host.
550 * This function tries to get it elsewhere,
551 * provided accept-intercepted-requests is enabled.
553 * "Elsewhere" currently only means "Host: header",
554 * but in the future we may ask the redirecting
555 * packet filter to look the destination up.
557 * If the destination stays unknown, an error
558 * response is send to the client and headers
559 * are freed so that chat() can return directly.
562 * 1 : csp = Current client state (buffers, headers, etc...)
563 * 2 : headers = a header list
565 * Returns : JB_ERR_OK if the destination is now known, or
566 * JB_ERR_PARSE if it isn't.
568 *********************************************************************/
569 static jb_err get_request_destination_elsewhere(struct client_state *csp, struct list *headers)
573 if (!(csp->config->feature_flags & RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS))
575 log_error(LOG_LEVEL_ERROR, "%s's request: \'%s\' is invalid."
576 " Privoxy isn't configured to accept intercepted requests.",
577 csp->ip_addr_str, csp->http->cmd);
578 /* XXX: Use correct size */
579 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
580 csp->ip_addr_str, csp->http->cmd);
582 write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER),
583 get_write_delay(csp));
584 destroy_list(headers);
588 else if (JB_ERR_OK == get_destination_from_headers(headers, csp->http))
590 /* Split the domain we just got for pattern matching */
591 init_domain_components(csp->http);
597 /* We can't work without destination. Go spread the news.*/
599 req = list_to_text(headers);
601 /* XXX: Use correct size */
602 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
603 csp->ip_addr_str, csp->http->cmd);
604 log_error(LOG_LEVEL_ERROR,
605 "Privoxy was unable to get the destination for %s's request:\n%s\n%s",
606 csp->ip_addr_str, csp->http->cmd, req);
609 write_socket_delayed(csp->cfd, MISSING_DESTINATION_RESPONSE,
610 strlen(MISSING_DESTINATION_RESPONSE), get_write_delay(csp));
611 destroy_list(headers);
616 * TODO: If available, use PF's ioctl DIOCNATLOOK as last resort
617 * to get the destination IP address, use it as host directly
618 * or do a reverse DNS lookup first.
623 /*********************************************************************
625 * Function : get_server_headers
627 * Description : Parses server headers in iob and fills them
628 * into csp->headers so that they can later be
632 * 1 : csp = Current client state (buffers, headers, etc...)
634 * Returns : JB_ERR_OK if everything went fine, or
635 * JB_ERR_PARSE if the headers were incomplete.
637 *********************************************************************/
638 static jb_err get_server_headers(struct client_state *csp)
640 int continue_hack_in_da_house = 0;
643 while (((header = get_header(csp->iob)) != NULL) || continue_hack_in_da_house)
648 * continue hack in da house. Ignore the ending of
649 * this head and continue enlisting header lines.
650 * The reason is described below.
652 enlist(csp->headers, "");
653 continue_hack_in_da_house = 0;
656 else if (0 == strncmpic(header, "HTTP/1.1 100", 12))
659 * It's a bodyless continue response, don't
660 * stop header parsing after reaching its end.
662 * As a result Privoxy will concatenate the
663 * next response's head and parse and deliver
664 * the headers as if they belonged to one request.
666 * The client will separate them because of the
667 * empty line between them.
669 * XXX: What we're doing here is clearly against
670 * the intended purpose of the continue header,
671 * and under some conditions (HTTP/1.0 client request)
672 * it's a standard violation.
674 * Anyway, "sort of against the spec" is preferable
675 * to "always getting confused by Continue responses"
676 * (Privoxy's behaviour before this hack was added)
678 log_error(LOG_LEVEL_HEADER, "Continue hack in da house.");
679 continue_hack_in_da_house = 1;
681 else if (*header == '\0')
684 * If the header is empty, but the Continue hack
685 * isn't active, we can assume that we reached the
686 * end of the buffer before we hit the end of the
689 * Inform the caller an let it decide how to handle it.
694 if (JB_ERR_MEMORY == enlist(csp->headers, header))
697 * XXX: Should we quit the request and return a
698 * out of memory error page instead?
700 log_error(LOG_LEVEL_ERROR,
701 "Out of memory while enlisting server headers. %s lost.",
711 /*********************************************************************
713 * Function : crunch_reason
715 * Description : Translates the crunch reason code into a string.
718 * 1 : rsp = a http_response
720 * Returns : A string with the crunch reason or an error description.
722 *********************************************************************/
723 static const char *crunch_reason(const struct http_response *rsp)
725 char * reason = NULL;
730 return "Internal error while searching for crunch reason";
733 switch (rsp->crunch_reason)
736 reason = "Unsupported HTTP feature";
742 reason = "Untrusted";
745 reason = "Redirected";
751 reason = "DNS failure";
753 case FORWARDING_FAILED:
754 reason = "Forwarding failed";
757 reason = "Connection failure";
760 reason = "Out of memory (may mask other reasons)";
762 case CONNECTION_TIMEOUT:
763 reason = "Connection timeout";
766 reason = "No server data received";
769 reason = "No reason recorded";
777 /*********************************************************************
779 * Function : log_applied_actions
781 * Description : Logs the applied actions if LOG_LEVEL_ACTIONS is
785 * 1 : actions = Current action spec to log
789 *********************************************************************/
790 static void log_applied_actions(const struct current_action_spec *actions)
793 * The conversion to text requires lots of memory allocations so
794 * we only do the conversion if the user is actually interested.
796 if (debug_level_is_enabled(LOG_LEVEL_ACTIONS))
798 char *actions_as_text = actions_to_line_of_text(actions);
799 log_error(LOG_LEVEL_ACTIONS, "%s", actions_as_text);
800 freez(actions_as_text);
805 /*********************************************************************
807 * Function : send_crunch_response
809 * Description : Delivers already prepared response for
810 * intercepted requests, logs the interception
811 * and frees the response.
814 * 1 : csp = Current client state (buffers, headers, etc...)
815 * 2 : rsp = Fully prepared response. Will be freed on exit.
819 *********************************************************************/
820 static void send_crunch_response(struct client_state *csp, struct http_response *rsp)
822 const struct http_request *http = csp->http;
826 assert(rsp->head != NULL);
830 log_error(LOG_LEVEL_FATAL, "NULL response in send_crunch_response.");
834 * Extract the status code from the actual head
835 * that will be send to the client. It is the only
836 * way to get it right for all requests, including
837 * the fixed ones for out-of-memory problems.
839 * A head starts like this: 'HTTP/1.1 200...'
843 status_code[0] = rsp->head[9];
844 status_code[1] = rsp->head[10];
845 status_code[2] = rsp->head[11];
846 status_code[3] = '\0';
848 /* Log that the request was crunched and why. */
849 log_applied_actions(csp->action);
850 #ifdef FEATURE_HTTPS_INSPECTION
851 if (client_use_ssl(csp))
853 log_error(LOG_LEVEL_CRUNCH, "%s: https://%s%s", crunch_reason(rsp),
854 http->hostport, http->path);
855 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" %s %llu",
856 csp->ip_addr_str, http->gpc, http->hostport, http->path,
857 http->version, status_code, rsp->content_length);
862 log_error(LOG_LEVEL_CRUNCH, "%s: %s", crunch_reason(rsp), http->url);
863 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" %s %u",
864 csp->ip_addr_str, http->ocmd, status_code, rsp->content_length);
866 /* Write the answer to the client */
867 #ifdef FEATURE_HTTPS_INSPECTION
868 if (client_use_ssl(csp))
870 if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
871 (const unsigned char *)rsp->head, rsp->head_length,
872 get_write_delay(csp)) < 0)
873 || (ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
874 (const unsigned char *)rsp->body, rsp->content_length,
875 get_write_delay(csp)) < 0))
877 /* There is nothing we can do about it. */
878 log_error(LOG_LEVEL_CONNECT, "Couldn't deliver the error message "
879 "for %s through client socket %d using TLS/SSL",
880 http->url, csp->cfd);
886 if (write_socket_delayed(csp->cfd, rsp->head, rsp->head_length,
887 get_write_delay(csp))
888 || write_socket_delayed(csp->cfd, rsp->body, rsp->content_length,
889 get_write_delay(csp)))
891 /* There is nothing we can do about it. */
892 log_error(LOG_LEVEL_CONNECT,
893 "Couldn't deliver the error message for %s through client socket %d: %E",
894 http->url, csp->cfd);
898 /* Clean up and return */
899 if (cgi_error_memory() != rsp)
901 free_http_response(rsp);
907 /*********************************************************************
909 * Function : crunch_response_triggered
911 * Description : Checks if the request has to be crunched,
912 * and delivers the crunch response if necessary.
915 * 1 : csp = Current client state (buffers, headers, etc...)
916 * 2 : crunchers = list of cruncher functions to run
918 * Returns : TRUE if the request was answered with a crunch response
921 *********************************************************************/
922 static int crunch_response_triggered(struct client_state *csp, const struct cruncher crunchers[])
924 struct http_response *rsp = NULL;
925 const struct cruncher *c;
928 * If CGI request crunching is disabled,
929 * check the CGI dispatcher out of order to
930 * prevent unintentional blocks or redirects.
932 if (!(csp->config->feature_flags & RUNTIME_FEATURE_CGI_CRUNCHING)
933 && (NULL != (rsp = dispatch_cgi(csp))))
935 /* Deliver, log and free the interception response. */
936 send_crunch_response(csp, rsp);
937 csp->flags |= CSP_FLAG_CRUNCHED;
941 for (c = crunchers; c->cruncher != NULL; c++)
944 * Check the cruncher if either Privoxy is toggled
945 * on and the request isn't forced, or if the cruncher
946 * applies to forced requests as well.
948 if (((csp->flags & CSP_FLAG_TOGGLED_ON) &&
949 !(csp->flags & CSP_FLAG_FORCED)) ||
950 (c->flags & CF_IGNORE_FORCE))
952 rsp = c->cruncher(csp);
955 /* Deliver, log and free the interception response. */
956 send_crunch_response(csp, rsp);
957 csp->flags |= CSP_FLAG_CRUNCHED;
958 #ifdef FEATURE_STATISTICS
959 if (c->flags & CF_COUNT_AS_REJECT)
961 csp->flags |= CSP_FLAG_REJECTED;
963 #endif /* def FEATURE_STATISTICS */
974 /*********************************************************************
976 * Function : build_request_line
978 * Description : Builds the HTTP request line.
980 * If a HTTP forwarder is used it expects the whole URL,
981 * web servers only get the path.
984 * 1 : csp = Current client state (buffers, headers, etc...)
985 * 2 : fwd = The forwarding spec used for the request
986 * XXX: Should use http->fwd instead.
987 * 3 : request_line = The old request line which will be replaced.
989 * Returns : Nothing. Terminates in case of memory problems.
991 *********************************************************************/
992 static void build_request_line(struct client_state *csp, const struct forward_spec *fwd, char **request_line)
994 struct http_request *http = csp->http;
997 * Downgrade http version from 1.1 to 1.0
998 * if +downgrade action applies.
1000 if ((csp->action->flags & ACTION_DOWNGRADE)
1001 && (!strcmpic(http->version, "HTTP/1.1")))
1003 freez(http->version);
1004 http->version = strdup_or_die("HTTP/1.0");
1008 * Rebuild the request line.
1010 freez(*request_line);
1011 *request_line = strdup(http->gpc);
1012 string_append(request_line, " ");
1014 if (fwd->forward_host && fwd->type != FORWARD_WEBSERVER)
1016 string_append(request_line, http->url);
1020 string_append(request_line, http->path);
1022 string_append(request_line, " ");
1023 string_append(request_line, http->version);
1025 if (*request_line == NULL)
1027 log_error(LOG_LEVEL_FATAL, "Out of memory writing HTTP command");
1029 log_error(LOG_LEVEL_HEADER, "New HTTP Request-Line: %s", *request_line);
1033 /*********************************************************************
1035 * Function : change_request_destination
1037 * Description : Parse a (rewritten) request line and regenerate
1038 * the http request data.
1041 * 1 : csp = Current client state (buffers, headers, etc...)
1043 * Returns : Forwards the parse_http_request() return code.
1044 * Terminates in case of memory problems.
1046 *********************************************************************/
1047 static jb_err change_request_destination(struct client_state *csp)
1049 struct http_request *http = csp->http;
1052 log_error(LOG_LEVEL_REDIRECTS, "Rewrite detected: %s",
1053 csp->headers->first->str);
1054 free_http_request(http);
1055 err = parse_http_request(csp->headers->first->str, http);
1056 if (JB_ERR_OK != err)
1058 log_error(LOG_LEVEL_ERROR, "Couldn't parse rewritten request: %s.",
1059 jb_err_to_string(err));
1066 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1067 /*********************************************************************
1069 * Function : server_response_is_complete
1071 * Description : Determines whether we should stop reading
1072 * from the server socket.
1075 * 1 : csp = Current client state (buffers, headers, etc...)
1076 * 2 : content_length = Length of content received so far.
1078 * Returns : TRUE if the response is complete,
1081 *********************************************************************/
1082 static int server_response_is_complete(struct client_state *csp,
1083 unsigned long long content_length)
1085 int content_length_known = !!(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET);
1087 if (!strcmpic(csp->http->gpc, "HEAD"))
1090 * "HEAD" implies no body, we are thus expecting
1091 * no content. XXX: incomplete "list" of methods?
1093 csp->expected_content_length = 0;
1094 content_length_known = TRUE;
1095 csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1098 if (csp->http->status == 204 || csp->http->status == 304)
1101 * Expect no body. XXX: incomplete "list" of status codes?
1103 csp->expected_content_length = 0;
1104 content_length_known = TRUE;
1105 csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1108 return (content_length_known && ((0 == csp->expected_content_length)
1109 || (csp->expected_content_length <= content_length)));
1113 #ifdef FEATURE_CONNECTION_SHARING
1114 /*********************************************************************
1116 * Function : wait_for_alive_connections
1118 * Description : Waits for alive connections to timeout.
1124 *********************************************************************/
1125 static void wait_for_alive_connections(void)
1127 int connections_alive = close_unusable_connections();
1129 while (0 < connections_alive)
1131 log_error(LOG_LEVEL_CONNECT,
1132 "Waiting for %d connections to timeout.",
1135 connections_alive = close_unusable_connections();
1138 log_error(LOG_LEVEL_CONNECT, "No connections to wait for left.");
1141 #endif /* def FEATURE_CONNECTION_SHARING */
1144 /*********************************************************************
1146 * Function : save_connection_destination
1148 * Description : Remembers a connection for reuse later on.
1151 * 1 : sfd = Open socket to remember.
1152 * 2 : http = The destination for the connection.
1153 * 3 : fwd = The forwarder settings used.
1154 * 4 : server_connection = storage.
1158 *********************************************************************/
1159 void save_connection_destination(jb_socket sfd,
1160 const struct http_request *http,
1161 const struct forward_spec *fwd,
1162 struct reusable_connection *server_connection)
1164 assert(sfd != JB_INVALID_SOCKET);
1165 assert(NULL != http->host);
1167 server_connection->sfd = sfd;
1168 server_connection->host = strdup_or_die(http->host);
1169 server_connection->port = http->port;
1171 assert(NULL != fwd);
1172 assert(server_connection->gateway_host == NULL);
1173 assert(server_connection->gateway_port == 0);
1174 assert(server_connection->forwarder_type == 0);
1175 assert(server_connection->forward_host == NULL);
1176 assert(server_connection->forward_port == 0);
1178 server_connection->forwarder_type = fwd->type;
1179 if (NULL != fwd->gateway_host)
1181 server_connection->gateway_host = strdup_or_die(fwd->gateway_host);
1185 server_connection->gateway_host = NULL;
1187 server_connection->gateway_port = fwd->gateway_port;
1189 if (NULL != fwd->forward_host)
1191 server_connection->forward_host = strdup_or_die(fwd->forward_host);
1195 server_connection->forward_host = NULL;
1197 server_connection->forward_port = fwd->forward_port;
1201 /*********************************************************************
1203 * Function : verify_request_length
1205 * Description : Checks if we already got the whole client requests
1206 * and sets CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ if
1209 * Data that doesn't belong to the current request is
1210 * either thrown away to let the client retry on a clean
1211 * socket, or stashed to be dealt with after the current
1212 * request is served.
1215 * 1 : csp = Current client state (buffers, headers, etc...)
1219 *********************************************************************/
1220 static void verify_request_length(struct client_state *csp)
1222 unsigned long long buffered_request_bytes =
1223 (unsigned long long)(csp->client_iob->eod - csp->client_iob->cur);
1225 if ((csp->expected_client_content_length != 0)
1226 && (buffered_request_bytes != 0))
1228 if (csp->expected_client_content_length >= buffered_request_bytes)
1230 csp->expected_client_content_length -= buffered_request_bytes;
1231 log_error(LOG_LEVEL_CONNECT, "Reduced expected bytes to %llu "
1232 "to account for the %llu ones we already got.",
1233 csp->expected_client_content_length, buffered_request_bytes);
1237 assert(csp->client_iob->eod > csp->client_iob->cur + csp->expected_client_content_length);
1238 csp->client_iob->eod = csp->client_iob->cur + csp->expected_client_content_length;
1239 log_error(LOG_LEVEL_CONNECT, "Reducing expected bytes to 0. "
1240 "Marking the server socket tainted after throwing %llu bytes away.",
1241 buffered_request_bytes - csp->expected_client_content_length);
1242 csp->expected_client_content_length = 0;
1243 csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1246 if (csp->expected_client_content_length == 0)
1248 csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1252 if (!(csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ)
1253 && ((csp->client_iob->cur < csp->client_iob->eod)
1254 || (csp->expected_client_content_length != 0)))
1256 if (strcmpic(csp->http->gpc, "GET")
1257 && strcmpic(csp->http->gpc, "HEAD")
1258 && strcmpic(csp->http->gpc, "TRACE")
1259 && strcmpic(csp->http->gpc, "OPTIONS")
1260 && strcmpic(csp->http->gpc, "DELETE"))
1262 /* XXX: this is an incomplete hack */
1263 csp->flags &= ~CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1264 log_error(LOG_LEVEL_CONNECT, "There better be a request body.");
1268 csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1270 if ((csp->config->feature_flags & RUNTIME_FEATURE_TOLERATE_PIPELINING) == 0)
1272 csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1273 log_error(LOG_LEVEL_CONNECT,
1274 "Possible pipeline attempt detected. The connection will not "
1275 "be kept alive and we will only serve the first request.");
1276 /* Nuke the pipelined requests from orbit, just to be sure. */
1277 clear_iob(csp->client_iob);
1282 * Keep the pipelined data around for now, we'll deal with
1283 * it once we're done serving the current request.
1285 csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
1286 assert(csp->client_iob->eod >= csp->client_iob->cur);
1287 log_error(LOG_LEVEL_CONNECT, "Complete client request followed by "
1288 "%d bytes of pipelined data received.",
1289 (int)(csp->client_iob->eod - csp->client_iob->cur));
1295 csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1296 log_error(LOG_LEVEL_CONNECT, "Complete client request received.");
1299 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
1302 /*********************************************************************
1304 * Function : mark_server_socket_tainted
1306 * Description : Makes sure we don't reuse a server socket
1307 * (if we didn't read everything the server sent
1308 * us reusing the socket would lead to garbage).
1311 * 1 : csp = Current client state (buffers, headers, etc...)
1315 *********************************************************************/
1316 static void mark_server_socket_tainted(struct client_state *csp)
1319 * For consistency we always mark the server socket
1320 * tainted, however, to reduce the log noise we only
1321 * emit a log message if the server socket could have
1322 * actually been reused.
1324 if ((csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
1325 && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
1327 log_error(LOG_LEVEL_CONNECT,
1328 "Marking the server socket %d tainted.",
1329 csp->server_connection.sfd);
1331 csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1334 /*********************************************************************
1336 * Function : get_request_line
1338 * Description : Read the client request line.
1341 * 1 : csp = Current client state (buffers, headers, etc...)
1343 * Returns : Pointer to request line or NULL in case of errors.
1345 *********************************************************************/
1346 static char *get_request_line(struct client_state *csp)
1348 char buf[BUFFER_SIZE];
1349 char *request_line = NULL;
1352 memset(buf, 0, sizeof(buf));
1354 if ((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) != 0)
1357 * If there are multiple pipelined requests waiting,
1358 * the flag will be set again once the next request
1361 csp->flags &= ~CSP_FLAG_PIPELINED_REQUEST_WAITING;
1363 request_line = get_header(csp->client_iob);
1364 if ((NULL != request_line) && ('\0' != *request_line))
1366 return request_line;
1370 log_error(LOG_LEVEL_CONNECT, "No complete request line "
1371 "received yet. Continuing reading from %d.", csp->cfd);
1379 0 == (csp->flags & CSP_FLAG_FUZZED_INPUT) &&
1381 !data_is_available(csp->cfd, csp->config->socket_timeout)
1384 if (socket_is_still_alive(csp->cfd))
1386 log_error(LOG_LEVEL_CONNECT,
1387 "No request line on socket %d received in time. Timeout: %d.",
1388 csp->cfd, csp->config->socket_timeout);
1389 write_socket_delayed(csp->cfd, CLIENT_CONNECTION_TIMEOUT_RESPONSE,
1390 strlen(CLIENT_CONNECTION_TIMEOUT_RESPONSE),
1391 get_write_delay(csp));
1395 log_error(LOG_LEVEL_CONNECT,
1396 "The client side of the connection on socket %d got "
1397 "closed without sending a complete request line.", csp->cfd);
1402 len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1404 if (len <= 0) return NULL;
1407 * If there is no memory left for buffering the
1408 * request, there is nothing we can do but hang up
1410 if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1415 request_line = get_header(csp->client_iob);
1417 } while ((NULL != request_line) && ('\0' == *request_line));
1419 return request_line;
1425 CHUNK_STATUS_MISSING_DATA,
1426 CHUNK_STATUS_BODY_COMPLETE,
1427 CHUNK_STATUS_PARSE_ERROR
1431 /*********************************************************************
1433 * Function : chunked_body_is_complete
1435 * Description : Figures out whether or not a chunked body is complete.
1437 * Currently it always starts at the beginning of the
1438 * buffer which is somewhat wasteful and prevents Privoxy
1439 * from starting to forward the correctly parsed chunks
1440 * as soon as theoretically possible.
1442 * Should be modified to work with a common buffer,
1443 * and allow the caller to skip already parsed chunks.
1445 * This would allow the function to be used for unbuffered
1446 * response bodies as well.
1449 * 1 : iob = Buffer with the body to check.
1450 * 2 : length = Length of complete body
1452 * Returns : Enum with the result of the check.
1454 *********************************************************************/
1455 static enum chunk_status chunked_body_is_complete(struct iob *iob, size_t *length)
1457 unsigned int chunksize;
1463 * We need at least a single digit, followed by "\r\n",
1464 * followed by an unknown amount of data, followed by "\r\n".
1466 if (p + 5 > iob->eod)
1468 return CHUNK_STATUS_MISSING_DATA;
1470 if (sscanf(p, "%x", &chunksize) != 1)
1472 return CHUNK_STATUS_PARSE_ERROR;
1476 * We want at least a single digit, followed by "\r\n",
1477 * followed by the specified amount of data, followed by "\r\n".
1479 if (p + chunksize + 5 > iob->eod)
1481 return CHUNK_STATUS_MISSING_DATA;
1484 /* Skip chunk-size. */
1485 p = strstr(p, "\r\n");
1488 return CHUNK_STATUS_PARSE_ERROR;
1490 /* Move beyond the chunkdata. */
1493 /* There should be another "\r\n" to skip */
1494 if (memcmp(p, "\r\n", 2))
1496 return CHUNK_STATUS_PARSE_ERROR;
1499 } while (chunksize > 0U);
1501 *length = (size_t)(p - iob->cur);
1502 assert(*length <= (size_t)(iob->eod - iob->cur));
1503 assert(p <= iob->eod);
1505 return CHUNK_STATUS_BODY_COMPLETE;
1510 /*********************************************************************
1512 * Function : receive_chunked_client_request_body
1514 * Description : Read the chunk-encoded client request body.
1515 * Failures are dealt with.
1518 * 1 : csp = Current client state (buffers, headers, etc...)
1520 * Returns : JB_ERR_OK or JB_ERR_PARSE
1522 *********************************************************************/
1523 static jb_err receive_chunked_client_request_body(struct client_state *csp)
1526 enum chunk_status status;
1528 while (CHUNK_STATUS_MISSING_DATA ==
1529 (status = chunked_body_is_complete(csp->client_iob, &body_length)))
1531 char buf[BUFFER_SIZE];
1534 if (!data_is_available(csp->cfd, csp->config->socket_timeout))
1536 log_error(LOG_LEVEL_ERROR,
1537 "Timeout while waiting for the client body.");
1540 len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1543 log_error(LOG_LEVEL_ERROR, "Read the client body failed: %E");
1546 if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1551 if (status != CHUNK_STATUS_BODY_COMPLETE)
1553 write_socket_delayed(csp->cfd, CLIENT_BODY_PARSE_ERROR_RESPONSE,
1554 strlen(CLIENT_BODY_PARSE_ERROR_RESPONSE), get_write_delay(csp));
1555 log_error(LOG_LEVEL_CLF,
1556 "%s - - [%T] \"Failed reading chunked client body\" 400 0", csp->ip_addr_str);
1557 return JB_ERR_PARSE;
1559 log_error(LOG_LEVEL_CONNECT,
1560 "Chunked client body completely read. Length: %d", body_length);
1561 csp->expected_client_content_length = body_length;
1569 /*********************************************************************
1571 * Function : fuzz_chunked_transfer_encoding
1573 * Description : Treat the fuzzed input as chunked transfer encoding
1574 * to check and dechunk.
1577 * 1 : csp = Used to store the data.
1578 * 2 : fuzz_input_file = File to read the input from.
1580 * Returns : Result of dechunking
1582 *********************************************************************/
1583 extern int fuzz_chunked_transfer_encoding(struct client_state *csp, char *fuzz_input_file)
1586 size_t size = (size_t)(csp->iob->eod - csp->iob->cur);
1587 enum chunk_status status;
1589 status = chunked_body_is_complete(csp->iob, &length);
1590 if (CHUNK_STATUS_BODY_COMPLETE != status)
1592 log_error(LOG_LEVEL_INFO, "Chunked body is incomplete or invalid");
1595 return (JB_ERR_OK == remove_chunked_transfer_coding(csp->iob->cur, &size));
1600 /*********************************************************************
1602 * Function : fuzz_client_request
1604 * Description : Try to get a client request from the fuzzed input.
1607 * 1 : csp = Current client state (buffers, headers, etc...)
1608 * 2 : fuzz_input_file = File to read the input from.
1610 * Returns : Result of fuzzing.
1612 *********************************************************************/
1613 extern int fuzz_client_request(struct client_state *csp, char *fuzz_input_file)
1618 csp->ip_addr_str = "fuzzer";
1620 if (strcmp(fuzz_input_file, "-") != 0)
1622 log_error(LOG_LEVEL_FATAL,
1623 "Fuzzed client requests can currently only be read from stdin (-).");
1625 err = receive_client_request(csp);
1626 if (err != JB_ERR_OK)
1630 err = parse_client_request(csp);
1631 if (err != JB_ERR_OK)
1639 #endif /* def FUZZ */
1642 #ifdef FEATURE_FORCE_LOAD
1643 /*********************************************************************
1645 * Function : force_required
1647 * Description : Checks a request line to see if it contains
1648 * the FORCE_PREFIX. If it does, it is removed
1649 * unless enforcing requests has beend disabled.
1652 * 1 : request_line = HTTP request line
1654 * Returns : TRUE if force is required, FALSE otherwise.
1656 *********************************************************************/
1657 static int force_required(const struct client_state *csp, char *request_line)
1661 p = strstr(request_line, "http://");
1665 p += strlen("http://");
1669 /* Intercepted request usually don't specify the protocol. */
1673 /* Go to the beginning of the path */
1678 * If the path is missing the request line is invalid and we
1679 * are done here. The client-visible rejection happens later on.
1684 if (0 == strncmpic(p, FORCE_PREFIX, strlen(FORCE_PREFIX) - 1))
1686 if (!(csp->config->feature_flags & RUNTIME_FEATURE_ENFORCE_BLOCKS))
1688 /* XXX: Should clean more carefully */
1689 strclean(request_line, FORCE_PREFIX);
1690 log_error(LOG_LEVEL_FORCE,
1691 "Enforcing request: \"%s\".", request_line);
1695 log_error(LOG_LEVEL_FORCE,
1696 "Ignored force prefix in request: \"%s\".", request_line);
1702 #endif /* def FEATURE_FORCE_LOAD */
1705 /*********************************************************************
1707 * Function : receive_client_request
1709 * Description : Read the client's request (more precisely the
1710 * client headers) and answer it if necessary.
1713 * 1 : csp = Current client state (buffers, headers, etc...)
1715 * Returns : JB_ERR_OK, JB_ERR_PARSE or JB_ERR_MEMORY
1717 *********************************************************************/
1718 static jb_err receive_client_request(struct client_state *csp)
1720 char buf[BUFFER_SIZE];
1723 struct http_request *http;
1727 /* Temporary copy of the client's headers before they get enlisted in csp->headers */
1728 struct list header_list;
1729 struct list *headers = &header_list;
1731 /* We don't care if the arriving data is a valid HTTP request or not. */
1732 csp->requests_received_total++;
1736 memset(buf, 0, sizeof(buf));
1738 req = get_request_line(csp);
1741 mark_server_socket_tainted(csp);
1742 return JB_ERR_PARSE;
1744 assert(*req != '\0');
1746 if (client_protocol_is_unsupported(csp, req))
1748 return JB_ERR_PARSE;
1751 #ifdef FEATURE_FORCE_LOAD
1752 if (force_required(csp, req))
1754 csp->flags |= CSP_FLAG_FORCED;
1756 #endif /* def FEATURE_FORCE_LOAD */
1758 err = parse_http_request(req, http);
1760 if (JB_ERR_OK != err)
1762 write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER),
1763 get_write_delay(csp));
1764 /* XXX: Use correct size */
1765 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
1766 log_error(LOG_LEVEL_ERROR,
1767 "Couldn't parse request line received from %s: %s",
1768 csp->ip_addr_str, jb_err_to_string(err));
1770 free_http_request(http);
1771 return JB_ERR_PARSE;
1774 /* grab the rest of the client's headers */
1778 p = get_header(csp->client_iob);
1782 /* There are no additional headers to read. */
1789 * We didn't receive a complete header
1790 * line yet, get the rest of it.
1792 if (!data_is_available(csp->cfd, csp->config->socket_timeout))
1794 log_error(LOG_LEVEL_ERROR,
1795 "Stopped grabbing the client headers.");
1796 destroy_list(headers);
1797 return JB_ERR_PARSE;
1800 len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1803 log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
1804 destroy_list(headers);
1805 return JB_ERR_PARSE;
1808 if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1811 * If there is no memory left for buffering the
1812 * request, there is nothing we can do but hang up
1814 destroy_list(headers);
1815 return JB_ERR_MEMORY;
1820 if (!strncmpic(p, "Transfer-Encoding:", 18))
1823 * XXX: should be called through sed()
1824 * but currently can't.
1826 client_transfer_encoding(csp, &p);
1829 * We were able to read a complete
1830 * header and can finally enlist it.
1837 if (http->host == NULL)
1840 * If we still don't know the request destination,
1841 * the request is invalid or the client uses
1842 * Privoxy without its knowledge.
1844 if (JB_ERR_OK != get_request_destination_elsewhere(csp, headers))
1847 * Our attempts to get the request destination
1848 * elsewhere failed or Privoxy is configured
1849 * to only accept proxy requests.
1851 * An error response has already been send
1852 * and we're done here.
1854 return JB_ERR_PARSE;
1858 #ifdef FEATURE_CLIENT_TAGS
1859 /* XXX: If the headers were enlisted sooner, passing csp would do. */
1860 set_client_address(csp, headers);
1861 get_tag_list_for_client(csp->client_tags, csp->client_address);
1865 * Determine the actions for this URL
1867 #ifdef FEATURE_TOGGLE
1868 if (!(csp->flags & CSP_FLAG_TOGGLED_ON))
1870 /* Most compatible set of actions (i.e. none) */
1871 init_current_action(csp->action);
1874 #endif /* ndef FEATURE_TOGGLE */
1876 get_url_actions(csp, http);
1879 enlist(csp->headers, http->cmd);
1881 /* Append the previously read headers */
1882 err = list_append_list_unique(csp->headers, headers);
1883 destroy_list(headers);
1890 /*********************************************************************
1892 * Function : parse_client_request
1894 * Description : Parses the client's request and decides what to do
1897 * Note that since we're not using select() we could get
1898 * blocked here if a client connected, then didn't say
1902 * 1 : csp = Current client state (buffers, headers, etc...)
1904 * Returns : JB_ERR_OK or JB_ERR_PARSE
1906 *********************************************************************/
1907 static jb_err parse_client_request(struct client_state *csp)
1909 struct http_request *http = csp->http;
1912 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1913 if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1914 && (!strcmpic(csp->http->version, "HTTP/1.1"))
1915 && (csp->http->ssl == 0))
1917 /* Assume persistence until further notice */
1918 csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1921 if (csp->http->ssl == 0)
1924 * This whole block belongs to chat() but currently
1925 * has to be executed before sed().
1927 if (csp->flags & CSP_FLAG_CHUNKED_CLIENT_BODY)
1929 if (receive_chunked_client_request_body(csp) != JB_ERR_OK)
1931 return JB_ERR_PARSE;
1936 csp->expected_client_content_length = get_expected_content_length(csp->headers);
1938 verify_request_length(csp);
1942 csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1944 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1946 err = sed(csp, FILTER_CLIENT_HEADERS);
1947 if (JB_ERR_OK != err)
1949 log_error(LOG_LEVEL_ERROR, "Failed to parse client request from %s.",
1951 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
1952 csp->ip_addr_str, csp->http->cmd);
1953 write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER), get_write_delay(csp));
1954 return JB_ERR_PARSE;
1956 csp->flags |= CSP_FLAG_CLIENT_HEADER_PARSING_DONE;
1958 /* Check request line for rewrites. */
1959 if ((NULL == csp->headers->first->str)
1960 || (strcmp(http->cmd, csp->headers->first->str) &&
1961 (JB_ERR_OK != change_request_destination(csp))))
1964 * A header filter broke the request line - bail out.
1966 write_socket_delayed(csp->cfd, MESSED_UP_REQUEST_RESPONSE,
1967 strlen(MESSED_UP_REQUEST_RESPONSE), get_write_delay(csp));
1968 /* XXX: Use correct size */
1969 log_error(LOG_LEVEL_CLF,
1970 "%s - - [%T] \"Invalid request generated\" 500 0", csp->ip_addr_str);
1971 log_error(LOG_LEVEL_ERROR,
1972 "Invalid request line after applying header filters.");
1973 free_http_request(http);
1975 return JB_ERR_PARSE;
1978 if (client_has_unsupported_expectations(csp))
1980 return JB_ERR_PARSE;
1988 /*********************************************************************
1990 * Function : send_http_request
1992 * Description : Sends the HTTP headers from the client request
1993 * and all the body data that has already been received.
1996 * 1 : csp = Current client state (buffers, headers, etc...)
1998 * Returns : 0 on success, anything else is an error.
2000 *********************************************************************/
2001 static int send_http_request(struct client_state *csp)
2006 hdr = list_to_text(csp->headers);
2009 /* FIXME Should handle error properly */
2010 log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
2012 list_remove_all(csp->headers);
2015 * Write the client's (modified) header to the server
2016 * (along with anything else that may be in the buffer)
2018 write_failure = 0 != write_socket(csp->server_connection.sfd, hdr, strlen(hdr));
2023 log_error(LOG_LEVEL_CONNECT, "Failed sending request headers to: %s: %E",
2024 csp->http->hostport);
2026 else if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
2027 && (flush_iob(csp->server_connection.sfd, csp->client_iob, 0) < 0))
2030 log_error(LOG_LEVEL_CONNECT, "Failed sending request body to: %s: %E",
2031 csp->http->hostport);
2034 return write_failure;
2039 #ifdef FEATURE_HTTPS_INSPECTION
2040 /*********************************************************************
2042 * Function : receive_and_send_encrypted_post_data
2044 * Description : Reads remaining POST data from the client and sends
2048 * 1 : csp = Current client state (buffers, headers, etc...)
2050 * Returns : 0 on success, anything else is an error.
2052 *********************************************************************/
2053 static int receive_and_send_encrypted_post_data(struct client_state *csp)
2055 int content_length_known = csp->expected_client_content_length != 0;
2057 while (is_ssl_pending(&(csp->mbedtls_client_attr.ssl))
2058 || (content_length_known && csp->expected_client_content_length != 0))
2060 unsigned char buf[BUFFER_SIZE];
2062 int max_bytes_to_read = sizeof(buf);
2064 if (content_length_known && csp->expected_client_content_length < sizeof(buf))
2066 max_bytes_to_read = (int)csp->expected_client_content_length;
2068 log_error(LOG_LEVEL_CONNECT,
2069 "Waiting for up to %d bytes of POST data from the client.",
2071 len = ssl_recv_data(&(csp->mbedtls_client_attr.ssl), buf,
2072 (unsigned)max_bytes_to_read);
2079 /* XXX: Does this actually happen? */
2082 log_error(LOG_LEVEL_CONNECT, "Forwarding %d bytes of encrypted POST data",
2084 len = ssl_send_data(&(csp->mbedtls_server_attr.ssl), buf, (size_t)len);
2089 if (csp->expected_client_content_length != 0)
2091 if (csp->expected_client_content_length >= len)
2093 csp->expected_client_content_length -= (unsigned)len;
2095 if (csp->expected_client_content_length == 0)
2097 log_error(LOG_LEVEL_CONNECT, "Forwarded the last %d bytes", len);
2103 log_error(LOG_LEVEL_CONNECT, "Done forwarding encrypted POST data");
2110 /*********************************************************************
2112 * Function : send_https_request
2114 * Description : Sends the HTTP headers from the client request
2115 * and all the body data that has already been received.
2118 * 1 : csp = Current client state (buffers, headers, etc...)
2120 * Returns : 0 on success, anything else is an error.
2122 *********************************************************************/
2123 static int send_https_request(struct client_state *csp)
2129 hdr = list_to_text(csp->https_headers);
2132 /* FIXME Should handle error properly */
2133 log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
2135 list_remove_all(csp->https_headers);
2138 * Write the client's (modified) header to the server
2139 * (along with anything else that may be in the buffer)
2141 ret = ssl_send_data(&(csp->mbedtls_server_attr.ssl),
2142 (const unsigned char *)hdr, strlen(hdr));
2147 log_error(LOG_LEVEL_CONNECT,
2148 "Failed sending encrypted request headers to: %s: %E",
2149 csp->http->hostport);
2150 mark_server_socket_tainted(csp);
2154 if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
2155 && ((flushed = ssl_flush_socket(&(csp->mbedtls_server_attr.ssl),
2156 csp->client_iob)) < 0))
2158 log_error(LOG_LEVEL_CONNECT, "Failed sending request body to: %s: %E",
2159 csp->http->hostport);
2164 if (csp->expected_client_content_length != 0)
2166 if (csp->expected_client_content_length < flushed)
2168 log_error(LOG_LEVEL_ERROR,
2169 "Flushed %d bytes of request body while only expecting %llu",
2170 flushed, csp->expected_client_content_length);
2171 csp->expected_client_content_length = 0;
2175 log_error(LOG_LEVEL_CONNECT,
2176 "Flushed %d bytes of request body while expecting %llu",
2177 flushed, csp->expected_client_content_length);
2178 csp->expected_client_content_length -= (unsigned)flushed;
2179 if (receive_and_send_encrypted_post_data(csp))
2187 log_error(LOG_LEVEL_CONNECT,
2188 "Flushed %d bytes of request body", flushed);
2192 log_error(LOG_LEVEL_CONNECT, "Encrypted request sent");
2199 /*********************************************************************
2201 * Function : receive_encrypted_request
2203 * Description : Receives an encrypted request.
2206 * 1 : csp = Current client state (buffers, headers, etc...)
2208 * Returns : JB_ERR_OK on success,
2209 * JB_ERR_PARSE or JB_ERR_MEMORY otherwise
2211 *********************************************************************/
2212 static jb_err receive_encrypted_request(struct client_state *csp)
2214 char buf[BUFFER_SIZE];
2220 log_error(LOG_LEVEL_HEADER, "Reading encrypted headers");
2221 if (!is_ssl_pending(&(csp->mbedtls_client_attr.ssl)) &&
2222 !data_is_available(csp->cfd, csp->config->socket_timeout))
2224 log_error(LOG_LEVEL_CONNECT,
2225 "Socket %d timed out while waiting for client headers", csp->cfd);
2226 return JB_ERR_PARSE;
2228 len = ssl_recv_data(&(csp->mbedtls_client_attr.ssl),
2229 (unsigned char *)buf, sizeof(buf));
2232 return JB_ERR_PARSE;
2234 if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
2236 return JB_ERR_MEMORY;
2238 p = strstr(csp->client_iob->cur, "\r\n\r\n");
2239 } while (p == NULL);
2241 log_error(LOG_LEVEL_HEADER, "Encrypted headers received completely");
2247 /*********************************************************************
2249 * Function : process_encrypted_request
2251 * Description : Receives and parses an encrypted request.
2254 * 1 : csp = Current client state (buffers, headers, etc...)
2256 * Returns : JB_ERR_OK on success,
2257 * JB_ERR_PARSE or JB_ERR_MEMORY otherwise
2259 *********************************************************************/
2260 static jb_err process_encrypted_request(struct client_state *csp)
2265 /* Temporary copy of the client's headers before they get enlisted in csp->https_headers */
2266 struct list header_list;
2267 struct list *headers = &header_list;
2269 err = receive_encrypted_request(csp);
2270 if (err != JB_ERR_OK)
2272 /* XXX: Also used for JB_ERR_MEMORY */
2273 log_error(LOG_LEVEL_ERROR, "Failed to receive encrypted request: %s",
2274 jb_err_to_string(err));
2275 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2276 (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2280 /* We don't need get_request_line() because the whole HTTP head is buffered. */
2281 request_line = get_header(csp->client_iob);
2282 if (request_line == NULL)
2284 log_error(LOG_LEVEL_ERROR, "Failed to get the encrypted request line");
2285 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2286 (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2287 return JB_ERR_PARSE;
2289 assert(*request_line != '\0');
2291 if (client_protocol_is_unsupported(csp, request_line))
2294 * If the protocol is unsupported we're done here.
2295 * client_protocol_is_unsupported() took care of sending
2296 * the error response and logging the error message.
2298 return JB_ERR_PARSE;
2301 #ifdef FEATURE_FORCE_LOAD
2302 if (force_required(csp, request_line))
2304 csp->flags |= CSP_FLAG_FORCED;
2306 #endif /* def FEATURE_FORCE_LOAD */
2308 free_http_request(csp->http);
2310 err = parse_http_request(request_line, csp->http);
2311 /* XXX: Restore ssl setting. This is ugly */
2312 csp->http->client_ssl = 1;
2313 csp->http->server_ssl = 1;
2315 freez(request_line);
2316 if (JB_ERR_OK != err)
2318 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2319 (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2320 /* XXX: Use correct size */
2321 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
2322 log_error(LOG_LEVEL_ERROR,
2323 "Couldn't parse request line received from %s: %s",
2324 csp->ip_addr_str, jb_err_to_string(err));
2326 free_http_request(csp->http);
2327 return JB_ERR_PARSE;
2330 /* Parse the rest of the client's headers. */
2334 p = get_header(csp->client_iob);
2338 /* There are no additional headers to read. */
2345 if (JB_ERR_OK != get_destination_from_https_headers(headers, csp->http))
2348 * Our attempts to get the request destination
2351 log_error(LOG_LEVEL_ERROR,
2352 "Failed to get the encrypted request destination");
2353 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2354 (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2355 return JB_ERR_PARSE;
2358 /* Split the domain we just got for pattern matching */
2359 init_domain_components(csp->http);
2361 #ifdef FEATURE_TOGGLE
2362 if ((csp->flags & CSP_FLAG_TOGGLED_ON) != 0)
2365 /* Determine the actions for this URL */
2366 get_url_actions(csp, csp->http);
2369 enlist(csp->https_headers, csp->http->cmd);
2371 /* Append the previously read headers */
2372 err = list_append_list_unique(csp->https_headers, headers);
2373 destroy_list(headers);
2374 if (JB_ERR_OK != err)
2376 /* XXX: Send error message */
2380 /* XXX: Work around crash */
2381 csp->error_message = NULL;
2383 /* XXX: Why do this here? */
2386 err = sed_https(csp);
2387 if (JB_ERR_OK != err)
2389 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2390 (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2391 log_error(LOG_LEVEL_ERROR, "Failed to parse client request from %s.",
2393 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
2394 csp->ip_addr_str, csp->http->cmd);
2395 return JB_ERR_PARSE;
2398 log_error(LOG_LEVEL_HEADER, "Encrypted request processed");
2399 log_applied_actions(csp->action);
2400 log_error(LOG_LEVEL_REQUEST, "https://%s%s", csp->http->hostport,
2407 /*********************************************************************
2409 * Function : cgi_page_requested
2411 * Description : Checks if a request is for an internal CGI page.
2414 * 1 : host = The host requested by the client.
2416 * Returns : 1 if a CGI page has been requested, 0 otherwise
2418 *********************************************************************/
2419 static int cgi_page_requested(const char *host)
2421 if ((0 == strcmpic(host, CGI_SITE_1_HOST))
2422 || (0 == strcmpic(host, CGI_SITE_1_HOST "."))
2423 || (0 == strcmpic(host, CGI_SITE_2_HOST))
2424 || (0 == strcmpic(host, CGI_SITE_2_HOST ".")))
2436 /*********************************************************************
2438 * Function : handle_established_connection
2440 * Description : Shuffle data between client and server once the
2441 * connection has been established.
2444 * 1 : csp = Current client state (buffers, headers, etc...)
2446 * Returns : Nothing.
2448 *********************************************************************/
2449 static void handle_established_connection(struct client_state *csp)
2455 struct pollfd poll_fds[2];
2459 struct timeval timeout;
2462 int ms_iis5_hack = 0;
2463 unsigned long long byte_count = 0;
2464 struct http_request *http;
2465 long len = 0; /* for buffer sizes (and negative error codes) */
2466 int buffer_and_filter_content = 0;
2467 unsigned int write_delay;
2468 #ifdef FEATURE_HTTPS_INSPECTION
2470 int use_ssl_tunnel = 0;
2471 csp->dont_verify_certificate = 0;
2473 if (csp->http->ssl && !(csp->action->flags & ACTION_HTTPS_INSPECTION))
2475 /* Pass encrypted content without filtering. */
2480 /* Skeleton for HTTP response, if we should intercept the request */
2481 struct http_response *rsp;
2482 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2483 int watch_client_socket;
2486 csp->receive_buffer_size = csp->config->receive_buffer_size;
2487 csp->receive_buffer = zalloc(csp->receive_buffer_size + 1);
2488 if (csp->receive_buffer == NULL)
2490 log_error(LOG_LEVEL_ERROR,
2491 "Out of memory. Failed to allocate the receive buffer.");
2492 rsp = cgi_error_memory();
2493 send_crunch_response(csp, rsp);
2500 maxfd = (csp->cfd > csp->server_connection.sfd) ?
2501 csp->cfd : csp->server_connection.sfd;
2504 /* pass data between the client and server
2505 * until one or the other shuts down the connection.
2510 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2511 watch_client_socket = 0 == (csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING);
2513 write_delay = get_write_delay(csp);
2520 * FD_ZERO here seems to point to an errant macro which crashes.
2521 * So do this by hand for now...
2523 memset(&rfds,0x00,sizeof(fd_set));
2527 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2528 if (!watch_client_socket)
2530 maxfd = csp->server_connection.sfd;
2533 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2535 FD_SET(csp->cfd, &rfds);
2538 FD_SET(csp->server_connection.sfd, &rfds);
2539 #endif /* ndef HAVE_POLL */
2541 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2542 if ((csp->flags & CSP_FLAG_CHUNKED)
2543 && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
2544 && ((csp->iob->eod - csp->iob->cur) >= 5)
2545 && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
2548 * XXX: This check should be obsolete now,
2549 * but let's wait a while to be sure.
2551 log_error(LOG_LEVEL_CONNECT,
2552 "Looks like we got the last chunk together with "
2553 "the server headers but didn't detect it earlier. "
2554 "We better stop reading.");
2555 byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
2556 csp->expected_content_length = byte_count;
2557 csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
2559 if (server_body && server_response_is_complete(csp, byte_count))
2561 if (csp->expected_content_length == byte_count)
2563 log_error(LOG_LEVEL_CONNECT,
2564 "Done reading from server. Content length: %llu as expected. "
2565 "Bytes most recently read: %d.",
2570 log_error(LOG_LEVEL_CONNECT,
2571 "Done reading from server. Expected content length: %llu. "
2572 "Actual content length: %llu. Bytes most recently read: %d.",
2573 csp->expected_content_length, byte_count, len);
2577 * XXX: Should not jump around, handle_established_connection()
2578 * is complicated enough already.
2582 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
2585 poll_fds[0].fd = csp->cfd;
2586 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2587 if (!watch_client_socket)
2590 * Ignore incoming data, but still watch out
2591 * for disconnects etc. These flags are always
2592 * implied anyway but explicitly setting them
2595 poll_fds[0].events = POLLERR|POLLHUP;
2600 poll_fds[0].events = POLLIN;
2602 poll_fds[1].fd = csp->server_connection.sfd;
2603 poll_fds[1].events = POLLIN;
2604 n = poll(poll_fds, 2, csp->config->socket_timeout * 1000);
2606 timeout.tv_sec = csp->config->socket_timeout;
2607 timeout.tv_usec = 0;
2608 n = select((int)maxfd + 1, &rfds, NULL, NULL, &timeout);
2609 #endif /* def HAVE_POLL */
2611 /*server or client not responding in timeout */
2614 log_error(LOG_LEVEL_CONNECT, "Socket timeout %d reached: %s",
2615 csp->config->socket_timeout, http->url);
2616 if ((byte_count == 0) && (http->ssl == 0))
2618 send_crunch_response(csp, error_response(csp, "connection-timeout"));
2620 mark_server_socket_tainted(csp);
2621 #ifdef FEATURE_HTTPS_INSPECTION
2622 close_client_and_server_ssl_connections(csp);
2629 log_error(LOG_LEVEL_ERROR, "poll() failed!: %E");
2631 log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
2633 mark_server_socket_tainted(csp);
2634 #ifdef FEATURE_HTTPS_INSPECTION
2635 close_client_and_server_ssl_connections(csp);
2641 * This is the body of the browser's request,
2642 * just read and write it.
2644 * Receives data from browser and sends it to server
2646 * XXX: Make sure the client doesn't use pipelining
2647 * behind Privoxy's back.
2650 if ((poll_fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) != 0)
2652 log_error(LOG_LEVEL_CONNECT,
2653 "The client socket %d has become unusable while "
2654 "the server socket %d is still open.",
2655 csp->cfd, csp->server_connection.sfd);
2656 mark_server_socket_tainted(csp);
2660 if (poll_fds[0].revents != 0)
2662 if (FD_ISSET(csp->cfd, &rfds))
2663 #endif /* def HAVE_POLL*/
2665 int max_bytes_to_read = (int)csp->receive_buffer_size;
2667 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2668 if ((csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ))
2670 if (data_is_available(csp->cfd, 0))
2673 * If the next request is already waiting, we have
2674 * to stop select()ing the client socket. Otherwise
2675 * we would always return right away and get nothing
2678 watch_client_socket = 0;
2679 log_error(LOG_LEVEL_CONNECT,
2680 "Stop watching client socket %d. "
2681 "There's already another request waiting.",
2686 * If the client socket is set, but there's no data
2687 * available on the socket, the client went fishing
2688 * and continuing talking to the server makes no sense.
2690 log_error(LOG_LEVEL_CONNECT,
2691 "The client closed socket %d while "
2692 "the server socket %d is still open.",
2693 csp->cfd, csp->server_connection.sfd);
2694 mark_server_socket_tainted(csp);
2697 if (csp->expected_client_content_length != 0)
2699 if (csp->expected_client_content_length < csp->receive_buffer_size)
2701 max_bytes_to_read = (int)csp->expected_client_content_length;
2703 log_error(LOG_LEVEL_CONNECT,
2704 "Waiting for up to %d bytes from the client.",
2707 assert(max_bytes_to_read <= csp->receive_buffer_size);
2708 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2710 #ifdef FEATURE_HTTPS_INSPECTION
2711 if (client_use_ssl(csp))
2713 log_error(LOG_LEVEL_CONNECT, "Breaking with TLS/SSL.");
2717 #endif /* def FEATURE_HTTPS_INSPECTION */
2719 len = read_socket(csp->cfd, csp->receive_buffer, max_bytes_to_read);
2723 /* XXX: not sure if this is necessary. */
2724 mark_server_socket_tainted(csp);
2725 break; /* "game over, man" */
2728 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2729 if (csp->expected_client_content_length != 0)
2731 assert(len <= max_bytes_to_read);
2732 csp->expected_client_content_length -= (unsigned)len;
2733 log_error(LOG_LEVEL_CONNECT,
2734 "Expected client content length set to %llu "
2735 "after reading %d bytes.",
2736 csp->expected_client_content_length, len);
2737 if (csp->expected_client_content_length == 0)
2739 log_error(LOG_LEVEL_CONNECT,
2740 "Done reading from the client.");
2741 csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
2744 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2746 if (write_socket(csp->server_connection.sfd, csp->receive_buffer, (size_t)len))
2748 log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
2749 mark_server_socket_tainted(csp);
2757 * The server wants to talk. It could be the header or the body.
2758 * If `hdr' is null, then it's the header otherwise it's the body.
2759 * FIXME: Does `hdr' really mean `host'? No.
2762 if (poll_fds[1].revents != 0)
2764 if (FD_ISSET(csp->server_connection.sfd, &rfds))
2765 #endif /* HAVE_POLL */
2767 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2769 * If we are buffering content, we don't want to eat up to
2770 * buffer-limit bytes if the client no longer cares about them.
2771 * If we aren't buffering, however, a dead client socket will be
2772 * noticed pretty much right away anyway, so we can reduce the
2773 * overhead by skipping the check.
2775 if (buffer_and_filter_content && !socket_is_still_alive(csp->cfd))
2778 log_error(LOG_LEVEL_CONNECT,
2779 "The server still wants to talk, but the client may already have hung up on us.");
2781 log_error(LOG_LEVEL_CONNECT,
2782 "The server still wants to talk, but the client hung up on us.");
2783 mark_server_socket_tainted(csp);
2784 #ifdef FEATURE_HTTPS_INSPECTION
2785 close_client_and_server_ssl_connections(csp);
2788 #endif /* def _WIN32 */
2790 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2792 #ifdef FEATURE_HTTPS_INSPECTION
2794 * Reading data from standard or secured connection (HTTP/HTTPS)
2796 if (server_use_ssl(csp))
2798 len = ssl_recv_data(&(csp->mbedtls_server_attr.ssl),
2799 (unsigned char *)csp->receive_buffer, csp->receive_buffer_size);
2804 len = read_socket(csp->server_connection.sfd, csp->receive_buffer,
2805 (int)csp->receive_buffer_size);
2810 log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
2812 if ((http->ssl && (csp->fwd == NULL))
2813 #ifdef FEATURE_HTTPS_INSPECTION
2819 * Just hang up. We already confirmed the client's CONNECT
2820 * request with status code 200 and unencrypted content is
2821 * no longer welcome.
2823 log_error(LOG_LEVEL_ERROR,
2824 "CONNECT already confirmed. Unable to tell the client about the problem.");
2827 else if (byte_count)
2830 * Just hang up. We already transmitted the original headers
2831 * and parts of the original content and therefore missed the
2832 * chance to send an error message (without risking data corruption).
2834 * XXX: we could retry with a fancy range request here.
2836 log_error(LOG_LEVEL_ERROR, "Already forwarded the original headers. "
2837 "Unable to tell the client about the problem.");
2838 mark_server_socket_tainted(csp);
2839 #ifdef FEATURE_HTTPS_INSPECTION
2840 close_client_and_server_ssl_connections(csp);
2845 * XXX: Consider handling the cases above the same.
2847 mark_server_socket_tainted(csp);
2851 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2852 if (csp->flags & CSP_FLAG_CHUNKED)
2854 if ((len >= 5) && !memcmp(csp->receive_buffer+len-5, "0\r\n\r\n", 5))
2856 /* XXX: this is a temporary hack */
2857 log_error(LOG_LEVEL_CONNECT,
2858 "Looks like we reached the end of the last chunk. "
2859 "We better stop reading.");
2860 csp->expected_content_length = byte_count + (unsigned long long)len;
2861 csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
2865 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
2868 * This is guaranteed by allocating with zalloc_or_die()
2869 * and never (intentionally) writing to the last byte.
2871 * csp->receive_buffer_size is the size of the part of the
2872 * buffer we intentionally write to, but we actually
2873 * allocated csp->receive_buffer_size+1 bytes so the assertion
2874 * stays within the allocated range.
2876 assert(csp->receive_buffer[csp->receive_buffer_size] == '\0');
2879 * Add a trailing zero to let be able to use string operations.
2880 * XXX: do we still need this with filter_popups gone?
2882 assert(len <= csp->receive_buffer_size);
2883 csp->receive_buffer[len] = '\0';
2886 * Normally, this would indicate that we've read
2887 * as much as the server has sent us and we can
2888 * close the client connection. However, Microsoft
2889 * in its wisdom has released IIS/5 with a bug that
2890 * prevents it from sending the trailing \r\n in
2891 * a 302 redirect header (and possibly other headers).
2892 * To work around this if we've haven't parsed
2893 * a full header we'll append a trailing \r\n
2894 * and see if this now generates a valid one.
2896 * This hack shouldn't have any impacts. If we've
2897 * already transmitted the header or if this is a
2898 * SSL connection, then we won't bother with this
2899 * hack. So we only work on partially received
2900 * headers. If we append a \r\n and this still
2901 * doesn't generate a valid header, then we won't
2902 * transmit anything to the client.
2907 if (server_body || (http->ssl
2908 #ifdef FEATURE_HTTPS_INSPECTION
2914 * If we have been buffering up the document,
2915 * now is the time to apply content modification
2916 * and send the result to the client.
2918 if (buffer_and_filter_content)
2920 p = execute_content_filters(csp);
2922 * If content filtering fails, use the original
2923 * buffer and length.
2924 * (see p != NULL ? p : csp->iob->cur below)
2928 csp->content_length = (size_t)(csp->iob->eod - csp->iob->cur);
2930 #ifdef FEATURE_COMPRESSION
2931 else if ((csp->flags & CSP_FLAG_CLIENT_SUPPORTS_DEFLATE)
2932 && (csp->content_length > LOWER_LENGTH_LIMIT_FOR_COMPRESSION))
2934 char *compressed_content = compress_buffer(p,
2935 (size_t *)&csp->content_length, csp->config->compression_level);
2936 if (compressed_content != NULL)
2939 p = compressed_content;
2940 csp->flags |= CSP_FLAG_BUFFERED_CONTENT_DEFLATED;
2945 if (JB_ERR_OK != update_server_headers(csp))
2947 log_error(LOG_LEVEL_FATAL,
2948 "Failed to update server headers. after filtering.");
2951 hdr = list_to_text(csp->headers);
2954 /* FIXME Should handle error properly */
2955 log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
2958 #ifdef FEATURE_HTTPS_INSPECTION
2960 * Sending data with standard or secured connection (HTTP/HTTPS)
2962 if (client_use_ssl(csp))
2964 if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2965 (const unsigned char *)hdr, strlen(hdr),
2966 get_write_delay(csp)) < 0)
2967 || (ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2968 (const unsigned char *) ((p != NULL) ? p : csp->iob->cur),
2969 csp->content_length, get_write_delay(csp)) < 0))
2971 log_error(LOG_LEVEL_ERROR, "write modified content to "
2972 "client over TLS/SSL failed");
2975 mark_server_socket_tainted(csp);
2976 close_client_and_server_ssl_connections(csp);
2981 #endif /* def FEATURE_HTTPS_INSPECTION */
2983 if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
2984 || write_socket_delayed(csp->cfd, ((p != NULL) ? p : csp->iob->cur),
2985 (size_t)csp->content_length, write_delay))
2987 log_error(LOG_LEVEL_ERROR, "write modified content to client failed: %E");
2990 mark_server_socket_tainted(csp);
2999 break; /* "game over, man" */
3003 * This is NOT the body, so
3004 * Let's pretend the server just sent us a blank line.
3006 snprintf(csp->receive_buffer, csp->receive_buffer_size, "\r\n");
3007 len = (int)strlen(csp->receive_buffer);
3010 * Now, let the normal header parsing algorithm below do its
3011 * job. If it fails, we'll exit instead of continuing.
3018 * If we're in the body of the server document, just write it to
3019 * the client, unless we need to buffer the body for later
3020 * content-filtering.
3022 if (server_body || (http->ssl
3023 #ifdef FEATURE_HTTPS_INSPECTION
3028 if (buffer_and_filter_content)
3031 * If there is no memory left for buffering the content, or the buffer limit
3032 * has been reached, switch to non-filtering mode, i.e. make & write the
3033 * header, flush the iob and buf, and get out of the way.
3035 if (add_to_iob(csp->iob, csp->config->buffer_limit, csp->receive_buffer, len))
3040 log_error(LOG_LEVEL_INFO,
3041 "Flushing header and buffers. Stepping back from filtering.");
3043 hdr = list_to_text(csp->headers);
3047 * Memory is too tight to even generate the header.
3048 * Send our static "Out-of-memory" page.
3050 log_error(LOG_LEVEL_ERROR, "Out of memory while trying to flush.");
3051 rsp = cgi_error_memory();
3052 send_crunch_response(csp, rsp);
3053 mark_server_socket_tainted(csp);
3054 #ifdef FEATURE_HTTPS_INSPECTION
3055 close_client_and_server_ssl_connections(csp);
3059 hdrlen = strlen(hdr);
3061 #ifdef FEATURE_HTTPS_INSPECTION
3063 * Sending data with standard or secured connection (HTTP/HTTPS)
3065 if (client_use_ssl(csp))
3067 if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3068 (const unsigned char *)hdr, hdrlen, get_write_delay(csp)) < 0)
3069 || ((flushed = ssl_flush_socket(&(csp->mbedtls_client_attr.ssl),
3071 || (ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3072 (const unsigned char *)csp->receive_buffer, (size_t)len,
3073 get_write_delay(csp)) < 0))
3075 log_error(LOG_LEVEL_CONNECT,
3076 "Flush header and buffers to client failed");
3078 mark_server_socket_tainted(csp);
3079 close_client_and_server_ssl_connections(csp);
3084 #endif /* def FEATURE_HTTPS_INSPECTION */
3086 if (write_socket_delayed(csp->cfd, hdr, hdrlen, write_delay)
3087 || ((flushed = flush_iob(csp->cfd, csp->iob, write_delay)) < 0)
3088 || write_socket_delayed(csp->cfd, csp->receive_buffer, (size_t)len,
3091 log_error(LOG_LEVEL_CONNECT,
3092 "Flush header and buffers to client failed: %E");
3094 mark_server_socket_tainted(csp);
3100 * Reset the byte_count to the amount of bytes
3101 * we just flushed. len will be added a few lines below,
3102 * hdrlen doesn't matter for LOG_LEVEL_CLF.
3104 byte_count = (unsigned long long)flushed;
3106 buffer_and_filter_content = 0;
3112 #ifdef FEATURE_HTTPS_INSPECTION
3114 * Sending data with standard or secured connection (HTTP/HTTPS)
3116 if (client_use_ssl(csp))
3118 ret = ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3119 (const unsigned char *)csp->receive_buffer, (size_t)len,
3120 get_write_delay(csp));
3123 log_error(LOG_LEVEL_ERROR,
3124 "Sending data to client failed");
3125 mark_server_socket_tainted(csp);
3126 close_client_and_server_ssl_connections(csp);
3131 #endif /* def FEATURE_HTTPS_INSPECTION */
3133 if (write_socket_delayed(csp->cfd, csp->receive_buffer,
3134 (size_t)len, write_delay))
3136 log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
3137 mark_server_socket_tainted(csp);
3142 byte_count += (unsigned long long)len;
3148 * We're still looking for the end of the server's header.
3149 * Buffer up the data we just read. If that fails, there's
3150 * little we can do but send our static out-of-memory page.
3152 if (add_to_iob(csp->iob, csp->config->buffer_limit, csp->receive_buffer, len))
3154 log_error(LOG_LEVEL_ERROR, "Out of memory while looking for end of server headers.");
3155 rsp = cgi_error_memory();
3156 send_crunch_response(csp, rsp);
3157 mark_server_socket_tainted(csp);
3158 #ifdef FEATURE_HTTPS_INSPECTION
3159 close_client_and_server_ssl_connections(csp);
3164 /* Convert iob into something sed() can digest */
3165 if (JB_ERR_PARSE == get_server_headers(csp))
3170 * Well, we tried our MS IIS/5 hack and it didn't work.
3171 * The header is incomplete and there isn't anything
3172 * we can do about it.
3174 log_error(LOG_LEVEL_ERROR, "Invalid server headers. "
3175 "Applying the MS IIS5 hack didn't help.");
3176 log_error(LOG_LEVEL_CLF,
3177 "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3178 #ifdef FEATURE_HTTPS_INSPECTION
3180 * Sending data with standard or secured connection (HTTP/HTTPS)
3182 if (client_use_ssl(csp))
3184 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3185 (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3186 strlen(INVALID_SERVER_HEADERS_RESPONSE), get_write_delay(csp));
3189 #endif /* def FEATURE_HTTPS_INSPECTION */
3191 write_socket_delayed(csp->cfd,
3192 INVALID_SERVER_HEADERS_RESPONSE,
3193 strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3195 mark_server_socket_tainted(csp);
3196 #ifdef FEATURE_HTTPS_INSPECTION
3197 close_client_and_server_ssl_connections(csp);
3204 * Since we have to wait for more from the server before
3205 * we can parse the headers we just continue here.
3207 log_error(LOG_LEVEL_CONNECT,
3208 "Continuing buffering server headers from socket %d. "
3209 "Bytes most recently read: %d.", csp->cfd, len);
3216 * Account for the content bytes we
3217 * might have gotten with the headers.
3219 assert(csp->iob->eod >= csp->iob->cur);
3220 byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3223 /* Did we actually get anything? */
3224 if (NULL == csp->headers->first)
3226 if ((csp->flags & CSP_FLAG_REUSED_CLIENT_CONNECTION))
3228 log_error(LOG_LEVEL_ERROR,
3229 "No server or forwarder response received on socket %d. "
3230 "Closing client socket %d without sending data.",
3231 csp->server_connection.sfd, csp->cfd);
3232 log_error(LOG_LEVEL_CLF,
3233 "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3237 log_error(LOG_LEVEL_ERROR,
3238 "No server or forwarder response received on socket %d.",
3239 csp->server_connection.sfd);
3240 send_crunch_response(csp, error_response(csp, "no-server-data"));
3242 free_http_request(http);
3243 mark_server_socket_tainted(csp);
3244 #ifdef FEATURE_HTTPS_INSPECTION
3245 close_client_and_server_ssl_connections(csp);
3250 if (!csp->headers->first->str)
3252 log_error(LOG_LEVEL_ERROR, "header search: csp->headers->first->str == NULL, assert will be called");
3254 assert(csp->headers->first->str);
3256 if (strncmpic(csp->headers->first->str, "HTTP", 4) &&
3257 strncmpic(csp->headers->first->str, "ICY", 3))
3260 * It doesn't look like a HTTP (or Shoutcast) response:
3261 * tell the client and log the problem.
3263 if (strlen(csp->headers->first->str) > 30)
3265 csp->headers->first->str[30] = '\0';
3267 log_error(LOG_LEVEL_ERROR,
3268 "Invalid server or forwarder response. Starts with: %s",
3269 csp->headers->first->str);
3270 log_error(LOG_LEVEL_CLF,
3271 "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3272 #ifdef FEATURE_HTTPS_INSPECTION
3274 * Sending data with standard or secured connection (HTTP/HTTPS)
3276 if (client_use_ssl(csp))
3278 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3279 (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3280 strlen(INVALID_SERVER_HEADERS_RESPONSE),
3281 get_write_delay(csp));
3284 #endif /* def FEATURE_HTTPS_INSPECTION */
3286 write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3287 strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3289 free_http_request(http);
3290 mark_server_socket_tainted(csp);
3291 #ifdef FEATURE_HTTPS_INSPECTION
3292 close_client_and_server_ssl_connections(csp);
3298 * We have now received the entire server header,
3299 * filter it and send the result to the client
3301 if (JB_ERR_OK != sed(csp, FILTER_SERVER_HEADERS))
3303 log_error(LOG_LEVEL_CLF,
3304 "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3305 #ifdef FEATURE_HTTPS_INSPECTION
3307 * Sending data with standard or secured connection (HTTP/HTTPS)
3309 if (client_use_ssl(csp))
3311 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3312 (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3313 strlen(INVALID_SERVER_HEADERS_RESPONSE),
3314 get_write_delay(csp));
3319 write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3320 strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3322 free_http_request(http);
3323 mark_server_socket_tainted(csp);
3324 #ifdef FEATURE_HTTPS_INSPECTION
3325 close_client_and_server_ssl_connections(csp);
3329 hdr = list_to_text(csp->headers);
3332 /* FIXME Should handle error properly */
3333 log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
3336 if ((csp->flags & CSP_FLAG_CHUNKED)
3337 && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
3338 && ((csp->iob->eod - csp->iob->cur) >= 5)
3339 && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
3341 log_error(LOG_LEVEL_CONNECT,
3342 "Looks like we got the last chunk together with "
3343 "the server headers. We better stop reading.");
3344 byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3345 csp->expected_content_length = byte_count;
3346 csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
3349 csp->server_connection.response_received = time(NULL);
3351 if (crunch_response_triggered(csp, crunchers_light))
3354 * One of the tags created by a server-header
3355 * tagger triggered a crunch. We already
3356 * delivered the crunch response to the client
3357 * and are done here after cleaning up.
3360 mark_server_socket_tainted(csp);
3361 #ifdef FEATURE_HTTPS_INSPECTION
3362 close_client_and_server_ssl_connections(csp);
3367 /* Buffer and pcrs filter this if appropriate. */
3368 buffer_and_filter_content = content_requires_filtering(csp);
3370 if (!buffer_and_filter_content)
3373 * Write the server's (modified) header to
3374 * the client (along with anything else that
3375 * may be in the buffer). Use standard or secured
3378 #ifdef FEATURE_HTTPS_INSPECTION
3379 if (client_use_ssl(csp))
3381 if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3382 (const unsigned char *)hdr, strlen(hdr),
3383 get_write_delay(csp)) < 0)
3384 || (len = ssl_flush_socket(&(csp->mbedtls_client_attr.ssl),
3387 log_error(LOG_LEVEL_CONNECT, "Write header to client failed");
3390 * The write failed, so don't bother mentioning it
3391 * to the client... it probably can't hear us anyway.
3394 mark_server_socket_tainted(csp);
3395 #ifdef FEATURE_HTTPS_INSPECTION
3396 close_client_and_server_ssl_connections(csp);
3402 #endif /* def FEATURE_HTTPS_INSPECTION */
3404 if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
3405 || ((len = flush_iob(csp->cfd, csp->iob, write_delay)) < 0))
3407 log_error(LOG_LEVEL_ERROR,
3408 "write header to client failed");
3410 * The write failed, so don't bother mentioning it
3411 * to the client... it probably can't hear us anyway.
3414 mark_server_socket_tainted(csp);
3420 /* we're finished with the server's header */
3426 * If this was a MS IIS/5 hack then it means the server
3427 * has already closed the connection. Nothing more to read.
3432 log_error(LOG_LEVEL_ERROR,
3433 "Closed server connection detected. "
3434 "Applying the MS IIS5 hack didn't help.");
3435 log_error(LOG_LEVEL_CLF,
3436 "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3437 #ifdef FEATURE_HTTPS_INSPECTION
3439 * Sending data with standard or secured connection (HTTP/HTTPS)
3441 if (client_use_ssl(csp))
3443 ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3444 (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3445 strlen(INVALID_SERVER_HEADERS_RESPONSE),
3446 get_write_delay(csp));
3449 #endif /* def FEATURE_HTTPS_INSPECTION */
3451 write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3452 strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3454 mark_server_socket_tainted(csp);
3455 #ifdef FEATURE_HTTPS_INSPECTION
3456 close_client_and_server_ssl_connections(csp);
3463 mark_server_socket_tainted(csp);
3464 #ifdef FEATURE_HTTPS_INSPECTION
3465 close_client_and_server_ssl_connections(csp);
3467 return; /* huh? we should never get here */
3469 #ifdef FEATURE_HTTPS_INSPECTION
3470 close_client_and_server_ssl_connections(csp);
3472 if (csp->content_length == 0)
3475 * If Privoxy didn't recalculate the Content-Length,
3476 * byte_count is still correct.
3478 csp->content_length = byte_count;
3481 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3482 if ((csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
3483 && (csp->expected_content_length != byte_count))
3485 log_error(LOG_LEVEL_CONNECT,
3486 "Received %llu bytes while expecting %llu.",
3487 byte_count, csp->expected_content_length);
3488 mark_server_socket_tainted(csp);
3492 #ifdef FEATURE_HTTPS_INSPECTION
3493 if (client_use_ssl(csp))
3495 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" 200 %llu",
3496 csp->ip_addr_str, http->gpc, http->hostport, http->path,
3497 http->version, csp->content_length);
3502 log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %llu",
3503 csp->ip_addr_str, http->ocmd, csp->content_length);
3505 csp->server_connection.timestamp = time(NULL);
3509 /*********************************************************************
3513 * Description : Once a connection from the client has been accepted,
3514 * this function is called (via serve()) to handle the
3515 * main business of the communication. This function
3516 * returns after dealing with a single request. It can
3517 * be called multiple times with the same client socket
3518 * if the client is keeping the connection alive.
3520 * The decision whether or not a client connection will
3521 * be kept alive is up to the caller which also must
3522 * close the client socket when done.
3524 * FIXME: chat is nearly thousand lines long.
3528 * 1 : csp = Current client state (buffers, headers, etc...)
3530 * Returns : Nothing.
3532 *********************************************************************/
3533 static void chat(struct client_state *csp)
3535 const struct forward_spec *fwd;
3536 struct http_request *http;
3537 /* Skeleton for HTTP response, if we should intercept the request */
3538 struct http_response *rsp;
3539 #ifdef FEATURE_HTTPS_INSPECTION
3540 int use_ssl_tunnel = 0;
3545 if (receive_client_request(csp) != JB_ERR_OK)
3549 if (parse_client_request(csp) != JB_ERR_OK)
3554 /* decide how to route the HTTP request */
3555 fwd = forward_url(csp, http);
3558 log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!? This can't happen!");
3559 /* Never get here - LOG_LEVEL_FATAL causes program exit */
3563 #ifdef FEATURE_HTTPS_INSPECTION
3565 * Setting flags to use old solution with SSL tunnel and to disable
3566 * certificate verification.
3568 if (csp->http->ssl && !(csp->action->flags & ACTION_HTTPS_INSPECTION)
3569 && !cgi_page_requested(csp->http->host))
3574 if (http->ssl && csp->action->flags & ACTION_IGNORE_CERTIFICATE_ERRORS)
3576 csp->dont_verify_certificate = 1;
3581 * build the http request to send to the server
3582 * we have to do one of the following:
3584 * create = use the original HTTP request to create a new
3585 * HTTP request that has either the path component
3586 * without the http://domainspec (w/path) or the
3587 * full orininal URL (w/url)
3588 * Note that the path and/or the HTTP version may
3589 * have been altered by now.
3591 * SSL proxy = Open a socket to the host:port of the server
3592 * and create TLS/SSL connection with server and
3593 * with client. Then behave like mediator between
3594 * client and server over TLS/SSL.
3596 * SSL proxy = Pass the request unchanged if forwarding a CONNECT
3597 * with request to a parent proxy. Note that we'll be sending
3598 * forwarding the CFAIL message ourselves if connecting to the parent
3599 * fails, but we won't send a CSUCCEED message if it works,
3600 * since that would result in a double message (ours and the
3601 * parent's). After sending the request to the parent, we
3602 * must parse answer and send it to client. If connection
3603 * with server is established, we do TLS/SSL proxy. Otherwise
3604 * we send parent response to client and close connections.
3606 * here's the matrix:
3609 * +--------+--------+
3611 * 0 | create | SSL |
3612 * | w/path | proxy |
3613 * Forwarding +--------+--------+
3615 * 1 | create | proxy |
3616 * | w/url |+forward|
3617 * +--------+--------+
3621 #ifdef FEATURE_HTTPS_INSPECTION
3623 * Presetting SSL client and server flags
3625 if (http->ssl && !use_ssl_tunnel)
3627 http->client_ssl = 1;
3628 http->server_ssl = 1;
3632 http->client_ssl = 0;
3633 http->server_ssl = 0;
3637 #ifdef FEATURE_HTTPS_INSPECTION
3639 * Log the request unless we're https inspecting
3640 * in which case we don't have the path yet and
3641 * will log the request later.
3643 if (!client_use_ssl(csp))
3646 log_error(LOG_LEVEL_REQUEST, "%s%s", http->hostport, http->path);
3649 if (http->ssl && connect_port_is_forbidden(csp))
3651 const char *acceptable_connect_ports =
3652 csp->action->string[ACTION_STRING_LIMIT_CONNECT];
3653 assert(NULL != acceptable_connect_ports);
3654 log_error(LOG_LEVEL_INFO, "Request from %s marked for blocking. "
3655 "limit-connect{%s} doesn't allow CONNECT requests to %s",
3656 csp->ip_addr_str, acceptable_connect_ports, csp->http->hostport);
3657 csp->action->flags |= ACTION_BLOCK;
3659 #ifdef FEATURE_HTTPS_INSPECTION
3660 http->client_ssl = 0;
3661 http->server_ssl = 0;
3666 freez(csp->headers->first->str);
3667 build_request_line(csp, fwd, &csp->headers->first->str);
3670 * We have a request. Check if one of the crunchers wants it
3671 * unless the client wants to use TLS/SSL in which case we
3672 * haven't setup the TLS context yet and will send the crunch
3676 #ifdef FEATURE_HTTPS_INSPECTION
3677 !client_use_ssl(csp) &&
3679 crunch_response_triggered(csp, crunchers_all))
3682 * Yes. The client got the crunch response and we're done here.
3687 log_applied_actions(csp->action);
3688 if (fwd->forward_host)
3690 log_error(LOG_LEVEL_CONNECT, "via [%s]:%d to: %s",
3691 fwd->forward_host, fwd->forward_port, http->hostport);
3695 log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
3698 /* here we connect to the server, gateway, or the forwarder */
3700 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3701 if ((csp->server_connection.sfd != JB_INVALID_SOCKET)
3702 && socket_is_still_alive(csp->server_connection.sfd)
3703 && connection_destination_matches(&csp->server_connection, http, fwd))
3705 log_error(LOG_LEVEL_CONNECT,
3706 "Reusing server socket %d connected to %s. Total requests: %u.",
3707 csp->server_connection.sfd, csp->server_connection.host,
3708 csp->server_connection.requests_sent_total);
3712 if (csp->server_connection.sfd != JB_INVALID_SOCKET)
3714 #ifdef FEATURE_CONNECTION_SHARING
3715 if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
3717 remember_connection(&csp->server_connection);
3720 #endif /* def FEATURE_CONNECTION_SHARING */
3722 log_error(LOG_LEVEL_CONNECT,
3723 "Closing server socket %d connected to %s. Total requests: %u.",
3724 csp->server_connection.sfd, csp->server_connection.host,
3725 csp->server_connection.requests_sent_total);
3726 close_socket(csp->server_connection.sfd);
3728 mark_connection_closed(&csp->server_connection);
3730 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3731 #ifdef FEATURE_HTTPS_INSPECTION
3732 if (http->ssl && !use_ssl_tunnel)
3736 * Creating a SSL proxy.
3738 * By sending the CSUCCEED message we're lying to the client as
3739 * the connection hasn't actually been established yet. We don't
3740 * establish the connection until we have seen and parsed the
3741 * encrypted client headers.
3743 if (write_socket_delayed(csp->cfd, CSUCCEED,
3744 strlen(CSUCCEED), get_write_delay(csp)) != 0)
3746 log_error(LOG_LEVEL_ERROR, "Sending SUCCEED to client failed");
3750 ret = create_client_ssl_connection(csp);
3753 log_error(LOG_LEVEL_ERROR,
3754 "Failed to open a secure connection with the client");
3757 if (JB_ERR_OK != process_encrypted_request(csp))
3759 log_error(LOG_LEVEL_ERROR, "Failed to parse encrypted request.");
3760 close_client_ssl_connection(csp);
3764 * We have an encrypted request. Check if one of the crunchers now
3765 * wants it (for example because the previously invisible path was
3766 * required to match).
3768 if (crunch_response_triggered(csp, crunchers_all))
3771 * Yes. The client got the crunch response and we're done here.
3773 close_client_ssl_connection(csp);