2006-05-21 15:49:27 +00:00
|
|
|
/* $OpenBSD: socks.c,v 1.16 2006/01/25 23:21:37 djm Exp $ */
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1999 Niklas Hallqvist. All rights reserved.
|
2006-05-21 15:49:27 +00:00
|
|
|
* Copyright (c) 2004, 2005 Damien Miller. All rights reserved.
|
2005-02-04 08:41:44 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2006-05-21 15:49:27 +00:00
|
|
|
#include <resolv.h>
|
|
|
|
#include <readpassphrase.h>
|
|
|
|
#include "atomicio.h"
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
#define SOCKS_PORT "1080"
|
|
|
|
#define HTTP_PROXY_PORT "3128"
|
|
|
|
#define HTTP_MAXHDRS 64
|
|
|
|
#define SOCKS_V5 5
|
|
|
|
#define SOCKS_V4 4
|
|
|
|
#define SOCKS_NOAUTH 0
|
|
|
|
#define SOCKS_NOMETHOD 0xff
|
|
|
|
#define SOCKS_CONNECT 1
|
|
|
|
#define SOCKS_IPV4 1
|
2006-05-21 15:49:27 +00:00
|
|
|
#define SOCKS_DOMAIN 3
|
|
|
|
#define SOCKS_IPV6 4
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
int remote_connect(const char *, const char *, struct addrinfo);
|
|
|
|
int socks_connect(const char *, const char *, struct addrinfo,
|
|
|
|
const char *, const char *, struct addrinfo, int,
|
|
|
|
const char *);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
static int
|
|
|
|
decode_addrport(const char *h, const char *p, struct sockaddr *addr,
|
|
|
|
socklen_t addrlen, int v4only, int numeric)
|
2005-02-04 08:41:44 +00:00
|
|
|
{
|
2006-05-21 15:49:27 +00:00
|
|
|
int r;
|
|
|
|
struct addrinfo hints, *res;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
bzero(&hints, sizeof(hints));
|
|
|
|
hints.ai_family = v4only ? PF_INET : PF_UNSPEC;
|
|
|
|
hints.ai_flags = numeric ? AI_NUMERICHOST : 0;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
r = getaddrinfo(h, p, &hints, &res);
|
|
|
|
/* Don't fatal when attempting to convert a numeric address */
|
|
|
|
if (r != 0) {
|
|
|
|
if (!numeric) {
|
|
|
|
errx(1, "getaddrinfo(\"%.64s\", \"%.64s\"): %s", h, p,
|
|
|
|
gai_strerror(r));
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (addrlen < res->ai_addrlen) {
|
|
|
|
freeaddrinfo(res);
|
|
|
|
errx(1, "internal error: addrlen < res->ai_addrlen");
|
2005-02-04 08:41:44 +00:00
|
|
|
}
|
2006-05-21 15:49:27 +00:00
|
|
|
memcpy(addr, res->ai_addr, res->ai_addrlen);
|
|
|
|
freeaddrinfo(res);
|
|
|
|
return (0);
|
2005-02-04 08:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-05-21 15:49:27 +00:00
|
|
|
proxy_read_line(int fd, char *buf, size_t bufsz)
|
2005-02-04 08:41:44 +00:00
|
|
|
{
|
2006-05-21 15:49:27 +00:00
|
|
|
size_t off;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
for(off = 0;;) {
|
|
|
|
if (off >= bufsz)
|
|
|
|
errx(1, "proxy read too long");
|
2006-05-21 15:49:27 +00:00
|
|
|
if (atomicio(read, fd, buf + off, 1) != 1)
|
2005-02-04 08:41:44 +00:00
|
|
|
err(1, "proxy read");
|
|
|
|
/* Skip CR */
|
|
|
|
if (buf[off] == '\r')
|
|
|
|
continue;
|
|
|
|
if (buf[off] == '\n') {
|
|
|
|
buf[off] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off++;
|
|
|
|
}
|
|
|
|
return (off);
|
|
|
|
}
|
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
static const char *
|
|
|
|
getproxypass(const char *proxyuser, const char *proxyhost)
|
|
|
|
{
|
|
|
|
char prompt[512];
|
|
|
|
static char pw[256];
|
|
|
|
|
|
|
|
snprintf(prompt, sizeof(prompt), "Proxy password for %s@%s: ",
|
|
|
|
proxyuser, proxyhost);
|
|
|
|
if (readpassphrase(prompt, pw, sizeof(pw), RPP_REQUIRE_TTY) == NULL)
|
|
|
|
errx(1, "Unable to read proxy passphrase");
|
|
|
|
return (pw);
|
|
|
|
}
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
int
|
2006-05-21 15:49:27 +00:00
|
|
|
socks_connect(const char *host, const char *port,
|
|
|
|
struct addrinfo hints __attribute__ ((__unused__)),
|
|
|
|
const char *proxyhost, const char *proxyport, struct addrinfo proxyhints,
|
|
|
|
int socksv, const char *proxyuser)
|
2005-02-04 08:41:44 +00:00
|
|
|
{
|
2006-05-21 15:49:27 +00:00
|
|
|
int proxyfd, r, authretry = 0;
|
|
|
|
size_t hlen, wlen;
|
2005-02-04 08:41:44 +00:00
|
|
|
unsigned char buf[1024];
|
2006-05-21 15:49:27 +00:00
|
|
|
size_t cnt;
|
|
|
|
struct sockaddr_storage addr;
|
|
|
|
struct sockaddr_in *in4 = (struct sockaddr_in *)&addr;
|
|
|
|
struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)&addr;
|
2005-02-04 08:41:44 +00:00
|
|
|
in_port_t serverport;
|
2006-05-21 15:49:27 +00:00
|
|
|
const char *proxypass = NULL;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (proxyport == NULL)
|
|
|
|
proxyport = (socksv == -1) ? HTTP_PROXY_PORT : SOCKS_PORT;
|
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
/* Abuse API to lookup port */
|
|
|
|
if (decode_addrport("0.0.0.0", port, (struct sockaddr *)&addr,
|
|
|
|
sizeof(addr), 1, 1) == -1)
|
|
|
|
errx(1, "unknown port \"%.64s\"", port);
|
|
|
|
serverport = in4->sin_port;
|
|
|
|
|
|
|
|
again:
|
|
|
|
if (authretry++ > 3)
|
|
|
|
errx(1, "Too many authentication failures");
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
proxyfd = remote_connect(proxyhost, proxyport, proxyhints);
|
|
|
|
|
|
|
|
if (proxyfd < 0)
|
2006-05-21 15:49:27 +00:00
|
|
|
return (-1);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (socksv == 5) {
|
2006-05-21 15:49:27 +00:00
|
|
|
if (decode_addrport(host, port, (struct sockaddr *)&addr,
|
|
|
|
sizeof(addr), 0, 1) == -1)
|
|
|
|
addr.ss_family = 0; /* used in switch below */
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
/* Version 5, one method: no authentication */
|
|
|
|
buf[0] = SOCKS_V5;
|
|
|
|
buf[1] = 1;
|
|
|
|
buf[2] = SOCKS_NOAUTH;
|
2006-05-21 15:49:27 +00:00
|
|
|
cnt = atomicio(vwrite, proxyfd, buf, 3);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (cnt != 3)
|
2006-05-21 15:49:27 +00:00
|
|
|
err(1, "write failed (%d/3)", cnt);
|
|
|
|
|
|
|
|
cnt = atomicio(read, proxyfd, buf, 2);
|
|
|
|
if (cnt != 2)
|
|
|
|
err(1, "read failed (%d/3)", cnt);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
if (buf[1] == SOCKS_NOMETHOD)
|
2006-05-21 15:49:27 +00:00
|
|
|
errx(1, "authentication method negotiation failed");
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
switch (addr.ss_family) {
|
|
|
|
case 0:
|
|
|
|
/* Version 5, connect: domain name */
|
|
|
|
|
|
|
|
/* Max domain name length is 255 bytes */
|
|
|
|
hlen = strlen(host);
|
|
|
|
if (hlen > 255)
|
|
|
|
errx(1, "host name too long for SOCKS5");
|
|
|
|
buf[0] = SOCKS_V5;
|
|
|
|
buf[1] = SOCKS_CONNECT;
|
|
|
|
buf[2] = 0;
|
|
|
|
buf[3] = SOCKS_DOMAIN;
|
|
|
|
buf[4] = hlen;
|
|
|
|
memcpy(buf + 5, host, hlen);
|
|
|
|
memcpy(buf + 5 + hlen, &serverport, sizeof serverport);
|
|
|
|
wlen = 7 + hlen;
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
/* Version 5, connect: IPv4 address */
|
|
|
|
buf[0] = SOCKS_V5;
|
|
|
|
buf[1] = SOCKS_CONNECT;
|
|
|
|
buf[2] = 0;
|
|
|
|
buf[3] = SOCKS_IPV4;
|
|
|
|
memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
|
|
|
|
memcpy(buf + 8, &in4->sin_port, sizeof in4->sin_port);
|
|
|
|
wlen = 10;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
/* Version 5, connect: IPv6 address */
|
|
|
|
buf[0] = SOCKS_V5;
|
|
|
|
buf[1] = SOCKS_CONNECT;
|
|
|
|
buf[2] = 0;
|
|
|
|
buf[3] = SOCKS_IPV6;
|
|
|
|
memcpy(buf + 4, &in6->sin6_addr, sizeof in6->sin6_addr);
|
|
|
|
memcpy(buf + 20, &in6->sin6_port,
|
|
|
|
sizeof in6->sin6_port);
|
|
|
|
wlen = 22;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errx(1, "internal error: silly AF");
|
|
|
|
}
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
cnt = atomicio(vwrite, proxyfd, buf, wlen);
|
|
|
|
if (cnt != wlen)
|
|
|
|
err(1, "write failed (%d/%d)", cnt, wlen);
|
|
|
|
|
|
|
|
cnt = atomicio(read, proxyfd, buf, 10);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (cnt != 10)
|
2006-05-21 15:49:27 +00:00
|
|
|
err(1, "read failed (%d/10)", cnt);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (buf[1] != 0)
|
2006-05-21 15:49:27 +00:00
|
|
|
errx(1, "connection failed, SOCKS error %d", buf[1]);
|
2005-02-04 08:41:44 +00:00
|
|
|
} else if (socksv == 4) {
|
2006-05-21 15:49:27 +00:00
|
|
|
/* This will exit on lookup failure */
|
|
|
|
decode_addrport(host, port, (struct sockaddr *)&addr,
|
|
|
|
sizeof(addr), 1, 0);
|
|
|
|
|
2005-02-04 08:41:44 +00:00
|
|
|
/* Version 4 */
|
|
|
|
buf[0] = SOCKS_V4;
|
|
|
|
buf[1] = SOCKS_CONNECT; /* connect */
|
2006-05-21 15:49:27 +00:00
|
|
|
memcpy(buf + 2, &in4->sin_port, sizeof in4->sin_port);
|
|
|
|
memcpy(buf + 4, &in4->sin_addr, sizeof in4->sin_addr);
|
2005-02-04 08:41:44 +00:00
|
|
|
buf[8] = 0; /* empty username */
|
2006-05-21 15:49:27 +00:00
|
|
|
wlen = 9;
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
cnt = atomicio(vwrite, proxyfd, buf, wlen);
|
|
|
|
if (cnt != wlen)
|
|
|
|
err(1, "write failed (%d/%d)", cnt, wlen);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
cnt = atomicio(read, proxyfd, buf, 8);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (cnt != 8)
|
2006-05-21 15:49:27 +00:00
|
|
|
err(1, "read failed (%d/8)", cnt);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (buf[1] != 90)
|
2006-05-21 15:49:27 +00:00
|
|
|
errx(1, "connection failed, SOCKS error %d", buf[1]);
|
2005-02-04 08:41:44 +00:00
|
|
|
} else if (socksv == -1) {
|
|
|
|
/* HTTP proxy CONNECT */
|
|
|
|
|
|
|
|
/* Disallow bad chars in hostname */
|
|
|
|
if (strcspn(host, "\r\n\t []:") != strlen(host))
|
2006-05-21 15:49:27 +00:00
|
|
|
errx(1, "Invalid hostname");
|
2005-02-04 08:41:44 +00:00
|
|
|
|
|
|
|
/* Try to be sane about numeric IPv6 addresses */
|
|
|
|
if (strchr(host, ':') != NULL) {
|
|
|
|
r = snprintf(buf, sizeof(buf),
|
2006-05-21 15:49:27 +00:00
|
|
|
"CONNECT [%s]:%d HTTP/1.0\r\n",
|
2005-02-04 08:41:44 +00:00
|
|
|
host, ntohs(serverport));
|
|
|
|
} else {
|
|
|
|
r = snprintf(buf, sizeof(buf),
|
2006-05-21 15:49:27 +00:00
|
|
|
"CONNECT %s:%d HTTP/1.0\r\n",
|
2005-02-04 08:41:44 +00:00
|
|
|
host, ntohs(serverport));
|
|
|
|
}
|
2006-05-21 15:49:27 +00:00
|
|
|
if (r == -1 || (size_t)r >= sizeof(buf))
|
|
|
|
errx(1, "hostname too long");
|
2005-02-04 08:41:44 +00:00
|
|
|
r = strlen(buf);
|
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
cnt = atomicio(vwrite, proxyfd, buf, r);
|
2005-02-04 08:41:44 +00:00
|
|
|
if (cnt != r)
|
2006-05-21 15:49:27 +00:00
|
|
|
err(1, "write failed (%d/%d)", cnt, r);
|
|
|
|
|
|
|
|
if (authretry > 1) {
|
|
|
|
char resp[1024];
|
|
|
|
|
|
|
|
proxypass = getproxypass(proxyuser, proxyhost);
|
|
|
|
r = snprintf(buf, sizeof(buf), "%s:%s",
|
|
|
|
proxyuser, proxypass);
|
|
|
|
if (r == -1 || (size_t)r >= sizeof(buf) ||
|
|
|
|
b64_ntop(buf, strlen(buf), resp,
|
|
|
|
sizeof(resp)) == -1)
|
|
|
|
errx(1, "Proxy username/password too long");
|
|
|
|
r = snprintf(buf, sizeof(buf), "Proxy-Authorization: "
|
|
|
|
"Basic %s\r\n", resp);
|
|
|
|
if (r == -1 || (size_t)r >= sizeof(buf))
|
|
|
|
errx(1, "Proxy auth response too long");
|
|
|
|
r = strlen(buf);
|
|
|
|
if ((cnt = atomicio(vwrite, proxyfd, buf, r)) != r)
|
|
|
|
err(1, "write failed (%d/%d)", cnt, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate headers */
|
|
|
|
if ((r = atomicio(vwrite, proxyfd, "\r\n", 2)) != 2)
|
|
|
|
err(1, "write failed (2/%d)", r);
|
|
|
|
|
|
|
|
/* Read status reply */
|
|
|
|
proxy_read_line(proxyfd, buf, sizeof(buf));
|
|
|
|
if (proxyuser != NULL &&
|
|
|
|
strncmp(buf, "HTTP/1.0 407 ", 12) == 0) {
|
|
|
|
if (authretry > 1) {
|
|
|
|
fprintf(stderr, "Proxy authentication "
|
|
|
|
"failed\n");
|
|
|
|
}
|
|
|
|
close(proxyfd);
|
|
|
|
goto again;
|
|
|
|
} else if (strncmp(buf, "HTTP/1.0 200 ", 12) != 0)
|
|
|
|
errx(1, "Proxy error: \"%s\"", buf);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
/* Headers continue until we hit an empty line */
|
2005-02-04 08:41:44 +00:00
|
|
|
for (r = 0; r < HTTP_MAXHDRS; r++) {
|
|
|
|
proxy_read_line(proxyfd, buf, sizeof(buf));
|
|
|
|
if (*buf == '\0')
|
|
|
|
break;
|
|
|
|
}
|
2006-05-21 15:49:27 +00:00
|
|
|
if (*buf != '\0')
|
|
|
|
errx(1, "Too many proxy headers received");
|
2005-02-04 08:41:44 +00:00
|
|
|
} else
|
2006-05-21 15:49:27 +00:00
|
|
|
errx(1, "Unknown proxy protocol %d", socksv);
|
2005-02-04 08:41:44 +00:00
|
|
|
|
2006-05-21 15:49:27 +00:00
|
|
|
return (proxyfd);
|
2005-02-04 08:41:44 +00:00
|
|
|
}
|