From 07d2b1a005b7875b268af736349246b9d6421059 Mon Sep 17 00:00:00 2001 From: Rayson Zhu Date: Sun, 13 Nov 2016 23:32:43 -0600 Subject: [PATCH] exit when running as a nonexistent user and warn when running as root --- src/local.c | 7 +++++-- src/manager.c | 9 ++++++--- src/redir.c | 7 +++++-- src/server.c | 7 +++++-- src/tunnel.c | 7 +++++-- 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/local.c b/src/local.c index 8066d47f..e8faceac 100644 --- a/src/local.c +++ b/src/local.c @@ -1440,8 +1440,11 @@ main(int argc, char **argv) LOGI("listening at %s:%s", local_addr, local_port); // setuid - if (user != NULL) { - run_as(user); + if (user != NULL && ! run_as(user)) { + FATAL("failed to switch user"); + } + if (geteuid() == 0){ + LOGI("You are running this process as the root user!"); } // Init connections diff --git a/src/manager.c b/src/manager.c index 7ef7459c..78bca1f4 100644 --- a/src/manager.c +++ b/src/manager.c @@ -845,11 +845,14 @@ main(int argc, char **argv) struct ev_loop *loop = EV_DEFAULT; // setuid - if (user != NULL) { - run_as(user); + if (user != NULL && ! run_as(user)) { + FATAL("failed to switch user"); + } + if (geteuid() == 0){ + LOGI("You are running this process as the root user!"); } - struct passwd *pw = getpwuid(getuid()); + struct passwd *pw = getpwuid(geteuid()); const char *homedir = pw->pw_dir; working_dir_size = strlen(homedir) + 15; working_dir = malloc(working_dir_size); diff --git a/src/redir.c b/src/redir.c index 8daeb2f2..5880c936 100644 --- a/src/redir.c +++ b/src/redir.c @@ -1021,8 +1021,11 @@ main(int argc, char **argv) LOGI("listening at %s:%s", local_addr, local_port); // setuid - if (user != NULL) { - run_as(user); + if (user != NULL && ! run_as(user)) { + FATAL("failed to switch user"); + } + if (geteuid() == 0){ + LOGI("You are running this process as the root user!"); } ev_run(loop, 0); diff --git a/src/server.c b/src/server.c index 0569d68a..3f1b4e1a 100644 --- a/src/server.c +++ b/src/server.c @@ -1871,8 +1871,11 @@ main(int argc, char **argv) ev_timer_start(EV_DEFAULT, &block_list_watcher); // setuid - if (user != NULL) { - run_as(user); + if (user != NULL && ! run_as(user)) { + FATAL("failed to switch user"); + } + if (geteuid() == 0){ + LOGI("You are running this process as the root user!"); } // init block list diff --git a/src/tunnel.c b/src/tunnel.c index 4ddfe0b6..0bfe2ec0 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -1028,8 +1028,11 @@ main(int argc, char **argv) LOGI("listening at %s:%s", local_addr, local_port); // setuid - if (user != NULL) { - run_as(user); + if (user != NULL && ! run_as(user)) { + FATAL("failed to switch user"); + } + if (geteuid() == 0){ + LOGI("You are running this process as the root user!"); } ev_run(loop, 0);