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