tor-resolve.c 12 KB

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