diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/jail.c | 35 | ||||
-rw-r--r-- | src/jail.h | 2 | ||||
-rw-r--r-- | src/main.c | 23 | ||||
-rw-r--r-- | src/server_ssh.c | 13 | ||||
-rw-r--r-- | src/utils.c | 45 | ||||
-rw-r--r-- | src/utils.h | 2 |
6 files changed, 86 insertions, 34 deletions
@@ -5,7 +5,9 @@ #include <assert.h> #include "jail.h" +#include "log.h" +static int jail_daemonfn(jail_ctx *ctx); static int jail_childfn(void *arg); @@ -32,20 +34,45 @@ void jail_free(jail_ctx **ctx) *ctx = NULL; } -int jail_fork(jail_ctx *ctx) +int jail_daemonize(jail_ctx *ctx) +{ + assert(ctx); + ctx->jail_pid = fork(); + + switch (ctx->jail_pid) { + case -1: + W_STRERR("Jail daemonize"); + return 1; + case 0: + N("%s", "Jail daemon mainloop"); + jail_daemonfn(ctx); + break; + } + D2("Jail daemon pid: %d", ctx->jail_pid); + + return 0; +} + +static int jail_daemonfn(jail_ctx *ctx) { int clone_flags = CLONE_NEWUTS|CLONE_NEWPID|CLONE_NEWIPC| CLONE_NEWNS|CLONE_NEWNET; assert(ctx); - ctx->jail_pid = clone(jail_childfn, ctx->stack_beg, - SIGCHLD|clone_flags, ctx); - return ctx->jail_pid < 0; + while (1) { + ctx->jail_pid = clone(jail_childfn, ctx->stack_beg, + SIGCHLD|clone_flags, ctx); + sleep(1); + printf("---\n"); + } + + exit(EXIT_SUCCESS); } static int jail_childfn(void *arg) { + printf("----> CHILD FN <----\n"); FILE *log = fopen("./test.log", "wb"); fprintf(log, "---> CHILD FN <----\n"); sleep(200); @@ -18,6 +18,6 @@ void jail_init(jail_ctx **ctx, size_t stacksize); void jail_free(jail_ctx **ctx); -int jail_fork(jail_ctx *ctx); +int jail_daemonize(jail_ctx *ctx); #endif @@ -1,8 +1,12 @@ +#include <stdio.h> + #include "log.h" #include "log_colored.h" +#include "utils.h" #include "server.h" #include "server_ssh.h" #include "forward.h" +#include "jail.h" #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -14,7 +18,9 @@ int main(int argc, char *argv[]) const char *ssh_ports[srv_siz]; server_ctx *srv[srv_siz]; forward_ctx *ssh_fwd = NULL; + jail_ctx *jail = NULL; int epoll_fd; + pid_t daemon_pid; (void) argc; (void) argv; @@ -22,6 +28,23 @@ int main(int argc, char *argv[]) LOG_SET_FUNCS_VA(LOG_COLORED_FUNCS); N("%s (C) 2018 Toni Uhlig (%s)", PACKAGE_STRING, PACKAGE_BUGREPORT); + D("%s", "Forking into background/foreground"); + daemon_pid = daemonize(1); + ABORT_ON_FATAL( daemon_pid > 0, "Forking" ); + if (daemon_pid == 0) { + D("Daemon: main child pid: %d", daemon_pid); + } else { + E("Forking failed: %d", daemon_pid); + E_STRERR("Daemonize"); + exit(EXIT_FAILURE); + } + + { + jail_init(&jail, BUFSIZ); + ABORT_ON_FATAL( jail_daemonize(jail), + "Jail daemon startup" ); + } + { ABORT_ON_FATAL( fwd_init_ctx(&ssh_fwd, ssh_init_cb), "Forwarder initialisation" ); diff --git a/src/server_ssh.c b/src/server_ssh.c index 474b5ab..100c6a8 100644 --- a/src/server_ssh.c +++ b/src/server_ssh.c @@ -12,7 +12,6 @@ #include <libssh/server.h> #include "server_ssh.h" -#include "jail.h" #include "log.h" #if LIBSSH_VERSION_MAJOR != 0 || LIBSSH_VERSION_MINOR < 7 || \ @@ -39,7 +38,7 @@ static void * ssh_thread_mainloop(void *arg); static int authenticate(ssh_session session); static int auth_password(const char *user, const char *password); -static int main_loop(ssh_channel chan); +static int client_mainloop(ssh_channel chan); static int copy_fd_to_chan(socket_t fd, int revents, void *userdata); static int copy_chan_to_fd(ssh_session session, ssh_channel channel, void *data, uint32_t len, int is_stderr, void *userdata); @@ -327,7 +326,7 @@ ssh_thread_mainloop(void *arg) } N("%s", "Dropping user into shell"); - main_loop(chan); + client_mainloop(chan); failed: ssh_disconnect(ses); @@ -404,7 +403,7 @@ static int auth_password(const char *user, const char *password) return 1; /* authenticated */ } -static int main_loop(ssh_channel chan) +static int client_mainloop(ssh_channel chan) { ssh_session session = ssh_channel_get_session(chan); socket_t fd; @@ -413,12 +412,6 @@ static int main_loop(ssh_channel chan) pid_t childpid; ssh_event event; short events; - jail_ctx jail, *jail_ptr = &jail; - - jail_init(&jail_ptr, BUFSIZ); - if (jail_fork(jail_ptr)) { - E_STRERR("Jail fork"); - } childpid = forkpty(&fd, NULL, term, win); if (childpid == 0) { diff --git a/src/utils.c b/src/utils.c index 5c86017..37d901c 100644 --- a/src/utils.c +++ b/src/utils.c @@ -5,6 +5,7 @@ #include <signal.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/wait.h> #include <syslog.h> #include "utils.h" @@ -18,8 +19,9 @@ void set_procname(char *arg0, const char *newname) strncpy(arg0, newname, _POSIX_PATH_MAX); } -int daemonize(void) +pid_t daemonize(int stay_foreground) { + int status = -1; pid_t pid; /* Fork off the parent process */ @@ -27,35 +29,45 @@ int daemonize(void) /* An error occurred */ if (pid < 0) - exit(EXIT_FAILURE); + return pid; /* Success: Let the parent terminate */ - if (pid > 0) + if (pid > 0) { + if (!stay_foreground) + exit(EXIT_SUCCESS); + waitpid(-1, &status, 0); exit(EXIT_SUCCESS); + } /* On success: The child process becomes session leader */ - if (setsid() < 0) + if (!stay_foreground && setsid() < 0) exit(EXIT_FAILURE); /* Catch, ignore and handle signals */ //TODO: Implement a working signal handler */ - signal(SIGCHLD, SIG_IGN); - signal(SIGHUP, SIG_IGN); + //signal(SIGCHLD, SIG_IGN); + //signal(SIGHUP, SIG_IGN); - /* Fork off for the second time*/ - pid = fork(); + if (!stay_foreground) { + /* Fork off for the second time*/ + pid = fork(); - /* An error occurred */ - if (pid < 0) - exit(EXIT_FAILURE); + /* An error occurred */ + if (pid < 0) + exit(EXIT_FAILURE); - /* Success: Let the parent terminate */ - if (pid > 0) - exit(EXIT_SUCCESS); + /* Success: Let the parent terminate */ + if (pid > 0) { + exit(EXIT_SUCCESS); + } + } /* Set new file permissions */ umask(0); + if (stay_foreground) + return pid; + /* Change the working directory to the root directory */ /* or another appropriated directory */ chdir("/"); @@ -67,8 +79,5 @@ int daemonize(void) close (x); } - /* Open the log file */ - openlog ("firstdaemon", LOG_PID, LOG_DAEMON); - - return 0; + return pid; } diff --git a/src/utils.h b/src/utils.h index f391943..dc89311 100644 --- a/src/utils.h +++ b/src/utils.h @@ -8,6 +8,6 @@ void set_procname(char *arg0, const char *newname); -int daemonize(void); +pid_t daemonize(int stay_foreground); #endif |