2008-02-07 06:31:47 +01:00
|
|
|
/* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
|
2018-06-20 14:13:28 +02:00
|
|
|
* Copyright (c) 2007-2018, The Tor Project, Inc.
|
2008-02-07 06:31:47 +01:00
|
|
|
*/
|
2004-10-28 22:28:13 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
2018-06-29 17:41:38 +02:00
|
|
|
|
|
|
|
#include "lib/arch/bytes.h"
|
2018-07-10 21:20:28 +02:00
|
|
|
#include "lib/log/log.h"
|
2018-07-10 21:16:57 +02:00
|
|
|
#include "lib/malloc/malloc.h"
|
2018-06-29 17:41:38 +02:00
|
|
|
#include "lib/net/address.h"
|
|
|
|
#include "lib/net/resolve.h"
|
|
|
|
#include "lib/net/socket.h"
|
2018-06-27 15:49:08 +02:00
|
|
|
#include "lib/sandbox/sandbox.h"
|
2018-06-29 17:41:38 +02:00
|
|
|
#include "lib/string/util_string.h"
|
|
|
|
|
2018-07-05 20:48:29 +02:00
|
|
|
#include "lib/net/socks5_status.h"
|
2004-10-28 22:28:13 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
|
|
|
#include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-31 16:59:42 +01:00
|
|
|
#ifdef _WIN32
|
2004-10-28 22:28:13 +02:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
#define RESPONSE_LEN_4 8
|
2005-10-19 00:21:29 +02:00
|
|
|
#define log_sock_error(act, _s) \
|
2007-06-17 20:22:39 +02:00
|
|
|
STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
|
|
|
|
tor_socket_strerror(tor_socket_errno(_s))); STMT_END
|
2004-10-28 22:28:13 +02:00
|
|
|
|
2007-05-15 23:17:48 +02:00
|
|
|
static void usage(void) ATTR_NORETURN;
|
|
|
|
|
2005-06-28 03:52:58 +02:00
|
|
|
/** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
|
|
|
|
* <b>username</b> and <b>hostname</b> as provided. Return the number
|
|
|
|
* of bytes in the request. */
|
2009-09-01 05:23:47 +02:00
|
|
|
static ssize_t
|
2006-12-26 23:41:49 +01:00
|
|
|
build_socks_resolve_request(char **out,
|
|
|
|
const char *username,
|
|
|
|
const char *hostname,
|
|
|
|
int reverse,
|
|
|
|
int version)
|
2004-10-28 22:28:13 +02:00
|
|
|
{
|
2006-12-26 23:41:49 +01:00
|
|
|
size_t len = 0;
|
2004-10-28 22:28:13 +02:00
|
|
|
tor_assert(out);
|
|
|
|
tor_assert(username);
|
|
|
|
tor_assert(hostname);
|
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
if (version == 4) {
|
|
|
|
len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
|
|
|
|
*out = tor_malloc(len);
|
|
|
|
(*out)[0] = 4; /* SOCKS version 4 */
|
|
|
|
(*out)[1] = '\xF0'; /* Command: resolve. */
|
|
|
|
set_uint16((*out)+2, htons(0)); /* port: 0. */
|
|
|
|
set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
|
2007-01-16 00:56:38 +01:00
|
|
|
memcpy((*out)+8, username, strlen(username)+1);
|
|
|
|
memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
|
2006-12-26 23:41:49 +01:00
|
|
|
} else if (version == 5) {
|
|
|
|
int is_ip_address;
|
2012-11-15 04:20:18 +01:00
|
|
|
tor_addr_t addr;
|
2006-12-26 23:41:49 +01:00
|
|
|
size_t addrlen;
|
2012-11-15 04:20:18 +01:00
|
|
|
int ipv6;
|
|
|
|
is_ip_address = tor_addr_parse(&addr, hostname) != -1;
|
2006-12-26 23:41:49 +01:00
|
|
|
if (!is_ip_address && reverse) {
|
|
|
|
log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-11-15 04:20:18 +01:00
|
|
|
ipv6 = reverse && tor_addr_family(&addr) == AF_INET6;
|
|
|
|
addrlen = reverse ? (ipv6 ? 16 : 4) : 1 + strlen(hostname);
|
2017-01-24 19:40:01 +01:00
|
|
|
if (addrlen > UINT8_MAX) {
|
|
|
|
log_err(LD_GENERAL, "Hostname is too long!");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-12-26 23:41:49 +01:00
|
|
|
len = 6 + addrlen;
|
|
|
|
*out = tor_malloc(len);
|
|
|
|
(*out)[0] = 5; /* SOCKS version 5 */
|
|
|
|
(*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
|
|
|
|
(*out)[2] = 0; /* reserved. */
|
2007-09-04 20:27:40 +02:00
|
|
|
if (reverse) {
|
2012-11-15 04:20:18 +01:00
|
|
|
(*out)[3] = ipv6 ? 4 : 1;
|
|
|
|
if (ipv6)
|
|
|
|
memcpy((*out)+4, tor_addr_to_in6_addr8(&addr), 16);
|
|
|
|
else
|
|
|
|
set_uint32((*out)+4, tor_addr_to_ipv4n(&addr));
|
2006-12-26 23:41:49 +01:00
|
|
|
} else {
|
2012-11-15 04:20:18 +01:00
|
|
|
(*out)[3] = 3;
|
2006-12-26 23:41:49 +01:00
|
|
|
(*out)[4] = (char)(uint8_t)(addrlen - 1);
|
|
|
|
memcpy((*out)+5, hostname, addrlen - 1);
|
|
|
|
}
|
|
|
|
set_uint16((*out)+4+addrlen, 0); /* port */
|
|
|
|
} else {
|
|
|
|
tor_assert(0);
|
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:11:08 +01:00
|
|
|
static void
|
|
|
|
onion_warning(const char *hostname)
|
|
|
|
{
|
|
|
|
log_warn(LD_NET,
|
|
|
|
"%s is a hidden service; those don't have IP addresses. "
|
|
|
|
"You can use the AutomapHostsOnResolve option to have Tor return a "
|
|
|
|
"fake address for hidden services. Or you can have your "
|
|
|
|
"application send the address to Tor directly; we recommend an "
|
|
|
|
"application that uses SOCKS 5 with hostnames.",
|
|
|
|
hostname);
|
|
|
|
}
|
|
|
|
|
2005-06-28 03:52:58 +02:00
|
|
|
/** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
|
|
|
|
* *<b>addr_out</b> to the address it contains (in host order).
|
|
|
|
* Return 0 on success, -1 on error.
|
2005-06-11 20:52:12 +02:00
|
|
|
*/
|
2004-11-09 21:04:00 +01:00
|
|
|
static int
|
2009-06-18 17:08:10 +02:00
|
|
|
parse_socks4a_resolve_response(const char *hostname,
|
|
|
|
const char *response, size_t len,
|
2012-11-15 04:07:18 +01:00
|
|
|
tor_addr_t *addr_out)
|
2004-10-28 22:28:13 +02:00
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
tor_assert(response);
|
|
|
|
tor_assert(addr_out);
|
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
if (len < RESPONSE_LEN_4) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Truncated socks response.");
|
2004-10-28 22:28:13 +02:00
|
|
|
return -1;
|
2005-04-01 09:04:40 +02:00
|
|
|
}
|
|
|
|
if (((uint8_t)response[0])!=0) { /* version: 0 */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
|
2004-10-28 22:28:13 +02:00
|
|
|
return -1;
|
2005-04-01 09:04:40 +02:00
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
status = (uint8_t)response[1];
|
2005-04-01 09:04:40 +02:00
|
|
|
if (get_uint16(response+2)!=0) { /* port: 0 */
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
|
2004-10-28 22:28:13 +02:00
|
|
|
return -1;
|
2005-04-01 09:04:40 +02:00
|
|
|
}
|
2004-11-24 08:51:30 +01:00
|
|
|
if (status != 90) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
|
2009-06-18 17:08:10 +02:00
|
|
|
if (!strcasecmpend(hostname, ".onion")) {
|
2015-01-28 16:11:08 +01:00
|
|
|
onion_warning(hostname);
|
2009-06-18 17:08:10 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
return -1;
|
2004-11-24 08:51:30 +01:00
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
|
2012-11-15 04:07:18 +01:00
|
|
|
tor_addr_from_ipv4n(addr_out, get_uint32(response+4));
|
2004-10-28 22:28:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-08-25 23:02:22 +02:00
|
|
|
/* It would be nice to let someone know what SOCKS5 issue a user may have */
|
2008-08-25 23:02:44 +02:00
|
|
|
static const char *
|
2008-08-25 23:02:22 +02:00
|
|
|
socks5_reason_to_string(char reason)
|
|
|
|
{
|
2010-05-21 04:30:08 +02:00
|
|
|
switch (reason) {
|
2008-08-26 21:13:54 +02:00
|
|
|
case SOCKS5_SUCCEEDED:
|
|
|
|
return "succeeded";
|
|
|
|
case SOCKS5_GENERAL_ERROR:
|
|
|
|
return "general error";
|
|
|
|
case SOCKS5_NOT_ALLOWED:
|
|
|
|
return "not allowed";
|
|
|
|
case SOCKS5_NET_UNREACHABLE:
|
|
|
|
return "network is unreachable";
|
|
|
|
case SOCKS5_HOST_UNREACHABLE:
|
|
|
|
return "host is unreachable";
|
|
|
|
case SOCKS5_CONNECTION_REFUSED:
|
|
|
|
return "connection refused";
|
|
|
|
case SOCKS5_TTL_EXPIRED:
|
|
|
|
return "ttl expired";
|
|
|
|
case SOCKS5_COMMAND_NOT_SUPPORTED:
|
|
|
|
return "command not supported";
|
|
|
|
case SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED:
|
|
|
|
return "address type not supported";
|
|
|
|
default:
|
|
|
|
return "unknown SOCKS5 code";
|
|
|
|
}
|
2008-08-25 23:02:22 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Send a resolve request for <b>hostname</b> to the Tor listening on
|
|
|
|
* <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
|
2005-06-28 03:52:58 +02:00
|
|
|
* address (in host order) into *<b>result_addr</b>.
|
2005-06-11 20:52:12 +02:00
|
|
|
*/
|
2004-11-09 21:04:00 +01:00
|
|
|
static int
|
2018-07-10 19:18:55 +02:00
|
|
|
do_resolve(const char *hostname,
|
|
|
|
const tor_addr_t *sockshost, uint16_t socksport,
|
2006-12-26 23:41:49 +01:00
|
|
|
int reverse, int version,
|
2012-11-15 04:07:18 +01:00
|
|
|
tor_addr_t *result_addr, char **result_hostname)
|
2004-10-28 22:28:13 +02:00
|
|
|
{
|
2013-02-11 21:13:42 +01:00
|
|
|
int s = -1;
|
2018-07-10 19:18:55 +02:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t socklen;
|
2006-12-26 23:41:49 +01:00
|
|
|
char *req = NULL;
|
2009-09-01 05:23:47 +02:00
|
|
|
ssize_t len = 0;
|
2004-10-28 22:28:13 +02:00
|
|
|
|
|
|
|
tor_assert(hostname);
|
|
|
|
tor_assert(result_addr);
|
2006-12-26 23:41:49 +01:00
|
|
|
tor_assert(version == 4 || version == 5);
|
|
|
|
|
2012-11-15 04:07:18 +01:00
|
|
|
tor_addr_make_unspec(result_addr);
|
2006-12-26 23:41:49 +01:00
|
|
|
*result_hostname = NULL;
|
2004-10-28 22:28:13 +02:00
|
|
|
|
2007-04-21 19:24:18 +02:00
|
|
|
s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
|
2004-10-28 22:28:13 +02:00
|
|
|
if (s<0) {
|
|
|
|
log_sock_error("creating_socket", -1);
|
2013-02-11 23:32:58 +01:00
|
|
|
return -1;
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 19:18:55 +02:00
|
|
|
socklen = tor_addr_to_sockaddr(sockshost, socksport,
|
|
|
|
(struct sockaddr *)&ss, sizeof(ss));
|
|
|
|
|
2018-09-10 20:50:30 +02:00
|
|
|
if (connect(s, (struct sockaddr*)&ss, socklen)) {
|
2004-10-28 22:28:13 +02:00
|
|
|
log_sock_error("connecting to SOCKS host", s);
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
if (version == 5) {
|
|
|
|
char method_buf[2];
|
2018-06-29 16:16:57 +02:00
|
|
|
if (write_all_to_socket(s, "\x05\x01\x00", 3) != 3) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error sending SOCKS5 method list.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, method_buf, 2) != 2) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error reading SOCKS5 methods.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
|
|
|
if (method_buf[0] != '\x05') {
|
|
|
|
log_err(LD_NET, "Unrecognized socks version: %u",
|
|
|
|
(unsigned)method_buf[0]);
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
|
|
|
if (method_buf[1] != '\x00') {
|
|
|
|
log_err(LD_NET, "Unrecognized socks authentication method: %u",
|
|
|
|
(unsigned)method_buf[1]);
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
|
|
|
|
version))<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_err(LD_BUG,"Error generating SOCKS request");
|
2008-01-16 06:27:19 +01:00
|
|
|
tor_assert(!req);
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
2018-06-29 16:16:57 +02:00
|
|
|
if (write_all_to_socket(s, req, len) != len) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_sock_error("sending SOCKS request", s);
|
|
|
|
tor_free(req);
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
|
|
|
tor_free(req);
|
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
if (version == 4) {
|
|
|
|
char reply_buf[RESPONSE_LEN_4];
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, reply_buf, RESPONSE_LEN_4) != RESPONSE_LEN_4) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error reading SOCKS4 response.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2004-11-24 08:45:42 +01:00
|
|
|
}
|
2009-06-18 17:08:10 +02:00
|
|
|
if (parse_socks4a_resolve_response(hostname,
|
|
|
|
reply_buf, RESPONSE_LEN_4,
|
2010-05-21 04:30:08 +02:00
|
|
|
result_addr)<0) {
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
2006-12-26 23:41:49 +01:00
|
|
|
} else {
|
2012-11-15 04:07:18 +01:00
|
|
|
char reply_buf[16];
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, reply_buf, 4) != 4) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error reading SOCKS5 response.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
|
|
|
if (reply_buf[0] != 5) {
|
|
|
|
log_err(LD_NET, "Bad SOCKS5 reply version.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2008-08-25 23:02:22 +02:00
|
|
|
/* Give a user some useful feedback about SOCKS5 errors */
|
2006-12-26 23:41:49 +01:00
|
|
|
if (reply_buf[1] != 0) {
|
2008-08-25 23:02:22 +02:00
|
|
|
log_warn(LD_NET,"Got SOCKS5 status response '%u': %s",
|
|
|
|
(unsigned)reply_buf[1],
|
|
|
|
socks5_reason_to_string(reply_buf[1]));
|
2009-06-18 17:08:10 +02:00
|
|
|
if (reply_buf[1] == 4 && !strcasecmpend(hostname, ".onion")) {
|
2015-01-28 16:11:08 +01:00
|
|
|
onion_warning(hostname);
|
2009-06-18 17:08:10 +02:00
|
|
|
}
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
|
|
|
if (reply_buf[3] == 1) {
|
|
|
|
/* IPv4 address */
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, reply_buf, 4) != 4) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error reading address in socks5 response.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2012-11-15 04:07:18 +01:00
|
|
|
tor_addr_from_ipv4n(result_addr, get_uint32(reply_buf));
|
|
|
|
} else if (reply_buf[3] == 4) {
|
|
|
|
/* IPv6 address */
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, reply_buf, 16) != 16) {
|
2012-11-15 04:07:18 +01:00
|
|
|
log_err(LD_NET, "Error reading address in socks5 response.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2012-11-15 04:07:18 +01:00
|
|
|
}
|
|
|
|
tor_addr_from_ipv6_bytes(result_addr, reply_buf);
|
2006-12-26 23:41:49 +01:00
|
|
|
} else if (reply_buf[3] == 3) {
|
2012-11-15 04:07:18 +01:00
|
|
|
/* Domain name */
|
2007-05-17 00:15:14 +02:00
|
|
|
size_t result_len;
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, reply_buf, 1) != 1) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error reading address_length in socks5 response.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2007-05-17 00:15:14 +02:00
|
|
|
result_len = *(uint8_t*)(reply_buf);
|
|
|
|
*result_hostname = tor_malloc(result_len+1);
|
2018-06-29 16:16:57 +02:00
|
|
|
if (read_all_from_socket(s, *result_hostname, result_len)
|
|
|
|
!= (int) result_len) {
|
2006-12-26 23:41:49 +01:00
|
|
|
log_err(LD_NET, "Error reading hostname in socks5 response.");
|
2013-02-11 21:13:42 +01:00
|
|
|
goto err;
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2007-05-17 00:15:14 +02:00
|
|
|
(*result_hostname)[result_len] = '\0';
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
|
|
|
|
2013-02-11 21:13:42 +01:00
|
|
|
tor_close_socket(s);
|
2004-10-28 22:28:13 +02:00
|
|
|
return 0;
|
2013-02-11 21:13:42 +01:00
|
|
|
err:
|
|
|
|
tor_close_socket(s);
|
|
|
|
return -1;
|
2004-10-28 22:28:13 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Print a usage message and exit. */
|
2004-11-09 21:04:00 +01:00
|
|
|
static void
|
2004-11-02 04:02:17 +01:00
|
|
|
usage(void)
|
2004-10-28 22:28:13 +02:00
|
|
|
{
|
2015-08-30 20:57:24 +02:00
|
|
|
puts("Syntax: tor-resolve [-4] [-5] [-v] [-x] [-p port] "
|
2015-01-28 16:11:08 +01:00
|
|
|
"hostname [sockshost[:socksport]]");
|
2004-10-28 22:28:13 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Entry point to tor-resolve */
|
2004-10-28 22:28:13 +02:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2018-07-10 19:18:55 +02:00
|
|
|
tor_addr_t sockshost;
|
2008-09-29 16:18:47 +02:00
|
|
|
uint16_t socksport = 0, port_option = 0;
|
2011-05-23 22:59:41 +02:00
|
|
|
int isSocks4 = 0, isVerbose = 0, isReverse = 0;
|
2004-10-28 22:28:13 +02:00
|
|
|
char **arg;
|
|
|
|
int n_args;
|
2012-11-15 04:07:18 +01:00
|
|
|
tor_addr_t result;
|
2006-12-26 23:41:49 +01:00
|
|
|
char *result_hostname = NULL;
|
2004-10-28 22:28:13 +02:00
|
|
|
|
2014-09-11 05:30:37 +02:00
|
|
|
init_logging(1);
|
2014-09-29 18:57:07 +02:00
|
|
|
sandbox_disable_getaddrinfo_cache();
|
2008-01-03 23:44:32 +01:00
|
|
|
|
2004-10-28 22:28:13 +02:00
|
|
|
arg = &argv[1];
|
|
|
|
n_args = argc-1;
|
|
|
|
|
2004-11-30 09:54:08 +01:00
|
|
|
if (!n_args)
|
2004-10-28 22:28:13 +02:00
|
|
|
usage();
|
|
|
|
|
2005-01-16 03:21:14 +01:00
|
|
|
if (!strcmp(arg[0],"--version")) {
|
2016-12-18 16:03:14 +01:00
|
|
|
printf("Tor version %s.\n",VERSION);
|
2005-01-16 03:21:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2006-12-26 23:41:49 +01:00
|
|
|
while (n_args && *arg[0] == '-') {
|
|
|
|
if (!strcmp("-v", arg[0]))
|
|
|
|
isVerbose = 1;
|
|
|
|
else if (!strcmp("-4", arg[0]))
|
|
|
|
isSocks4 = 1;
|
|
|
|
else if (!strcmp("-5", arg[0]))
|
|
|
|
isSocks4 = 0;
|
|
|
|
else if (!strcmp("-x", arg[0]))
|
|
|
|
isReverse = 1;
|
2008-09-29 16:18:47 +02:00
|
|
|
else if (!strcmp("-p", arg[0])) {
|
|
|
|
int p;
|
|
|
|
if (n_args < 2) {
|
|
|
|
fprintf(stderr, "No arguments given to -p\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
p = atoi(arg[1]);
|
|
|
|
if (p<1 || p > 65535) {
|
|
|
|
fprintf(stderr, "-p requires a number between 1 and 65535\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
port_option = (uint16_t) p;
|
|
|
|
++arg; /* skip the port */
|
|
|
|
--n_args;
|
|
|
|
} else {
|
2006-12-26 23:41:49 +01:00
|
|
|
fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
++arg;
|
|
|
|
--n_args;
|
|
|
|
}
|
2005-01-16 03:21:14 +01:00
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
if (isSocks4 && isReverse) {
|
|
|
|
fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:49:57 +01:00
|
|
|
log_severity_list_t *severities =
|
|
|
|
tor_malloc_zero(sizeof(log_severity_list_t));
|
2008-03-13 23:18:38 +01:00
|
|
|
if (isVerbose)
|
2017-12-11 17:49:57 +01:00
|
|
|
set_log_severity_config(LOG_DEBUG, LOG_ERR, severities);
|
2008-03-13 23:18:38 +01:00
|
|
|
else
|
2017-12-11 17:49:57 +01:00
|
|
|
set_log_severity_config(LOG_WARN, LOG_ERR, severities);
|
|
|
|
add_stream_log(severities, "<stderr>", fileno(stderr));
|
|
|
|
tor_free(severities);
|
2008-03-05 23:31:39 +01:00
|
|
|
|
2004-10-28 22:28:13 +02:00
|
|
|
if (n_args == 1) {
|
2008-09-29 16:18:47 +02:00
|
|
|
log_debug(LD_CONFIG, "defaulting to localhost");
|
2018-07-10 19:18:55 +02:00
|
|
|
tor_addr_from_ipv4h(&sockshost, 0x7f000001u); /* localhost */
|
2008-09-29 16:18:47 +02:00
|
|
|
if (port_option) {
|
|
|
|
log_debug(LD_CONFIG, "Using port %d", (int)port_option);
|
|
|
|
socksport = port_option;
|
|
|
|
} else {
|
|
|
|
log_debug(LD_CONFIG, "defaulting to port 9050");
|
|
|
|
socksport = 9050; /* 9050 */
|
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
} else if (n_args == 2) {
|
2018-07-10 19:18:55 +02:00
|
|
|
if (tor_addr_port_lookup(arg[1], &sockshost, &socksport)<0) {
|
2004-10-28 22:28:13 +02:00
|
|
|
fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-09-29 16:18:47 +02:00
|
|
|
if (socksport && port_option && socksport != port_option) {
|
|
|
|
log_warn(LD_CONFIG, "Conflicting ports; using %d, not %d",
|
|
|
|
(int)socksport, (int)port_option);
|
|
|
|
} else if (port_option) {
|
|
|
|
socksport = port_option;
|
2009-03-10 01:52:45 +01:00
|
|
|
} else if (!socksport) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_CONFIG, "defaulting to port 9050");
|
2004-10-28 22:28:13 +02:00
|
|
|
socksport = 9050;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2004-12-22 06:29:06 +01:00
|
|
|
if (network_init()<0) {
|
2006-09-30 00:33:40 +02:00
|
|
|
log_err(LD_BUG,"Error initializing network; exiting.");
|
2005-01-16 03:21:14 +01:00
|
|
|
return 1;
|
2004-12-22 06:29:06 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 19:18:55 +02:00
|
|
|
if (do_resolve(arg[0], &sockshost, socksport, isReverse,
|
2006-12-26 23:41:49 +01:00
|
|
|
isSocks4 ? 4 : 5, &result,
|
|
|
|
&result_hostname))
|
2004-10-28 22:28:13 +02:00
|
|
|
return 1;
|
|
|
|
|
2006-12-26 23:41:49 +01:00
|
|
|
if (result_hostname) {
|
|
|
|
printf("%s\n", result_hostname);
|
|
|
|
} else {
|
2012-11-15 04:07:18 +01:00
|
|
|
printf("%s\n", fmt_addr(&result));
|
2006-12-26 23:41:49 +01:00
|
|
|
}
|
2004-10-28 22:28:13 +02:00
|
|
|
return 0;
|
|
|
|
}
|