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