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