tor-resolve.c 13 KB

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