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