Back to index

tor  0.2.3.18-rc
tor-resolve.c
Go to the documentation of this file.
00001 /* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson
00002  * Copyright (c) 2007-2012, The Tor Project, Inc.
00003  */
00004 /* See LICENSE for licensing information */
00005 
00006 #include "orconfig.h"
00007 
00008 #include "compat.h"
00009 #include "../common/util.h"
00010 #include "address.h"
00011 #include "../common/torlog.h"
00012 
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <stdarg.h>
00016 #include <string.h>
00017 #include <assert.h>
00018 
00019 #ifdef HAVE_NETINET_IN_H
00020 #include <netinet/in.h>
00021 #endif
00022 #ifdef HAVE_ARPA_INET_H
00023 #include <arpa/inet.h>
00024 #endif
00025 #ifdef HAVE_SYS_SOCKET_H
00026 #include <sys/socket.h>
00027 #endif
00028 #ifdef HAVE_SYS_TYPES_H
00029 #include <sys/types.h> /* Must be included before sys/stat.h for Ultrix */
00030 #endif
00031 #ifdef HAVE_ERRNO_H
00032 #include <errno.h>
00033 #endif
00034 
00035 #ifdef _WIN32
00036 #if defined(_MSC_VER) && (_MSC_VER <= 1300)
00037 #include <winsock.h>
00038 #else
00039 #include <winsock2.h>
00040 #include <ws2tcpip.h>
00041 #endif
00042 #endif
00043 
00044 #define RESPONSE_LEN_4 8
00045 #define log_sock_error(act, _s)                                         \
00046   STMT_BEGIN log_fn(LOG_ERR, LD_NET, "Error while %s: %s", act,         \
00047               tor_socket_strerror(tor_socket_errno(_s))); STMT_END
00048 
00049 static void usage(void) ATTR_NORETURN;
00050 
00054 static ssize_t
00055 build_socks_resolve_request(char **out,
00056                             const char *username,
00057                             const char *hostname,
00058                             int reverse,
00059                             int version)
00060 {
00061   size_t len = 0;
00062   tor_assert(out);
00063   tor_assert(username);
00064   tor_assert(hostname);
00065 
00066   if (version == 4) {
00067     len = 8 + strlen(username) + 1 + strlen(hostname) + 1;
00068     *out = tor_malloc(len);
00069     (*out)[0] = 4;      /* SOCKS version 4 */
00070     (*out)[1] = '\xF0'; /* Command: resolve. */
00071     set_uint16((*out)+2, htons(0)); /* port: 0. */
00072     set_uint32((*out)+4, htonl(0x00000001u)); /* addr: 0.0.0.1 */
00073     memcpy((*out)+8, username, strlen(username)+1);
00074     memcpy((*out)+8+strlen(username)+1, hostname, strlen(hostname)+1);
00075   } else if (version == 5) {
00076     int is_ip_address;
00077     struct in_addr in;
00078     size_t addrlen;
00079     is_ip_address = tor_inet_aton(hostname, &in);
00080     if (!is_ip_address && reverse) {
00081       log_err(LD_GENERAL, "Tried to do a reverse lookup on a non-IP!");
00082       return -1;
00083     }
00084     addrlen = reverse ? 4 : 1 + strlen(hostname);
00085     len = 6 + addrlen;
00086     *out = tor_malloc(len);
00087     (*out)[0] = 5; /* SOCKS version 5 */
00088     (*out)[1] = reverse ? '\xF1' : '\xF0'; /* RESOLVE_PTR or RESOLVE */
00089     (*out)[2] = 0; /* reserved. */
00090     (*out)[3] = reverse ? 1 : 3;
00091     if (reverse) {
00092       set_uint32((*out)+4, in.s_addr);
00093     } else {
00094       (*out)[4] = (char)(uint8_t)(addrlen - 1);
00095       memcpy((*out)+5, hostname, addrlen - 1);
00096     }
00097     set_uint16((*out)+4+addrlen, 0); /* port */
00098   } else {
00099     tor_assert(0);
00100   }
00101 
00102   return len;
00103 }
00104 
00109 static int
00110 parse_socks4a_resolve_response(const char *hostname,
00111                                const char *response, size_t len,
00112                                uint32_t *addr_out)
00113 {
00114   uint8_t status;
00115   tor_assert(response);
00116   tor_assert(addr_out);
00117 
00118   if (len < RESPONSE_LEN_4) {
00119     log_warn(LD_PROTOCOL,"Truncated socks response.");
00120     return -1;
00121   }
00122   if (((uint8_t)response[0])!=0) { /* version: 0 */
00123     log_warn(LD_PROTOCOL,"Nonzero version in socks response: bad format.");
00124     return -1;
00125   }
00126   status = (uint8_t)response[1];
00127   if (get_uint16(response+2)!=0) { /* port: 0 */
00128     log_warn(LD_PROTOCOL,"Nonzero port in socks response: bad format.");
00129     return -1;
00130   }
00131   if (status != 90) {
00132     log_warn(LD_NET,"Got status response '%d': socks request failed.", status);
00133     if (!strcasecmpend(hostname, ".onion")) {
00134       log_warn(LD_NET,
00135         "%s is a hidden service; those don't have IP addresses. "
00136         "To connect to a hidden service, you need to send the hostname "
00137         "to Tor; we suggest an application that uses SOCKS 4a.",hostname);
00138       return -1;
00139     }
00140     return -1;
00141   }
00142 
00143   *addr_out = ntohl(get_uint32(response+4));
00144   return 0;
00145 }
00146 
00147 /* It would be nice to let someone know what SOCKS5 issue a user may have */
00148 static const char *
00149 socks5_reason_to_string(char reason)
00150 {
00151   switch (reason) {
00152     case SOCKS5_SUCCEEDED:
00153       return "succeeded";
00154     case SOCKS5_GENERAL_ERROR:
00155       return "general error";
00156     case SOCKS5_NOT_ALLOWED:
00157       return "not allowed";
00158     case SOCKS5_NET_UNREACHABLE:
00159       return "network is unreachable";
00160     case SOCKS5_HOST_UNREACHABLE:
00161       return "host is unreachable";
00162     case SOCKS5_CONNECTION_REFUSED:
00163       return "connection refused";
00164     case SOCKS5_TTL_EXPIRED:
00165       return "ttl expired";
00166     case SOCKS5_COMMAND_NOT_SUPPORTED:
00167       return "command not supported";
00168     case SOCKS5_ADDRESS_TYPE_NOT_SUPPORTED:
00169       return "address type not supported";
00170     default:
00171       return "unknown SOCKS5 code";
00172   }
00173 }
00174 
00179 static int
00180 do_resolve(const char *hostname, uint32_t sockshost, uint16_t socksport,
00181            int reverse, int version,
00182            uint32_t *result_addr, char **result_hostname)
00183 {
00184   int s;
00185   struct sockaddr_in socksaddr;
00186   char *req = NULL;
00187   ssize_t len = 0;
00188 
00189   tor_assert(hostname);
00190   tor_assert(result_addr);
00191   tor_assert(version == 4 || version == 5);
00192 
00193   *result_addr = 0;
00194   *result_hostname = NULL;
00195 
00196   s = tor_open_socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
00197   if (s<0) {
00198     log_sock_error("creating_socket", -1);
00199     return -1;
00200   }
00201 
00202   memset(&socksaddr, 0, sizeof(socksaddr));
00203   socksaddr.sin_family = AF_INET;
00204   socksaddr.sin_port = htons(socksport);
00205   socksaddr.sin_addr.s_addr = htonl(sockshost);
00206   if (connect(s, (struct sockaddr*)&socksaddr, sizeof(socksaddr))) {
00207     log_sock_error("connecting to SOCKS host", s);
00208     return -1;
00209   }
00210 
00211   if (version == 5) {
00212     char method_buf[2];
00213     if (write_all(s, "\x05\x01\x00", 3, 1) != 3) {
00214       log_err(LD_NET, "Error sending SOCKS5 method list.");
00215       return -1;
00216     }
00217     if (read_all(s, method_buf, 2, 1) != 2) {
00218       log_err(LD_NET, "Error reading SOCKS5 methods.");
00219       return -1;
00220     }
00221     if (method_buf[0] != '\x05') {
00222       log_err(LD_NET, "Unrecognized socks version: %u",
00223               (unsigned)method_buf[0]);
00224       return -1;
00225     }
00226     if (method_buf[1] != '\x00') {
00227       log_err(LD_NET, "Unrecognized socks authentication method: %u",
00228               (unsigned)method_buf[1]);
00229       return -1;
00230     }
00231   }
00232 
00233   if ((len = build_socks_resolve_request(&req, "", hostname, reverse,
00234                                          version))<0) {
00235     log_err(LD_BUG,"Error generating SOCKS request");
00236     tor_assert(!req);
00237     return -1;
00238   }
00239   if (write_all(s, req, len, 1) != len) {
00240     log_sock_error("sending SOCKS request", s);
00241     tor_free(req);
00242     return -1;
00243   }
00244   tor_free(req);
00245 
00246   if (version == 4) {
00247     char reply_buf[RESPONSE_LEN_4];
00248     if (read_all(s, reply_buf, RESPONSE_LEN_4, 1) != RESPONSE_LEN_4) {
00249       log_err(LD_NET, "Error reading SOCKS4 response.");
00250       return -1;
00251     }
00252     if (parse_socks4a_resolve_response(hostname,
00253                                        reply_buf, RESPONSE_LEN_4,
00254                                        result_addr)<0) {
00255       return -1;
00256     }
00257   } else {
00258     char reply_buf[4];
00259     if (read_all(s, reply_buf, 4, 1) != 4) {
00260       log_err(LD_NET, "Error reading SOCKS5 response.");
00261       return -1;
00262     }
00263     if (reply_buf[0] != 5) {
00264       log_err(LD_NET, "Bad SOCKS5 reply version.");
00265       return -1;
00266     }
00267     /* Give a user some useful feedback about SOCKS5 errors */
00268     if (reply_buf[1] != 0) {
00269       log_warn(LD_NET,"Got SOCKS5 status response '%u': %s",
00270                (unsigned)reply_buf[1],
00271                socks5_reason_to_string(reply_buf[1]));
00272       if (reply_buf[1] == 4 && !strcasecmpend(hostname, ".onion")) {
00273         log_warn(LD_NET,
00274             "%s is a hidden service; those don't have IP addresses. "
00275             "To connect to a hidden service, you need to send the hostname "
00276             "to Tor; we suggest an application that uses SOCKS 4a.",
00277             hostname);
00278       }
00279       return -1;
00280     }
00281     if (reply_buf[3] == 1) {
00282       /* IPv4 address */
00283       if (read_all(s, reply_buf, 4, 1) != 4) {
00284         log_err(LD_NET, "Error reading address in socks5 response.");
00285         return -1;
00286       }
00287       *result_addr = ntohl(get_uint32(reply_buf));
00288     } else if (reply_buf[3] == 3) {
00289       size_t result_len;
00290       if (read_all(s, reply_buf, 1, 1) != 1) {
00291         log_err(LD_NET, "Error reading address_length in socks5 response.");
00292         return -1;
00293       }
00294       result_len = *(uint8_t*)(reply_buf);
00295       *result_hostname = tor_malloc(result_len+1);
00296       if (read_all(s, *result_hostname, result_len, 1) != (int) result_len) {
00297         log_err(LD_NET, "Error reading hostname in socks5 response.");
00298         return -1;
00299       }
00300       (*result_hostname)[result_len] = '\0';
00301     }
00302   }
00303 
00304   return 0;
00305 }
00306 
00308 static void
00309 usage(void)
00310 {
00311   puts("Syntax: tor-resolve [-4] [-v] [-x] [-F] [-p port] "
00312        "hostname [sockshost:socksport]");
00313   exit(1);
00314 }
00315 
00317 int
00318 main(int argc, char **argv)
00319 {
00320   uint32_t sockshost;
00321   uint16_t socksport = 0, port_option = 0;
00322   int isSocks4 = 0, isVerbose = 0, isReverse = 0;
00323   char **arg;
00324   int n_args;
00325   struct in_addr a;
00326   uint32_t result = 0;
00327   char *result_hostname = NULL;
00328   char buf[INET_NTOA_BUF_LEN];
00329   log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
00330 
00331   init_logging();
00332 
00333   arg = &argv[1];
00334   n_args = argc-1;
00335 
00336   if (!n_args)
00337     usage();
00338 
00339   if (!strcmp(arg[0],"--version")) {
00340     printf("Tor version %s.\n",VERSION);
00341     return 0;
00342   }
00343   while (n_args && *arg[0] == '-') {
00344     if (!strcmp("-v", arg[0]))
00345       isVerbose = 1;
00346     else if (!strcmp("-4", arg[0]))
00347       isSocks4 = 1;
00348     else if (!strcmp("-5", arg[0]))
00349       isSocks4 = 0;
00350     else if (!strcmp("-x", arg[0]))
00351       isReverse = 1;
00352     else if (!strcmp("-p", arg[0])) {
00353       int p;
00354       if (n_args < 2) {
00355         fprintf(stderr, "No arguments given to -p\n");
00356         usage();
00357       }
00358       p = atoi(arg[1]);
00359       if (p<1 || p > 65535) {
00360         fprintf(stderr, "-p requires a number between 1 and 65535\n");
00361         usage();
00362       }
00363       port_option = (uint16_t) p;
00364       ++arg; /* skip the port */
00365       --n_args;
00366     } else {
00367       fprintf(stderr, "Unrecognized flag '%s'\n", arg[0]);
00368       usage();
00369     }
00370     ++arg;
00371     --n_args;
00372   }
00373 
00374   if (isSocks4 && isReverse) {
00375     fprintf(stderr, "Reverse lookups not supported with SOCKS4a\n");
00376     usage();
00377   }
00378 
00379   if (isVerbose)
00380     set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
00381   else
00382     set_log_severity_config(LOG_WARN, LOG_ERR, s);
00383   add_stream_log(s, "<stderr>", fileno(stderr));
00384 
00385   if (n_args == 1) {
00386     log_debug(LD_CONFIG, "defaulting to localhost");
00387     sockshost = 0x7f000001u; /* localhost */
00388     if (port_option) {
00389       log_debug(LD_CONFIG, "Using port %d", (int)port_option);
00390       socksport = port_option;
00391     } else {
00392       log_debug(LD_CONFIG, "defaulting to port 9050");
00393       socksport = 9050; /* 9050 */
00394     }
00395   } else if (n_args == 2) {
00396     if (addr_port_lookup(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
00397       fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
00398       return 1;
00399     }
00400     if (socksport && port_option && socksport != port_option) {
00401       log_warn(LD_CONFIG, "Conflicting ports; using %d, not %d",
00402                (int)socksport, (int)port_option);
00403     } else if (port_option) {
00404       socksport = port_option;
00405     } else if (!socksport) {
00406       log_debug(LD_CONFIG, "defaulting to port 9050");
00407       socksport = 9050;
00408     }
00409   } else {
00410     usage();
00411   }
00412 
00413   if (network_init()<0) {
00414     log_err(LD_BUG,"Error initializing network; exiting.");
00415     return 1;
00416   }
00417 
00418   if (do_resolve(arg[0], sockshost, socksport, isReverse,
00419                  isSocks4 ? 4 : 5, &result,
00420                  &result_hostname))
00421     return 1;
00422 
00423   if (result_hostname) {
00424     printf("%s\n", result_hostname);
00425   } else {
00426     a.s_addr = htonl(result);
00427     tor_inet_ntoa(&a, buf, sizeof(buf));
00428     printf("%s\n", buf);
00429   }
00430   return 0;
00431 }
00432