xref: /freebsd-src/contrib/netcat/socks.c (revision 11d38a5764295585a2472d5e861fa8abe1a11eb2)
1*100e2a06SXin LI /*	$OpenBSD: socks.c,v 1.21 2015/03/26 21:19:51 tobias Exp $	*/
28c384020SXin LI 
38c384020SXin LI /*
48c384020SXin LI  * Copyright (c) 1999 Niklas Hallqvist.  All rights reserved.
57a997a69SXin LI  * Copyright (c) 2004, 2005 Damien Miller.  All rights reserved.
68c384020SXin LI  *
78c384020SXin LI  * Redistribution and use in source and binary forms, with or without
88c384020SXin LI  * modification, are permitted provided that the following conditions
98c384020SXin LI  * are met:
108c384020SXin LI  * 1. Redistributions of source code must retain the above copyright
118c384020SXin LI  *    notice, this list of conditions and the following disclaimer.
128c384020SXin LI  * 2. Redistributions in binary form must reproduce the above copyright
138c384020SXin LI  *    notice, this list of conditions and the following disclaimer in the
148c384020SXin LI  *    documentation and/or other materials provided with the distribution.
158c384020SXin LI  *
168c384020SXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178c384020SXin LI  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188c384020SXin LI  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198c384020SXin LI  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208c384020SXin LI  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218c384020SXin LI  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228c384020SXin LI  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238c384020SXin LI  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248c384020SXin LI  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258c384020SXin LI  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268c384020SXin LI  */
278c384020SXin LI 
288c384020SXin LI #include <sys/types.h>
298c384020SXin LI #include <sys/socket.h>
308c384020SXin LI #include <netinet/in.h>
318c384020SXin LI #include <arpa/inet.h>
328c384020SXin LI 
338c384020SXin LI #include <err.h>
348c384020SXin LI #include <errno.h>
358c384020SXin LI #include <netdb.h>
368c384020SXin LI #include <stdio.h>
378c384020SXin LI #include <stdlib.h>
388c384020SXin LI #include <string.h>
398c384020SXin LI #include <unistd.h>
407a997a69SXin LI #include <resolv.h>
417a997a69SXin LI #include <readpassphrase.h>
427a997a69SXin LI #include "atomicio.h"
438c384020SXin LI 
448c384020SXin LI #define SOCKS_PORT	"1080"
458c384020SXin LI #define HTTP_PROXY_PORT	"3128"
468c384020SXin LI #define HTTP_MAXHDRS	64
478c384020SXin LI #define SOCKS_V5	5
488c384020SXin LI #define SOCKS_V4	4
498c384020SXin LI #define SOCKS_NOAUTH	0
508c384020SXin LI #define SOCKS_NOMETHOD	0xff
518c384020SXin LI #define SOCKS_CONNECT	1
528c384020SXin LI #define SOCKS_IPV4	1
537a997a69SXin LI #define SOCKS_DOMAIN	3
547a997a69SXin LI #define SOCKS_IPV6	4
558c384020SXin LI 
567a997a69SXin LI int	remote_connect(const char *, const char *, struct addrinfo);
577a997a69SXin LI int	socks_connect(const char *, const char *, struct addrinfo,
587a997a69SXin LI 	    const char *, const char *, struct addrinfo, int,
597a997a69SXin LI 	    const char *);
608c384020SXin LI 
617a997a69SXin LI static int
decode_addrport(const char * h,const char * p,struct sockaddr * addr,socklen_t addrlen,int v4only,int numeric)627a997a69SXin LI decode_addrport(const char *h, const char *p, struct sockaddr *addr,
637a997a69SXin LI     socklen_t addrlen, int v4only, int numeric)
648c384020SXin LI {
657a997a69SXin LI 	int r;
667a997a69SXin LI 	struct addrinfo hints, *res;
678c384020SXin LI 
687a997a69SXin LI 	bzero(&hints, sizeof(hints));
697a997a69SXin LI 	hints.ai_family = v4only ? PF_INET : PF_UNSPEC;
707a997a69SXin LI 	hints.ai_flags = numeric ? AI_NUMERICHOST : 0;
717a997a69SXin LI 	hints.ai_socktype = SOCK_STREAM;
727a997a69SXin LI 	r = getaddrinfo(h, p, &hints, &res);
737a997a69SXin LI 	/* Don't fatal when attempting to convert a numeric address */
747a997a69SXin LI 	if (r != 0) {
757a997a69SXin LI 		if (!numeric) {
767a997a69SXin LI 			errx(1, "getaddrinfo(\"%.64s\", \"%.64s\"): %s", h, p,
777a997a69SXin LI 			    gai_strerror(r));
788c384020SXin LI 		}
797a997a69SXin LI 		return (-1);
808c384020SXin LI 	}
817a997a69SXin LI 	if (addrlen < res->ai_addrlen) {
827a997a69SXin LI 		freeaddrinfo(res);
837a997a69SXin LI 		errx(1, "internal error: addrlen < res->ai_addrlen");
847a997a69SXin LI 	}
857a997a69SXin LI 	memcpy(addr, res->ai_addr, res->ai_addrlen);
867a997a69SXin LI 	freeaddrinfo(res);
877a997a69SXin LI 	return (0);
888c384020SXin LI }
898c384020SXin LI 
908c384020SXin LI static int
proxy_read_line(int fd,char * buf,size_t bufsz)917a997a69SXin LI proxy_read_line(int fd, char *buf, size_t bufsz)
928c384020SXin LI {
937a997a69SXin LI 	size_t off;
948c384020SXin LI 
958c384020SXin LI 	for(off = 0;;) {
968c384020SXin LI 		if (off >= bufsz)
978c384020SXin LI 			errx(1, "proxy read too long");
987a997a69SXin LI 		if (atomicio(read, fd, buf + off, 1) != 1)
998c384020SXin LI 			err(1, "proxy read");
1008c384020SXin LI 		/* Skip CR */
1018c384020SXin LI 		if (buf[off] == '\r')
1028c384020SXin LI 			continue;
1038c384020SXin LI 		if (buf[off] == '\n') {
1048c384020SXin LI 			buf[off] = '\0';
1058c384020SXin LI 			break;
1068c384020SXin LI 		}
1078c384020SXin LI 		off++;
1088c384020SXin LI 	}
1098c384020SXin LI 	return (off);
1108c384020SXin LI }
1118c384020SXin LI 
1127a997a69SXin LI static const char *
getproxypass(const char * proxyuser,const char * proxyhost)1137a997a69SXin LI getproxypass(const char *proxyuser, const char *proxyhost)
1148c384020SXin LI {
1157a997a69SXin LI 	char prompt[512];
1167a997a69SXin LI 	static char pw[256];
1177a997a69SXin LI 
1187a997a69SXin LI 	snprintf(prompt, sizeof(prompt), "Proxy password for %s@%s: ",
1197a997a69SXin LI 	   proxyuser, proxyhost);
1207a997a69SXin LI 	if (readpassphrase(prompt, pw, sizeof(pw), RPP_REQUIRE_TTY) == NULL)
1217a997a69SXin LI 		errx(1, "Unable to read proxy passphrase");
1227a997a69SXin LI 	return (pw);
1237a997a69SXin LI }
1247a997a69SXin LI 
1257a997a69SXin LI int
socks_connect(const char * host,const char * port,struct addrinfo hints,const char * proxyhost,const char * proxyport,struct addrinfo proxyhints,int socksv,const char * proxyuser)1267a997a69SXin LI socks_connect(const char *host, const char *port,
1277a997a69SXin LI     struct addrinfo hints __attribute__ ((__unused__)),
1287a997a69SXin LI     const char *proxyhost, const char *proxyport, struct addrinfo proxyhints,
1297a997a69SXin LI     int socksv, const char *proxyuser)
1307a997a69SXin LI {
1317a997a69SXin LI 	int proxyfd, r, authretry = 0;
1327a997a69SXin LI 	size_t hlen, wlen;
1338c384020SXin LI 	unsigned char buf[1024];
1347a997a69SXin LI 	size_t cnt;
1357a997a69SXin LI 	struct sockaddr_storage addr;
1367a997a69SXin LI 	struct sockaddr_in *in4 = (struct sockaddr_in *)&addr;
1377a997a69SXin LI 	struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr;
1388c384020SXin LI 	in_port_t serverport;
1397a997a69SXin LI 	const char *proxypass = NULL;
1408c384020SXin LI 
1418c384020SXin LI 	if (proxyport == NULL)
1428c384020SXin LI 		proxyport = (socksv == -1) ? HTTP_PROXY_PORT : SOCKS_PORT;
1438c384020SXin LI 
1447a997a69SXin LI 	/* Abuse API to lookup port */
1457a997a69SXin LI 	if (decode_addrport("0.0.0.0", port, (struct sockaddr *)&addr,
1467a997a69SXin LI 	    sizeof(addr), 1, 1) == -1)
1477a997a69SXin LI 		errx(1, "unknown port \"%.64s\"", port);
1487a997a69SXin LI 	serverport = in4->sin_port;
1497a997a69SXin LI 
1507a997a69SXin LI  again:
1517a997a69SXin LI 	if (authretry++ > 3)
1527a997a69SXin LI 		errx(1, "Too many authentication failures");
1537a997a69SXin LI 
1548c384020SXin LI 	proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
1558c384020SXin LI 
1568c384020SXin LI 	if (proxyfd < 0)
1577a997a69SXin LI 		return (-1);
1588c384020SXin LI 
1598c384020SXin LI 	if (socksv == 5) {
1607a997a69SXin LI 		if (decode_addrport(host, port, (struct sockaddr *)&addr,
1617a997a69SXin LI 		    sizeof(addr), 0, 1) == -1)
1627a997a69SXin LI 			addr.ss_family = 0; /* used in switch below */
1637a997a69SXin LI 
1648c384020SXin LI 		/* Version 5, one method: no authentication */
1658c384020SXin LI 		buf[0] = SOCKS_V5;
1668c384020SXin LI 		buf[1] = 1;
1678c384020SXin LI 		buf[2] = SOCKS_NOAUTH;
1687a997a69SXin LI 		cnt = atomicio(vwrite, proxyfd, buf, 3);
1698c384020SXin LI 		if (cnt != 3)
1704f2bbc00SXin LI 			err(1, "write failed (%zu/3)", cnt);
1718c384020SXin LI 
1727a997a69SXin LI 		cnt = atomicio(read, proxyfd, buf, 2);
1737a997a69SXin LI 		if (cnt != 2)
1744f2bbc00SXin LI 			err(1, "read failed (%zu/3)", cnt);
1757a997a69SXin LI 
1768c384020SXin LI 		if (buf[1] == SOCKS_NOMETHOD)
1778c384020SXin LI 			errx(1, "authentication method negotiation failed");
1788c384020SXin LI 
1797a997a69SXin LI 		switch (addr.ss_family) {
1807a997a69SXin LI 		case 0:
1817a997a69SXin LI 			/* Version 5, connect: domain name */
1827a997a69SXin LI 
1837a997a69SXin LI 			/* Max domain name length is 255 bytes */
1847a997a69SXin LI 			hlen = strlen(host);
1857a997a69SXin LI 			if (hlen > 255)
1867a997a69SXin LI 				errx(1, "host name too long for SOCKS5");
1877a997a69SXin LI 			buf[0] = SOCKS_V5;
1887a997a69SXin LI 			buf[1] = SOCKS_CONNECT;
1897a997a69SXin LI 			buf[2] = 0;
1907a997a69SXin LI 			buf[3] = SOCKS_DOMAIN;
1917a997a69SXin LI 			buf[4] = hlen;
1927a997a69SXin LI 			memcpy(buf + 5, host, hlen);
1937a997a69SXin LI 			memcpy(buf + 5 + hlen, &serverport, sizeof serverport);
1947a997a69SXin LI 			wlen = 7 + hlen;
1957a997a69SXin LI 			break;
1967a997a69SXin LI 		case AF_INET:
1978c384020SXin LI 			/* Version 5, connect: IPv4 address */
1988c384020SXin LI 			buf[0] = SOCKS_V5;
1998c384020SXin LI 			buf[1] = SOCKS_CONNECT;
2008c384020SXin LI 			buf[2] = 0;
2018c384020SXin LI 			buf[3] = SOCKS_IPV4;
2027a997a69SXin LI 			memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
2037a997a69SXin LI 			memcpy(buf + 8, &in4->sin_port, sizeof in4->sin_port);
2047a997a69SXin LI 			wlen = 10;
2057a997a69SXin LI 			break;
2067a997a69SXin LI 		case AF_INET6:
2077a997a69SXin LI 			/* Version 5, connect: IPv6 address */
2087a997a69SXin LI 			buf[0] = SOCKS_V5;
2097a997a69SXin LI 			buf[1] = SOCKS_CONNECT;
2107a997a69SXin LI 			buf[2] = 0;
2117a997a69SXin LI 			buf[3] = SOCKS_IPV6;
2127a997a69SXin LI 			memcpy(buf + 4, &in6->sin6_addr, sizeof in6->sin6_addr);
2137a997a69SXin LI 			memcpy(buf + 20, &in6->sin6_port,
2147a997a69SXin LI 			    sizeof in6->sin6_port);
2157a997a69SXin LI 			wlen = 22;
2167a997a69SXin LI 			break;
2177a997a69SXin LI 		default:
2187a997a69SXin LI 			errx(1, "internal error: silly AF");
2197a997a69SXin LI 		}
2208c384020SXin LI 
2217a997a69SXin LI 		cnt = atomicio(vwrite, proxyfd, buf, wlen);
2227a997a69SXin LI 		if (cnt != wlen)
2234f2bbc00SXin LI 			err(1, "write failed (%zu/%zu)", cnt, wlen);
2248c384020SXin LI 
22559c7ad52SXin LI 		cnt = atomicio(read, proxyfd, buf, 4);
22659c7ad52SXin LI 		if (cnt != 4)
22759c7ad52SXin LI 			err(1, "read failed (%zu/4)", cnt);
2288c384020SXin LI 		if (buf[1] != 0)
2298c384020SXin LI 			errx(1, "connection failed, SOCKS error %d", buf[1]);
23059c7ad52SXin LI 		switch (buf[3]) {
23159c7ad52SXin LI 		case SOCKS_IPV4:
23259c7ad52SXin LI 			cnt = atomicio(read, proxyfd, buf + 4, 6);
23359c7ad52SXin LI 			if (cnt != 6)
23452dba105SXin LI 				err(1, "read failed (%zu/6)", cnt);
23559c7ad52SXin LI 			break;
23659c7ad52SXin LI 		case SOCKS_IPV6:
23759c7ad52SXin LI 			cnt = atomicio(read, proxyfd, buf + 4, 18);
23859c7ad52SXin LI 			if (cnt != 18)
23952dba105SXin LI 				err(1, "read failed (%zu/18)", cnt);
24059c7ad52SXin LI 			break;
24159c7ad52SXin LI 		default:
24259c7ad52SXin LI 			errx(1, "connection failed, unsupported address type");
24359c7ad52SXin LI 		}
2448c384020SXin LI 	} else if (socksv == 4) {
2457a997a69SXin LI 		/* This will exit on lookup failure */
2467a997a69SXin LI 		decode_addrport(host, port, (struct sockaddr *)&addr,
2477a997a69SXin LI 		    sizeof(addr), 1, 0);
2487a997a69SXin LI 
2498c384020SXin LI 		/* Version 4 */
2508c384020SXin LI 		buf[0] = SOCKS_V4;
2518c384020SXin LI 		buf[1] = SOCKS_CONNECT;	/* connect */
2527a997a69SXin LI 		memcpy(buf + 2, &in4->sin_port, sizeof in4->sin_port);
2537a997a69SXin LI 		memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
2548c384020SXin LI 		buf[8] = 0;	/* empty username */
2557a997a69SXin LI 		wlen = 9;
2568c384020SXin LI 
2577a997a69SXin LI 		cnt = atomicio(vwrite, proxyfd, buf, wlen);
2587a997a69SXin LI 		if (cnt != wlen)
2594f2bbc00SXin LI 			err(1, "write failed (%zu/%zu)", cnt, wlen);
2608c384020SXin LI 
2617a997a69SXin LI 		cnt = atomicio(read, proxyfd, buf, 8);
2628c384020SXin LI 		if (cnt != 8)
2634f2bbc00SXin LI 			err(1, "read failed (%zu/8)", cnt);
2648c384020SXin LI 		if (buf[1] != 90)
2658c384020SXin LI 			errx(1, "connection failed, SOCKS error %d", buf[1]);
2668c384020SXin LI 	} else if (socksv == -1) {
2678c384020SXin LI 		/* HTTP proxy CONNECT */
2688c384020SXin LI 
2698c384020SXin LI 		/* Disallow bad chars in hostname */
2708c384020SXin LI 		if (strcspn(host, "\r\n\t []:") != strlen(host))
2718c384020SXin LI 			errx(1, "Invalid hostname");
2728c384020SXin LI 
2738c384020SXin LI 		/* Try to be sane about numeric IPv6 addresses */
2748c384020SXin LI 		if (strchr(host, ':') != NULL) {
2758c384020SXin LI 			r = snprintf(buf, sizeof(buf),
2767a997a69SXin LI 			    "CONNECT [%s]:%d HTTP/1.0\r\n",
2778c384020SXin LI 			    host, ntohs(serverport));
2788c384020SXin LI 		} else {
2798c384020SXin LI 			r = snprintf(buf, sizeof(buf),
2807a997a69SXin LI 			    "CONNECT %s:%d HTTP/1.0\r\n",
2818c384020SXin LI 			    host, ntohs(serverport));
2828c384020SXin LI 		}
2837a997a69SXin LI 		if (r == -1 || (size_t)r >= sizeof(buf))
2848c384020SXin LI 			errx(1, "hostname too long");
2858c384020SXin LI 		r = strlen(buf);
2868c384020SXin LI 
2877a997a69SXin LI 		cnt = atomicio(vwrite, proxyfd, buf, r);
2888c384020SXin LI 		if (cnt != r)
2894f2bbc00SXin LI 			err(1, "write failed (%zu/%d)", cnt, r);
2908c384020SXin LI 
2917a997a69SXin LI 		if (authretry > 1) {
2927a997a69SXin LI 			char resp[1024];
2937a997a69SXin LI 
2947a997a69SXin LI 			proxypass = getproxypass(proxyuser, proxyhost);
2957a997a69SXin LI 			r = snprintf(buf, sizeof(buf), "%s:%s",
2967a997a69SXin LI 			    proxyuser, proxypass);
2977a997a69SXin LI 			if (r == -1 || (size_t)r >= sizeof(buf) ||
2987a997a69SXin LI 			    b64_ntop(buf, strlen(buf), resp,
2997a997a69SXin LI 			    sizeof(resp)) == -1)
3007a997a69SXin LI 				errx(1, "Proxy username/password too long");
3017a997a69SXin LI 			r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
3027a997a69SXin LI 			    "Basic %s\r\n", resp);
3037a997a69SXin LI 			if (r == -1 || (size_t)r >= sizeof(buf))
3047a997a69SXin LI 				errx(1, "Proxy auth response too long");
3057a997a69SXin LI 			r = strlen(buf);
3067a997a69SXin LI 			if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
3074f2bbc00SXin LI 				err(1, "write failed (%zu/%d)", cnt, r);
3087a997a69SXin LI 		}
3097a997a69SXin LI 
3107a997a69SXin LI 		/* Terminate headers */
311*100e2a06SXin LI 		if ((cnt = atomicio(vwrite, proxyfd, "\r\n", 2)) != 2)
312*100e2a06SXin LI 			err(1, "write failed (%zu/2)", cnt);
3137a997a69SXin LI 
3147a997a69SXin LI 		/* Read status reply */
3157a997a69SXin LI 		proxy_read_line(proxyfd, buf, sizeof(buf));
3167a997a69SXin LI 		if (proxyuser != NULL &&
3177a997a69SXin LI 		    strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
3187a997a69SXin LI 			if (authretry > 1) {
3197a997a69SXin LI 				fprintf(stderr, "Proxy authentication "
3207a997a69SXin LI 				    "failed\n");
3217a997a69SXin LI 			}
3227a997a69SXin LI 			close(proxyfd);
3237a997a69SXin LI 			goto again;
324b2f6436dSXin LI 		} else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0 &&
325b2f6436dSXin LI 		    strncmp(buf, "HTTP/1.1 200 ", 12) != 0)
3267a997a69SXin LI 			errx(1, "Proxy error: \"%s\"", buf);
3277a997a69SXin LI 
3287a997a69SXin LI 		/* Headers continue until we hit an empty line */
3298c384020SXin LI 		for (r = 0; r < HTTP_MAXHDRS; r++) {
3308c384020SXin LI 			proxy_read_line(proxyfd, buf, sizeof(buf));
3318c384020SXin LI 			if (*buf == '\0')
3328c384020SXin LI 				break;
3338c384020SXin LI 		}
3347a997a69SXin LI 		if (*buf != '\0')
3357a997a69SXin LI 			errx(1, "Too many proxy headers received");
3368c384020SXin LI 	} else
3378c384020SXin LI 		errx(1, "Unknown proxy protocol %d", socksv);
3388c384020SXin LI 
3397a997a69SXin LI 	return (proxyfd);
3408c384020SXin LI }
341