tor-resolve.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
  2. * Copyright (c) 2007-2013, The Tor Project, Inc.
  3. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "compat.h"
  7. #include "../common/util.h"
  8. #include "address.h"
  9. #include "../common/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. #if defined(_MSC_VER) && (_MSC_VER <= 1300)
  33. #include <winsock.h>
  34. #else
  35. #include <winsock2.h>
  36. #include <ws2tcpip.h>
  37. #endif
  38. #endif
  39. #define RESPONSE_LEN_4 8
  40. #define log_sock_error(act, _s) \
  41. STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
  42. tor_socket_strerror(tor_socket_errno(_s))); STMT_END
  43. static void usage(void) ATTR_NORETURN;
  44. /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
  45. * <b>username</b> and <b>hostname</b> as provided. Return the number
  46. * of bytes in the request. */
  47. static ssize_t
  48. build_socks_resolve_request(char **out,
  49. const char *username,
  50. const char *hostname,
  51. int reverse,
  52. int version)
  53. {
  54. size_t len = 0;
  55. tor_assert(out);
  56. tor_assert(username);
  57. tor_assert(hostname);
  58. if (version == 4) {
  59. len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
  60. *out = tor_malloc(len);
  61. (*out)[0] = 4; /* SOCKS version 4 */
  62. (*out)[1] = '\xF0'; /* Command: resolve. */
  63. set_uint16((*out)+2, htons(0)); /* port: 0. */
  64. set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
  65. memcpy((*out)+8, username, strlen(username)+1);
  66. memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
  67. } else if (version == 5) {
  68. int is_ip_address;
  69. tor_addr_t addr;
  70. size_t addrlen;
  71. int ipv6;
  72. is_ip_address = tor_addr_parse(&addr, hostname) != -1;
  73. if (!is_ip_address && reverse) {
  74. log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
  75. return -1;
  76. }
  77. ipv6 = reverse && tor_addr_family(&addr) == AF_INET6;
  78. addrlen = reverse ? (ipv6 ? 16 : 4) : 1 + strlen(hostname);
  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. /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
  102. * *<b>addr_out</b> to the address it contains (in host order).
  103. * Return 0 on success, -1 on error.
  104. */
  105. static int
  106. parse_socks4a_resolve_response(const char *hostname,
  107. const char *response, size_t len,
  108. tor_addr_t *addr_out)
  109. {
  110. uint8_t status;
  111. tor_assert(response);
  112. tor_assert(addr_out);
  113. if (len < RESPONSE_LEN_4) {
  114. log_warn(LD_PROTOCOL,"Truncated socks response.");
  115. return -1;
  116. }
  117. if (((uint8_t)response[0])!=0) { /* version: 0 */
  118. log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
  119. return -1;
  120. }
  121. status = (uint8_t)response[1];
  122. if (get_uint16(response+2)!=0) { /* port: 0 */
  123. log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
  124. return -1;
  125. }
  126. if (status != 90) {
  127. log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
  128. if (!strcasecmpend(hostname, ".onion")) {
  129. log_warn(LD_NET,
  130. "%s is a hidden service; those don't have IP addresses. "
  131. "To connect to a hidden service, you need to send the hostname "
  132. "to Tor; we suggest an application that uses SOCKS 4a.",hostname);
  133. return -1;
  134. }
  135. return -1;
  136. }
  137. tor_addr_from_ipv4n(addr_out, get_uint32(response+4));
  138. return 0;
  139. }
  140. /* It would be nice to let someone know what SOCKS5 issue a user may have */
  141. static const char *
  142. socks5_reason_to_string(char reason)
  143. {
  144. switch (reason) {
  145. case SOCKS5_SUCCEEDED:
  146. return "succeeded";
  147. case SOCKS5_GENERAL_ERROR:
  148. return "general error";
  149. case SOCKS5_NOT_ALLOWED:
  150. return "not allowed";
  151. case SOCKS5_NET_UNREACHABLE:
  152. return "network is unreachable";
  153. case SOCKS5_HOST_UNREACHABLE:
  154. return "host is unreachable";
  155. case SOCKS5_CONNECTION_REFUSED:
  156. return "connection refused";
  157. case SOCKS5_TTL_EXPIRED:
  158. return "ttl expired";
  159. case SOCKS5_COMMAND_NOT_SUPPORTED:
  160. return "command not supported";
  161. case SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED:
  162. return "address type not supported";
  163. default:
  164. return "unknown SOCKS5 code";
  165. }
  166. }
  167. /** Send a resolve request for <b>hostname</b> to the Tor listening on
  168. * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
  169. * address (in host order) into *<b>result_addr</b>.
  170. */
  171. static int
  172. do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
  173. int reverse, int version,
  174. tor_addr_t *result_addr, char **result_hostname)
  175. {
  176. int s = -1;
  177. struct sockaddr_in socksaddr;
  178. char *req = NULL;
  179. ssize_t len = 0;
  180. tor_assert(hostname);
  181. tor_assert(result_addr);
  182. tor_assert(version == 4 || version == 5);
  183. tor_addr_make_unspec(result_addr);
  184. *result_hostname = NULL;
  185. s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  186. if (s<0) {
  187. log_sock_error("creating_socket", -1);
  188. return -1;
  189. }
  190. memset(&socksaddr, 0, sizeof(socksaddr));
  191. socksaddr.sin_family = AF_INET;
  192. socksaddr.sin_port = htons(socksport);
  193. socksaddr.sin_addr.s_addr = htonl(sockshost);
  194. if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
  195. log_sock_error("connecting to SOCKS host", s);
  196. goto err;
  197. }
  198. if (version == 5) {
  199. char method_buf[2];
  200. if (write_all(s, "\x05\x01\x00", 3, 1) != 3) {
  201. log_err(LD_NET, "Error sending SOCKS5 method list.");
  202. goto err;
  203. }
  204. if (read_all(s, method_buf, 2, 1) != 2) {
  205. log_err(LD_NET, "Error reading SOCKS5 methods.");
  206. goto err;
  207. }
  208. if (method_buf[0] != '\x05') {
  209. log_err(LD_NET, "Unrecognized socks version: %u",
  210. (unsigned)method_buf[0]);
  211. goto err;
  212. }
  213. if (method_buf[1] != '\x00') {
  214. log_err(LD_NET, "Unrecognized socks authentication method: %u",
  215. (unsigned)method_buf[1]);
  216. goto err;
  217. }
  218. }
  219. if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
  220. version))<0) {
  221. log_err(LD_BUG,"Error generating SOCKS request");
  222. tor_assert(!req);
  223. goto err;
  224. }
  225. if (write_all(s, req, len, 1) != len) {
  226. log_sock_error("sending SOCKS request", s);
  227. tor_free(req);
  228. goto err;
  229. }
  230. tor_free(req);
  231. if (version == 4) {
  232. char reply_buf[RESPONSE_LEN_4];
  233. if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
  234. log_err(LD_NET, "Error reading SOCKS4 response.");
  235. goto err;
  236. }
  237. if (parse_socks4a_resolve_response(hostname,
  238. reply_buf, RESPONSE_LEN_4,
  239. result_addr)<0) {
  240. goto err;
  241. }
  242. } else {
  243. char reply_buf[16];
  244. if (read_all(s, reply_buf, 4, 1) != 4) {
  245. log_err(LD_NET, "Error reading SOCKS5 response.");
  246. goto err;
  247. }
  248. if (reply_buf[0] != 5) {
  249. log_err(LD_NET, "Bad SOCKS5 reply version.");
  250. goto err;
  251. }
  252. /* Give a user some useful feedback about SOCKS5 errors */
  253. if (reply_buf[1] != 0) {
  254. log_warn(LD_NET,"Got SOCKS5 status response '%u': %s",
  255. (unsigned)reply_buf[1],
  256. socks5_reason_to_string(reply_buf[1]));
  257. if (reply_buf[1] == 4 && !strcasecmpend(hostname, ".onion")) {
  258. log_warn(LD_NET,
  259. "%s is a hidden service; those don't have IP addresses. "
  260. "To connect to a hidden service, you need to send the hostname "
  261. "to Tor; we suggest an application that uses SOCKS 4a.",
  262. 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] [-v] [-x] [-F] [-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();
  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. }