1 /* $OpenBSD: socks.c,v 1.21 2015/03/26 21:19:51 tobias Exp $ */
4 * Copyright (c) 1999 Niklas Hallqvist. All rights reserved.
5 * Copyright (c) 2004, 2005 Damien Miller. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
41 #include <readpassphrase.h>
44 #define SOCKS_PORT "1080"
45 #define HTTP_PROXY_PORT "3128"
46 #define HTTP_MAXHDRS 64
49 #define SOCKS_NOAUTH 0
50 #define SOCKS_NOMETHOD 0xff
51 #define SOCKS_CONNECT 1
53 #define SOCKS_DOMAIN 3
56 int remote_connect(const char *, const char *, struct addrinfo);
57 int socks_connect(const char *, const char *, struct addrinfo,
58 const char *, const char *, struct addrinfo, int,
62 decode_addrport(const char *h, const char *p, struct sockaddr *addr,
63 socklen_t addrlen, int v4only, int numeric)
66 struct addrinfo hints, *res;
68 bzero(&hints, sizeof(hints));
69 hints.ai_family = v4only ? PF_INET : PF_UNSPEC;
70 hints.ai_flags = numeric ? AI_NUMERICHOST : 0;
71 hints.ai_socktype = SOCK_STREAM;
72 r = getaddrinfo(h, p, &hints, &res);
73 /* Don't fatal when attempting to convert a numeric address */
76 errx(1, "getaddrinfo(\"%.64s\", \"%.64s\"): %s", h, p,
81 if (addrlen < res->ai_addrlen) {
83 errx(1, "internal error: addrlen < res->ai_addrlen");
85 memcpy(addr, res->ai_addr, res->ai_addrlen);
91 proxy_read_line(int fd, char *buf, size_t bufsz)
97 errx(1, "proxy read too long");
98 if (atomicio(read, fd, buf + off, 1) != 1)
101 if (buf[off] == '\r')
103 if (buf[off] == '\n') {
113 getproxypass(const char *proxyuser, const char *proxyhost)
118 snprintf(prompt, sizeof(prompt), "Proxy password for %s@%s: ",
119 proxyuser, proxyhost);
120 if (readpassphrase(prompt, pw, sizeof(pw), RPP_REQUIRE_TTY) == NULL)
121 errx(1, "Unable to read proxy passphrase");
126 socks_connect(const char *host, const char *port,
127 struct addrinfo hints __attribute__ ((__unused__)),
128 const char *proxyhost, const char *proxyport, struct addrinfo proxyhints,
129 int socksv, const char *proxyuser)
131 int proxyfd, r, authretry = 0;
133 unsigned char buf[1024];
135 struct sockaddr_storage addr;
136 struct sockaddr_in *in4 = (struct sockaddr_in *)&addr;
137 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr;
138 in_port_t serverport;
139 const char *proxypass = NULL;
141 if (proxyport == NULL)
142 proxyport = (socksv == -1) ? HTTP_PROXY_PORT : SOCKS_PORT;
144 /* Abuse API to lookup port */
145 if (decode_addrport("0.0.0.0", port, (struct sockaddr *)&addr,
146 sizeof(addr), 1, 1) == -1)
147 errx(1, "unknown port \"%.64s\"", port);
148 serverport = in4->sin_port;
152 errx(1, "Too many authentication failures");
154 proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
160 if (decode_addrport(host, port, (struct sockaddr *)&addr,
161 sizeof(addr), 0, 1) == -1)
162 addr.ss_family = 0; /* used in switch below */
164 /* Version 5, one method: no authentication */
167 buf[2] = SOCKS_NOAUTH;
168 cnt = atomicio(vwrite, proxyfd, buf, 3);
170 err(1, "write failed (%zu/3)", cnt);
172 cnt = atomicio(read, proxyfd, buf, 2);
174 err(1, "read failed (%zu/3)", cnt);
176 if (buf[1] == SOCKS_NOMETHOD)
177 errx(1, "authentication method negotiation failed");
179 switch (addr.ss_family) {
181 /* Version 5, connect: domain name */
183 /* Max domain name length is 255 bytes */
186 errx(1, "host name too long for SOCKS5");
188 buf[1] = SOCKS_CONNECT;
190 buf[3] = SOCKS_DOMAIN;
192 memcpy(buf + 5, host, hlen);
193 memcpy(buf + 5 + hlen, &serverport, sizeof serverport);
197 /* Version 5, connect: IPv4 address */
199 buf[1] = SOCKS_CONNECT;
202 memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
203 memcpy(buf + 8, &in4->sin_port, sizeof in4->sin_port);
207 /* Version 5, connect: IPv6 address */
209 buf[1] = SOCKS_CONNECT;
212 memcpy(buf + 4, &in6->sin6_addr, sizeof in6->sin6_addr);
213 memcpy(buf + 20, &in6->sin6_port,
214 sizeof in6->sin6_port);
218 errx(1, "internal error: silly AF");
221 cnt = atomicio(vwrite, proxyfd, buf, wlen);
223 err(1, "write failed (%zu/%zu)", cnt, wlen);
225 cnt = atomicio(read, proxyfd, buf, 4);
227 err(1, "read failed (%zu/4)", cnt);
229 errx(1, "connection failed, SOCKS error %d", buf[1]);
232 cnt = atomicio(read, proxyfd, buf + 4, 6);
234 err(1, "read failed (%zu/6)", cnt);
237 cnt = atomicio(read, proxyfd, buf + 4, 18);
239 err(1, "read failed (%zu/18)", cnt);
242 errx(1, "connection failed, unsupported address type");
244 } else if (socksv == 4) {
245 /* This will exit on lookup failure */
246 decode_addrport(host, port, (struct sockaddr *)&addr,
251 buf[1] = SOCKS_CONNECT; /* connect */
252 memcpy(buf + 2, &in4->sin_port, sizeof in4->sin_port);
253 memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
254 buf[8] = 0; /* empty username */
257 cnt = atomicio(vwrite, proxyfd, buf, wlen);
259 err(1, "write failed (%zu/%zu)", cnt, wlen);
261 cnt = atomicio(read, proxyfd, buf, 8);
263 err(1, "read failed (%zu/8)", cnt);
265 errx(1, "connection failed, SOCKS error %d", buf[1]);
266 } else if (socksv == -1) {
267 /* HTTP proxy CONNECT */
269 /* Disallow bad chars in hostname */
270 if (strcspn(host, "\r\n\t []:") != strlen(host))
271 errx(1, "Invalid hostname");
273 /* Try to be sane about numeric IPv6 addresses */
274 if (strchr(host, ':') != NULL) {
275 r = snprintf(buf, sizeof(buf),
276 "CONNECT [%s]:%d HTTP/1.0\r\n",
277 host, ntohs(serverport));
279 r = snprintf(buf, sizeof(buf),
280 "CONNECT %s:%d HTTP/1.0\r\n",
281 host, ntohs(serverport));
283 if (r == -1 || (size_t)r >= sizeof(buf))
284 errx(1, "hostname too long");
287 cnt = atomicio(vwrite, proxyfd, buf, r);
289 err(1, "write failed (%zu/%d)", cnt, r);
294 proxypass = getproxypass(proxyuser, proxyhost);
295 r = snprintf(buf, sizeof(buf), "%s:%s",
296 proxyuser, proxypass);
297 if (r == -1 || (size_t)r >= sizeof(buf) ||
298 b64_ntop(buf, strlen(buf), resp,
300 errx(1, "Proxy username/password too long");
301 r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
302 "Basic %s\r\n", resp);
303 if (r == -1 || (size_t)r >= sizeof(buf))
304 errx(1, "Proxy auth response too long");
306 if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
307 err(1, "write failed (%zu/%d)", cnt, r);
310 /* Terminate headers */
311 if ((cnt = atomicio(vwrite, proxyfd, "\r\n", 2)) != 2)
312 err(1, "write failed (%zu/2)", cnt);
314 /* Read status reply */
315 proxy_read_line(proxyfd, buf, sizeof(buf));
316 if (proxyuser != NULL &&
317 strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
319 fprintf(stderr, "Proxy authentication "
324 } else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
325 strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
326 errx(1, "Proxy error: \"%s\"", buf);
328 /* Headers continue until we hit an empty line */
329 for (r = 0; r < HTTP_MAXHDRS; r++) {
330 proxy_read_line(proxyfd, buf, sizeof(buf));
335 errx(1, "Too many proxy headers received");
337 errx(1, "Unknown proxy protocol %d", socksv);