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