#include #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 int main(int argc, char *argv[]) { const size_t srv_siz = 3; 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; 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) { D("Daemon: main child pid: %d", daemon_pid); set_procname("[potd] main"); } else { E("Forking failed: %d", daemon_pid); E_STRERR("Daemonize"); exit(EXIT_FAILURE); } { jail_init_ctx(&jail, MAX_STACKSIZE); ABORT_ON_FATAL( jail_setup(jail, "127.0.0.1", "33333"), "Jail daemon setup" ); ABORT_ON_FATAL( jail_validate_ctx(jail), "Jail validation" ); ABORT_ON_FATAL( jail_daemonize(jail), "Jail daemon startup" ); } { ABORT_ON_FATAL( fwd_init_ctx(&ssh_fwd, ssh_init_cb), "Forwarder initialisation" ); ABORT_ON_FATAL( fwd_setup(ssh_fwd, "127.0.0.1", "22222"), "Forwarder setup" ); ABORT_ON_FATAL( fwd_validate_ctx( ssh_fwd ), "Forwarder validation" ); } memset(srv, 0, sizeof(srv)); 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]); server_init_ctx(&srv[i], ssh_fwd); ABORT_ON_FATAL( server_setup(srv[i], NULL, ssh_ports[i]), "Server setup" ); ABORT_ON_FATAL( server_validate_ctx(srv[i]), "Server validation" ); } D2("%s", "Server epoll setup"); epoll_fd = server_setup_epoll( srv, srv_siz ); D2("epoll_fd: %d", epoll_fd); ABORT_ON_FATAL( epoll_fd < 0, "Server epoll setup" ); ABORT_ON_FATAL( setgid(65534), "Change group" ); ABORT_ON_FATAL( setuid(65534), "Change user" ); N("%s", "Server epoll mainloop"); ABORT_ON_FATAL( server_mainloop_epoll( epoll_fd, srv, srv_siz ), "Server epoll mainloop" ); return 0; }