commit 7a2244dd3d03767f78d73e4d62d2cf54ca67ff2d Author: David Goulet dgoulet@ev0ke.net Date: Mon Jun 3 21:04:22 2013 -0400
Add torsocks.c/.h with logging initialization
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/lib/Makefile.am | 7 +++-- src/lib/torsocks.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib/torsocks.h | 25 +++++++++++++++ 3 files changed, 116 insertions(+), 3 deletions(-)
diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 4a4fbea..f578d3f 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -3,12 +3,13 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src -I$(builddir) libdir = @libdir@/torsocks
# Install invocation scripts -bin_SCRIPTS = torsocks usewithtor -INSTALL_SCRIPT = $(install_sh) -c -m 755 +#bin_SCRIPTS = torsocks usewithtor +#INSTALL_SCRIPT = $(install_sh) -c -m 755 +EXTRA_DIST = torsocks.in usewithtor.in
lib_LTLIBRARIES = libtorsocks.la
-libtorsocks_la_SOURCES = +libtorsocks_la_SOURCES = torsocks.c torsocks.h
libtorsocks_la_LDFLAGS = $(TORSOCKSLDFLAGS)
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c new file mode 100644 index 0000000..c0450ad --- /dev/null +++ b/src/lib/torsocks.c @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdlib.h> + +#include <common/defaults.h> +#include <common/log.h> + +#include "torsocks.h" + +/* + * Set to 1 if the binary is set with suid or 0 if not. This is set once during + * initialization so after that it can be read without any protection. + */ +static int is_suid; + +/* + * Initialize logging subsytem using either the default values or the one given + * by the environment variables. + */ +static void init_logging(void) +{ + int level; + const char *filepath = NULL, *level_str, *time_status_str; + enum log_time_status t_status; + + /* Get log level from user or use default. */ + level_str = getenv(DEFAULT_LOG_LEVEL_ENV); + if (level_str) { + level = atoi(level_str); + } else { + /* Set to the default loglevel. */ + level = tsocks_loglevel; + } + + /* Get time status from user or use default. */ + time_status_str = getenv(DEFAULT_LOG_TIME_ENV); + if (time_status_str) { + t_status = atoi(time_status_str); + } else { + t_status = DEFAULT_LOG_TIME_STATUS; + } + + /* NULL value is valid which will set the output to stderr. */ + if (!is_suid) { + filepath = getenv(DEFAULT_LOG_FILEPATH_ENV); + } + + /* + * The return value is not important because this call will output the + * errors to the user if needed. Worst case, there is no logging. + */ + (void) log_init(level, filepath, t_status); + + /* After this, it is safe to call any logging macros. */ + + DBG("Logging subsytem initialized. Level %d, file %s, time %d", + level, filepath, t_status); +} + +/* + * Lib constructor. Initialize torsocks here before the main execution of the + * binary we are preloading. + */ +static void __attribute__((constructor)) init() +{ + /* UID and effective UID MUST be the same or else we are SUID. */ + is_suid = (getuid() != geteuid()); + + init_logging(); +} diff --git a/src/lib/torsocks.h b/src/lib/torsocks.h new file mode 100644 index 0000000..7e6eec1 --- /dev/null +++ b/src/lib/torsocks.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef TORSOCKS_H +#define TORSOCKS_H + +#include <common/compat.h> + +#endif /* TORSOCKS_H */