tor-resolve.c 9.6 KB

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