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