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