tor-resolve.c 11 KB

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