tor-resolve.c 13 KB

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