tor-resolve.c 13 KB

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