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