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