pcrs_compile_replacement(): Fix a clang warning when building with FUZZ support
[privoxy.git] / gateway.c
1 const char gateway_rcs[] = "$Id: gateway.c,v 1.99 2016/10/25 10:46:56 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/gateway.c,v $
5  *
6  * Purpose     :  Contains functions to connect to a server, possibly
7  *                using a "forwarder" (i.e. HTTP proxy and/or a SOCKS4
8  *                or SOCKS5 proxy).
9  *
10  * Copyright   :  Written by and Copyright (C) 2001-2016 the
11  *                Privoxy team. http://www.privoxy.org/
12  *
13  *                Based on the Internet Junkbuster originally written
14  *                by and Copyright (C) 1997 Anonymous Coders and
15  *                Junkbusters Corporation.  http://www.junkbusters.com
16  *
17  *                This program is free software; you can redistribute it
18  *                and/or modify it under the terms of the GNU General
19  *                Public License as published by the Free Software
20  *                Foundation; either version 2 of the License, or (at
21  *                your option) any later version.
22  *
23  *                This program is distributed in the hope that it will
24  *                be useful, but WITHOUT ANY WARRANTY; without even the
25  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
26  *                PARTICULAR PURPOSE.  See the GNU General Public
27  *                License for more details.
28  *
29  *                The GNU General Public License should be included with
30  *                this file.  If not, you can view it at
31  *                http://www.gnu.org/copyleft/gpl.html
32  *                or write to the Free Software Foundation, Inc., 59
33  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
34  *
35  *********************************************************************/
36
37
38 #include "config.h"
39
40 #include <stdio.h>
41 #include <sys/types.h>
42
43 #ifndef _WIN32
44 #include <netinet/in.h>
45 #endif
46
47 #include <errno.h>
48 #include <string.h>
49 #include "assert.h"
50
51 #ifdef _WIN32
52 #include <winsock2.h>
53 #endif /* def _WIN32 */
54
55 #ifdef __BEOS__
56 #include <netdb.h>
57 #endif /* def __BEOS__ */
58
59 #ifdef __OS2__
60 #include <utils.h>
61 #endif /* def __OS2__ */
62
63 #include "project.h"
64 #include "jcc.h"
65 #include "errlog.h"
66 #include "jbsockets.h"
67 #include "gateway.h"
68 #include "miscutil.h"
69 #include "list.h"
70 #include "parsers.h"
71
72 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
73 #ifdef HAVE_POLL
74 #ifdef __GLIBC__
75 #include <sys/poll.h>
76 #else
77 #include <poll.h>
78 #endif /* def __GLIBC__ */
79 #endif /* HAVE_POLL */
80 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
81
82 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
83
84 static jb_socket socks4_connect(const struct forward_spec * fwd,
85                                 const char * target_host,
86                                 int target_port,
87                                 struct client_state *csp);
88
89 static jb_socket socks5_connect(const struct forward_spec *fwd,
90                                 const char *target_host,
91                                 int target_port,
92                                 struct client_state *csp);
93
94 enum {
95    SOCKS4_REQUEST_GRANTED        =  90,
96    SOCKS4_REQUEST_REJECT         =  91,
97    SOCKS4_REQUEST_IDENT_FAILED   =  92,
98    SOCKS4_REQUEST_IDENT_CONFLICT =  93
99 };
100
101 enum {
102    SOCKS5_REQUEST_GRANTED             = 0,
103    SOCKS5_REQUEST_FAILED              = 1,
104    SOCKS5_REQUEST_DENIED              = 2,
105    SOCKS5_REQUEST_NETWORK_UNREACHABLE = 3,
106    SOCKS5_REQUEST_HOST_UNREACHABLE    = 4,
107    SOCKS5_REQUEST_CONNECTION_REFUSED  = 5,
108    SOCKS5_REQUEST_TTL_EXPIRED         = 6,
109    SOCKS5_REQUEST_PROTOCOL_ERROR      = 7,
110    SOCKS5_REQUEST_BAD_ADDRESS_TYPE    = 8
111 };
112
113 /* structure of a socks client operation */
114 struct socks_op {
115    unsigned char vn;          /* socks version number */
116    unsigned char cd;          /* command code */
117    unsigned char dstport[2];  /* destination port */
118    unsigned char dstip[4];    /* destination address */
119    char userid;               /* first byte of userid */
120    char padding[3];           /* make sure sizeof(struct socks_op) is endian-independent. */
121    /* more bytes of the userid follow, terminated by a NULL */
122 };
123
124 /* structure of a socks server reply */
125 struct socks_reply {
126    unsigned char vn;          /* socks version number */
127    unsigned char cd;          /* command code */
128    unsigned char dstport[2];  /* destination port */
129    unsigned char dstip[4];    /* destination address */
130 };
131
132 static const char socks_userid[] = "anonymous";
133
134 #ifdef FEATURE_CONNECTION_SHARING
135
136 #define MAX_REUSABLE_CONNECTIONS 100
137 static unsigned int keep_alive_timeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
138
139 static struct reusable_connection reusable_connection[MAX_REUSABLE_CONNECTIONS];
140 static int mark_connection_unused(const struct reusable_connection *connection);
141
142 /*********************************************************************
143  *
144  * Function    :  initialize_reusable_connections
145  *
146  * Description :  Initializes the reusable_connection structures.
147  *                Must be called with connection_reuse_mutex locked.
148  *
149  * Parameters  : N/A
150  *
151  * Returns     : void
152  *
153  *********************************************************************/
154 extern void initialize_reusable_connections(void)
155 {
156    unsigned int slot = 0;
157
158 #if !defined(HAVE_POLL) && !defined(_WIN32)
159    log_error(LOG_LEVEL_INFO,
160       "Detecting already dead connections might not work "
161       "correctly on your platform. In case of problems, "
162       "unset the keep-alive-timeout option.");
163 #endif
164
165    for (slot = 0; slot < SZ(reusable_connection); slot++)
166    {
167       mark_connection_closed(&reusable_connection[slot]);
168    }
169
170    log_error(LOG_LEVEL_CONNECT, "Initialized %d socket slots.", slot);
171 }
172
173
174 /*********************************************************************
175  *
176  * Function    :  remember_connection
177  *
178  * Description :  Remembers a server connection for reuse later on.
179  *
180  * Parameters  :
181  *          1  :  connection = The server connection to remember.
182  *
183  * Returns     : void
184  *
185  *********************************************************************/
186 void remember_connection(const struct reusable_connection *connection)
187 {
188    unsigned int slot = 0;
189    int free_slot_found = FALSE;
190
191    assert(NULL != connection);
192    assert(connection->sfd != JB_INVALID_SOCKET);
193
194    if (mark_connection_unused(connection))
195    {
196       return;
197    }
198
199    privoxy_mutex_lock(&connection_reuse_mutex);
200
201    /* Find free socket slot. */
202    for (slot = 0; slot < SZ(reusable_connection); slot++)
203    {
204       if (reusable_connection[slot].sfd == JB_INVALID_SOCKET)
205       {
206          assert(reusable_connection[slot].in_use == 0);
207          log_error(LOG_LEVEL_CONNECT,
208             "Remembering socket %d for %s:%d in slot %d.",
209             connection->sfd, connection->host, connection->port, slot);
210          free_slot_found = TRUE;
211          break;
212       }
213    }
214
215    if (!free_slot_found)
216    {
217       log_error(LOG_LEVEL_CONNECT,
218         "No free slots found to remember socket for %s:%d. Last slot %d.",
219         connection->host, connection->port, slot);
220       privoxy_mutex_unlock(&connection_reuse_mutex);
221       close_socket(connection->sfd);
222       return;
223    }
224
225    assert(NULL != connection->host);
226    reusable_connection[slot].host = strdup_or_die(connection->host);
227    reusable_connection[slot].sfd = connection->sfd;
228    reusable_connection[slot].port = connection->port;
229    reusable_connection[slot].in_use = 0;
230    reusable_connection[slot].timestamp = connection->timestamp;
231    reusable_connection[slot].request_sent = connection->request_sent;
232    reusable_connection[slot].response_received = connection->response_received;
233    reusable_connection[slot].keep_alive_timeout = connection->keep_alive_timeout;
234    reusable_connection[slot].requests_sent_total = connection->requests_sent_total;
235
236    assert(reusable_connection[slot].gateway_host == NULL);
237    assert(reusable_connection[slot].gateway_port == 0);
238    assert(reusable_connection[slot].forwarder_type == SOCKS_NONE);
239    assert(reusable_connection[slot].forward_host == NULL);
240    assert(reusable_connection[slot].forward_port == 0);
241
242    reusable_connection[slot].forwarder_type = connection->forwarder_type;
243    if (NULL != connection->gateway_host)
244    {
245       reusable_connection[slot].gateway_host = strdup_or_die(connection->gateway_host);
246    }
247    else
248    {
249       reusable_connection[slot].gateway_host = NULL;
250    }
251    reusable_connection[slot].gateway_port = connection->gateway_port;
252
253    if (NULL != connection->forward_host)
254    {
255       reusable_connection[slot].forward_host = strdup_or_die(connection->forward_host);
256    }
257    else
258    {
259       reusable_connection[slot].forward_host = NULL;
260    }
261    reusable_connection[slot].forward_port = connection->forward_port;
262
263    privoxy_mutex_unlock(&connection_reuse_mutex);
264 }
265 #endif /* def FEATURE_CONNECTION_SHARING */
266
267
268 /*********************************************************************
269  *
270  * Function    :  mark_connection_closed
271  *
272  * Description : Marks a reused connection closed.
273  *
274  * Parameters  :
275  *          1  :  closed_connection = The connection to mark as closed.
276  *
277  * Returns     : void
278  *
279  *********************************************************************/
280 void mark_connection_closed(struct reusable_connection *closed_connection)
281 {
282    closed_connection->in_use = FALSE;
283    closed_connection->sfd = JB_INVALID_SOCKET;
284    freez(closed_connection->host);
285    closed_connection->port = 0;
286    closed_connection->timestamp = 0;
287    closed_connection->request_sent = 0;
288    closed_connection->response_received = 0;
289    closed_connection->keep_alive_timeout = 0;
290    closed_connection->requests_sent_total = 0;
291    closed_connection->forwarder_type = SOCKS_NONE;
292    freez(closed_connection->gateway_host);
293    closed_connection->gateway_port = 0;
294    freez(closed_connection->forward_host);
295    closed_connection->forward_port = 0;
296 }
297
298
299 #ifdef FEATURE_CONNECTION_SHARING
300 /*********************************************************************
301  *
302  * Function    :  forget_connection
303  *
304  * Description :  Removes a previously remembered connection from
305  *                the list of reusable connections.
306  *
307  * Parameters  :
308  *          1  :  sfd = The socket belonging to the connection in question.
309  *
310  * Returns     : void
311  *
312  *********************************************************************/
313 void forget_connection(jb_socket sfd)
314 {
315    unsigned int slot = 0;
316
317    assert(sfd != JB_INVALID_SOCKET);
318
319    privoxy_mutex_lock(&connection_reuse_mutex);
320
321    for (slot = 0; slot < SZ(reusable_connection); slot++)
322    {
323       if (reusable_connection[slot].sfd == sfd)
324       {
325          assert(reusable_connection[slot].in_use);
326
327          log_error(LOG_LEVEL_CONNECT,
328             "Forgetting socket %d for %s:%d in slot %d.",
329             sfd, reusable_connection[slot].host,
330             reusable_connection[slot].port, slot);
331          mark_connection_closed(&reusable_connection[slot]);
332          break;
333       }
334    }
335
336    privoxy_mutex_unlock(&connection_reuse_mutex);
337
338 }
339 #endif /* def FEATURE_CONNECTION_SHARING */
340
341
342 #ifdef FEATURE_CONNECTION_KEEP_ALIVE
343 /*********************************************************************
344  *
345  * Function    :  connection_destination_matches
346  *
347  * Description :  Determines whether a remembered connection can
348  *                be reused. That is, whether the destination and
349  *                the forwarding settings match.
350  *
351  * Parameters  :
352  *          1  :  connection = The connection to check.
353  *          2  :  http = The destination for the connection.
354  *          3  :  fwd  = The forwarder settings.
355  *
356  * Returns     :  TRUE for yes, FALSE otherwise.
357  *
358  *********************************************************************/
359 int connection_destination_matches(const struct reusable_connection *connection,
360                                    const struct http_request *http,
361                                    const struct forward_spec *fwd)
362 {
363    if ((connection->forwarder_type != fwd->type)
364     || (connection->gateway_port   != fwd->gateway_port)
365     || (connection->forward_port   != fwd->forward_port)
366     || (connection->port           != http->port))
367    {
368       return FALSE;
369    }
370
371    if ((    (NULL != connection->gateway_host)
372          && (NULL != fwd->gateway_host)
373          && strcmpic(connection->gateway_host, fwd->gateway_host))
374        && (connection->gateway_host != fwd->gateway_host))
375    {
376       log_error(LOG_LEVEL_CONNECT,
377          "Gateway mismatch. Previous gateway: %s. Current gateway: %s",
378          connection->gateway_host, fwd->gateway_host);
379       return FALSE;
380    }
381
382    if ((    (NULL != connection->forward_host)
383          && (NULL != fwd->forward_host)
384          && strcmpic(connection->forward_host, fwd->forward_host))
385       && (connection->forward_host != fwd->forward_host))
386    {
387       log_error(LOG_LEVEL_CONNECT,
388          "Forwarding proxy mismatch. Previous proxy: %s. Current proxy: %s",
389          connection->forward_host, fwd->forward_host);
390       return FALSE;
391    }
392
393    return (!strcmpic(connection->host, http->host));
394
395 }
396 #endif /* def FEATURE_CONNECTION_KEEP_ALIVE */
397
398
399 #ifdef FEATURE_CONNECTION_SHARING
400 /*********************************************************************
401  *
402  * Function    :  close_unusable_connections
403  *
404  * Description :  Closes remembered connections that have timed
405  *                out or have been closed on the other side.
406  *
407  * Parameters  :  none
408  *
409  * Returns     :  Number of connections that are still alive.
410  *
411  *********************************************************************/
412 int close_unusable_connections(void)
413 {
414    unsigned int slot = 0;
415    int connections_alive = 0;
416
417    privoxy_mutex_lock(&connection_reuse_mutex);
418
419    for (slot = 0; slot < SZ(reusable_connection); slot++)
420    {
421       if (!reusable_connection[slot].in_use
422          && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
423       {
424          time_t time_open = time(NULL) - reusable_connection[slot].timestamp;
425          time_t latency = (reusable_connection[slot].response_received -
426             reusable_connection[slot].request_sent) / 2;
427
428          if (reusable_connection[slot].keep_alive_timeout < time_open + latency)
429          {
430             log_error(LOG_LEVEL_CONNECT,
431                "The connection to %s:%d in slot %d timed out. "
432                "Closing socket %d. Timeout is: %d. Assumed latency: %d.",
433                reusable_connection[slot].host,
434                reusable_connection[slot].port, slot,
435                reusable_connection[slot].sfd,
436                reusable_connection[slot].keep_alive_timeout,
437                latency);
438             close_socket(reusable_connection[slot].sfd);
439             mark_connection_closed(&reusable_connection[slot]);
440          }
441          else if (!socket_is_still_alive(reusable_connection[slot].sfd))
442          {
443             log_error(LOG_LEVEL_CONNECT,
444                "The connection to %s:%d in slot %d is no longer usable. "
445                "Closing socket %d.", reusable_connection[slot].host,
446                reusable_connection[slot].port, slot,
447                reusable_connection[slot].sfd);
448             close_socket(reusable_connection[slot].sfd);
449             mark_connection_closed(&reusable_connection[slot]);
450          }
451          else
452          {
453             connections_alive++;
454          }
455       }
456    }
457
458    privoxy_mutex_unlock(&connection_reuse_mutex);
459
460    return connections_alive;
461
462 }
463
464
465 /*********************************************************************
466  *
467  * Function    :  get_reusable_connection
468  *
469  * Description :  Returns an open socket to a previously remembered
470  *                open connection (if there is one).
471  *
472  * Parameters  :
473  *          1  :  http = The destination for the connection.
474  *          2  :  fwd  = The forwarder settings.
475  *
476  * Returns     :  JB_INVALID_SOCKET => No reusable connection found,
477  *                otherwise a usable socket.
478  *
479  *********************************************************************/
480 static jb_socket get_reusable_connection(const struct http_request *http,
481                                          const struct forward_spec *fwd)
482 {
483    jb_socket sfd = JB_INVALID_SOCKET;
484    unsigned int slot = 0;
485
486    close_unusable_connections();
487
488    privoxy_mutex_lock(&connection_reuse_mutex);
489
490    for (slot = 0; slot < SZ(reusable_connection); slot++)
491    {
492       if (!reusable_connection[slot].in_use
493          && (JB_INVALID_SOCKET != reusable_connection[slot].sfd))
494       {
495          if (connection_destination_matches(&reusable_connection[slot], http, fwd))
496          {
497             reusable_connection[slot].in_use = TRUE;
498             sfd = reusable_connection[slot].sfd;
499             log_error(LOG_LEVEL_CONNECT,
500                "Found reusable socket %d for %s:%d in slot %d. Timestamp made %d "
501                "seconds ago. Timeout: %d. Latency: %d. Requests served: %d",
502                sfd, reusable_connection[slot].host, reusable_connection[slot].port,
503                slot, time(NULL) - reusable_connection[slot].timestamp,
504                reusable_connection[slot].keep_alive_timeout,
505                (int)(reusable_connection[slot].response_received -
506                reusable_connection[slot].request_sent),
507                reusable_connection[slot].requests_sent_total);
508             break;
509          }
510       }
511    }
512
513    privoxy_mutex_unlock(&connection_reuse_mutex);
514
515    return sfd;
516
517 }
518
519
520 /*********************************************************************
521  *
522  * Function    :  mark_connection_unused
523  *
524  * Description :  Gives a remembered connection free for reuse.
525  *
526  * Parameters  :
527  *          1  :  connection = The connection in question.
528  *
529  * Returns     :  TRUE => Socket found and marked as unused.
530  *                FALSE => Socket not found.
531  *
532  *********************************************************************/
533 static int mark_connection_unused(const struct reusable_connection *connection)
534 {
535    unsigned int slot = 0;
536    int socket_found = FALSE;
537
538    assert(connection->sfd != JB_INVALID_SOCKET);
539
540    privoxy_mutex_lock(&connection_reuse_mutex);
541
542    for (slot = 0; slot < SZ(reusable_connection); slot++)
543    {
544       if (reusable_connection[slot].sfd == connection->sfd)
545       {
546          assert(reusable_connection[slot].in_use);
547          socket_found = TRUE;
548          log_error(LOG_LEVEL_CONNECT,
549             "Marking open socket %d for %s:%d in slot %d as unused.",
550             connection->sfd, reusable_connection[slot].host,
551             reusable_connection[slot].port, slot);
552          reusable_connection[slot].in_use = 0;
553          reusable_connection[slot].timestamp = connection->timestamp;
554          break;
555       }
556    }
557
558    privoxy_mutex_unlock(&connection_reuse_mutex);
559
560    return socket_found;
561
562 }
563
564
565 /*********************************************************************
566  *
567  * Function    :  set_keep_alive_timeout
568  *
569  * Description :  Sets the timeout after which open
570  *                connections will no longer be reused.
571  *
572  * Parameters  :
573  *          1  :  timeout = The timeout in seconds.
574  *
575  * Returns     :  void
576  *
577  *********************************************************************/
578 void set_keep_alive_timeout(unsigned int timeout)
579 {
580    keep_alive_timeout = timeout;
581 }
582 #endif /* def FEATURE_CONNECTION_SHARING */
583
584
585 /*********************************************************************
586  *
587  * Function    :  forwarded_connect
588  *
589  * Description :  Connect to a specified web server, possibly via
590  *                a HTTP proxy and/or a SOCKS proxy.
591  *
592  * Parameters  :
593  *          1  :  fwd = the proxies to use when connecting.
594  *          2  :  http = the http request and apropos headers
595  *          3  :  csp = Current client state (buffers, headers, etc...)
596  *
597  * Returns     :  JB_INVALID_SOCKET => failure, else it is the socket file descriptor.
598  *
599  *********************************************************************/
600 jb_socket forwarded_connect(const struct forward_spec * fwd,
601                             struct http_request *http,
602                             struct client_state *csp)
603 {
604    const char * dest_host;
605    int dest_port;
606    jb_socket sfd = JB_INVALID_SOCKET;
607
608 #ifdef FEATURE_CONNECTION_SHARING
609    if ((csp->config->feature_flags & RUNTIME_FEATURE_CONNECTION_SHARING)
610       && !(csp->flags & CSP_FLAG_SERVER_SOCKET_TAINTED))
611    {
612       sfd = get_reusable_connection(http, fwd);
613       if (JB_INVALID_SOCKET != sfd)
614       {
615          return sfd;
616       }
617    }
618 #endif /* def FEATURE_CONNECTION_SHARING */
619
620    /* Figure out if we need to connect to the web server or a HTTP proxy. */
621    if (fwd->forward_host)
622    {
623       /* HTTP proxy */
624       dest_host = fwd->forward_host;
625       dest_port = fwd->forward_port;
626    }
627    else
628    {
629       /* Web server */
630       dest_host = http->host;
631       dest_port = http->port;
632    }
633
634    /* Connect, maybe using a SOCKS proxy */
635    switch (fwd->type)
636    {
637       case SOCKS_NONE:
638       case FORWARD_WEBSERVER:
639          sfd = connect_to(dest_host, dest_port, csp);
640          break;
641       case SOCKS_4:
642       case SOCKS_4A:
643          sfd = socks4_connect(fwd, dest_host, dest_port, csp);
644          break;
645       case SOCKS_5:
646       case SOCKS_5T:
647          sfd = socks5_connect(fwd, dest_host, dest_port, csp);
648          break;
649       default:
650          /* Should never get here */
651          log_error(LOG_LEVEL_FATAL,
652             "Internal error in forwarded_connect(). Bad proxy type: %d", fwd->type);
653    }
654
655    if (JB_INVALID_SOCKET != sfd)
656    {
657       log_error(LOG_LEVEL_CONNECT,
658          "Created new connection to %s:%d on socket %d.",
659          http->host, http->port, sfd);
660    }
661
662    return sfd;
663
664 }
665
666
667 #ifdef FUZZ_SOCKS
668 /*********************************************************************
669  *
670  * Function    :  socks_fuzz
671  *
672  * Description :  Wrapper around socks[45]_connect() used for fuzzing.
673  *
674  * Parameters  :
675  *          1  :  csp = Current client state (buffers, headers, etc...)
676  *
677  * Returns     :  JB_ERR_OK or JB_ERR_PARSE
678  *
679  *********************************************************************/
680 extern jb_err socks_fuzz(struct client_state *csp)
681 {
682    jb_socket socket;
683    static struct forward_spec fwd;
684    char target_host[] = "fuzz.example.org";
685    int target_port = 12345;
686
687    fwd.gateway_host = strdup_or_die("fuzz.example.org");
688    fwd.gateway_port = 12345;
689
690    fwd.type = SOCKS_4A;
691    socket = socks4_connect(&fwd, target_host, target_port, csp);
692
693    if (JB_INVALID_SOCKET != socket)
694    {
695       fwd.type = SOCKS_5;
696       socket = socks5_connect(&fwd, target_host, target_port, csp);
697    }
698
699    if (JB_INVALID_SOCKET == socket)
700    {
701       log_error(LOG_LEVEL_ERROR, "%s", csp->error_message);
702       return JB_ERR_PARSE;
703    }
704
705    log_error(LOG_LEVEL_INFO, "Input looks like an acceptable socks response");
706
707    return JB_ERR_OK;
708
709 }
710 #endif
711
712 /*********************************************************************
713  *
714  * Function    :  socks4_connect
715  *
716  * Description :  Connect to the SOCKS server, and connect through
717  *                it to the specified server.   This handles
718  *                all the SOCKS negotiation, and returns a file
719  *                descriptor for a socket which can be treated as a
720  *                normal (non-SOCKS) socket.
721  *
722  *                Logged error messages are saved to csp->error_message
723  *                and later reused by error_response() for the CGI
724  *                message. strdup allocation failures are handled there.
725  *
726  * Parameters  :
727  *          1  :  fwd = Specifies the SOCKS proxy to use.
728  *          2  :  target_host = The final server to connect to.
729  *          3  :  target_port = The final port to connect to.
730  *          4  :  csp = Current client state (buffers, headers, etc...)
731  *
732  * Returns     :  JB_INVALID_SOCKET => failure, else a socket file descriptor.
733  *
734  *********************************************************************/
735 static jb_socket socks4_connect(const struct forward_spec * fwd,
736                                 const char * target_host,
737                                 int target_port,
738                                 struct client_state *csp)
739 {
740    unsigned long web_server_addr;
741    char buf[BUFFER_SIZE];
742    struct socks_op    *c = (struct socks_op    *)buf;
743    struct socks_reply *s = (struct socks_reply *)buf;
744    size_t n;
745    size_t csiz;
746    jb_socket sfd;
747    int err = 0;
748    char *errstr = NULL;
749
750    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
751    {
752       /* XXX: Shouldn't the config file parser prevent this? */
753       errstr = "NULL gateway host specified.";
754       err = 1;
755    }
756
757    if (fwd->gateway_port <= 0)
758    {
759       errstr = "invalid gateway port specified.";
760       err = 1;
761    }
762
763    if (err)
764    {
765       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
766       csp->error_message = strdup(errstr);
767       errno = EINVAL;
768       return(JB_INVALID_SOCKET);
769    }
770
771    /* build a socks request for connection to the web server */
772
773    strlcpy(&(c->userid), socks_userid, sizeof(buf) - sizeof(struct socks_op));
774
775    csiz = sizeof(*c) + sizeof(socks_userid) - sizeof(c->userid) - sizeof(c->padding);
776
777    switch (fwd->type)
778    {
779       case SOCKS_4:
780          web_server_addr = resolve_hostname_to_ip(target_host);
781          if (web_server_addr == INADDR_NONE)
782          {
783             errstr = "could not resolve target host";
784             log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s %s", errstr, target_host);
785             err = 1;
786          }
787          else
788          {
789             web_server_addr = htonl(web_server_addr);
790          }
791          break;
792       case SOCKS_4A:
793          web_server_addr = 0x00000001;
794          n = csiz + strlen(target_host) + 1;
795          if (n > sizeof(buf))
796          {
797             errno = EINVAL;
798             errstr = "buffer cbuf too small.";
799             log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
800             err = 1;
801          }
802          else
803          {
804             strlcpy(buf + csiz, target_host, sizeof(buf) - sizeof(struct socks_op) - csiz);
805             /*
806              * What we forward to the socks4a server should have the
807              * size of socks_op, plus the length of the userid plus
808              * its \0 byte (which we don't have to add because the
809              * first byte of the userid is counted twice as it's also
810              * part of sock_op) minus the padding bytes (which are part
811              * of the userid as well), plus the length of the target_host
812              * (which is stored csiz bytes after the beginning of the buffer),
813              * plus another \0 byte.
814              */
815             assert(n == sizeof(struct socks_op) + strlen(&(c->userid)) - sizeof(c->padding) + strlen(buf + csiz) + 1);
816             csiz = n;
817          }
818          break;
819       default:
820          /* Should never get here */
821          log_error(LOG_LEVEL_FATAL,
822             "socks4_connect: SOCKS4 impossible internal error - bad SOCKS type.");
823          /* Not reached */
824          return(JB_INVALID_SOCKET);
825    }
826
827    if (err)
828    {
829       csp->error_message = strdup(errstr);
830       return(JB_INVALID_SOCKET);
831    }
832
833    c->vn          = 4;
834    c->cd          = 1;
835    c->dstport[0]  = (unsigned char)((target_port       >> 8 ) & 0xff);
836    c->dstport[1]  = (unsigned char)((target_port            ) & 0xff);
837    c->dstip[0]    = (unsigned char)((web_server_addr   >> 24) & 0xff);
838    c->dstip[1]    = (unsigned char)((web_server_addr   >> 16) & 0xff);
839    c->dstip[2]    = (unsigned char)((web_server_addr   >>  8) & 0xff);
840    c->dstip[3]    = (unsigned char)((web_server_addr        ) & 0xff);
841
842 #ifdef FUZZ_SOCKS
843    sfd = 0;
844 #else
845    /* pass the request to the socks server */
846    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
847
848    if (sfd == JB_INVALID_SOCKET)
849    {
850       /* The error an its reason have already been logged by connect_to()  */
851       return(JB_INVALID_SOCKET);
852    }
853    else if (write_socket(sfd, (char *)c, csiz))
854    {
855       errstr = "SOCKS4 negotiation write failed.";
856       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
857       err = 1;
858       close_socket(sfd);
859    }
860    else if (!data_is_available(sfd, csp->config->socket_timeout))
861    {
862       if (socket_is_still_alive(sfd))
863       {
864          errstr = "SOCKS4 negotiation timed out";
865       }
866       else
867       {
868          errstr = "SOCKS4 negotiation got aborted by the server";
869       }
870       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
871       err = 1;
872       close_socket(sfd);
873    }
874    else
875 #endif
876        if (read_socket(sfd, buf, sizeof(buf)) != sizeof(*s))
877    {
878       errstr = "SOCKS4 negotiation read failed.";
879       log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
880       err = 1;
881       close_socket(sfd);
882    }
883
884    if (err)
885    {
886       csp->error_message = strdup(errstr);
887       return(JB_INVALID_SOCKET);
888    }
889
890    switch (s->cd)
891    {
892       case SOCKS4_REQUEST_GRANTED:
893          return(sfd);
894       case SOCKS4_REQUEST_REJECT:
895          errstr = "SOCKS request rejected or failed.";
896          errno = EINVAL;
897          break;
898       case SOCKS4_REQUEST_IDENT_FAILED:
899          errstr = "SOCKS request rejected because "
900             "SOCKS server cannot connect to identd on the client.";
901          errno = EACCES;
902          break;
903       case SOCKS4_REQUEST_IDENT_CONFLICT:
904          errstr = "SOCKS request rejected because "
905             "the client program and identd report "
906             "different user-ids.";
907          errno = EACCES;
908          break;
909       default:
910          errno = ENOENT;
911          snprintf(buf, sizeof(buf),
912             "SOCKS request rejected for reason code %d.", s->cd);
913          errstr = buf;
914    }
915
916    log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s", errstr);
917    csp->error_message = strdup(errstr);
918    close_socket(sfd);
919
920    return(JB_INVALID_SOCKET);
921
922 }
923
924 /*********************************************************************
925  *
926  * Function    :  translate_socks5_error
927  *
928  * Description :  Translates a SOCKS errors to a string.
929  *
930  * Parameters  :
931  *          1  :  socks_error = The error code to translate.
932  *
933  * Returns     :  The string translation.
934  *
935  *********************************************************************/
936 static const char *translate_socks5_error(int socks_error)
937 {
938    switch (socks_error)
939    {
940       /* XXX: these should be more descriptive */
941       case SOCKS5_REQUEST_FAILED:
942          return "SOCKS5 request failed";
943       case SOCKS5_REQUEST_DENIED:
944          return "SOCKS5 request denied";
945       case SOCKS5_REQUEST_NETWORK_UNREACHABLE:
946          return "SOCKS5 network unreachable";
947       case SOCKS5_REQUEST_HOST_UNREACHABLE:
948          return "SOCKS5 destination host unreachable";
949       case SOCKS5_REQUEST_CONNECTION_REFUSED:
950          return "SOCKS5 connection refused";
951       case SOCKS5_REQUEST_TTL_EXPIRED:
952          return "SOCKS5 TTL expired";
953       case SOCKS5_REQUEST_PROTOCOL_ERROR:
954          return "SOCKS5 client protocol error";
955       case SOCKS5_REQUEST_BAD_ADDRESS_TYPE:
956          return "SOCKS5 domain names unsupported";
957       case SOCKS5_REQUEST_GRANTED:
958          return "everything's peachy";
959       default:
960          return "SOCKS5 negotiation protocol error";
961    }
962 }
963
964
965 /*********************************************************************
966  *
967  * Function    :  socks5_connect
968  *
969  * Description :  Connect to the SOCKS server, and connect through
970  *                it to the specified server.   This handles
971  *                all the SOCKS negotiation, and returns a file
972  *                descriptor for a socket which can be treated as a
973  *                normal (non-SOCKS) socket.
974  *
975  * Parameters  :
976  *          1  :  fwd = Specifies the SOCKS proxy to use.
977  *          2  :  target_host = The final server to connect to.
978  *          3  :  target_port = The final port to connect to.
979  *          4  :  csp = Current client state (buffers, headers, etc...)
980  *
981  * Returns     :  JB_INVALID_SOCKET => failure, else a socket file descriptor.
982  *
983  *********************************************************************/
984 static jb_socket socks5_connect(const struct forward_spec *fwd,
985                                 const char *target_host,
986                                 int target_port,
987                                 struct client_state *csp)
988 {
989 #define SIZE_SOCKS5_REPLY_IPV4 10
990 #define SIZE_SOCKS5_REPLY_IPV6 22
991 #define SOCKS5_REPLY_DIFFERENCE (SIZE_SOCKS5_REPLY_IPV6 - SIZE_SOCKS5_REPLY_IPV4)
992    int err = 0;
993    char cbuf[300];
994    char sbuf[SIZE_SOCKS5_REPLY_IPV6];
995    size_t client_pos = 0;
996    int server_size = 0;
997    size_t hostlen = 0;
998    jb_socket sfd;
999    const char *errstr = NULL;
1000
1001    assert(fwd->gateway_host);
1002    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
1003    {
1004       errstr = "NULL gateway host specified";
1005       err = 1;
1006    }
1007
1008    if (fwd->gateway_port <= 0)
1009    {
1010       /*
1011        * XXX: currently this can't happen because in
1012        * case of invalid gateway ports we use the defaults.
1013        * Of course we really shouldn't do that.
1014        */
1015       errstr = "invalid gateway port specified";
1016       err = 1;
1017    }
1018
1019    hostlen = strlen(target_host);
1020    if (hostlen > (size_t)255)
1021    {
1022       errstr = "target host name is longer than 255 characters";
1023       err = 1;
1024    }
1025
1026    if ((fwd->type != SOCKS_5) && (fwd->type != SOCKS_5T))
1027    {
1028       /* Should never get here */
1029       log_error(LOG_LEVEL_FATAL,
1030          "SOCKS5 impossible internal error - bad SOCKS type");
1031       err = 1;
1032    }
1033
1034    if (err)
1035    {
1036       errno = EINVAL;
1037       assert(errstr != NULL);
1038       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1039       csp->error_message = strdup(errstr);
1040       return(JB_INVALID_SOCKET);
1041    }
1042
1043 #ifdef FUZZ_SOCKS
1044    sfd = 0;
1045    if (!err && read_socket(sfd, sbuf, 2) != 2)
1046 #else
1047    /* pass the request to the socks server */
1048    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
1049
1050    if (sfd == JB_INVALID_SOCKET)
1051    {
1052       errstr = "socks5 server unreachable";
1053       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1054       /* Free the generic error message provided by connect_to() */
1055       freez(csp->error_message);
1056       csp->error_message = strdup(errstr);
1057       return(JB_INVALID_SOCKET);
1058    }
1059
1060    client_pos = 0;
1061    cbuf[client_pos++] = '\x05'; /* Version */
1062    cbuf[client_pos++] = '\x01'; /* One authentication method supported */
1063    cbuf[client_pos++] = '\x00'; /* The no authentication authentication method */
1064
1065    if (write_socket(sfd, cbuf, client_pos))
1066    {
1067       errstr = "SOCKS5 negotiation write failed";
1068       csp->error_message = strdup(errstr);
1069       log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1070       close_socket(sfd);
1071       return(JB_INVALID_SOCKET);
1072    }
1073    if (!data_is_available(sfd, csp->config->socket_timeout))
1074    {
1075       if (socket_is_still_alive(sfd))
1076       {
1077          errstr = "SOCKS5 negotiation timed out";
1078       }
1079       else
1080       {
1081          errstr = "SOCKS5 negotiation got aborted by the server";
1082       }
1083       err = 1;
1084    }
1085
1086    if (!err && read_socket(sfd, sbuf, sizeof(sbuf)) != 2)
1087 #endif
1088    {
1089       errstr = "SOCKS5 negotiation read failed";
1090       err = 1;
1091    }
1092
1093    if (!err && (sbuf[0] != '\x05'))
1094    {
1095       errstr = "SOCKS5 negotiation protocol version error";
1096       err = 1;
1097    }
1098
1099    if (!err && (sbuf[1] == '\xff'))
1100    {
1101       errstr = "SOCKS5 authentication required";
1102       err = 1;
1103    }
1104
1105    if (!err && (sbuf[1] != '\x00'))
1106    {
1107       errstr = "SOCKS5 negotiation protocol error";
1108       err = 1;
1109    }
1110
1111    if (err)
1112    {
1113       assert(errstr != NULL);
1114       log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1115       csp->error_message = strdup(errstr);
1116       close_socket(sfd);
1117       errno = EINVAL;
1118       return(JB_INVALID_SOCKET);
1119    }
1120
1121    client_pos = 0;
1122    cbuf[client_pos++] = '\x05'; /* Version */
1123    cbuf[client_pos++] = '\x01'; /* TCP connect */
1124    cbuf[client_pos++] = '\x00'; /* Reserved, must be 0x00 */
1125    cbuf[client_pos++] = '\x03'; /* Address is domain name */
1126    cbuf[client_pos++] = (char)(hostlen & 0xffu);
1127    assert(sizeof(cbuf) - client_pos > (size_t)255);
1128    /* Using strncpy because we really want the nul byte padding. */
1129    strncpy(cbuf + client_pos, target_host, sizeof(cbuf) - client_pos);
1130    client_pos += (hostlen & 0xffu);
1131    cbuf[client_pos++] = (char)((target_port >> 8) & 0xff);
1132    cbuf[client_pos++] = (char)((target_port     ) & 0xff);
1133
1134 #ifndef FUZZ_SOCKS
1135    if (write_socket(sfd, cbuf, client_pos))
1136    {
1137       errstr = "SOCKS5 negotiation write failed";
1138       csp->error_message = strdup(errstr);
1139       log_error(LOG_LEVEL_CONNECT, "%s", errstr);
1140       close_socket(sfd);
1141       errno = EINVAL;
1142       return(JB_INVALID_SOCKET);
1143    }
1144
1145    /*
1146     * Optimistically send the HTTP request with the initial
1147     * SOCKS request if the user enabled the use of Tor extensions,
1148     * the CONNECT method isn't being used (in which case the client
1149     * doesn't send data until it gets our 200 response) and the
1150     * client request has actually been completely read already.
1151     */
1152    if ((fwd->type == SOCKS_5T) && (csp->http->ssl == 0)
1153       && (csp->flags & CSP_FLAG_CLIENT_REQUEST_COMPLETELY_READ))
1154    {
1155       char *client_headers = list_to_text(csp->headers);
1156       size_t header_length;
1157
1158       if (client_headers == NULL)
1159       {
1160          log_error(LOG_LEVEL_FATAL, "Out of memory rebuilding client headers");
1161       }
1162       list_remove_all(csp->headers);
1163       header_length= strlen(client_headers);
1164
1165       log_error(LOG_LEVEL_CONNECT,
1166          "Optimistically sending %d bytes of client headers intended for %s",
1167          header_length, csp->http->hostport);
1168
1169       if (write_socket(sfd, client_headers, header_length))
1170       {
1171          log_error(LOG_LEVEL_CONNECT,
1172             "optimistically writing header to: %s failed: %E", csp->http->hostport);
1173          freez(client_headers);
1174          return(JB_INVALID_SOCKET);
1175       }
1176       freez(client_headers);
1177       if (csp->expected_client_content_length != 0)
1178       {
1179          unsigned long long buffered_request_bytes =
1180             (unsigned long long)(csp->client_iob->eod - csp->client_iob->cur);
1181          log_error(LOG_LEVEL_CONNECT,
1182             "Optimistically sending %d bytes of client body. Expected %d",
1183             csp->expected_client_content_length, buffered_request_bytes);
1184          assert(csp->expected_client_content_length == buffered_request_bytes);
1185          if (write_socket(sfd, csp->client_iob->cur, buffered_request_bytes))
1186          {
1187             log_error(LOG_LEVEL_CONNECT,
1188                "optimistically writing %d bytes of client body to: %s failed: %E",
1189                buffered_request_bytes, csp->http->hostport);
1190             return(JB_INVALID_SOCKET);
1191          }
1192          clear_iob(csp->client_iob);
1193       }
1194    }
1195 #endif
1196
1197    server_size = read_socket(sfd, sbuf, SIZE_SOCKS5_REPLY_IPV4);
1198    if (server_size != SIZE_SOCKS5_REPLY_IPV4)
1199    {
1200       errstr = "SOCKS5 negotiation read failed";
1201    }
1202    else
1203    {
1204       if (sbuf[0] != '\x05')
1205       {
1206          errstr = "SOCKS5 negotiation protocol version error";
1207       }
1208       else if (sbuf[2] != '\x00')
1209       {
1210          errstr = "SOCKS5 negotiation protocol error";
1211       }
1212       else if (sbuf[1] != SOCKS5_REQUEST_GRANTED)
1213       {
1214          errstr = translate_socks5_error(sbuf[1]);
1215       }
1216       else
1217       {
1218          if (sbuf[3] == '\x04')
1219          {
1220             /*
1221              * The address field contains an IPv6 address
1222              * which means we didn't get the whole reply
1223              * yet. Read and discard the rest of it to make
1224              * sure it isn't treated as HTTP data later on.
1225              */
1226             server_size = read_socket(sfd, sbuf, SOCKS5_REPLY_DIFFERENCE);
1227             if (server_size != SOCKS5_REPLY_DIFFERENCE)
1228             {
1229                errstr = "SOCKS5 negotiation read failed (IPv6 address)";
1230             }
1231          }
1232          else if (sbuf[3] != '\x01')
1233          {
1234              errstr = "SOCKS5 reply contains unsupported address type";
1235          }
1236          if (errstr == NULL)
1237          {
1238             return(sfd);
1239          }
1240       }
1241    }
1242
1243    assert(errstr != NULL);
1244    csp->error_message = strdup(errstr);
1245    log_error(LOG_LEVEL_CONNECT, "socks5_connect: %s", errstr);
1246    close_socket(sfd);
1247    errno = EINVAL;
1248
1249    return(JB_INVALID_SOCKET);
1250
1251 }
1252
1253 /*
1254   Local Variables:
1255   tab-width: 3
1256   end:
1257 */