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