socks.c revision 1.1 1 1.1 christos /* $OpenBSD: socks.c,v 1.24 2016/06/27 14:43:04 deraadt Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1999 Niklas Hallqvist. All rights reserved.
5 1.1 christos * Copyright (c) 2004, 2005 Damien Miller. All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 christos */
27 1.1 christos
28 1.1 christos #include <sys/types.h>
29 1.1 christos #include <sys/socket.h>
30 1.1 christos #include <netinet/in.h>
31 1.1 christos #include <arpa/inet.h>
32 1.1 christos
33 1.1 christos #include <err.h>
34 1.1 christos #include <errno.h>
35 1.1 christos #include <netdb.h>
36 1.1 christos #include <stdio.h>
37 1.1 christos #include <stdlib.h>
38 1.1 christos #include <string.h>
39 1.1 christos #include <unistd.h>
40 1.1 christos #include <resolv.h>
41 1.1 christos #include <readpassphrase.h>
42 1.1 christos #include "atomicio.h"
43 1.1 christos
44 1.1 christos #define SOCKS_PORT "1080"
45 1.1 christos #define HTTP_PROXY_PORT "3128"
46 1.1 christos #define HTTP_MAXHDRS 64
47 1.1 christos #define SOCKS_V5 5
48 1.1 christos #define SOCKS_V4 4
49 1.1 christos #define SOCKS_NOAUTH 0
50 1.1 christos #define SOCKS_NOMETHOD 0xff
51 1.1 christos #define SOCKS_CONNECT 1
52 1.1 christos #define SOCKS_IPV4 1
53 1.1 christos #define SOCKS_DOMAIN 3
54 1.1 christos #define SOCKS_IPV6 4
55 1.1 christos
56 1.1 christos int remote_connect(const char *, const char *, struct addrinfo);
57 1.1 christos int socks_connect(const char *, const char *, struct addrinfo,
58 1.1 christos const char *, const char *, struct addrinfo, int,
59 1.1 christos const char *);
60 1.1 christos
61 1.1 christos static int
62 1.1 christos decode_addrport(const char *h, const char *p, struct sockaddr *addr,
63 1.1 christos socklen_t addrlen, int v4only, int numeric)
64 1.1 christos {
65 1.1 christos int r;
66 1.1 christos struct addrinfo hints, *res;
67 1.1 christos
68 1.1 christos bzero(&hints, sizeof(hints));
69 1.1 christos hints.ai_family = v4only ? PF_INET : PF_UNSPEC;
70 1.1 christos hints.ai_flags = numeric ? AI_NUMERICHOST : 0;
71 1.1 christos hints.ai_socktype = SOCK_STREAM;
72 1.1 christos r = getaddrinfo(h, p, &hints, &res);
73 1.1 christos /* Don't fatal when attempting to convert a numeric address */
74 1.1 christos if (r != 0) {
75 1.1 christos if (!numeric) {
76 1.1 christos errx(1, "getaddrinfo(\"%.64s\", \"%.64s\"): %s", h, p,
77 1.1 christos gai_strerror(r));
78 1.1 christos }
79 1.1 christos return (-1);
80 1.1 christos }
81 1.1 christos if (addrlen < res->ai_addrlen) {
82 1.1 christos freeaddrinfo(res);
83 1.1 christos errx(1, "internal error: addrlen < res->ai_addrlen");
84 1.1 christos }
85 1.1 christos memcpy(addr, res->ai_addr, res->ai_addrlen);
86 1.1 christos freeaddrinfo(res);
87 1.1 christos return (0);
88 1.1 christos }
89 1.1 christos
90 1.1 christos static int
91 1.1 christos proxy_read_line(int fd, char *buf, size_t bufsz)
92 1.1 christos {
93 1.1 christos size_t off;
94 1.1 christos
95 1.1 christos for(off = 0;;) {
96 1.1 christos if (off >= bufsz)
97 1.1 christos errx(1, "proxy read too long");
98 1.1 christos if (atomicio(read, fd, buf + off, 1) != 1)
99 1.1 christos err(1, "proxy read");
100 1.1 christos /* Skip CR */
101 1.1 christos if (buf[off] == '\r')
102 1.1 christos continue;
103 1.1 christos if (buf[off] == '\n') {
104 1.1 christos buf[off] = '\0';
105 1.1 christos break;
106 1.1 christos }
107 1.1 christos off++;
108 1.1 christos }
109 1.1 christos return (off);
110 1.1 christos }
111 1.1 christos
112 1.1 christos static const char *
113 1.1 christos getproxypass(const char *proxyuser, const char *proxyhost)
114 1.1 christos {
115 1.1 christos char prompt[512];
116 1.1 christos static char pw[256];
117 1.1 christos
118 1.1 christos snprintf(prompt, sizeof(prompt), "Proxy password for %s@%s: ",
119 1.1 christos proxyuser, proxyhost);
120 1.1 christos if (readpassphrase(prompt, pw, sizeof(pw), RPP_REQUIRE_TTY) == NULL)
121 1.1 christos errx(1, "Unable to read proxy passphrase");
122 1.1 christos return (pw);
123 1.1 christos }
124 1.1 christos
125 1.1 christos /*
126 1.1 christos * Error strings adapted from the generally accepted SOCKSv4 spec:
127 1.1 christos *
128 1.1 christos * http://ftp.icm.edu.pl/packages/socks/socks4/SOCKS4.protocol
129 1.1 christos */
130 1.1 christos static const char *
131 1.1 christos socks4_strerror(int e)
132 1.1 christos {
133 1.1 christos switch (e) {
134 1.1 christos case 90:
135 1.1 christos return "Succeeded";
136 1.1 christos case 91:
137 1.1 christos return "Request rejected or failed";
138 1.1 christos case 92:
139 1.1 christos return "SOCKS server cannot connect to identd on the client";
140 1.1 christos case 93:
141 1.1 christos return "Client program and identd report different user-ids";
142 1.1 christos default:
143 1.1 christos return "Unknown error";
144 1.1 christos }
145 1.1 christos }
146 1.1 christos
147 1.1 christos /*
148 1.1 christos * Error strings taken almost directly from RFC 1928.
149 1.1 christos */
150 1.1 christos static const char *
151 1.1 christos socks5_strerror(int e)
152 1.1 christos {
153 1.1 christos switch (e) {
154 1.1 christos case 0:
155 1.1 christos return "Succeeded";
156 1.1 christos case 1:
157 1.1 christos return "General SOCKS server failure";
158 1.1 christos case 2:
159 1.1 christos return "Connection not allowed by ruleset";
160 1.1 christos case 3:
161 1.1 christos return "Network unreachable";
162 1.1 christos case 4:
163 1.1 christos return "Host unreachable";
164 1.1 christos case 5:
165 1.1 christos return "Connection refused";
166 1.1 christos case 6:
167 1.1 christos return "TTL expired";
168 1.1 christos case 7:
169 1.1 christos return "Command not supported";
170 1.1 christos case 8:
171 1.1 christos return "Address type not supported";
172 1.1 christos default:
173 1.1 christos return "Unknown error";
174 1.1 christos }
175 1.1 christos }
176 1.1 christos
177 1.1 christos int
178 1.1 christos socks_connect(const char *host, const char *port,
179 1.1 christos struct addrinfo hints __attribute__ ((__unused__)),
180 1.1 christos const char *proxyhost, const char *proxyport, struct addrinfo proxyhints,
181 1.1 christos int socksv, const char *proxyuser)
182 1.1 christos {
183 1.1 christos int proxyfd, r, authretry = 0;
184 1.1 christos size_t hlen, wlen;
185 1.1 christos unsigned char buf[1024];
186 1.1 christos size_t cnt;
187 1.1 christos struct sockaddr_storage addr;
188 1.1 christos struct sockaddr_in *in4 = (struct sockaddr_in *)&addr;
189 1.1 christos struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr;
190 1.1 christos in_port_t serverport;
191 1.1 christos const char *proxypass = NULL;
192 1.1 christos
193 1.1 christos if (proxyport == NULL)
194 1.1 christos proxyport = (socksv == -1) ? HTTP_PROXY_PORT : SOCKS_PORT;
195 1.1 christos
196 1.1 christos /* Abuse API to lookup port */
197 1.1 christos if (decode_addrport("0.0.0.0", port, (struct sockaddr *)&addr,
198 1.1 christos sizeof(addr), 1, 1) == -1)
199 1.1 christos errx(1, "unknown port \"%.64s\"", port);
200 1.1 christos serverport = in4->sin_port;
201 1.1 christos
202 1.1 christos again:
203 1.1 christos if (authretry++ > 3)
204 1.1 christos errx(1, "Too many authentication failures");
205 1.1 christos
206 1.1 christos proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
207 1.1 christos
208 1.1 christos if (proxyfd < 0)
209 1.1 christos return (-1);
210 1.1 christos
211 1.1 christos if (socksv == 5) {
212 1.1 christos if (decode_addrport(host, port, (struct sockaddr *)&addr,
213 1.1 christos sizeof(addr), 0, 1) == -1)
214 1.1 christos addr.ss_family = 0; /* used in switch below */
215 1.1 christos
216 1.1 christos /* Version 5, one method: no authentication */
217 1.1 christos buf[0] = SOCKS_V5;
218 1.1 christos buf[1] = 1;
219 1.1 christos buf[2] = SOCKS_NOAUTH;
220 1.1 christos cnt = atomicio(vwrite, proxyfd, buf, 3);
221 1.1 christos if (cnt != 3)
222 1.1 christos err(1, "write failed (%zu/3)", cnt);
223 1.1 christos
224 1.1 christos cnt = atomicio(read, proxyfd, buf, 2);
225 1.1 christos if (cnt != 2)
226 1.1 christos err(1, "read failed (%zu/3)", cnt);
227 1.1 christos
228 1.1 christos if (buf[1] == SOCKS_NOMETHOD)
229 1.1 christos errx(1, "authentication method negotiation failed");
230 1.1 christos
231 1.1 christos switch (addr.ss_family) {
232 1.1 christos case 0:
233 1.1 christos /* Version 5, connect: domain name */
234 1.1 christos
235 1.1 christos /* Max domain name length is 255 bytes */
236 1.1 christos hlen = strlen(host);
237 1.1 christos if (hlen > 255)
238 1.1 christos errx(1, "host name too long for SOCKS5");
239 1.1 christos buf[0] = SOCKS_V5;
240 1.1 christos buf[1] = SOCKS_CONNECT;
241 1.1 christos buf[2] = 0;
242 1.1 christos buf[3] = SOCKS_DOMAIN;
243 1.1 christos buf[4] = hlen;
244 1.1 christos memcpy(buf + 5, host, hlen);
245 1.1 christos memcpy(buf + 5 + hlen, &serverport, sizeof serverport);
246 1.1 christos wlen = 7 + hlen;
247 1.1 christos break;
248 1.1 christos case AF_INET:
249 1.1 christos /* Version 5, connect: IPv4 address */
250 1.1 christos buf[0] = SOCKS_V5;
251 1.1 christos buf[1] = SOCKS_CONNECT;
252 1.1 christos buf[2] = 0;
253 1.1 christos buf[3] = SOCKS_IPV4;
254 1.1 christos memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
255 1.1 christos memcpy(buf + 8, &in4->sin_port, sizeof in4->sin_port);
256 1.1 christos wlen = 10;
257 1.1 christos break;
258 1.1 christos case AF_INET6:
259 1.1 christos /* Version 5, connect: IPv6 address */
260 1.1 christos buf[0] = SOCKS_V5;
261 1.1 christos buf[1] = SOCKS_CONNECT;
262 1.1 christos buf[2] = 0;
263 1.1 christos buf[3] = SOCKS_IPV6;
264 1.1 christos memcpy(buf + 4, &in6->sin6_addr, sizeof in6->sin6_addr);
265 1.1 christos memcpy(buf + 20, &in6->sin6_port,
266 1.1 christos sizeof in6->sin6_port);
267 1.1 christos wlen = 22;
268 1.1 christos break;
269 1.1 christos default:
270 1.1 christos errx(1, "internal error: silly AF");
271 1.1 christos }
272 1.1 christos
273 1.1 christos cnt = atomicio(vwrite, proxyfd, buf, wlen);
274 1.1 christos if (cnt != wlen)
275 1.1 christos err(1, "write failed (%zu/%zu)", cnt, wlen);
276 1.1 christos
277 1.1 christos cnt = atomicio(read, proxyfd, buf, 4);
278 1.1 christos if (cnt != 4)
279 1.1 christos err(1, "read failed (%zu/4)", cnt);
280 1.1 christos if (buf[1] != 0) {
281 1.1 christos errx(1, "connection failed, SOCKSv5 error: %s",
282 1.1 christos socks5_strerror(buf[1]));
283 1.1 christos }
284 1.1 christos switch (buf[3]) {
285 1.1 christos case SOCKS_IPV4:
286 1.1 christos cnt = atomicio(read, proxyfd, buf + 4, 6);
287 1.1 christos if (cnt != 6)
288 1.1 christos err(1, "read failed (%zu/6)", cnt);
289 1.1 christos break;
290 1.1 christos case SOCKS_IPV6:
291 1.1 christos cnt = atomicio(read, proxyfd, buf + 4, 18);
292 1.1 christos if (cnt != 18)
293 1.1 christos err(1, "read failed (%zu/18)", cnt);
294 1.1 christos break;
295 1.1 christos default:
296 1.1 christos errx(1, "connection failed, unsupported address type");
297 1.1 christos }
298 1.1 christos } else if (socksv == 4) {
299 1.1 christos /* This will exit on lookup failure */
300 1.1 christos decode_addrport(host, port, (struct sockaddr *)&addr,
301 1.1 christos sizeof(addr), 1, 0);
302 1.1 christos
303 1.1 christos /* Version 4 */
304 1.1 christos buf[0] = SOCKS_V4;
305 1.1 christos buf[1] = SOCKS_CONNECT; /* connect */
306 1.1 christos memcpy(buf + 2, &in4->sin_port, sizeof in4->sin_port);
307 1.1 christos memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
308 1.1 christos buf[8] = 0; /* empty username */
309 1.1 christos wlen = 9;
310 1.1 christos
311 1.1 christos cnt = atomicio(vwrite, proxyfd, buf, wlen);
312 1.1 christos if (cnt != wlen)
313 1.1 christos err(1, "write failed (%zu/%zu)", cnt, wlen);
314 1.1 christos
315 1.1 christos cnt = atomicio(read, proxyfd, buf, 8);
316 1.1 christos if (cnt != 8)
317 1.1 christos err(1, "read failed (%zu/8)", cnt);
318 1.1 christos if (buf[1] != 90) {
319 1.1 christos errx(1, "connection failed, SOCKSv4 error: %s",
320 1.1 christos socks4_strerror(buf[1]));
321 1.1 christos }
322 1.1 christos } else if (socksv == -1) {
323 1.1 christos /* HTTP proxy CONNECT */
324 1.1 christos
325 1.1 christos /* Disallow bad chars in hostname */
326 1.1 christos if (strcspn(host, "\r\n\t []:") != strlen(host))
327 1.1 christos errx(1, "Invalid hostname");
328 1.1 christos
329 1.1 christos /* Try to be sane about numeric IPv6 addresses */
330 1.1 christos if (strchr(host, ':') != NULL) {
331 1.1 christos r = snprintf(buf, sizeof(buf),
332 1.1 christos "CONNECT [%s]:%d HTTP/1.0\r\n",
333 1.1 christos host, ntohs(serverport));
334 1.1 christos } else {
335 1.1 christos r = snprintf(buf, sizeof(buf),
336 1.1 christos "CONNECT %s:%d HTTP/1.0\r\n",
337 1.1 christos host, ntohs(serverport));
338 1.1 christos }
339 1.1 christos if (r == -1 || (size_t)r >= sizeof(buf))
340 1.1 christos errx(1, "hostname too long");
341 1.1 christos r = strlen(buf);
342 1.1 christos
343 1.1 christos cnt = atomicio(vwrite, proxyfd, buf, r);
344 1.1 christos if (cnt != r)
345 1.1 christos err(1, "write failed (%zu/%d)", cnt, r);
346 1.1 christos
347 1.1 christos if (authretry > 1) {
348 1.1 christos char resp[1024];
349 1.1 christos
350 1.1 christos proxypass = getproxypass(proxyuser, proxyhost);
351 1.1 christos r = snprintf(buf, sizeof(buf), "%s:%s",
352 1.1 christos proxyuser, proxypass);
353 1.1 christos if (r == -1 || (size_t)r >= sizeof(buf) ||
354 1.1 christos b64_ntop(buf, strlen(buf), resp,
355 1.1 christos sizeof(resp)) == -1)
356 1.1 christos errx(1, "Proxy username/password too long");
357 1.1 christos r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
358 1.1 christos "Basic %s\r\n", resp);
359 1.1 christos if (r == -1 || (size_t)r >= sizeof(buf))
360 1.1 christos errx(1, "Proxy auth response too long");
361 1.1 christos r = strlen(buf);
362 1.1 christos if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
363 1.1 christos err(1, "write failed (%zu/%d)", cnt, r);
364 1.1 christos }
365 1.1 christos
366 1.1 christos /* Terminate headers */
367 1.1 christos if ((cnt = atomicio(vwrite, proxyfd, "\r\n", 2)) != 2)
368 1.1 christos err(1, "write failed (%zu/2)", cnt);
369 1.1 christos
370 1.1 christos /* Read status reply */
371 1.1 christos proxy_read_line(proxyfd, buf, sizeof(buf));
372 1.1 christos if (proxyuser != NULL &&
373 1.1 christos strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
374 1.1 christos if (authretry > 1) {
375 1.1 christos fprintf(stderr, "Proxy authentication "
376 1.1 christos "failed\n");
377 1.1 christos }
378 1.1 christos close(proxyfd);
379 1.1 christos goto again;
380 1.1 christos } else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
381 1.1 christos strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
382 1.1 christos errx(1, "Proxy error: \"%s\"", buf);
383 1.1 christos
384 1.1 christos /* Headers continue until we hit an empty line */
385 1.1 christos for (r = 0; r < HTTP_MAXHDRS; r++) {
386 1.1 christos proxy_read_line(proxyfd, buf, sizeof(buf));
387 1.1 christos if (*buf == '\0')
388 1.1 christos break;
389 1.1 christos }
390 1.1 christos if (*buf != '\0')
391 1.1 christos errx(1, "Too many proxy headers received");
392 1.1 christos } else
393 1.1 christos errx(1, "Unknown proxy protocol %d", socksv);
394 1.1 christos
395 1.1 christos return (proxyfd);
396 1.1 christos }
397