tor-resolve.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
  2. * Copyright (c) 2007-2008, The Tor Project, Inc.
  3. */
  4. /* See LICENSE for licensing information */
  5. /* $Id$ */
  6. #include "orconfig.h"
  7. #include "compat.h"
  8. #include "util.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. /** Send a resolve request for <b>hostname</b> to the Tor listening on
  126. * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
  127. * address (in host order) into *<b>result_addr</b>.
  128. */
  129. static int
  130. do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
  131. int reverse, int version,
  132. uint32_t *result_addr, char **result_hostname)
  133. {
  134. int s;
  135. struct sockaddr_in socksaddr;
  136. char *req = NULL;
  137. int len = 0;
  138. tor_assert(hostname);
  139. tor_assert(result_addr);
  140. tor_assert(version == 4 || version == 5);
  141. *result_addr = 0;
  142. *result_hostname = NULL;
  143. s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  144. if (s<0) {
  145. log_sock_error("creating_socket", -1);
  146. return -1;
  147. }
  148. memset(&socksaddr, 0, sizeof(socksaddr));
  149. socksaddr.sin_family = AF_INET;
  150. socksaddr.sin_port = htons(socksport);
  151. socksaddr.sin_addr.s_addr = htonl(sockshost);
  152. if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
  153. log_sock_error("connecting to SOCKS host", s);
  154. return -1;
  155. }
  156. if (version == 5) {
  157. char method_buf[2];
  158. if (write_all(s, "\x05\x01\x00", 3, 1) != 3) {
  159. log_err(LD_NET, "Error sending SOCKS5 method list.");
  160. return -1;
  161. }
  162. if (read_all(s, method_buf, 2, 1) != 2) {
  163. log_err(LD_NET, "Error reading SOCKS5 methods.");
  164. return -1;
  165. }
  166. if (method_buf[0] != '\x05') {
  167. log_err(LD_NET, "Unrecognized socks version: %u",
  168. (unsigned)method_buf[0]);
  169. return -1;
  170. }
  171. if (method_buf[1] != '\x00') {
  172. log_err(LD_NET, "Unrecognized socks authentication method: %u",
  173. (unsigned)method_buf[1]);
  174. return -1;
  175. }
  176. }
  177. if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
  178. version))<0) {
  179. log_err(LD_BUG,"Error generating SOCKS request");
  180. tor_assert(!req);
  181. return -1;
  182. }
  183. if (write_all(s, req, len, 1) != len) {
  184. log_sock_error("sending SOCKS request", s);
  185. tor_free(req);
  186. return -1;
  187. }
  188. tor_free(req);
  189. if (version == 4) {
  190. char reply_buf[RESPONSE_LEN_4];
  191. if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
  192. log_err(LD_NET, "Error reading SOCKS4 response.");
  193. return -1;
  194. }
  195. if (parse_socks4a_resolve_response(reply_buf, RESPONSE_LEN_4,
  196. result_addr)<0){
  197. return -1;
  198. }
  199. } else {
  200. char reply_buf[4];
  201. if (read_all(s, reply_buf, 4, 1) != 4) {
  202. log_err(LD_NET, "Error reading SOCKS5 response.");
  203. return -1;
  204. }
  205. if (reply_buf[0] != 5) {
  206. log_err(LD_NET, "Bad SOCKS5 reply version.");
  207. return -1;
  208. }
  209. if (reply_buf[1] != 0) {
  210. log_warn(LD_NET,"Got status response '%u': SOCKS5 request failed.",
  211. (unsigned)reply_buf[1]);
  212. return -1;
  213. }
  214. if (reply_buf[3] == 1) {
  215. /* IPv4 address */
  216. if (read_all(s, reply_buf, 4, 1) != 4) {
  217. log_err(LD_NET, "Error reading address in socks5 response.");
  218. return -1;
  219. }
  220. *result_addr = ntohl(get_uint32(reply_buf));
  221. } else if (reply_buf[3] == 3) {
  222. size_t result_len;
  223. if (read_all(s, reply_buf, 1, 1) != 1) {
  224. log_err(LD_NET, "Error reading address_length in socks5 response.");
  225. return -1;
  226. }
  227. result_len = *(uint8_t*)(reply_buf);
  228. *result_hostname = tor_malloc(result_len+1);
  229. if (read_all(s, *result_hostname, result_len, 1) != (int) result_len) {
  230. log_err(LD_NET, "Error reading hostname in socks5 response.");
  231. return -1;
  232. }
  233. (*result_hostname)[result_len] = '\0';
  234. }
  235. }
  236. return 0;
  237. }
  238. /** Print a usage message and exit. */
  239. static void
  240. usage(void)
  241. {
  242. puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] "
  243. "hostname [sockshost:socksport]");
  244. exit(1);
  245. }
  246. /** Entry point to tor-resolve */
  247. int
  248. main(int argc, char **argv)
  249. {
  250. uint32_t sockshost;
  251. uint16_t socksport;
  252. int isSocks4 = 0, isVerbose = 0, isReverse = 0, force = 0;
  253. char **arg;
  254. int n_args;
  255. struct in_addr a;
  256. uint32_t result = 0;
  257. char *result_hostname = NULL;
  258. char buf[INET_NTOA_BUF_LEN];
  259. init_logging();
  260. arg = &argv[1];
  261. n_args = argc-1;
  262. if (!n_args)
  263. usage();
  264. if (!strcmp(arg[0],"--version")) {
  265. printf("Tor version %s.\n",VERSION);
  266. return 0;
  267. }
  268. while (n_args && *arg[0] == '-') {
  269. if (!strcmp("-v", arg[0]))
  270. isVerbose = 1;
  271. else if (!strcmp("-4", arg[0]))
  272. isSocks4 = 1;
  273. else if (!strcmp("-5", arg[0]))
  274. isSocks4 = 0;
  275. else if (!strcmp("-x", arg[0]))
  276. isReverse = 1;
  277. else if (!strcmp("-F", arg[0]))
  278. force = 1;
  279. else {
  280. fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
  281. usage();
  282. }
  283. ++arg;
  284. --n_args;
  285. }
  286. if (isSocks4 && isReverse) {
  287. fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
  288. usage();
  289. }
  290. if (isVerbose) {
  291. add_stream_log(LOG_DEBUG, LOG_ERR, "<stderr>", stderr);
  292. } else {
  293. add_stream_log(LOG_WARN, LOG_ERR, "<stderr>", stderr);
  294. }
  295. if (n_args == 1) {
  296. log_debug(LD_CONFIG, "defaulting to localhost:9050");
  297. sockshost = 0x7f000001u; /* localhost */
  298. socksport = 9050; /* 9050 */
  299. } else if (n_args == 2) {
  300. if (parse_addr_port(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
  301. fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
  302. return 1;
  303. }
  304. if (socksport == 0) {
  305. log_debug(LD_CONFIG, "defaulting to port 9050");
  306. socksport = 9050;
  307. }
  308. } else {
  309. usage();
  310. }
  311. if (!strcasecmpend(arg[0], ".onion") && !force) {
  312. fprintf(stderr,
  313. "%s is a hidden service; those don't have IP addresses.\n\n"
  314. "To connect to a hidden service, you need to send the hostname\n"
  315. "to Tor; we suggest an application that uses SOCKS 4a.\n", arg[0]);
  316. return 1;
  317. }
  318. if (network_init()<0) {
  319. log_err(LD_BUG,"Error initializing network; exiting.");
  320. return 1;
  321. }
  322. if (do_resolve(arg[0], sockshost, socksport, isReverse,
  323. isSocks4 ? 4 : 5, &result,
  324. &result_hostname))
  325. return 1;
  326. if (result_hostname) {
  327. printf("%s\n", result_hostname);
  328. } else {
  329. a.s_addr = htonl(result);
  330. tor_inet_ntoa(&a, buf, sizeof(buf));
  331. printf("%s\n", buf);
  332. }
  333. return 0;
  334. }