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