tor-resolve.c 13 KB

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