receive_client_request(): Improve error message
[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-2021 the
9  *                Privoxy team. https://www.privoxy.org/
10  *
11  *                Based on the Internet Junkbuster originally written
12  *                by and Copyright (C) 1997 Anonymous Coders and
13  *                Junkbusters Corporation.  http://www.junkbusters.com
14  *
15  *                This program is free software; you can redistribute it
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  *********************************************************************/
34
35
36 #include "config.h"
37
38 #include <stdio.h>
39 #include <sys/types.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <signal.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <assert.h>
46
47 #ifdef _WIN32
48 # ifndef FEATURE_PTHREAD
49 #  ifndef STRICT
50 #   define STRICT
51 #  endif
52 #  include <winsock2.h>
53 #  include <windows.h>
54 #  include <process.h>
55 # endif /* ndef FEATURE_PTHREAD */
56
57 # include "win32.h"
58 # ifndef _WIN_CONSOLE
59 #  include "w32log.h"
60 # endif /* ndef _WIN_CONSOLE */
61 # include "w32svrapi.h"
62
63 #else /* ifndef _WIN32 */
64
65 # include <unistd.h>
66 # include <sys/wait.h>
67 # include <sys/time.h>
68 # include <sys/stat.h>
69 # include <sys/ioctl.h>
70
71 #ifdef sun
72 #include <sys/termios.h>
73 #endif /* sun */
74
75 #ifdef unix
76 #include <pwd.h>
77 #include <grp.h>
78 #endif
79
80 # include <signal.h>
81
82 # ifdef __BEOS__
83 #  include <socket.h>  /* BeOS has select() for sockets only. */
84 #  include <OS.h>      /* declarations for threads and stuff. */
85 # endif
86
87 #ifdef HAVE_POLL
88 #ifdef __GLIBC__
89 #include <sys/poll.h>
90 #else
91 #include <poll.h>
92 #endif /* def __GLIBC__ */
93 #else
94 # ifndef FD_ZERO
95 #  include <select.h>
96 # endif
97 #warning poll() appears to be unavailable. Your platform will become unsupported in the future.
98 #endif /* HAVE_POLL */
99
100 #endif
101
102 #include "project.h"
103 #include "list.h"
104 #include "jcc.h"
105 #ifdef FEATURE_HTTPS_INSPECTION
106 #include "ssl.h"
107 #endif
108 #include "filters.h"
109 #include "loaders.h"
110 #include "parsers.h"
111 #include "miscutil.h"
112 #include "errlog.h"
113 #include "jbsockets.h"
114 #include "gateway.h"
115 #include "actions.h"
116 #include "cgi.h"
117 #include "loadcfg.h"
118 #include "urlmatch.h"
119 #ifdef FEATURE_CLIENT_TAGS
120 #include "client-tags.h"
121 #endif
122
123 int daemon_mode = 1;
124 struct client_states clients[1];
125 struct file_list     files[1];
126
127 #ifdef FEATURE_STATISTICS
128 int urls_read     = 0;     /* total nr of urls read inc rejected */
129 int urls_rejected = 0;     /* total nr of urls rejected */
130 #endif /* def FEATURE_STATISTICS */
131
132 #ifdef FEATURE_GRACEFUL_TERMINATION
133 int g_terminate = 0;
134 #endif
135
136 #if !defined(_WIN32)
137 static void sig_handler(int the_signal);
138 #endif
139 static int client_protocol_is_unsupported(struct client_state *csp, char *req);
140 static jb_err get_request_destination_elsewhere(struct client_state *csp, struct list *headers);
141 static jb_err get_server_headers(struct client_state *csp);
142 static const char *crunch_reason(const struct http_response *rsp);
143 static void send_crunch_response(struct client_state *csp, struct http_response *rsp);
144 static char *get_request_line(struct client_state *csp);
145 static jb_err receive_client_request(struct client_state *csp);
146 static jb_err parse_client_request(struct client_state *csp);
147 static void build_request_line(struct client_state *csp, const struct forward_spec *fwd, char **request_line);
148 static jb_err change_request_destination(struct client_state *csp);
149 static void handle_established_connection(struct client_state *csp);
150 static void chat(struct client_state *csp);
151 static void serve(struct client_state *csp);
152 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
153 static void usage(const char *myname);
154 #endif
155 static void initialize_mutexes(void);
156 static jb_socket bind_port_helper(const char *haddr, int hport, int backlog);
157 static void bind_ports_helper(struct configuration_spec *config, jb_socket sockets[]);
158 static void close_ports_helper(jb_socket sockets[]);
159 static void listen_loop(void);
160 static void serve(struct client_state *csp);
161
162 #ifdef __BEOS__
163 static int32 server_thread(void *data);
164 #endif /* def __BEOS__ */
165
166 #ifdef _WIN32
167 #define sleep(N)  Sleep(((N) * 1000))
168 #endif
169
170 #ifdef FUZZ
171 int process_fuzzed_input(char *fuzz_input_type, char *fuzz_input_file);
172 void show_fuzz_usage(const char *name);
173 #endif
174
175 #ifdef MUTEX_LOCKS_AVAILABLE
176 /*
177  * XXX: Does the locking stuff really belong in this file?
178  */
179 privoxy_mutex_t log_mutex;
180 privoxy_mutex_t log_init_mutex;
181 privoxy_mutex_t connection_reuse_mutex;
182
183 #ifdef FEATURE_HTTPS_INSPECTION
184 privoxy_mutex_t certificate_mutex;
185 privoxy_mutex_t ssl_init_mutex;
186 #endif
187
188 #ifdef FEATURE_EXTERNAL_FILTERS
189 privoxy_mutex_t external_filter_mutex;
190 #endif
191 #ifdef FEATURE_CLIENT_TAGS
192 privoxy_mutex_t client_tags_mutex;
193 #endif
194 #ifdef FEATURE_EXTENDED_STATISTICS
195 privoxy_mutex_t filter_statistics_mutex;
196 privoxy_mutex_t block_statistics_mutex;
197 #endif
198
199 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R)
200 privoxy_mutex_t resolver_mutex;
201 #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */
202
203 #ifndef HAVE_GMTIME_R
204 privoxy_mutex_t gmtime_mutex;
205 #endif /* ndef HAVE_GMTIME_R */
206
207 #ifndef HAVE_LOCALTIME_R
208 privoxy_mutex_t localtime_mutex;
209 #endif /* ndef HAVE_GMTIME_R */
210
211 #if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM)
212 privoxy_mutex_t rand_mutex;
213 #endif /* !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM) */
214
215 #endif /* def MUTEX_LOCKS_AVAILABLE */
216
217 #if defined(unix)
218 const char *basedir = NULL;
219 const char *pidfile = NULL;
220 static int received_hup_signal = 0;
221 #endif /* defined unix */
222
223 /* HTTP snipplets. */
224 static const char CSUCCEED[] =
225    "HTTP/1.1 200 Connection established\r\n\r\n";
226
227 static const char CHEADER[] =
228    "HTTP/1.1 400 Invalid header received from client\r\n"
229    "Content-Type: text/plain\r\n"
230    "Connection: close\r\n\r\n"
231    "Invalid header received from client.\n";
232
233 static const char FTP_RESPONSE[] =
234    "HTTP/1.1 400 Invalid request received from client\r\n"
235    "Content-Type: text/plain\r\n"
236    "Connection: close\r\n\r\n"
237    "Invalid request. Privoxy doesn't support FTP.\n";
238
239 static const char GOPHER_RESPONSE[] =
240    "HTTP/1.1 400 Invalid request received from client\r\n"
241    "Content-Type: text/plain\r\n"
242    "Connection: close\r\n\r\n"
243    "Invalid request. Privoxy doesn't support gopher.\n";
244
245 /* XXX: should be a template */
246 static const char MISSING_DESTINATION_RESPONSE[] =
247    "HTTP/1.1 400 Bad request received from client\r\n"
248    "Content-Type: text/plain\r\n"
249    "Connection: close\r\n\r\n"
250    "Bad request. Privoxy was unable to extract the destination.\n";
251
252 /* XXX: should be a template */
253 static const char INVALID_SERVER_HEADERS_RESPONSE[] =
254    "HTTP/1.1 502 Server or forwarder response invalid\r\n"
255    "Content-Type: text/plain\r\n"
256    "Connection: close\r\n\r\n"
257    "Bad response. The server or forwarder response doesn't look like HTTP.\n";
258
259 /* XXX: should be a template */
260 static const char MESSED_UP_REQUEST_RESPONSE[] =
261    "HTTP/1.1 400 Malformed request after rewriting\r\n"
262    "Content-Type: text/plain\r\n"
263    "Connection: close\r\n\r\n"
264    "Bad request. Messed up with header filters.\n";
265
266 static const char TOO_MANY_CONNECTIONS_RESPONSE[] =
267    "HTTP/1.1 503 Too many open connections\r\n"
268    "Content-Type: text/plain\r\n"
269    "Connection: close\r\n\r\n"
270    "Maximum number of open connections reached.\n";
271
272 static const char CLIENT_CONNECTION_TIMEOUT_RESPONSE[] =
273    "HTTP/1.1 504 Connection timeout\r\n"
274    "Content-Type: text/plain\r\n"
275    "Connection: close\r\n\r\n"
276    "The connection timed out because the client request didn't arrive in time.\n";
277
278 static const char CLIENT_BODY_PARSE_ERROR_RESPONSE[] =
279    "HTTP/1.1 400 Failed reading client body\r\n"
280    "Content-Type: text/plain\r\n"
281    "Connection: close\r\n\r\n"
282    "Failed parsing or buffering the chunk-encoded client body.\n";
283
284 static const char UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE[] =
285    "HTTP/1.1 417 Expecting too much\r\n"
286    "Content-Type: text/plain\r\n"
287    "Connection: close\r\n\r\n"
288    "Privoxy detected an unsupported Expect header value.\n";
289
290 /* A function to crunch a response */
291 typedef struct http_response *(*crunch_func_ptr)(struct client_state *);
292
293 /* Crunch function flags */
294 #define CF_NO_FLAGS        0
295 /* Cruncher applies to forced requests as well */
296 #define CF_IGNORE_FORCE    1
297 /* Crunched requests are counted for the block statistics */
298 #define CF_COUNT_AS_REJECT 2
299
300 /* A crunch function and its flags */
301 struct cruncher
302 {
303    const crunch_func_ptr cruncher;
304    const int flags;
305 };
306
307 static int crunch_response_triggered(struct client_state *csp, const struct cruncher crunchers[]);
308
309 /* Complete list of cruncher functions */
310 static const struct cruncher crunchers_all[] = {
311    { direct_response, CF_COUNT_AS_REJECT|CF_IGNORE_FORCE},
312    { block_url,       CF_COUNT_AS_REJECT },
313 #ifdef FEATURE_TRUST
314    { trust_url,       CF_COUNT_AS_REJECT },
315 #endif /* def FEATURE_TRUST */
316    { redirect_url,    CF_NO_FLAGS  },
317    { dispatch_cgi,    CF_IGNORE_FORCE},
318    { NULL,            0 }
319 };
320
321 /* Light version, used after tags are applied */
322 static const struct cruncher crunchers_light[] = {
323    { block_url,       CF_COUNT_AS_REJECT },
324    { redirect_url,    CF_NO_FLAGS },
325    { NULL,            0 }
326 };
327
328
329 /*
330  * XXX: Don't we really mean
331  *
332  * #if defined(unix)
333  *
334  * here?
335  */
336 #if !defined(_WIN32)
337 /*********************************************************************
338  *
339  * Function    :  sig_handler
340  *
341  * Description :  Signal handler for different signals.
342  *                Exit gracefully on TERM and INT
343  *                or set a flag that will cause the errlog
344  *                to be reopened by the main thread on HUP.
345  *
346  * Parameters  :
347  *          1  :  the_signal = the signal cause this function to call
348  *
349  * Returns     :  -
350  *
351  *********************************************************************/
352 static void sig_handler(int the_signal)
353 {
354    switch(the_signal)
355    {
356       case SIGTERM:
357       case SIGINT:
358          log_error(LOG_LEVEL_INFO, "exiting by signal %d .. bye", the_signal);
359 #if defined(unix)
360          if (pidfile)
361          {
362             unlink(pidfile);
363          }
364 #endif /* unix */
365          exit(the_signal);
366          break;
367
368       case SIGHUP:
369 #if defined(unix)
370          received_hup_signal = 1;
371 #endif
372          break;
373
374       default:
375          /*
376           * We shouldn't be here, unless we catch signals
377           * in main() that we can't handle here!
378           */
379          log_error(LOG_LEVEL_FATAL,
380             "sig_handler: exiting on unexpected signal %d", the_signal);
381    }
382    return;
383
384 }
385 #endif
386
387
388 /*********************************************************************
389  *
390  * Function    :  get_write_delay
391  *
392  * Description :  Parse the delay-response parameter.
393  *
394  * Parameters  :
395  *          1  :  csp = Current client state (buffers, headers, etc...)
396  *
397  * Returns     :  Number of milliseconds to delay writes.
398  *
399  *********************************************************************/
400 static unsigned int get_write_delay(const struct client_state *csp)
401 {
402    unsigned int delay;
403    char *endptr;
404    char *newval;
405
406    if ((csp->action->flags & ACTION_DELAY_RESPONSE) == 0)
407    {
408       return 0;
409    }
410    newval = csp->action->string[ACTION_STRING_DELAY_RESPONSE];
411
412    delay = (unsigned)strtol(newval, &endptr, 0);
413    if (*endptr != '\0')
414    {
415       log_error(LOG_LEVEL_FATAL,
416          "Invalid delay-response{} parameter: '%s'", newval);
417    }
418
419    return delay;
420
421 }
422
423
424 /*********************************************************************
425  *
426  * Function    :  client_protocol_is_unsupported
427  *
428  * Description :  Checks if the client used a known unsupported
429  *                protocol and deals with it by sending an error
430  *                response.
431  *
432  * Parameters  :
433  *          1  :  csp = Current client state (buffers, headers, etc...)
434  *          2  :  req = the first request line send by the client
435  *
436  * Returns     :  TRUE if an error response has been generated, or
437  *                FALSE if the request doesn't look invalid.
438  *
439  *********************************************************************/
440 static int client_protocol_is_unsupported(struct client_state *csp, char *req)
441 {
442    /*
443     * If it's a FTP or gopher request, we don't support it.
444     *
445     * These checks are better than nothing, but they might
446     * not work in all configurations and some clients might
447     * have problems digesting the answer.
448     *
449     * They should, however, never cause more problems than
450     * Privoxy's old behaviour (returning the misleading HTML
451     * error message:
452     *
453     * "Could not resolve http://(ftp|gopher)://example.org").
454     */
455    if (!strncmpic(req, "GET ftp://", 10) || !strncmpic(req, "GET gopher://", 13))
456    {
457       const char *response = NULL;
458       const char *protocol = NULL;
459
460       if (!strncmpic(req, "GET ftp://", 10))
461       {
462          response = FTP_RESPONSE;
463          protocol = "FTP";
464       }
465       else
466       {
467          response = GOPHER_RESPONSE;
468          protocol = "GOPHER";
469       }
470       log_error(LOG_LEVEL_ERROR,
471          "%s tried to use Privoxy as %s proxy: %s",
472          csp->ip_addr_str, protocol, req);
473       log_error(LOG_LEVEL_CLF,
474          "%s - - [%T] \"%s\" 400 0", csp->ip_addr_str, req);
475       freez(req);
476
477 #ifdef FEATURE_HTTPS_INSPECTION
478       if (client_use_ssl(csp))
479       {
480          ssl_send_data_delayed(&(csp->ssl_client_attr),
481             (const unsigned char *)response, strlen(response),
482             get_write_delay(csp));
483       }
484       else
485 #endif
486       {
487          write_socket_delayed(csp->cfd, response, strlen(response),
488             get_write_delay(csp));
489       }
490
491       return TRUE;
492    }
493
494    return FALSE;
495 }
496
497
498 /*********************************************************************
499  *
500  * Function    :  client_has_unsupported_expectations
501  *
502  * Description :  Checks if the client used an unsupported expectation
503  *                in which case an error message is delivered.
504  *
505  * Parameters  :
506  *          1  :  csp = Current client state (buffers, headers, etc...)
507  *
508  * Returns     :  TRUE if an error response has been generated, or
509  *                FALSE if the request doesn't look invalid.
510  *
511  *********************************************************************/
512 static int client_has_unsupported_expectations(const struct client_state *csp)
513 {
514    if ((csp->flags & CSP_FLAG_UNSUPPORTED_CLIENT_EXPECTATION))
515    {
516       log_error(LOG_LEVEL_ERROR,
517          "Rejecting request from client %s with unsupported Expect header value",
518          csp->ip_addr_str);
519       log_error(LOG_LEVEL_CLF,
520          "%s - - [%T] \"%s\" 417 0", csp->ip_addr_str, csp->http->cmd);
521       write_socket_delayed(csp->cfd,
522          UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE,
523          strlen(UNSUPPORTED_CLIENT_EXPECTATION_ERROR_RESPONSE),
524          get_write_delay(csp));
525
526       return TRUE;
527    }
528
529    return FALSE;
530
531 }
532
533
534 /*********************************************************************
535  *
536  * Function    :  get_request_destination_elsewhere
537  *
538  * Description :  If the client's request was redirected into
539  *                Privoxy without the client's knowledge,
540  *                the request line lacks the destination host.
541  *
542  *                This function tries to get it elsewhere,
543  *                provided accept-intercepted-requests is enabled.
544  *
545  *                "Elsewhere" currently only means "Host: header",
546  *                but in the future we may ask the redirecting
547  *                packet filter to look the destination up.
548  *
549  *                If the destination stays unknown, an error
550  *                response is send to the client and headers
551  *                are freed so that chat() can return directly.
552  *
553  * Parameters  :
554  *          1  :  csp = Current client state (buffers, headers, etc...)
555  *          2  :  headers = a header list
556  *
557  * Returns     :  JB_ERR_OK if the destination is now known, or
558  *                JB_ERR_PARSE if it isn't.
559  *
560  *********************************************************************/
561 static jb_err get_request_destination_elsewhere(struct client_state *csp, struct list *headers)
562 {
563    if (!(csp->config->feature_flags & RUNTIME_FEATURE_ACCEPT_INTERCEPTED_REQUESTS))
564    {
565       log_error(LOG_LEVEL_ERROR, "%s's request: \'%s\' is invalid."
566          " Privoxy isn't configured to accept intercepted requests.",
567          csp->ip_addr_str, csp->http->cmd);
568       /* XXX: Use correct size */
569       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
570          csp->ip_addr_str, csp->http->cmd);
571
572       write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER),
573          get_write_delay(csp));
574       destroy_list(headers);
575
576       return JB_ERR_PARSE;
577    }
578    else if (JB_ERR_OK == get_destination_from_headers(headers, csp->http))
579    {
580       /* Split the domain we just got for pattern matching */
581       init_domain_components(csp->http);
582
583       return JB_ERR_OK;
584    }
585    else
586    {
587       /* We can't work without destination. Go spread the news.*/
588
589       /* XXX: Use correct size */
590       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
591          csp->ip_addr_str, csp->http->cmd);
592       log_error(LOG_LEVEL_ERROR,
593          "Privoxy was unable to get the destination for %s's request: %s",
594          csp->ip_addr_str, csp->http->cmd);
595
596       write_socket_delayed(csp->cfd, MISSING_DESTINATION_RESPONSE,
597          strlen(MISSING_DESTINATION_RESPONSE), get_write_delay(csp));
598       destroy_list(headers);
599
600       return JB_ERR_PARSE;
601    }
602    /*
603     * TODO: If available, use PF's ioctl DIOCNATLOOK as last resort
604     * to get the destination IP address, use it as host directly
605     * or do a reverse DNS lookup first.
606     */
607 }
608
609
610 /*********************************************************************
611  *
612  * Function    :  get_server_headers
613  *
614  * Description :  Parses server headers in iob and fills them
615  *                into csp->headers so that they can later be
616  *                handled by sed().
617  *
618  * Parameters  :
619  *          1  :  csp = Current client state (buffers, headers, etc...)
620  *
621  * Returns     :  JB_ERR_OK if everything went fine, or
622  *                JB_ERR_PARSE if the headers were incomplete.
623  *
624  *********************************************************************/
625 static jb_err get_server_headers(struct client_state *csp)
626 {
627    int continue_hack_in_da_house = 0;
628    char * header;
629
630    while (((header = get_header(csp->iob)) != NULL) || continue_hack_in_da_house)
631    {
632       if (header == NULL)
633       {
634          /*
635           * continue hack in da house. Ignore the ending of
636           * this head and continue enlisting header lines.
637           * The reason is described below.
638           */
639          enlist(csp->headers, "");
640          continue_hack_in_da_house = 0;
641          continue;
642       }
643       else if (0 == strncmpic(header, "HTTP/1.1 100", 12))
644       {
645          /*
646           * It's a bodyless continue response, don't
647           * stop header parsing after reaching its end.
648           *
649           * As a result Privoxy will concatenate the
650           * next response's head and parse and deliver
651           * the headers as if they belonged to one request.
652           *
653           * The client will separate them because of the
654           * empty line between them.
655           *
656           * XXX: What we're doing here is clearly against
657           * the intended purpose of the continue header,
658           * and under some conditions (HTTP/1.0 client request)
659           * it's a standard violation.
660           *
661           * Anyway, "sort of against the spec" is preferable
662           * to "always getting confused by Continue responses"
663           * (Privoxy's behaviour before this hack was added)
664           */
665          log_error(LOG_LEVEL_HEADER, "Continue hack in da house.");
666          continue_hack_in_da_house = 1;
667       }
668       else if (*header == '\0')
669       {
670          /*
671           * If the header is empty, but the Continue hack
672           * isn't active, we can assume that we reached the
673           * end of the buffer before we hit the end of the
674           * head.
675           *
676           * Inform the caller an let it decide how to handle it.
677           */
678          return JB_ERR_PARSE;
679       }
680
681       if (JB_ERR_MEMORY == enlist(csp->headers, header))
682       {
683          /*
684           * XXX: Should we quit the request and return a
685           * out of memory error page instead?
686           */
687          log_error(LOG_LEVEL_ERROR,
688             "Out of memory while enlisting server headers. %s lost.",
689             header);
690       }
691       freez(header);
692    }
693
694    return JB_ERR_OK;
695 }
696
697
698 /*********************************************************************
699  *
700  * Function    :  crunch_reason
701  *
702  * Description :  Translates the crunch reason code into a string.
703  *
704  * Parameters  :
705  *          1  :  rsp = a http_response
706  *
707  * Returns     :  A string with the crunch reason or an error description.
708  *
709  *********************************************************************/
710 static const char *crunch_reason(const struct http_response *rsp)
711 {
712    char * reason = NULL;
713
714    assert(rsp != NULL);
715    if (rsp == NULL)
716    {
717       return "Internal error while searching for crunch reason";
718    }
719
720    switch (rsp->crunch_reason)
721    {
722       case UNSUPPORTED:
723          reason = "Unsupported HTTP feature";
724          break;
725       case BLOCKED:
726          reason = "Blocked";
727          break;
728       case UNTRUSTED:
729          reason = "Untrusted";
730          break;
731       case REDIRECTED:
732          reason = "Redirected";
733          break;
734       case CGI_CALL:
735          reason = "CGI Call";
736          break;
737       case NO_SUCH_DOMAIN:
738          reason = "DNS failure";
739          break;
740       case FORWARDING_FAILED:
741          reason = "Forwarding failed";
742          break;
743       case CONNECT_FAILED:
744          reason = "Connection failure";
745          break;
746       case OUT_OF_MEMORY:
747          reason = "Out of memory (may mask other reasons)";
748          break;
749       case CONNECTION_TIMEOUT:
750          reason = "Connection timeout";
751          break;
752       case NO_SERVER_DATA:
753          reason = "No server data received";
754          break;
755       default:
756          reason = "No reason recorded";
757          break;
758    }
759
760    return reason;
761 }
762
763
764 /*********************************************************************
765  *
766  * Function    :  log_applied_actions
767  *
768  * Description :  Logs the applied actions if LOG_LEVEL_ACTIONS is
769  *                enabled.
770  *
771  * Parameters  :
772  *          1  :  actions = Current action spec to log
773  *
774  * Returns     :  Nothing.
775  *
776  *********************************************************************/
777 static void log_applied_actions(const struct current_action_spec *actions)
778 {
779    /*
780     * The conversion to text requires lots of memory allocations so
781     * we only do the conversion if the user is actually interested.
782     */
783    if (debug_level_is_enabled(LOG_LEVEL_ACTIONS))
784    {
785       char *actions_as_text = actions_to_line_of_text(actions);
786       log_error(LOG_LEVEL_ACTIONS, "%s", actions_as_text);
787       freez(actions_as_text);
788    }
789 }
790
791
792 /*********************************************************************
793  *
794  * Function    :  send_crunch_response
795  *
796  * Description :  Delivers already prepared response for
797  *                intercepted requests, logs the interception
798  *                and frees the response.
799  *
800  * Parameters  :
801  *          1  :  csp = Current client state (buffers, headers, etc...)
802  *          2  :  rsp = Fully prepared response. Will be freed on exit.
803  *
804  * Returns     :  Nothing.
805  *
806  *********************************************************************/
807 static void send_crunch_response(struct client_state *csp, struct http_response *rsp)
808 {
809       const struct http_request *http = csp->http;
810       char status_code[4];
811
812       assert(rsp != NULL);
813       assert(rsp->head != NULL);
814
815       if (rsp == NULL)
816       {
817          log_error(LOG_LEVEL_FATAL, "NULL response in send_crunch_response.");
818       }
819
820       /*
821        * Extract the status code from the actual head
822        * that will be send to the client. It is the only
823        * way to get it right for all requests, including
824        * the fixed ones for out-of-memory problems.
825        *
826        * A head starts like this: 'HTTP/1.1 200...'
827        *                           0123456789|11
828        *                                     10
829        */
830       status_code[0] = rsp->head[9];
831       status_code[1] = rsp->head[10];
832       status_code[2] = rsp->head[11];
833       status_code[3] = '\0';
834
835       /* Log that the request was crunched and why. */
836       log_applied_actions(csp->action);
837 #ifdef FEATURE_HTTPS_INSPECTION
838       if (client_use_ssl(csp))
839       {
840          log_error(LOG_LEVEL_CRUNCH, "%s: https://%s%s", crunch_reason(rsp),
841             http->hostport, http->path);
842          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" %s %lu",
843             csp->ip_addr_str, http->gpc, http->hostport, http->path,
844             http->version, status_code, rsp->content_length);
845       }
846       else
847 #endif
848       {
849          log_error(LOG_LEVEL_CRUNCH, "%s: %s", crunch_reason(rsp), http->url);
850          log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" %s %lu",
851             csp->ip_addr_str, http->ocmd, status_code, rsp->content_length);
852       }
853       /* Write the answer to the client */
854 #ifdef FEATURE_HTTPS_INSPECTION
855       if (client_use_ssl(csp))
856       {
857          if ((ssl_send_data_delayed(&(csp->ssl_client_attr),
858                 (const unsigned char *)rsp->head, rsp->head_length,
859                 get_write_delay(csp)) < 0)
860           || (ssl_send_data_delayed(&(csp->ssl_client_attr),
861                 (const unsigned char *)rsp->body, rsp->content_length,
862                 get_write_delay(csp)) < 0))
863          {
864             /* There is nothing we can do about it. */
865             log_error(LOG_LEVEL_CONNECT, "Couldn't deliver the error message "
866                "for https://%s%s through client socket %d using TLS/SSL",
867                http->hostport, http->url, csp->cfd);
868          }
869       }
870       else
871 #endif
872       {
873          if (write_socket_delayed(csp->cfd, rsp->head, rsp->head_length,
874                 get_write_delay(csp))
875           || write_socket_delayed(csp->cfd, rsp->body, rsp->content_length,
876                 get_write_delay(csp)))
877          {
878             /* There is nothing we can do about it. */
879             log_error(LOG_LEVEL_CONNECT,
880                "Couldn't deliver the error message for %s through client socket %d: %E",
881                http->url, csp->cfd);
882          }
883       }
884
885       /* Clean up and return */
886       if (cgi_error_memory() != rsp)
887       {
888          free_http_response(rsp);
889       }
890       return;
891 }
892
893
894 /*********************************************************************
895  *
896  * Function    :  crunch_response_triggered
897  *
898  * Description :  Checks if the request has to be crunched,
899  *                and delivers the crunch response if necessary.
900  *
901  * Parameters  :
902  *          1  :  csp = Current client state (buffers, headers, etc...)
903  *          2  :  crunchers = list of cruncher functions to run
904  *
905  * Returns     :  TRUE if the request was answered with a crunch response
906  *                FALSE otherwise.
907  *
908  *********************************************************************/
909 static int crunch_response_triggered(struct client_state *csp, const struct cruncher crunchers[])
910 {
911    struct http_response *rsp = NULL;
912    const struct cruncher *c;
913
914    /*
915     * If CGI request crunching is disabled,
916     * check the CGI dispatcher out of order to
917     * prevent unintentional blocks or redirects.
918     */
919    if (!(csp->config->feature_flags & RUNTIME_FEATURE_CGI_CRUNCHING)
920        && (NULL != (rsp = dispatch_cgi(csp))))
921    {
922       /* Deliver, log and free the interception response. */
923       send_crunch_response(csp, rsp);
924       csp->flags |= CSP_FLAG_CRUNCHED;
925       return TRUE;
926    }
927
928    for (c = crunchers; c->cruncher != NULL; c++)
929    {
930       /*
931        * Check the cruncher if either Privoxy is toggled
932        * on and the request isn't forced, or if the cruncher
933        * applies to forced requests as well.
934        */
935       if (((csp->flags & CSP_FLAG_TOGGLED_ON) &&
936           !(csp->flags & CSP_FLAG_FORCED)) ||
937           (c->flags & CF_IGNORE_FORCE))
938       {
939          rsp = c->cruncher(csp);
940          if (NULL != rsp)
941          {
942             /* Deliver, log and free the interception response. */
943             send_crunch_response(csp, rsp);
944             csp->flags |= CSP_FLAG_CRUNCHED;
945 #ifdef FEATURE_STATISTICS
946             if (c->flags & CF_COUNT_AS_REJECT)
947             {
948                csp->flags |= CSP_FLAG_REJECTED;
949             }
950 #endif /* def FEATURE_STATISTICS */
951
952             return TRUE;
953          }
954       }
955    }
956
957    return FALSE;
958 }
959
960
961 /*********************************************************************
962  *
963  * Function    :  build_request_line
964  *
965  * Description :  Builds the HTTP request line.
966  *
967  *                If a HTTP forwarder is used it expects the whole URL,
968  *                web servers only get the path.
969  *
970  * Parameters  :
971  *          1  :  csp = Current client state (buffers, headers, etc...)
972  *          2  :  fwd = The forwarding spec used for the request.
973  *                      Can be NULL.
974  *          3  :  request_line = The old request line which will be replaced.
975  *
976  * Returns     :  Nothing. Terminates in case of memory problems.
977  *
978  *********************************************************************/
979 static void build_request_line(struct client_state *csp, const struct forward_spec *fwd, char **request_line)
980 {
981    struct http_request *http = csp->http;
982
983    /*
984     * Downgrade http version from 1.1 to 1.0
985     * if +downgrade action applies.
986     */
987    if ((csp->action->flags & ACTION_DOWNGRADE)
988      && (!strcmpic(http->version, "HTTP/1.1")))
989    {
990       freez(http->version);
991       http->version = strdup_or_die("HTTP/1.0");
992    }
993
994    /*
995     * Rebuild the request line.
996     */
997    freez(*request_line);
998 #ifdef FEATURE_HTTPS_INSPECTION
999    if (fwd != NULL && fwd->forward_host &&
1000        fwd->type != FORWARD_WEBSERVER && client_use_ssl(csp))
1001    {
1002       *request_line = strdup("CONNECT ");
1003    }
1004    else
1005 #endif
1006    {
1007       *request_line = strdup(http->gpc);
1008       string_append(request_line, " ");
1009    }
1010
1011    if (fwd != NULL && fwd->forward_host && fwd->type != FORWARD_WEBSERVER)
1012    {
1013 #ifdef FEATURE_HTTPS_INSPECTION
1014       if (client_use_ssl(csp))
1015       {
1016          char port_string[10];
1017
1018          string_append(request_line, http->host);
1019          snprintf(port_string, sizeof(port_string), ":%d", http->port);
1020          string_append(request_line, port_string);
1021       }
1022       else
1023 #endif
1024       {
1025          string_append(request_line, http->url);
1026       }
1027    }
1028    else
1029    {
1030       string_append(request_line, http->path);
1031    }
1032    string_append(request_line, " ");
1033    string_append(request_line, http->version);
1034
1035    if (*request_line == NULL)
1036    {
1037       log_error(LOG_LEVEL_FATAL, "Out of memory writing HTTP command");
1038    }
1039    log_error(LOG_LEVEL_HEADER, "New HTTP Request-Line: %s", *request_line);
1040 }
1041
1042
1043 /*********************************************************************
1044  *
1045  * Function    :  change_request_destination
1046  *
1047  * Description :  Parse a (rewritten) request line and regenerate
1048  *                the http request data.
1049  *
1050  * Parameters  :
1051  *          1  :  csp = Current client state (buffers, headers, etc...)
1052  *
1053  * Returns     :  Forwards the parse_http_request() return code.
1054  *                Terminates in case of memory problems.
1055  *
1056  *********************************************************************/
1057 static jb_err change_request_destination(struct client_state *csp)
1058 {
1059    struct http_request *http = csp->http;
1060    jb_err err;
1061
1062    log_error(LOG_LEVEL_REDIRECTS, "Rewrite detected: %s",
1063       csp->headers->first->str);
1064    free_http_request(http);
1065    err = parse_http_request(csp->headers->first->str, http);
1066    if (JB_ERR_OK != err)
1067    {
1068       log_error(LOG_LEVEL_ERROR, "Couldn't parse rewritten request: %s.",
1069          jb_err_to_string(err));
1070    }
1071    if (http->ssl && strcmpic(csp->http->gpc, "CONNECT"))
1072    {
1073       /*
1074        * A client header filter changed the request URL from
1075        * http:// to https:// which we currently don't support.
1076        */
1077       log_error(LOG_LEVEL_ERROR, "Changing the request destination from http "
1078          "to https behind the client's back currently isn't supported.");
1079       return JB_ERR_PARSE;
1080    }
1081
1082    return err;
1083 }
1084
1085
1086 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1087 /*********************************************************************
1088  *
1089  * Function    :  server_response_is_complete
1090  *
1091  * Description :  Determines whether we should stop reading
1092  *                from the server socket.
1093  *
1094  * Parameters  :
1095  *          1  :  csp = Current client state (buffers, headers, etc...)
1096  *          2  :  content_length = Length of content received so far.
1097  *
1098  * Returns     :  TRUE if the response is complete,
1099  *                FALSE otherwise.
1100  *
1101  *********************************************************************/
1102 static int server_response_is_complete(struct client_state *csp,
1103    unsigned long long content_length)
1104 {
1105    int content_length_known = !!(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET);
1106
1107    if (!strcmpic(csp->http->gpc, "HEAD"))
1108    {
1109       /*
1110        * "HEAD" implies no body, we are thus expecting
1111        * no content. XXX: incomplete "list" of methods?
1112        */
1113       csp->expected_content_length = 0;
1114       content_length_known = TRUE;
1115       csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1116    }
1117
1118    if (csp->http->status == 204 || csp->http->status == 304)
1119    {
1120       /*
1121        * Expect no body. XXX: incomplete "list" of status codes?
1122        */
1123       csp->expected_content_length = 0;
1124       content_length_known = TRUE;
1125       csp->flags |= CSP_FLAG_SERVER_CONTENT_LENGTH_SET;
1126    }
1127
1128    return (content_length_known && ((0 == csp->expected_content_length)
1129             || (csp->expected_content_length <= content_length)));
1130 }
1131
1132
1133 #ifdef FEATURE_CONNECTION_SHARING
1134 /*********************************************************************
1135  *
1136  * Function    :  wait_for_alive_connections
1137  *
1138  * Description :  Waits for alive connections to timeout.
1139  *
1140  * Parameters  :  N/A
1141  *
1142  * Returns     :  N/A
1143  *
1144  *********************************************************************/
1145 static void wait_for_alive_connections(void)
1146 {
1147    int connections_alive = close_unusable_connections();
1148
1149    while (0 < connections_alive)
1150    {
1151       log_error(LOG_LEVEL_CONNECT,
1152          "Waiting for %d connections to timeout.",
1153          connections_alive);
1154       sleep(60);
1155       connections_alive = close_unusable_connections();
1156    }
1157
1158    log_error(LOG_LEVEL_CONNECT, "No connections to wait for left.");
1159
1160 }
1161 #endif /* def FEATURE_CONNECTION_SHARING */
1162
1163
1164 /*********************************************************************
1165  *
1166  * Function    :  save_connection_destination
1167  *
1168  * Description :  Remembers a connection for reuse later on.
1169  *
1170  * Parameters  :
1171  *          1  :  sfd  = Open socket to remember.
1172  *          2  :  http = The destination for the connection.
1173  *          3  :  fwd  = The forwarder settings used.
1174  *          4  :  server_connection  = storage.
1175  *
1176  * Returns     : void
1177  *
1178  *********************************************************************/
1179 static void save_connection_destination(jb_socket sfd,
1180                                         const struct http_request *http,
1181                                         const struct forward_spec *fwd,
1182                                         struct reusable_connection *server_connection)
1183 {
1184    assert(sfd != JB_INVALID_SOCKET);
1185    assert(NULL != http->host);
1186
1187    server_connection->sfd = sfd;
1188    server_connection->host = strdup_or_die(http->host);
1189    server_connection->port = http->port;
1190
1191    assert(NULL != fwd);
1192    assert(server_connection->gateway_host == NULL);
1193    assert(server_connection->gateway_port == 0);
1194    assert(server_connection->forwarder_type == 0);
1195    assert(server_connection->forward_host == NULL);
1196    assert(server_connection->forward_port == 0);
1197
1198    server_connection->forwarder_type = fwd->type;
1199    if (NULL != fwd->gateway_host)
1200    {
1201       server_connection->gateway_host = strdup_or_die(fwd->gateway_host);
1202    }
1203    else
1204    {
1205       server_connection->gateway_host = NULL;
1206    }
1207    server_connection->gateway_port = fwd->gateway_port;
1208    if (NULL != fwd->auth_username)
1209    {
1210       server_connection->auth_username = strdup_or_die(fwd->auth_username);
1211    }
1212    else
1213    {
1214       server_connection->auth_username = NULL;
1215    }
1216    if (NULL != fwd->auth_password)
1217    {
1218       server_connection->auth_password = strdup_or_die(fwd->auth_password);
1219    }
1220    else
1221    {
1222       server_connection->auth_password = NULL;
1223    }
1224
1225    if (NULL != fwd->forward_host)
1226    {
1227       server_connection->forward_host = strdup_or_die(fwd->forward_host);
1228    }
1229    else
1230    {
1231       server_connection->forward_host = NULL;
1232    }
1233    server_connection->forward_port = fwd->forward_port;
1234 }
1235 #endif /* FEATURE_CONNECTION_KEEP_ALIVE */
1236
1237
1238 /*********************************************************************
1239  *
1240  * Function    : verify_request_length
1241  *
1242  * Description : Checks if we already got the whole client requests
1243  *               and sets CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ if
1244  *               we do.
1245  *
1246  *               Data that doesn't belong to the current request is
1247  *               either thrown away to let the client retry on a clean
1248  *               socket, or stashed to be dealt with after the current
1249  *               request is served.
1250  *
1251  * Parameters  :
1252  *          1  :  csp = Current client state (buffers, headers, etc...)
1253  *
1254  * Returns     :  void
1255  *
1256  *********************************************************************/
1257 static void verify_request_length(struct client_state *csp)
1258 {
1259    unsigned long long buffered_request_bytes =
1260       (unsigned long long)(csp->client_iob->eod - csp->client_iob->cur);
1261
1262    if ((csp->expected_client_content_length != 0)
1263       && (buffered_request_bytes != 0))
1264    {
1265       if (csp->expected_client_content_length >= buffered_request_bytes)
1266       {
1267          csp->expected_client_content_length -= buffered_request_bytes;
1268          log_error(LOG_LEVEL_CONNECT, "Reduced expected bytes to %llu "
1269             "to account for the %llu ones we already got.",
1270             csp->expected_client_content_length, buffered_request_bytes);
1271       }
1272       else
1273       {
1274          assert(csp->client_iob->eod > csp->client_iob->cur + csp->expected_client_content_length);
1275          csp->client_iob->eod = csp->client_iob->cur + csp->expected_client_content_length;
1276          log_error(LOG_LEVEL_CONNECT, "Reducing expected bytes to 0. "
1277             "Marking the server socket tainted after throwing %llu bytes away.",
1278             buffered_request_bytes - csp->expected_client_content_length);
1279          csp->expected_client_content_length = 0;
1280          csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1281       }
1282
1283       if (csp->expected_client_content_length == 0)
1284       {
1285          csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1286       }
1287    }
1288
1289    if (!(csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ)
1290       && ((csp->client_iob->cur < csp->client_iob->eod)
1291          || (csp->expected_client_content_length != 0)))
1292    {
1293       if (strcmpic(csp->http->gpc, "GET")
1294          && strcmpic(csp->http->gpc, "HEAD")
1295          && strcmpic(csp->http->gpc, "TRACE")
1296          && strcmpic(csp->http->gpc, "OPTIONS")
1297          && strcmpic(csp->http->gpc, "DELETE"))
1298       {
1299          /* XXX: this is an incomplete hack */
1300          csp->flags &= ~CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1301          log_error(LOG_LEVEL_CONNECT, "There better be a request body.");
1302       }
1303       else
1304       {
1305          csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1306
1307          if ((csp->config->feature_flags & RUNTIME_FEATURE_TOLERATE_PIPELINING) == 0)
1308          {
1309             csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1310             log_error(LOG_LEVEL_CONNECT,
1311                "Possible pipeline attempt detected. The connection will not "
1312                "be kept alive and we will only serve the first request.");
1313             /* Nuke the pipelined requests from orbit, just to be sure. */
1314             clear_iob(csp->client_iob);
1315          }
1316          else
1317          {
1318             /*
1319              * Keep the pipelined data around for now, we'll deal with
1320              * it once we're done serving the current request.
1321              */
1322             csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
1323             assert(csp->client_iob->eod >= csp->client_iob->cur);
1324             log_error(LOG_LEVEL_CONNECT, "Complete client request followed by "
1325                "%d bytes of pipelined data received.",
1326                (int)(csp->client_iob->eod - csp->client_iob->cur));
1327          }
1328       }
1329    }
1330    else
1331    {
1332       csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
1333       log_error(LOG_LEVEL_CONNECT, "Complete client request received.");
1334    }
1335 }
1336
1337
1338 /*********************************************************************
1339  *
1340  * Function    :  mark_server_socket_tainted
1341  *
1342  * Description :  Makes sure we don't reuse a server socket
1343  *                (if we didn't read everything the server sent
1344  *                us reusing the socket would lead to garbage).
1345  *
1346  * Parameters  :
1347  *          1  :  csp = Current client state (buffers, headers, etc...)
1348  *
1349  * Returns     :  void.
1350  *
1351  *********************************************************************/
1352 static void mark_server_socket_tainted(struct client_state *csp)
1353 {
1354    /*
1355     * For consistency we always mark the server socket
1356     * tainted, however, to reduce the log noise we only
1357     * emit a log message if the server socket could have
1358     * actually been reused.
1359     */
1360    if ((csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
1361       && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
1362    {
1363       log_error(LOG_LEVEL_CONNECT,
1364          "Marking the server socket %d tainted.",
1365          csp->server_connection.sfd);
1366    }
1367    csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1368 }
1369
1370 /*********************************************************************
1371  *
1372  * Function    :  get_request_line
1373  *
1374  * Description : Read the client request line.
1375  *
1376  * Parameters  :
1377  *          1  :  csp = Current client state (buffers, headers, etc...)
1378  *
1379  * Returns     :  Pointer to request line or NULL in case of errors.
1380  *
1381  *********************************************************************/
1382 static char *get_request_line(struct client_state *csp)
1383 {
1384    char buf[BUFFER_SIZE];
1385    char *request_line = NULL;
1386    int len;
1387
1388    memset(buf, 0, sizeof(buf));
1389
1390    if ((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) != 0)
1391    {
1392       /*
1393        * If there are multiple pipelined requests waiting,
1394        * the flag will be set again once the next request
1395        * has been parsed.
1396        */
1397       csp->flags &= ~CSP_FLAG_PIPELINED_REQUEST_WAITING;
1398
1399       request_line = get_header(csp->client_iob);
1400       if ((NULL != request_line) && ('\0' != *request_line))
1401       {
1402          return request_line;
1403       }
1404       else
1405       {
1406          log_error(LOG_LEVEL_CONNECT, "No complete request line "
1407             "received yet. Continuing reading from %d.", csp->cfd);
1408       }
1409    }
1410
1411    do
1412    {
1413       if (
1414 #ifdef FUZZ
1415           0 == (csp->flags & CSP_FLAG_FUZZED_INPUT) &&
1416 #endif
1417           !data_is_available(csp->cfd, csp->config->socket_timeout)
1418           )
1419       {
1420          if (socket_is_still_alive(csp->cfd))
1421          {
1422             log_error(LOG_LEVEL_CONNECT,
1423                "No request line on socket %d received in time. Timeout: %d.",
1424                csp->cfd, csp->config->socket_timeout);
1425             write_socket_delayed(csp->cfd, CLIENT_CONNECTION_TIMEOUT_RESPONSE,
1426                strlen(CLIENT_CONNECTION_TIMEOUT_RESPONSE),
1427                get_write_delay(csp));
1428          }
1429          else
1430          {
1431             log_error(LOG_LEVEL_CONNECT,
1432                "The client side of the connection on socket %d got "
1433                "closed without sending a complete request line.", csp->cfd);
1434          }
1435          return NULL;
1436       }
1437
1438       len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1439
1440       if (len <= 0) return NULL;
1441
1442       /*
1443        * If there is no memory left for buffering the
1444        * request, there is nothing we can do but hang up
1445        */
1446       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1447       {
1448          return NULL;
1449       }
1450
1451       request_line = get_header(csp->client_iob);
1452
1453    } while ((NULL != request_line) && ('\0' == *request_line));
1454
1455    return request_line;
1456
1457 }
1458
1459 enum chunk_status
1460 {
1461    CHUNK_STATUS_MISSING_DATA,
1462    CHUNK_STATUS_BODY_COMPLETE,
1463    CHUNK_STATUS_PARSE_ERROR
1464 };
1465
1466
1467 /*********************************************************************
1468  *
1469  * Function    :  chunked_body_is_complete
1470  *
1471  * Description :  Figures out whether or not a chunked body is complete.
1472  *
1473  *                Currently it always starts at the beginning of the
1474  *                buffer which is somewhat wasteful and prevents Privoxy
1475  *                from starting to forward the correctly parsed chunks
1476  *                as soon as theoretically possible.
1477  *
1478  *                Should be modified to work with a common buffer,
1479  *                and allow the caller to skip already parsed chunks.
1480  *
1481  *                This would allow the function to be used for unbuffered
1482  *                response bodies as well.
1483  *
1484  * Parameters  :
1485  *          1  :  iob = Buffer with the body to check.
1486  *          2  :  length = Length of complete body
1487  *
1488  * Returns     :  Enum with the result of the check.
1489  *
1490  *********************************************************************/
1491 static enum chunk_status chunked_body_is_complete(struct iob *iob, size_t *length)
1492 {
1493    unsigned int chunksize;
1494    char *p = iob->cur;
1495
1496    do
1497    {
1498       /*
1499        * We need at least a single digit, followed by "\r\n",
1500        * followed by an unknown amount of data, followed by "\r\n".
1501        */
1502       if (p + 5 > iob->eod)
1503       {
1504          return CHUNK_STATUS_MISSING_DATA;
1505       }
1506       if (sscanf(p, "%x", &chunksize) != 1)
1507       {
1508          return CHUNK_STATUS_PARSE_ERROR;
1509       }
1510
1511       /*
1512        * We want at least a single digit, followed by "\r\n",
1513        * followed by the specified amount of data, followed by "\r\n".
1514        */
1515       if (p + chunksize + 5 > iob->eod)
1516       {
1517          return CHUNK_STATUS_MISSING_DATA;
1518       }
1519
1520       /* Skip chunk-size. */
1521       p = strstr(p, "\r\n");
1522       if (NULL == p)
1523       {
1524          return CHUNK_STATUS_PARSE_ERROR;
1525       }
1526       /* Move beyond the chunkdata. */
1527       p += 2 + chunksize;
1528
1529       /* Make sure we're still within the buffer and have two bytes left */
1530       if (p + 2 > iob->eod)
1531       {
1532          return CHUNK_STATUS_MISSING_DATA;
1533       }
1534
1535       /* There should be another "\r\n" to skip */
1536       if (memcmp(p, "\r\n", 2))
1537       {
1538          return CHUNK_STATUS_PARSE_ERROR;
1539       }
1540       p += 2;
1541    } while (chunksize > 0U);
1542
1543    *length = (size_t)(p - iob->cur);
1544    assert(*length <= (size_t)(iob->eod - iob->cur));
1545    assert(p <= iob->eod);
1546
1547    return CHUNK_STATUS_BODY_COMPLETE;
1548
1549 }
1550
1551
1552 /*********************************************************************
1553  *
1554  * Function    : receive_chunked_client_request_body
1555  *
1556  * Description : Read the chunk-encoded client request body.
1557  *               Failures are dealt with.
1558  *
1559  * Parameters  :
1560  *          1  :  csp = Current client state (buffers, headers, etc...)
1561  *
1562  * Returns     :  JB_ERR_OK or JB_ERR_PARSE
1563  *
1564  *********************************************************************/
1565 static jb_err receive_chunked_client_request_body(struct client_state *csp)
1566 {
1567    size_t body_length;
1568    enum chunk_status status;
1569
1570    while (CHUNK_STATUS_MISSING_DATA ==
1571       (status = chunked_body_is_complete(csp->client_iob, &body_length)))
1572    {
1573       char buf[BUFFER_SIZE];
1574       int len;
1575
1576       if (!data_is_available(csp->cfd, csp->config->socket_timeout))
1577       {
1578          log_error(LOG_LEVEL_ERROR,
1579             "Timeout while waiting for the client body.");
1580          break;
1581       }
1582       len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1583       if (len <= 0)
1584       {
1585          log_error(LOG_LEVEL_ERROR,
1586             "Reading the client body failed: %E");
1587          break;
1588       }
1589       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1590       {
1591          break;
1592       }
1593    }
1594    if (status != CHUNK_STATUS_BODY_COMPLETE)
1595    {
1596       write_socket_delayed(csp->cfd, CLIENT_BODY_PARSE_ERROR_RESPONSE,
1597          strlen(CLIENT_BODY_PARSE_ERROR_RESPONSE), get_write_delay(csp));
1598       log_error(LOG_LEVEL_CLF,
1599          "%s - - [%T] \"Failed reading chunked client body\" 400 0", csp->ip_addr_str);
1600       return JB_ERR_PARSE;
1601    }
1602    log_error(LOG_LEVEL_CONNECT,
1603       "Chunked client body completely read. Length: %lu", body_length);
1604    csp->expected_client_content_length = body_length;
1605
1606    return JB_ERR_OK;
1607
1608 }
1609
1610
1611 #ifdef FUZZ
1612 /*********************************************************************
1613  *
1614  * Function    :  fuzz_chunked_transfer_encoding
1615  *
1616  * Description :  Treat the fuzzed input as chunked transfer encoding
1617  *                to check and dechunk.
1618  *
1619  * Parameters  :
1620  *          1  :  csp      = Used to store the data.
1621  *          2  :  fuzz_input_file = File to read the input from.
1622  *
1623  * Returns     : Result of dechunking
1624  *
1625  *********************************************************************/
1626 extern int fuzz_chunked_transfer_encoding(struct client_state *csp, char *fuzz_input_file)
1627 {
1628    size_t length;
1629    size_t size = (size_t)(csp->iob->eod - csp->iob->cur);
1630    enum chunk_status status;
1631
1632    status = chunked_body_is_complete(csp->iob, &length);
1633    if (CHUNK_STATUS_BODY_COMPLETE != status)
1634    {
1635       log_error(LOG_LEVEL_INFO, "Chunked body is incomplete or invalid");
1636    }
1637
1638    return (JB_ERR_OK == remove_chunked_transfer_coding(csp->iob->cur, &size));
1639
1640 }
1641
1642
1643 /*********************************************************************
1644  *
1645  * Function    : fuzz_client_request
1646  *
1647  * Description : Try to get a client request from the fuzzed input.
1648  *
1649  * Parameters  :
1650  *          1  :  csp = Current client state (buffers, headers, etc...)
1651  *          2  :  fuzz_input_file = File to read the input from.
1652  *
1653  * Returns     :  Result of fuzzing.
1654  *
1655  *********************************************************************/
1656 extern int fuzz_client_request(struct client_state *csp, char *fuzz_input_file)
1657 {
1658    jb_err err;
1659
1660    csp->cfd = 0;
1661    csp->ip_addr_str = "fuzzer";
1662
1663    if (strcmp(fuzz_input_file, "-") != 0)
1664    {
1665       log_error(LOG_LEVEL_FATAL,
1666          "Fuzzed client requests can currently only be read from stdin (-).");
1667    }
1668    err = receive_client_request(csp);
1669    if (err != JB_ERR_OK)
1670    {
1671       return 1;
1672    }
1673    err = parse_client_request(csp);
1674    if (err != JB_ERR_OK)
1675    {
1676       return 1;
1677    }
1678
1679    return 0;
1680
1681 }
1682 #endif  /* def FUZZ */
1683
1684
1685 #ifdef FEATURE_FORCE_LOAD
1686 /*********************************************************************
1687  *
1688  * Function    :  force_required
1689  *
1690  * Description : Checks a request line to see if it contains
1691  *               the FORCE_PREFIX. If it does, it is removed
1692  *               unless enforcing requests has beend disabled.
1693  *
1694  * Parameters  :
1695  *          1  :  request_line = HTTP request line
1696  *
1697  * Returns     :  TRUE if force is required, FALSE otherwise.
1698  *
1699  *********************************************************************/
1700 static int force_required(const struct client_state *csp, char *request_line)
1701 {
1702    char *p;
1703
1704    p = strstr(request_line, "http://");
1705    if (p != NULL)
1706    {
1707       /* Skip protocol */
1708       p += strlen("http://");
1709    }
1710    else
1711    {
1712       /* Intercepted request usually don't specify the protocol. */
1713       p = request_line;
1714    }
1715
1716    /* Go to the beginning of the path */
1717    p = strstr(p, "/");
1718    if (p == NULL)
1719    {
1720       /*
1721        * If the path is missing the request line is invalid and we
1722        * are done here. The client-visible rejection happens later on.
1723        */
1724       return 0;
1725    }
1726
1727    if (0 == strncmpic(p, FORCE_PREFIX, strlen(FORCE_PREFIX) - 1))
1728    {
1729       if (!(csp->config->feature_flags & RUNTIME_FEATURE_ENFORCE_BLOCKS))
1730       {
1731          /* XXX: Should clean more carefully */
1732          strclean(request_line, FORCE_PREFIX);
1733          log_error(LOG_LEVEL_FORCE,
1734             "Enforcing request: \"%s\".", request_line);
1735
1736          return 1;
1737       }
1738       log_error(LOG_LEVEL_FORCE,
1739          "Ignored force prefix in request: \"%s\".", request_line);
1740    }
1741
1742    return 0;
1743
1744 }
1745 #endif /* def FEATURE_FORCE_LOAD */
1746
1747
1748 /*********************************************************************
1749  *
1750  * Function    :  receive_client_request
1751  *
1752  * Description : Read the client's request (more precisely the
1753  *               client headers) and answer it if necessary.
1754  *
1755  * Parameters  :
1756  *          1  :  csp = Current client state (buffers, headers, etc...)
1757  *
1758  * Returns     :  JB_ERR_OK, JB_ERR_PARSE or JB_ERR_MEMORY
1759  *
1760  *********************************************************************/
1761 static jb_err receive_client_request(struct client_state *csp)
1762 {
1763    char buf[BUFFER_SIZE];
1764    char *p;
1765    char *req = NULL;
1766    struct http_request *http;
1767    int len;
1768    jb_err err;
1769
1770    /* Temporary copy of the client's headers before they get enlisted in csp->headers */
1771    struct list header_list;
1772    struct list *headers = &header_list;
1773
1774    /* We don't care if the arriving data is a valid HTTP request or not. */
1775    csp->requests_received_total++;
1776
1777    http = csp->http;
1778
1779    memset(buf, 0, sizeof(buf));
1780
1781    req = get_request_line(csp);
1782    if (req == NULL)
1783    {
1784       mark_server_socket_tainted(csp);
1785       return JB_ERR_PARSE;
1786    }
1787    assert(*req != '\0');
1788
1789    if (client_protocol_is_unsupported(csp, req))
1790    {
1791       return JB_ERR_PARSE;
1792    }
1793
1794 #ifdef FEATURE_FORCE_LOAD
1795    if (force_required(csp, req))
1796    {
1797       csp->flags |= CSP_FLAG_FORCED;
1798    }
1799 #endif /* def FEATURE_FORCE_LOAD */
1800
1801    err = parse_http_request(req, http);
1802    freez(req);
1803    if (JB_ERR_OK != err)
1804    {
1805       write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER),
1806          get_write_delay(csp));
1807       /* XXX: Use correct size */
1808       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
1809       log_error(LOG_LEVEL_ERROR,
1810          "Couldn't parse request line received from %s: %s",
1811          csp->ip_addr_str, jb_err_to_string(err));
1812
1813       free_http_request(http);
1814       return JB_ERR_PARSE;
1815    }
1816
1817    /* grab the rest of the client's headers */
1818    init_list(headers);
1819    for (;;)
1820    {
1821       p = get_header(csp->client_iob);
1822
1823       if (p == NULL)
1824       {
1825          /* There are no additional headers to read. */
1826          break;
1827       }
1828
1829       if (*p == '\0')
1830       {
1831          /*
1832           * We didn't receive a complete header
1833           * line yet, get the rest of it.
1834           */
1835          if (!data_is_available(csp->cfd, csp->config->socket_timeout))
1836          {
1837             log_error(LOG_LEVEL_ERROR,
1838                "Client headers did not arrive in time. Timeout: %d",
1839                csp->config->socket_timeout);
1840             destroy_list(headers);
1841             return JB_ERR_PARSE;
1842          }
1843
1844          len = read_socket(csp->cfd, buf, sizeof(buf) - 1);
1845          if (len <= 0)
1846          {
1847             log_error(LOG_LEVEL_ERROR, "read from client failed: %E");
1848             destroy_list(headers);
1849             return JB_ERR_PARSE;
1850          }
1851
1852          if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
1853          {
1854             /*
1855              * If there is no memory left for buffering the
1856              * request, there is nothing we can do but hang up
1857              */
1858             destroy_list(headers);
1859             return JB_ERR_MEMORY;
1860          }
1861       }
1862       else
1863       {
1864          if (!strncmpic(p, "Transfer-Encoding:", 18))
1865          {
1866             /*
1867              * XXX: should be called through sed()
1868              *      but currently can't.
1869              */
1870             client_transfer_encoding(csp, &p);
1871          }
1872          /*
1873           * We were able to read a complete
1874           * header and can finally enlist it.
1875           */
1876          enlist(headers, p);
1877          freez(p);
1878       }
1879    }
1880
1881    if (http->host == NULL)
1882    {
1883       /*
1884        * If we still don't know the request destination,
1885        * the request is invalid or the client uses
1886        * Privoxy without its knowledge.
1887        */
1888       if (JB_ERR_OK != get_request_destination_elsewhere(csp, headers))
1889       {
1890          /*
1891           * Our attempts to get the request destination
1892           * elsewhere failed or Privoxy is configured
1893           * to only accept proxy requests.
1894           *
1895           * An error response has already been sent
1896           * and we're done here.
1897           */
1898          return JB_ERR_PARSE;
1899       }
1900    }
1901
1902 #ifdef FEATURE_CLIENT_TAGS
1903    /* XXX: If the headers were enlisted sooner, passing csp would do. */
1904    set_client_address(csp, headers);
1905    get_tag_list_for_client(csp->client_tags, csp->client_address);
1906 #endif
1907
1908    /*
1909     * Determine the actions for this URL
1910     */
1911 #ifdef FEATURE_TOGGLE
1912    if (!(csp->flags & CSP_FLAG_TOGGLED_ON))
1913    {
1914       /* Most compatible set of actions (i.e. none) */
1915       init_current_action(csp->action);
1916    }
1917    else
1918 #endif /* ndef FEATURE_TOGGLE */
1919    {
1920       get_url_actions(csp, http);
1921    }
1922
1923    enlist(csp->headers, http->cmd);
1924
1925    /* Append the previously read headers */
1926    err = list_append_list_unique(csp->headers, headers);
1927    destroy_list(headers);
1928
1929    return err;
1930
1931 }
1932
1933
1934 /*********************************************************************
1935  *
1936  * Function    : parse_client_request
1937  *
1938  * Description : Parses the client's request and decides what to do
1939  *               with it.
1940  *
1941  *               Note that since we're not using select() we could get
1942  *               blocked here if a client connected, then didn't say
1943  *               anything!
1944  *
1945  * Parameters  :
1946  *          1  :  csp = Current client state (buffers, headers, etc...)
1947  *
1948  * Returns     :  JB_ERR_OK or JB_ERR_PARSE
1949  *
1950  *********************************************************************/
1951 static jb_err parse_client_request(struct client_state *csp)
1952 {
1953    struct http_request *http = csp->http;
1954    jb_err err;
1955
1956 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
1957    if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
1958     && (!strcmpic(csp->http->version, "HTTP/1.1"))
1959     && (csp->http->ssl == 0))
1960    {
1961       /* Assume persistence until further notice */
1962       csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
1963    }
1964 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
1965
1966    if (csp->http->ssl == 0)
1967    {
1968       /*
1969        * This whole block belongs to chat() but currently
1970        * has to be executed before sed().
1971        */
1972       if (csp->flags & CSP_FLAG_CHUNKED_CLIENT_BODY)
1973       {
1974          if (receive_chunked_client_request_body(csp) != JB_ERR_OK)
1975          {
1976             return JB_ERR_PARSE;
1977          }
1978       }
1979       else
1980       {
1981          csp->expected_client_content_length = get_expected_content_length(csp->headers);
1982       }
1983       verify_request_length(csp);
1984    }
1985 #ifndef FEATURE_HTTPS_INSPECTION
1986    else
1987    {
1988       csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
1989    }
1990 #endif
1991
1992    err = sed(csp, FILTER_CLIENT_HEADERS);
1993    if (JB_ERR_OK != err)
1994    {
1995       log_error(LOG_LEVEL_ERROR, "Failed to parse client request from %s.",
1996          csp->ip_addr_str);
1997       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
1998          csp->ip_addr_str, csp->http->cmd);
1999       write_socket_delayed(csp->cfd, CHEADER, strlen(CHEADER), get_write_delay(csp));
2000       return JB_ERR_PARSE;
2001    }
2002    csp->flags |= CSP_FLAG_CLIENT_HEADER_PARSING_DONE;
2003
2004    /* Check request line for rewrites. */
2005    if ((NULL == csp->headers->first->str)
2006       || (strcmp(http->cmd, csp->headers->first->str) &&
2007          (JB_ERR_OK != change_request_destination(csp))))
2008    {
2009       /*
2010        * A header filter broke the request line - bail out.
2011        */
2012       write_socket_delayed(csp->cfd, MESSED_UP_REQUEST_RESPONSE,
2013          strlen(MESSED_UP_REQUEST_RESPONSE), get_write_delay(csp));
2014       /* XXX: Use correct size */
2015       log_error(LOG_LEVEL_CLF,
2016          "%s - - [%T] \"Invalid request generated\" 400 0", csp->ip_addr_str);
2017       log_error(LOG_LEVEL_ERROR,
2018          "Invalid request line after applying header filters.");
2019       free_http_request(http);
2020
2021       return JB_ERR_PARSE;
2022    }
2023
2024    if (client_has_unsupported_expectations(csp))
2025    {
2026       return JB_ERR_PARSE;
2027    }
2028
2029    return JB_ERR_OK;
2030
2031 }
2032
2033
2034 /*********************************************************************
2035  *
2036  * Function    : read_http_request_body
2037  *
2038  * Description : Reads remaining request body from the client.
2039  *
2040  * Parameters  :
2041  *          1  :  csp = Current client state (buffers, headers, etc...)
2042  *
2043  * Returns     :  0 on success, anything else is an error.
2044  *
2045  *********************************************************************/
2046 static int read_http_request_body(struct client_state *csp)
2047 {
2048    size_t to_read = csp->expected_client_content_length;
2049    int len;
2050
2051    assert(to_read != 0);
2052
2053    /* check if all data has been already read */
2054    if (to_read <= (csp->client_iob->eod - csp->client_iob->cur))
2055    {
2056       return 0;
2057    }
2058
2059    for (to_read -= (size_t)(csp->client_iob->eod - csp->client_iob->cur);
2060         to_read > 0 && data_is_available(csp->cfd, csp->config->socket_timeout);
2061         to_read -= (unsigned)len)
2062    {
2063       char buf[BUFFER_SIZE];
2064       size_t max_bytes_to_read = to_read < sizeof(buf) ? to_read : sizeof(buf);
2065
2066       log_error(LOG_LEVEL_CONNECT,
2067          "Waiting for up to %lu bytes of request body from the client.",
2068          max_bytes_to_read);
2069       len = read_socket(csp->cfd, buf, (int)max_bytes_to_read);
2070       if (len <= -1)
2071       {
2072          log_error(LOG_LEVEL_CONNECT, "Failed receiving request body from %s: %E", csp->ip_addr_str);
2073          return 1;
2074       }
2075       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, (char *)buf, len))
2076       {
2077          return 1;
2078       }
2079       assert(to_read >= len);
2080    }
2081
2082    if (to_read != 0)
2083    {
2084       log_error(LOG_LEVEL_CONNECT,
2085          "Not enough request body has been read: expected %lu more bytes",
2086          to_read);
2087       return 1;
2088    }
2089    log_error(LOG_LEVEL_CONNECT,
2090       "The last %d bytes of the request body have been read", len);
2091    return 0;
2092 }
2093
2094
2095 /*********************************************************************
2096  *
2097  * Function    : update_client_headers
2098  *
2099  * Description : Updates the HTTP headers from the client request.
2100  *
2101  * Parameters  :
2102  *          1  :  csp = Current client state (buffers, headers, etc...)
2103  *          2  :  new_content_length = new content length value to set
2104  *
2105  * Returns     :  0 on success, anything else is an error.
2106  *
2107  *********************************************************************/
2108 static int update_client_headers(struct client_state *csp, size_t new_content_length)
2109 {
2110    static const char content_length[] = "Content-Length:";
2111    int updated = 0;
2112    struct list_entry *p;
2113
2114 #ifndef FEATURE_HTTPS_INSPECTION
2115    for (p = csp->headers->first;
2116 #else
2117    for (p = csp->http->client_ssl ? csp->https_headers->first : csp->headers->first;
2118 #endif
2119         !updated  && (p != NULL); p = p->next)
2120    {
2121       /* Header crunch()ed in previous run? -> ignore */
2122       if (p->str == NULL)
2123       {
2124          continue;
2125       }
2126
2127       /* Does the current parser handle this header? */
2128       if (0 == strncmpic(p->str, content_length, sizeof(content_length) - 1))
2129       {
2130          updated = (JB_ERR_OK == header_adjust_content_length((char **)&(p->str), new_content_length));
2131          if (!updated)
2132          {
2133             return 1;
2134          }
2135       }
2136    }
2137
2138    return !updated;
2139 }
2140
2141
2142 /*********************************************************************
2143  *
2144  * Function    : can_filter_request_body
2145  *
2146  * Description : Checks if the current request body can be stored in
2147  *               the client_iob without hitting buffer limit.
2148  *
2149  * Parameters  :
2150  *          1  : csp = Current client state (buffers, headers, etc...)
2151  *
2152  * Returns     : TRUE if the current request size do not exceed buffer limit
2153  *               FALSE otherwise.
2154  *
2155  *********************************************************************/
2156 static int can_filter_request_body(const struct client_state *csp)
2157 {
2158    if (!can_add_to_iob(csp->client_iob, csp->config->buffer_limit,
2159                        csp->expected_client_content_length))
2160    {
2161       log_error(LOG_LEVEL_INFO,
2162          "Not filtering request body from %s: buffer limit %lu will be exceeded "
2163          "(content length %llu)", csp->ip_addr_str, csp->config->buffer_limit,
2164          csp->expected_client_content_length);
2165       return FALSE;
2166    }
2167    return TRUE;
2168 }
2169
2170
2171 /*********************************************************************
2172  *
2173  * Function    : send_http_request
2174  *
2175  * Description : Sends the HTTP headers from the client request
2176  *               and all the body data that has already been received.
2177  *
2178  * Parameters  :
2179  *          1  :  csp = Current client state (buffers, headers, etc...)
2180  *
2181  * Returns     :  0 on success, anything else is an error.
2182  *
2183  *********************************************************************/
2184 static int send_http_request(struct client_state *csp)
2185 {
2186    char *hdr;
2187    int write_failure;
2188    const char *to_send;
2189    size_t to_send_len;
2190    int filter_client_body = csp->expected_client_content_length != 0 &&
2191       client_body_filters_enabled(csp->action) && can_filter_request_body(csp);
2192
2193    if (filter_client_body)
2194    {
2195       if (read_http_request_body(csp))
2196       {
2197          return 1;
2198       }
2199       to_send_len = csp->expected_client_content_length;
2200       to_send = execute_client_body_filters(csp, &to_send_len);
2201       if (to_send == NULL)
2202       {
2203          /* just flush client_iob */
2204          filter_client_body = FALSE;
2205       }
2206       else if (to_send_len != csp->expected_client_content_length &&
2207          update_client_headers(csp, to_send_len))
2208       {
2209          log_error(LOG_LEVEL_HEADER, "Error updating client headers");
2210          freez(to_send);
2211          return 1;
2212       }
2213       csp->expected_client_content_length = 0;
2214    }
2215
2216    hdr = list_to_text(csp->headers);
2217    if (hdr == NULL)
2218    {
2219       /* FIXME Should handle error properly */
2220       log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
2221    }
2222    list_remove_all(csp->headers);
2223
2224    /*
2225     * Write the client's (modified) header to the server
2226     * (along with anything else that may be in the buffer)
2227     */
2228    write_failure = 0 != write_socket(csp->server_connection.sfd, hdr, strlen(hdr));
2229    freez(hdr);
2230
2231    if (write_failure)
2232    {
2233       log_error(LOG_LEVEL_CONNECT, "Failed sending request headers to: %s: %E",
2234          csp->http->hostport);
2235       if (filter_client_body)
2236       {
2237          freez(to_send);
2238       }
2239       return 1;
2240    }
2241
2242    if (filter_client_body)
2243    {
2244       write_failure = 0 != write_socket(csp->server_connection.sfd, to_send, to_send_len);
2245       freez(to_send);
2246       if (write_failure)
2247       {
2248          log_error(LOG_LEVEL_CONNECT, "Failed sending filtered request body to: %s: %E",
2249             csp->http->hostport);
2250          return 1;
2251       }
2252    }
2253
2254    if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
2255       && (flush_iob(csp->server_connection.sfd, csp->client_iob, 0) < 0))
2256    {
2257       log_error(LOG_LEVEL_CONNECT, "Failed sending request body to: %s: %E",
2258          csp->http->hostport);
2259       return 1;
2260    }
2261    return 0;
2262 }
2263
2264
2265 #ifdef FEATURE_HTTPS_INSPECTION
2266 /*********************************************************************
2267  *
2268  * Function    : read_https_request_body
2269  *
2270  * Description : Reads remaining request body from the client.
2271  *
2272  * Parameters  :
2273  *          1  :  csp = Current client state (buffers, headers, etc...)
2274  *
2275  * Returns     :  0 on success, anything else is an error.
2276  *
2277  *********************************************************************/
2278 static int read_https_request_body(struct client_state *csp)
2279 {
2280    size_t to_read = csp->expected_client_content_length;
2281    int len;
2282
2283    assert(to_read != 0);
2284
2285    /* check if all data has been already read */
2286    if (to_read <= (csp->client_iob->eod - csp->client_iob->cur))
2287    {
2288       return 0;
2289    }
2290
2291    for (to_read -= (size_t)(csp->client_iob->eod - csp->client_iob->cur);
2292         to_read > 0 && (is_ssl_pending(&(csp->ssl_client_attr)) ||
2293           data_is_available(csp->cfd, csp->config->socket_timeout));
2294         to_read -= (unsigned)len)
2295    {
2296       unsigned char buf[BUFFER_SIZE];
2297       size_t max_bytes_to_read = to_read < sizeof(buf) ? to_read : sizeof(buf);
2298
2299       log_error(LOG_LEVEL_CONNECT,
2300          "Waiting for up to %lu bytes of request body from the client.",
2301          max_bytes_to_read);
2302       len = ssl_recv_data(&(csp->ssl_client_attr), buf,
2303          (unsigned)max_bytes_to_read);
2304       if (len <= 0)
2305       {
2306          log_error(LOG_LEVEL_CONNECT, "Failed receiving request body from %s", csp->ip_addr_str);
2307          return 1;
2308       }
2309       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, (char *)buf, len))
2310       {
2311          return 1;
2312       }
2313       assert(to_read >= len);
2314    }
2315
2316    if (to_read != 0)
2317    {
2318       log_error(LOG_LEVEL_CONNECT,
2319          "Not enough request body has been read: expected %lu more bytes",
2320          to_read);
2321       return 1;
2322    }
2323
2324    log_error(LOG_LEVEL_CONNECT,
2325       "The last %llu bytes of the request body have been read",
2326       csp->expected_client_content_length);
2327    return 0;
2328 }
2329
2330
2331 /*********************************************************************
2332  *
2333  * Function    : receive_and_send_encrypted_post_data
2334  *
2335  * Description : Reads remaining request body from the client and sends
2336  *               it to the server.
2337  *
2338  * Parameters  :
2339  *          1  :  csp = Current client state (buffers, headers, etc...)
2340  *
2341  * Returns     :  0 on success, anything else is an error.
2342  *
2343  *********************************************************************/
2344 static int receive_and_send_encrypted_post_data(struct client_state *csp)
2345 {
2346    int content_length_known = csp->expected_client_content_length != 0;
2347
2348    while (is_ssl_pending(&(csp->ssl_client_attr))
2349       || (content_length_known && csp->expected_client_content_length != 0))
2350    {
2351       unsigned char buf[BUFFER_SIZE];
2352       int len;
2353       int max_bytes_to_read = sizeof(buf);
2354
2355       if (content_length_known && csp->expected_client_content_length < sizeof(buf))
2356       {
2357          max_bytes_to_read = (int)csp->expected_client_content_length;
2358       }
2359       log_error(LOG_LEVEL_CONNECT,
2360          "Waiting for up to %d bytes of request body from the client.",
2361          max_bytes_to_read);
2362       len = ssl_recv_data(&(csp->ssl_client_attr), buf,
2363          (unsigned)max_bytes_to_read);
2364       if (len == -1)
2365       {
2366          return 1;
2367       }
2368       if (len == 0)
2369       {
2370          /* XXX: Does this actually happen? */
2371          break;
2372       }
2373       log_error(LOG_LEVEL_CONNECT, "Forwarding %d bytes of encrypted request body",
2374          len);
2375       len = ssl_send_data(&(csp->ssl_server_attr), buf, (size_t)len);
2376       if (len == -1)
2377       {
2378          return 1;
2379       }
2380       if (csp->expected_client_content_length != 0)
2381       {
2382          if (csp->expected_client_content_length >= len)
2383          {
2384             csp->expected_client_content_length -= (unsigned)len;
2385          }
2386          if (csp->expected_client_content_length == 0)
2387          {
2388             log_error(LOG_LEVEL_CONNECT, "Forwarded the last %d bytes", len);
2389             break;
2390          }
2391       }
2392    }
2393
2394    log_error(LOG_LEVEL_CONNECT, "Done forwarding encrypted request body");
2395
2396    return 0;
2397
2398 }
2399
2400
2401 /*********************************************************************
2402  *
2403  * Function    : send_https_request
2404  *
2405  * Description : Sends the HTTP headers from the client request
2406  *               and all the body data that has already been received.
2407  *
2408  * Parameters  :
2409  *          1  :  csp = Current client state (buffers, headers, etc...)
2410  *
2411  * Returns     :  0 on success, anything else is an error.
2412  *
2413  *********************************************************************/
2414 static int send_https_request(struct client_state *csp)
2415 {
2416    char *hdr;
2417    int ret;
2418    long flushed = 0;
2419    const char *to_send;
2420    size_t to_send_len;
2421    int filter_client_body = csp->expected_client_content_length != 0 &&
2422       client_body_filters_enabled(csp->action) && can_filter_request_body(csp);
2423
2424    if (filter_client_body)
2425    {
2426       if (read_https_request_body(csp))
2427       {
2428          return 1;
2429       }
2430       to_send_len = csp->expected_client_content_length;
2431       to_send = execute_client_body_filters(csp, &to_send_len);
2432       if (to_send == NULL)
2433       {
2434          /* just flush client_iob */
2435          filter_client_body = FALSE;
2436       }
2437       else if (to_send_len != csp->expected_client_content_length &&
2438          update_client_headers(csp, to_send_len))
2439       {
2440          log_error(LOG_LEVEL_HEADER, "Error updating client headers");
2441          return 1;
2442       }
2443       csp->expected_client_content_length = 0;
2444    }
2445
2446    hdr = list_to_text(csp->https_headers);
2447    if (hdr == NULL)
2448    {
2449       /* FIXME Should handle error properly */
2450       log_error(LOG_LEVEL_FATAL, "Out of memory parsing client header");
2451    }
2452    list_remove_all(csp->https_headers);
2453
2454    /*
2455     * Write the client's (modified) header to the server
2456     * (along with anything else that may be in the buffer)
2457     */
2458    ret = ssl_send_data(&(csp->ssl_server_attr),
2459       (const unsigned char *)hdr, strlen(hdr));
2460    freez(hdr);
2461
2462    if (ret < 0)
2463    {
2464       log_error(LOG_LEVEL_CONNECT,
2465          "Failed sending encrypted request headers to: %s: %E",
2466          csp->http->hostport);
2467       mark_server_socket_tainted(csp);
2468       return 1;
2469    }
2470
2471    if (filter_client_body)
2472    {
2473       ret = ssl_send_data(&(csp->ssl_server_attr), (const unsigned char *)to_send, to_send_len);
2474       freez(to_send);
2475       if (ret < 0)
2476       {
2477          log_error(LOG_LEVEL_CONNECT, "Failed sending filtered request body to: %s",
2478             csp->http->hostport);
2479          return 1;
2480       }
2481    }
2482
2483    if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) == 0)
2484       && ((flushed = ssl_flush_socket(&(csp->ssl_server_attr),
2485             csp->client_iob)) < 0))
2486    {
2487       log_error(LOG_LEVEL_CONNECT, "Failed sending request body to: %s: %E",
2488          csp->http->hostport);
2489       return 1;
2490    }
2491    if (flushed != 0 || csp->expected_client_content_length != 0)
2492    {
2493       if (csp->expected_client_content_length != 0)
2494       {
2495          if (csp->expected_client_content_length < flushed)
2496          {
2497             log_error(LOG_LEVEL_ERROR,
2498                "Flushed %ld bytes of request body while only expecting %llu",
2499                flushed, csp->expected_client_content_length);
2500             csp->expected_client_content_length = 0;
2501          }
2502          else
2503          {
2504             log_error(LOG_LEVEL_CONNECT,
2505                "Flushed %ld bytes of request body while expecting %llu",
2506                flushed, csp->expected_client_content_length);
2507             csp->expected_client_content_length -= (unsigned)flushed;
2508             if (receive_and_send_encrypted_post_data(csp))
2509             {
2510                return 1;
2511             }
2512          }
2513       }
2514       else
2515       {
2516          log_error(LOG_LEVEL_CONNECT,
2517             "Flushed %ld bytes of request body", flushed);
2518       }
2519    }
2520
2521    log_error(LOG_LEVEL_CONNECT, "Encrypted request sent");
2522
2523    return 0;
2524
2525 }
2526
2527
2528 /*********************************************************************
2529  *
2530  * Function    :  receive_encrypted_request_headers
2531  *
2532  * Description :  Receives the encrypted request headers when
2533  *                https-inspecting.
2534  *
2535  * Parameters  :
2536  *          1  :  csp = Current client state (buffers, headers, etc...)
2537  *
2538  * Returns     :  JB_ERR_OK on success,
2539  *                JB_ERR_PARSE or JB_ERR_MEMORY otherwise
2540  *
2541  *********************************************************************/
2542 static jb_err receive_encrypted_request_headers(struct client_state *csp)
2543 {
2544    char buf[BUFFER_SIZE];
2545    int len;
2546    char *p;
2547
2548    do
2549    {
2550       log_error(LOG_LEVEL_HEADER, "Waiting for encrypted client headers");
2551       if (!is_ssl_pending(&(csp->ssl_client_attr)) &&
2552           !data_is_available(csp->cfd, csp->config->socket_timeout))
2553       {
2554          log_error(LOG_LEVEL_CONNECT,
2555             "Socket %d timed out while waiting for client headers", csp->cfd);
2556          return JB_ERR_PARSE;
2557       }
2558       len = ssl_recv_data(&(csp->ssl_client_attr),
2559          (unsigned char *)buf, sizeof(buf));
2560       if (len == 0)
2561       {
2562          log_error(LOG_LEVEL_CONNECT,
2563             "Socket %d closed while waiting for client headers", csp->cfd);
2564          return JB_ERR_PARSE;
2565       }
2566       if (len == -1)
2567       {
2568          return JB_ERR_PARSE;
2569       }
2570       if (add_to_iob(csp->client_iob, csp->config->buffer_limit, buf, len))
2571       {
2572          return JB_ERR_MEMORY;
2573       }
2574       p = strstr(csp->client_iob->cur, "\r\n\r\n");
2575    } while (p == NULL);
2576
2577    log_error(LOG_LEVEL_HEADER, "Encrypted headers received completely");
2578
2579    return JB_ERR_OK;
2580 }
2581
2582
2583 /*********************************************************************
2584  *
2585  * Function    :  change_encrypted_request_destination
2586  *
2587  * Description :  Parse a (rewritten) request line from an encrypted
2588  *                request and regenerate the http request data.
2589  *
2590  * Parameters  :
2591  *          1  :  csp = Current client state (buffers, headers, etc...)
2592  *
2593  * Returns     :  Forwards the parse_http_request() return code.
2594  *                Terminates in case of memory problems.
2595  *
2596  *********************************************************************/
2597 static jb_err change_encrypted_request_destination(struct client_state *csp)
2598 {
2599    jb_err err;
2600    char *original_host = csp->http->host;
2601    int original_port = csp->http->port;
2602
2603    log_error(LOG_LEVEL_REDIRECTS, "Rewrite detected: %s",
2604       csp->https_headers->first->str);
2605    csp->http->host = NULL;
2606    free_http_request(csp->http);
2607    err = parse_http_request(csp->https_headers->first->str, csp->http);
2608    if (JB_ERR_OK != err)
2609    {
2610       log_error(LOG_LEVEL_ERROR, "Couldn't parse rewritten request: %s.",
2611          jb_err_to_string(err));
2612       freez(original_host);
2613       return err;
2614    }
2615
2616    if (csp->http->host == NULL)
2617    {
2618       char port_string[10];
2619       /*
2620        * The rewritten request line did not specify a host
2621        * which means we can use the original host specified
2622        * by the client.
2623        */
2624       csp->http->host = original_host;
2625       csp->http->port = original_port;
2626       log_error(LOG_LEVEL_REDIRECTS, "Keeping the original host: %s",
2627          csp->http->host);
2628       /*
2629        * If the rewritten request line didn't contain a host
2630        * it also didn't contain a port so we can reuse the host
2631        * port.
2632        */
2633       freez(csp->http->hostport);
2634       csp->http->hostport = strdup_or_die(csp->http->host);
2635       snprintf(port_string, sizeof(port_string), ":%d", original_port);
2636       err = string_append(&csp->http->hostport, port_string);
2637       if (err != JB_ERR_OK)
2638       {
2639          log_error(LOG_LEVEL_ERROR, "Failed to rebuild hostport: %s.",
2640             jb_err_to_string(err));
2641          return err;
2642       }
2643
2644       /*
2645        * While the request line didn't mention it,
2646        * we're https-inspecting and want to speak TLS
2647        * with the server.
2648        */
2649       csp->http->server_ssl = 1;
2650       csp->http->ssl = 1;
2651    }
2652    else
2653    {
2654       /* The rewrite filter added a host so we can ditch the original */
2655       freez(original_host);
2656       csp->http->server_ssl = csp->http->ssl;
2657    }
2658
2659    csp->http->client_ssl = 1;
2660
2661    freez(csp->https_headers->first->str);
2662    build_request_line(csp, NULL, &csp->https_headers->first->str);
2663
2664    if (!server_use_ssl(csp))
2665    {
2666       log_error(LOG_LEVEL_REDIRECTS,
2667          "Rewritten request line results in downgrade to http");
2668       /*
2669        * Replace the unencryptd headers received with the
2670        * CONNECT request with the ones we received securely.
2671        */
2672       destroy_list(csp->headers);
2673       csp->headers->first = csp->https_headers->first;
2674       csp->headers->last  = csp->https_headers->last;
2675       csp->https_headers->first = NULL;
2676       csp->https_headers->last = NULL;
2677    }
2678
2679    return JB_ERR_OK;
2680
2681 }
2682
2683
2684 /*********************************************************************
2685  *
2686  * Function    :  process_encrypted_request_headers
2687  *
2688  * Description :  Receives and parses the encrypted headers send
2689  *                by the client when https-inspecting.
2690  *
2691  * Parameters  :
2692  *          1  :  csp = Current client state (buffers, headers, etc...)
2693  *
2694  * Returns     :  JB_ERR_OK on success,
2695  *                JB_ERR_PARSE or JB_ERR_MEMORY otherwise
2696  *
2697  *********************************************************************/
2698 static jb_err process_encrypted_request_headers(struct client_state *csp)
2699 {
2700    char *p;
2701    char *request_line;
2702    jb_err err;
2703    /* Temporary copy of the client's headers before they get enlisted in csp->https_headers */
2704    struct list header_list;
2705    struct list *headers = &header_list;
2706
2707    assert(csp->ssl_with_client_is_opened);
2708
2709 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2710    if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
2711    {
2712       csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
2713    }
2714 #endif
2715    err = receive_encrypted_request_headers(csp);
2716    if (err != JB_ERR_OK)
2717    {
2718       if (csp->client_iob->cur == NULL ||
2719           csp->client_iob->cur == csp->client_iob->eod)
2720       {
2721          /*
2722           * We did not receive any data, most likely because the
2723           * client is done. Don't log this as a parse failure.
2724           */
2725          return JB_ERR_PARSE;
2726       }
2727       /* XXX: Also used for JB_ERR_MEMORY */
2728       log_error(LOG_LEVEL_ERROR, "Failed to receive encrypted request: %s",
2729          jb_err_to_string(err));
2730       ssl_send_data_delayed(&(csp->ssl_client_attr),
2731          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2732       return err;
2733    }
2734
2735    /* We don't need get_request_line() because the whole HTTP head is buffered. */
2736    request_line = get_header(csp->client_iob);
2737    if (request_line == NULL)
2738    {
2739       log_error(LOG_LEVEL_ERROR, "Failed to get the encrypted request line");
2740       ssl_send_data_delayed(&(csp->ssl_client_attr),
2741          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2742       return JB_ERR_PARSE;
2743    }
2744    assert(*request_line != '\0');
2745
2746    if (client_protocol_is_unsupported(csp, request_line))
2747    {
2748       /*
2749        * If the protocol is unsupported we're done here.
2750        * client_protocol_is_unsupported() took care of sending
2751        * the error response and logging the error message.
2752        */
2753       return JB_ERR_PARSE;
2754    }
2755
2756 #ifdef FEATURE_FORCE_LOAD
2757    if (force_required(csp, request_line))
2758    {
2759       csp->flags |= CSP_FLAG_FORCED;
2760    }
2761 #endif /* def FEATURE_FORCE_LOAD */
2762
2763    free_http_request(csp->http);
2764
2765    err = parse_http_request(request_line, csp->http);
2766    /* XXX: Restore ssl setting. This is ugly */
2767    csp->http->client_ssl = 1;
2768    csp->http->server_ssl = 1;
2769
2770    freez(request_line);
2771    if (JB_ERR_OK != err)
2772    {
2773       ssl_send_data_delayed(&(csp->ssl_client_attr),
2774          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2775       /* XXX: Use correct size */
2776       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"Invalid request\" 400 0", csp->ip_addr_str);
2777       log_error(LOG_LEVEL_ERROR,
2778          "Couldn't parse request line received from %s: %s",
2779          csp->ip_addr_str, jb_err_to_string(err));
2780
2781       free_http_request(csp->http);
2782       return JB_ERR_PARSE;
2783    }
2784
2785    /* Parse the rest of the client's headers. */
2786    init_list(headers);
2787    for (;;)
2788    {
2789       p = get_header(csp->client_iob);
2790
2791       if (p == NULL)
2792       {
2793          /* There are no additional headers to read. */
2794          break;
2795       }
2796       enlist(headers, p);
2797       freez(p);
2798    }
2799
2800    if (JB_ERR_OK != get_destination_from_https_headers(headers, csp->http))
2801    {
2802       /*
2803        * Our attempts to get the request destination
2804        * elsewhere failed.
2805        */
2806       log_error(LOG_LEVEL_ERROR,
2807          "Failed to get the encrypted request destination");
2808       ssl_send_data_delayed(&(csp->ssl_client_attr),
2809          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2810       destroy_list(headers);
2811
2812       return JB_ERR_PARSE;
2813    }
2814
2815    /* Split the domain we just got for pattern matching */
2816    init_domain_components(csp->http);
2817
2818 #ifdef FEATURE_CLIENT_TAGS
2819    /* XXX: If the headers were enlisted sooner, passing csp would do. */
2820    if (csp->client_address == NULL)
2821    {
2822       set_client_address(csp, headers);
2823       get_tag_list_for_client(csp->client_tags, csp->client_address);
2824    }
2825 #endif
2826
2827 #ifdef FEATURE_TOGGLE
2828    if ((csp->flags & CSP_FLAG_TOGGLED_ON) != 0)
2829 #endif
2830    {
2831       /*
2832        * Determine the actions for this request after
2833        * clearing the ones from the previous one.
2834        */
2835       free_current_action(csp->action);
2836       get_url_actions(csp, csp->http);
2837    }
2838
2839    enlist(csp->https_headers, csp->http->cmd);
2840
2841    /* Append the previously read headers */
2842    err = list_append_list_unique(csp->https_headers, headers);
2843    destroy_list(headers);
2844    if (JB_ERR_OK != err)
2845    {
2846       /* XXX: Send error message */
2847       return err;
2848    }
2849
2850    /* XXX: Work around crash */
2851    csp->error_message = NULL;
2852
2853    /* XXX: Why do this here? */
2854    csp->http->ssl = 1;
2855
2856    err = sed_https(csp);
2857    if (JB_ERR_OK != err)
2858    {
2859       ssl_send_data_delayed(&(csp->ssl_client_attr),
2860          (const unsigned char *)CHEADER, strlen(CHEADER), get_write_delay(csp));
2861       log_error(LOG_LEVEL_ERROR, "Failed to parse client request from %s.",
2862          csp->ip_addr_str);
2863       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 400 0",
2864          csp->ip_addr_str, csp->http->cmd);
2865       return JB_ERR_PARSE;
2866    }
2867
2868    if ((NULL == csp->https_headers->first->str)
2869       || (strcmp(csp->http->cmd, csp->https_headers->first->str) &&
2870          (JB_ERR_OK != change_encrypted_request_destination(csp))))
2871    {
2872       ssl_send_data_delayed(&(csp->ssl_client_attr),
2873          (const unsigned char *)MESSED_UP_REQUEST_RESPONSE,
2874          strlen(MESSED_UP_REQUEST_RESPONSE), get_write_delay(csp));
2875       log_error(LOG_LEVEL_ERROR,
2876          "Invalid request line after applying header filters.");
2877       /* XXX: Use correct size */
2878       log_error(LOG_LEVEL_CLF,
2879          "%s - - [%T] \"Invalid request generated\" 400 0", csp->ip_addr_str);
2880
2881       return JB_ERR_PARSE;
2882    }
2883
2884    log_error(LOG_LEVEL_HEADER, "Encrypted request headers processed");
2885    log_error(LOG_LEVEL_REQUEST, "https://%s%s", csp->http->hostport,
2886       csp->http->path);
2887
2888    return err;
2889
2890 }
2891
2892 /*********************************************************************
2893  *
2894  * Function    :  cgi_page_requested
2895  *
2896  * Description :  Checks if a request is for an internal CGI page.
2897  *
2898  * Parameters  :
2899  *          1  :  host = The host requested by the client.
2900  *
2901  * Returns     :  1 if a CGI page has been requested, 0 otherwise
2902  *
2903  *********************************************************************/
2904 static int cgi_page_requested(const char *host)
2905 {
2906    if ((0 == strcmpic(host, CGI_SITE_1_HOST))
2907     || (0 == strcmpic(host, CGI_SITE_1_HOST "."))
2908     || (0 == strcmpic(host, CGI_SITE_2_HOST))
2909     || (0 == strcmpic(host, CGI_SITE_2_HOST ".")))
2910    {
2911       return 1;
2912    }
2913
2914    return 0;
2915
2916 }
2917
2918
2919 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
2920 /*********************************************************************
2921  *
2922  * Function    :  continue_https_chat
2923  *
2924  * Description :  Behaves similar to chat() but only deals with
2925  *                https-inspected requests that arrive on an already
2926  *                established connection. The first request is always
2927  *                served by chat() which is a lot more complex as it
2928  *                has to deal with forwarding settings and connection
2929  *                failures etc.
2930  *
2931  *                If a connection to the server has already been
2932  *                opened it is reused unless the request is blocked
2933  *                or the forwarder changed.
2934  *
2935  *                If a connection to the server has not yet been
2936  *                opened (because the previous request was crunched),
2937  *                or the forwarder changed, the connection is dropped
2938  *                so that the client retries on a fresh one.
2939  *
2940  * Parameters  :
2941  *          1  :  csp = Current client state (buffers, headers, etc...)
2942  *
2943  * Returns     :  Nothing.
2944  *
2945  *********************************************************************/
2946 static void continue_https_chat(struct client_state *csp)
2947 {
2948    const struct forward_spec *fwd;
2949
2950    if (JB_ERR_OK != process_encrypted_request_headers(csp))
2951    {
2952       csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
2953       return;
2954    }
2955
2956    csp->requests_received_total++;
2957
2958    /*
2959     * We have an encrypted request. Check if one of the crunchers wants it.
2960     */
2961    if (crunch_response_triggered(csp, crunchers_all))
2962    {
2963       /*
2964        * Yes. The client got the crunch response and we're done here.
2965        */
2966       return;
2967    }
2968    if (csp->ssl_with_server_is_opened == 0)
2969    {
2970       log_error(LOG_LEVEL_CONNECT,
2971          "Dropping the client connection on socket %d. "
2972          "The server connection has not been established yet.",
2973          csp->cfd);
2974       csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
2975       return;
2976    }
2977    assert(csp->server_connection.sfd != JB_INVALID_SOCKET);
2978
2979    fwd = forward_url(csp, csp->http);
2980    if (!connection_destination_matches(&csp->server_connection, csp->http, fwd))
2981    {
2982       log_error(LOG_LEVEL_CONNECT,
2983          "Dropping the client connection on socket %d with "
2984          "server socket %d connected to %s. The forwarder has changed.",
2985          csp->cfd, csp->server_connection.sfd, csp->server_connection.host);
2986       csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
2987       return;
2988    }
2989
2990    log_applied_actions(csp->action);
2991
2992    log_error(LOG_LEVEL_CONNECT,
2993       "Reusing server socket %d connected to %s. Requests already sent: %u.",
2994       csp->server_connection.sfd, csp->server_connection.host,
2995       csp->server_connection.requests_sent_total);
2996
2997    if (send_https_request(csp))
2998    {
2999       /*
3000        * Most likely the server connection timed out. We can't easily
3001        * create a new one so simply drop the client connection without a
3002        * error response to let the client retry.
3003        */
3004       log_error(LOG_LEVEL_CONNECT,
3005          "Dropping client connection on socket %d. "
3006          "Forwarding the encrypted client request failed.",
3007          csp->cfd);
3008       return;
3009    }
3010    csp->server_connection.request_sent = time(NULL);
3011    csp->server_connection.requests_sent_total++;
3012    handle_established_connection(csp);
3013    freez(csp->receive_buffer);
3014 }
3015 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3016 #endif
3017
3018
3019 /*********************************************************************
3020  *
3021  * Function    :  handle_established_connection
3022  *
3023  * Description :  Shuffle data between client and server once the
3024  *                connection has been established and the request
3025  *                has been sent.
3026  *
3027  * Parameters  :
3028  *          1  :  csp = Current client state (buffers, headers, etc...)
3029  *
3030  * Returns     :  Nothing.
3031  *
3032  *********************************************************************/
3033 static void handle_established_connection(struct client_state *csp)
3034 {
3035    char *hdr;
3036    char *p;
3037    int n;
3038 #ifdef HAVE_POLL
3039    struct pollfd poll_fds[2];
3040 #else
3041    fd_set rfds;
3042    jb_socket maxfd;
3043    struct timeval timeout;
3044 #endif
3045    int server_body;
3046    int ms_iis5_hack = 0;
3047    unsigned long long byte_count = 0;
3048    struct http_request *http;
3049    long len = 0; /* for buffer sizes (and negative error codes) */
3050    int buffer_and_filter_content = 0;
3051    unsigned int write_delay;
3052 #ifdef FEATURE_HTTPS_INSPECTION
3053    int ret = 0;
3054    int use_ssl_tunnel = 0;
3055    csp->dont_verify_certificate = 0;
3056
3057    if (csp->http->ssl && !(csp->action->flags & ACTION_HTTPS_INSPECTION))
3058    {
3059       /* Pass encrypted content without filtering. */
3060       use_ssl_tunnel = 1;
3061    }
3062 #endif
3063
3064    /* Skeleton for HTTP response, if we should intercept the request */
3065    struct http_response *rsp;
3066 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3067    int watch_client_socket;
3068 #endif
3069
3070    csp->receive_buffer_size = csp->config->receive_buffer_size;
3071    csp->receive_buffer = zalloc(csp->receive_buffer_size + 1);
3072    if (csp->receive_buffer == NULL)
3073    {
3074       log_error(LOG_LEVEL_ERROR,
3075          "Out of memory. Failed to allocate the receive buffer.");
3076       rsp = cgi_error_memory();
3077       send_crunch_response(csp, rsp);
3078       return;
3079    }
3080
3081    http = csp->http;
3082
3083 #ifndef HAVE_POLL
3084    maxfd = (csp->cfd > csp->server_connection.sfd) ?
3085       csp->cfd : csp->server_connection.sfd;
3086 #endif
3087
3088    /* pass data between the client and server
3089     * until one or the other shuts down the connection.
3090     */
3091
3092    server_body = 0;
3093
3094 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3095    watch_client_socket = 0 == (csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING);
3096 #endif
3097    write_delay = get_write_delay(csp);
3098
3099    for (;;)
3100    {
3101 #ifdef FEATURE_HTTPS_INSPECTION
3102       if (server_use_ssl(csp) && is_ssl_pending(&(csp->ssl_server_attr)))
3103       {
3104          /*
3105           * It's possible that the TLS library already consumed all the
3106           * data the server intends to send. If that happens poll() and
3107           * select() will no longer see the data as available so we have
3108           * to skip the calls.
3109           */
3110          goto server_wants_to_talk;
3111       }
3112 #endif
3113 #ifndef HAVE_POLL
3114       FD_ZERO(&rfds);
3115 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3116       if (!watch_client_socket)
3117       {
3118          maxfd = csp->server_connection.sfd;
3119       }
3120       else
3121 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3122       {
3123          FD_SET(csp->cfd, &rfds);
3124       }
3125
3126       FD_SET(csp->server_connection.sfd, &rfds);
3127 #endif /* ndef HAVE_POLL */
3128
3129 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3130       if ((csp->flags & CSP_FLAG_CHUNKED)
3131          && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
3132          && ((csp->iob->eod - csp->iob->cur) >= 5)
3133          && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
3134       {
3135          /*
3136           * XXX: This check should be obsolete now,
3137           *      but let's wait a while to be sure.
3138           */
3139          log_error(LOG_LEVEL_CONNECT,
3140             "Looks like we got the last chunk together with "
3141             "the server headers but didn't detect it earlier. "
3142             "We better stop reading.");
3143          byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3144          csp->expected_content_length = byte_count;
3145          csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
3146       }
3147       if (server_body && server_response_is_complete(csp, byte_count))
3148       {
3149          if (csp->expected_content_length == byte_count)
3150          {
3151             log_error(LOG_LEVEL_CONNECT,
3152                "Done reading from server. Content length: %llu as expected. "
3153                "Bytes most recently read: %ld.",
3154                byte_count, len);
3155          }
3156          else
3157          {
3158             log_error(LOG_LEVEL_CONNECT,
3159                "Done reading from server. Expected content length: %llu. "
3160                "Actual content length: %llu. Bytes most recently read: %ld.",
3161                csp->expected_content_length, byte_count, len);
3162          }
3163          len = 0;
3164          /*
3165           * XXX: Should not jump around, handle_established_connection()
3166           * is complicated enough already.
3167           */
3168          goto reading_done;
3169       }
3170 #endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
3171
3172 #ifdef HAVE_POLL
3173       poll_fds[0].fd = csp->cfd;
3174 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3175       if (!watch_client_socket)
3176       {
3177          /*
3178           * Ignore incoming data, but still watch out
3179           * for disconnects etc. These flags are always
3180           * implied anyway but explicitly setting them
3181           * doesn't hurt.
3182           */
3183          poll_fds[0].events = POLLERR|POLLHUP;
3184       }
3185       else
3186 #endif
3187       {
3188          poll_fds[0].events = POLLIN;
3189       }
3190       poll_fds[1].fd = csp->server_connection.sfd;
3191       poll_fds[1].events = POLLIN;
3192       n = poll(poll_fds, 2, csp->config->socket_timeout * 1000);
3193 #else
3194       timeout.tv_sec = csp->config->socket_timeout;
3195       timeout.tv_usec = 0;
3196       n = select((int)maxfd + 1, &rfds, NULL, NULL, &timeout);
3197 #endif /* def HAVE_POLL */
3198
3199       /* Server or client not responding in timeout */
3200       if (n == 0)
3201       {
3202          log_error(LOG_LEVEL_CONNECT, "Socket timeout %d reached: %s",
3203             csp->config->socket_timeout, http->url);
3204          if ((byte_count == 0) && (http->ssl == 0))
3205          {
3206             send_crunch_response(csp, error_response(csp, "connection-timeout"));
3207          }
3208          mark_server_socket_tainted(csp);
3209 #ifdef FEATURE_HTTPS_INSPECTION
3210          close_client_and_server_ssl_connections(csp);
3211 #endif
3212          return;
3213       }
3214       else if (n < 0)
3215       {
3216 #ifdef HAVE_POLL
3217          log_error(LOG_LEVEL_ERROR, "poll() failed!: %E");
3218 #else
3219          log_error(LOG_LEVEL_ERROR, "select() failed!: %E");
3220 #endif
3221          mark_server_socket_tainted(csp);
3222 #ifdef FEATURE_HTTPS_INSPECTION
3223          close_client_and_server_ssl_connections(csp);
3224 #endif
3225          return;
3226       }
3227
3228       /*
3229        * This is the body of the browser's request,
3230        * just read and write it.
3231        *
3232        * Receives data from browser and sends it to server
3233        *
3234        * XXX: Make sure the client doesn't use pipelining
3235        * behind Privoxy's back.
3236        */
3237 #ifdef HAVE_POLL
3238       if ((poll_fds[0].revents & (POLLERR|POLLHUP|POLLNVAL)) != 0)
3239       {
3240          log_error(LOG_LEVEL_CONNECT,
3241             "The client socket %d has become unusable while "
3242             "the server socket %d is still open.",
3243             csp->cfd, csp->server_connection.sfd);
3244          mark_server_socket_tainted(csp);
3245          break;
3246       }
3247
3248       if (poll_fds[0].revents != 0)
3249 #else
3250       if (FD_ISSET(csp->cfd, &rfds))
3251 #endif /* def HAVE_POLL*/
3252       {
3253          int max_bytes_to_read = (int)csp->receive_buffer_size;
3254
3255 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3256          if ((csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ))
3257          {
3258             if (data_is_available(csp->cfd, 0))
3259             {
3260                /*
3261                 * If the next request is already waiting, we have
3262                 * to stop select()ing the client socket. Otherwise
3263                 * we would always return right away and get nothing
3264                 * else done.
3265                 */
3266                watch_client_socket = 0;
3267                log_error(LOG_LEVEL_CONNECT,
3268                   "Stop watching client socket %d. "
3269                   "There's already another request waiting.",
3270                   csp->cfd);
3271                continue;
3272             }
3273             /*
3274              * If the client socket is set, but there's no data
3275              * available on the socket, the client went fishing
3276              * and continuing talking to the server makes no sense.
3277              */
3278             log_error(LOG_LEVEL_CONNECT,
3279                "The client closed socket %d while "
3280                "the server socket %d is still open.",
3281                csp->cfd, csp->server_connection.sfd);
3282             mark_server_socket_tainted(csp);
3283             break;
3284          }
3285          if (csp->expected_client_content_length != 0)
3286          {
3287             if (csp->expected_client_content_length < csp->receive_buffer_size)
3288             {
3289                max_bytes_to_read = (int)csp->expected_client_content_length;
3290             }
3291             log_error(LOG_LEVEL_CONNECT,
3292                "Waiting for up to %d bytes from the client.",
3293                max_bytes_to_read);
3294          }
3295          assert(max_bytes_to_read <= csp->receive_buffer_size);
3296 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3297
3298 #ifdef FEATURE_HTTPS_INSPECTION
3299          if (client_use_ssl(csp))
3300          {
3301             if (csp->http->status == 101)
3302             {
3303                len = ssl_recv_data(&(csp->ssl_client_attr),
3304                   (unsigned char *)csp->receive_buffer,
3305                   (size_t)max_bytes_to_read);
3306                if (len == -1)
3307                {
3308                   log_error(LOG_LEVEL_ERROR, "Failed to receive data "
3309                      "on client socket %d for an upgraded connection",
3310                      csp->cfd);
3311                   break;
3312                }
3313                if (len == 0)
3314                {
3315                   log_error(LOG_LEVEL_CONNECT, "Done receiving data "
3316                      "on client socket %d for an upgraded connection",
3317                      csp->cfd);
3318                   break;
3319                }
3320                byte_count += (unsigned long long)len;
3321                len = ssl_send_data(&(csp->ssl_server_attr),
3322                   (unsigned char *)csp->receive_buffer, (size_t)len);
3323                if (len == -1)
3324                {
3325                   log_error(LOG_LEVEL_ERROR, "Failed to send data "
3326                      "on server socket %d for an upgraded connection",
3327                      csp->server_connection.sfd);
3328                   break;
3329                }
3330                continue;
3331             }
3332             log_error(LOG_LEVEL_CONNECT, "Breaking with TLS/SSL.");
3333             break;
3334          }
3335          else
3336 #endif /* def FEATURE_HTTPS_INSPECTION */
3337          {
3338             len = read_socket(csp->cfd, csp->receive_buffer, max_bytes_to_read);
3339
3340             if (len <= 0)
3341             {
3342                /* XXX: not sure if this is necessary. */
3343                mark_server_socket_tainted(csp);
3344                break; /* "game over, man" */
3345             }
3346
3347 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3348             if (csp->expected_client_content_length != 0)
3349             {
3350                assert(len <= max_bytes_to_read);
3351                csp->expected_client_content_length -= (unsigned)len;
3352                log_error(LOG_LEVEL_CONNECT,
3353                   "Expected client content length set to %llu "
3354                   "after reading %ld bytes.",
3355                   csp->expected_client_content_length, len);
3356                if (csp->expected_client_content_length == 0)
3357                {
3358                   log_error(LOG_LEVEL_CONNECT,
3359                      "Done reading from the client.");
3360                   csp->flags |= CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ;
3361                }
3362             }
3363 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3364
3365             if (write_socket(csp->server_connection.sfd, csp->receive_buffer, (size_t)len))
3366             {
3367                log_error(LOG_LEVEL_ERROR, "write to: %s failed: %E", http->host);
3368                mark_server_socket_tainted(csp);
3369                return;
3370             }
3371          }
3372          continue;
3373       }
3374
3375       /*
3376        * The server wants to talk. It could be the header or the body.
3377        * If `hdr' is null, then it's the header otherwise it's the body.
3378        * FIXME: Does `hdr' really mean `host'? No.
3379        */
3380 #ifdef HAVE_POLL
3381       if (poll_fds[1].revents != 0)
3382 #else
3383       if (FD_ISSET(csp->server_connection.sfd, &rfds))
3384 #endif /* HAVE_POLL */
3385       {
3386 #ifdef FEATURE_HTTPS_INSPECTION
3387          server_wants_to_talk:
3388 #endif
3389 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3390          /*
3391           * If we are buffering content, we don't want to eat up to
3392           * buffer-limit bytes if the client no longer cares about them.
3393           * If we aren't buffering, however, a dead client socket will be
3394           * noticed pretty much right away anyway, so we can reduce the
3395           * overhead by skipping the check.
3396           */
3397          if (buffer_and_filter_content && !socket_is_still_alive(csp->cfd))
3398          {
3399 #ifdef _WIN32
3400             log_error(LOG_LEVEL_CONNECT,
3401                "The server still wants to talk, but the client may already have hung up on us.");
3402 #else
3403             log_error(LOG_LEVEL_CONNECT,
3404                "The server still wants to talk, but the client hung up on us.");
3405             mark_server_socket_tainted(csp);
3406 #ifdef FEATURE_HTTPS_INSPECTION
3407             close_client_and_server_ssl_connections(csp);
3408 #endif
3409             return;
3410 #endif /* def _WIN32 */
3411          }
3412 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
3413
3414 #ifdef FEATURE_HTTPS_INSPECTION
3415          /*
3416           * Reading data from standard or secured connection (HTTP/HTTPS)
3417           */
3418          if (server_use_ssl(csp))
3419          {
3420             len = ssl_recv_data(&(csp->ssl_server_attr),
3421                (unsigned char *)csp->receive_buffer, csp->receive_buffer_size);
3422          }
3423          else
3424 #endif
3425          {
3426             len = read_socket(csp->server_connection.sfd, csp->receive_buffer,
3427                (int)csp->receive_buffer_size);
3428          }
3429
3430          if (len < 0)
3431          {
3432             log_error(LOG_LEVEL_ERROR, "read from: %s failed: %E", http->host);
3433
3434             if ((http->ssl && (csp->fwd == NULL))
3435 #ifdef FEATURE_HTTPS_INSPECTION
3436                && use_ssl_tunnel
3437 #endif
3438                 )
3439             {
3440                /*
3441                 * Just hang up. We already confirmed the client's CONNECT
3442                 * request with status code 200 and unencrypted content is
3443                 * no longer welcome.
3444                 */
3445                log_error(LOG_LEVEL_ERROR,
3446                   "CONNECT already confirmed. Unable to tell the client about the problem.");
3447                return;
3448             }
3449             else if (byte_count)
3450             {
3451                /*
3452                 * Just hang up. We already transmitted the original headers
3453                 * and parts of the original content and therefore missed the
3454                 * chance to send an error message (without risking data corruption).
3455                 *
3456                 * XXX: we could retry with a fancy range request here.
3457                 */
3458                log_error(LOG_LEVEL_ERROR, "Already forwarded the original headers. "
3459                   "Unable to tell the client about the problem.");
3460                mark_server_socket_tainted(csp);
3461 #ifdef FEATURE_HTTPS_INSPECTION
3462                close_client_and_server_ssl_connections(csp);
3463 #endif
3464                return;
3465             }
3466             /*
3467              * XXX: Consider handling the cases above the same.
3468              */
3469             mark_server_socket_tainted(csp);
3470             len = 0;
3471          }
3472
3473 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
3474          if (csp->flags & CSP_FLAG_CHUNKED)
3475          {
3476             if ((len >= 5) && !memcmp(csp->receive_buffer+len-5, "0\r\n\r\n", 5))
3477             {
3478                /* XXX: this is a temporary hack */
3479                log_error(LOG_LEVEL_CONNECT,
3480                   "Looks like we reached the end of the last chunk. "
3481                   "We better stop reading.");
3482                csp->expected_content_length = byte_count + (unsigned long long)len;
3483                csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
3484             }
3485          }
3486          reading_done:
3487 #endif  /* FEATURE_CONNECTION_KEEP_ALIVE */
3488
3489          /*
3490           * This is guaranteed by allocating with zalloc_or_die()
3491           * and never (intentionally) writing to the last byte.
3492           *
3493           * csp->receive_buffer_size is the size of the part of the
3494           * buffer we intentionally write to, but we actually
3495           * allocated csp->receive_buffer_size+1 bytes so the assertion
3496           * stays within the allocated range.
3497           */
3498          assert(csp->receive_buffer[csp->receive_buffer_size] == '\0');
3499
3500          /*
3501           * Add a trailing zero to let be able to use string operations.
3502           * XXX: do we still need this with filter_popups gone?
3503           */
3504          assert(len <= csp->receive_buffer_size);
3505          csp->receive_buffer[len] = '\0';
3506
3507          /*
3508           * Normally, this would indicate that we've read
3509           * as much as the server has sent us and we can
3510           * close the client connection.  However, Microsoft
3511           * in its wisdom has released IIS/5 with a bug that
3512           * prevents it from sending the trailing \r\n in
3513           * a 302 redirect header (and possibly other headers).
3514           * To work around this if we've haven't parsed
3515           * a full header we'll append a trailing \r\n
3516           * and see if this now generates a valid one.
3517           *
3518           * This hack shouldn't have any impacts.  If we've
3519           * already transmitted the header or if this is a
3520           * SSL connection, then we won't bother with this
3521           * hack.  So we only work on partially received
3522           * headers.  If we append a \r\n and this still
3523           * doesn't generate a valid header, then we won't
3524           * transmit anything to the client.
3525           */
3526          if (len == 0)
3527          {
3528
3529             if (server_body || (http->ssl
3530 #ifdef FEATURE_HTTPS_INSPECTION
3531                   && use_ssl_tunnel
3532 #endif
3533                ))
3534             {
3535                /*
3536                 * If we have been buffering up the document,
3537                 * now is the time to apply content modification
3538                 * and send the result to the client.
3539                 */
3540                if (buffer_and_filter_content)
3541                {
3542                   p = execute_content_filters(csp);
3543                   /*
3544                    * If content filtering fails, use the original
3545                    * buffer and length.
3546                    * (see p != NULL ? p : csp->iob->cur below)
3547                    */
3548                   if (NULL == p)
3549                   {
3550                      csp->content_length = (size_t)(csp->iob->eod - csp->iob->cur);
3551                   }
3552 #ifdef FEATURE_COMPRESSION
3553                   else if ((csp->flags & CSP_FLAG_CLIENT_SUPPORTS_DEFLATE)
3554                      && (csp->content_length > LOWER_LENGTH_LIMIT_FOR_COMPRESSION))
3555                   {
3556                      char *compressed_content = compress_buffer(p,
3557                         (size_t *)&csp->content_length, csp->config->compression_level);
3558                      if (compressed_content != NULL)
3559                      {
3560                         freez(p);
3561                         p = compressed_content;
3562                         csp->flags |= CSP_FLAG_BUFFERED_CONTENT_DEFLATED;
3563                      }
3564                   }
3565 #endif
3566
3567                   if (JB_ERR_OK != update_server_headers(csp))
3568                   {
3569                      log_error(LOG_LEVEL_FATAL,
3570                         "Failed to update server headers. after filtering.");
3571                   }
3572
3573                   hdr = list_to_text(csp->headers);
3574                   if (hdr == NULL)
3575                   {
3576                      /* FIXME Should handle error properly */
3577                      log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
3578                   }
3579
3580 #ifdef FEATURE_HTTPS_INSPECTION
3581                   /*
3582                    * Sending data with standard or secured connection (HTTP/HTTPS)
3583                    */
3584                   if (client_use_ssl(csp))
3585                   {
3586                      if ((ssl_send_data_delayed(&(csp->ssl_client_attr),
3587                               (const unsigned char *)hdr, strlen(hdr),
3588                               get_write_delay(csp)) < 0)
3589                         || (ssl_send_data_delayed(&(csp->ssl_client_attr),
3590                               (const unsigned char *) ((p != NULL) ? p : csp->iob->cur),
3591                               csp->content_length, get_write_delay(csp)) < 0))
3592                      {
3593                         log_error(LOG_LEVEL_ERROR,
3594                            "Failed to send the modified content to the client over TLS");
3595                         freez(hdr);
3596                         freez(p);
3597                         mark_server_socket_tainted(csp);
3598                         close_client_and_server_ssl_connections(csp);
3599                         return;
3600                      }
3601                   }
3602                   else
3603 #endif /* def FEATURE_HTTPS_INSPECTION */
3604                   {
3605                      if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
3606                       || write_socket_delayed(csp->cfd, ((p != NULL) ? p : csp->iob->cur),
3607                          (size_t)csp->content_length, write_delay))
3608                      {
3609                         log_error(LOG_LEVEL_ERROR, "write modified content to client failed: %E");
3610                         freez(hdr);
3611                         freez(p);
3612                         mark_server_socket_tainted(csp);
3613                         return;
3614                      }
3615                   }
3616
3617                   freez(hdr);
3618                   freez(p);
3619                }
3620
3621                break; /* "game over, man" */
3622             }
3623
3624             /*
3625              * This is not the body, so let's pretend the server just sent
3626              * us a blank line.
3627              */
3628             snprintf(csp->receive_buffer, csp->receive_buffer_size, "\r\n");
3629             len = (int)strlen(csp->receive_buffer);
3630
3631             /*
3632              * Now, let the normal header parsing algorithm below do its
3633              * job.  If it fails, we'll exit instead of continuing.
3634              */
3635
3636             ms_iis5_hack = 1;
3637          }
3638
3639          /*
3640           * If we're in the body of the server document, just write it to
3641           * the client, unless we need to buffer the body for later
3642           * content-filtering.
3643           */
3644          if (server_body || (http->ssl
3645 #ifdef FEATURE_HTTPS_INSPECTION
3646                && use_ssl_tunnel
3647 #endif
3648             ))
3649          {
3650             if (buffer_and_filter_content)
3651             {
3652                /*
3653                 * If there is no memory left for buffering the content, or the buffer limit
3654                 * has been reached, switch to non-filtering mode, i.e. make & write the
3655                 * header, flush the iob and buf, and get out of the way.
3656                 */
3657                if (add_to_iob(csp->iob, csp->config->buffer_limit, csp->receive_buffer, len))
3658                {
3659                   size_t hdrlen;
3660                   long flushed;
3661
3662                   log_error(LOG_LEVEL_INFO,
3663                      "Flushing header and buffers. Stepping back from filtering.");
3664
3665                   hdr = list_to_text(csp->headers);
3666                   if (hdr == NULL)
3667                   {
3668                      /*
3669                       * Memory is too tight to even generate the header.
3670                       * Send our static "Out-of-memory" page.
3671                       */
3672                      log_error(LOG_LEVEL_ERROR, "Out of memory while trying to flush.");
3673                      rsp = cgi_error_memory();
3674                      send_crunch_response(csp, rsp);
3675                      mark_server_socket_tainted(csp);
3676 #ifdef FEATURE_HTTPS_INSPECTION
3677                      close_client_and_server_ssl_connections(csp);
3678 #endif
3679                      return;
3680                   }
3681                   hdrlen = strlen(hdr);
3682
3683 #ifdef FEATURE_HTTPS_INSPECTION
3684                   /*
3685                    * Sending data with standard or secured connection (HTTP/HTTPS)
3686                    */
3687                   if (client_use_ssl(csp))
3688                   {
3689                      if ((ssl_send_data_delayed(&(csp->ssl_client_attr),
3690                              (const unsigned char *)hdr, hdrlen, get_write_delay(csp)) < 0)
3691                         || ((flushed = ssl_flush_socket(&(csp->ssl_client_attr),
3692                                 csp->iob)) < 0)
3693                         || (ssl_send_data_delayed(&(csp->ssl_client_attr),
3694                               (const unsigned char *)csp->receive_buffer, (size_t)len,
3695                               get_write_delay(csp)) < 0))
3696                      {
3697                         log_error(LOG_LEVEL_CONNECT,
3698                            "Flush header and buffers to client failed");
3699                         freez(hdr);
3700                         mark_server_socket_tainted(csp);
3701                         close_client_and_server_ssl_connections(csp);
3702                         return;
3703                      }
3704                   }
3705                   else
3706 #endif /* def FEATURE_HTTPS_INSPECTION */
3707                   {
3708                      if (write_socket_delayed(csp->cfd, hdr, hdrlen, write_delay)
3709                       || ((flushed = flush_iob(csp->cfd, csp->iob, write_delay)) < 0)
3710                       || write_socket_delayed(csp->cfd, csp->receive_buffer, (size_t)len,
3711                             write_delay))
3712                      {
3713                         log_error(LOG_LEVEL_CONNECT,
3714                            "Flush header and buffers to client failed: %E");
3715                         freez(hdr);
3716                         mark_server_socket_tainted(csp);
3717                         return;
3718                      }
3719                   }
3720
3721                   /*
3722                    * Reset the byte_count to the amount of bytes
3723                    * we just flushed. len will be added a few lines below,
3724                    * hdrlen doesn't matter for LOG_LEVEL_CLF.
3725                    */
3726                   byte_count = (unsigned long long)flushed;
3727                   freez(hdr);
3728                   buffer_and_filter_content = 0;
3729                   server_body = 1;
3730                }
3731             }
3732             else
3733             {
3734 #ifdef FEATURE_HTTPS_INSPECTION
3735                /*
3736                 * Sending data with standard or secured connection (HTTP/HTTPS)
3737                 */
3738                if (client_use_ssl(csp))
3739                {
3740                   ret = ssl_send_data_delayed(&(csp->ssl_client_attr),
3741                      (const unsigned char *)csp->receive_buffer, (size_t)len,
3742                      get_write_delay(csp));
3743                   if (ret < 0)
3744                   {
3745                      log_error(LOG_LEVEL_ERROR,
3746                         "Sending data to client failed");
3747                      mark_server_socket_tainted(csp);
3748                      close_client_and_server_ssl_connections(csp);
3749                      return;
3750                   }
3751                }
3752                else
3753 #endif /* def FEATURE_HTTPS_INSPECTION */
3754                {
3755                   if (write_socket_delayed(csp->cfd, csp->receive_buffer,
3756                         (size_t)len, write_delay))
3757                   {
3758                      log_error(LOG_LEVEL_ERROR, "write to client failed: %E");
3759                      mark_server_socket_tainted(csp);
3760                      return;
3761                   }
3762                }
3763             }
3764             byte_count += (unsigned long long)len;
3765             continue;
3766          }
3767          else
3768          {
3769             /*
3770              * We're still looking for the end of the server's header.
3771              * Buffer up the data we just read.  If that fails, there's
3772              * little we can do but send our static out-of-memory page.
3773              */
3774             if (add_to_iob(csp->iob, csp->config->buffer_limit, csp->receive_buffer, len))
3775             {
3776                log_error(LOG_LEVEL_ERROR, "Out of memory while looking for end of server headers.");
3777                rsp = cgi_error_memory();
3778                send_crunch_response(csp, rsp);
3779                mark_server_socket_tainted(csp);
3780 #ifdef FEATURE_HTTPS_INSPECTION
3781                close_client_and_server_ssl_connections(csp);
3782 #endif
3783                return;
3784             }
3785
3786             /* Convert iob into something sed() can digest */
3787             if (JB_ERR_PARSE == get_server_headers(csp))
3788             {
3789                if (ms_iis5_hack)
3790                {
3791                   /*
3792                    * Well, we tried our MS IIS/5 hack and it didn't work.
3793                    * The header is incomplete and there isn't anything
3794                    * we can do about it.
3795                    */
3796                   log_error(LOG_LEVEL_ERROR, "Invalid server headers. "
3797                      "Applying the MS IIS5 hack didn't help.");
3798                   log_error(LOG_LEVEL_CLF,
3799                      "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3800 #ifdef FEATURE_HTTPS_INSPECTION
3801                   /*
3802                    * Sending data with standard or secured connection (HTTP/HTTPS)
3803                    */
3804                   if (client_use_ssl(csp))
3805                   {
3806                      ssl_send_data_delayed(&(csp->ssl_client_attr),
3807                         (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3808                         strlen(INVALID_SERVER_HEADERS_RESPONSE), get_write_delay(csp));
3809                   }
3810                   else
3811 #endif /* def FEATURE_HTTPS_INSPECTION */
3812                   {
3813                      write_socket_delayed(csp->cfd,
3814                         INVALID_SERVER_HEADERS_RESPONSE,
3815                         strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3816                   }
3817                   mark_server_socket_tainted(csp);
3818 #ifdef FEATURE_HTTPS_INSPECTION
3819                   close_client_and_server_ssl_connections(csp);
3820 #endif
3821                   return;
3822                }
3823                else
3824                {
3825                   /*
3826                    * Since we have to wait for more from the server before
3827                    * we can parse the headers we just continue here.
3828                    */
3829                   log_error(LOG_LEVEL_CONNECT,
3830                      "Continuing buffering server headers from socket %d. "
3831                      "Bytes most recently read: %ld.", csp->cfd, len);
3832                   continue;
3833                }
3834             }
3835             else
3836             {
3837                /*
3838                 * Account for the content bytes we
3839                 * might have gotten with the headers.
3840                 */
3841                assert(csp->iob->eod >= csp->iob->cur);
3842                byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3843             }
3844
3845             /* Did we actually get anything? */
3846             if (NULL == csp->headers->first)
3847             {
3848                if ((csp->flags & CSP_FLAG_REUSED_CLIENT_CONNECTION))
3849                {
3850                   log_error(LOG_LEVEL_ERROR,
3851                      "No server or forwarder response received on socket %d. "
3852                      "Closing client socket %d without sending data.",
3853                      csp->server_connection.sfd, csp->cfd);
3854                   log_error(LOG_LEVEL_CLF,
3855                      "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3856                }
3857                else
3858                {
3859                   log_error(LOG_LEVEL_ERROR,
3860                      "No server or forwarder response received on socket %d.",
3861                      csp->server_connection.sfd);
3862                   send_crunch_response(csp, error_response(csp, "no-server-data"));
3863                }
3864                free_http_request(http);
3865                mark_server_socket_tainted(csp);
3866 #ifdef FEATURE_HTTPS_INSPECTION
3867                close_client_and_server_ssl_connections(csp);
3868 #endif
3869                return;
3870             }
3871
3872             if (!csp->headers->first->str)
3873             {
3874                log_error(LOG_LEVEL_ERROR, "header search: csp->headers->first->str == NULL, assert will be called");
3875             }
3876             assert(csp->headers->first->str);
3877
3878             if (strncmpic(csp->headers->first->str, "HTTP", 4) &&
3879                 strncmpic(csp->headers->first->str, "ICY", 3))
3880             {
3881                /*
3882                 * It doesn't look like a HTTP (or Shoutcast) response:
3883                 * tell the client and log the problem.
3884                 */
3885                if (strlen(csp->headers->first->str) > 30)
3886                {
3887                   csp->headers->first->str[30] = '\0';
3888                }
3889                log_error(LOG_LEVEL_ERROR,
3890                   "Invalid server or forwarder response. Starts with: %s",
3891                   csp->headers->first->str);
3892                log_error(LOG_LEVEL_CLF,
3893                   "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3894 #ifdef FEATURE_HTTPS_INSPECTION
3895                /*
3896                 * Sending data with standard or secured connection (HTTP/HTTPS)
3897                 */
3898                if (client_use_ssl(csp))
3899                {
3900                   ssl_send_data_delayed(&(csp->ssl_client_attr),
3901                      (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3902                      strlen(INVALID_SERVER_HEADERS_RESPONSE),
3903                      get_write_delay(csp));
3904                }
3905                else
3906 #endif /* def FEATURE_HTTPS_INSPECTION */
3907                {
3908                   write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3909                      strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3910                }
3911                free_http_request(http);
3912                mark_server_socket_tainted(csp);
3913 #ifdef FEATURE_HTTPS_INSPECTION
3914                close_client_and_server_ssl_connections(csp);
3915 #endif
3916                return;
3917             }
3918
3919             /*
3920              * Disable redirect checkers, so that they will be only run
3921              * again if the user also enables them through tags.
3922              *
3923              * From a performance point of view it doesn't matter,
3924              * but it prevents duplicated log messages.
3925              */
3926 #ifdef FEATURE_FAST_REDIRECTS
3927             csp->action->flags &= ~ACTION_FAST_REDIRECTS;
3928 #endif
3929             csp->action->flags &= ~ACTION_REDIRECT;
3930
3931             /*
3932              * We have now received the entire server header,
3933              * filter it and send the result to the client
3934              */
3935             if (JB_ERR_OK != sed(csp, FILTER_SERVER_HEADERS))
3936             {
3937                log_error(LOG_LEVEL_CLF,
3938                   "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
3939 #ifdef FEATURE_HTTPS_INSPECTION
3940                /*
3941                 * Sending data with standard or secured connection (HTTP/HTTPS)
3942                 */
3943                if (client_use_ssl(csp))
3944                {
3945                   ssl_send_data_delayed(&(csp->ssl_client_attr),
3946                      (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
3947                      strlen(INVALID_SERVER_HEADERS_RESPONSE),
3948                      get_write_delay(csp));
3949                }
3950                else
3951 #endif
3952                {
3953                   write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
3954                      strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
3955                }
3956                free_http_request(http);
3957                mark_server_socket_tainted(csp);
3958 #ifdef FEATURE_HTTPS_INSPECTION
3959                close_client_and_server_ssl_connections(csp);
3960 #endif
3961                return;
3962             }
3963             hdr = list_to_text(csp->headers);
3964             if (hdr == NULL)
3965             {
3966                /* FIXME Should handle error properly */
3967                log_error(LOG_LEVEL_FATAL, "Out of memory parsing server header");
3968             }
3969
3970             if ((csp->flags & CSP_FLAG_CHUNKED)
3971                && !(csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
3972                && ((csp->iob->eod - csp->iob->cur) >= 5)
3973                && !memcmp(csp->iob->eod-5, "0\r\n\r\n", 5))
3974             {
3975                log_error(LOG_LEVEL_CONNECT,
3976                   "Looks like we got the last chunk together with "
3977                   "the server headers. We better stop reading.");
3978                byte_count = (unsigned long long)(csp->iob->eod - csp->iob->cur);
3979                csp->expected_content_length = byte_count;
3980                csp->flags |= CSP_FLAG_CONTENT_LENGTH_SET;
3981             }
3982
3983             csp->server_connection.response_received = time(NULL);
3984
3985             if (crunch_response_triggered(csp, crunchers_light))
3986             {
3987                /*
3988                 * One of the tags created by a server-header
3989                 * tagger triggered a crunch. We already
3990                 * delivered the crunch response to the client
3991                 * and are done here after cleaning up.
3992                 */
3993                freez(hdr);
3994                mark_server_socket_tainted(csp);
3995 #ifdef FEATURE_HTTPS_INSPECTION
3996                close_client_and_server_ssl_connections(csp);
3997 #endif
3998                return;
3999             }
4000
4001             /* Buffer and pcrs filter this if appropriate. */
4002             buffer_and_filter_content = content_requires_filtering(csp);
4003
4004             if (!buffer_and_filter_content)
4005             {
4006                /*
4007                 * Write the server's (modified) header to
4008                 * the client (along with anything else that
4009                 * may be in the buffer). Use standard or secured
4010                 * connection.
4011                 */
4012 #ifdef FEATURE_HTTPS_INSPECTION
4013                if (client_use_ssl(csp))
4014                {
4015                   if ((ssl_send_data_delayed(&(csp->ssl_client_attr),
4016                           (const unsigned char *)hdr, strlen(hdr),
4017                           get_write_delay(csp)) < 0)
4018                      || ((len = ssl_flush_socket(&(csp->ssl_client_attr),
4019                             csp->iob)) < 0))
4020                   {
4021                      log_error(LOG_LEVEL_CONNECT, "Write header to client failed");
4022
4023                      /*
4024                       * The write failed, so don't bother mentioning it
4025                       * to the client... it probably can't hear us anyway.
4026                       */
4027                      freez(hdr);
4028                      mark_server_socket_tainted(csp);
4029 #ifdef FEATURE_HTTPS_INSPECTION
4030                      close_client_and_server_ssl_connections(csp);
4031 #endif
4032                      return;
4033                   }
4034                }
4035                else
4036 #endif /* def FEATURE_HTTPS_INSPECTION */
4037                {
4038                   if (write_socket_delayed(csp->cfd, hdr, strlen(hdr), write_delay)
4039                      || ((len = flush_iob(csp->cfd, csp->iob, write_delay)) < 0))
4040                   {
4041                      log_error(LOG_LEVEL_ERROR,
4042                         "write header to client failed");
4043                      /*
4044                       * The write failed, so don't bother mentioning it
4045                       * to the client... it probably can't hear us anyway.
4046                       */
4047                      freez(hdr);
4048                      mark_server_socket_tainted(csp);
4049                      return;
4050                   }
4051                }
4052                                 }
4053
4054             /* we're finished with the server's header */
4055
4056             freez(hdr);
4057             server_body = 1;
4058
4059             /*
4060              * If this was a MS IIS/5 hack then it means the server
4061              * has already closed the connection. Nothing more to read.
4062              * Time to bail.
4063              */
4064             if (ms_iis5_hack)
4065             {
4066                log_error(LOG_LEVEL_ERROR,
4067                   "Closed server connection detected. "
4068                   "Applying the MS IIS5 hack didn't help.");
4069                log_error(LOG_LEVEL_CLF,
4070                   "%s - - [%T] \"%s\" 502 0", csp->ip_addr_str, http->cmd);
4071 #ifdef FEATURE_HTTPS_INSPECTION
4072                /*
4073                 * Sending data with standard or secured connection (HTTP/HTTPS)
4074                 */
4075                if (client_use_ssl(csp))
4076                {
4077                   ssl_send_data_delayed(&(csp->ssl_client_attr),
4078                      (const unsigned char *)INVALID_SERVER_HEADERS_RESPONSE,
4079                      strlen(INVALID_SERVER_HEADERS_RESPONSE),
4080                      get_write_delay(csp));
4081                }
4082                else
4083 #endif /* def FEATURE_HTTPS_INSPECTION */
4084                {
4085                   write_socket_delayed(csp->cfd, INVALID_SERVER_HEADERS_RESPONSE,
4086                      strlen(INVALID_SERVER_HEADERS_RESPONSE), write_delay);
4087                }
4088                mark_server_socket_tainted(csp);
4089 #ifdef FEATURE_HTTPS_INSPECTION
4090                close_client_and_server_ssl_connections(csp);
4091 #endif
4092                return;
4093             }
4094          }
4095          continue;
4096       }
4097       mark_server_socket_tainted(csp);
4098 #ifdef FEATURE_HTTPS_INSPECTION
4099       close_client_and_server_ssl_connections(csp);
4100 #endif
4101       return; /* huh? we should never get here */
4102    }
4103
4104    if (csp->content_length == 0)
4105    {
4106       /*
4107        * If Privoxy didn't recalculate the Content-Length,
4108        * byte_count is still correct.
4109        */
4110       csp->content_length = byte_count;
4111    }
4112
4113 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
4114    if ((csp->flags & CSP_FLAG_CONTENT_LENGTH_SET)
4115       && (csp->expected_content_length != byte_count))
4116    {
4117       log_error(LOG_LEVEL_CONNECT,
4118          "Received %llu bytes while expecting %llu.",
4119          byte_count, csp->expected_content_length);
4120       mark_server_socket_tainted(csp);
4121    }
4122 #endif
4123
4124 #ifdef FEATURE_HTTPS_INSPECTION
4125    if (client_use_ssl(csp))
4126    {
4127       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s https://%s%s %s\" 200 %llu",
4128          csp->ip_addr_str, http->gpc, http->hostport, http->path,
4129          http->version, csp->content_length);
4130    }
4131    else
4132 #endif
4133    {
4134       log_error(LOG_LEVEL_CLF, "%s - - [%T] \"%s\" 200 %llu",
4135          csp->ip_addr_str, http->ocmd, csp->content_length);
4136    }
4137    csp->server_connection.timestamp = time(NULL);
4138 }
4139
4140
4141 /*********************************************************************
4142  *
4143  * Function    :  chat
4144  *
4145  * Description :  Once a connection from the client has been accepted,
4146  *                this function is called (via serve()) to handle the
4147  *                main business of the communication.  This function
4148  *                returns after dealing with a single request. It can
4149  *                be called multiple times with the same client socket
4150  *                if the client is keeping the connection alive.
4151  *
4152  *                The decision whether or not a client connection will
4153  *                be kept alive is up to the caller which also must
4154  *                close the client socket when done.
4155  *
4156  *                FIXME: chat is nearly thousand lines long.
4157  *                Ridiculous.
4158  *
4159  * Parameters  :
4160  *          1  :  csp = Current client state (buffers, headers, etc...)
4161  *
4162  * Returns     :  Nothing.
4163  *
4164  *********************************************************************/
4165 static void chat(struct client_state *csp)
4166 {
4167    const struct forward_spec *fwd;
4168    struct http_request *http;
4169    /* Skeleton for HTTP response, if we should intercept the request */
4170    struct http_response *rsp;
4171 #ifdef FEATURE_HTTPS_INSPECTION
4172    int use_ssl_tunnel = 0;
4173 #endif
4174
4175    http = csp->http;
4176
4177    if (receive_client_request(csp) != JB_ERR_OK)
4178    {
4179       return;
4180    }
4181    if (parse_client_request(csp) != JB_ERR_OK)
4182    {
4183       return;
4184    }
4185
4186 #ifdef FEATURE_HTTPS_INSPECTION
4187    /*
4188     * Setting flags to use old solution with SSL tunnel and to disable
4189     * certificate verification.
4190     */
4191    if (csp->http->ssl && !(csp->action->flags & ACTION_HTTPS_INSPECTION)
4192       && !cgi_page_requested(csp->http->host))
4193    {
4194       use_ssl_tunnel = 1;
4195    }
4196
4197    if (http->ssl && (csp->action->flags & ACTION_IGNORE_CERTIFICATE_ERRORS))
4198    {
4199       csp->dont_verify_certificate = 1;
4200    }
4201 #endif
4202
4203    /*
4204     * build the http request to send to the server
4205     * we have to do one of the following:
4206     *
4207     * create =    use the original HTTP request to create a new
4208     *             HTTP request that has either the path component
4209     *             without the http://domainspec (w/path) or the
4210     *             full orininal URL (w/url)
4211     *             Note that the path and/or the HTTP version may
4212     *             have been altered by now.
4213     *
4214     * SSL proxy = Open a socket to the host:port of the server
4215     *             and create TLS/SSL connection with server and
4216     *             with client. Then behave like mediator between
4217     *             client and server over TLS/SSL.
4218     *
4219     * SSL proxy = Pass the request unchanged if forwarding a CONNECT
4220     *    with     request to a parent proxy. Note that we'll be sending
4221     * forwarding  the CFAIL message ourselves if connecting to the parent
4222     *             fails, but we won't send a CSUCCEED message if it works,
4223     *             since that would result in a double message (ours and the
4224     *             parent's). After sending the request to the parent, we
4225     *             must parse answer and send it to client. If connection
4226     *             with server is established, we do TLS/SSL proxy. Otherwise
4227     *             we send parent response to client and close connections.
4228     *
4229     * here's the matrix:
4230     *                        SSL
4231     *                    0        1
4232     *                +--------+--------+
4233     *                |        |        |
4234     *             0  | create |   SSL  |
4235     *                | w/path |  proxy |
4236     *  Forwarding    +--------+--------+
4237     *                |        |   SSL  |
4238     *             1  | create |  proxy |
4239     *                | w/url  |+forward|
4240     *                +--------+--------+
4241     *
4242     */
4243
4244 #ifdef FEATURE_HTTPS_INSPECTION
4245    /*
4246     * Presetting SSL client and server flags
4247     */
4248    if (http->ssl && !use_ssl_tunnel)
4249    {
4250       http->client_ssl = 1;
4251       http->server_ssl = 1;
4252    }
4253    else
4254    {
4255       http->client_ssl = 0;
4256       http->server_ssl = 0;
4257    }
4258 #endif
4259
4260 #ifdef FEATURE_HTTPS_INSPECTION
4261    /*
4262     * Log the request unless we're https inspecting
4263     * in which case we don't have the path yet and
4264     * will log the request later.
4265     */
4266    if (!client_use_ssl(csp))
4267 #endif
4268    {
4269       log_error(LOG_LEVEL_REQUEST, "%s%s", http->hostport, http->path);
4270    }
4271
4272    if (http->ssl && connect_port_is_forbidden(csp))
4273    {
4274       const char *acceptable_connect_ports =
4275          csp->action->string[ACTION_STRING_LIMIT_CONNECT];
4276       assert(NULL != acceptable_connect_ports);
4277       log_error(LOG_LEVEL_INFO, "Request from %s marked for blocking. "
4278          "limit-connect{%s} doesn't allow CONNECT requests to %s",
4279          csp->ip_addr_str, acceptable_connect_ports, csp->http->hostport);
4280       csp->action->flags |= ACTION_BLOCK;
4281       http->ssl = 0;
4282 #ifdef FEATURE_HTTPS_INSPECTION
4283       http->client_ssl = 0;
4284       http->server_ssl = 0;
4285 #endif
4286    }
4287
4288    /*
4289     * We have a request. Check if one of the crunchers wants it
4290     * unless the client wants to use TLS/SSL in which case we
4291     * haven't setup the TLS context yet and will send the crunch
4292     * response later.
4293     */
4294    if (
4295 #ifdef FEATURE_HTTPS_INSPECTION
4296        !client_use_ssl(csp) &&
4297 #endif
4298        crunch_response_triggered(csp, crunchers_all))
4299    {
4300       /*
4301        * Yes. The client got the crunch response and we're done here.
4302        */
4303       return;
4304    }
4305
4306 #ifdef FEATURE_HTTPS_INSPECTION
4307    if (client_use_ssl(csp) && !use_ssl_tunnel)
4308    {
4309       int ret;
4310       /*
4311        * Creating a SSL proxy.
4312        *
4313        * By sending the CSUCCEED message we're lying to the client as
4314        * the connection hasn't actually been established yet. We don't
4315        * establish the connection until we have seen and parsed the
4316        * encrypted client headers.
4317        */
4318       if (write_socket_delayed(csp->cfd, CSUCCEED,
4319             strlen(CSUCCEED), get_write_delay(csp)) != 0)
4320       {
4321          log_error(LOG_LEVEL_ERROR, "Sending SUCCEED to client failed");
4322          return;
4323       }
4324
4325       ret = create_client_ssl_connection(csp);
4326       if (ret != 0)
4327       {
4328          log_error(LOG_LEVEL_ERROR,
4329             "Failed to open a secure connection with the client");
4330          return;
4331       }
4332       if (JB_ERR_OK != process_encrypted_request_headers(csp))
4333       {
4334          close_client_ssl_connection(csp);
4335          return;
4336       }
4337       /*
4338        * We have an encrypted request. Check if one of the crunchers now
4339        * wants it (for example because the previously invisible path was
4340        * required to match).
4341        */
4342       if (crunch_response_triggered(csp, crunchers_all))
4343       {
4344          /*
4345           * Yes. The client got the crunch response and we're done here.
4346           */
4347          return;
4348       }
4349    }
4350 #endif
4351
4352    log_applied_actions(csp->action);
4353
4354    /* decide how to route the HTTP request */
4355    fwd = forward_url(csp, http);
4356
4357    freez(csp->headers->first->str);
4358    build_request_line(csp, fwd, &csp->headers->first->str);
4359
4360    if (fwd->forward_host)
4361    {
4362       log_error(LOG_LEVEL_CONNECT, "via [%s]:%d to: %s",
4363          fwd->forward_host, fwd->forward_port, http->hostport);
4364    }
4365    else
4366    {
4367       log_error(LOG_LEVEL_CONNECT, "to %s", http->hostport);
4368    }
4369
4370    /* here we connect to the server, gateway, or the forwarder */
4371
4372 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
4373    if ((csp->server_connection.sfd != JB_INVALID_SOCKET)
4374       && socket_is_still_alive(csp->server_connection.sfd)
4375       && connection_destination_matches(&csp->server_connection, http, fwd))
4376    {
4377       log_error(LOG_LEVEL_CONNECT,
4378          "Reusing server socket %d connected to %s. Total requests: %u.",
4379          csp->server_connection.sfd, csp->server_connection.host,
4380          csp->server_connection.requests_sent_total);
4381    }
4382    else
4383    {
4384       if (csp->server_connection.sfd != JB_INVALID_SOCKET)
4385       {
4386 #ifdef FEATURE_CONNECTION_SHARING
4387          if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING
4388 #ifdef FEATURE_HTTPS_INSPECTION
4389             && !server_use_ssl(csp)
4390 #endif
4391             )
4392          {
4393             remember_connection(&csp->server_connection);
4394          }
4395          else
4396 #endif /* def FEATURE_CONNECTION_SHARING */
4397          {
4398             log_error(LOG_LEVEL_CONNECT,
4399                "Closing server socket %d connected to %s. Total requests: %u.",
4400                csp->server_connection.sfd, csp->server_connection.host,
4401                csp->server_connection.requests_sent_total);
4402             close_socket(csp->server_connection.sfd);
4403          }
4404          mark_connection_closed(&csp->server_connection);
4405       }
4406 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
4407
4408       /*
4409        * Connecting to destination server
4410        */
4411       csp->server_connection.sfd = forwarded_connect(fwd, http, csp);
4412
4413       if (csp->server_connection.sfd == JB_INVALID_SOCKET)
4414       {
4415          if (fwd->type != SOCKS_NONE)
4416          {
4417             /* Socks error. */
4418             rsp = error_response(csp, "forwarding-failed");
4419          }
4420          else if (errno == EINVAL)
4421          {
4422             rsp = error_response(csp, "no-such-domain");
4423          }
4424          else
4425          {
4426             rsp = error_response(csp, "connect-failed");
4427          }
4428
4429          /* Write the answer to the client */
4430          if (rsp != NULL)
4431          {
4432             send_crunch_response(csp, rsp);
4433          }
4434
4435          /*
4436           * Temporary workaround to prevent already-read client
4437           * bodies from being parsed as new requests. For now we
4438           * err on the safe side and throw all the following
4439           * requests under the bus, even if no client body has been
4440           * buffered. A compliant client will repeat the dropped
4441           * requests on an untainted connection.
4442           *
4443           * The proper fix is to discard the no longer needed
4444           * client body in the buffer (if there is one) and to
4445           * continue parsing the bytes that follow.
4446           */
4447 #ifdef FEATURE_HTTPS_INSPECTION
4448          close_client_ssl_connection(csp);
4449 #endif
4450          drain_and_close_socket(csp->cfd);
4451          csp->cfd = JB_INVALID_SOCKET;
4452
4453          return;
4454       }
4455
4456 #ifdef FEATURE_HTTPS_INSPECTION
4457       /*
4458        * Creating TLS/SSL connections with destination server or parent
4459        * proxy. If forwarding is enabled, we must send client request to
4460        * parent proxy and receive, parse and resend parent proxy answer.
4461        */
4462       if (http->ssl && !use_ssl_tunnel)
4463       {
4464          if (fwd->forward_host != NULL)
4465          {
4466             char server_response[BUFFER_SIZE];
4467             int ret = 0;
4468             int len = 0;
4469             char *hdr = list_to_text(csp->headers);
4470             memset(server_response, 0, sizeof(server_response));
4471
4472             if (hdr == NULL)
4473             {
4474                log_error(LOG_LEVEL_FATAL,
4475                   "Out of memory parsing client header");
4476             }
4477             list_remove_all(csp->headers);
4478
4479             /*
4480              * Sending client's CONNECT request to the parent proxy
4481              */
4482             ret = write_socket(csp->server_connection.sfd, hdr, strlen(hdr));
4483
4484             freez(hdr);
4485
4486             if (ret != 0)
4487             {
4488                log_error(LOG_LEVEL_CONNECT,
4489                   "Sending request headers to: %s failed", http->hostport);
4490                mark_server_socket_tainted(csp);
4491                close_client_ssl_connection(csp);
4492                return;
4493             }
4494
4495             /* Waiting for parent proxy server response */
4496             len = read_socket(csp->server_connection.sfd, server_response,
4497                sizeof(server_response)-1);
4498
4499             if (len <= 0)
4500             {
4501                log_error(LOG_LEVEL_ERROR, "No response from parent proxy "
4502                   "server on socket %d.", csp->server_connection.sfd);
4503
4504                rsp = error_response(csp, "no-server-data");
4505                if (rsp)
4506                {
4507                   send_crunch_response(csp, rsp);
4508                }
4509                mark_server_socket_tainted(csp);
4510                close_client_ssl_connection(csp);
4511                return;
4512             }
4513
4514             /*
4515              * Test if the connection to the destination server was
4516              * established successfully by the parent proxy.
4517              */
4518             if (!tunnel_established_successfully(server_response, (unsigned int)len))
4519             {
4520                log_error(LOG_LEVEL_ERROR,
4521                   "The forwarder %s failed to establish a connection with %s",
4522                   fwd->forward_host, http->host);
4523                rsp = error_response(csp, "connect-failed");
4524                if (rsp)
4525                {
4526                   send_crunch_response(csp, rsp);
4527                }
4528                mark_server_socket_tainted(csp);
4529                close_client_ssl_connection(csp);
4530                return;
4531             }
4532          } /* -END- if (fwd->forward_host != NULL) */
4533
4534          /*
4535           * We can now create the TLS/SSL connection with the destination server.
4536           */
4537          int ret = create_server_ssl_connection(csp);
4538          if (ret != 0)
4539          {
4540             if (csp->server_cert_verification_result != SSL_CERT_VALID &&
4541                 csp->server_cert_verification_result != SSL_CERT_NOT_VERIFIED)
4542             {
4543                /*
4544                 * If the server certificate is invalid, we must inform
4545                 * the client and then close connection to the client.
4546                 */
4547                ssl_send_certificate_error(csp);
4548                close_client_and_server_ssl_connections(csp);
4549                return;
4550             }
4551             if (csp->server_cert_verification_result == SSL_CERT_NOT_VERIFIED
4552              || csp->server_cert_verification_result == SSL_CERT_VALID)
4553             {
4554                /*
4555                 * The TLS/SSL connection wasn't created but an invalid
4556                 * certificate wasn't detected. Report it as connection
4557                 * failure.
4558                 */
4559                rsp = error_response(csp, "connect-failed");
4560                if (rsp)
4561                {
4562                   send_crunch_response(csp, rsp);
4563                }
4564                close_client_and_server_ssl_connections(csp);
4565                return;
4566             }
4567          }
4568       }/* -END- if (http->ssl) */
4569 #endif /* def FEATURE_HTTPS_INSPECTION */
4570
4571 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
4572       save_connection_destination(csp->server_connection.sfd,
4573          http, fwd, &csp->server_connection);
4574       csp->server_connection.keep_alive_timeout =
4575          (unsigned)csp->config->keep_alive_timeout;
4576    }
4577 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
4578
4579    csp->server_connection.requests_sent_total++;
4580
4581    if ((fwd->type == SOCKS_5T) && (NULL == csp->headers->first))
4582    {
4583       /* Client headers have been sent optimistically */
4584       assert(csp->headers->last == NULL);
4585    }
4586    else if (http->ssl == 0 || (fwd->forward_host
4587 #ifdef FEATURE_HTTPS_INSPECTION
4588          && use_ssl_tunnel
4589 #endif
4590            ))
4591    {
4592       if (send_http_request(csp))
4593       {
4594          rsp = error_response(csp, "connect-failed");
4595          if (rsp)
4596          {
4597             send_crunch_response(csp, rsp);
4598          }
4599          return;
4600       }
4601    }
4602    else
4603    {
4604       /*
4605        * Using old solution with SSL tunnel or new solution with SSL proxy
4606        */
4607       list_remove_all(csp->headers);
4608 #ifdef FEATURE_HTTPS_INSPECTION
4609       if (use_ssl_tunnel)
4610 #endif
4611       {
4612          /*
4613          * We're running an SSL tunnel and we're not forwarding,
4614          * so just ditch the client headers, send the "connect succeeded"
4615          * message to the client, flush the rest, and get out of the way.
4616          */
4617          if (write_socket_delayed(csp->cfd, CSUCCEED,
4618                strlen(CSUCCEED), get_write_delay(csp)))
4619          {
4620             return;
4621          }
4622       }
4623 #ifdef FEATURE_HTTPS_INSPECTION
4624       else
4625       {
4626          /*
4627           * If server certificate has been verified and is invalid,
4628           * we must inform the client and then close the connection
4629           * with client and server.
4630           */
4631          if (csp->server_cert_verification_result != SSL_CERT_VALID &&
4632              csp->server_cert_verification_result != SSL_CERT_NOT_VERIFIED)
4633          {
4634             ssl_send_certificate_error(csp);
4635             close_client_and_server_ssl_connections(csp);
4636             return;
4637          }
4638          if (send_https_request(csp))
4639          {
4640             rsp = error_response(csp, "connect-failed");
4641             if (rsp)
4642             {
4643                send_crunch_response(csp, rsp);
4644             }
4645             close_client_and_server_ssl_connections(csp);
4646             return;
4647          }
4648       }
4649 #endif /* def FEATURE_HTTPS_INSPECTION */
4650       clear_iob(csp->client_iob);
4651    }/* -END- else ... if (http->ssl == 1) */
4652
4653    log_error(LOG_LEVEL_CONNECT, "to %s successful", http->hostport);
4654
4655    /* XXX: should the time start earlier for optimistically sent data? */
4656    csp->server_connection.request_sent = time(NULL);
4657
4658    handle_established_connection(csp);
4659    freez(csp->receive_buffer);
4660 }
4661
4662
4663 #ifdef FUZZ
4664 /*********************************************************************
4665  *
4666  * Function    :  fuzz_server_response
4667  *
4668  * Description :  Treat the input as a whole server response.
4669  *
4670  * Parameters  :
4671  *          1  :  csp = Current client state (buffers, headers, etc...)
4672  *          2  :  fuzz_input_file = File to read the input from.
4673  *
4674  * Returns     :  0
4675  *
4676  *********************************************************************/
4677 extern int fuzz_server_response(struct client_state *csp, char *fuzz_input_file)
4678 {
4679    static struct forward_spec fwd; /* Zero'd due to being static */
4680    csp->cfd = 0;
4681
4682    if (strcmp(fuzz_input_file, "-") == 0)
4683    {
4684       /* XXX: Doesn't work yet. */
4685       csp->server_connection.sfd = 0;
4686    }
4687    else
4688    {
4689       csp->server_connection.sfd = open(fuzz_input_file, O_RDONLY);
4690       if (csp->server_connection.sfd == -1)
4691       {
4692          log_error(LOG_LEVEL_FATAL, "Failed to open %s: %E",
4693             fuzz_input_file);
4694       }
4695    }
4696    csp->fwd = &fwd;
4697    csp->content_type |= CT_GIF;
4698    csp->action->flags |= ACTION_DEANIMATE;
4699    csp->action->string[ACTION_STRING_DEANIMATE] = "last";
4700
4701    csp->http->path = strdup_or_die("/");
4702    csp->http->host = strdup_or_die("fuzz.example.org");
4703    csp->http->hostport = strdup_or_die("fuzz.example.org:80");
4704    /* Prevent client socket monitoring */
4705    csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
4706    csp->flags |= CSP_FLAG_CHUNKED;
4707
4708    csp->config->feature_flags |= RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE;
4709    csp->flags |= CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
4710
4711    csp->content_type |= CT_DECLARED|CT_GIF;
4712
4713    csp->config->socket_timeout = 0;
4714
4715    cgi_init_error_messages();
4716
4717    handle_established_connection(csp);
4718    freez(csp->receive_buffer);
4719
4720    return 0;
4721 }
4722 #endif
4723
4724
4725 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
4726 /*********************************************************************
4727  *
4728  * Function    :  prepare_csp_for_next_request
4729  *
4730  * Description :  Put the csp in a mostly vergin state.
4731  *
4732  * Parameters  :
4733  *          1  :  csp = Current client state (buffers, headers, etc...)
4734  *
4735  * Returns     :  N/A
4736  *
4737  *********************************************************************/
4738 static void prepare_csp_for_next_request(struct client_state *csp)
4739 {
4740    csp->content_type = 0;
4741    csp->content_length = 0;
4742    csp->expected_content_length = 0;
4743    csp->expected_client_content_length = 0;
4744    list_remove_all(csp->headers);
4745    clear_iob(csp->iob);
4746    freez(csp->error_message);
4747    free_http_request(csp->http);
4748    destroy_list(csp->headers);
4749 #ifdef FEATURE_HTTPS_INSPECTION
4750    destroy_list(csp->https_headers);
4751 #endif
4752    destroy_list(csp->tags);
4753 #ifdef FEATURE_CLIENT_TAGS
4754    destroy_list(csp->client_tags);
4755    freez(csp->client_address);
4756 #endif
4757    free_current_action(csp->action);
4758    if (NULL != csp->fwd)
4759    {
4760       unload_forward_spec(csp->fwd);
4761       csp->fwd = NULL;
4762    }
4763    /* XXX: Store per-connection flags someplace else. */
4764    csp->flags = (CSP_FLAG_ACTIVE | CSP_FLAG_REUSED_CLIENT_CONNECTION);
4765 #ifdef FEATURE_TOGGLE
4766    if (global_toggle_state)
4767 #endif /* def FEATURE_TOGGLE */
4768    {
4769       csp->flags |= CSP_FLAG_TOGGLED_ON;
4770    }
4771
4772    if (csp->client_iob->eod > csp->client_iob->cur)
4773    {
4774       long bytes_to_shift = csp->client_iob->cur - csp->client_iob->buf;
4775       size_t data_length  = (size_t)(csp->client_iob->eod - csp->client_iob->cur);
4776
4777       assert(bytes_to_shift > 0);
4778       assert(data_length > 0);
4779
4780       log_error(LOG_LEVEL_CONNECT, "Shifting %lu pipelined bytes by %ld bytes",
4781          data_length, bytes_to_shift);
4782       memmove(csp->client_iob->buf, csp->client_iob->cur, data_length);
4783       csp->client_iob->cur = csp->client_iob->buf;
4784       assert(csp->client_iob->eod == csp->client_iob->buf + bytes_to_shift + data_length);
4785       csp->client_iob->eod = csp->client_iob->buf + data_length;
4786       memset(csp->client_iob->eod, '\0', (size_t)bytes_to_shift);
4787
4788       csp->flags |= CSP_FLAG_PIPELINED_REQUEST_WAITING;
4789    }
4790    else
4791    {
4792       /*
4793        * We mainly care about resetting client_iob->cur so we don't
4794        * waste buffer space at the beginning and don't mess up the
4795        * request restoration done by cgi_show_request().
4796        *
4797        * Freeing the buffer itself isn't technically necessary,
4798        * but makes debugging more convenient.
4799        */
4800       clear_iob(csp->client_iob);
4801    }
4802 }
4803 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
4804
4805
4806 /*********************************************************************
4807  *
4808  * Function    :  serve
4809  *
4810  * Description :  This is little more than chat.  We only "serve" to
4811  *                to close (or remember) any socket that chat may have
4812  *                opened.
4813  *
4814  * Parameters  :
4815  *          1  :  csp = Current client state (buffers, headers, etc...)
4816  *
4817  * Returns     :  N/A
4818  *
4819  *********************************************************************/
4820 static void serve(struct client_state *csp)
4821 {
4822    int config_file_change_detected = 0; /* Only used for debugging */
4823 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
4824 #ifdef FEATURE_CONNECTION_SHARING
4825    static int monitor_thread_running = 0;
4826 #endif /* def FEATURE_CONNECTION_SHARING */
4827    int continue_chatting = 0;
4828
4829    log_error(LOG_LEVEL_CONNECT, "Accepted connection from %s on socket %d",
4830       csp->ip_addr_str, csp->cfd);
4831
4832    do
4833    {
4834       unsigned int latency;
4835
4836 #ifdef FEATURE_HTTPS_INSPECTION
4837       if (continue_chatting && client_use_ssl(csp))
4838       {
4839          continue_https_chat(csp);
4840       }
4841       else
4842 #endif
4843       {
4844          chat(csp);
4845       }
4846
4847       /*
4848        * If the request has been crunched,
4849        * the calculated latency is zero.
4850        */
4851       latency = (unsigned)(csp->server_connection.response_received -
4852          csp->server_connection.request_sent) / 2;
4853
4854       if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
4855          && (csp->flags & CSP_FLAG_CRUNCHED)
4856          && (csp->expected_client_content_length != 0))
4857       {
4858          csp->flags |= CSP_FLAG_SERVER_SOCKET_TAINTED;
4859          log_error(LOG_LEVEL_CONNECT,
4860             "Tainting client socket %d due to unread data.", csp->cfd);
4861       }
4862
4863       continue_chatting = (csp->config->feature_flags
4864          & RUNTIME_FEATURE_CONNECTION_KEEP_ALIVE)
4865          && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
4866          && (csp->cfd != JB_INVALID_SOCKET)
4867          && (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
4868          && ((csp->flags & CSP_FLAG_SERVER_CONTENT_LENGTH_SET)
4869             || (csp->flags & CSP_FLAG_CHUNKED));
4870
4871       if (!(csp->flags & CSP_FLAG_CRUNCHED)
4872          && (csp->server_connection.sfd != JB_INVALID_SOCKET))
4873       {
4874          if (!(csp->flags & CSP_FLAG_SERVER_KEEP_ALIVE_TIMEOUT_SET))
4875          {
4876             csp->server_connection.keep_alive_timeout = csp->config->default_server_timeout;
4877          }
4878          if (!(csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE)
4879             || (csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED)
4880             || !socket_is_still_alive(csp->server_connection.sfd)
4881             || !(latency < csp->server_connection.keep_alive_timeout))
4882          {
4883             log_error(LOG_LEVEL_CONNECT,
4884                "Closing server socket %d connected to %s. "
4885                "Keep-alive: %u. Tainted: %u. Socket alive: %u. Timeout: %u.",
4886                csp->server_connection.sfd, (csp->server_connection.host != NULL) ?
4887                csp->server_connection.host : csp->http->host,
4888                0 != (csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE),
4889                0 != (csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED),
4890                socket_is_still_alive(csp->server_connection.sfd),
4891                csp->server_connection.keep_alive_timeout);
4892 #ifdef FEATURE_CONNECTION_SHARING
4893             if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
4894             {
4895                forget_connection(csp->server_connection.sfd);
4896             }
4897 #endif /* def FEATURE_CONNECTION_SHARING */
4898 #ifdef FEATURE_HTTPS_INSPECTION
4899             close_server_ssl_connection(csp);
4900 #endif
4901             close_socket(csp->server_connection.sfd);
4902             mark_connection_closed(&csp->server_connection);
4903 #ifdef FEATURE_HTTPS_INSPECTION
4904             if (continue_chatting && client_use_ssl(csp))
4905             {
4906                /*
4907                 * Close the client socket as well as Privoxy currently
4908                 * can't establish a new server connection when the client
4909                 * socket is reused and would drop the connection in
4910                 * continue_https_chat() anyway.
4911                 */
4912                continue_chatting = 0;
4913                csp->flags &= ~CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE;
4914                log_error(LOG_LEVEL_CONNECT,
4915                   "Client socket %d is no longer usable. "
4916                   "The server socket has been closed.", csp->cfd);
4917             }
4918 #endif
4919          }
4920       }
4921
4922       if (continue_chatting && any_loaded_file_changed(csp))
4923       {
4924          continue_chatting = 0;
4925          config_file_change_detected = 1;
4926       }
4927 #ifdef FEATURE_HTTPS_INSPECTION
4928       if (continue_chatting && client_use_ssl(csp) &&
4929          csp->ssl_with_client_is_opened == 0)
4930       {
4931          continue_chatting = 0;
4932          log_error(LOG_LEVEL_CONNECT, "Client socket %d is no longer usable. "
4933             "The TLS session has been terminated.", csp->cfd);
4934       }
4935 #endif
4936
4937       if (continue_chatting)
4938       {
4939          if (((csp->flags & CSP_FLAG_PIPELINED_REQUEST_WAITING) != 0)
4940             && socket_is_still_alive(csp->cfd))
4941          {
4942             log_error(LOG_LEVEL_CONNECT, "Client request %d has been "
4943                "pipelined on socket %d and the socket is still alive.",
4944                csp->requests_received_total+1, csp->cfd);
4945             prepare_csp_for_next_request(csp);
4946             continue;
4947          }
4948
4949          if (0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE))
4950          {
4951             if (csp->server_connection.sfd != JB_INVALID_SOCKET)
4952             {
4953                log_error(LOG_LEVEL_CONNECT,
4954                   "Waiting for the next client request on socket %d. "
4955                   "Keeping the server socket %d to %s open.",
4956                   csp->cfd, csp->server_connection.sfd, csp->server_connection.host);
4957             }
4958             else
4959             {
4960                log_error(LOG_LEVEL_CONNECT,
4961                   "Waiting for the next client request on socket %d. "
4962                   "No server socket to keep open.", csp->cfd);
4963             }
4964          }
4965
4966          if ((csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE)
4967             && data_is_available(csp->cfd, (int)csp->config->keep_alive_timeout)
4968             && socket_is_still_alive(csp->cfd))
4969          {
4970             log_error(LOG_LEVEL_CONNECT,
4971                "Data arrived in time on client socket %d. Requests so far: %u",
4972                csp->cfd, csp->requests_received_total);
4973             prepare_csp_for_next_request(csp);
4974          }
4975          else
4976          {
4977 #ifdef FEATURE_CONNECTION_SHARING
4978             if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
4979                && (csp->server_connection.sfd != JB_INVALID_SOCKET)
4980                && (socket_is_still_alive(csp->server_connection.sfd))
4981 #ifdef FEATURE_HTTPS_INSPECTION
4982                && !server_use_ssl(csp)
4983 #endif
4984                 )
4985             {
4986                time_t time_open = time(NULL) - csp->server_connection.timestamp;
4987
4988                if (csp->server_connection.keep_alive_timeout < time_open - (time_t)latency)
4989                {
4990                   break;
4991                }
4992
4993                remember_connection(&csp->server_connection);
4994                csp->server_connection.sfd = JB_INVALID_SOCKET;
4995                drain_and_close_socket(csp->cfd);
4996                csp->cfd = JB_INVALID_SOCKET;
4997                privoxy_mutex_lock(&connection_reuse_mutex);
4998                if (!monitor_thread_running)
4999                {
5000                   monitor_thread_running = 1;
5001                   privoxy_mutex_unlock(&connection_reuse_mutex);
5002                   wait_for_alive_connections();
5003                   privoxy_mutex_lock(&connection_reuse_mutex);
5004                   monitor_thread_running = 0;
5005                }
5006                privoxy_mutex_unlock(&connection_reuse_mutex);
5007             }
5008 #endif /* def FEATURE_CONNECTION_SHARING */
5009             break;
5010          }
5011       }
5012       else if (csp->server_connection.sfd != JB_INVALID_SOCKET)
5013       {
5014          log_error(LOG_LEVEL_CONNECT,
5015             "Closing server socket %d connected to %s. Keep-alive: %u. "
5016             "Tainted: %u. Socket alive: %u. Timeout: %u. "
5017             "Configuration file change detected: %u",
5018             csp->server_connection.sfd, csp->server_connection.host,
5019             0 != (csp->flags & CSP_FLAG_SERVER_CONNECTION_KEEP_ALIVE),
5020             0 != (csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED),
5021             socket_is_still_alive(csp->server_connection.sfd),
5022             csp->server_connection.keep_alive_timeout,
5023             config_file_change_detected);
5024       }
5025    } while (continue_chatting);
5026
5027 #else
5028    chat(csp);
5029 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
5030
5031    if (csp->cfd != JB_INVALID_SOCKET)
5032    {
5033       log_error(LOG_LEVEL_CONNECT, "Closing client socket %d. "
5034          "Keep-alive: %u. Socket alive: %u. Data available: %u. "
5035          "Configuration file change detected: %u. Requests received: %u.",
5036          csp->cfd, 0 != (csp->flags & CSP_FLAG_CLIENT_CONNECTION_KEEP_ALIVE),
5037          socket_is_still_alive(csp->cfd), data_is_available(csp->cfd, 0),
5038          config_file_change_detected, csp->requests_received_total);
5039 #ifdef FEATURE_HTTPS_INSPECTION
5040       close_client_ssl_connection(csp);
5041 #endif
5042       drain_and_close_socket(csp->cfd);
5043    }
5044
5045    if (csp->server_connection.sfd != JB_INVALID_SOCKET)
5046    {
5047 #ifdef FEATURE_CONNECTION_SHARING
5048       if (csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
5049       {
5050          forget_connection(csp->server_connection.sfd);
5051       }
5052 #endif /* def FEATURE_CONNECTION_SHARING */
5053
5054 #ifdef FEATURE_HTTPS_INSPECTION
5055       close_server_ssl_connection(csp);
5056 #endif /* def FEATURE_HTTPS_INSPECTION */
5057
5058       close_socket(csp->server_connection.sfd);
5059    }
5060
5061 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
5062    mark_connection_closed(&csp->server_connection);
5063 #endif
5064
5065    free_csp_resources(csp);
5066
5067    csp->flags &= ~CSP_FLAG_ACTIVE;
5068
5069 }
5070
5071
5072 #ifdef __BEOS__
5073 /*********************************************************************
5074  *
5075  * Function    :  server_thread
5076  *
5077  * Description :  We only exist to call `serve' in a threaded environment.
5078  *
5079  * Parameters  :
5080  *          1  :  data = Current client state (buffers, headers, etc...)
5081  *
5082  * Returns     :  Always 0.
5083  *
5084  *********************************************************************/
5085 static int32 server_thread(void *data)
5086 {
5087    serve((struct client_state *) data);
5088    return 0;
5089
5090 }
5091 #endif
5092
5093
5094 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
5095 /*********************************************************************
5096  *
5097  * Function    :  usage
5098  *
5099  * Description :  Print usage info & exit.
5100  *
5101  * Parameters  :  Pointer to argv[0] for identifying ourselves
5102  *
5103  * Returns     :  No. ,-)
5104  *
5105  *********************************************************************/
5106 static void usage(const char *name)
5107 {
5108    printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n"
5109           "Usage: %s [--config-test] "
5110 #if defined(unix)
5111           "[--chroot] "
5112 #endif /* defined(unix) */
5113           "[--help] "
5114 #if defined(unix)
5115           "[--no-daemon] [--pidfile pidfile] [--pre-chroot-nslookup hostname] [--user user[.group]] "
5116 #endif /* defined(unix) */
5117          "[--version] [configfile]\n",
5118           name);
5119
5120 #ifdef FUZZ
5121    show_fuzz_usage(name);
5122 #endif
5123
5124    printf("Aborting\n");
5125
5126    exit(2);
5127
5128 }
5129 #endif /* #if !defined(_WIN32) || defined(_WIN_CONSOLE) */
5130
5131
5132 #ifdef MUTEX_LOCKS_AVAILABLE
5133 /*********************************************************************
5134  *
5135  * Function    :  privoxy_mutex_lock
5136  *
5137  * Description :  Locks a mutex.
5138  *
5139  * Parameters  :
5140  *          1  :  mutex = The mutex to lock.
5141  *
5142  * Returns     :  Void. May exit in case of errors.
5143  *
5144  *********************************************************************/
5145 void privoxy_mutex_lock(privoxy_mutex_t *mutex)
5146 {
5147 #ifdef FEATURE_PTHREAD
5148    int err = pthread_mutex_lock(mutex);
5149    if (err)
5150    {
5151       if (mutex != &log_mutex)
5152       {
5153          log_error(LOG_LEVEL_FATAL,
5154             "Mutex locking failed: %s.\n", strerror(err));
5155       }
5156       exit(1);
5157    }
5158 #else
5159    EnterCriticalSection(mutex);
5160 #endif /* def FEATURE_PTHREAD */
5161 }
5162
5163
5164 /*********************************************************************
5165  *
5166  * Function    :  privoxy_mutex_unlock
5167  *
5168  * Description :  Unlocks a mutex.
5169  *
5170  * Parameters  :
5171  *          1  :  mutex = The mutex to unlock.
5172  *
5173  * Returns     :  Void. May exit in case of errors.
5174  *
5175  *********************************************************************/
5176 void privoxy_mutex_unlock(privoxy_mutex_t *mutex)
5177 {
5178 #ifdef FEATURE_PTHREAD
5179    int err = pthread_mutex_unlock(mutex);
5180    if (err)
5181    {
5182       if (mutex != &log_mutex)
5183       {
5184          log_error(LOG_LEVEL_FATAL,
5185             "Mutex unlocking failed: %s.\n", strerror(err));
5186       }
5187       exit(1);
5188    }
5189 #else
5190    LeaveCriticalSection(mutex);
5191 #endif /* def FEATURE_PTHREAD */
5192 }
5193
5194
5195 /*********************************************************************
5196  *
5197  * Function    :  privoxy_mutex_init
5198  *
5199  * Description :  Prepares a mutex.
5200  *
5201  * Parameters  :
5202  *          1  :  mutex = The mutex to initialize.
5203  *
5204  * Returns     :  Void. May exit in case of errors.
5205  *
5206  *********************************************************************/
5207 static void privoxy_mutex_init(privoxy_mutex_t *mutex)
5208 {
5209 #ifdef FEATURE_PTHREAD
5210    int err = pthread_mutex_init(mutex, 0);
5211    if (err)
5212    {
5213       printf("Fatal error. Mutex initialization failed: %s.\n",
5214          strerror(err));
5215       exit(1);
5216    }
5217 #else
5218    InitializeCriticalSection(mutex);
5219 #endif /* def FEATURE_PTHREAD */
5220 }
5221 #endif /* def MUTEX_LOCKS_AVAILABLE */
5222
5223 /*********************************************************************
5224  *
5225  * Function    :  initialize_mutexes
5226  *
5227  * Description :  Prepares mutexes if mutex support is available.
5228  *
5229  * Parameters  :  None
5230  *
5231  * Returns     :  Void, exits in case of errors.
5232  *
5233  *********************************************************************/
5234 static void initialize_mutexes(void)
5235 {
5236 #ifdef MUTEX_LOCKS_AVAILABLE
5237    /*
5238     * Prepare global mutex semaphores
5239     */
5240
5241 #ifdef FEATURE_HTTPS_INSPECTION
5242    privoxy_mutex_init(&certificate_mutex);
5243    privoxy_mutex_init(&ssl_init_mutex);
5244 #endif
5245
5246    privoxy_mutex_init(&log_mutex);
5247    privoxy_mutex_init(&log_init_mutex);
5248    privoxy_mutex_init(&connection_reuse_mutex);
5249 #ifdef FEATURE_EXTERNAL_FILTERS
5250    privoxy_mutex_init(&external_filter_mutex);
5251 #endif
5252 #ifdef FEATURE_CLIENT_TAGS
5253    privoxy_mutex_init(&client_tags_mutex);
5254 #endif
5255 #ifdef FEATURE_EXTENDED_STATISTICS
5256    privoxy_mutex_init(&filter_statistics_mutex);
5257    privoxy_mutex_init(&block_statistics_mutex);
5258 #endif
5259
5260    /*
5261     * XXX: The assumptions below are a bit naive
5262     * and can cause locks that aren't necessary.
5263     *
5264     * For example older FreeBSD versions (< 6.x?)
5265     * have no gethostbyname_r, but gethostbyname is
5266     * thread safe.
5267     */
5268 #if !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R)
5269    privoxy_mutex_init(&resolver_mutex);
5270 #endif /* !defined(HAVE_GETHOSTBYADDR_R) || !defined(HAVE_GETHOSTBYNAME_R) */
5271    /*
5272     * XXX: should we use a single mutex for
5273     * localtime() and gmtime() as well?
5274     */
5275 #ifndef HAVE_GMTIME_R
5276    privoxy_mutex_init(&gmtime_mutex);
5277 #endif /* ndef HAVE_GMTIME_R */
5278
5279 #ifndef HAVE_LOCALTIME_R
5280    privoxy_mutex_init(&localtime_mutex);
5281 #endif /* ndef HAVE_GMTIME_R */
5282
5283 #if !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM)
5284    privoxy_mutex_init(&rand_mutex);
5285 #endif /* !defined(HAVE_ARC4RANDOM) && !defined(HAVE_RANDOM) */
5286
5287 #endif /* def MUTEX_LOCKS_AVAILABLE */
5288 }
5289
5290 /*********************************************************************
5291  *
5292  * Function    :  main
5293  *
5294  * Description :  Load the config file and start the listen loop.
5295  *                This function is a lot more *sane* with the `load_config'
5296  *                and `listen_loop' functions; although it stills does
5297  *                a *little* too much for my taste.
5298  *
5299  * Parameters  :
5300  *          1  :  argc = Number of parameters (including $0).
5301  *          2  :  argv = Array of (char *)'s to the parameters.
5302  *
5303  * Returns     :  1 if : can't open config file, unrecognized directive,
5304  *                stats requested in multi-thread mode, can't open the
5305  *                log file, can't open the jar file, listen port is invalid,
5306  *                any load fails, and can't bind port.
5307  *
5308  *                Else main never returns, the process must be signaled
5309  *                to terminate execution.  Or, on Windows, use the
5310  *                "File", "Exit" menu option.
5311  *
5312  *********************************************************************/
5313 #ifdef __MINGW32__
5314 int real_main(int argc, char **argv)
5315 #else
5316 int main(int argc, char **argv)
5317 #endif
5318 {
5319    int argc_pos = 0;
5320    int do_config_test = 0;
5321 #ifndef HAVE_ARC4RANDOM
5322    unsigned int random_seed;
5323 #endif
5324 #ifdef unix
5325    struct passwd *pw = NULL;
5326    struct group *grp = NULL;
5327    int do_chroot = 0;
5328    char *pre_chroot_nslookup_to_load_resolver = NULL;
5329 #endif
5330 #ifdef FUZZ
5331    char *fuzz_input_type = NULL;
5332    char *fuzz_input_file = NULL;
5333 #endif
5334
5335    Argc = argc;
5336    Argv = argv;
5337
5338    configfile =
5339 #if !defined(_WIN32)
5340    "config"
5341 #else
5342    "config.txt"
5343 #endif
5344       ;
5345
5346    /* Prepare mutexes if supported and necessary. */
5347    initialize_mutexes();
5348
5349    /* Enable logging until further notice. */
5350    init_log_module();
5351
5352    /*
5353     * Parse the command line arguments
5354     *
5355     * XXX: simply printing usage information in case of
5356     * invalid arguments isn't particularly user friendly.
5357     */
5358    while (++argc_pos < argc)
5359    {
5360 #ifdef _WIN32
5361       /* Check to see if the service must be installed or uninstalled */
5362       if (strncmp(argv[argc_pos], "--install", 9) == 0)
5363       {
5364          const char *pName = argv[argc_pos] + 9;
5365          if (*pName == ':')
5366             pName++;
5367          exit((install_service(pName)) ? 0 : 1);
5368       }
5369       else if (strncmp(argv[argc_pos], "--uninstall", 11) == 0)
5370       {
5371          const char *pName = argv[argc_pos] + 11;
5372          if (*pName == ':')
5373             pName++;
5374          exit((uninstall_service(pName)) ? 0 : 1);
5375       }
5376       else if (strcmp(argv[argc_pos], "--service") == 0)
5377       {
5378          bRunAsService = TRUE;
5379          w32_set_service_cwd();
5380          atexit(w32_service_exit_notify);
5381       }
5382       else
5383 #endif /* defined(_WIN32) */
5384
5385
5386 #if !defined(_WIN32) || defined(_WIN_CONSOLE)
5387
5388       if (strcmp(argv[argc_pos], "--help") == 0)
5389       {
5390          usage(argv[0]);
5391       }
5392
5393       else if (strcmp(argv[argc_pos], "--version") == 0)
5394       {
5395          printf("Privoxy version " VERSION " (" HOME_PAGE_URL ")\n");
5396          exit(0);
5397       }
5398
5399 #if defined(unix)
5400
5401       else if (strcmp(argv[argc_pos], "--no-daemon") == 0)
5402       {
5403          set_debug_level(LOG_LEVEL_FATAL | LOG_LEVEL_ERROR | LOG_LEVEL_INFO);
5404          daemon_mode = 0;
5405       }
5406
5407       else if (strcmp(argv[argc_pos], "--pidfile") == 0)
5408       {
5409          if (++argc_pos == argc) usage(argv[0]);
5410          pidfile = strdup_or_die(argv[argc_pos]);
5411       }
5412
5413       else if (strcmp(argv[argc_pos], "--user") == 0)
5414       {
5415          char *user_arg;
5416          char *group_name;
5417
5418          if (++argc_pos == argc) usage(argv[argc_pos]);
5419
5420          user_arg = strdup_or_die(argv[argc_pos]);
5421          group_name = strchr(user_arg, '.');
5422          if (NULL != group_name)
5423          {
5424             /* Nul-terminate the user name */
5425             *group_name = '\0';
5426
5427             /* Skip the former delimiter to actually reach the group name */
5428             group_name++;
5429
5430             grp = getgrnam(group_name);
5431             if (NULL == grp)
5432             {
5433                log_error(LOG_LEVEL_FATAL, "Group '%s' not found.", group_name);
5434             }
5435          }
5436          pw = getpwnam(user_arg);
5437          if (NULL == pw)
5438          {
5439             log_error(LOG_LEVEL_FATAL, "User '%s' not found.", user_arg);
5440          }
5441
5442          freez(user_arg);
5443       }
5444
5445       else if (strcmp(argv[argc_pos], "--pre-chroot-nslookup") == 0)
5446       {
5447          if (++argc_pos == argc) usage(argv[0]);
5448          pre_chroot_nslookup_to_load_resolver = strdup_or_die(argv[argc_pos]);
5449       }
5450
5451       else if (strcmp(argv[argc_pos], "--chroot") == 0)
5452       {
5453          do_chroot = 1;
5454       }
5455 #endif /* defined(unix) */
5456
5457       else if (strcmp(argv[argc_pos], "--config-test") == 0)
5458       {
5459          do_config_test = 1;
5460       }
5461 #ifdef FUZZ
5462       else if (strcmp(argv[argc_pos], "--fuzz") == 0)
5463       {
5464          argc_pos++;
5465          if (argc < argc_pos + 2) usage(argv[0]);
5466          fuzz_input_type = argv[argc_pos];
5467          argc_pos++;
5468          fuzz_input_file = argv[argc_pos];
5469       }
5470       else if (strcmp(argv[argc_pos], "--stfu") == 0)
5471       {
5472          set_debug_level(LOG_LEVEL_STFU);
5473       }
5474 #endif
5475       else if (argc_pos + 1 != argc)
5476       {
5477          /*
5478           * This is neither the last command line
5479           * option, nor was it recognized before,
5480           * therefore it must be invalid.
5481           */
5482          usage(argv[0]);
5483       }
5484       else
5485
5486 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
5487       {
5488          configfile = argv[argc_pos];
5489       }
5490
5491    } /* -END- while (more arguments) */
5492
5493    show_version(Argv[0]);
5494
5495 #if defined(unix)
5496    if (*configfile != '/')
5497    {
5498       char cwd[BUFFER_SIZE];
5499       char *abs_file;
5500       size_t abs_file_size;
5501
5502       /* make config-filename absolute here */
5503       if (NULL == getcwd(cwd, sizeof(cwd)))
5504       {
5505          perror("failed to get current working directory");
5506          exit(1);
5507       }
5508
5509       basedir = strdup_or_die(cwd);
5510       /* XXX: why + 5? */
5511       abs_file_size = strlen(cwd) + strlen(configfile) + 5;
5512       abs_file = malloc_or_die(abs_file_size);
5513       strlcpy(abs_file, basedir, abs_file_size);
5514       strlcat(abs_file, "/", abs_file_size);
5515       strlcat(abs_file, configfile, abs_file_size);
5516       configfile = abs_file;
5517    }
5518 #endif /* defined unix */
5519
5520
5521    files->next = NULL;
5522    clients->next = NULL;
5523
5524    /* XXX: factor out initialising after the next stable release. */
5525 #ifdef _WIN32
5526    InitWin32();
5527 #endif
5528
5529 #ifndef HAVE_ARC4RANDOM
5530    random_seed = (unsigned int)time(NULL);
5531 #ifdef HAVE_RANDOM
5532    srandom(random_seed);
5533 #else
5534    srand(random_seed);
5535 #endif /* ifdef HAVE_RANDOM */
5536 #endif /* ifndef HAVE_ARC4RANDOM */
5537
5538    /*
5539     * Unix signal handling
5540     *
5541     * Catch the abort, interrupt and terminate signals for a graceful exit
5542     * Catch the hangup signal so the errlog can be reopened.
5543     *
5544     * Ignore the broken pipe signal as connection failures
5545     * are handled when and where they occur without relying
5546     * on a signal.
5547     */
5548 #if !defined(_WIN32)
5549 {
5550    int idx;
5551    const int catched_signals[] = { SIGTERM, SIGINT, SIGHUP };
5552
5553    for (idx = 0; idx < SZ(catched_signals); idx++)
5554    {
5555 #ifdef sun /* FIXME: Is it safe to check for HAVE_SIGSET instead? */
5556       if (sigset(catched_signals[idx], sig_handler) == SIG_ERR)
5557 #else
5558       if (signal(catched_signals[idx], sig_handler) == SIG_ERR)
5559 #endif /* ifdef sun */
5560       {
5561          log_error(LOG_LEVEL_FATAL, "Can't set signal-handler for signal %d: %E", catched_signals[idx]);
5562       }
5563    }
5564
5565    if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
5566    {
5567       log_error(LOG_LEVEL_FATAL, "Can't set ignore-handler for SIGPIPE: %E");
5568    }
5569
5570 }
5571 #else /* ifdef _WIN32 */
5572 # ifdef _WIN_CONSOLE
5573    /*
5574     * We *are* in a windows console app.
5575     * Print a verbose messages about FAQ's and such
5576     */
5577    printf("%s", win32_blurb);
5578 # endif /* def _WIN_CONSOLE */
5579 #endif /* def _WIN32 */
5580
5581 #ifdef FUZZ
5582    if (fuzz_input_type != NULL)
5583    {
5584       exit(process_fuzzed_input(fuzz_input_type, fuzz_input_file));
5585    }
5586    log_error(LOG_LEVEL_FATAL,
5587       "When compiled with fuzzing support, Privoxy should only be used for fuzzing. "
5588       "Various data structures are static which is unsafe when using threads.");
5589 #endif
5590
5591    if (do_config_test)
5592    {
5593       exit(NULL == load_config());
5594    }
5595
5596    /* Initialize the CGI subsystem */
5597    cgi_init_error_messages();
5598
5599    /*
5600     * If running on unix and without the --no-daemon
5601     * option, become a daemon. I.e. fork, detach
5602     * from tty and get process group leadership
5603     */
5604 #if defined(unix)
5605 {
5606    if (daemon_mode)
5607    {
5608       int fd;
5609       pid_t pid = fork();
5610
5611       if (pid < 0) /* error */
5612       {
5613          perror("fork");
5614          exit(3);
5615       }
5616       else if (pid != 0) /* parent */
5617       {
5618          int status;
5619          pid_t wpid;
5620          /*
5621           * must check for errors
5622           * child died due to missing files aso
5623           */
5624          sleep(1);
5625          wpid = waitpid(pid, &status, WNOHANG);
5626          if (wpid != 0)
5627          {
5628             exit(1);
5629          }
5630          exit(0);
5631       }
5632       /* child */
5633
5634       setsid();
5635
5636       /*
5637        * stderr (fd 2) will be closed later on,
5638        * when the config file has been parsed.
5639        */
5640       close(0);
5641       close(1);
5642
5643       /*
5644        * Reserve fd 0 and 1 to prevent abort() and friends
5645        * from sending stuff to the clients or servers.
5646        */
5647       fd = open("/dev/null", O_RDONLY);
5648       if (fd == -1)
5649       {
5650          log_error(LOG_LEVEL_FATAL, "Failed to open /dev/null: %E");
5651       }
5652       else if (fd != 0)
5653       {
5654          if (dup2(fd, 0) == -1)
5655          {
5656             log_error(LOG_LEVEL_FATAL, "Failed to reserve fd 0: %E");
5657          }
5658          close(fd);
5659       }
5660       fd = open("/dev/null", O_WRONLY);
5661       if (fd == -1)
5662       {
5663          log_error(LOG_LEVEL_FATAL, "Failed to open /dev/null: %E");
5664       }
5665       else if (fd != 1)
5666       {
5667          if (dup2(fd, 1) == -1)
5668          {
5669             log_error(LOG_LEVEL_FATAL, "Failed to reserve fd 1: %E");
5670          }
5671          close(fd);
5672       }
5673
5674 #ifdef FEATURE_EXTERNAL_FILTERS
5675       for (fd = 0; fd < 3; fd++)
5676       {
5677          mark_socket_for_close_on_execute(fd);
5678       }
5679 #endif
5680
5681       if (chdir("/") != 0)
5682       {
5683          log_error(LOG_LEVEL_FATAL, "Failed to cd into '/': %E");
5684       }
5685
5686    } /* -END- if (daemon_mode) */
5687
5688    /*
5689     * As soon as we have written the PID file, we can switch
5690     * to the user and group ID indicated by the --user option
5691     */
5692    if (pidfile != NULL)
5693    {
5694       write_pid_file(pidfile);
5695    }
5696    if (NULL != pw)
5697    {
5698       if (setgid((NULL != grp) ? grp->gr_gid : pw->pw_gid))
5699       {
5700          log_error(LOG_LEVEL_FATAL, "Cannot setgid(): Insufficient permissions.");
5701       }
5702       if (NULL != grp)
5703       {
5704          if (setgroups(1, &grp->gr_gid))
5705          {
5706             log_error(LOG_LEVEL_FATAL, "setgroups() failed: %E");
5707          }
5708       }
5709       else if (initgroups(pw->pw_name, pw->pw_gid))
5710       {
5711          log_error(LOG_LEVEL_FATAL, "initgroups() failed: %E");
5712       }
5713       if (do_chroot)
5714       {
5715          if (!pw->pw_dir)
5716          {
5717             log_error(LOG_LEVEL_FATAL, "Home directory for %s undefined", pw->pw_name);
5718          }
5719          /* Read the time zone file from /etc before doing chroot. */
5720          tzset();
5721          if (NULL != pre_chroot_nslookup_to_load_resolver
5722              && '\0' != pre_chroot_nslookup_to_load_resolver[0])
5723          {
5724             /* Initialize resolver library. */
5725             (void) resolve_hostname_to_ip(pre_chroot_nslookup_to_load_resolver);
5726          }
5727          if (chroot(pw->pw_dir) < 0)
5728          {
5729             log_error(LOG_LEVEL_FATAL, "Cannot chroot to %s", pw->pw_dir);
5730          }
5731          if (chdir ("/"))
5732          {
5733             log_error(LOG_LEVEL_FATAL, "Cannot chdir /");
5734          }
5735       }
5736       if (setuid(pw->pw_uid))
5737       {
5738          log_error(LOG_LEVEL_FATAL, "Cannot setuid(): Insufficient permissions.");
5739       }
5740       if (do_chroot)
5741       {
5742          char putenv_dummy[64];
5743
5744          strlcpy(putenv_dummy, "HOME=/", sizeof(putenv_dummy));
5745          if (putenv(putenv_dummy) != 0)
5746          {
5747             log_error(LOG_LEVEL_FATAL, "Cannot putenv(): HOME");
5748          }
5749
5750          snprintf(putenv_dummy, sizeof(putenv_dummy), "USER=%s", pw->pw_name);
5751          if (putenv(putenv_dummy) != 0)
5752          {
5753             log_error(LOG_LEVEL_FATAL, "Cannot putenv(): USER");
5754          }
5755       }
5756    }
5757    else if (do_chroot)
5758    {
5759       log_error(LOG_LEVEL_FATAL, "Cannot chroot without --user argument.");
5760    }
5761 }
5762 #endif /* defined unix */
5763
5764 #ifdef _WIN32
5765    /* This will be FALSE unless the command line specified --service
5766     */
5767    if (bRunAsService)
5768    {
5769       /* Yup, so now we must attempt to establish a connection
5770        * with the service dispatcher. This will only work if this
5771        * process was launched by the service control manager to
5772        * actually run as a service. If this isn't the case, i've
5773        * known it take around 30 seconds or so for the call to return.
5774        */
5775
5776       /* The StartServiceCtrlDispatcher won't return until the service is stopping */
5777       if (w32_start_service_ctrl_dispatcher(w32ServiceDispatchTable))
5778       {
5779          /* Service has run, and at this point is now being stopped, so just return */
5780          return 0;
5781       }
5782
5783 #ifdef _WIN_CONSOLE
5784       printf("Warning: Failed to connect to Service Control Dispatcher\nwhen starting as a service!\n");
5785 #endif
5786       /* An error occurred. Usually it's because --service was wrongly specified
5787        * and we were unable to connect to the Service Control Dispatcher because
5788        * it wasn't expecting us and is therefore not listening.
5789        *
5790        * For now, just continue below to call the listen_loop function.
5791        */
5792    }
5793 #endif /* def _WIN32 */
5794
5795    listen_loop();
5796
5797    /* NOTREACHED */
5798    return(-1);
5799
5800 }
5801
5802
5803 /*********************************************************************
5804  *
5805  * Function    :  bind_port_helper
5806  *
5807  * Description :  Bind the listen port.  Handles logging, and aborts
5808  *                on failure.
5809  *
5810  * Parameters  :
5811  *          1  :  haddr = Host address to bind to. Use NULL to bind to
5812  *                        INADDR_ANY.
5813  *          2  :  hport = Specifies port to bind to.
5814  *          3  :  backlog = Listen backlog.
5815  *
5816  * Returns     :  Port that was opened.
5817  *
5818  *********************************************************************/
5819 static jb_socket bind_port_helper(const char *haddr, int hport, int backlog)
5820 {
5821    int result;
5822    jb_socket bfd;
5823
5824    result = bind_port(haddr, hport, backlog, &bfd);
5825
5826    if (result < 0)
5827    {
5828       const char *bind_address = (NULL != haddr) ? haddr : "INADDR_ANY";
5829       switch(result)
5830       {
5831          case -3:
5832             log_error(LOG_LEVEL_FATAL,
5833                "can't bind to %s:%d: There may be another Privoxy "
5834                "or some other proxy running on port %d",
5835                bind_address, hport, hport);
5836             exit(-1);
5837
5838          case -2:
5839             log_error(LOG_LEVEL_FATAL,
5840                "can't bind to %s:%d: The hostname is not resolvable",
5841                bind_address, hport);
5842             exit(-1);
5843
5844          default:
5845             log_error(LOG_LEVEL_FATAL, "can't bind to %s:%d: %E",
5846                bind_address, hport);
5847             exit(-1);
5848       }
5849
5850       /* shouldn't get here */
5851       return JB_INVALID_SOCKET;
5852    }
5853
5854 #ifndef HAVE_POLL
5855 #ifndef _WIN32
5856    if (bfd >= FD_SETSIZE)
5857    {
5858       log_error(LOG_LEVEL_FATAL,
5859          "Bind socket number too high to use select(): %d >= %d",
5860          bfd, FD_SETSIZE);
5861    }
5862 #endif
5863 #endif
5864
5865    if (haddr == NULL)
5866    {
5867       log_error(LOG_LEVEL_INFO, "Listening on port %d on all IP addresses",
5868          hport);
5869    }
5870    else
5871    {
5872       log_error(LOG_LEVEL_INFO, "Listening on port %d on IP address %s",
5873          hport, haddr);
5874    }
5875
5876    return bfd;
5877 }
5878
5879
5880 /*********************************************************************
5881  *
5882  * Function    :  bind_ports_helper
5883  *
5884  * Description :  Bind the listen ports.  Handles logging, and aborts
5885  *                on failure.
5886  *
5887  * Parameters  :
5888  *          1  :  config = Privoxy configuration.  Specifies ports
5889  *                         to bind to.
5890  *          2  :  sockets = Preallocated array of opened sockets
5891  *                          corresponding to specification in config.
5892  *                          All non-opened sockets will be set to
5893  *                          JB_INVALID_SOCKET.
5894  *
5895  * Returns     :  Nothing. Inspect sockets argument.
5896  *
5897  *********************************************************************/
5898 static void bind_ports_helper(struct configuration_spec * config,
5899                               jb_socket sockets[])
5900 {
5901    int i;
5902
5903    for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
5904    {
5905       if (config->hport[i])
5906       {
5907          sockets[i] = bind_port_helper(config->haddr[i],
5908             config->hport[i], config->listen_backlog);
5909 #if defined(FEATURE_ACCEPT_FILTER) && defined(SO_ACCEPTFILTER)
5910          if (config->enable_accept_filter && sockets[i] != JB_INVALID_SOCKET)
5911          {
5912             struct accept_filter_arg af_options;
5913             bzero(&af_options, sizeof(af_options));
5914             strlcpy(af_options.af_name, "httpready", sizeof(af_options.af_name));
5915             if (setsockopt(sockets[i], SOL_SOCKET, SO_ACCEPTFILTER, &af_options,
5916                   sizeof(af_options)))
5917             {
5918                log_error(LOG_LEVEL_ERROR,
5919                   "Enabling accept filter for socket %d failed: %E", sockets[i]);
5920             }
5921          }
5922 #endif
5923       }
5924       else
5925       {
5926          sockets[i] = JB_INVALID_SOCKET;
5927       }
5928    }
5929    config->need_bind = 0;
5930 }
5931
5932
5933 /*********************************************************************
5934  *
5935  * Function    :  close_ports_helper
5936  *
5937  * Description :  Close listenings ports.
5938  *
5939  * Parameters  :
5940  *          1  :  sockets = Array of opened and non-opened sockets to
5941  *                          close. All sockets will be set to
5942  *                          JB_INVALID_SOCKET.
5943  *
5944  * Returns     :  Nothing.
5945  *
5946  *********************************************************************/
5947 static void close_ports_helper(jb_socket sockets[])
5948 {
5949    int i;
5950
5951    for (i = 0; i < MAX_LISTENING_SOCKETS; i++)
5952    {
5953       if (JB_INVALID_SOCKET != sockets[i])
5954       {
5955          close_socket(sockets[i]);
5956       }
5957       sockets[i] = JB_INVALID_SOCKET;
5958    }
5959 }
5960
5961
5962 #ifdef _WIN32
5963 /* Without this simple workaround we get this compiler warning from _beginthread
5964  *     warning C4028: formal parameter 1 different from declaration
5965  */
5966 void w32_service_listen_loop(void *p)
5967 {
5968    listen_loop();
5969 }
5970 #endif /* def _WIN32 */
5971
5972
5973 /*********************************************************************
5974  *
5975  * Function    :  listen_loop
5976  *
5977  * Description :  bind the listen port and enter a "FOREVER" listening loop.
5978  *
5979  * Parameters  :  N/A
5980  *
5981  * Returns     :  Never.
5982  *
5983  *********************************************************************/
5984 static void listen_loop(void)
5985 {
5986    struct client_states *csp_list = NULL;
5987    struct client_state *csp = NULL;
5988    jb_socket bfds[MAX_LISTENING_SOCKETS];
5989    struct configuration_spec *config;
5990    unsigned int active_threads = 0;
5991 #if defined(FEATURE_PTHREAD)
5992    pthread_attr_t attrs;
5993
5994    pthread_attr_init(&attrs);
5995    pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
5996 #endif
5997
5998    config = load_config();
5999
6000 #ifdef FEATURE_CONNECTION_SHARING
6001    /*
6002     * XXX: Should be relocated once it no
6003     * longer needs to emit log messages.
6004     */
6005    initialize_reusable_connections();
6006 #endif /* def FEATURE_CONNECTION_SHARING */
6007
6008    bind_ports_helper(config, bfds);
6009
6010 #ifdef FEATURE_GRACEFUL_TERMINATION
6011    while (!g_terminate)
6012 #else
6013    for (;;)
6014 #endif
6015    {
6016 #if !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__)
6017       while (waitpid(-1, NULL, WNOHANG) > 0)
6018       {
6019          /* zombie children */
6020       }
6021 #endif /* !defined(FEATURE_PTHREAD) && !defined(_WIN32) && !defined(__BEOS__) */
6022
6023       /*
6024        * Free data that was used by died threads
6025        */
6026       active_threads = sweep();
6027
6028 #if defined(unix)
6029       /*
6030        * Re-open the errlog after HUP signal
6031        */
6032       if (received_hup_signal)
6033       {
6034          if (NULL != config->logfile)
6035          {
6036             init_error_log(Argv[0], config->logfile);
6037          }
6038          received_hup_signal = 0;
6039       }
6040 #endif
6041
6042       csp_list = zalloc_or_die(sizeof(*csp_list));
6043       csp = &csp_list->csp;
6044
6045       log_error(LOG_LEVEL_CONNECT,
6046          "Waiting for the next client connection. Currently active threads: %u",
6047          active_threads);
6048
6049       /*
6050        * This config may be outdated, but for accept_connection()
6051        * it's fresh enough.
6052        */
6053       csp->config = config;
6054
6055       if (!accept_connection(csp, bfds))
6056       {
6057          log_error(LOG_LEVEL_CONNECT, "accept failed: %E");
6058          freez(csp_list);
6059          continue;
6060       }
6061
6062       csp->flags |= CSP_FLAG_ACTIVE;
6063       csp->server_connection.sfd = JB_INVALID_SOCKET;
6064
6065       csp->config = config = load_config();
6066
6067       if (config->need_bind)
6068       {
6069          /*
6070           * Since we were listening to the "old port", we will not see
6071           * a "listen" param change until the next request.  So, at
6072           * least 1 more request must be made for us to find the new
6073           * setting.  I am simply closing the old socket and binding the
6074           * new one.
6075           *
6076           * Which-ever is correct, we will serve 1 more page via the
6077           * old settings.  This should probably be a "show-status"
6078           * request.  This should not be a so common of an operation
6079           * that this will hurt people's feelings.
6080           */
6081
6082          close_ports_helper(bfds);
6083
6084          bind_ports_helper(config, bfds);
6085       }
6086
6087 #ifdef FEATURE_TOGGLE
6088       if (global_toggle_state)
6089 #endif /* def FEATURE_TOGGLE */
6090       {
6091          csp->flags |= CSP_FLAG_TOGGLED_ON;
6092       }
6093
6094       if (run_loader(csp))
6095       {
6096          log_error(LOG_LEVEL_FATAL, "a loader failed - must exit");
6097          /* Never get here - LOG_LEVEL_FATAL causes program exit */
6098       }
6099
6100 #ifdef FEATURE_ACL
6101       if (block_acl(NULL,csp))
6102       {
6103          log_error(LOG_LEVEL_CONNECT,
6104             "Connection from %s on %s (socket %d) dropped due to ACL",
6105             csp->ip_addr_str, csp->listen_addr_str, csp->cfd);
6106          close_socket(csp->cfd);
6107          freez(csp->ip_addr_str);
6108          freez(csp->listen_addr_str);
6109          freez(csp_list);
6110          continue;
6111       }
6112 #endif /* def FEATURE_ACL */
6113
6114       if ((0 != config->max_client_connections)
6115          && (active_threads >= config->max_client_connections))
6116       {
6117          log_error(LOG_LEVEL_ERROR,
6118             "Rejecting connection from %s. Maximum number of connections reached.",
6119             csp->ip_addr_str);
6120          write_socket_delayed(csp->cfd, TOO_MANY_CONNECTIONS_RESPONSE,
6121             strlen(TOO_MANY_CONNECTIONS_RESPONSE), get_write_delay(csp));
6122          close_socket(csp->cfd);
6123          freez(csp->ip_addr_str);
6124          freez(csp->listen_addr_str);
6125          freez(csp_list);
6126          continue;
6127       }
6128
6129       /* add it to the list of clients */
6130       csp_list->next = clients->next;
6131       clients->next = csp_list;
6132
6133       if (config->multi_threaded)
6134       {
6135          int child_id;
6136
6137 /* this is a switch () statement in the C preprocessor - ugh */
6138 #undef SELECTED_ONE_OPTION
6139
6140 /* Use Pthreads in preference to native code */
6141 #if defined(FEATURE_PTHREAD) && !defined(SELECTED_ONE_OPTION)
6142 #define SELECTED_ONE_OPTION
6143          {
6144             pthread_t the_thread;
6145             int ret;
6146
6147             ret = pthread_create(&the_thread, &attrs,
6148                (void * (*)(void *))serve, csp);
6149             child_id = ret ? -1 : 0;
6150          }
6151 #endif
6152
6153 #if defined(_WIN32) && !defined(SELECTED_ONE_OPTION)
6154 #define SELECTED_ONE_OPTION
6155          child_id = _beginthread(
6156             (void (*)(void *))serve,
6157             64 * 1024,
6158             csp);
6159 #endif
6160
6161 #if defined(__BEOS__) && !defined(SELECTED_ONE_OPTION)
6162 #define SELECTED_ONE_OPTION
6163          {
6164             thread_id tid = spawn_thread
6165                (server_thread, "server", B_NORMAL_PRIORITY, csp);
6166
6167             if ((tid >= 0) && (resume_thread(tid) == B_OK))
6168             {
6169                child_id = (int) tid;
6170             }
6171             else
6172             {
6173                child_id = -1;
6174             }
6175          }
6176 #endif
6177
6178 #if !defined(SELECTED_ONE_OPTION)
6179          child_id = fork();
6180
6181          /* This block is only needed when using fork().
6182           * When using threads, the server thread was
6183           * created and run by the call to _beginthread().
6184           */
6185          if (child_id == 0)   /* child */
6186          {
6187             int rc = 0;
6188 #ifdef FEATURE_TOGGLE
6189             int inherited_toggle_state = global_toggle_state;
6190 #endif /* def FEATURE_TOGGLE */
6191
6192             serve(csp);
6193
6194             /*
6195              * If we've been toggled or we've blocked the request, tell Mom
6196              */
6197
6198 #ifdef FEATURE_TOGGLE
6199             if (inherited_toggle_state != global_toggle_state)
6200             {
6201                rc |= RC_FLAG_TOGGLED;
6202             }
6203 #endif /* def FEATURE_TOGGLE */
6204
6205 #ifdef FEATURE_STATISTICS
6206             if (csp->flags & CSP_FLAG_REJECTED)
6207             {
6208                rc |= RC_FLAG_BLOCKED;
6209             }
6210 #endif /* ndef FEATURE_STATISTICS */
6211
6212             _exit(rc);
6213          }
6214          else if (child_id > 0) /* parent */
6215          {
6216             /* in a fork()'d environment, the parent's
6217              * copy of the client socket and the CSP
6218              * are not used.
6219              */
6220             int child_status;
6221 #if !defined(_WIN32) && !defined(__CYGWIN__)
6222
6223             wait(&child_status);
6224
6225             /*
6226              * Evaluate child's return code: If the child has
6227              *  - been toggled, toggle ourselves
6228              *  - blocked its request, bump up the stats counter
6229              */
6230
6231 #ifdef FEATURE_TOGGLE
6232             if (WIFEXITED(child_status) && (WEXITSTATUS(child_status) & RC_FLAG_TOGGLED))
6233             {
6234                global_toggle_state = !global_toggle_state;
6235             }
6236 #endif /* def FEATURE_TOGGLE */
6237
6238 #ifdef FEATURE_STATISTICS
6239             urls_read++;
6240             if (WIFEXITED(child_status) && (WEXITSTATUS(child_status) & RC_FLAG_BLOCKED))
6241             {
6242                urls_rejected++;
6243             }
6244 #endif /* def FEATURE_STATISTICS */
6245
6246 #endif /* !defined(_WIN32) && defined(__CYGWIN__) */
6247             close_socket(csp->cfd);
6248             csp->flags &= ~CSP_FLAG_ACTIVE;
6249          }
6250 #endif
6251
6252 #undef SELECTED_ONE_OPTION
6253 /* end of cpp switch () */
6254
6255          if (child_id < 0)
6256          {
6257             /*
6258              * Spawning the child failed, assume it's because
6259              * there are too many children running already.
6260              * XXX: If you assume ...
6261              */
6262             log_error(LOG_LEVEL_ERROR,
6263                "Unable to take any additional connections: %E. Active threads: %u",
6264                active_threads);
6265             write_socket_delayed(csp->cfd, TOO_MANY_CONNECTIONS_RESPONSE,
6266                strlen(TOO_MANY_CONNECTIONS_RESPONSE), get_write_delay(csp));
6267             close_socket(csp->cfd);
6268             csp->flags &= ~CSP_FLAG_ACTIVE;
6269          }
6270       }
6271       else
6272       {
6273          serve(csp);
6274       }
6275    }
6276
6277 #if defined(FEATURE_PTHREAD)
6278    pthread_attr_destroy(&attrs);
6279 #endif
6280
6281    /* NOTREACHED unless FEATURE_GRACEFUL_TERMINATION is defined */
6282
6283 #ifdef FEATURE_GRACEFUL_TERMINATION
6284
6285    log_error(LOG_LEVEL_INFO, "Graceful termination requested.");
6286
6287    close_ports_helper(bfds);
6288
6289    unload_current_config_file();
6290    unload_current_actions_file();
6291    unload_current_re_filterfile();
6292 #ifdef FEATURE_TRUST
6293    unload_current_trust_file();
6294 #endif
6295
6296    if (config->multi_threaded)
6297    {
6298       int i = 60;
6299       do
6300       {
6301          sleep(1);
6302          sweep();
6303       } while ((clients->next != NULL) && (--i > 0));
6304
6305       if (i <= 0)
6306       {
6307          log_error(LOG_LEVEL_ERROR, "Graceful termination failed "
6308             "- still some live clients after 1 minute wait.");
6309       }
6310    }
6311    sweep();
6312    sweep();
6313
6314 #if defined(unix)
6315    freez(basedir);
6316 #endif
6317
6318 #ifdef FEATURE_HTTPS_INSPECTION
6319    /*
6320     * Only release TLS backed resources if there
6321     * are no active connections left.
6322     */
6323    if (clients->next == NULL)
6324    {
6325       ssl_release();
6326    }
6327 #endif
6328
6329    log_error(LOG_LEVEL_INFO, "Exiting gracefully.");
6330
6331 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
6332    /* Cleanup - remove taskbar icon etc. */
6333    TermLogWindow();
6334 #endif
6335
6336    exit(0);
6337 #endif /* FEATURE_GRACEFUL_TERMINATION */
6338
6339 }
6340
6341
6342 /*
6343   Local Variables:
6344   tab-width: 3
6345   end:
6346 */