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