#include #include #include #include "log.h" #include "log_colored.h" #include "utils.h" #include "redirector.h" #include "server_ssh.h" #include "forward.h" #include "jail.h" #ifdef HAVE_CONFIG_H #include "config.h" #endif int main(int argc, char *argv[]) { const size_t srv_siz = 3; const size_t jail_siz = 2; const char *ssh_ports[srv_siz]; const char *jail_ports[jail_siz]; redirector_ctx *rdr[srv_siz]; jail_ctx *jail[jail_siz]; forward_ctx *ssh_fwd = NULL; event_ctx *rdr_event = NULL; event_ctx *jail_event = NULL; int proc_status; pid_t daemon_pid, rdr_pid, jail_pid, wpid; (void) argc; (void) argv; arg0 = argv[0]; 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) { set_procname("[potd] main"); } else { FATAL("Forking (fork returned %d)", daemon_pid); } D2("Master pid: %d", getpid()); 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" ); { ABORT_ON_FATAL( fwd_init_ctx(&ssh_fwd, ssh_init_cb), "Forwarder initialisation" ); ABORT_ON_FATAL( fwd_setup_client(ssh_fwd, "127.0.0.1", "22222"), "Forwarder setup" ); ABORT_ON_FATAL( fwd_validate_ctx( ssh_fwd ), "Forwarder validation" ); } memset(rdr, 0, sizeof(rdr)); ssh_ports[0] = "2222"; ssh_ports[1] = "2223"; ssh_ports[2] = "22050"; for (size_t i = 0; i < srv_siz; ++i) { D("Initialising redirector service on port %s", ssh_ports[i]); redirector_init_ctx(&rdr[i], ssh_fwd); ABORT_ON_FATAL( redirector_setup(rdr[i], NULL, ssh_ports[i]), "Server setup" ); ABORT_ON_FATAL( redirector_validate_ctx(rdr[i]), "Server validation" ); } D2("%s", "Server event setup"); ABORT_ON_FATAL( redirector_setup_event( rdr, srv_siz, &rdr_event ), "Server event setup" ); D2("Server dropping privileges to %s:%s", "nobody", "NULL"); ABORT_ON_FATAL( change_user_group("nobody", NULL), "Server dropping privileges" ); N("%s", "Server epoll mainloop"); rdr_pid = redirector_daemonize( rdr_event, rdr, srv_siz ); ABORT_ON_FATAL( rdr_pid < 1, "Server epoll mainloop" ); while (1) { wpid = wait(&proc_status); if (wpid == jail_pid || wpid == rdr_pid) { E2("%s daemon with pid %d terminated, exiting", (wpid == jail_pid ? "Jail" : "Server"), (wpid == jail_pid ? jail_pid : rdr_pid)); break; } } return 0; }