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