2e4cbe27332bbbec1d3a63b6aa1325efeec5ac1c
[privoxy.git] / gateway.c
1 const char gateway_rcs[] = "$Id: gateway.c,v 1.10 2002/03/07 03:50:19 oes 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  *                proxy).
9  *
10  * Copyright   :  Written by and Copyright (C) 2001 the SourceForge
11  *                IJBSWA team.  http://ijbswa.sourceforge.net
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  * Revisions   :
36  *    $Log: gateway.c,v $
37  *    Revision 1.10  2002/03/07 03:50:19  oes
38  *     - Improved handling of failed DNS lookups
39  *     - Fixed compiler warnings
40  *
41  *    Revision 1.9  2001/10/25 03:40:48  david__schmidt
42  *    Change in porting tactics: OS/2's EMX porting layer doesn't allow multiple
43  *    threads to call select() simultaneously.  So, it's time to do a real, live,
44  *    native OS/2 port.  See defines for __EMX__ (the porting layer) vs. __OS2__
45  *    (native). Both versions will work, but using __OS2__ offers multi-threading.
46  *
47  *    Revision 1.8  2001/09/13 20:10:12  jongfoster
48  *    Fixing missing #include under Windows
49  *
50  *    Revision 1.7  2001/09/12 17:58:26  steudten
51  *
52  *    add #include <string.h>
53  *
54  *    Revision 1.6  2001/09/10 10:41:16  oes
55  *    Added #include in.h
56  *
57  *    Revision 1.5  2001/07/29 18:47:57  jongfoster
58  *    Adding missing #include project.h
59  *
60  *    Revision 1.4  2001/07/24 12:47:06  oes
61  *    Applied BeOS support update by Eugenia
62  *
63  *    Revision 1.3  2001/06/09 10:55:28  jongfoster
64  *    Changing BUFSIZ ==> BUFFER_SIZE
65  *
66  *    Revision 1.2  2001/06/07 23:11:38  jongfoster
67  *    Removing gateways[] list - no longer used.
68  *    Replacing function pointer in struct gateway with a directly
69  *    called function forwarded_connect(), which can do the common
70  *    task of deciding whether to connect to the web server or HTTP
71  *    proxy.
72  *    Replacing struct gateway with struct forward_spec
73  *    Fixing bug with SOCKS4A and HTTP proxy server in combination.
74  *    It was a bug which led to the connection being made to the web
75  *    server rather than the HTTP proxy, and also a buffer overrun.
76  *
77  *    Revision 1.1.1.1  2001/05/15 13:58:54  oes
78  *    Initial import of version 2.9.3 source tree
79  *
80  *
81  *********************************************************************/
82 \f
83
84 #include "config.h"
85
86 #include <stdio.h>
87 #include <sys/types.h>
88
89 #ifndef _WIN32
90 #include <netinet/in.h>
91 #endif
92
93 #include <errno.h>
94 #include <string.h>
95
96 #ifdef _WIN32
97 #include <winsock2.h>
98 #endif /* def _WIN32 */
99
100 #ifdef __BEOS__
101 #include <netdb.h>
102 #endif /* def __BEOS__ */
103
104 #ifdef __OS2__
105 #include <utils.h>
106 #endif /* def __OS2__ */
107
108 #include "project.h"
109 #include "jcc.h"
110 #include "errlog.h"
111 #include "jbsockets.h"
112 #include "gateway.h"
113
114 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
115
116 static int socks4_connect(const struct forward_spec * fwd,
117                           const char * target_host,
118                           int target_port,
119                           struct client_state *csp);
120
121
122 #define SOCKS_REQUEST_GRANTED          90
123 #define SOCKS_REQUEST_REJECT           91
124 #define SOCKS_REQUEST_IDENT_FAILED     92
125 #define SOCKS_REQUEST_IDENT_CONFLICT   93
126
127 /* structure of a socks client operation */
128 struct socks_op {
129    unsigned char vn;          /* socks version number */
130    unsigned char cd;          /* command code */
131    unsigned char dstport[2];  /* destination port */
132    unsigned char dstip[4];    /* destination address */
133    unsigned char userid;      /* first byte of userid */
134    /* more bytes of the userid follow, terminated by a NULL */
135 };
136
137 /* structure of a socks server reply */
138 struct socks_reply {
139    unsigned char vn;          /* socks version number */
140    unsigned char cd;          /* command code */
141    unsigned char dstport[2];  /* destination port */
142    unsigned char dstip[4];    /* destination address */
143 };
144
145 static const char socks_userid[] = "anonymous";
146
147
148 /*********************************************************************
149  *
150  * Function    :  forwarded_connect
151  *
152  * Description :  Connect to a specified web server, possibly via
153  *                a HTTP proxy and/or a SOCKS proxy.
154  *
155  * Parameters  :
156  *          1  :  gw = pointer to a gateway structure (such as gw_default)
157  *          2  :  http = the http request and apropos headers
158  *          3  :  csp = Current client state (buffers, headers, etc...)
159  *
160  * Returns     :  -1 => failure, else it is the socket file descriptor.
161  *
162  *********************************************************************/
163 int forwarded_connect(const struct forward_spec * fwd,
164                       struct http_request *http,
165                       struct client_state *csp)
166 {
167    const char * dest_host;
168    int dest_port;
169
170    /* Figure out if we need to connect to the web server or a HTTP proxy. */
171    if (fwd->forward_host)
172    {
173       /* HTTP proxy */
174       dest_host = fwd->forward_host;
175       dest_port = fwd->forward_port;
176    }
177    else
178    {
179       /* Web server */
180       dest_host = http->host;
181       dest_port = http->port;
182    }
183
184    /* Connect, maybe using a SOCKS proxy */
185    switch (fwd->type)
186    {
187       case SOCKS_NONE:
188          return (connect_to(dest_host, dest_port, csp));
189
190       case SOCKS_4:
191       case SOCKS_4A:
192          return (socks4_connect(fwd, dest_host, dest_port, csp));
193
194       default:
195          /* Should never get here */
196          log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
197          errno = EINVAL;
198          return(-1);
199    }
200 }
201
202
203 /*********************************************************************
204  *
205  * Function    :  socks4_connect
206  *
207  * Description :  Connect to the SOCKS server, and connect through
208  *                it to the specified server.   This handles
209  *                all the SOCKS negotiation, and returns a file
210  *                descriptor for a socket which can be treated as a
211  *                normal (non-SOCKS) socket.
212  *
213  * Parameters  :
214  *          1  :  gw = pointer to a gateway structure (such as gw_default)
215  *          2  :  http = the http request and apropos headers
216  *          3  :  csp = Current client state (buffers, headers, etc...)
217  *
218  * Returns     :  -1 => failure, else a socket file descriptor.
219  *
220  *********************************************************************/
221 static int socks4_connect(const struct forward_spec * fwd,
222                           const char * target_host,
223                           int target_port,
224                           struct client_state *csp)
225 {
226    int web_server_addr;
227    char cbuf[BUFFER_SIZE];
228    char sbuf[BUFFER_SIZE];
229    struct socks_op    *c = (struct socks_op    *)cbuf;
230    struct socks_reply *s = (struct socks_reply *)sbuf;
231    size_t n;
232    size_t csiz;
233    int sfd;
234    int err = 0;
235    char *errstr;
236
237    if ((fwd->gateway_host == NULL) || (*fwd->gateway_host == '\0'))
238    {
239       log_error(LOG_LEVEL_CONNECT, "socks4_connect: NULL gateway host specified");
240       err = 1;
241    }
242
243    if (fwd->gateway_port <= 0)
244    {
245       log_error(LOG_LEVEL_CONNECT, "socks4_connect: invalid gateway port specified");
246       err = 1;
247    }
248
249    if (err)
250    {
251       errno = EINVAL;
252       return(-1);
253    }
254
255    /* build a socks request for connection to the web server */
256
257    strcpy((char *)&(c->userid), socks_userid);
258
259    csiz = sizeof(*c) + sizeof(socks_userid) - 1;
260
261    switch (fwd->type)
262    {
263       case SOCKS_4:
264          web_server_addr = htonl(resolve_hostname_to_ip(target_host));
265          if (web_server_addr == INADDR_NONE)
266          {
267             log_error(LOG_LEVEL_CONNECT, "socks4_connect: could not resolve target host %s", target_host);
268             return(-1);
269          }
270          break;
271       case SOCKS_4A:
272          web_server_addr = 0x00000001;
273          n = csiz + strlen(target_host) + 1;
274          if (n > sizeof(cbuf))
275          {
276             errno = EINVAL;
277             return(-1);
278          }
279          strcpy(cbuf + csiz, target_host);
280          csiz = n;
281          break;
282       default:
283          /* Should never get here */
284          log_error(LOG_LEVEL_FATAL, "SOCKS4 impossible internal error - bad SOCKS type.");
285          errno = EINVAL;
286          return(-1);
287    }
288
289    c->vn          = 4;
290    c->cd          = 1;
291    c->dstport[0]  = (target_port       >> 8  ) & 0xff;
292    c->dstport[1]  = (target_port             ) & 0xff;
293    c->dstip[0]    = (web_server_addr   >> 24 ) & 0xff;
294    c->dstip[1]    = (web_server_addr   >> 16 ) & 0xff;
295    c->dstip[2]    = (web_server_addr   >>  8 ) & 0xff;
296    c->dstip[3]    = (web_server_addr         ) & 0xff;
297
298    /* pass the request to the socks server */
299    sfd = connect_to(fwd->gateway_host, fwd->gateway_port, csp);
300
301    if (sfd < 0)
302    {
303       return(-1);
304    }
305
306    if (write_socket(sfd, (char *)c, csiz) != csiz)
307    {
308       log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation write failed...");
309       close_socket(sfd);
310       return(-1);
311    }
312
313    if (read_socket(sfd, sbuf, sizeof(sbuf)) != sizeof(*s))
314    {
315       log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation read failed...");
316       close_socket(sfd);
317       return(-1);
318    }
319
320    switch (s->cd)
321    {
322       case SOCKS_REQUEST_GRANTED:
323          return(sfd);
324          break;
325       case SOCKS_REQUEST_REJECT:
326          errstr = "SOCKS request rejected or failed";
327          errno = EINVAL;
328          break;
329       case SOCKS_REQUEST_IDENT_FAILED:
330          errstr = "SOCKS request rejected because "
331             "SOCKS server cannot connect to identd on the client";
332          errno = EACCES;
333          break;
334       case SOCKS_REQUEST_IDENT_CONFLICT:
335          errstr = "SOCKS request rejected because "
336             "the client program and identd report "
337             "different user-ids";
338          errno = EACCES;
339          break;
340       default:
341          errstr = cbuf;
342          errno = ENOENT;
343          sprintf(errstr,
344                  "SOCKS request rejected for reason code %d\n", s->cd);
345    }
346
347    log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s ...", errstr);
348
349    close_socket(sfd);
350    return(-1);
351
352 }
353
354
355 /*
356   Local Variables:
357   tab-width: 3
358   end:
359 */