tor-resolve.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
  2. * Copyright (c) 2007-2018, The Tor Project, Inc.
  3. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/arch/bytes.h"
  7. #include "lib/log/log.h"
  8. #include "lib/malloc/malloc.h"
  9. #include "lib/net/address.h"
  10. #include "lib/net/resolve.h"
  11. #include "lib/net/socket.h"
  12. #include "lib/sandbox/sandbox.h"
  13. #include "lib/string/util_string.h"
  14. #include "lib/net/socks5_status.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #ifdef HAVE_NETINET_IN_H
  20. #include <netinet/in.h>
  21. #endif
  22. #ifdef HAVE_ARPA_INET_H
  23. #include <arpa/inet.h>
  24. #endif
  25. #ifdef HAVE_SYS_SOCKET_H
  26. #include <sys/socket.h>
  27. #endif
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  30. #endif
  31. #ifdef HAVE_ERRNO_H
  32. #include <errno.h>
  33. #endif
  34. #ifdef _WIN32
  35. #include <winsock2.h>
  36. #include <ws2tcpip.h>
  37. #endif
  38. #define RESPONSE_LEN_4 8
  39. #define log_sock_error(act, _s) \
  40. STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
  41. tor_socket_strerror(tor_socket_errno(_s))); STMT_END
  42. static void usage(void) ATTR_NORETURN;
  43. /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
  44. * <b>username</b> and <b>hostname</b> as provided. Return the number
  45. * of bytes in the request. */
  46. static ssize_t
  47. build_socks_resolve_request(char **out,
  48. const char *username,
  49. const char *hostname,
  50. int reverse,
  51. int version)
  52. {
  53. size_t len = 0;
  54. tor_assert(out);
  55. tor_assert(username);
  56. tor_assert(hostname);
  57. if (version == 4) {
  58. len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
  59. *out = tor_malloc(len);
  60. (*out)[0] = 4; /* SOCKS version 4 */
  61. (*out)[1] = '\xF0'; /* Command: resolve. */
  62. set_uint16((*out)+2, htons(0)); /* port: 0. */
  63. set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
  64. memcpy((*out)+8, username, strlen(username)+1);
  65. memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
  66. } else if (version == 5) {
  67. int is_ip_address;
  68. tor_addr_t addr;
  69. size_t addrlen;
  70. int ipv6;
  71. is_ip_address = tor_addr_parse(&addr, hostname) != -1;
  72. if (!is_ip_address && reverse) {
  73. log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
  74. return -1;
  75. }
  76. ipv6 = reverse && tor_addr_family(&addr) == AF_INET6;
  77. addrlen = reverse ? (ipv6 ? 16 : 4) : 1 + strlen(hostname);
  78. if (addrlen > UINT8_MAX) {
  79. log_err(LD_GENERAL, "Hostname is too long!");
  80. return -1;
  81. }
  82. len = 6 + addrlen;
  83. *out = tor_malloc(len);
  84. (*out)[0] = 5; /* SOCKS version 5 */
  85. (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
  86. (*out)[2] = 0; /* reserved. */
  87. if (reverse) {
  88. (*out)[3] = ipv6 ? 4 : 1;
  89. if (ipv6)
  90. memcpy((*out)+4, tor_addr_to_in6_addr8(&addr), 16);
  91. else
  92. set_uint32((*out)+4, tor_addr_to_ipv4n(&addr));
  93. } else {
  94. (*out)[3] = 3;
  95. (*out)[4] = (char)(uint8_t)(addrlen - 1);
  96. memcpy((*out)+5, hostname, addrlen - 1);
  97. }
  98. set_uint16((*out)+4+addrlen, 0); /* port */
  99. } else {
  100. tor_assert(0);
  101. }
  102. return len;
  103. }
  104. static void
  105. onion_warning(const char *hostname)
  106. {
  107. log_warn(LD_NET,
  108. "%s is a hidden service; those don't have IP addresses. "
  109. "You can use the AutomapHostsOnResolve option to have Tor return a "
  110. "fake address for hidden services. Or you can have your "
  111. "application send the address to Tor directly; we recommend an "
  112. "application that uses SOCKS 5 with hostnames.",
  113. hostname);
  114. }
  115. /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
  116. * *<b>addr_out</b> to the address it contains (in host order).
  117. * Return 0 on success, -1 on error.
  118. */
  119. static int
  120. parse_socks4a_resolve_response(const char *hostname,
  121. const char *response, size_t len,
  122. tor_addr_t *addr_out)
  123. {
  124. uint8_t status;
  125. tor_assert(response);
  126. tor_assert(addr_out);
  127. if (len < RESPONSE_LEN_4) {
  128. log_warn(LD_PROTOCOL,"Truncated socks response.");
  129. return -1;
  130. }
  131. if (((uint8_t)response[0])!=0) { /* version: 0 */
  132. log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
  133. return -1;
  134. }
  135. status = (uint8_t)response[1];
  136. if (get_uint16(response+2)!=0) { /* port: 0 */
  137. log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
  138. return -1;
  139. }
  140. if (status != 90) {
  141. log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
  142. if (!strcasecmpend(hostname, ".onion")) {
  143. onion_warning(hostname);
  144. return -1;
  145. }
  146. return -1;
  147. }
  148. tor_addr_from_ipv4n(addr_out, get_uint32(response+4));
  149. return 0;
  150. }
  151. /* It would be nice to let someone know what SOCKS5 issue a user may have */
  152. static const char *
  153. socks5_reason_to_string(char reason)
  154. {
  155. switch (reason) {
  156. case SOCKS5_SUCCEEDED:
  157. return "succeeded";
  158. case SOCKS5_GENERAL_ERROR:
  159. return "general error";
  160. case SOCKS5_NOT_ALLOWED:
  161. return "not allowed";
  162. case SOCKS5_NET_UNREACHABLE:
  163. return "network is unreachable";
  164. case SOCKS5_HOST_UNREACHABLE:
  165. return "host is unreachable";
  166. case SOCKS5_CONNECTION_REFUSED:
  167. return "connection refused";
  168. case SOCKS5_TTL_EXPIRED:
  169. return "ttl expired";
  170. case SOCKS5_COMMAND_NOT_SUPPORTED:
  171. return "command not supported";
  172. case SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED:
  173. return "address type not supported";
  174. default:
  175. return "unknown SOCKS5 code";
  176. }
  177. }
  178. /** Send a resolve request for <b>hostname</b> to the Tor listening on
  179. * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
  180. * address (in host order) into *<b>result_addr</b>.
  181. */
  182. static int
  183. do_resolve(const char *hostname,
  184. const tor_addr_t *sockshost, uint16_t socksport,
  185. int reverse, int version,
  186. tor_addr_t *result_addr, char **result_hostname)
  187. {
  188. int s = -1;
  189. struct sockaddr_storage ss;
  190. socklen_t socklen;
  191. char *req = NULL;
  192. ssize_t len = 0;
  193. tor_assert(hostname);
  194. tor_assert(result_addr);
  195. tor_assert(version == 4 || version == 5);
  196. tor_addr_make_unspec(result_addr);
  197. *result_hostname = NULL;
  198. s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  199. if (s<0) {
  200. log_sock_error("creating_socket", -1);
  201. return -1;
  202. }
  203. socklen = tor_addr_to_sockaddr(sockshost, socksport,
  204. (struct sockaddr *)&ss, sizeof(ss));
  205. if (connect(s, (struct sockaddr*)&ss, socklen)) {
  206. log_sock_error("connecting to SOCKS host", s);
  207. goto err;
  208. }
  209. if (version == 5) {
  210. char method_buf[2];
  211. if (write_all_to_socket(s, "\x05\x01\x00", 3) != 3) {
  212. log_err(LD_NET, "Error sending SOCKS5 method list.");
  213. goto err;
  214. }
  215. if (read_all_from_socket(s, method_buf, 2) != 2) {
  216. log_err(LD_NET, "Error reading SOCKS5 methods.");
  217. goto err;
  218. }
  219. if (method_buf[0] != '\x05') {
  220. log_err(LD_NET, "Unrecognized socks version: %u",
  221. (unsigned)method_buf[0]);
  222. goto err;
  223. }
  224. if (method_buf[1] != '\x00') {
  225. log_err(LD_NET, "Unrecognized socks authentication method: %u",
  226. (unsigned)method_buf[1]);
  227. goto err;
  228. }
  229. }
  230. if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
  231. version))<0) {
  232. log_err(LD_BUG,"Error generating SOCKS request");
  233. tor_assert(!req);
  234. goto err;
  235. }
  236. if (write_all_to_socket(s, req, len) != len) {
  237. log_sock_error("sending SOCKS request", s);
  238. tor_free(req);
  239. goto err;
  240. }
  241. tor_free(req);
  242. if (version == 4) {
  243. char reply_buf[RESPONSE_LEN_4];
  244. if (read_all_from_socket(s, reply_buf, RESPONSE_LEN_4) != RESPONSE_LEN_4) {
  245. log_err(LD_NET, "Error reading SOCKS4 response.");
  246. goto err;
  247. }
  248. if (parse_socks4a_resolve_response(hostname,
  249. reply_buf, RESPONSE_LEN_4,
  250. result_addr)<0) {
  251. goto err;
  252. }
  253. } else {
  254. char reply_buf[16];
  255. if (read_all_from_socket(s, reply_buf, 4) != 4) {
  256. log_err(LD_NET, "Error reading SOCKS5 response.");
  257. goto err;
  258. }
  259. if (reply_buf[0] != 5) {
  260. log_err(LD_NET, "Bad SOCKS5 reply version.");
  261. goto err;
  262. }
  263. /* Give a user some useful feedback about SOCKS5 errors */
  264. if (reply_buf[1] != 0) {
  265. log_warn(LD_NET,"Got SOCKS5 status response '%u': %s",
  266. (unsigned)reply_buf[1],
  267. socks5_reason_to_string(reply_buf[1]));
  268. if (reply_buf[1] == 4 && !strcasecmpend(hostname, ".onion")) {
  269. onion_warning(hostname);
  270. }
  271. goto err;
  272. }
  273. if (reply_buf[3] == 1) {
  274. /* IPv4 address */
  275. if (read_all_from_socket(s, reply_buf, 4) != 4) {
  276. log_err(LD_NET, "Error reading address in socks5 response.");
  277. goto err;
  278. }
  279. tor_addr_from_ipv4n(result_addr, get_uint32(reply_buf));
  280. } else if (reply_buf[3] == 4) {
  281. /* IPv6 address */
  282. if (read_all_from_socket(s, reply_buf, 16) != 16) {
  283. log_err(LD_NET, "Error reading address in socks5 response.");
  284. goto err;
  285. }
  286. tor_addr_from_ipv6_bytes(result_addr, reply_buf);
  287. } else if (reply_buf[3] == 3) {
  288. /* Domain name */
  289. size_t result_len;
  290. if (read_all_from_socket(s, reply_buf, 1) != 1) {
  291. log_err(LD_NET, "Error reading address_length in socks5 response.");
  292. goto err;
  293. }
  294. result_len = *(uint8_t*)(reply_buf);
  295. *result_hostname = tor_malloc(result_len+1);
  296. if (read_all_from_socket(s, *result_hostname, result_len)
  297. != (int) result_len) {
  298. log_err(LD_NET, "Error reading hostname in socks5 response.");
  299. goto err;
  300. }
  301. (*result_hostname)[result_len] = '\0';
  302. }
  303. }
  304. tor_close_socket(s);
  305. return 0;
  306. err:
  307. tor_close_socket(s);
  308. return -1;
  309. }
  310. /** Print a usage message and exit. */
  311. static void
  312. usage(void)
  313. {
  314. puts("Syntax: tor-resolve [-4] [-5] [-v] [-x] [-p port] "
  315. "hostname [sockshost[:socksport]]");
  316. exit(1);
  317. }
  318. /** Entry point to tor-resolve */
  319. int
  320. main(int argc, char **argv)
  321. {
  322. tor_addr_t sockshost;
  323. uint16_t socksport = 0, port_option = 0;
  324. int isSocks4 = 0, isVerbose = 0, isReverse = 0;
  325. char **arg;
  326. int n_args;
  327. tor_addr_t result;
  328. char *result_hostname = NULL;
  329. init_logging(1);
  330. sandbox_disable_getaddrinfo_cache();
  331. arg = &argv[1];
  332. n_args = argc-1;
  333. if (!n_args)
  334. usage();
  335. if (!strcmp(arg[0],"--version")) {
  336. printf("Tor version %s.\n",VERSION);
  337. return 0;
  338. }
  339. while (n_args && *arg[0] == '-') {
  340. if (!strcmp("-v", arg[0]))
  341. isVerbose = 1;
  342. else if (!strcmp("-4", arg[0]))
  343. isSocks4 = 1;
  344. else if (!strcmp("-5", arg[0]))
  345. isSocks4 = 0;
  346. else if (!strcmp("-x", arg[0]))
  347. isReverse = 1;
  348. else if (!strcmp("-p", arg[0])) {
  349. int p;
  350. if (n_args < 2) {
  351. fprintf(stderr, "No arguments given to -p\n");
  352. usage();
  353. }
  354. p = atoi(arg[1]);
  355. if (p<1 || p > 65535) {
  356. fprintf(stderr, "-p requires a number between 1 and 65535\n");
  357. usage();
  358. }
  359. port_option = (uint16_t) p;
  360. ++arg; /* skip the port */
  361. --n_args;
  362. } else {
  363. fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
  364. usage();
  365. }
  366. ++arg;
  367. --n_args;
  368. }
  369. if (isSocks4 && isReverse) {
  370. fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
  371. usage();
  372. }
  373. log_severity_list_t *severities =
  374. tor_malloc_zero(sizeof(log_severity_list_t));
  375. if (isVerbose)
  376. set_log_severity_config(LOG_DEBUG, LOG_ERR, severities);
  377. else
  378. set_log_severity_config(LOG_WARN, LOG_ERR, severities);
  379. add_stream_log(severities, "<stderr>", fileno(stderr));
  380. tor_free(severities);
  381. if (n_args == 1) {
  382. log_debug(LD_CONFIG, "defaulting to localhost");
  383. tor_addr_from_ipv4h(&sockshost, 0x7f000001u); /* localhost */
  384. if (port_option) {
  385. log_debug(LD_CONFIG, "Using port %d", (int)port_option);
  386. socksport = port_option;
  387. } else {
  388. log_debug(LD_CONFIG, "defaulting to port 9050");
  389. socksport = 9050; /* 9050 */
  390. }
  391. } else if (n_args == 2) {
  392. if (tor_addr_port_lookup(arg[1], &sockshost, &socksport)<0) {
  393. fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
  394. return 1;
  395. }
  396. if (socksport && port_option && socksport != port_option) {
  397. log_warn(LD_CONFIG, "Conflicting ports; using %d, not %d",
  398. (int)socksport, (int)port_option);
  399. } else if (port_option) {
  400. socksport = port_option;
  401. } else if (!socksport) {
  402. log_debug(LD_CONFIG, "defaulting to port 9050");
  403. socksport = 9050;
  404. }
  405. } else {
  406. usage();
  407. }
  408. if (network_init()<0) {
  409. log_err(LD_BUG,"Error initializing network; exiting.");
  410. return 1;
  411. }
  412. if (do_resolve(arg[0], &sockshost, socksport, isReverse,
  413. isSocks4 ? 4 : 5, &result,
  414. &result_hostname))
  415. return 1;
  416. if (result_hostname) {
  417. printf("%s\n", result_hostname);
  418. } else {
  419. printf("%s\n", fmt_addr(&result));
  420. }
  421. return 0;
  422. }