aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: ec7b69856e1bb5dadb515bd638dc9b1d8c5b1443 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#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


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(&jail, BUFSIZ);
        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]);

        ABORT_ON_FATAL( server_init_ctx(&srv[i], ssh_fwd),
           "Server initialisation" );
        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" );

    N("%s", "Server epoll mainloop");
    ABORT_ON_FATAL( server_mainloop_epoll( epoll_fd, srv, srv_siz ),
        "Server epoll mainloop" );

    return 0;
}