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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.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 size_t jail_siz = 2;
const char *ssh_ports[srv_siz];
const char *jail_ports[jail_siz];
server_ctx *srv[srv_siz];
jail_ctx *jail[jail_siz];
forward_ctx *ssh_fwd = NULL;
int jail_epoll_fd, srv_epoll_fd, proc_status;
pid_t daemon_pid, srv_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" );
}
jail_epoll_fd = jail_setup_epoll( jail, jail_siz );
D2("Jail epoll fd: %d", jail_epoll_fd);
ABORT_ON_FATAL( jail_epoll_fd < 0, "Jail daemon epoll setup" );
jail_pid = jail_daemonize(jail_epoll_fd, 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(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");
srv_epoll_fd = server_setup_epoll( srv, srv_siz );
D2("Server epoll fd: %d", srv_epoll_fd);
ABORT_ON_FATAL( srv_epoll_fd < 0, "Server epoll 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");
srv_pid = server_daemonize( srv_epoll_fd, srv, srv_siz );
ABORT_ON_FATAL( srv_pid < 1, "Server epoll mainloop" );
while (1) {
wpid = wait(&proc_status);
if (wpid == jail_pid ||
wpid == srv_pid) {
E2("%s daemon with pid %d terminated, exiting",
(wpid == jail_pid ? "Jail" : "Server"),
(wpid == jail_pid ? jail_pid : srv_pid));
break;
}
}
return 0;
}
|