tor-resolve.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Copyright 2004-2005 Roger Dingledine, Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "orconfig.h"
  5. #include "compat.h"
  6. #include "util.h"
  7. #include "log.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <assert.h>
  13. #ifdef HAVE_NETINET_IN_H
  14. #include <netinet/in.h>
  15. #endif
  16. #ifdef HAVE_ARPA_INET_H
  17. #include <arpa/inet.h>
  18. #endif
  19. #ifdef HAVE_SYS_SOCKET_H
  20. #include <sys/socket.h>
  21. #endif
  22. #ifdef HAVE_SYS_TYPES_H
  23. #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
  24. #endif
  25. #ifdef HAVE_ERRNO_H
  26. #include <errno.h>
  27. #endif
  28. #ifdef MS_WINDOWS
  29. #if defined(_MSC_VER) && (_MSC_VER <= 1300)
  30. #include <winsock.h>
  31. #else
  32. #include <winsock2.h>
  33. #include <ws2tcpip.h>
  34. #endif
  35. #endif
  36. #define RESPONSE_LEN_4 8
  37. #define log_sock_error(act, _s) \
  38. STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act, \
  39. tor_socket_strerror(tor_socket_errno(_s))); STMT_END
  40. static void usage(void) ATTR_NORETURN;
  41. /** Set *<b>out</b> to a newly allocated SOCKS4a resolve request with
  42. * <b>username</b> and <b>hostname</b> as provided. Return the number
  43. * of bytes in the request. */
  44. static int
  45. build_socks_resolve_request(char **out,
  46. const char *username,
  47. const char *hostname,
  48. int reverse,
  49. int version)
  50. {
  51. size_t len = 0;
  52. tor_assert(out);
  53. tor_assert(username);
  54. tor_assert(hostname);
  55. if (version == 4) {
  56. len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
  57. *out = tor_malloc(len);
  58. (*out)[0] = 4; /* SOCKS version 4 */
  59. (*out)[1] = '\xF0'; /* Command: resolve. */
  60. set_uint16((*out)+2, htons(0)); /* port: 0. */
  61. set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
  62. memcpy((*out)+8, username, strlen(username)+1);
  63. memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
  64. } else if (version == 5) {
  65. int is_ip_address;
  66. struct in_addr in;
  67. size_t addrlen;
  68. is_ip_address = tor_inet_aton(hostname, &in);
  69. if (!is_ip_address && reverse) {
  70. log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
  71. return -1;
  72. }
  73. addrlen = is_ip_address ? 4 : 1 + strlen(hostname);
  74. len = 6 + addrlen;
  75. *out = tor_malloc(len);
  76. (*out)[0] = 5; /* SOCKS version 5 */
  77. (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
  78. (*out)[2] = 0; /* reserved. */
  79. (*out)[3] = is_ip_address ? 1 : 3;
  80. if (is_ip_address) {
  81. set_uint32((*out)+4, in.s_addr);
  82. } else {
  83. (*out)[4] = (char)(uint8_t)(addrlen - 1);
  84. memcpy((*out)+5, hostname, addrlen - 1);
  85. }
  86. set_uint16((*out)+4+addrlen, 0); /* port */
  87. } else {
  88. tor_assert(0);
  89. }
  90. return len;
  91. }
  92. /** Given a <b>len</b>-byte SOCKS4a response in <b>response</b>, set
  93. * *<b>addr_out</b> to the address it contains (in host order).
  94. * Return 0 on success, -1 on error.
  95. */
  96. static int
  97. parse_socks4a_resolve_response(const char *response, size_t len,
  98. uint32_t *addr_out)
  99. {
  100. uint8_t status;
  101. tor_assert(response);
  102. tor_assert(addr_out);
  103. if (len < RESPONSE_LEN_4) {
  104. log_warn(LD_PROTOCOL,"Truncated socks response.");
  105. return -1;
  106. }
  107. if (((uint8_t)response[0])!=0) { /* version: 0 */
  108. log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
  109. return -1;
  110. }
  111. status = (uint8_t)response[1];
  112. if (get_uint16(response+2)!=0) { /* port: 0 */
  113. log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
  114. return -1;
  115. }
  116. if (status != 90) {
  117. log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
  118. return -1;
  119. }
  120. *addr_out = ntohl(get_uint32(response+4));
  121. return 0;
  122. }
  123. /** Send a resolve request for <b>hostname</b> to the Tor listening on
  124. * <b>sockshost</b>:<b>socksport</b>. Store the resulting IPv4
  125. * address (in host order) into *<b>result_addr</b>.
  126. */
  127. static int
  128. do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
  129. int reverse, int version,
  130. uint32_t *result_addr, char **result_hostname)
  131. {
  132. int s;
  133. struct sockaddr_in socksaddr;
  134. char *req = NULL;
  135. int len = 0;
  136. tor_assert(hostname);
  137. tor_assert(result_addr);
  138. tor_assert(version == 4 || version == 5);
  139. *result_addr = 0;
  140. *result_hostname = NULL;
  141. s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
  142. if (s<0) {
  143. log_sock_error("creating_socket", -1);
  144. return -1;
  145. }
  146. memset(&socksaddr, 0, sizeof(socksaddr));
  147. socksaddr.sin_family = AF_INET;
  148. socksaddr.sin_port = htons(socksport);
  149. socksaddr.sin_addr.s_addr = htonl(sockshost);
  150. if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
  151. log_sock_error("connecting to SOCKS host", s);
  152. return -1;
  153. }
  154. if (version == 5) {
  155. char method_buf[2];
  156. if (write_all(s, "\x05\x01\x00", 3, 1) != 3) {
  157. log_err(LD_NET, "Error sending SOCKS5 method list.");
  158. return -1;
  159. }
  160. if (read_all(s, method_buf, 2, 1) != 2) {
  161. log_err(LD_NET, "Error reading SOCKS5 methods.");
  162. return -1;
  163. }
  164. if (method_buf[0] != '\x05') {
  165. log_err(LD_NET, "Unrecognized socks version: %u",
  166. (unsigned)method_buf[0]);
  167. return -1;
  168. }
  169. if (method_buf[1] != '\x00') {
  170. log_err(LD_NET, "Unrecognized socks authentication method: %u",
  171. (unsigned)method_buf[1]);
  172. return -1;
  173. }
  174. }
  175. if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
  176. version))<0) {
  177. log_err(LD_BUG,"Error generating SOCKS request");
  178. return -1;
  179. }
  180. if (write_all(s, req, len, 1) != len) {
  181. log_sock_error("sending SOCKS request", s);
  182. tor_free(req);
  183. return -1;
  184. }
  185. tor_free(req);
  186. if (version == 4) {
  187. char reply_buf[RESPONSE_LEN_4];
  188. if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
  189. log_err(LD_NET, "Error reading SOCKS4 response.");
  190. return -1;
  191. }
  192. if (parse_socks4a_resolve_response(reply_buf, RESPONSE_LEN_4,
  193. result_addr)<0){
  194. return -1;
  195. }
  196. } else {
  197. char reply_buf[4];
  198. if (read_all(s, reply_buf, 4, 1) != 4) {
  199. log_err(LD_NET, "Error reading SOCKS5 response.");
  200. return -1;
  201. }
  202. if (reply_buf[0] != 5) {
  203. log_err(LD_NET, "Bad SOCKS5 reply version.");
  204. return -1;
  205. }
  206. if (reply_buf[1] != 0) {
  207. log_warn(LD_NET,"Got status response '%u': SOCKS5 request failed.",
  208. (unsigned)reply_buf[1]);
  209. return -1;
  210. }
  211. if (reply_buf[3] == 1) {
  212. /* IPv4 address */
  213. if (read_all(s, reply_buf, 4, 1) != 4) {
  214. log_err(LD_NET, "Error reading address in socks5 response.");
  215. return -1;
  216. }
  217. *result_addr = ntohl(get_uint32(reply_buf));
  218. } else if (reply_buf[3] == 3) {
  219. size_t result_len;
  220. if (read_all(s, reply_buf, 1, 1) != 1) {
  221. log_err(LD_NET, "Error reading address_length in socks5 response.");
  222. return -1;
  223. }
  224. result_len = *(uint8_t*)(reply_buf);
  225. *result_hostname = tor_malloc(result_len+1);
  226. if (read_all(s, *result_hostname, result_len, 1) != (int) result_len) {
  227. log_err(LD_NET, "Error reading hostname in socks5 response.");
  228. return -1;
  229. }
  230. (*result_hostname)[result_len] = '\0';
  231. }
  232. }
  233. return 0;
  234. }
  235. /** Print a usage message and exit. */
  236. static void
  237. usage(void)
  238. {
  239. puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] "
  240. "hostname [sockshost:socksport]");
  241. exit(1);
  242. }
  243. /** Entry point to tor-resolve */
  244. int
  245. main(int argc, char **argv)
  246. {
  247. uint32_t sockshost;
  248. uint16_t socksport;
  249. int isSocks4 = 0, isVerbose = 0, isReverse = 0, force = 0;
  250. char **arg;
  251. int n_args;
  252. struct in_addr a;
  253. uint32_t result = 0;
  254. char *result_hostname = NULL;
  255. char buf[INET_NTOA_BUF_LEN];
  256. arg = &argv[1];
  257. n_args = argc-1;
  258. if (!n_args)
  259. usage();
  260. if (!strcmp(arg[0],"--version")) {
  261. printf("Tor version %s.\n",VERSION);
  262. return 0;
  263. }
  264. while (n_args && *arg[0] == '-') {
  265. if (!strcmp("-v", arg[0]))
  266. isVerbose = 1;
  267. else if (!strcmp("-4", arg[0]))
  268. isSocks4 = 1;
  269. else if (!strcmp("-5", arg[0]))
  270. isSocks4 = 0;
  271. else if (!strcmp("-x", arg[0]))
  272. isReverse = 1;
  273. else if (!strcmp("-F", arg[0]))
  274. force = 1;
  275. else {
  276. fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
  277. usage();
  278. }
  279. ++arg;
  280. --n_args;
  281. }
  282. if (isSocks4 && isReverse) {
  283. fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
  284. usage();
  285. }
  286. if (isVerbose) {
  287. add_stream_log(LOG_DEBUG, LOG_ERR, "<stderr>", stderr);
  288. } else {
  289. add_stream_log(LOG_WARN, LOG_ERR, "<stderr>", stderr);
  290. }
  291. if (n_args == 1) {
  292. log_debug(LD_CONFIG, "defaulting to localhost:9050");
  293. sockshost = 0x7f000001u; /* localhost */
  294. socksport = 9050; /* 9050 */
  295. } else if (n_args == 2) {
  296. if (parse_addr_port(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
  297. fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
  298. return 1;
  299. }
  300. if (socksport == 0) {
  301. log_debug(LD_CONFIG, "defaulting to port 9050");
  302. socksport = 9050;
  303. }
  304. } else {
  305. usage();
  306. }
  307. if (!strcasecmpend(arg[0], ".onion") && !force) {
  308. fprintf(stderr,
  309. "%s is a hidden service; those don't have IP addresses.\n\n"
  310. "To connect to a hidden service, you need to send the hostname\n"
  311. "to Tor; we suggest an application that uses SOCKS 4a.\n", arg[0]);
  312. return 1;
  313. }
  314. if (network_init()<0) {
  315. log_err(LD_BUG,"Error initializing network; exiting.");
  316. return 1;
  317. }
  318. if (do_resolve(arg[0], sockshost, socksport, isReverse,
  319. isSocks4 ? 4 : 5, &result,
  320. &result_hostname))
  321. return 1;
  322. if (result_hostname) {
  323. printf("%s\n", result_hostname);
  324. } else {
  325. a.s_addr = htonl(result);
  326. tor_inet_ntoa(&a, buf, sizeof(buf));
  327. printf("%s\n", buf);
  328. }
  329. return 0;
  330. }