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