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