diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-05-21 14:41:56 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-05-21 14:41:56 +0200 |
commit | 7684aa5922b4bb1b88d8a7609d238a9d6792feca (patch) | |
tree | 1a4e08e4331a864aecd53b1836dc0452654cd325 | |
parent | 472a5a7a0078b1d2792cb52003e3bd50e208cafc (diff) |
POTD skeleton #68.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
-rw-r--r-- | src/log.h | 11 | ||||
-rw-r--r-- | src/main.c | 108 | ||||
-rw-r--r-- | src/utils.c | 5 |
3 files changed, 78 insertions, 46 deletions
@@ -34,14 +34,9 @@ E_STRERR("`%s` returned %ld. %s", \ #expr, rv, msg); abort(); \ */ \ - E_STRERR("%s", msg); abort(); \ - } \ - } -#define GAI_ABORT_ON_FATAL(expr, msg) \ - { int rv = expr; \ - if (rv) { \ - /* E2("`%s` returned: %d", #expr, rv); */ \ - E_GAIERR(rv, msg); abort(); \ + E_STRERR("%s", msg); \ + kill(0, SIGABRT); \ + abort(); \ } \ } #define C(fmt, ...) log_fmt(CMD, fmt, __VA_ARGS__) @@ -16,11 +16,46 @@ #include "config.h" #endif +static void jail_preinit(const char *jail_ports[], jail_ctx *ctx[], + const size_t siz); +static pid_t jail_init(jail_ctx *ctx[], const size_t siz); static void ssh_protocol_preinit(const char *ssh_ports[], protocol_ctx *ctx[], const char *jail_ports[], const size_t siz); static void ssh_protocol_init(protocol_ctx *ctx[], const size_t siz); +static void rdr_preinit(const char *rdr_ports[], redirector_ctx *ctx[], + const size_t siz); +static pid_t rdr_init(redirector_ctx *ctx[], const size_t siz); +static void jail_preinit(const char *jail_ports[], jail_ctx *ctx[], + const size_t siz) +{ + for (size_t i = 0; i < siz; ++i) { + D("Initialising jail service on port %s", jail_ports[i]); + + jail_init_ctx(&ctx[i], MAX_STACKSIZE); + //jail[i]->newroot = strdup("/home/lns/git/busybox/sysroot"); + ctx[i]->newroot = strdup("/home/toni/git/busybox/_install"); + ABORT_ON_FATAL( jail_setup(ctx[i], "127.0.0.1", jail_ports[i]), + "Jail daemon setup" ); + ABORT_ON_FATAL( jail_validate_ctx(ctx[i]), + "Jail validation" ); + } +} + +static pid_t jail_init(jail_ctx *ctx[], const size_t siz) +{ + pid_t jail_pid; + event_ctx *event = NULL; + + ABORT_ON_FATAL( jail_setup_event( ctx, siz, &event ), + "Jail daemon epoll setup" ); + jail_pid = jail_daemonize(&event, ctx, siz); + ABORT_ON_FATAL( jail_pid < 1, "Jail daemon startup" ); + + return jail_pid; +} + static void ssh_protocol_preinit(const char *ssh_ports[], protocol_ctx *ctx[], const char *jail_ports[], const size_t siz) { @@ -42,6 +77,37 @@ static void ssh_protocol_init(protocol_ctx *ctx[], const size_t siz) } } +static void rdr_preinit(const char *rdr_ports[], redirector_ctx *ctx[], + const size_t siz) +{ + for (size_t i = 0; i < siz; ++i) { + D("Initialising redirector service on port %s", rdr_ports[i]); + + ABORT_ON_FATAL( redirector_init_ctx(&ctx[i]), + "Redirector init" ); + ABORT_ON_FATAL( redirector_setup(ctx[i], NULL, rdr_ports[i], + "127.0.0.1", "22222"), "Redirector setup" ); + ABORT_ON_FATAL( redirector_validate_ctx(ctx[i]), + "Redirector validation" ); + } +} + +static pid_t rdr_init(redirector_ctx *ctx[], const size_t siz) +{ + pid_t rdr_pid; + event_ctx *event = NULL; + + D2("%s", "Redirector event setup"); + ABORT_ON_FATAL( redirector_setup_event( ctx, siz, &event ), + "Redirector event setup" ); + + N("%s", "Redirector epoll mainloop"); + rdr_pid = redirector_daemonize( event, ctx, siz ); + ABORT_ON_FATAL( rdr_pid < 1, "Server epoll mainloop" ); + + return rdr_pid; +} + int main(int argc, char *argv[]) { const size_t rdr_siz = 3; @@ -53,8 +119,6 @@ int main(int argc, char *argv[]) redirector_ctx *rdr[rdr_siz]; protocol_ctx *ssh_proto[proto_siz]; jail_ctx *jail[jail_siz]; - event_ctx *rdr_event = NULL; - event_ctx *jail_event = NULL; int proc_status; pid_t daemon_pid, rdr_pid, jail_pid, child_pid; pseccomp_ctx *psc = NULL; @@ -94,23 +158,8 @@ int main(int argc, char *argv[]) memset(jail, 0, sizeof(jail)); jail_ports[0] = "33333"; jail_ports[1] = "33334"; - - for (size_t i = 0; i < jail_siz; ++i) { - D("Initialising jail service on port %s", jail_ports[i]); - - jail_init_ctx(&jail[i], MAX_STACKSIZE); - //jail[i]->newroot = strdup("/home/lns/git/busybox/sysroot"); - jail[i]->newroot = strdup("/home/toni/git/busybox/_install"); - ABORT_ON_FATAL( jail_setup(jail[i], "127.0.0.1", jail_ports[i]), - "Jail daemon setup" ); - ABORT_ON_FATAL( jail_validate_ctx(jail[i]), - "Jail validation" ); - } - - ABORT_ON_FATAL( jail_setup_event( jail, jail_siz, &jail_event ), - "Jail daemon epoll setup" ); - jail_pid = jail_daemonize(&jail_event, jail, jail_siz); - ABORT_ON_FATAL( jail_pid < 1, "Jail daemon startup" ); + jail_preinit(jail_ports, jail, SIZEOF(jail_ports)); + jail_pid = jail_init(jail, SIZEOF(jail_ports)); memset(ssh_proto, 0, sizeof(proto_ports)); proto_ports[0] = "22222"; @@ -123,25 +172,8 @@ int main(int argc, char *argv[]) rdr_ports[0] = "2222"; rdr_ports[1] = "2223"; rdr_ports[2] = "22050"; - - for (size_t i = 0; i < rdr_siz; ++i) { - D("Initialising redirector service on port %s", rdr_ports[i]); - - ABORT_ON_FATAL( redirector_init_ctx(&rdr[i]), - "Redirector init" ); - ABORT_ON_FATAL( redirector_setup(rdr[i], NULL, rdr_ports[i], - "127.0.0.1", "22222"), "Redirector setup" ); - ABORT_ON_FATAL( redirector_validate_ctx(rdr[i]), - "Redirector validation" ); - } - - D2("%s", "Redirector event setup"); - ABORT_ON_FATAL( redirector_setup_event( rdr, rdr_siz, &rdr_event ), - "Redirector event setup" ); - - N("%s", "Redirector epoll mainloop"); - rdr_pid = redirector_daemonize( rdr_event, rdr, rdr_siz ); - ABORT_ON_FATAL( rdr_pid < 1, "Server epoll mainloop" ); + rdr_preinit(rdr_ports, rdr, SIZEOF(rdr_ports)); + rdr_pid = rdr_init(rdr, SIZEOF(rdr_ports)); while (1) { child_pid = wait(&proc_status); diff --git a/src/utils.c b/src/utils.c index 7953614..34982d1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -72,10 +72,15 @@ int set_child_sighandler(void) static void sighandler_master(int signo) { + static int exiting = 0; + switch (signo) { case SIGINT: case SIGTERM: case SIGABRT: + if (exiting) + break; + exiting = 1; W("Got signal %d, exiting", signo); kill(0, SIGTERM); exit(EXIT_FAILURE); |