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