http://i.j.b/ijb-send-banner changed to http://i.j.b/send-banner
[privoxy.git] / gateway.c
1 const char gateway_rcs[] = "$Id: gateway.c,v 1.1 2001/05/13 21:57:06 administrator Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /home/administrator/cvs/ijb/gateway.c,v $
5  *
6  * Purpose     :  Contains functions to connect to a server, possibly
7  *                using a "gateway" (i.e. HTTP proxy and/or SOCKS4
8  *                proxy).  Also contains the list of gateway types.
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  *
38  *********************************************************************/
39 \f
40
41 #include "config.h"
42
43 #include <stdio.h>
44 #include <sys/types.h>
45 #include <errno.h>
46
47 #ifdef _WIN32
48 #include <winsock2.h>
49 #endif /* def _WIN32 */
50
51 #include "jcc.h"
52 #include "errlog.h"
53 #include "jbsockets.h"
54 #include "gateway.h"
55
56 const char gateway_h_rcs[] = GATEWAY_H_VERSION;
57
58 #define SOCKS_4      40    /* original SOCKS 4 protocol */
59 #define SOCKS_4A     41    /* as modified for hosts w/o external DNS */
60
61 const struct gateway gateways[] = {
62    /* type        function          gw type/host/port, fw host/port */
63    { "direct",    direct_connect,   0,          NULL, 0,    NULL, 0 },
64    { ".",         direct_connect,   0,          NULL, 0,    NULL, 0 },
65    { "socks",     socks4_connect,   SOCKS_4,    NULL, 1080, NULL, 0 },
66    { "socks4",    socks4_connect,   SOCKS_4,    NULL, 1080, NULL, 0 },
67    { "socks4a",   socks4_connect,   SOCKS_4A,   NULL, 1080, NULL, 0 },
68    { NULL,        NULL,             0,          NULL, 0,    NULL, 0 }
69 };
70
71 const struct gateway *gw_default = gateways; /* direct */
72
73
74 #define SOCKS_REQUEST_GRANTED          90
75 #define SOCKS_REQUEST_REJECT           91
76 #define SOCKS_REQUEST_IDENT_FAILED     92
77 #define SOCKS_REQUEST_IDENT_CONFLICT   93
78
79 /* structure of a socks client operation */
80 struct socks_op {
81    unsigned char vn;          /* socks version number */
82    unsigned char cd;          /* command code */
83    unsigned char dstport[2];  /* destination port */
84    unsigned char dstip[4];    /* destination address */
85    unsigned char userid;      /* first byte of userid */
86    /* more bytes of the userid follow, terminated by a NULL */
87 };
88
89 /* structure of a socks server reply */
90 struct socks_reply {
91    unsigned char vn;          /* socks version number */
92    unsigned char cd;          /* command code */
93    unsigned char dstport[2];  /* destination port */
94    unsigned char dstip[4];    /* destination address */
95 };
96
97 static const char socks_userid[] = "anonymous";
98
99
100 /*********************************************************************
101  *
102  * Function    :  direct_connect
103  *
104  * Description :  Direct how we connect to the web.  This can be:
105  *                directly    : no forwarding, or
106  *                indirectly  : through another proxy such as squid.
107  *
108  * Parameters  :
109  *          1  :  gw = pointer to a gateway structure (such as gw_default)
110  *          2  :  http = the http request and apropos headers
111  *          3  :  csp = Current client state (buffers, headers, etc...)
112  *
113  * Returns     :  -1 => failure, else it is the socket file descriptor.
114  *
115  *********************************************************************/
116 int direct_connect(const struct gateway *gw, struct http_request *http, struct client_state *csp)
117 {
118    if (gw->forward_host)
119    {
120       return(connect_to(gw->forward_host, gw->forward_port, csp));
121    }
122    else
123    {
124       return(connect_to(http->host, http->port, csp));
125    }
126
127 }
128
129
130 /*********************************************************************
131  *
132  * Function    :  socks4_connect
133  *
134  * Description :  Connect to the SOCKS server, and connect through
135  *                it to the web server or web proxy.   This handles
136  *                all the SOCKS negotiation, and returns a file
137  *                descriptor for a socket which can be treated as a
138  *                normal (non-SOCKS) socket.
139  *
140  * Parameters  :
141  *          1  :  gw = pointer to a gateway structure (such as gw_default)
142  *          2  :  http = the http request and apropos headers
143  *          3  :  csp = Current client state (buffers, headers, etc...)
144  *
145  * Returns     :  -1 => failure, else a socket file descriptor.
146  *
147  *********************************************************************/
148 int socks4_connect(const struct gateway *gw, struct http_request *http, struct client_state *csp)
149 {
150    int web_server_addr;
151    unsigned char cbuf[BUFSIZ];
152    unsigned char sbuf[BUFSIZ];
153    struct socks_op    *c = (struct socks_op    *)cbuf;
154    struct socks_reply *s = (struct socks_reply *)sbuf;
155    int n, csiz, sfd, target_port;
156    int err = 0;
157    char *errstr, *target_host;
158
159    if ((gw->gateway_host == NULL) || (*gw->gateway_host == '\0'))
160    {
161       log_error(LOG_LEVEL_CONNECT, "socks4_connect: NULL gateway host specified");
162       err = 1;
163    }
164
165    if (gw->gateway_port <= 0)
166    {
167       log_error(LOG_LEVEL_CONNECT, "socks4_connect: invalid gateway port specified");
168       err = 1;
169    }
170
171    if (err)
172    {
173       errno = EINVAL;
174       return(-1);
175    }
176
177    if (gw->forward_host)
178    {
179       target_host = gw->forward_host;
180       target_port = gw->forward_port;
181    }
182    else
183    {
184       target_host = http->host;
185       target_port = http->port;
186    }
187
188    /* build a socks request for connection to the web server */
189
190    strcpy((char *)&(c->userid), socks_userid);
191
192    csiz = sizeof(*c) + sizeof(socks_userid) - 1;
193
194    switch (gw->type)
195    {
196       case SOCKS_4:
197          web_server_addr = htonl(resolve_hostname_to_ip(target_host));
198          break;
199       case SOCKS_4A:
200          web_server_addr = 0x00000001;
201          n = csiz + strlen(target_host) + 1;
202          if (n > sizeof(cbuf))
203          {
204             errno = EINVAL;
205             return(-1);
206          }
207          strcpy(((char *)cbuf) + csiz, http->host);
208          csiz = n;
209          break;
210       default:
211          /* Should never get here */
212          log_error(LOG_LEVEL_ERROR, "SOCKS4 impossible internal error - bad SOCKS type.");
213          errno = EINVAL;
214          return(-1);
215    }
216
217    c->vn          = 4;
218    c->cd          = 1;
219    c->dstport[0]  = (target_port       >> 8  ) & 0xff;
220    c->dstport[1]  = (target_port             ) & 0xff;
221    c->dstip[0]    = (web_server_addr   >> 24 ) & 0xff;
222    c->dstip[1]    = (web_server_addr   >> 16 ) & 0xff;
223    c->dstip[2]    = (web_server_addr   >>  8 ) & 0xff;
224    c->dstip[3]    = (web_server_addr         ) & 0xff;
225
226    /* pass the request to the socks server */
227    sfd = connect_to(gw->gateway_host, gw->gateway_port, csp);
228
229    if (sfd < 0)
230    {
231       return(-1);
232    }
233
234    if ((n = write_socket(sfd, (char *)c, csiz)) != csiz)
235    {
236       log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation write failed...");
237       close_socket(sfd);
238       return(-1);
239    }
240
241    if ((n = read_socket(sfd, sbuf, sizeof(sbuf))) != sizeof(*s))
242    {
243       log_error(LOG_LEVEL_CONNECT, "SOCKS4 negotiation read failed...");
244       close_socket(sfd);
245       return(-1);
246    }
247
248    switch (s->cd)
249    {
250       case SOCKS_REQUEST_GRANTED:
251          return(sfd);
252          break;
253       case SOCKS_REQUEST_REJECT:
254          errstr = "SOCKS request rejected or failed";
255          errno = EINVAL;
256          break;
257       case SOCKS_REQUEST_IDENT_FAILED:
258          errstr = "SOCKS request rejected because "
259             "SOCKS server cannot connect to identd on the client";
260          errno = EACCES;
261          break;
262       case SOCKS_REQUEST_IDENT_CONFLICT:
263          errstr = "SOCKS request rejected because "
264             "the client program and identd report "
265             "different user-ids";
266          errno = EACCES;
267          break;
268       default:
269          errstr = (char *) cbuf;
270          errno = ENOENT;
271          sprintf(errstr,
272                  "SOCKS request rejected for reason code %d\n", s->cd);
273    }
274
275    log_error(LOG_LEVEL_CONNECT, "socks4_connect: %s ...", errstr);
276
277    close_socket(sfd);
278    return(-1);
279
280 }
281
282
283 /*
284   Local Variables:
285   tab-width: 3
286   end:
287 */