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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "pseccomp.h"
#include "capabilities.h"
#include "log.h"
#include "log_colored.h"
#include "log_file.h"
#include "options.h"
#include "utils.h"
#include "redirector.h"
#include "protocol_ssh.h"
#include "forward.h"
#include "jail.h"
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 int setup_instances(int validate_only);
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);
//ctx[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)
{
for (size_t i = 0; i < siz; ++i) {
ABORT_ON_FATAL( proto_init_ctx(&ctx[i], ssh_init_cb),
"SSH Protocol init" );
ABORT_ON_FATAL( proto_setup(ctx[i], "127.0.0.1", ssh_ports[i],
"127.0.0.1", jail_ports[i]), "SSH Protocol setup" );
ABORT_ON_FATAL( proto_validate_ctx(ctx[i]),
"SSH validation" );
}
}
static void ssh_protocol_init(protocol_ctx *ctx[], const size_t siz)
{
for (size_t i = 0; i < siz; ++i) {
ABORT_ON_FATAL( proto_listen(ctx[i]),
"SSH Protocol listen" );
}
}
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;
}
static int setup_instances(int validate_only)
{
char *value = NULL;
struct opt_list *ol = NULL;
char *hostport[2];
size_t hostport_siz[2];
size_t siz;
if (getopt_used(OPT_REDIRECT))
while ((value = getopt_strlist(OPT_REDIRECT, &ol))) {
siz = parse_hostport(value, hostport, hostport_siz);
if (!siz) {
fprintf(stderr, "%s: invalid redtirector host:port combination: '%s'\n",
arg0, value);
return 1;
}
}
if (getopt_used(OPT_PROTOCOL))
while ((value = getopt_strlist(OPT_PROTOCOL, &ol))) {
siz = parse_hostport(value, hostport, hostport_siz);
if (!siz) {
fprintf(stderr, "%s: invalid protocol host:port combination: '%s'\n",
arg0, value);
return 1;
}
}
if (getopt_used(OPT_JAIL))
while ((value = getopt_strlist(OPT_JAIL, &ol))) {
siz = parse_hostport(value, hostport, hostport_siz);
if (!siz) {
fprintf(stderr, "%s: invalid jail host:port combination: '%s'\n",
arg0, value);
return 1;
}
}
/* TODO: setup redirect/protocol/jail instances if validate_only == 0 */
return 0;
}
int main(int argc, char *argv[])
{
char *value;
const size_t rdr_siz = 3;
const size_t proto_siz = 2;
const size_t jail_siz = 2;
const char *rdr_ports[rdr_siz];
const char *proto_ports[proto_siz];
const char *jail_ports[jail_siz];
redirector_ctx *rdr[rdr_siz];
protocol_ctx *ssh_proto[proto_siz];
jail_ctx *jail[jail_siz];
int proc_status;
pid_t daemon_pid, rdr_pid, jail_pid, child_pid;
pseccomp_ctx *psc = NULL;
(void) argc;
(void) argv;
arg0 = argv[0];
if (parse_cmdline(argc, argv)) {
fprintf(stderr, "%s: command line parsing failed\n", argv[0]);
exit(EXIT_FAILURE);
}
if (setup_instances(1)) {
fprintf(stderr, "%s: invalid config detected\n", argv[0]);
exit(EXIT_FAILURE);
}
if (getopt_used(OPT_LOGTOFILE) || getopt_used(OPT_LOGFILE)) {
log_file = getopt_str(OPT_LOGFILE);
LOG_SET_FUNCS_VA(LOG_FILE_FUNCS);
fprintf(stderr, "Logfile: '%s'\n", log_file);
} else {
LOG_SET_FUNCS_VA(LOG_COLORED_FUNCS);
}
if (getopt_used(OPT_LOGLEVEL)) {
value = getopt_str(OPT_LOGLEVEL);
if (!strcasecmp(value, "debug"))
log_prio = DEBUG;
else if (!strcasecmp(value, "notice"))
log_prio = NOTICE;
else if (!strcasecmp(value, "warning"))
log_prio = WARNING;
else if (!strcasecmp(value, "error"))
log_prio = ERROR;
else {
fprintf(stderr, "%s: unknown loglevel '%s'\n", argv[0], value);
exit(EXIT_FAILURE);
}
}
if (log_open())
exit(EXIT_FAILURE);
#ifdef HAVE_CONFIG_H
N("%s (C) 2018 Toni Uhlig (%s)", PACKAGE_STRING, PACKAGE_BUGREPORT);
#endif
if (geteuid() != 0) {
E("%s", "I was made for root!");
exit(EXIT_FAILURE);
}
caps_default_filter();
pseccomp_init(&psc, 0);
if (pseccomp_default_rules(psc))
FATAL("%s", "SECCOMP: adding default rules");
pseccomp_free(&psc);
D("%s", "Forking into background/foreground");
daemon_pid = daemonize(!getopt_used(OPT_DAEMON));
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());
ABORT_ON_FATAL( set_master_sighandler(),
"Master sighandler" );
memset(jail, 0, sizeof(jail));
jail_ports[0] = "33333";
jail_ports[1] = "33334";
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";
proto_ports[1] = "22223";
assert(SIZEOF(proto_ports) == SIZEOF(jail_ports));
ssh_protocol_preinit(proto_ports, ssh_proto, jail_ports, proto_siz);
ssh_protocol_init(ssh_proto, proto_siz);
memset(rdr, 0, sizeof(rdr));
rdr_ports[0] = "2222";
rdr_ports[1] = "2223";
rdr_ports[2] = "22050";
rdr_preinit(rdr_ports, rdr, SIZEOF(rdr_ports));
rdr_pid = rdr_init(rdr, SIZEOF(rdr_ports));
while (1) {
child_pid = wait(&proc_status);
if (child_pid == jail_pid ||
child_pid == rdr_pid) {
E2("%s daemon with pid %d terminated, exiting",
(child_pid == jail_pid ? "Jail" : "Redirector"),
(child_pid == jail_pid ? jail_pid : rdr_pid));
kill(getpid(), SIGTERM);
break;
} else W2("Process with pid %d terminated", child_pid);
}
log_close();
return 0;
}
|