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