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