Fix: quote torsocks script shell arguments

Reported-by: Jérémy Bobbio <lunar@debian.org>
Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Tests: add travis build status image to README

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Add portable is localhost function

This removes the utils_is_ipv4_local() call and move to a generic
portable way of checking if the sockaddr is localhost (both ipv4 and
ipv6 are supported).

Reported-by: Nick Mathewson <nickm@torproject.org>
Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: bad libc detection on system with libcap.so

The binary "yes" is much more generic and has a standard path in BSD,
Linux and OS X. Furthermore, it cannot be linked with libcap.so. But
this patch improves the grep regex to only detect "libc.so".

Fixes #21

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: README typos

Corrected some mistakes in the README file.

Closes #22

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Add localhost resolve utils function

Using a hostname, address family (v4/v6) and a buffer, this function
tries to match the hostname to the localhost names list local to
torsocks. If found, the given buffer is populated with the corresponding
IP address.

This is the first step to remove gethostent() in the code. It's
important for torsocks to be able to resolve localhost but it should NOT
try to resolve DNS with a local file (ex: /etc/hosts). One use cases is
for SSH to be able to bind to localhost for a SOCKS proxy for instance
using torsocks hence needing to resolve local name.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: remove gethostent() usage

Torsocks should not allow any local file resolution for external
hostname (not localhost stuff). Furthermore, gethostent() could do LDAP
listing thus clearly UNSAFE here.

This patch removes the use of gethostent() and replace it with the new
utils function that only resolves the hostname against localhost
hardcoded value.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Test: add unit test for sockaddr_is_localhost

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: bad reference in getaddrinfo() using inet_pton

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: socks5 connect use connection domain and correct len

The connection we are trying to do tells us if we need to connect to Tor
in ipv6 or ipv4 since the socket that we send back needs to be connected
with the right socket family.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: use libc close() when resolving through Tor

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: fd leak on tor resolve error

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: return right value with localhost resolve

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: build status markdown

Markdown was not being parsed.

Closes #24

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: assert conn->fd typo

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: IPv6 typo in socks5_send_resolve_ptr_request

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: change socks5_send_ptr_request to use address type

inet_ntop returns a pointer to the destination string, so an IPv6
address would always be converted as if it were an IPv4 address
(AF_INET).

Add the address family as a third parameter to socks5_send_ptr_request
to reliably determine the address type to request. The order of
parameters to socks5_send_ptr_request has also been changed.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: remove all variables with double underscore

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: deny connection to ANY address

It's possible to connect to 0.0.0.0/:: which are addresses that Tor
can't of course handle thus deny the call and send back EINVAL.

Signed-off-by: David Goulet <dgoulet@ev0ke.net>
Fix: check strdup return value

Reported-by: Nick Mathewson <nickm@torproject.org>
Signed-off-by: David Goulet <dgoulet@ev0ke.net>