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