1 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
2 * Copyright (c) 2007-2019, The Tor Project, Inc.
4 /* See LICENSE for licensing information */
8 #include "lib/arch/bytes.h"
9 #include "lib/log/log.h"
10 #include "lib/malloc/malloc.h"
11 #include "lib/net/address.h"
12 #include "lib/net/resolve.h"
13 #include "lib/net/socket.h"
14 #include "lib/sandbox/sandbox.h"
15 #include "lib/string/util_string.h"
17 #include "lib/net/socks5_status.h"
18 #include "trunnel/socks5.h"
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
46 #define RESPONSE_LEN_4 8
47 #define log_sock_error(act, _s) \
48 STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
49 tor_socket_strerror(tor_socket_errno(_s))); STMT_END
51 static void usage(void) ATTR_NORETURN
;
54 * Set <b>out</b> to a pointer to newly allocated buffer containing
55 * SOCKS4a RESOLVE request with <b>username</b> and <b>hostname</b>.
56 * Return number of bytes in the buffer if succeeded or -1 if failed.
59 build_socks4a_resolve_request(uint8_t **out
,
67 const char *errmsg
= NULL
;
68 uint8_t *output
= NULL
;
69 socks4_client_request_t
*rq
= socks4_client_request_new();
71 socks4_client_request_set_version(rq
, 4);
72 socks4_client_request_set_command(rq
, CMD_RESOLVE
);
73 socks4_client_request_set_port(rq
, 0);
74 socks4_client_request_set_addr(rq
, 0x00000001u
);
75 socks4_client_request_set_username(rq
, username
);
76 socks4_client_request_set_socks4a_addr_hostname(rq
, hostname
);
78 errmsg
= socks4_client_request_check(rq
);
83 ssize_t encoded_len
= socks4_client_request_encoded_len(rq
);
84 if (encoded_len
<= 0) {
85 errmsg
= "socks4_client_request_encoded_len failed";
89 output
= tor_malloc(encoded_len
);
90 memset(output
, 0, encoded_len
);
92 encoded_len
= socks4_client_request_encode(output
, encoded_len
, rq
);
93 if (encoded_len
<= 0) {
94 errmsg
= "socks4_client_request_encode failed";
101 socks4_client_request_free(rq
);
103 log_err(LD_NET
, "build_socks4a_resolve_request failed: %s", errmsg
);
107 return errmsg
? -1 : encoded_len
;
110 #define SOCKS5_ATYPE_HOSTNAME 0x03
111 #define SOCKS5_ATYPE_IPV4 0x01
112 #define SOCKS5_ATYPE_IPV6 0x04
115 * Set <b>out</b> to pointer to newly allocated buffer containing
116 * SOCKS5 RESOLVE/RESOLVE_PTR request with given <b>hostname<b>.
117 * Generate a reverse request if <b>reverse</b> is true.
118 * Return the number of bytes in the buffer if succeeded or -1 if failed.
121 build_socks5_resolve_request(uint8_t **out
,
122 const char *hostname
,
125 const char *errmsg
= NULL
;
126 uint8_t *outbuf
= NULL
;
131 is_ip_address
= tor_addr_parse(&addr
, hostname
) != -1;
132 if (!is_ip_address
&& reverse
) {
133 log_err(LD_GENERAL
, "Tried to do a reverse lookup on a non-IP!");
136 ipv6
= reverse
&& tor_addr_family(&addr
) == AF_INET6
;
137 addrlen
= reverse
? (ipv6
? 16 : 4) : 1 + strlen(hostname
);
138 if (addrlen
> UINT8_MAX
) {
139 log_err(LD_GENERAL
, "Hostname is too long!");
143 socks5_client_request_t
*rq
= socks5_client_request_new();
145 socks5_client_request_set_version(rq
, 5);
146 /* RESOLVE_PTR or RESOLVE */
147 socks5_client_request_set_command(rq
, reverse
? CMD_RESOLVE_PTR
:
149 socks5_client_request_set_reserved(rq
, 0);
151 uint8_t atype
= SOCKS5_ATYPE_HOSTNAME
;
153 atype
= ipv6
? SOCKS5_ATYPE_IPV6
: SOCKS5_ATYPE_IPV4
;
155 socks5_client_request_set_atype(rq
, atype
);
158 case SOCKS5_ATYPE_IPV4
: {
159 socks5_client_request_set_dest_addr_ipv4(rq
,
160 tor_addr_to_ipv4h(&addr
));
162 case SOCKS5_ATYPE_IPV6
: {
163 uint8_t *ipv6_array
=
164 socks5_client_request_getarray_dest_addr_ipv6(rq
);
166 tor_assert(ipv6_array
);
168 memcpy(ipv6_array
, tor_addr_to_in6_addr8(&addr
), 16);
171 case SOCKS5_ATYPE_HOSTNAME
: {
172 domainname_t
*dn
= domainname_new();
173 domainname_set_len(dn
, addrlen
- 1);
174 domainname_setlen_name(dn
, addrlen
- 1);
175 char *dn_buf
= domainname_getarray_name(dn
);
176 memcpy(dn_buf
, hostname
, addrlen
- 1);
178 errmsg
= domainname_check(dn
);
184 socks5_client_request_set_dest_addr_domainname(rq
, dn
);
188 tor_assert_unreached();
192 socks5_client_request_set_dest_port(rq
, 0);
194 errmsg
= socks5_client_request_check(rq
);
199 ssize_t encoded_len
= socks5_client_request_encoded_len(rq
);
200 if (encoded_len
< 0) {
201 errmsg
= "Cannot predict encoded length";
205 outbuf
= tor_malloc(encoded_len
);
206 memset(outbuf
, 0, encoded_len
);
208 encoded_len
= socks5_client_request_encode(outbuf
, encoded_len
, rq
);
209 if (encoded_len
< 0) {
210 errmsg
= "encoding failed";
217 socks5_client_request_free(rq
);
220 log_err(LD_NET
, "build_socks5_resolve_request failed with error: %s",
224 return errmsg
? -1 : encoded_len
;
227 /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
228 * <b>username</b> and <b>hostname</b> as provided. Return the number
229 * of bytes in the request. */
231 build_socks_resolve_request(uint8_t **out
,
232 const char *username
,
233 const char *hostname
,
238 tor_assert(username
);
239 tor_assert(hostname
);
241 tor_assert(version
== 4 || version
== 5);
244 return build_socks4a_resolve_request(out
, username
, hostname
);
245 } else if (version
== 5) {
246 return build_socks5_resolve_request(out
, hostname
, reverse
);
249 tor_assert_unreached();
254 onion_warning(const char *hostname
)
257 "%s is a hidden service; those don't have IP addresses. "
258 "You can use the AutomapHostsOnResolve option to have Tor return a "
259 "fake address for hidden services. Or you can have your "
260 "application send the address to Tor directly; we recommend an "
261 "application that uses SOCKS 5 with hostnames.",
265 /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
266 * *<b>addr_out</b> to the address it contains (in host order).
267 * Return 0 on success, -1 on error.
270 parse_socks4a_resolve_response(const char *hostname
,
271 const char *response
, size_t len
,
272 tor_addr_t
*addr_out
)
276 tor_assert(response
);
277 tor_assert(addr_out
);
279 socks4_server_reply_t
*reply
;
281 ssize_t parsed
= socks4_server_reply_parse(&reply
,
282 (const uint8_t *)response
,
286 log_warn(LD_PROTOCOL
, "Failed parsing SOCKS4a response");
287 result
= -1; goto cleanup
;
291 log_warn(LD_PROTOCOL
,"Truncated socks response.");
292 result
= -1; goto cleanup
;
295 if (socks4_server_reply_get_version(reply
) != 0) { /* version: 0 */
296 log_warn(LD_PROTOCOL
,"Nonzero version in socks response: bad format.");
297 result
= -1; goto cleanup
;
299 if (socks4_server_reply_get_port(reply
) != 0) { /* port: 0 */
300 log_warn(LD_PROTOCOL
,"Nonzero port in socks response: bad format.");
301 result
= -1; goto cleanup
;
303 status
= socks4_server_reply_get_status(reply
);
305 log_warn(LD_NET
,"Got status response '%d': socks request failed.", status
);
306 if (!strcasecmpend(hostname
, ".onion")) {
307 onion_warning(hostname
);
308 result
= -1; goto cleanup
;
310 result
= -1; goto cleanup
;
313 tor_addr_from_ipv4h(addr_out
, socks4_server_reply_get_addr(reply
));
316 socks4_server_reply_free(reply
);
320 /* It would be nice to let someone know what SOCKS5 issue a user may have */
322 socks5_reason_to_string(char reason
)
325 case SOCKS5_SUCCEEDED
:
327 case SOCKS5_GENERAL_ERROR
:
328 return "general error";
329 case SOCKS5_NOT_ALLOWED
:
330 return "not allowed";
331 case SOCKS5_NET_UNREACHABLE
:
332 return "network is unreachable";
333 case SOCKS5_HOST_UNREACHABLE
:
334 return "host is unreachable";
335 case SOCKS5_CONNECTION_REFUSED
:
336 return "connection refused";
337 case SOCKS5_TTL_EXPIRED
:
338 return "ttl expired";
339 case SOCKS5_COMMAND_NOT_SUPPORTED
:
340 return "command not supported";
341 case SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED
:
342 return "address type not supported";
344 return "unknown SOCKS5 code";
348 /** Send a resolve request for <b>hostname</b> to the Tor listening on
349 * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
350 * address (in host order) into *<b>result_addr</b>.
353 do_resolve(const char *hostname
,
354 const tor_addr_t
*sockshost
, uint16_t socksport
,
355 int reverse
, int version
,
356 tor_addr_t
*result_addr
, char **result_hostname
)
359 struct sockaddr_storage ss
;
364 tor_assert(hostname
);
365 tor_assert(result_addr
);
366 tor_assert(version
== 4 || version
== 5);
368 tor_addr_make_unspec(result_addr
);
369 *result_hostname
= NULL
;
371 s
= tor_open_socket(PF_INET
,SOCK_STREAM
,IPPROTO_TCP
);
373 log_sock_error("creating_socket", -1);
377 socklen
= tor_addr_to_sockaddr(sockshost
, socksport
,
378 (struct sockaddr
*)&ss
, sizeof(ss
));
380 if (connect(s
, (struct sockaddr
*)&ss
, socklen
)) {
381 log_sock_error("connecting to SOCKS host", s
);
386 socks5_client_version_t
*v
= socks5_client_version_new();
388 socks5_client_version_set_version(v
, 5);
389 socks5_client_version_set_n_methods(v
, 1);
390 socks5_client_version_setlen_methods(v
, 1);
391 socks5_client_version_set_methods(v
, 0, 0x00);
393 tor_assert(!socks5_client_version_check(v
));
394 ssize_t encoded_len
= socks5_client_version_encoded_len(v
);
395 tor_assert(encoded_len
> 0);
397 uint8_t *buf
= tor_malloc(encoded_len
);
398 encoded_len
= socks5_client_version_encode(buf
, encoded_len
, v
);
399 tor_assert(encoded_len
> 0);
401 socks5_client_version_free(v
);
403 if (write_all_to_socket(s
, (const char *)buf
,
404 encoded_len
) != encoded_len
) {
405 log_err(LD_NET
, "Error sending SOCKS5 method list.");
413 uint8_t method_buf
[2];
415 if (read_all_from_socket(s
, (char *)method_buf
, 2) != 2) {
416 log_err(LD_NET
, "Error reading SOCKS5 methods.");
420 socks5_server_method_t
*m
;
421 ssize_t parsed
= socks5_server_method_parse(&m
, method_buf
,
425 log_err(LD_NET
, "Failed to parse SOCKS5 method selection "
430 uint8_t method
= socks5_server_method_get_method(m
);
432 socks5_server_method_free(m
);
434 if (method
!= 0x00) {
435 log_err(LD_NET
, "Unrecognized socks authentication method: %u",
441 if ((len
= build_socks_resolve_request(&req
, "", hostname
, reverse
,
443 log_err(LD_BUG
,"Error generating SOCKS request");
447 if (write_all_to_socket(s
, (const char *)req
, len
) != len
) {
448 log_sock_error("sending SOCKS request", s
);
455 char reply_buf
[RESPONSE_LEN_4
];
456 if (read_all_from_socket(s
, reply_buf
, RESPONSE_LEN_4
) != RESPONSE_LEN_4
) {
457 log_err(LD_NET
, "Error reading SOCKS4 response.");
460 if (parse_socks4a_resolve_response(hostname
,
461 reply_buf
, RESPONSE_LEN_4
,
466 uint8_t reply_buf
[512];
468 len
= read_all_from_socket(s
, (char *)reply_buf
,
471 socks5_server_reply_t
*reply
;
473 ssize_t parsed
= socks5_server_reply_parse(&reply
,
477 log_err(LD_NET
, "Failed parsing SOCKS5 response");
482 log_err(LD_NET
, "Truncated SOCKS5 response");
486 /* Give a user some useful feedback about SOCKS5 errors */
487 uint8_t reply_field
= socks5_server_reply_get_reply(reply
);
488 if (reply_field
!= 0) {
489 log_warn(LD_NET
,"Got SOCKS5 status response '%u': %s",
490 (unsigned)reply_field
,
491 socks5_reason_to_string(reply_field
));
492 if (reply_field
== 4 && !strcasecmpend(hostname
, ".onion")) {
493 onion_warning(hostname
);
496 socks5_server_reply_free(reply
);
500 uint8_t atype
= socks5_server_reply_get_atype(reply
);
502 if (atype
== SOCKS5_ATYPE_IPV4
) {
504 tor_addr_from_ipv4h(result_addr
,
505 socks5_server_reply_get_bind_addr_ipv4(reply
));
506 } else if (atype
== SOCKS5_ATYPE_IPV6
) {
508 tor_addr_from_ipv6_bytes(result_addr
,
509 (const char *)socks5_server_reply_getarray_bind_addr_ipv6(reply
));
510 } else if (atype
== SOCKS5_ATYPE_HOSTNAME
) {
513 socks5_server_reply_get_bind_addr_domainname(reply
);
515 *result_hostname
= tor_strdup(domainname_getstr_name(dn
));
518 socks5_server_reply_free(reply
);
528 /** Print a usage message and exit. */
532 puts("Syntax: tor-resolve [-4] [-5] [-v] [-x] [-p port] "
533 "hostname [sockshost[:socksport]]");
537 /** Entry point to tor-resolve */
539 main(int argc
, char **argv
)
541 tor_addr_t sockshost
;
542 uint16_t socksport
= 0, port_option
= 0;
543 int isSocks4
= 0, isVerbose
= 0, isReverse
= 0;
547 char *result_hostname
= NULL
;
550 sandbox_disable_getaddrinfo_cache();
558 if (!strcmp(arg
[0],"--version")) {
559 printf("Tor version %s.\n",VERSION
);
562 while (n_args
&& *arg
[0] == '-') {
563 if (!strcmp("-v", arg
[0]))
565 else if (!strcmp("-4", arg
[0]))
567 else if (!strcmp("-5", arg
[0]))
569 else if (!strcmp("-x", arg
[0]))
571 else if (!strcmp("-p", arg
[0])) {
574 fprintf(stderr
, "No arguments given to -p\n");
578 if (p
<1 || p
> 65535) {
579 fprintf(stderr
, "-p requires a number between 1 and 65535\n");
582 port_option
= (uint16_t) p
;
583 ++arg
; /* skip the port */
586 fprintf(stderr
, "Unrecognized flag '%s'\n", arg
[0]);
593 if (isSocks4
&& isReverse
) {
594 fprintf(stderr
, "Reverse lookups not supported with SOCKS4a\n");
598 log_severity_list_t
*severities
=
599 tor_malloc_zero(sizeof(log_severity_list_t
));
601 set_log_severity_config(LOG_DEBUG
, LOG_ERR
, severities
);
603 set_log_severity_config(LOG_WARN
, LOG_ERR
, severities
);
604 add_stream_log(severities
, "<stderr>", fileno(stderr
));
605 tor_free(severities
);
608 log_debug(LD_CONFIG
, "defaulting to localhost");
609 tor_addr_from_ipv4h(&sockshost
, 0x7f000001u
); /* localhost */
611 log_debug(LD_CONFIG
, "Using port %d", (int)port_option
);
612 socksport
= port_option
;
614 log_debug(LD_CONFIG
, "defaulting to port 9050");
615 socksport
= 9050; /* 9050 */
617 } else if (n_args
== 2) {
618 if (tor_addr_port_lookup(arg
[1], &sockshost
, &socksport
)<0) {
619 fprintf(stderr
, "Couldn't parse/resolve address %s", arg
[1]);
622 if (socksport
&& port_option
&& socksport
!= port_option
) {
623 log_warn(LD_CONFIG
, "Conflicting ports; using %d, not %d",
624 (int)socksport
, (int)port_option
);
625 } else if (port_option
) {
626 socksport
= port_option
;
627 } else if (!socksport
) {
628 log_debug(LD_CONFIG
, "defaulting to port 9050");
635 if (network_init()<0) {
636 log_err(LD_BUG
,"Error initializing network; exiting.");
640 if (do_resolve(arg
[0], &sockshost
, socksport
, isReverse
,
641 isSocks4
? 4 : 5, &result
,
645 if (result_hostname
) {
646 printf("%s\n", result_hostname
);
648 printf("%s\n", fmt_addr(&result
));