From d8d42eec3475419a1ac1b4b1dc69db81c9d1396b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien-Pierre=20Av=C3=A9rous?= Date: Fri, 4 Mar 2016 19:11:12 +0100 Subject: [PATCH] - Some litle cleaning in tsocks. - Fix SLProcess parenting check method. - Fix message. --- TorProxifier/TorProxifier/TPProcess.m | 6 +- .../TorProxifier/en.lproj/Localizable.strings | 6 +- .../libcontrol/control/TPNetworkHelper.m | 1 - .../tsocks.xcodeproj/project.pbxproj | 12 ++ .../TorProxifier/libtsocks/tsocks/config.h | 12 +- .../TorProxifier/libtsocks/tsocks/tsocks.c | 142 ++++++------------ 6 files changed, 63 insertions(+), 116 deletions(-) diff --git a/TorProxifier/TorProxifier/TPProcess.m b/TorProxifier/TorProxifier/TPProcess.m index 31bf632..fb1b847 100644 --- a/TorProxifier/TorProxifier/TPProcess.m +++ b/TorProxifier/TorProxifier/TPProcess.m @@ -218,12 +218,12 @@ - (BOOL)parentOfPID:(pid_t)pid while (1) { - pid_t ppid = [[self class] PPIDForPID:pid]; + pid = [[self class] PPIDForPID:pid]; - if (ppid < 0) + if (pid <= 0) return NO; - if (ppid == rpid) + if (pid == rpid) return YES; } diff --git a/TorProxifier/TorProxifier/en.lproj/Localizable.strings b/TorProxifier/TorProxifier/en.lproj/Localizable.strings index a6c2aa9..15c82f9 100644 --- a/TorProxifier/TorProxifier/en.lproj/Localizable.strings +++ b/TorProxifier/TorProxifier/en.lproj/Localizable.strings @@ -28,9 +28,9 @@ "process_launching" = "Process is launching (%lu%%)."; "process_launched" = "Process is tunneled through Tor."; -"process_err_tor_socket" = "Can't verify that sockets are well redirected to Tor network. Launch of the process is stopped."; -"process_err_tor_nsurlconnection" = "Can't verify that NSURLConnection are well redirected to Tor network. Launch of the process is stopped."; -"process_err_tor_nsurlsession" = "Can't verify that NSURLSession are well redirected to Tor network. Launch of the process is stopped."; +"process_err_tor_socket" = "Can't verify that sockets are well redirected to Tor network. Launch of the process is suspended."; +"process_err_tor_nsurlconnection" = "Can't verify that NSURLConnection are well redirected to Tor network. Launch of the process is suspended."; +"process_err_tor_nsurlsession" = "Can't verify that NSURLSession are well redirected to Tor network. Launch of the process is suspended."; /* Drop */ "drop_app" = "Drop application here"; diff --git a/TorProxifier/TorProxifier/libcontrol/control/TPNetworkHelper.m b/TorProxifier/TorProxifier/libcontrol/control/TPNetworkHelper.m index dd44135..207dda3 100644 --- a/TorProxifier/TorProxifier/libcontrol/control/TPNetworkHelper.m +++ b/TorProxifier/TorProxifier/libcontrol/control/TPNetworkHelper.m @@ -118,7 +118,6 @@ __strong dispatch_block_t __concat(_exitBlock_, __LINE__) __attribute__((cleanup return nil; } - // Resolve host. struct addrinfo hints, *res, *res0 = NULL; int s = -1; diff --git a/TorProxifier/TorProxifier/libtsocks/tsocks.xcodeproj/project.pbxproj b/TorProxifier/TorProxifier/libtsocks/tsocks.xcodeproj/project.pbxproj index 779a0c7..60e1db2 100644 --- a/TorProxifier/TorProxifier/libtsocks/tsocks.xcodeproj/project.pbxproj +++ b/TorProxifier/TorProxifier/libtsocks/tsocks.xcodeproj/project.pbxproj @@ -17,6 +17,7 @@ E8A78E4B1C5AB92E00D3C999 /* tsocks.h in Headers */ = {isa = PBXBuildFile; fileRef = E8A78E421C5AB92E00D3C999 /* tsocks.h */; }; E8A78E4E1C5ABA6A00D3C999 /* parser.c in Sources */ = {isa = PBXBuildFile; fileRef = E8A78E4C1C5ABA6A00D3C999 /* parser.c */; }; E8A78E4F1C5ABA6A00D3C999 /* parser.h in Headers */ = {isa = PBXBuildFile; fileRef = E8A78E4D1C5ABA6A00D3C999 /* parser.h */; }; + E8B39B1A1C89BC5E007B7280 /* libresolv.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = E8B39B191C89BC5E007B7280 /* libresolv.tbd */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -31,6 +32,7 @@ E8A78E421C5AB92E00D3C999 /* tsocks.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tsocks.h; sourceTree = ""; }; E8A78E4C1C5ABA6A00D3C999 /* parser.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = parser.c; sourceTree = ""; }; E8A78E4D1C5ABA6A00D3C999 /* parser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parser.h; sourceTree = ""; }; + E8B39B191C89BC5E007B7280 /* libresolv.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libresolv.tbd; path = usr/lib/libresolv.tbd; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -38,6 +40,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + E8B39B1A1C89BC5E007B7280 /* libresolv.tbd in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -49,6 +52,7 @@ children = ( E851B7831C7BD26F0083D155 /* TPControlHelper.h */, E8A78DFC1C5AAE5B00D3C999 /* tsocks */, + E8B39B1B1C89BC65007B7280 /* Libs */, E8A78DFB1C5AAE5B00D3C999 /* Products */, ); sourceTree = ""; @@ -77,6 +81,14 @@ path = tsocks; sourceTree = ""; }; + E8B39B1B1C89BC65007B7280 /* Libs */ = { + isa = PBXGroup; + children = ( + E8B39B191C89BC5E007B7280 /* libresolv.tbd */, + ); + name = Libs; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ diff --git a/TorProxifier/TorProxifier/libtsocks/tsocks/config.h b/TorProxifier/TorProxifier/libtsocks/tsocks/config.h index b1f1613..c012579 100644 --- a/TorProxifier/TorProxifier/libtsocks/tsocks/config.h +++ b/TorProxifier/TorProxifier/libtsocks/tsocks/config.h @@ -15,19 +15,9 @@ location */ /* Use _GNU_SOURCE to define RTLD_NEXT, mostly for RH7 systems */ /* #undef USE_GNU_SOURCE */ -/* dlopen() the old libc to get connect() instead of RTLD_NEXT, -hopefully shouldn't be needed */ -/* #undef USE_OLD_DLSYM */ - -/* path to library containing connect(), needed if USE_OLD_DLSYM is enabled */ -/* #undef LIBCONNECT */ - -/* path to libc, needed if USE_OLD_DLSYM is enabled */ -/* #undef LIBC */ - /* Configure the system resolver to use TCP queries on startup, this allows socksified DNS */ -/* #undef USE_SOCKS_DNS */ +#define USE_SOCKS_DNS 1 /* Prototype and function header for connect function */ #define CONNECT_SIGNATURE int __fd, const struct sockaddr * __addr, socklen_t __len diff --git a/TorProxifier/TorProxifier/libtsocks/tsocks/tsocks.c b/TorProxifier/TorProxifier/libtsocks/tsocks/tsocks.c index 978fa84..e0714ac 100644 --- a/TorProxifier/TorProxifier/libtsocks/tsocks/tsocks.c +++ b/TorProxifier/TorProxifier/libtsocks/tsocks/tsocks.c @@ -58,31 +58,11 @@ char *progname = "libtsocks"; /* Name used in err msgs */ #include "dead_pool.h" - -// --JP/ -#include "TPControlHelper.h" - -static void __attribute((constructor)) tp_constructor() -{ - fprintf(stderr, "****** tsocks loaded ******\n"); -} -// --JP! - /* Global Declarations */ -#ifdef USE_SOCKS_DNS -static int (*realresinit)(void); -#endif #ifdef USE_TOR_DNS static dead_pool *pool = NULL; -//static struct hostent *(*realgethostbyname)(GETHOSTBYNAME_SIGNATURE); -//int (*realgetaddrinfo)(GETADDRINFO_SIGNATURE); -//static struct hostent *(*realgetipnodebyname)(GETIPNODEBYNAME_SIGNATURE); #endif -//int (*realconnect)(CONNECT_SIGNATURE); -//static int (*realselect)(SELECT_SIGNATURE); -//static int (*realpoll)(POLL_SIGNATURE); -//int (*realclose)(CLOSE_SIGNATURE); -//static int (*realgetpeername)(GETPEERNAME_SIGNATURE); + static struct parsedfile *config; static struct connreq *requests = NULL; static int suid = 0; @@ -90,7 +70,6 @@ static char *conffile = NULL; static char *confdata = NULL; /* Exported Function Prototypes */ -void _init(void); int connect(CONNECT_SIGNATURE); int select(SELECT_SIGNATURE); int poll(POLL_SIGNATURE); @@ -106,6 +85,7 @@ struct hostent *getipnodebyname(GETIPNODEBYNAME_SIGNATURE); #endif /* Private Function Prototypes */ +static void _init(void); static int get_config(); static int get_environment(); static int connect_server(struct connreq *conn); @@ -132,11 +112,25 @@ static int deadpool_init(); static int send_socksv4a_request(struct connreq *conn, const char *onion_host); #endif - // --JP/ -struct hostent *p_gethostbyname(GETHOSTBYNAME_SIGNATURE); -int p_getaddrinfo(GETADDRINFO_SIGNATURE); -struct hostent *p_getipnodebyname(GETIPNODEBYNAME_SIGNATURE); +#include "TPControlHelper.h" + +static void __attribute((constructor)) tp_constructor() +{ + fprintf(stderr, "****** tsocks loaded ******\n"); + + _init(); +} +// --JP! + +#ifdef USE_SOCKS_DNS +int p_res_init(void); +#endif +#if defined(USE_TOR_DNS) && USE_TOR_DNS +struct hostent * p_gethostbyname(GETHOSTBYNAME_SIGNATURE); +int p_getaddrinfo(GETADDRINFO_SIGNATURE); +struct hostent * p_getipnodebyname(GETIPNODEBYNAME_SIGNATURE); +#endif int p_connect(CONNECT_SIGNATURE); @@ -145,6 +139,7 @@ int p_poll(POLL_SIGNATURE); int p_close(CLOSE_SIGNATURE); int p_getpeername(GETPEERNAME_SIGNATURE); + // From 'OS X Internal' typedef struct interpose_s { void *new_func; @@ -152,23 +147,27 @@ typedef struct interpose_s { } interpose_t; __attribute__((used)) static const interpose_t interposers[] __attribute__((section("__DATA,__interpose"))) = { - { (void *)p_gethostbyname, (void *)gethostbyname }, - { (void *)p_getaddrinfo, (void *)getaddrinfo }, - { (void *)p_getipnodebyname, (void *)getipnodebyname }, - - { (void *)p_connect, (void *)connect }, - { (void *)p_select, (void *)select }, - { (void *)p_poll, (void *)poll }, - { (void *)p_close, (void *)close }, - { (void *)p_getpeername, (void *)getpeername }, + +#if defined(USE_SOCKS_DNS) && USE_SOCKS_DNS + { (void *)p_res_init, (void *)res_init }, +#endif + +#if defined(USE_TOR_DNS) && USE_TOR_DNS + { (void *)p_gethostbyname, (void *)gethostbyname }, + { (void *)p_getaddrinfo, (void *)getaddrinfo }, + { (void *)p_getipnodebyname, (void *)getipnodebyname }, +#endif + + { (void *)p_connect, (void *)connect }, + { (void *)p_select, (void *)select }, + { (void *)p_poll, (void *)poll }, + { (void *)p_close, (void *)close }, + { (void *)p_getpeername, (void *)getpeername }, }; // --JP! -void _init(void) { -#ifdef USE_OLD_DLSYM - void *lib; -#endif +static void _init(void) { /* We could do all our initialization here, but to be honest */ /* most programs that are run won't use our services, so */ @@ -176,40 +175,10 @@ void _init(void) { /* Determine the logging level */ suid = (getuid() != geteuid()); - -#ifndef USE_OLD_DLSYM - //realconnect = dlsym(RTLD_NEXT, "connect"); - //realselect = dlsym(RTLD_NEXT, "select"); - //realpoll = dlsym(RTLD_NEXT, "poll"); - //realclose = dlsym(RTLD_NEXT, "close"); - //realgetpeername = dlsym(RTLD_NEXT, "getpeername"); - #ifdef USE_SOCKS_DNS - realresinit = dlsym(RTLD_NEXT, "res_init"); - #endif - #ifdef USE_TOR_DNS - //realgethostbyname = dlsym(RTLD_NEXT, "gethostbyname"); - //realgetaddrinfo = dlsym(RTLD_NEXT, "getaddrinfo"); - //realgetipnodebyname = dlsym(RTLD_NEXT, "getipnodebyname"); - #endif -#else - lib = dlopen(LIBCONNECT, RTLD_LAZY); - realconnect = dlsym(lib, "connect"); - realselect = dlsym(lib, "select"); - realpoll = dlsym(lib, "poll"); - realgetpeername = dlsym(lib, "getpeername"); - #ifdef USE_SOCKS_DNS - realresinit = dlsym(lib, "res_init"); - #endif - #ifdef USE_TOR_DNS - realgethostbyname = dlsym(lib, "gethostbyname"); - realgetaddrinfo = dlsym(lib, "getaddrinfo"); - realgetipnodebyname = dlsym(RTLD_NEXT, "getipnodebyname"); - #endif - dlclose(lib); - lib = dlopen(LIBC, RTLD_LAZY); - realclose = dlsym(lib, "close"); - dlclose(lib); -#endif + + get_environment(); + get_config(); + #ifdef USE_TOR_DNS /* Unfortunately, we can't do this lazily because otherwise our mmap'd area won't be shared across fork()s. */ @@ -317,13 +286,6 @@ int p_connect(CONNECT_SIGNATURE) { struct connreq *newconn; get_environment(); - - /* If the real connect doesn't exist, we're stuffed */ - //if (realconnect == NULL) { - // show_msg(MSGERR, "Unresolved symbol: connect\n"); - // return(-1); - //} - char *ipstr; @@ -812,11 +774,6 @@ int p_close(CLOSE_SIGNATURE) { int rc; struct connreq *conn; - //if (realclose == NULL) { - // show_msg(MSGERR, "Unresolved symbol: close\n"); - // return(-1); - //} - show_msg(MSGDEBUG, "Call to close(%d)\n", fd); rc = close(fd); @@ -852,14 +809,8 @@ int p_getpeername(GETPEERNAME_SIGNATURE) { struct connreq *conn; int rc; - //if (realgetpeername == NULL) { - // show_msg(MSGERR, "Unresolved symbol: getpeername\n"); - // return(-1); - //} - show_msg(MSGDEBUG, "Call to getpeername for fd %d\n", __fd); - rc = getpeername(__fd, __name, __namelen); if (rc == -1) return rc; @@ -1424,16 +1375,11 @@ static int read_socksv4_req(struct connreq *conn) { } #ifdef USE_SOCKS_DNS -int res_init(void) { +int p_res_init(void) { int rc; - - if (realresinit == NULL) { - show_msg(MSGERR, "Unresolved symbol: res_init\n"); - return(-1); - } /* Call normal res_init */ - rc = realresinit(); + rc = res_init(); /* Force using TCP protocol for DNS queries */ _res.options |= RES_USEVC;