5846f8a20060afba1ba6e6df05c8d03285bb716a
[privoxy.git] / jcc.c
1 /*********************************************************************
2  *
3  * File        :  $Source: /cvsroot/ijbswa/current/jcc.c,v $
4  *
5  * Purpose     :  Main file.  Contains main() method, main loop, and
6  *                the main connection-handling function.
7  *
8  * Copyright   :  Written by and Copyright (C) 2001-2020 the
9  *                Privoxy team. https://www.privoxy.org/
10  *
11  *                Based on the Internet Junkbuster originally written
12  *                by and Copyright (C) 1997 Anonymous Coders and
13  *                Junkbusters Corporation.  http://www.junkbusters.com
14  *
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.
20  *
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.
26  *
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.
32  *
33  *********************************************************************/
34
35
36 #include "config.h"
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <assert.h>
46
47 #ifdef _WIN32
48 # ifndef FEATURE_PTHREAD
49 #  ifndef STRICT
50 #   define STRICT
51 #  endif
52 #  include <winsock2.h>
53 #  include <windows.h>
54 #  include <process.h>
55 # endif /* ndef FEATURE_PTHREAD */
56
57 # include "win32.h"
58 # ifndef _WIN_CONSOLE
59 #  include "w32log.h"
60 # endif /* ndef _WIN_CONSOLE */
61 # include "w32svrapi.h"
62
63 #else /* ifndef _WIN32 */
64
65 # if !defined (__OS2__)
66 # include <unistd.h>
67 # include <sys/wait.h>
68 # endif /* ndef __OS2__ */
69 # include <sys/time.h>
70 # include <sys/stat.h>
71 # include <sys/ioctl.h>
72
73 #ifdef sun
74 #include <sys/termios.h>
75 #endif /* sun */
76
77 #ifdef unix
78 #include <pwd.h>
79 #include <grp.h>
80 #endif
81
82 # include <signal.h>
83
84 # ifdef __BEOS__
85 #  include <socket.h>  /* BeOS has select() for sockets only. */
86 #  include <OS.h>      /* declarations for threads and stuff. */
87 # endif
88
89 # if defined(__EMX__) || defined(__OS2__)
90 #  include <sys/select.h>  /* OS/2/EMX needs a little help with select */
91 # endif
92 # ifdef __OS2__
93 #define INCL_DOS
94 # include <os2.h>
95 # endif
96
97 #ifdef HAVE_POLL
98 #ifdef __GLIBC__
99 #include <sys/poll.h>
100 #else
101 #include <poll.h>
102 #endif /* def __GLIBC__ */
103 #else
104 # ifndef FD_ZERO
105 #  include <select.h>
106 # endif
107 #warning poll() appears to be unavailable. Your platform will become unsupported in the future.
108 #endif /* HAVE_POLL */
109
110 #endif
111
112 #include "project.h"
113 #include "list.h"
114 #include "jcc.h"
115 #ifdef FEATURE_HTTPS_INSPECTION
116 #include "ssl.h"
117 #endif
118 #include "filters.h"
119 #include "loaders.h"
120 #include "parsers.h"
121 #include "miscutil.h"
122 #include "errlog.h"
123 #include "jbsockets.h"
124 #include "gateway.h"
125 #include "actions.h"
126 #include "cgi.h"
127 #include "loadcfg.h"
128 #include "urlmatch.h"
129 #ifdef FEATURE_CLIENT_TAGS
130 #include "client-tags.h"
131 #endif
132
133 int daemon_mode = 1;
134 struct client_states clients[1];
135 struct file_list     files[1];
136
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 */
141
142 #ifdef FEATURE_GRACEFUL_TERMINATION
143 int g_terminate = 0;
144 #endif
145
146 #if !defined(_WIN32) && !defined(__OS2__)
147 static void sig_handler(int the_signal);
148 #endif
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);
163 #endif
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);
170
171 #ifdef __BEOS__
172 static int32 server_thread(void *data);
173 #endif /* def __BEOS__ */
174
175 #ifdef _WIN32
176 #define sleep(N)  Sleep(((N) * 1000))
177 #endif
178
179 #ifdef __OS2__
180 #define sleep(N)  DosSleep(((N) * 100))
181 #endif
182
183 #ifdef FUZZ
184 int process_fuzzed_input(char *fuzz_input_type, char *fuzz_input_file);
185 void show_fuzz_usage(const char *name);
186 #endif
187
188 #ifdef MUTEX_LOCKS_AVAILABLE
189 /*
190  * XXX: Does the locking stuff really belong in this file?
191  */
192 privoxy_mutex_t log_mutex;
193 privoxy_mutex_t log_init_mutex;
194 privoxy_mutex_t connection_reuse_mutex;
195
196 #ifdef FEATURE_HTTPS_INSPECTION
197 privoxy_mutex_t certificate_mutex;
198 privoxy_mutex_t rng_mutex;
199 #endif
200
201 #ifdef FEATURE_EXTERNAL_FILTERS
202 privoxy_mutex_t external_filter_mutex;
203 #endif
204 #ifdef FEATURE_CLIENT_TAGS
205 privoxy_mutex_t client_tags_mutex;
206 #endif
207 #ifdef FEATURE_EXTENDED_STATISTICS
208 privoxy_mutex_t filter_statistics_mutex;
209 #endif
210
211 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R)
212 privoxy_mutex_t resolver_mutex;
213 #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */
214
215 #ifndef HAVE_GMTIME_R
216 privoxy_mutex_t gmtime_mutex;
217 #endif /* ndef HAVE_GMTIME_R */
218
219 #ifndef HAVE_LOCALTIME_R
220 privoxy_mutex_t localtime_mutex;
221 #endif /* ndef HAVE_GMTIME_R */
222
223 #if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM)
224 privoxy_mutex_t rand_mutex;
225 #endif /* !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM) */
226
227 #endif /* def MUTEX_LOCKS_AVAILABLE */
228
229 #if defined(unix)
230 const char *basedir = NULL;
231 const char *pidfile = NULL;
232 static int received_hup_signal = 0;
233 #endif /* defined unix */
234
235 /* HTTP snipplets. */
236 static const char CSUCCEED[] =
237    "HTTP/1.1 200 Connection established\r\n\r\n";
238
239 static const char CHEADER[] =
240    "HTTP/1.1 400 Invalid header received from client\r\n"
241    "Content-Type: text/plain\r\n"
242    "Connection: close\r\n\r\n"
243    "Invalid header received from client.\r\n";
244
245 static const char FTP_RESPONSE[] =
246    "HTTP/1.1 400 Invalid request received from client\r\n"
247    "Content-Type: text/plain\r\n"
248    "Connection: close\r\n\r\n"
249    "Invalid request. Privoxy doesn't support FTP.\r\n";
250
251 static const char GOPHER_RESPONSE[] =
252    "HTTP/1.1 400 Invalid request received from client\r\n"
253    "Content-Type: text/plain\r\n"
254    "Connection: close\r\n\r\n"
255    "Invalid request. Privoxy doesn't support gopher.\r\n";
256
257 /* XXX: should be a template */
258 static const char MISSING_DESTINATION_RESPONSE[] =
259    "HTTP/1.1 400 Bad request received from client\r\n"
260    "Content-Type: text/plain\r\n"
261    "Connection: close\r\n\r\n"
262    "Bad request. Privoxy was unable to extract the destination.\r\n";
263
264 /* XXX: should be a template */
265 static const char INVALID_SERVER_HEADERS_RESPONSE[] =
266    "HTTP/1.1 502 Server or forwarder response invalid\r\n"
267    "Content-Type: text/plain\r\n"
268    "Connection: close\r\n\r\n"
269    "Bad response. The server or forwarder response doesn't look like HTTP.\r\n";
270
271 /* XXX: should be a template */
272 static const char MESSED_UP_REQUEST_RESPONSE[] =
273    "HTTP/1.1 400 Malformed request after rewriting\r\n"
274    "Content-Type: text/plain\r\n"
275    "Connection: close\r\n\r\n"
276    "Bad request. Messed up with header filters.\r\n";
277
278 static const char TOO_MANY_CONNECTIONS_RESPONSE[] =
279    "HTTP/1.1 503 Too many open connections\r\n"
280    "Content-Type: text/plain\r\n"
281    "Connection: close\r\n\r\n"
282    "Maximum number of open connections reached.\r\n";
283
284 static const char CLIENT_CONNECTION_TIMEOUT_RESPONSE[] =
285    "HTTP/1.1 504 Connection timeout\r\n"
286    "Content-Type: text/plain\r\n"
287    "Connection: close\r\n\r\n"
288    "The connection timed out because the client request didn't arrive in time.\r\n";
289
290 static const char CLIENT_BODY_PARSE_ERROR_RESPONSE[] =
291    "HTTP/1.1 400 Failed reading client body\r\n"
292    "Content-Type: text/plain\r\n"
293    "Connection: close\r\n\r\n"
294    "Failed parsing or buffering the chunk-encoded client body.\r\n";
295
296 static const char UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE[] =
297    "HTTP/1.1 417 Expecting too much\r\n"
298    "Content-Type: text/plain\r\n"
299    "Connection: close\r\n\r\n"
300    "Privoxy detected an unsupported Expect header value.\r\n";
301
302 /* A function to crunch a response */
303 typedef struct http_response *(*crunch_func_ptr)(struct client_state *);
304
305 /* Crunch function flags */
306 #define CF_NO_FLAGS        0
307 /* Cruncher applies to forced requests as well */
308 #define CF_IGNORE_FORCE    1
309 /* Crunched requests are counted for the block statistics */
310 #define CF_COUNT_AS_REJECT 2
311
312 /* A crunch function and its flags */
313 struct cruncher
314 {
315    const crunch_func_ptr cruncher;
316    const int flags;
317 };
318
319 static int crunch_response_triggered(struct client_state *csp, const struct cruncher crunchers[]);
320
321 /* Complete list of cruncher functions */
322 static const struct cruncher crunchers_all[] = {
323    { direct_response, CF_COUNT_AS_REJECT|CF_IGNORE_FORCE},
324    { block_url,       CF_COUNT_AS_REJECT },
325 #ifdef FEATURE_TRUST
326    { trust_url,       CF_COUNT_AS_REJECT },
327 #endif /* def FEATURE_TRUST */
328    { redirect_url,    CF_NO_FLAGS  },
329    { dispatch_cgi,    CF_IGNORE_FORCE},
330    { NULL,            0 }
331 };
332
333 /* Light version, used after tags are applied */
334 static const struct cruncher crunchers_light[] = {
335    { block_url,       CF_COUNT_AS_REJECT },
336    { redirect_url,    CF_NO_FLAGS },
337    { NULL,            0 }
338 };
339
340
341 /*
342  * XXX: Don't we really mean
343  *
344  * #if defined(unix)
345  *
346  * here?
347  */
348 #if !defined(_WIN32) && !defined(__OS2__)
349 /*********************************************************************
350  *
351  * Function    :  sig_handler
352  *
353  * Description :  Signal handler for different signals.
354  *                Exit gracefully on TERM and INT
355  *                or set a flag that will cause the errlog
356  *                to be reopened by the main thread on HUP.
357  *
358  * Parameters  :
359  *          1  :  the_signal = the signal cause this function to call
360  *
361  * Returns     :  -
362  *
363  *********************************************************************/
364 static void sig_handler(int the_signal)
365 {
366    switch(the_signal)
367    {
368       case SIGTERM:
369       case SIGINT:
370          log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
371 #if defined(unix)
372          if (pidfile)
373          {
374             unlink(pidfile);
375          }
376 #endif /* unix */
377          exit(the_signal);
378          break;
379
380       case SIGHUP:
381 #if defined(unix)
382          received_hup_signal = 1;
383 #endif
384          break;
385
386       default:
387          /*
388           * We shouldn't be here, unless we catch signals
389           * in main() that we can't handle here!
390           */
391          log_error(LOG_LEVEL_FATAL, "sig_handler: exiting on unexpected signal %d", the_signal);
392    }
393    return;
394
395 }
396 #endif
397
398
399 /*********************************************************************
400  *
401  * Function    :  get_write_delay
402  *
403  * Description :  Parse the delay-response parameter.
404  *
405  * Parameters  :
406  *          1  :  csp = Current client state (buffers, headers, etc...)
407  *
408  * Returns     :  Number of milliseconds to delay writes.
409  *
410  *********************************************************************/
411 static unsigned int get_write_delay(const struct client_state *csp)
412 {
413    unsigned int delay;
414    char *endptr;
415    char *newval;
416
417    if ((csp->action->flags & ACTION_DELAY_RESPONSE) == 0)
418    {
419       return 0;
420    }
421    newval = csp->action->string[ACTION_STRING_DELAY_RESPONSE];
422
423    delay = (unsigned)strtol(newval, &endptr, 0);
424    if (*endptr != '\0')
425    {
426       log_error(LOG_LEVEL_FATAL,
427          "Invalid delay-response{} parameter: '%s'", newval);
428    }
429
430    return delay;
431
432 }
433
434
435 /*********************************************************************
436  *
437  * Function    :  client_protocol_is_unsupported
438  *
439  * Description :  Checks if the client used a known unsupported
440  *                protocol and deals with it by sending an error
441  *                response.
442  *
443  * Parameters  :
444  *          1  :  csp = Current client state (buffers, headers, etc...)
445  *          2  :  req = the first request line send by the client
446  *
447  * Returns     :  TRUE if an error response has been generated, or
448  *                FALSE if the request doesn't look invalid.
449  *
450  *********************************************************************/
451 static int client_protocol_is_unsupported(struct client_state *csp, char *req)
452 {
453    /*
454     * If it's a FTP or gopher request, we don't support it.
455     *
456     * These checks are better than nothing, but they might
457     * not work in all configurations and some clients might
458     * have problems digesting the answer.
459     *
460     * They should, however, never cause more problems than
461     * Privoxy's old behaviour (returning the misleading HTML
462     * error message:
463     *
464     * "Could not resolve http://(ftp|gopher)://example.org").
465     */
466    if (!strncmpic(req, "GET ftp://", 10) || !strncmpic(req, "GET gopher://", 13))
467    {
468       const char *response = NULL;
469       const char *protocol = NULL;
470
471       if (!strncmpic(req, "GET ftp://", 10))
472       {
473          response = FTP_RESPONSE;
474          protocol = "FTP";
475       }
476       else
477       {
478          response = GOPHER_RESPONSE;
479          protocol = "GOPHER";
480       }
481       log_error(LOG_LEVEL_ERROR,
482          "%s tried to use Privoxy as %s proxy: %s",
483          csp->ip_addr_str, protocol, req);
484       log_error(LOG_LEVEL_CLF,
485          "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, req);
486       freez(req);
487
488 #ifdef FEATURE_HTTPS_INSPECTION
489       if (client_use_ssl(csp))
490       {
491          ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
492             (const unsigned char *)response, strlen(response),
493             get_write_delay(csp));
494       }
495       else
496 #endif
497       {
498          write_socket_delayed(csp->cfd, response, strlen(response),
499             get_write_delay(csp));
500       }
501
502       return TRUE;
503    }
504
505    return FALSE;
506 }
507
508
509 /*********************************************************************
510  *
511  * Function    :  client_has_unsupported_expectations
512  *
513  * Description :  Checks if the client used an unsupported expectation
514  *                in which case an error message is delivered.
515  *
516  * Parameters  :
517  *          1  :  csp = Current client state (buffers, headers, etc...)
518  *
519  * Returns     :  TRUE if an error response has been generated, or
520  *                FALSE if the request doesn't look invalid.
521  *
522  *********************************************************************/
523 static int client_has_unsupported_expectations(const struct client_state *csp)
524 {
525    if ((csp->flags & CSP_FLAG_UNSUPPORTED_CLIENT_EXPECTATION))
526    {
527       log_error(LOG_LEVEL_ERROR,
528          "Rejecting request from client %s with unsupported Expect header value",
529          csp->ip_addr_str);
530       log_error(LOG_LEVEL_CLF,
531          "%s - - [%T] \"%s\" 417 0", csp->ip_addr_str, csp->http->cmd);
532       write_socket_delayed(csp->cfd,
533          UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE,
534          strlen(UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE),
535          get_write_delay(csp));
536
537       return TRUE;
538    }
539
540    return FALSE;
541
542 }
543
544
545 /*********************************************************************
546  *
547  * Function    :  get_request_destination_elsewhere
548  *
549  * Description :  If the client's request was redirected into
550  *                Privoxy without the client's knowledge,
551  *                the request line lacks the destination host.
552  *
553  *                This function tries to get it elsewhere,
554  *                provided accept-intercepted-requests is enabled.
555  *
556  *                "Elsewhere" currently only means "Host: header",
557  *                but in the future we may ask the redirecting
558  *                packet filter to look the destination up.
559  *
560  *                If the destination stays unknown, an error
561  *                response is send to the client and headers
562  *                are freed so that chat() can return directly.
563  *
564  * Parameters  :
565  *          1  :  csp = Current client state (buffers, headers, etc...)
566  *          2  :  headers = a header list
567  *
568  * Returns     :  JB_ERR_OK if the destination is now known, or
569  *                JB_ERR_PARSE if it isn't.
570  *
571  *********************************************************************/
572 static jb_err get_request_destination_elsewhere(struct client_state *csp, struct list *headers)
573 {
574    char *req;
575
576    if (!(csp->config->feature_flags & RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS))
577    {
578       log_error(LOG_LEVEL_ERROR, "%s's request: \'%s\' is invalid."
579          " Privoxy isn't configured to accept intercepted requests.",
580          csp->ip_addr_str, csp->http->cmd);
581       /* XXX: Use correct size */
582       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
583          csp->ip_addr_str, csp->http->cmd);
584
585       write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER),
586          get_write_delay(csp));
587       destroy_list(headers);
588
589       return JB_ERR_PARSE;
590    }
591    else if (JB_ERR_OK == get_destination_from_headers(headers, csp->http))
592    {
593       /* Split the domain we just got for pattern matching */
594       init_domain_components(csp->http);
595
596       return JB_ERR_OK;
597    }
598    else
599    {
600       /* We can't work without destination. Go spread the news.*/
601
602       req = list_to_text(headers);
603       chomp(req);
604       /* XXX: Use correct size */
605       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
606          csp->ip_addr_str, csp->http->cmd);
607       log_error(LOG_LEVEL_ERROR,
608          "Privoxy was unable to get the destination for %s's request:\n%s\n%s",
609          csp->ip_addr_str, csp->http->cmd, req);
610       freez(req);
611
612       write_socket_delayed(csp->cfd, MISSING_DESTINATION_RESPONSE,
613          strlen(MISSING_DESTINATION_RESPONSE), get_write_delay(csp));
614       destroy_list(headers);
615
616       return JB_ERR_PARSE;
617    }
618    /*
619     * TODO: If available, use PF's ioctl DIOCNATLOOK as last resort
620     * to get the destination IP address, use it as host directly
621     * or do a reverse DNS lookup first.
622     */
623 }
624
625
626 /*********************************************************************
627  *
628  * Function    :  get_server_headers
629  *
630  * Description :  Parses server headers in iob and fills them
631  *                into csp->headers so that they can later be
632  *                handled by sed().
633  *
634  * Parameters  :
635  *          1  :  csp = Current client state (buffers, headers, etc...)
636  *
637  * Returns     :  JB_ERR_OK if everything went fine, or
638  *                JB_ERR_PARSE if the headers were incomplete.
639  *
640  *********************************************************************/
641 static jb_err get_server_headers(struct client_state *csp)
642 {
643    int continue_hack_in_da_house = 0;
644    char * header;
645
646    while (((header = get_header(csp->iob)) != NULL) || continue_hack_in_da_house)
647    {
648       if (header == NULL)
649       {
650          /*
651           * continue hack in da house. Ignore the ending of
652           * this head and continue enlisting header lines.
653           * The reason is described below.
654           */
655          enlist(csp->headers, "");
656          continue_hack_in_da_house = 0;
657          continue;
658       }
659       else if (0 == strncmpic(header, "HTTP/1.1 100", 12))
660       {
661          /*
662           * It's a bodyless continue response, don't
663           * stop header parsing after reaching its end.
664           *
665           * As a result Privoxy will concatenate the
666           * next response's head and parse and deliver
667           * the headers as if they belonged to one request.
668           *
669           * The client will separate them because of the
670           * empty line between them.
671           *
672           * XXX: What we're doing here is clearly against
673           * the intended purpose of the continue header,
674           * and under some conditions (HTTP/1.0 client request)
675           * it's a standard violation.
676           *
677           * Anyway, "sort of against the spec" is preferable
678           * to "always getting confused by Continue responses"
679           * (Privoxy's behaviour before this hack was added)
680           */
681          log_error(LOG_LEVEL_HEADER, "Continue hack in da house.");
682          continue_hack_in_da_house = 1;
683       }
684       else if (*header == '\0')
685       {
686          /*
687           * If the header is empty, but the Continue hack
688           * isn't active, we can assume that we reached the
689           * end of the buffer before we hit the end of the
690           * head.
691           *
692           * Inform the caller an let it decide how to handle it.
693           */
694          return JB_ERR_PARSE;
695       }
696
697       if (JB_ERR_MEMORY == enlist(csp->headers, header))
698       {
699          /*
700           * XXX: Should we quit the request and return a
701           * out of memory error page instead?
702           */
703          log_error(LOG_LEVEL_ERROR,
704             "Out of memory while enlisting server headers. %s lost.",
705             header);
706       }
707       freez(header);
708    }
709
710    return JB_ERR_OK;
711 }
712
713
714 /*********************************************************************
715  *
716  * Function    :  crunch_reason
717  *
718  * Description :  Translates the crunch reason code into a string.
719  *
720  * Parameters  :
721  *          1  :  rsp = a http_response
722  *
723  * Returns     :  A string with the crunch reason or an error description.
724  *
725  *********************************************************************/
726 static const char *crunch_reason(const struct http_response *rsp)
727 {
728    char * reason = NULL;
729
730    assert(rsp != NULL);
731    if (rsp == NULL)
732    {
733       return "Internal error while searching for crunch reason";
734    }
735
736    switch (rsp->crunch_reason)
737    {
738       case UNSUPPORTED:
739          reason = "Unsupported HTTP feature";
740          break;
741       case BLOCKED:
742          reason = "Blocked";
743          break;
744       case UNTRUSTED:
745          reason = "Untrusted";
746          break;
747       case REDIRECTED:
748          reason = "Redirected";
749          break;
750       case CGI_CALL:
751          reason = "CGI Call";
752          break;
753       case NO_SUCH_DOMAIN:
754          reason = "DNS failure";
755          break;
756       case FORWARDING_FAILED:
757          reason = "Forwarding failed";
758          break;
759       case CONNECT_FAILED:
760          reason = "Connection failure";
761          break;
762       case OUT_OF_MEMORY:
763          reason = "Out of memory (may mask other reasons)";
764          break;
765       case CONNECTION_TIMEOUT:
766          reason = "Connection timeout";
767          break;
768       case NO_SERVER_DATA:
769          reason = "No server data received";
770          break;
771       default:
772          reason = "No reason recorded";
773          break;
774    }
775
776    return reason;
777 }
778
779
780 /*********************************************************************
781  *
782  * Function    :  log_applied_actions
783  *
784  * Description :  Logs the applied actions if LOG_LEVEL_ACTIONS is
785  *                enabled.
786  *
787  * Parameters  :
788  *          1  :  actions = Current action spec to log
789  *
790  * Returns     :  Nothing.
791  *
792  *********************************************************************/
793 static void log_applied_actions(const struct current_action_spec *actions)
794 {
795    /*
796     * The conversion to text requires lots of memory allocations so
797     * we only do the conversion if the user is actually interested.
798     */
799    if (debug_level_is_enabled(LOG_LEVEL_ACTIONS))
800    {
801       char *actions_as_text = actions_to_line_of_text(actions);
802       log_error(LOG_LEVEL_ACTIONS, "%s", actions_as_text);
803       freez(actions_as_text);
804    }
805 }
806
807
808 /*********************************************************************
809  *
810  * Function    :  send_crunch_response
811  *
812  * Description :  Delivers already prepared response for
813  *                intercepted requests, logs the interception
814  *                and frees the response.
815  *
816  * Parameters  :
817  *          1  :  csp = Current client state (buffers, headers, etc...)
818  *          2  :  rsp = Fully prepared response. Will be freed on exit.
819  *
820  * Returns     :  Nothing.
821  *
822  *********************************************************************/
823 static void send_crunch_response(struct client_state *csp, struct http_response *rsp)
824 {
825       const struct http_request *http = csp->http;
826       char status_code[4];
827
828       assert(rsp != NULL);
829       assert(rsp->head != NULL);
830
831       if (rsp == NULL)
832       {
833          log_error(LOG_LEVEL_FATAL, "NULL response in send_crunch_response.");
834       }
835
836       /*
837        * Extract the status code from the actual head
838        * that will be send to the client. It is the only
839        * way to get it right for all requests, including
840        * the fixed ones for out-of-memory problems.
841        *
842        * A head starts like this: 'HTTP/1.1 200...'
843        *                           0123456789|11
844        *                                     10
845        */
846       status_code[0] = rsp->head[9];
847       status_code[1] = rsp->head[10];
848       status_code[2] = rsp->head[11];
849       status_code[3] = '\0';
850
851       /* Log that the request was crunched and why. */
852       log_applied_actions(csp->action);
853 #ifdef FEATURE_HTTPS_INSPECTION
854       if (client_use_ssl(csp))
855       {
856          log_error(LOG_LEVEL_CRUNCH, "%s: https://%s%s", crunch_reason(rsp),
857             http->hostport, http->path);
858          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" %s %llu",
859             csp->ip_addr_str, http->gpc, http->hostport, http->path,
860             http->version, status_code, rsp->content_length);
861       }
862       else
863 #endif
864       {
865          log_error(LOG_LEVEL_CRUNCH, "%s: %s", crunch_reason(rsp), http->url);
866          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" %s %u",
867             csp->ip_addr_str, http->ocmd, status_code, rsp->content_length);
868       }
869       /* Write the answer to the client */
870 #ifdef FEATURE_HTTPS_INSPECTION
871       if (client_use_ssl(csp))
872       {
873          if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
874                 (const unsigned char *)rsp->head, rsp->head_length,
875                 get_write_delay(csp)) < 0)
876           || (ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
877                 (const unsigned char *)rsp->body, rsp->content_length,
878                 get_write_delay(csp)) < 0))
879          {
880             /* There is nothing we can do about it. */
881             log_error(LOG_LEVEL_CONNECT, "Couldn't deliver the error message "
882                "for %s through client socket %d using TLS/SSL",
883                http->url, csp->cfd);
884          }
885       }
886       else
887 #endif
888       {
889          if (write_socket_delayed(csp->cfd, rsp->head, rsp->head_length,
890                 get_write_delay(csp))
891           || write_socket_delayed(csp->cfd, rsp->body, rsp->content_length,
892                 get_write_delay(csp)))
893          {
894             /* There is nothing we can do about it. */
895             log_error(LOG_LEVEL_CONNECT,
896                "Couldn't deliver the error message for %s through client socket %d: %E",
897                http->url, csp->cfd);
898          }
899       }
900
901       /* Clean up and return */
902       if (cgi_error_memory() != rsp)
903       {
904          free_http_response(rsp);
905       }
906       return;
907 }
908
909
910 /*********************************************************************
911  *
912  * Function    :  crunch_response_triggered
913  *
914  * Description :  Checks if the request has to be crunched,
915  *                and delivers the crunch response if necessary.
916  *
917  * Parameters  :
918  *          1  :  csp = Current client state (buffers, headers, etc...)
919  *          2  :  crunchers = list of cruncher functions to run
920  *
921  * Returns     :  TRUE if the request was answered with a crunch response
922  *                FALSE otherwise.
923  *
924  *********************************************************************/
925 static int crunch_response_triggered(struct client_state *csp, const struct cruncher crunchers[])
926 {
927    struct http_response *rsp = NULL;
928    const struct cruncher *c;
929
930    /*
931     * If CGI request crunching is disabled,
932     * check the CGI dispatcher out of order to
933     * prevent unintentional blocks or redirects.
934     */
935    if (!(csp->config->feature_flags & RUNTIME_FEATURE_CGI_CRUNCHING)
936        && (NULL != (rsp = dispatch_cgi(csp))))
937    {
938       /* Deliver, log and free the interception response. */
939       send_crunch_response(csp, rsp);
940       csp->flags |= CSP_FLAG_CRUNCHED;
941       return TRUE;
942    }
943
944    for (c = crunchers; c->cruncher != NULL; c++)
945    {
946       /*
947        * Check the cruncher if either Privoxy is toggled
948        * on and the request isn't forced, or if the cruncher
949        * applies to forced requests as well.
950        */
951       if (((csp->flags & CSP_FLAG_TOGGLED_ON) &&
952           !(csp->flags & CSP_FLAG_FORCED)) ||
953           (c->flags & CF_IGNORE_FORCE))
954       {
955          rsp = c->cruncher(csp);
956          if (NULL != rsp)
957          {
958             /* Deliver, log and free the interception response. */
959             send_crunch_response(csp, rsp);
960             csp->flags |= CSP_FLAG_CRUNCHED;
961 #ifdef FEATURE_STATISTICS
962             if (c->flags & CF_COUNT_AS_REJECT)
963             {
964                csp->flags |= CSP_FLAG_REJECTED;
965             }
966 #endif /* def FEATURE_STATISTICS */
967
968             return TRUE;
969          }
970       }
971    }
972
973    return FALSE;
974 }
975
976
977 /*********************************************************************
978  *
979  * Function    :  build_request_line
980  *
981  * Description :  Builds the HTTP request line.
982  *
983  *                If a HTTP forwarder is used it expects the whole URL,
984  *                web servers only get the path.
985  *
986  * Parameters  :
987  *          1  :  csp = Current client state (buffers, headers, etc...)
988  *          2  :  fwd = The forwarding spec used for the request
989  *                XXX: Should use http->fwd instead.
990  *          3  :  request_line = The old request line which will be replaced.
991  *
992  * Returns     :  Nothing. Terminates in case of memory problems.
993  *
994  *********************************************************************/
995 static void build_request_line(struct client_state *csp, const struct forward_spec *fwd, char **request_line)
996 {
997    struct http_request *http = csp->http;
998
999    /*
1000     * Downgrade http version from 1.1 to 1.0
1001     * if +downgrade action applies.
1002     */
1003    if ((csp->action->flags & ACTION_DOWNGRADE)
1004      && (!strcmpic(http->version, "HTTP/1.1")))
1005    {
1006       freez(http->version);
1007       http->version = strdup_or_die("HTTP/1.0");
1008    }
1009
1010    /*
1011     * Rebuild the request line.
1012     */
1013    freez(*request_line);
1014    *request_line = strdup(http->gpc);
1015    string_append(request_line, " ");
1016
1017    if (fwd->forward_host && fwd->type != FORWARD_WEBSERVER)
1018    {
1019       string_append(request_line, http->url);
1020    }
1021    else
1022    {
1023       string_append(request_line, http->path);
1024    }
1025    string_append(request_line, " ");
1026    string_append(request_line, http->version);
1027
1028    if (*request_line == NULL)
1029    {
1030       log_error(LOG_LEVEL_FATAL, "Out of memory writing HTTP command");
1031    }
1032    log_error(LOG_LEVEL_HEADER, "New HTTP Request-Line: %s", *request_line);
1033 }
1034
1035
1036 /*********************************************************************
1037  *
1038  * Function    :  change_request_destination
1039  *
1040  * Description :  Parse a (rewritten) request line and regenerate
1041  *                the http request data.
1042  *
1043  * Parameters  :
1044  *          1  :  csp = Current client state (buffers, headers, etc...)
1045  *
1046  * Returns     :  Forwards the parse_http_request() return code.
1047  *                Terminates in case of memory problems.
1048  *
1049  *********************************************************************/
1050 static jb_err change_request_destination(struct client_state *csp)
1051 {
1052    struct http_request *http = csp->http;
1053    jb_err err;
1054
1055    log_error(LOG_LEVEL_REDIRECTS, "Rewrite detected: %s",
1056       csp->headers->first->str);
1057    free_http_request(http);
1058    err = parse_http_request(csp->headers->first->str, http);
1059    if (JB_ERR_OK != err)
1060    {
1061       log_error(LOG_LEVEL_ERROR, "Couldn't parse rewritten request: %s.",
1062          jb_err_to_string(err));
1063    }
1064
1065    return err;
1066 }
1067
1068
1069 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1070 /*********************************************************************
1071  *
1072  * Function    :  server_response_is_complete
1073  *
1074  * Description :  Determines whether we should stop reading
1075  *                from the server socket.
1076  *
1077  * Parameters  :
1078  *          1  :  csp = Current client state (buffers, headers, etc...)
1079  *          2  :  content_length = Length of content received so far.
1080  *
1081  * Returns     :  TRUE if the response is complete,
1082  *                FALSE otherwise.
1083  *
1084  *********************************************************************/
1085 static int server_response_is_complete(struct client_state *csp,
1086    unsigned long long content_length)
1087 {
1088    int content_length_known = !!(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET);
1089
1090    if (!strcmpic(csp->http->gpc, "HEAD"))
1091    {
1092       /*
1093        * "HEAD" implies no body, we are thus expecting
1094        * no content. XXX: incomplete "list" of methods?
1095        */
1096       csp->expected_content_length = 0;
1097       content_length_known = TRUE;
1098       csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1099    }
1100
1101    if (csp->http->status == 204 || csp->http->status == 304)
1102    {
1103       /*
1104        * Expect no body. XXX: incomplete "list" of status codes?
1105        */
1106       csp->expected_content_length = 0;
1107       content_length_known = TRUE;
1108       csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1109    }
1110
1111    return (content_length_known && ((0 == csp->expected_content_length)
1112             || (csp->expected_content_length <= content_length)));
1113 }
1114
1115
1116 #ifdef FEATURE_CONNECTION_SHARING
1117 /*********************************************************************
1118  *
1119  * Function    :  wait_for_alive_connections
1120  *
1121  * Description :  Waits for alive connections to timeout.
1122  *
1123  * Parameters  :  N/A
1124  *
1125  * Returns     :  N/A
1126  *
1127  *********************************************************************/
1128 static void wait_for_alive_connections(void)
1129 {
1130    int connections_alive = close_unusable_connections();
1131
1132    while (0 < connections_alive)
1133    {
1134       log_error(LOG_LEVEL_CONNECT,
1135          "Waiting for %d connections to timeout.",
1136          connections_alive);
1137       sleep(60);
1138       connections_alive = close_unusable_connections();
1139    }
1140
1141    log_error(LOG_LEVEL_CONNECT, "No connections to wait for left.");
1142
1143 }
1144 #endif /* def FEATURE_CONNECTION_SHARING */
1145
1146
1147 /*********************************************************************
1148  *
1149  * Function    :  save_connection_destination
1150  *
1151  * Description :  Remembers a connection for reuse later on.
1152  *
1153  * Parameters  :
1154  *          1  :  sfd  = Open socket to remember.
1155  *          2  :  http = The destination for the connection.
1156  *          3  :  fwd  = The forwarder settings used.
1157  *          4  :  server_connection  = storage.
1158  *
1159  * Returns     : void
1160  *
1161  *********************************************************************/
1162 void save_connection_destination(jb_socket sfd,
1163                                  const struct http_request *http,
1164                                  const struct forward_spec *fwd,
1165                                  struct reusable_connection *server_connection)
1166 {
1167    assert(sfd != JB_INVALID_SOCKET);
1168    assert(NULL != http->host);
1169
1170    server_connection->sfd = sfd;
1171    server_connection->host = strdup_or_die(http->host);
1172    server_connection->port = http->port;
1173
1174    assert(NULL != fwd);
1175    assert(server_connection->gateway_host == NULL);
1176    assert(server_connection->gateway_port == 0);
1177    assert(server_connection->forwarder_type == 0);
1178    assert(server_connection->forward_host == NULL);
1179    assert(server_connection->forward_port == 0);
1180
1181    server_connection->forwarder_type = fwd->type;
1182    if (NULL != fwd->gateway_host)
1183    {
1184       server_connection->gateway_host = strdup_or_die(fwd->gateway_host);
1185    }
1186    else
1187    {
1188       server_connection->gateway_host = NULL;
1189    }
1190    server_connection->gateway_port = fwd->gateway_port;
1191
1192    if (NULL != fwd->forward_host)
1193    {
1194       server_connection->forward_host = strdup_or_die(fwd->forward_host);
1195    }
1196    else
1197    {
1198       server_connection->forward_host = NULL;
1199    }
1200    server_connection->forward_port = fwd->forward_port;
1201 }
1202
1203
1204 /*********************************************************************
1205  *
1206  * Function    : verify_request_length
1207  *
1208  * Description : Checks if we already got the whole client requests
1209  *               and sets CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ if
1210  *               we do.
1211  *
1212  *               Data that doesn't belong to the current request is
1213  *               either thrown away to let the client retry on a clean
1214  *               socket, or stashed to be dealt with after the current
1215  *               request is served.
1216  *
1217  * Parameters  :
1218  *          1  :  csp = Current client state (buffers, headers, etc...)
1219  *
1220  * Returns     :  void
1221  *
1222  *********************************************************************/
1223 static void verify_request_length(struct client_state *csp)
1224 {
1225    unsigned long long buffered_request_bytes =
1226       (unsigned long long)(csp->client_iob->eod - csp->client_iob->cur);
1227
1228    if ((csp->expected_client_content_length != 0)
1229       && (buffered_request_bytes != 0))
1230    {
1231       if (csp->expected_client_content_length >= buffered_request_bytes)
1232       {
1233          csp->expected_client_content_length -= buffered_request_bytes;
1234          log_error(LOG_LEVEL_CONNECT, "Reduced expected bytes to %llu "
1235             "to account for the %llu ones we already got.",
1236             csp->expected_client_content_length, buffered_request_bytes);
1237       }
1238       else
1239       {
1240          assert(csp->client_iob->eod > csp->client_iob->cur + csp->expected_client_content_length);
1241          csp->client_iob->eod = csp->client_iob->cur + csp->expected_client_content_length;
1242          log_error(LOG_LEVEL_CONNECT, "Reducing expected bytes to 0. "
1243             "Marking the server socket tainted after throwing %llu bytes away.",
1244             buffered_request_bytes - csp->expected_client_content_length);
1245          csp->expected_client_content_length = 0;
1246          csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1247       }
1248
1249       if (csp->expected_client_content_length == 0)
1250       {
1251          csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1252       }
1253    }
1254
1255    if (!(csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ)
1256       && ((csp->client_iob->cur < csp->client_iob->eod)
1257          || (csp->expected_client_content_length != 0)))
1258    {
1259       if (strcmpic(csp->http->gpc, "GET")
1260          && strcmpic(csp->http->gpc, "HEAD")
1261          && strcmpic(csp->http->gpc, "TRACE")
1262          && strcmpic(csp->http->gpc, "OPTIONS")
1263          && strcmpic(csp->http->gpc, "DELETE"))
1264       {
1265          /* XXX: this is an incomplete hack */
1266          csp->flags &= ~CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1267          log_error(LOG_LEVEL_CONNECT, "There better be a request body.");
1268       }
1269       else
1270       {
1271          csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1272
1273          if ((csp->config->feature_flags & RUNTIME_FEATURE_TOLERATE_PIPELINING) == 0)
1274          {
1275             csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1276             log_error(LOG_LEVEL_CONNECT,
1277                "Possible pipeline attempt detected. The connection will not "
1278                "be kept alive and we will only serve the first request.");
1279             /* Nuke the pipelined requests from orbit, just to be sure. */
1280             clear_iob(csp->client_iob);
1281          }
1282          else
1283          {
1284             /*
1285              * Keep the pipelined data around for now, we'll deal with
1286              * it once we're done serving the current request.
1287              */
1288             csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
1289             assert(csp->client_iob->eod >= csp->client_iob->cur);
1290             log_error(LOG_LEVEL_CONNECT, "Complete client request followed by "
1291                "%d bytes of pipelined data received.",
1292                (int)(csp->client_iob->eod - csp->client_iob->cur));
1293          }
1294       }
1295    }
1296    else
1297    {
1298       csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1299       log_error(LOG_LEVEL_CONNECT, "Complete client request received.");
1300    }
1301 }
1302 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
1303
1304
1305 /*********************************************************************
1306  *
1307  * Function    :  mark_server_socket_tainted
1308  *
1309  * Description :  Makes sure we don't reuse a server socket
1310  *                (if we didn't read everything the server sent
1311  *                us reusing the socket would lead to garbage).
1312  *
1313  * Parameters  :
1314  *          1  :  csp = Current client state (buffers, headers, etc...)
1315  *
1316  * Returns     :  void.
1317  *
1318  *********************************************************************/
1319 static void mark_server_socket_tainted(struct client_state *csp)
1320 {
1321    /*
1322     * For consistency we always mark the server socket
1323     * tainted, however, to reduce the log noise we only
1324     * emit a log message if the server socket could have
1325     * actually been reused.
1326     */
1327    if ((csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
1328       && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
1329    {
1330       log_error(LOG_LEVEL_CONNECT,
1331          "Marking the server socket %d tainted.",
1332          csp->server_connection.sfd);
1333    }
1334    csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1335 }
1336
1337 /*********************************************************************
1338  *
1339  * Function    :  get_request_line
1340  *
1341  * Description : Read the client request line.
1342  *
1343  * Parameters  :
1344  *          1  :  csp = Current client state (buffers, headers, etc...)
1345  *
1346  * Returns     :  Pointer to request line or NULL in case of errors.
1347  *
1348  *********************************************************************/
1349 static char *get_request_line(struct client_state *csp)
1350 {
1351    char buf[BUFFER_SIZE];
1352    char *request_line = NULL;
1353    int len;
1354
1355    memset(buf, 0, sizeof(buf));
1356
1357    if ((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) != 0)
1358    {
1359       /*
1360        * If there are multiple pipelined requests waiting,
1361        * the flag will be set again once the next request
1362        * has been parsed.
1363        */
1364       csp->flags &= ~CSP_FLAG_PIPELINED_REQUEST_WAITING;
1365
1366       request_line = get_header(csp->client_iob);
1367       if ((NULL != request_line) && ('\0' != *request_line))
1368       {
1369          return request_line;
1370       }
1371       else
1372       {
1373          log_error(LOG_LEVEL_CONNECT, "No complete request line "
1374             "received yet. Continuing reading from %d.", csp->cfd);
1375       }
1376    }
1377
1378    do
1379    {
1380       if (
1381 #ifdef FUZZ
1382           0 == (csp->flags & CSP_FLAG_FUZZED_INPUT) &&
1383 #endif
1384           !data_is_available(csp->cfd, csp->config->socket_timeout)
1385           )
1386       {
1387          if (socket_is_still_alive(csp->cfd))
1388          {
1389             log_error(LOG_LEVEL_CONNECT,
1390                "No request line on socket %d received in time. Timeout: %d.",
1391                csp->cfd, csp->config->socket_timeout);
1392             write_socket_delayed(csp->cfd, CLIENT_CONNECTION_TIMEOUT_RESPONSE,
1393                strlen(CLIENT_CONNECTION_TIMEOUT_RESPONSE),
1394                get_write_delay(csp));
1395          }
1396          else
1397          {
1398             log_error(LOG_LEVEL_CONNECT,
1399                "The client side of the connection on socket %d got "
1400                "closed without sending a complete request line.", csp->cfd);
1401          }
1402          return NULL;
1403       }
1404
1405       len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1406
1407       if (len <= 0) return NULL;
1408
1409       /*
1410        * If there is no memory left for buffering the
1411        * request, there is nothing we can do but hang up
1412        */
1413       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1414       {
1415          return NULL;
1416       }
1417
1418       request_line = get_header(csp->client_iob);
1419
1420    } while ((NULL != request_line) && ('\0' == *request_line));
1421
1422    return request_line;
1423
1424 }
1425
1426 enum chunk_status
1427 {
1428    CHUNK_STATUS_MISSING_DATA,
1429    CHUNK_STATUS_BODY_COMPLETE,
1430    CHUNK_STATUS_PARSE_ERROR
1431 };
1432
1433
1434 /*********************************************************************
1435  *
1436  * Function    :  chunked_body_is_complete
1437  *
1438  * Description :  Figures out whether or not a chunked body is complete.
1439  *
1440  *                Currently it always starts at the beginning of the
1441  *                buffer which is somewhat wasteful and prevents Privoxy
1442  *                from starting to forward the correctly parsed chunks
1443  *                as soon as theoretically possible.
1444  *
1445  *                Should be modified to work with a common buffer,
1446  *                and allow the caller to skip already parsed chunks.
1447  *
1448  *                This would allow the function to be used for unbuffered
1449  *                response bodies as well.
1450  *
1451  * Parameters  :
1452  *          1  :  iob = Buffer with the body to check.
1453  *          2  :  length = Length of complete body
1454  *
1455  * Returns     :  Enum with the result of the check.
1456  *
1457  *********************************************************************/
1458 static enum chunk_status chunked_body_is_complete(struct iob *iob, size_t *length)
1459 {
1460    unsigned int chunksize;
1461    char *p = iob->cur;
1462
1463    do
1464    {
1465       /*
1466        * We need at least a single digit, followed by "\r\n",
1467        * followed by an unknown amount of data, followed by "\r\n".
1468        */
1469       if (p + 5 > iob->eod)
1470       {
1471          return CHUNK_STATUS_MISSING_DATA;
1472       }
1473       if (sscanf(p, "%x", &chunksize) != 1)
1474       {
1475          return CHUNK_STATUS_PARSE_ERROR;
1476       }
1477
1478       /*
1479        * We want at least a single digit, followed by "\r\n",
1480        * followed by the specified amount of data, followed by "\r\n".
1481        */
1482       if (p + chunksize + 5 > iob->eod)
1483       {
1484          return CHUNK_STATUS_MISSING_DATA;
1485       }
1486
1487       /* Skip chunk-size. */
1488       p = strstr(p, "\r\n");
1489       if (NULL == p)
1490       {
1491          return CHUNK_STATUS_PARSE_ERROR;
1492       }
1493       /* Move beyond the chunkdata. */
1494       p += 2 + chunksize;
1495
1496       /* There should be another "\r\n" to skip */
1497       if (memcmp(p, "\r\n", 2))
1498       {
1499          return CHUNK_STATUS_PARSE_ERROR;
1500       }
1501       p += 2;
1502    } while (chunksize > 0U);
1503
1504    *length = (size_t)(p - iob->cur);
1505    assert(*length <= (size_t)(iob->eod - iob->cur));
1506    assert(p <= iob->eod);
1507
1508    return CHUNK_STATUS_BODY_COMPLETE;
1509
1510 }
1511
1512
1513 /*********************************************************************
1514  *
1515  * Function    : receive_chunked_client_request_body
1516  *
1517  * Description : Read the chunk-encoded client request body.
1518  *               Failures are dealt with.
1519  *
1520  * Parameters  :
1521  *          1  :  csp = Current client state (buffers, headers, etc...)
1522  *
1523  * Returns     :  JB_ERR_OK or JB_ERR_PARSE
1524  *
1525  *********************************************************************/
1526 static jb_err receive_chunked_client_request_body(struct client_state *csp)
1527 {
1528    size_t body_length;
1529    enum chunk_status status;
1530
1531    while (CHUNK_STATUS_MISSING_DATA ==
1532       (status = chunked_body_is_complete(csp->client_iob, &body_length)))
1533    {
1534       char buf[BUFFER_SIZE];
1535       int len;
1536
1537       if (!data_is_available(csp->cfd, csp->config->socket_timeout))
1538       {
1539          log_error(LOG_LEVEL_ERROR,
1540             "Timeout while waiting for the client body.");
1541          break;
1542       }
1543       len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1544       if (len <= 0)
1545       {
1546          log_error(LOG_LEVEL_ERROR, "Read the client body failed: %E");
1547          break;
1548       }
1549       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1550       {
1551          break;
1552       }
1553    }
1554    if (status != CHUNK_STATUS_BODY_COMPLETE)
1555    {
1556       write_socket_delayed(csp->cfd, CLIENT_BODY_PARSE_ERROR_RESPONSE,
1557          strlen(CLIENT_BODY_PARSE_ERROR_RESPONSE), get_write_delay(csp));
1558       log_error(LOG_LEVEL_CLF,
1559          "%s - - [%T] \"Failed reading chunked client body\" 400 0", csp->ip_addr_str);
1560       return JB_ERR_PARSE;
1561    }
1562    log_error(LOG_LEVEL_CONNECT,
1563       "Chunked client body completely read. Length: %d", body_length);
1564    csp->expected_client_content_length = body_length;
1565
1566    return JB_ERR_OK;
1567
1568 }
1569
1570
1571 #ifdef FUZZ
1572 /*********************************************************************
1573  *
1574  * Function    :  fuzz_chunked_transfer_encoding
1575  *
1576  * Description :  Treat the fuzzed input as chunked transfer encoding
1577  *                to check and dechunk.
1578  *
1579  * Parameters  :
1580  *          1  :  csp      = Used to store the data.
1581  *          2  :  fuzz_input_file = File to read the input from.
1582  *
1583  * Returns     : Result of dechunking
1584  *
1585  *********************************************************************/
1586 extern int fuzz_chunked_transfer_encoding(struct client_state *csp, char *fuzz_input_file)
1587 {
1588    size_t length;
1589    size_t size = (size_t)(csp->iob->eod - csp->iob->cur);
1590    enum chunk_status status;
1591
1592    status = chunked_body_is_complete(csp->iob, &length);
1593    if (CHUNK_STATUS_BODY_COMPLETE != status)
1594    {
1595       log_error(LOG_LEVEL_INFO, "Chunked body is incomplete or invalid");
1596    }
1597
1598    return (JB_ERR_OK == remove_chunked_transfer_coding(csp->iob->cur, &size));
1599
1600 }
1601
1602
1603 /*********************************************************************
1604  *
1605  * Function    : fuzz_client_request
1606  *
1607  * Description : Try to get a client request from the fuzzed input.
1608  *
1609  * Parameters  :
1610  *          1  :  csp = Current client state (buffers, headers, etc...)
1611  *          2  :  fuzz_input_file = File to read the input from.
1612  *
1613  * Returns     :  Result of fuzzing.
1614  *
1615  *********************************************************************/
1616 extern int fuzz_client_request(struct client_state *csp, char *fuzz_input_file)
1617 {
1618    jb_err err;
1619
1620    csp->cfd = 0;
1621    csp->ip_addr_str = "fuzzer";
1622
1623    if (strcmp(fuzz_input_file, "-") != 0)
1624    {
1625       log_error(LOG_LEVEL_FATAL,
1626          "Fuzzed client requests can currently only be read from stdin (-).");
1627    }
1628    err = receive_client_request(csp);
1629    if (err != JB_ERR_OK)
1630    {
1631       return 1;
1632    }
1633    err = parse_client_request(csp);
1634    if (err != JB_ERR_OK)
1635    {
1636       return 1;
1637    }
1638
1639    return 0;
1640
1641 }
1642 #endif  /* def FUZZ */
1643
1644
1645 #ifdef FEATURE_FORCE_LOAD
1646 /*********************************************************************
1647  *
1648  * Function    :  force_required
1649  *
1650  * Description : Checks a request line to see if it contains
1651  *               the FORCE_PREFIX. If it does, it is removed
1652  *               unless enforcing requests has beend disabled.
1653  *
1654  * Parameters  :
1655  *          1  :  request_line = HTTP request line
1656  *
1657  * Returns     :  TRUE if force is required, FALSE otherwise.
1658  *
1659  *********************************************************************/
1660 static int force_required(const struct client_state *csp, char *request_line)
1661 {
1662    char *p;
1663
1664    p = strstr(request_line, "http://");
1665    if (p != NULL)
1666    {
1667       /* Skip protocol */
1668       p += strlen("http://");
1669    }
1670    else
1671    {
1672       /* Intercepted request usually don't specify the protocol. */
1673       p = request_line;
1674    }
1675
1676    /* Go to the beginning of the path */
1677    p = strstr(p, "/");
1678    if (p == NULL)
1679    {
1680       /*
1681        * If the path is missing the request line is invalid and we
1682        * are done here. The client-visible rejection happens later on.
1683        */
1684       return 0;
1685    }
1686
1687    if (0 == strncmpic(p, FORCE_PREFIX, strlen(FORCE_PREFIX) - 1))
1688    {
1689       if (!(csp->config->feature_flags & RUNTIME_FEATURE_ENFORCE_BLOCKS))
1690       {
1691          /* XXX: Should clean more carefully */
1692          strclean(request_line, FORCE_PREFIX);
1693          log_error(LOG_LEVEL_FORCE,
1694             "Enforcing request: \"%s\".", request_line);
1695
1696          return 1;
1697       }
1698       log_error(LOG_LEVEL_FORCE,
1699          "Ignored force prefix in request: \"%s\".", request_line);
1700    }
1701
1702    return 0;
1703
1704 }
1705 #endif /* def FEATURE_FORCE_LOAD */
1706
1707
1708 /*********************************************************************
1709  *
1710  * Function    :  receive_client_request
1711  *
1712  * Description : Read the client's request (more precisely the
1713  *               client headers) and answer it if necessary.
1714  *
1715  * Parameters  :
1716  *          1  :  csp = Current client state (buffers, headers, etc...)
1717  *
1718  * Returns     :  JB_ERR_OK, JB_ERR_PARSE or JB_ERR_MEMORY
1719  *
1720  *********************************************************************/
1721 static jb_err receive_client_request(struct client_state *csp)
1722 {
1723    char buf[BUFFER_SIZE];
1724    char *p;
1725    char *req = NULL;
1726    struct http_request *http;
1727    int len;
1728    jb_err err;
1729
1730    /* Temporary copy of the client's headers before they get enlisted in csp->headers */
1731    struct list header_list;
1732    struct list *headers = &header_list;
1733
1734    /* We don't care if the arriving data is a valid HTTP request or not. */
1735    csp->requests_received_total++;
1736
1737    http = csp->http;
1738
1739    memset(buf, 0, sizeof(buf));
1740
1741    req = get_request_line(csp);
1742    if (req == NULL)
1743    {
1744       mark_server_socket_tainted(csp);
1745       return JB_ERR_PARSE;
1746    }
1747    assert(*req != '\0');
1748
1749    if (client_protocol_is_unsupported(csp, req))
1750    {
1751       return JB_ERR_PARSE;
1752    }
1753
1754 #ifdef FEATURE_FORCE_LOAD
1755    if (force_required(csp, req))
1756    {
1757       csp->flags |= CSP_FLAG_FORCED;
1758    }
1759 #endif /* def FEATURE_FORCE_LOAD */
1760
1761    err = parse_http_request(req, http);
1762    freez(req);
1763    if (JB_ERR_OK != err)
1764    {
1765       write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER),
1766          get_write_delay(csp));
1767       /* XXX: Use correct size */
1768       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
1769       log_error(LOG_LEVEL_ERROR,
1770          "Couldn't parse request line received from %s: %s",
1771          csp->ip_addr_str, jb_err_to_string(err));
1772
1773       free_http_request(http);
1774       return JB_ERR_PARSE;
1775    }
1776
1777    /* grab the rest of the client's headers */
1778    init_list(headers);
1779    for (;;)
1780    {
1781       p = get_header(csp->client_iob);
1782
1783       if (p == NULL)
1784       {
1785          /* There are no additional headers to read. */
1786          break;
1787       }
1788
1789       if (*p == '\0')
1790       {
1791          /*
1792           * We didn't receive a complete header
1793           * line yet, get the rest of it.
1794           */
1795          if (!data_is_available(csp->cfd, csp->config->socket_timeout))
1796          {
1797             log_error(LOG_LEVEL_ERROR,
1798                "Stopped grabbing the client headers.");
1799             destroy_list(headers);
1800             return JB_ERR_PARSE;
1801          }
1802
1803          len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1804          if (len <= 0)
1805          {
1806             log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
1807             destroy_list(headers);
1808             return JB_ERR_PARSE;
1809          }
1810
1811          if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1812          {
1813             /*
1814              * If there is no memory left for buffering the
1815              * request, there is nothing we can do but hang up
1816              */
1817             destroy_list(headers);
1818             return JB_ERR_MEMORY;
1819          }
1820       }
1821       else
1822       {
1823          if (!strncmpic(p, "Transfer-Encoding:", 18))
1824          {
1825             /*
1826              * XXX: should be called through sed()
1827              *      but currently can't.
1828              */
1829             client_transfer_encoding(csp, &p);
1830          }
1831          /*
1832           * We were able to read a complete
1833           * header and can finally enlist it.
1834           */
1835          enlist(headers, p);
1836          freez(p);
1837       }
1838    }
1839
1840    if (http->host == NULL)
1841    {
1842       /*
1843        * If we still don't know the request destination,
1844        * the request is invalid or the client uses
1845        * Privoxy without its knowledge.
1846        */
1847       if (JB_ERR_OK != get_request_destination_elsewhere(csp, headers))
1848       {
1849          /*
1850           * Our attempts to get the request destination
1851           * elsewhere failed or Privoxy is configured
1852           * to only accept proxy requests.
1853           *
1854           * An error response has already been send
1855           * and we're done here.
1856           */
1857          return JB_ERR_PARSE;
1858       }
1859    }
1860
1861 #ifdef FEATURE_CLIENT_TAGS
1862    /* XXX: If the headers were enlisted sooner, passing csp would do. */
1863    set_client_address(csp, headers);
1864    get_tag_list_for_client(csp->client_tags, csp->client_address);
1865 #endif
1866
1867    /*
1868     * Determine the actions for this URL
1869     */
1870 #ifdef FEATURE_TOGGLE
1871    if (!(csp->flags & CSP_FLAG_TOGGLED_ON))
1872    {
1873       /* Most compatible set of actions (i.e. none) */
1874       init_current_action(csp->action);
1875    }
1876    else
1877 #endif /* ndef FEATURE_TOGGLE */
1878    {
1879       get_url_actions(csp, http);
1880    }
1881
1882    enlist(csp->headers, http->cmd);
1883
1884    /* Append the previously read headers */
1885    err = list_append_list_unique(csp->headers, headers);
1886    destroy_list(headers);
1887
1888    return err;
1889
1890 }
1891
1892
1893 /*********************************************************************
1894  *
1895  * Function    : parse_client_request
1896  *
1897  * Description : Parses the client's request and decides what to do
1898  *               with it.
1899  *
1900  *               Note that since we're not using select() we could get
1901  *               blocked here if a client connected, then didn't say
1902  *               anything!
1903  *
1904  * Parameters  :
1905  *          1  :  csp = Current client state (buffers, headers, etc...)
1906  *
1907  * Returns     :  JB_ERR_OK or JB_ERR_PARSE
1908  *
1909  *********************************************************************/
1910 static jb_err parse_client_request(struct client_state *csp)
1911 {
1912    struct http_request *http = csp->http;
1913    jb_err err;
1914
1915 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1916    if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1917     && (!strcmpic(csp->http->version, "HTTP/1.1"))
1918     && (csp->http->ssl == 0))
1919    {
1920       /* Assume persistence until further notice */
1921       csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1922    }
1923
1924    if (csp->http->ssl == 0)
1925    {
1926       /*
1927        * This whole block belongs to chat() but currently
1928        * has to be executed before sed().
1929        */
1930       if (csp->flags & CSP_FLAG_CHUNKED_CLIENT_BODY)
1931       {
1932          if (receive_chunked_client_request_body(csp) != JB_ERR_OK)
1933          {
1934             return JB_ERR_PARSE;
1935          }
1936       }
1937       else
1938       {
1939          csp->expected_client_content_length = get_expected_content_length(csp->headers);
1940       }
1941       verify_request_length(csp);
1942    }
1943    else
1944    {
1945       csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1946    }
1947 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1948
1949    err = sed(csp, FILTER_CLIENT_HEADERS);
1950    if (JB_ERR_OK != err)
1951    {
1952       log_error(LOG_LEVEL_ERROR, "Failed to parse client request from %s.",
1953          csp->ip_addr_str);
1954       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
1955          csp->ip_addr_str, csp->http->cmd);
1956       write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER), get_write_delay(csp));
1957       return JB_ERR_PARSE;
1958    }
1959    csp->flags |= CSP_FLAG_CLIENT_HEADER_PARSING_DONE;
1960
1961    /* Check request line for rewrites. */
1962    if ((NULL == csp->headers->first->str)
1963       || (strcmp(http->cmd, csp->headers->first->str) &&
1964          (JB_ERR_OK != change_request_destination(csp))))
1965    {
1966       /*
1967        * A header filter broke the request line - bail out.
1968        */
1969       write_socket_delayed(csp->cfd, MESSED_UP_REQUEST_RESPONSE,
1970          strlen(MESSED_UP_REQUEST_RESPONSE), get_write_delay(csp));
1971       /* XXX: Use correct size */
1972       log_error(LOG_LEVEL_CLF,
1973          "%s - - [%T] \"Invalid request generated\" 500 0", csp->ip_addr_str);
1974       log_error(LOG_LEVEL_ERROR,
1975          "Invalid request line after applying header filters.");
1976       free_http_request(http);
1977
1978       return JB_ERR_PARSE;
1979    }
1980
1981    if (client_has_unsupported_expectations(csp))
1982    {
1983       return JB_ERR_PARSE;
1984    }
1985
1986    return JB_ERR_OK;
1987
1988 }
1989
1990
1991 /*********************************************************************
1992  *
1993  * Function    : send_http_request
1994  *
1995  * Description : Sends the HTTP headers from the client request
1996  *               and all the body data that has already been received.
1997  *
1998  * Parameters  :
1999  *          1  :  csp = Current client state (buffers, headers, etc...)
2000  *
2001  * Returns     :  0 on success, anything else is an error.
2002  *
2003  *********************************************************************/
2004 static int send_http_request(struct client_state *csp)
2005 {
2006    char *hdr;
2007    int write_failure;
2008
2009    hdr = list_to_text(csp->headers);
2010    if (hdr == NULL)
2011    {
2012       /* FIXME Should handle error properly */
2013       log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
2014    }
2015    list_remove_all(csp->headers);
2016
2017    /*
2018     * Write the client's (modified) header to the server
2019     * (along with anything else that may be in the buffer)
2020     */
2021    write_failure = 0 != write_socket(csp->server_connection.sfd, hdr, strlen(hdr));
2022    freez(hdr);
2023
2024    if (write_failure)
2025    {
2026       log_error(LOG_LEVEL_CONNECT, "Failed sending request headers to: %s: %E",
2027          csp->http->hostport);
2028    }
2029    else if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
2030       && (flush_iob(csp->server_connection.sfd, csp->client_iob, 0) < 0))
2031    {
2032       write_failure = 1;
2033       log_error(LOG_LEVEL_CONNECT, "Failed sending request body to: %s: %E",
2034          csp->http->hostport);
2035    }
2036
2037    return write_failure;
2038
2039 }
2040
2041
2042 #ifdef FEATURE_HTTPS_INSPECTION
2043 /*********************************************************************
2044  *
2045  * Function    : receive_and_send_encrypted_post_data
2046  *
2047  * Description : Reads remaining POST data from the client and sends
2048  *               it to the server.
2049  *
2050  * Parameters  :
2051  *          1  :  csp = Current client state (buffers, headers, etc...)
2052  *
2053  * Returns     :  0 on success, anything else is an error.
2054  *
2055  *********************************************************************/
2056 static int receive_and_send_encrypted_post_data(struct client_state *csp)
2057 {
2058    int content_length_known = csp->expected_client_content_length != 0;
2059
2060    while (is_ssl_pending(&(csp->mbedtls_client_attr.ssl))
2061       || (content_length_known && csp->expected_client_content_length != 0))
2062    {
2063       unsigned char buf[BUFFER_SIZE];
2064       int len;
2065       int max_bytes_to_read = sizeof(buf);
2066
2067       if (content_length_known && csp->expected_client_content_length < sizeof(buf))
2068       {
2069          max_bytes_to_read = (int)csp->expected_client_content_length;
2070       }
2071       log_error(LOG_LEVEL_CONNECT,
2072          "Waiting for up to %d bytes of POST data from the client.",
2073          max_bytes_to_read);
2074       len = ssl_recv_data(&(csp->mbedtls_client_attr.ssl), buf,
2075          (unsigned)max_bytes_to_read);
2076       if (len == -1)
2077       {
2078          return 1;
2079       }
2080       if (len == 0)
2081       {
2082          /* XXX: Does this actually happen? */
2083          break;
2084       }
2085       log_error(LOG_LEVEL_CONNECT, "Forwarding %d bytes of encrypted POST data",
2086          len);
2087       len = ssl_send_data(&(csp->mbedtls_server_attr.ssl), buf, (size_t)len);
2088       if (len == -1)
2089       {
2090          return 1;
2091       }
2092       if (csp->expected_client_content_length != 0)
2093       {
2094          if (csp->expected_client_content_length >= len)
2095          {
2096             csp->expected_client_content_length -= (unsigned)len;
2097          }
2098          if (csp->expected_client_content_length == 0)
2099          {
2100             log_error(LOG_LEVEL_CONNECT, "Forwarded the last %d bytes", len);
2101             break;
2102          }
2103       }
2104    }
2105
2106    log_error(LOG_LEVEL_CONNECT, "Done forwarding encrypted POST data");
2107
2108    return 0;
2109
2110 }
2111
2112
2113 /*********************************************************************
2114  *
2115  * Function    : send_https_request
2116  *
2117  * Description : Sends the HTTP headers from the client request
2118  *               and all the body data that has already been received.
2119  *
2120  * Parameters  :
2121  *          1  :  csp = Current client state (buffers, headers, etc...)
2122  *
2123  * Returns     :  0 on success, anything else is an error.
2124  *
2125  *********************************************************************/
2126 static int send_https_request(struct client_state *csp)
2127 {
2128    char *hdr;
2129    int ret;
2130    long flushed = 0;
2131
2132    hdr = list_to_text(csp->https_headers);
2133    if (hdr == NULL)
2134    {
2135       /* FIXME Should handle error properly */
2136       log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
2137    }
2138    list_remove_all(csp->https_headers);
2139
2140    /*
2141     * Write the client's (modified) header to the server
2142     * (along with anything else that may be in the buffer)
2143     */
2144    ret = ssl_send_data(&(csp->mbedtls_server_attr.ssl),
2145       (const unsigned char *)hdr, strlen(hdr));
2146    freez(hdr);
2147
2148    if (ret < 0)
2149    {
2150       log_error(LOG_LEVEL_CONNECT,
2151          "Failed sending encrypted request headers to: %s: %E",
2152          csp->http->hostport);
2153       mark_server_socket_tainted(csp);
2154       return 1;
2155    }
2156
2157    if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
2158       && ((flushed = ssl_flush_socket(&(csp->mbedtls_server_attr.ssl),
2159             csp->client_iob)) < 0))
2160    {
2161       log_error(LOG_LEVEL_CONNECT, "Failed sending request body to: %s: %E",
2162          csp->http->hostport);
2163       return 1;
2164    }
2165    if (flushed != 0)
2166    {
2167       if (csp->expected_client_content_length != 0)
2168       {
2169          if (csp->expected_client_content_length < flushed)
2170          {
2171             log_error(LOG_LEVEL_ERROR,
2172                "Flushed %d bytes of request body while only expecting %llu",
2173                flushed, csp->expected_client_content_length);
2174             csp->expected_client_content_length = 0;
2175          }
2176          else
2177          {
2178             log_error(LOG_LEVEL_CONNECT,
2179                "Flushed %d bytes of request body while expecting %llu",
2180                flushed, csp->expected_client_content_length);
2181             csp->expected_client_content_length -= (unsigned)flushed;
2182             if (receive_and_send_encrypted_post_data(csp))
2183             {
2184                return 1;
2185             }
2186          }
2187       }
2188       else
2189       {
2190          log_error(LOG_LEVEL_CONNECT,
2191             "Flushed %d bytes of request body", flushed);
2192       }
2193    }
2194
2195    log_error(LOG_LEVEL_CONNECT, "Encrypted request sent");
2196
2197    return 0;
2198
2199 }
2200
2201
2202 /*********************************************************************
2203  *
2204  * Function    :  receive_encrypted_request
2205  *
2206  * Description :  Receives an encrypted request.
2207  *
2208  * Parameters  :
2209  *          1  :  csp = Current client state (buffers, headers, etc...)
2210  *
2211  * Returns     :  JB_ERR_OK on success,
2212  *                JB_ERR_PARSE or JB_ERR_MEMORY otherwise
2213  *
2214  *********************************************************************/
2215 static jb_err receive_encrypted_request(struct client_state *csp)
2216 {
2217    char buf[BUFFER_SIZE];
2218    int len;
2219    char *p;
2220
2221    do
2222    {
2223       log_error(LOG_LEVEL_HEADER, "Reading encrypted headers");
2224       if (!is_ssl_pending(&(csp->mbedtls_client_attr.ssl)) &&
2225           !data_is_available(csp->cfd, csp->config->socket_timeout))
2226       {
2227          log_error(LOG_LEVEL_CONNECT,
2228             "Socket %d timed out while waiting for client headers", csp->cfd);
2229          return JB_ERR_PARSE;
2230       }
2231       len = ssl_recv_data(&(csp->mbedtls_client_attr.ssl),
2232          (unsigned char *)buf, sizeof(buf));
2233       if (len == -1)
2234       {
2235          return JB_ERR_PARSE;
2236       }
2237       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
2238       {
2239          return JB_ERR_MEMORY;
2240       }
2241       p = strstr(csp->client_iob->cur, "\r\n\r\n");
2242    } while (p == NULL);
2243
2244    log_error(LOG_LEVEL_HEADER, "Encrypted headers received completely");
2245
2246    return JB_ERR_OK;
2247 }
2248
2249
2250 /*********************************************************************
2251  *
2252  * Function    :  process_encrypted_request
2253  *
2254  * Description :  Receives and parses an encrypted request.
2255  *
2256  * Parameters  :
2257  *          1  :  csp = Current client state (buffers, headers, etc...)
2258  *
2259  * Returns     :  JB_ERR_OK on success,
2260  *                JB_ERR_PARSE or JB_ERR_MEMORY otherwise
2261  *
2262  *********************************************************************/
2263 static jb_err process_encrypted_request(struct client_state *csp)
2264 {
2265    char *p;
2266    char *request_line;
2267    jb_err err;
2268    /* Temporary copy of the client's headers before they get enlisted in csp->https_headers */
2269    struct list header_list;
2270    struct list *headers = &header_list;
2271
2272    err = receive_encrypted_request(csp);
2273    if (err != JB_ERR_OK)
2274    {
2275       /* XXX: Also used for JB_ERR_MEMORY */
2276       log_error(LOG_LEVEL_ERROR, "Failed to receive encrypted request: %s",
2277          jb_err_to_string(err));
2278       ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2279          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2280       return err;
2281    }
2282
2283    /* We don't need get_request_line() because the whole HTTP head is buffered. */
2284    request_line = get_header(csp->client_iob);
2285    if (request_line == NULL)
2286    {
2287       log_error(LOG_LEVEL_ERROR, "Failed to get the encrypted request line");
2288       ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2289          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2290       return JB_ERR_PARSE;
2291    }
2292    assert(*request_line != '\0');
2293
2294    if (client_protocol_is_unsupported(csp, request_line))
2295    {
2296       /*
2297        * If the protocol is unsupported we're done here.
2298        * client_protocol_is_unsupported() took care of sending
2299        * the error response and logging the error message.
2300        */
2301       return JB_ERR_PARSE;
2302    }
2303
2304 #ifdef FEATURE_FORCE_LOAD
2305    if (force_required(csp, request_line))
2306    {
2307       csp->flags |= CSP_FLAG_FORCED;
2308    }
2309 #endif /* def FEATURE_FORCE_LOAD */
2310
2311    free_http_request(csp->http);
2312
2313    err = parse_http_request(request_line, csp->http);
2314    /* XXX: Restore ssl setting. This is ugly */
2315    csp->http->client_ssl = 1;
2316    csp->http->server_ssl = 1;
2317
2318    freez(request_line);
2319    if (JB_ERR_OK != err)
2320    {
2321       ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2322          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2323       /* XXX: Use correct size */
2324       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
2325       log_error(LOG_LEVEL_ERROR,
2326          "Couldn't parse request line received from %s: %s",
2327          csp->ip_addr_str, jb_err_to_string(err));
2328
2329       free_http_request(csp->http);
2330       return JB_ERR_PARSE;
2331    }
2332
2333    /* Parse the rest of the client's headers. */
2334    init_list(headers);
2335    for (;;)
2336    {
2337       p = get_header(csp->client_iob);
2338
2339       if (p == NULL)
2340       {
2341          /* There are no additional headers to read. */
2342          break;
2343       }
2344       enlist(headers, p);
2345       freez(p);
2346    }
2347
2348    if (JB_ERR_OK != get_destination_from_https_headers(headers, csp->http))
2349    {
2350       /*
2351        * Our attempts to get the request destination
2352        * elsewhere failed.
2353        */
2354       log_error(LOG_LEVEL_ERROR,
2355          "Failed to get the encrypted request destination");
2356       ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2357          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2358       return JB_ERR_PARSE;
2359    }
2360
2361    /* Split the domain we just got for pattern matching */
2362    init_domain_components(csp->http);
2363
2364 #ifdef FEATURE_TOGGLE
2365    if ((csp->flags & CSP_FLAG_TOGGLED_ON) != 0)
2366 #endif
2367    {
2368       /* Determine the actions for this URL */
2369       get_url_actions(csp, csp->http);
2370    }
2371
2372    enlist(csp->https_headers, csp->http->cmd);
2373
2374    /* Append the previously read headers */
2375    err = list_append_list_unique(csp->https_headers, headers);
2376    destroy_list(headers);
2377    if (JB_ERR_OK != err)
2378    {
2379       /* XXX: Send error message */
2380       return err;
2381    }
2382
2383    /* XXX: Work around crash */
2384    csp->error_message = NULL;
2385
2386    /* XXX: Why do this here? */
2387    csp->http->ssl = 1;
2388
2389    err = sed_https(csp);
2390    if (JB_ERR_OK != err)
2391    {
2392       ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2393          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2394       log_error(LOG_LEVEL_ERROR, "Failed to parse client request from %s.",
2395          csp->ip_addr_str);
2396       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
2397          csp->ip_addr_str, csp->http->cmd);
2398       return JB_ERR_PARSE;
2399    }
2400
2401    log_error(LOG_LEVEL_HEADER, "Encrypted request processed");
2402    log_applied_actions(csp->action);
2403    log_error(LOG_LEVEL_REQUEST, "https://%s%s", csp->http->hostport,
2404       csp->http->path);
2405
2406    return err;
2407
2408 }
2409
2410 /*********************************************************************
2411  *
2412  * Function    :  cgi_page_requested
2413  *
2414  * Description :  Checks if a request is for an internal CGI page.
2415  *
2416  * Parameters  :
2417  *          1  :  host = The host requested by the client.
2418  *
2419  * Returns     :  1 if a CGI page has been requested, 0 otherwise
2420  *
2421  *********************************************************************/
2422 static int cgi_page_requested(const char *host)
2423 {
2424    if ((0 == strcmpic(host, CGI_SITE_1_HOST))
2425     || (0 == strcmpic(host, CGI_SITE_1_HOST "."))
2426     || (0 == strcmpic(host, CGI_SITE_2_HOST))
2427     || (0 == strcmpic(host, CGI_SITE_2_HOST ".")))
2428    {
2429       return 1;
2430    }
2431
2432    return 0;
2433
2434 }
2435
2436 #endif
2437
2438
2439 /*********************************************************************
2440  *
2441  * Function    :  handle_established_connection
2442  *
2443  * Description :  Shuffle data between client and server once the
2444  *                connection has been established.
2445  *
2446  * Parameters  :
2447  *          1  :  csp = Current client state (buffers, headers, etc...)
2448  *
2449  * Returns     :  Nothing.
2450  *
2451  *********************************************************************/
2452 static void handle_established_connection(struct client_state *csp)
2453 {
2454    char *hdr;
2455    char *p;
2456    int n;
2457 #ifdef HAVE_POLL
2458    struct pollfd poll_fds[2];
2459 #else
2460    fd_set rfds;
2461    jb_socket maxfd;
2462    struct timeval timeout;
2463 #endif
2464    int server_body;
2465    int ms_iis5_hack = 0;
2466    unsigned long long byte_count = 0;
2467    struct http_request *http;
2468    long len = 0; /* for buffer sizes (and negative error codes) */
2469    int buffer_and_filter_content = 0;
2470    unsigned int write_delay;
2471 #ifdef FEATURE_HTTPS_INSPECTION
2472    int ret = 0;
2473    int use_ssl_tunnel = 0;
2474    csp->dont_verify_certificate = 0;
2475
2476    if (csp->http->ssl && !(csp->action->flags & ACTION_HTTPS_INSPECTION))
2477    {
2478       /* Pass encrypted content without filtering. */
2479       use_ssl_tunnel = 1;
2480    }
2481 #endif
2482
2483    /* Skeleton for HTTP response, if we should intercept the request */
2484    struct http_response *rsp;
2485 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2486    int watch_client_socket;
2487 #endif
2488
2489    csp->receive_buffer_size = csp->config->receive_buffer_size;
2490    csp->receive_buffer = zalloc(csp->receive_buffer_size + 1);
2491    if (csp->receive_buffer == NULL)
2492    {
2493       log_error(LOG_LEVEL_ERROR,
2494          "Out of memory. Failed to allocate the receive buffer.");
2495       rsp = cgi_error_memory();
2496       send_crunch_response(csp, rsp);
2497       return;
2498    }
2499
2500    http = csp->http;
2501
2502 #ifndef HAVE_POLL
2503    maxfd = (csp->cfd > csp->server_connection.sfd) ?
2504       csp->cfd : csp->server_connection.sfd;
2505 #endif
2506
2507    /* pass data between the client and server
2508     * until one or the other shuts down the connection.
2509     */
2510
2511    server_body = 0;
2512
2513 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2514    watch_client_socket = 0 == (csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING);
2515 #endif
2516    write_delay = get_write_delay(csp);
2517
2518    for (;;)
2519    {
2520 #ifndef HAVE_POLL
2521 #ifdef __OS2__
2522       /*
2523        * FD_ZERO here seems to point to an errant macro which crashes.
2524        * So do this by hand for now...
2525        */
2526       memset(&rfds,0x00,sizeof(fd_set));
2527 #else
2528       FD_ZERO(&rfds);
2529 #endif
2530 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2531       if (!watch_client_socket)
2532       {
2533          maxfd = csp->server_connection.sfd;
2534       }
2535       else
2536 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2537       {
2538          FD_SET(csp->cfd, &rfds);
2539       }
2540
2541       FD_SET(csp->server_connection.sfd, &rfds);
2542 #endif /* ndef HAVE_POLL */
2543
2544 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2545       if ((csp->flags & CSP_FLAG_CHUNKED)
2546          && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
2547          && ((csp->iob->eod - csp->iob->cur) >= 5)
2548          && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
2549       {
2550          /*
2551           * XXX: This check should be obsolete now,
2552           *      but let's wait a while to be sure.
2553           */
2554          log_error(LOG_LEVEL_CONNECT,
2555             "Looks like we got the last chunk together with "
2556             "the server headers but didn't detect it earlier. "
2557             "We better stop reading.");
2558          byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
2559          csp->expected_content_length = byte_count;
2560          csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
2561       }
2562       if (server_body && server_response_is_complete(csp, byte_count))
2563       {
2564          if (csp->expected_content_length == byte_count)
2565          {
2566             log_error(LOG_LEVEL_CONNECT,
2567                "Done reading from server. Content length: %llu as expected. "
2568                "Bytes most recently read: %d.",
2569                byte_count, len);
2570          }
2571          else
2572          {
2573             log_error(LOG_LEVEL_CONNECT,
2574                "Done reading from server. Expected content length: %llu. "
2575                "Actual content length: %llu. Bytes most recently read: %d.",
2576                csp->expected_content_length, byte_count, len);
2577          }
2578          len = 0;
2579          /*
2580           * XXX: Should not jump around, handle_established_connection()
2581           * is complicated enough already.
2582           */
2583          goto reading_done;
2584       }
2585 #endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
2586
2587 #ifdef HAVE_POLL
2588       poll_fds[0].fd = csp->cfd;
2589 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2590       if (!watch_client_socket)
2591       {
2592          /*
2593           * Ignore incoming data, but still watch out
2594           * for disconnects etc. These flags are always
2595           * implied anyway but explicitly setting them
2596           * doesn't hurt.
2597           */
2598          poll_fds[0].events = POLLERR|POLLHUP;
2599       }
2600       else
2601 #endif
2602       {
2603          poll_fds[0].events = POLLIN;
2604       }
2605       poll_fds[1].fd = csp->server_connection.sfd;
2606       poll_fds[1].events = POLLIN;
2607       n = poll(poll_fds, 2, csp->config->socket_timeout * 1000);
2608 #else
2609       timeout.tv_sec = csp->config->socket_timeout;
2610       timeout.tv_usec = 0;
2611       n = select((int)maxfd + 1, &rfds, NULL, NULL, &timeout);
2612 #endif /* def HAVE_POLL */
2613
2614       /*server or client not responding in timeout */
2615       if (n == 0)
2616       {
2617          log_error(LOG_LEVEL_CONNECT, "Socket timeout %d reached: %s",
2618             csp->config->socket_timeout, http->url);
2619          if ((byte_count == 0) && (http->ssl == 0))
2620          {
2621             send_crunch_response(csp, error_response(csp, "connection-timeout"));
2622          }
2623          mark_server_socket_tainted(csp);
2624 #ifdef FEATURE_HTTPS_INSPECTION
2625          close_client_and_server_ssl_connections(csp);
2626 #endif
2627          return;
2628       }
2629       else if (n < 0)
2630       {
2631 #ifdef HAVE_POLL
2632          log_error(LOG_LEVEL_ERROR, "poll() failed!: %E");
2633 #else
2634          log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
2635 #endif
2636          mark_server_socket_tainted(csp);
2637 #ifdef FEATURE_HTTPS_INSPECTION
2638          close_client_and_server_ssl_connections(csp);
2639 #endif
2640          return;
2641       }
2642
2643       /*
2644        * This is the body of the browser's request,
2645        * just read and write it.
2646        *
2647        * Receives data from browser and sends it to server
2648        *
2649        * XXX: Make sure the client doesn't use pipelining
2650        * behind Privoxy's back.
2651        */
2652 #ifdef HAVE_POLL
2653       if ((poll_fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) != 0)
2654       {
2655          log_error(LOG_LEVEL_CONNECT,
2656             "The client socket %d has become unusable while "
2657             "the server socket %d is still open.",
2658             csp->cfd, csp->server_connection.sfd);
2659          mark_server_socket_tainted(csp);
2660          break;
2661       }
2662
2663       if (poll_fds[0].revents != 0)
2664 #else
2665       if (FD_ISSET(csp->cfd, &rfds))
2666 #endif /* def HAVE_POLL*/
2667       {
2668          int max_bytes_to_read = (int)csp->receive_buffer_size;
2669
2670 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2671          if ((csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ))
2672          {
2673             if (data_is_available(csp->cfd, 0))
2674             {
2675                /*
2676                 * If the next request is already waiting, we have
2677                 * to stop select()ing the client socket. Otherwise
2678                 * we would always return right away and get nothing
2679                 * else done.
2680                 */
2681                watch_client_socket = 0;
2682                log_error(LOG_LEVEL_CONNECT,
2683                   "Stop watching client socket %d. "
2684                   "There's already another request waiting.",
2685                   csp->cfd);
2686                continue;
2687             }
2688             /*
2689              * If the client socket is set, but there's no data
2690              * available on the socket, the client went fishing
2691              * and continuing talking to the server makes no sense.
2692              */
2693             log_error(LOG_LEVEL_CONNECT,
2694                "The client closed socket %d while "
2695                "the server socket %d is still open.",
2696                csp->cfd, csp->server_connection.sfd);
2697             mark_server_socket_tainted(csp);
2698             break;
2699          }
2700          if (csp->expected_client_content_length != 0)
2701          {
2702             if (csp->expected_client_content_length < csp->receive_buffer_size)
2703             {
2704                max_bytes_to_read = (int)csp->expected_client_content_length;
2705             }
2706             log_error(LOG_LEVEL_CONNECT,
2707                "Waiting for up to %d bytes from the client.",
2708                max_bytes_to_read);
2709          }
2710          assert(max_bytes_to_read <= csp->receive_buffer_size);
2711 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2712
2713 #ifdef FEATURE_HTTPS_INSPECTION
2714          if (client_use_ssl(csp))
2715          {
2716             log_error(LOG_LEVEL_CONNECT, "Breaking with TLS/SSL.");
2717             break;
2718          }
2719          else
2720 #endif /* def FEATURE_HTTPS_INSPECTION */
2721          {
2722             len = read_socket(csp->cfd, csp->receive_buffer, max_bytes_to_read);
2723
2724             if (len <= 0)
2725             {
2726                /* XXX: not sure if this is necessary. */
2727                mark_server_socket_tainted(csp);
2728                break; /* "game over, man" */
2729             }
2730
2731 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2732             if (csp->expected_client_content_length != 0)
2733             {
2734                assert(len <= max_bytes_to_read);
2735                csp->expected_client_content_length -= (unsigned)len;
2736                log_error(LOG_LEVEL_CONNECT,
2737                   "Expected client content length set to %llu "
2738                   "after reading %d bytes.",
2739                   csp->expected_client_content_length, len);
2740                if (csp->expected_client_content_length == 0)
2741                {
2742                   log_error(LOG_LEVEL_CONNECT,
2743                      "Done reading from the client.");
2744                   csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
2745                }
2746             }
2747 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2748
2749             if (write_socket(csp->server_connection.sfd, csp->receive_buffer, (size_t)len))
2750             {
2751                log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
2752                mark_server_socket_tainted(csp);
2753                return;
2754             }
2755          }
2756          continue;
2757       }
2758
2759       /*
2760        * The server wants to talk. It could be the header or the body.
2761        * If `hdr' is null, then it's the header otherwise it's the body.
2762        * FIXME: Does `hdr' really mean `host'? No.
2763        */
2764 #ifdef HAVE_POLL
2765       if (poll_fds[1].revents != 0)
2766 #else
2767       if (FD_ISSET(csp->server_connection.sfd, &rfds))
2768 #endif /* HAVE_POLL */
2769       {
2770 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2771          /*
2772           * If we are buffering content, we don't want to eat up to
2773           * buffer-limit bytes if the client no longer cares about them.
2774           * If we aren't buffering, however, a dead client socket will be
2775           * noticed pretty much right away anyway, so we can reduce the
2776           * overhead by skipping the check.
2777           */
2778          if (buffer_and_filter_content && !socket_is_still_alive(csp->cfd))
2779          {
2780 #ifdef _WIN32
2781             log_error(LOG_LEVEL_CONNECT,
2782                "The server still wants to talk, but the client may already have hung up on us.");
2783 #else
2784             log_error(LOG_LEVEL_CONNECT,
2785                "The server still wants to talk, but the client hung up on us.");
2786             mark_server_socket_tainted(csp);
2787 #ifdef FEATURE_HTTPS_INSPECTION
2788             close_client_and_server_ssl_connections(csp);
2789 #endif
2790             return;
2791 #endif /* def _WIN32 */
2792          }
2793 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
2794
2795 #ifdef FEATURE_HTTPS_INSPECTION
2796          /*
2797           * Reading data from standard or secured connection (HTTP/HTTPS)
2798           */
2799          if (server_use_ssl(csp))
2800          {
2801             len = ssl_recv_data(&(csp->mbedtls_server_attr.ssl),
2802                (unsigned char *)csp->receive_buffer, csp->receive_buffer_size);
2803          }
2804          else
2805 #endif
2806          {
2807             len = read_socket(csp->server_connection.sfd, csp->receive_buffer,
2808                (int)csp->receive_buffer_size);
2809          }
2810
2811          if (len < 0)
2812          {
2813             log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
2814
2815             if ((http->ssl && (csp->fwd == NULL))
2816 #ifdef FEATURE_HTTPS_INSPECTION
2817                && use_ssl_tunnel
2818 #endif
2819                 )
2820             {
2821                /*
2822                 * Just hang up. We already confirmed the client's CONNECT
2823                 * request with status code 200 and unencrypted content is
2824                 * no longer welcome.
2825                 */
2826                log_error(LOG_LEVEL_ERROR,
2827                   "CONNECT already confirmed. Unable to tell the client about the problem.");
2828                return;
2829             }
2830             else if (byte_count)
2831             {
2832                /*
2833                 * Just hang up. We already transmitted the original headers
2834                 * and parts of the original content and therefore missed the
2835                 * chance to send an error message (without risking data corruption).
2836                 *
2837                 * XXX: we could retry with a fancy range request here.
2838                 */
2839                log_error(LOG_LEVEL_ERROR, "Already forwarded the original headers. "
2840                   "Unable to tell the client about the problem.");
2841                mark_server_socket_tainted(csp);
2842 #ifdef FEATURE_HTTPS_INSPECTION
2843                close_client_and_server_ssl_connections(csp);
2844 #endif
2845                return;
2846             }
2847             /*
2848              * XXX: Consider handling the cases above the same.
2849              */
2850             mark_server_socket_tainted(csp);
2851             len = 0;
2852          }
2853
2854 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2855          if (csp->flags & CSP_FLAG_CHUNKED)
2856          {
2857             if ((len >= 5) && !memcmp(csp->receive_buffer+len-5, "0\r\n\r\n", 5))
2858             {
2859                /* XXX: this is a temporary hack */
2860                log_error(LOG_LEVEL_CONNECT,
2861                   "Looks like we reached the end of the last chunk. "
2862                   "We better stop reading.");
2863                csp->expected_content_length = byte_count + (unsigned long long)len;
2864                csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
2865             }
2866          }
2867          reading_done:
2868 #endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
2869
2870          /*
2871           * This is guaranteed by allocating with zalloc_or_die()
2872           * and never (intentionally) writing to the last byte.
2873           *
2874           * csp->receive_buffer_size is the size of the part of the
2875           * buffer we intentionally write to, but we actually
2876           * allocated csp->receive_buffer_size+1 bytes so the assertion
2877           * stays within the allocated range.
2878           */
2879          assert(csp->receive_buffer[csp->receive_buffer_size] == '\0');
2880
2881          /*
2882           * Add a trailing zero to let be able to use string operations.
2883           * XXX: do we still need this with filter_popups gone?
2884           */
2885          assert(len <= csp->receive_buffer_size);
2886          csp->receive_buffer[len] = '\0';
2887
2888          /*
2889           * Normally, this would indicate that we've read
2890           * as much as the server has sent us and we can
2891           * close the client connection.  However, Microsoft
2892           * in its wisdom has released IIS/5 with a bug that
2893           * prevents it from sending the trailing \r\n in
2894           * a 302 redirect header (and possibly other headers).
2895           * To work around this if we've haven't parsed
2896           * a full header we'll append a trailing \r\n
2897           * and see if this now generates a valid one.
2898           *
2899           * This hack shouldn't have any impacts.  If we've
2900           * already transmitted the header or if this is a
2901           * SSL connection, then we won't bother with this
2902           * hack.  So we only work on partially received
2903           * headers.  If we append a \r\n and this still
2904           * doesn't generate a valid header, then we won't
2905           * transmit anything to the client.
2906           */
2907          if (len == 0)
2908          {
2909
2910             if (server_body || (http->ssl
2911 #ifdef FEATURE_HTTPS_INSPECTION
2912                   && use_ssl_tunnel
2913 #endif
2914                ))
2915             {
2916                /*
2917                 * If we have been buffering up the document,
2918                 * now is the time to apply content modification
2919                 * and send the result to the client.
2920                 */
2921                if (buffer_and_filter_content)
2922                {
2923                   p = execute_content_filters(csp);
2924                   /*
2925                    * If content filtering fails, use the original
2926                    * buffer and length.
2927                    * (see p != NULL ? p : csp->iob->cur below)
2928                    */
2929                   if (NULL == p)
2930                   {
2931                      csp->content_length = (size_t)(csp->iob->eod - csp->iob->cur);
2932                   }
2933 #ifdef FEATURE_COMPRESSION
2934                   else if ((csp->flags & CSP_FLAG_CLIENT_SUPPORTS_DEFLATE)
2935                      && (csp->content_length > LOWER_LENGTH_LIMIT_FOR_COMPRESSION))
2936                   {
2937                      char *compressed_content = compress_buffer(p,
2938                         (size_t *)&csp->content_length, csp->config->compression_level);
2939                      if (compressed_content != NULL)
2940                      {
2941                         freez(p);
2942                         p = compressed_content;
2943                         csp->flags |= CSP_FLAG_BUFFERED_CONTENT_DEFLATED;
2944                      }
2945                   }
2946 #endif
2947
2948                   if (JB_ERR_OK != update_server_headers(csp))
2949                   {
2950                      log_error(LOG_LEVEL_FATAL,
2951                         "Failed to update server headers. after filtering.");
2952                   }
2953
2954                   hdr = list_to_text(csp->headers);
2955                   if (hdr == NULL)
2956                   {
2957                      /* FIXME Should handle error properly */
2958                      log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
2959                   }
2960
2961 #ifdef FEATURE_HTTPS_INSPECTION
2962                   /*
2963                    * Sending data with standard or secured connection (HTTP/HTTPS)
2964                    */
2965                   if (client_use_ssl(csp))
2966                   {
2967                      if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2968                               (const unsigned char *)hdr, strlen(hdr),
2969                               get_write_delay(csp)) < 0)
2970                         || (ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
2971                               (const unsigned char *) ((p != NULL) ? p : csp->iob->cur),
2972                               csp->content_length, get_write_delay(csp)) < 0))
2973                      {
2974                         log_error(LOG_LEVEL_ERROR, "write modified content to "
2975                            "client over TLS/SSL failed");
2976                         freez(hdr);
2977                         freez(p);
2978                         mark_server_socket_tainted(csp);
2979                         close_client_and_server_ssl_connections(csp);
2980                         return;
2981                      }
2982                   }
2983                   else
2984 #endif /* def FEATURE_HTTPS_INSPECTION */
2985                   {
2986                      if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
2987                       || write_socket_delayed(csp->cfd, ((p != NULL) ? p : csp->iob->cur),
2988                          (size_t)csp->content_length, write_delay))
2989                      {
2990                         log_error(LOG_LEVEL_ERROR, "write modified content to client failed: %E");
2991                         freez(hdr);
2992                         freez(p);
2993                         mark_server_socket_tainted(csp);
2994                         return;
2995                      }
2996                   }
2997
2998                   freez(hdr);
2999                   freez(p);
3000                }
3001
3002                break; /* "game over, man" */
3003             }
3004
3005             /*
3006              * This is NOT the body, so
3007              * Let's pretend the server just sent us a blank line.
3008              */
3009             snprintf(csp->receive_buffer, csp->receive_buffer_size, "\r\n");
3010             len = (int)strlen(csp->receive_buffer);
3011
3012             /*
3013              * Now, let the normal header parsing algorithm below do its
3014              * job.  If it fails, we'll exit instead of continuing.
3015              */
3016
3017             ms_iis5_hack = 1;
3018          }
3019
3020          /*
3021           * If we're in the body of the server document, just write it to
3022           * the client, unless we need to buffer the body for later
3023           * content-filtering.
3024           */
3025          if (server_body || (http->ssl
3026 #ifdef FEATURE_HTTPS_INSPECTION
3027                && use_ssl_tunnel
3028 #endif
3029             ))
3030          {
3031             if (buffer_and_filter_content)
3032             {
3033                /*
3034                 * If there is no memory left for buffering the content, or the buffer limit
3035                 * has been reached, switch to non-filtering mode, i.e. make & write the
3036                 * header, flush the iob and buf, and get out of the way.
3037                 */
3038                if (add_to_iob(csp->iob, csp->config->buffer_limit, csp->receive_buffer, len))
3039                {
3040                   size_t hdrlen;
3041                   long flushed;
3042
3043                   log_error(LOG_LEVEL_INFO,
3044                      "Flushing header and buffers. Stepping back from filtering.");
3045
3046                   hdr = list_to_text(csp->headers);
3047                   if (hdr == NULL)
3048                   {
3049                      /*
3050                       * Memory is too tight to even generate the header.
3051                       * Send our static "Out-of-memory" page.
3052                       */
3053                      log_error(LOG_LEVEL_ERROR, "Out of memory while trying to flush.");
3054                      rsp = cgi_error_memory();
3055                      send_crunch_response(csp, rsp);
3056                      mark_server_socket_tainted(csp);
3057 #ifdef FEATURE_HTTPS_INSPECTION
3058                      close_client_and_server_ssl_connections(csp);
3059 #endif
3060                      return;
3061                   }
3062                   hdrlen = strlen(hdr);
3063
3064 #ifdef FEATURE_HTTPS_INSPECTION
3065                   /*
3066                    * Sending data with standard or secured connection (HTTP/HTTPS)
3067                    */
3068                   if (client_use_ssl(csp))
3069                   {
3070                      if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3071                              (const unsigned char *)hdr, hdrlen, get_write_delay(csp)) < 0)
3072                         || ((flushed = ssl_flush_socket(&(csp->mbedtls_client_attr.ssl),
3073                                 csp->iob)) < 0)
3074                         || (ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3075                               (const unsigned char *)csp->receive_buffer, (size_t)len,
3076                               get_write_delay(csp)) < 0))
3077                      {
3078                         log_error(LOG_LEVEL_CONNECT,
3079                            "Flush header and buffers to client failed");
3080                         freez(hdr);
3081                         mark_server_socket_tainted(csp);
3082                         close_client_and_server_ssl_connections(csp);
3083                         return;
3084                      }
3085                   }
3086                   else
3087 #endif /* def FEATURE_HTTPS_INSPECTION */
3088                   {
3089                      if (write_socket_delayed(csp->cfd, hdr, hdrlen, write_delay)
3090                       || ((flushed = flush_iob(csp->cfd, csp->iob, write_delay)) < 0)
3091                       || write_socket_delayed(csp->cfd, csp->receive_buffer, (size_t)len,
3092                             write_delay))
3093                      {
3094                         log_error(LOG_LEVEL_CONNECT,
3095                            "Flush header and buffers to client failed: %E");
3096                         freez(hdr);
3097                         mark_server_socket_tainted(csp);
3098                         return;
3099                      }
3100                   }
3101
3102                   /*
3103                    * Reset the byte_count to the amount of bytes
3104                    * we just flushed. len will be added a few lines below,
3105                    * hdrlen doesn't matter for LOG_LEVEL_CLF.
3106                    */
3107                   byte_count = (unsigned long long)flushed;
3108                   freez(hdr);
3109                   buffer_and_filter_content = 0;
3110                   server_body = 1;
3111                }
3112             }
3113             else
3114             {
3115 #ifdef FEATURE_HTTPS_INSPECTION
3116                /*
3117                 * Sending data with standard or secured connection (HTTP/HTTPS)
3118                 */
3119                if (client_use_ssl(csp))
3120                {
3121                   ret = ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3122                      (const unsigned char *)csp->receive_buffer, (size_t)len,
3123                      get_write_delay(csp));
3124                   if (ret < 0)
3125                   {
3126                      log_error(LOG_LEVEL_ERROR,
3127                         "Sending data to client failed");
3128                      mark_server_socket_tainted(csp);
3129                      close_client_and_server_ssl_connections(csp);
3130                      return;
3131                   }
3132                }
3133                else
3134 #endif /* def FEATURE_HTTPS_INSPECTION */
3135                {
3136                   if (write_socket_delayed(csp->cfd, csp->receive_buffer,
3137                         (size_t)len, write_delay))
3138                   {
3139                      log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
3140                      mark_server_socket_tainted(csp);
3141                      return;
3142                   }
3143                }
3144             }
3145             byte_count += (unsigned long long)len;
3146             continue;
3147          }
3148          else
3149          {
3150             /*
3151              * We're still looking for the end of the server's header.
3152              * Buffer up the data we just read.  If that fails, there's
3153              * little we can do but send our static out-of-memory page.
3154              */
3155             if (add_to_iob(csp->iob, csp->config->buffer_limit, csp->receive_buffer, len))
3156             {
3157                log_error(LOG_LEVEL_ERROR, "Out of memory while looking for end of server headers.");
3158                rsp = cgi_error_memory();
3159                send_crunch_response(csp, rsp);
3160                mark_server_socket_tainted(csp);
3161 #ifdef FEATURE_HTTPS_INSPECTION
3162                close_client_and_server_ssl_connections(csp);
3163 #endif
3164                return;
3165             }
3166
3167             /* Convert iob into something sed() can digest */
3168             if (JB_ERR_PARSE == get_server_headers(csp))
3169             {
3170                if (ms_iis5_hack)
3171                {
3172                   /*
3173                    * Well, we tried our MS IIS/5 hack and it didn't work.
3174                    * The header is incomplete and there isn't anything
3175                    * we can do about it.
3176                    */
3177                   log_error(LOG_LEVEL_ERROR, "Invalid server headers. "
3178                      "Applying the MS IIS5 hack didn't help.");
3179                   log_error(LOG_LEVEL_CLF,
3180                      "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3181 #ifdef FEATURE_HTTPS_INSPECTION
3182                   /*
3183                    * Sending data with standard or secured connection (HTTP/HTTPS)
3184                    */
3185                   if (client_use_ssl(csp))
3186                   {
3187                      ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3188                         (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3189                         strlen(INVALID_SERVER_HEADERS_RESPONSE), get_write_delay(csp));
3190                   }
3191                   else
3192 #endif /* def FEATURE_HTTPS_INSPECTION */
3193                   {
3194                      write_socket_delayed(csp->cfd,
3195                         INVALID_SERVER_HEADERS_RESPONSE,
3196                         strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3197                   }
3198                   mark_server_socket_tainted(csp);
3199 #ifdef FEATURE_HTTPS_INSPECTION
3200                   close_client_and_server_ssl_connections(csp);
3201 #endif
3202                   return;
3203                }
3204                else
3205                {
3206                   /*
3207                    * Since we have to wait for more from the server before
3208                    * we can parse the headers we just continue here.
3209                    */
3210                   log_error(LOG_LEVEL_CONNECT,
3211                      "Continuing buffering server headers from socket %d. "
3212                      "Bytes most recently read: %d.", csp->cfd, len);
3213                   continue;
3214                }
3215             }
3216             else
3217             {
3218                /*
3219                 * Account for the content bytes we
3220                 * might have gotten with the headers.
3221                 */
3222                assert(csp->iob->eod >= csp->iob->cur);
3223                byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3224             }
3225
3226             /* Did we actually get anything? */
3227             if (NULL == csp->headers->first)
3228             {
3229                if ((csp->flags & CSP_FLAG_REUSED_CLIENT_CONNECTION))
3230                {
3231                   log_error(LOG_LEVEL_ERROR,
3232                      "No server or forwarder response received on socket %d. "
3233                      "Closing client socket %d without sending data.",
3234                      csp->server_connection.sfd, csp->cfd);
3235                   log_error(LOG_LEVEL_CLF,
3236                      "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3237                }
3238                else
3239                {
3240                   log_error(LOG_LEVEL_ERROR,
3241                      "No server or forwarder response received on socket %d.",
3242                      csp->server_connection.sfd);
3243                   send_crunch_response(csp, error_response(csp, "no-server-data"));
3244                }
3245                free_http_request(http);
3246                mark_server_socket_tainted(csp);
3247 #ifdef FEATURE_HTTPS_INSPECTION
3248                close_client_and_server_ssl_connections(csp);
3249 #endif
3250                return;
3251             }
3252
3253             if (!csp->headers->first->str)
3254             {
3255                log_error(LOG_LEVEL_ERROR, "header search: csp->headers->first->str == NULL, assert will be called");
3256             }
3257             assert(csp->headers->first->str);
3258
3259             if (strncmpic(csp->headers->first->str, "HTTP", 4) &&
3260                 strncmpic(csp->headers->first->str, "ICY", 3))
3261             {
3262                /*
3263                 * It doesn't look like a HTTP (or Shoutcast) response:
3264                 * tell the client and log the problem.
3265                 */
3266                if (strlen(csp->headers->first->str) > 30)
3267                {
3268                   csp->headers->first->str[30] = '\0';
3269                }
3270                log_error(LOG_LEVEL_ERROR,
3271                   "Invalid server or forwarder response. Starts with: %s",
3272                   csp->headers->first->str);
3273                log_error(LOG_LEVEL_CLF,
3274                   "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3275 #ifdef FEATURE_HTTPS_INSPECTION
3276                /*
3277                 * Sending data with standard or secured connection (HTTP/HTTPS)
3278                 */
3279                if (client_use_ssl(csp))
3280                {
3281                   ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3282                      (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3283                      strlen(INVALID_SERVER_HEADERS_RESPONSE),
3284                      get_write_delay(csp));
3285                }
3286                else
3287 #endif /* def FEATURE_HTTPS_INSPECTION */
3288                {
3289                   write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3290                      strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3291                }
3292                free_http_request(http);
3293                mark_server_socket_tainted(csp);
3294 #ifdef FEATURE_HTTPS_INSPECTION
3295                close_client_and_server_ssl_connections(csp);
3296 #endif
3297                return;
3298             }
3299
3300             /*
3301              * We have now received the entire server header,
3302              * filter it and send the result to the client
3303              */
3304             if (JB_ERR_OK != sed(csp, FILTER_SERVER_HEADERS))
3305             {
3306                log_error(LOG_LEVEL_CLF,
3307                   "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3308 #ifdef FEATURE_HTTPS_INSPECTION
3309                /*
3310                 * Sending data with standard or secured connection (HTTP/HTTPS)
3311                 */
3312                if (client_use_ssl(csp))
3313                {
3314                   ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3315                      (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3316                      strlen(INVALID_SERVER_HEADERS_RESPONSE),
3317                      get_write_delay(csp));
3318                }
3319                else
3320 #endif
3321                {
3322                   write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3323                      strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3324                }
3325                free_http_request(http);
3326                mark_server_socket_tainted(csp);
3327 #ifdef FEATURE_HTTPS_INSPECTION
3328                close_client_and_server_ssl_connections(csp);
3329 #endif
3330                return;
3331             }
3332             hdr = list_to_text(csp->headers);
3333             if (hdr == NULL)
3334             {
3335                /* FIXME Should handle error properly */
3336                log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
3337             }
3338
3339             if ((csp->flags & CSP_FLAG_CHUNKED)
3340                && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
3341                && ((csp->iob->eod - csp->iob->cur) >= 5)
3342                && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
3343             {
3344                log_error(LOG_LEVEL_CONNECT,
3345                   "Looks like we got the last chunk together with "
3346                   "the server headers. We better stop reading.");
3347                byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3348                csp->expected_content_length = byte_count;
3349                csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
3350             }
3351
3352             csp->server_connection.response_received = time(NULL);
3353
3354             if (crunch_response_triggered(csp, crunchers_light))
3355             {
3356                /*
3357                 * One of the tags created by a server-header
3358                 * tagger triggered a crunch. We already
3359                 * delivered the crunch response to the client
3360                 * and are done here after cleaning up.
3361                 */
3362                freez(hdr);
3363                mark_server_socket_tainted(csp);
3364 #ifdef FEATURE_HTTPS_INSPECTION
3365                close_client_and_server_ssl_connections(csp);
3366 #endif
3367                return;
3368             }
3369
3370             /* Buffer and pcrs filter this if appropriate. */
3371             buffer_and_filter_content = content_requires_filtering(csp);
3372
3373             if (!buffer_and_filter_content)
3374             {
3375                /*
3376                 * Write the server's (modified) header to
3377                 * the client (along with anything else that
3378                 * may be in the buffer). Use standard or secured
3379                 * connection.
3380                 */
3381 #ifdef FEATURE_HTTPS_INSPECTION
3382                if (client_use_ssl(csp))
3383                {
3384                   if ((ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3385                           (const unsigned char *)hdr, strlen(hdr),
3386                           get_write_delay(csp)) < 0)
3387                      || (len = ssl_flush_socket(&(csp->mbedtls_client_attr.ssl),
3388                             csp->iob) < 0))
3389                   {
3390                      log_error(LOG_LEVEL_CONNECT, "Write header to client failed");
3391
3392                      /*
3393                       * The write failed, so don't bother mentioning it
3394                       * to the client... it probably can't hear us anyway.
3395                       */
3396                      freez(hdr);
3397                      mark_server_socket_tainted(csp);
3398 #ifdef FEATURE_HTTPS_INSPECTION
3399                      close_client_and_server_ssl_connections(csp);
3400 #endif
3401                      return;
3402                   }
3403                }
3404                else
3405 #endif /* def FEATURE_HTTPS_INSPECTION */
3406                {
3407                   if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
3408                      || ((len = flush_iob(csp->cfd, csp->iob, write_delay)) < 0))
3409                   {
3410                      log_error(LOG_LEVEL_ERROR,
3411                         "write header to client failed");
3412                      /*
3413                       * The write failed, so don't bother mentioning it
3414                       * to the client... it probably can't hear us anyway.
3415                       */
3416                      freez(hdr);
3417                      mark_server_socket_tainted(csp);
3418                      return;
3419                   }
3420                }
3421                                 }
3422
3423             /* we're finished with the server's header */
3424
3425             freez(hdr);
3426             server_body = 1;
3427
3428             /*
3429              * If this was a MS IIS/5 hack then it means the server
3430              * has already closed the connection. Nothing more to read.
3431              * Time to bail.
3432              */
3433             if (ms_iis5_hack)
3434             {
3435                log_error(LOG_LEVEL_ERROR,
3436                   "Closed server connection detected. "
3437                   "Applying the MS IIS5 hack didn't help.");
3438                log_error(LOG_LEVEL_CLF,
3439                   "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3440 #ifdef FEATURE_HTTPS_INSPECTION
3441                /*
3442                 * Sending data with standard or secured connection (HTTP/HTTPS)
3443                 */
3444                if (client_use_ssl(csp))
3445                {
3446                   ssl_send_data_delayed(&(csp->mbedtls_client_attr.ssl),
3447                      (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3448                      strlen(INVALID_SERVER_HEADERS_RESPONSE),
3449                      get_write_delay(csp));
3450                }
3451                else
3452 #endif /* def FEATURE_HTTPS_INSPECTION */
3453                {
3454                   write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3455                      strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3456                }
3457                mark_server_socket_tainted(csp);
3458 #ifdef FEATURE_HTTPS_INSPECTION
3459                close_client_and_server_ssl_connections(csp);
3460 #endif
3461                return;
3462             }
3463          }
3464          continue;
3465       }
3466       mark_server_socket_tainted(csp);
3467 #ifdef FEATURE_HTTPS_INSPECTION
3468       close_client_and_server_ssl_connections(csp);
3469 #endif
3470       return; /* huh? we should never get here */
3471    }
3472 #ifdef FEATURE_HTTPS_INSPECTION
3473    close_client_and_server_ssl_connections(csp);
3474 #endif
3475    if (csp->content_length == 0)
3476    {
3477       /*
3478        * If Privoxy didn't recalculate the Content-Length,
3479        * byte_count is still correct.
3480        */
3481       csp->content_length = byte_count;
3482    }
3483
3484 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3485    if ((csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
3486       && (csp->expected_content_length != byte_count))
3487    {
3488       log_error(LOG_LEVEL_CONNECT,
3489          "Received %llu bytes while expecting %llu.",
3490          byte_count, csp->expected_content_length);
3491       mark_server_socket_tainted(csp);
3492    }
3493 #endif
3494
3495 #ifdef FEATURE_HTTPS_INSPECTION
3496    if (client_use_ssl(csp))
3497    {
3498       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" 200 %llu",
3499          csp->ip_addr_str, http->gpc, http->hostport, http->path,
3500          http->version, csp->content_length);
3501    }
3502    else
3503 #endif
3504    {
3505       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %llu",
3506          csp->ip_addr_str, http->ocmd, csp->content_length);
3507    }
3508    csp->server_connection.timestamp = time(NULL);
3509 }
3510
3511
3512 /*********************************************************************
3513  *
3514  * Function    :  chat
3515  *
3516  * Description :  Once a connection from the client has been accepted,
3517  *                this function is called (via serve()) to handle the
3518  *                main business of the communication.  This function
3519  *                returns after dealing with a single request. It can
3520  *                be called multiple times with the same client socket
3521  *                if the client is keeping the connection alive.
3522  *
3523  *                The decision whether or not a client connection will
3524  *                be kept alive is up to the caller which also must
3525  *                close the client socket when done.
3526  *
3527  *                FIXME: chat is nearly thousand lines long.
3528  *                Ridiculous.
3529  *
3530  * Parameters  :
3531  *          1  :  csp = Current client state (buffers, headers, etc...)
3532  *
3533  * Returns     :  Nothing.
3534  *
3535  *********************************************************************/
3536 static void chat(struct client_state *csp)
3537 {
3538    const struct forward_spec *fwd;
3539    struct http_request *http;
3540    /* Skeleton for HTTP response, if we should intercept the request */
3541    struct http_response *rsp;
3542 #ifdef FEATURE_HTTPS_INSPECTION
3543    int use_ssl_tunnel = 0;
3544 #endif
3545
3546    http = csp->http;
3547
3548    if (receive_client_request(csp) != JB_ERR_OK)
3549    {
3550       return;
3551    }
3552    if (parse_client_request(csp) != JB_ERR_OK)
3553    {
3554       return;
3555    }
3556
3557    /* decide how to route the HTTP request */
3558    fwd = forward_url(csp, http);
3559    if (NULL == fwd)
3560    {
3561       log_error(LOG_LEVEL_FATAL, "gateway spec is NULL!?!?  This can't happen!");
3562       /* Never get here - LOG_LEVEL_FATAL causes program exit */
3563       return;
3564    }
3565
3566 #ifdef FEATURE_HTTPS_INSPECTION
3567    /*
3568     * Setting flags to use old solution with SSL tunnel and to disable
3569     * certificate verification.
3570     */
3571    if (csp->http->ssl && !(csp->action->flags & ACTION_HTTPS_INSPECTION)
3572       && !cgi_page_requested(csp->http->host))
3573    {
3574       use_ssl_tunnel = 1;
3575    }
3576
3577    if (http->ssl && csp->action->flags & ACTION_IGNORE_CERTIFICATE_ERRORS)
3578    {
3579       csp->dont_verify_certificate = 1;
3580    }
3581 #endif
3582
3583    /*
3584     * build the http request to send to the server
3585     * we have to do one of the following:
3586     *
3587     * create =    use the original HTTP request to create a new
3588     *             HTTP request that has either the path component
3589     *             without the http://domainspec (w/path) or the
3590     *             full orininal URL (w/url)
3591     *             Note that the path and/or the HTTP version may
3592     *             have been altered by now.
3593     *
3594     * SSL proxy = Open a socket to the host:port of the server
3595     *             and create TLS/SSL connection with server and
3596     *             with client. Then behave like mediator between
3597     *             client and server over TLS/SSL.
3598     *
3599     * SSL proxy = Pass the request unchanged if forwarding a CONNECT
3600     *    with     request to a parent proxy. Note that we'll be sending
3601     * forwarding  the CFAIL message ourselves if connecting to the parent
3602     *             fails, but we won't send a CSUCCEED message if it works,
3603     *             since that would result in a double message (ours and the
3604     *             parent's). After sending the request to the parent, we
3605     *             must parse answer and send it to client. If connection
3606     *             with server is established, we do TLS/SSL proxy. Otherwise
3607     *             we send parent response to client and close connections.
3608     *
3609     * here's the matrix:
3610     *                        SSL
3611     *                    0        1
3612     *                +--------+--------+
3613     *                |        |        |
3614     *             0  | create |   SSL  |
3615     *                | w/path |  proxy |
3616     *  Forwarding    +--------+--------+
3617     *                |        |   SSL  |
3618     *             1  | create |  proxy |
3619     *                | w/url  |+forward|
3620     *                +--------+--------+
3621     *
3622     */
3623
3624 #ifdef FEATURE_HTTPS_INSPECTION
3625    /*
3626     * Presetting SSL client and server flags
3627     */
3628    if (http->ssl && !use_ssl_tunnel)
3629    {
3630       http->client_ssl = 1;
3631       http->server_ssl = 1;
3632    }
3633    else
3634    {
3635       http->client_ssl = 0;
3636       http->server_ssl = 0;
3637    }
3638 #endif
3639
3640 #ifdef FEATURE_HTTPS_INSPECTION
3641    /*
3642     * Log the request unless we're https inspecting
3643     * in which case we don't have the path yet and
3644     * will log the request later.
3645     */
3646    if (!client_use_ssl(csp))
3647 #endif
3648    {
3649       log_error(LOG_LEVEL_REQUEST, "%s%s", http->hostport, http->path);
3650    }
3651
3652    if (http->ssl && connect_port_is_forbidden(csp))
3653    {
3654       const char *acceptable_connect_ports =
3655          csp->action->string[ACTION_STRING_LIMIT_CONNECT];
3656       assert(NULL != acceptable_connect_ports);
3657       log_error(LOG_LEVEL_INFO, "Request from %s marked for blocking. "
3658          "limit-connect{%s} doesn't allow CONNECT requests to %s",
3659          csp->ip_addr_str, acceptable_connect_ports, csp->http->hostport);
3660       csp->action->flags |= ACTION_BLOCK;
3661       http->ssl = 0;
3662 #ifdef FEATURE_HTTPS_INSPECTION
3663       http->client_ssl = 0;
3664       http->server_ssl = 0;
3665 #endif
3666    }
3667
3668
3669    freez(csp->headers->first->str);
3670    build_request_line(csp, fwd, &csp->headers->first->str);
3671
3672    /*
3673     * We have a request. Check if one of the crunchers wants it
3674     * unless the client wants to use TLS/SSL in which case we
3675     * haven't setup the TLS context yet and will send the crunch
3676     * response later.
3677     */
3678    if (
3679 #ifdef FEATURE_HTTPS_INSPECTION
3680        !client_use_ssl(csp) &&
3681 #endif
3682        crunch_response_triggered(csp, crunchers_all))
3683    {
3684       /*
3685        * Yes. The client got the crunch response and we're done here.
3686        */
3687       return;
3688    }
3689
3690    log_applied_actions(csp->action);
3691    if (fwd->forward_host)
3692    {
3693       log_error(LOG_LEVEL_CONNECT, "via [%s]:%d to: %s",
3694          fwd->forward_host, fwd->forward_port, http->hostport);
3695    }
3696    else
3697    {
3698       log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
3699    }
3700
3701    /* here we connect to the server, gateway, or the forwarder */
3702
3703 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3704    if ((csp->server_connection.sfd != JB_INVALID_SOCKET)
3705       && socket_is_still_alive(csp->server_connection.sfd)
3706       && connection_destination_matches(&csp->server_connection, http, fwd))
3707    {
3708       log_error(LOG_LEVEL_CONNECT,
3709          "Reusing server socket %d connected to %s. Total requests: %u.",
3710          csp->server_connection.sfd, csp->server_connection.host,
3711          csp->server_connection.requests_sent_total);
3712    }
3713    else
3714    {
3715       if (csp->server_connection.sfd != JB_INVALID_SOCKET)
3716       {
3717 #ifdef FEATURE_CONNECTION_SHARING
3718          if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
3719          {
3720             remember_connection(&csp->server_connection);
3721          }
3722          else
3723 #endif /* def FEATURE_CONNECTION_SHARING */
3724          {
3725             log_error(LOG_LEVEL_CONNECT,
3726                "Closing server socket %d connected to %s. Total requests: %u.",
3727                csp->server_connection.sfd, csp->server_connection.host,
3728                csp->server_connection.requests_sent_total);
3729             close_socket(csp->server_connection.sfd);
3730          }
3731          mark_connection_closed(&csp->server_connection);
3732       }
3733 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3734 #ifdef FEATURE_HTTPS_INSPECTION
3735       if (http->ssl && !use_ssl_tunnel)
3736       {
3737          int ret;
3738          /*
3739           * Creating a SSL proxy.
3740           *
3741           * By sending the CSUCCEED message we're lying to the client as
3742           * the connection hasn't actually been established yet. We don't
3743           * establish the connection until we have seen and parsed the
3744           * encrypted client headers.
3745           */
3746          if (write_socket_delayed(csp->cfd, CSUCCEED,
3747                strlen(CSUCCEED), get_write_delay(csp)) != 0)
3748          {
3749             log_error(LOG_LEVEL_ERROR, "Sending SUCCEED to client failed");
3750             return;
3751          }
3752
3753          ret = create_client_ssl_connection(csp);
3754          if (ret != 0)
3755          {
3756             log_error(LOG_LEVEL_ERROR,
3757                "Failed to open a secure connection with the client");
3758             return;
3759          }
3760          if (JB_ERR_OK != process_encrypted_request(csp))
3761          {
3762             log_error(LOG_LEVEL_ERROR, "Failed to parse encrypted request.");
3763             close_client_ssl_connection(csp);
3764             return;
3765          }
3766          /*
3767           * We have an encrypted request. Check if one of the crunchers now
3768           * wants it (for example because the previously invisible path was
3769           * required to match).
3770           */
3771          if (crunch_response_triggered(csp, crunchers_all))
3772          {
3773             /*
3774              * Yes. The client got the crunch response and we're done here.
3775              */
3776             close_client_ssl_connection(csp);
3777             return;
3778