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