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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/* $OpenBSD: proc.c,v 1.1 2010/06/03 16:41:12 reyk Exp $ */
/* $vantronix: proc.c,v 1.11 2010/06/01 16:45:56 jsg Exp $ */
/*
* Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net>
* Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/tree.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <pwd.h>
#include <event.h>
#include <openssl/rand.h>
#include "iked.h"
void proc_shutdown(struct iked_proc *);
void proc_sig_handler(int, short, void *);
void
init_procs(struct iked *env, struct iked_proc *p, u_int nproc)
{
u_int i;
iked_process = PROC_PARENT;
init_pipes(env);
for (i = 0; i < nproc; i++, p++) {
env->sc_title[p->id] = p->title;
env->sc_pid[p->id] = (*p->init)(env, p);
}
}
void
kill_procs(struct iked *env)
{
u_int i;
if (iked_process != PROC_PARENT)
return;
for (i = 0; i < PROC_MAX; i++) {
if (env->sc_pid[i] == 0)
continue;
kill(env->sc_pid[i], SIGTERM);
}
}
void
init_pipes(struct iked *env)
{
int i, j, fds[2];
for (i = 0; i < PROC_MAX; i++)
for (j = 0; j < PROC_MAX; j++) {
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC,
fds) == -1)
fatal("socketpair");
env->sc_pipes[i][j] = fds[0];
env->sc_pipes[j][i] = fds[1];
socket_set_blockmode(env->sc_pipes[i][j],
BM_NONBLOCK);
socket_set_blockmode(env->sc_pipes[j][i],
BM_NONBLOCK);
}
}
void
config_pipes(struct iked *env, struct iked_proc *p, u_int nproc)
{
u_int i, j, k, found;
for (i = 0; i < PROC_MAX; i++) {
if (i != iked_process) {
for (j = 0; j < PROC_MAX; j++) {
close(env->sc_pipes[i][j]);
env->sc_pipes[i][j] = -1;
}
} else {
for (j = found = 0; j < PROC_MAX; j++, found = 0) {
for (k = 0; k < nproc; k++) {
if (p[k].id == j)
found++;
}
if (!found) {
close(env->sc_pipes[i][j]);
env->sc_pipes[i][j] = -1;
}
}
}
}
}
void
config_procs(struct iked *env, struct iked_proc *p, u_int nproc)
{
u_int src, dst, i;
/*
* listen on appropriate pipes
*/
for (i = 0; i < nproc; i++, p++) {
src = iked_process;
dst = p->id;
p->env = env;
imsg_init(&env->sc_ievs[dst].ibuf,
env->sc_pipes[src][dst]);
env->sc_ievs[dst].handler = dispatch_proc;
env->sc_ievs[dst].events = EV_READ;
env->sc_ievs[dst].data = p;
env->sc_ievs[dst].name = p->title;
event_set(&env->sc_ievs[dst].ev,
env->sc_ievs[dst].ibuf.fd,
env->sc_ievs[dst].events,
env->sc_ievs[dst].handler,
env->sc_ievs[dst].data);
event_add(&env->sc_ievs[dst].ev, NULL);
}
}
void
proc_shutdown(struct iked_proc *p)
{
struct iked *env = p->env;
if (p->id == PROC_CONTROL)
control_cleanup(&env->sc_csock);
log_info("%s exiting", p->title);
_exit(0);
}
void
proc_sig_handler(int sig, short event, void *arg)
{
switch (sig) {
case SIGINT:
case SIGTERM:
proc_shutdown((struct iked_proc *)arg);
break;
case SIGCHLD:
case SIGHUP:
case SIGPIPE:
/* ignore */
break;
default:
fatalx("proc_sig_handler: unexpected signal");
/* NOTREACHED */
}
}
pid_t
run_proc(struct iked *env, struct iked_proc *p,
struct iked_proc *procs, u_int nproc,
void (*init)(struct iked *, void *), void *arg)
{
pid_t pid;
struct passwd *pw;
const char *root;
u_int32_t seed[256];
switch (pid = fork()) {
case -1:
fatal("run_proc: cannot fork");
case 0:
break;
default:
return (pid);
}
pw = env->sc_pw;
if (p->id == PROC_CONTROL) {
if (control_init(env, &env->sc_csock) == -1)
fatalx(p->title);
}
/* Change root directory */
if (p->chroot != NULL)
root = p->chroot;
else
root = pw->pw_dir;
#ifndef DEBUG
if (chroot(root) == -1)
fatal("run_proc: chroot");
if (chdir("/") == -1)
fatal("run_proc: chdir(\"/\")");
#else
#warning disabling privilege revocation and chroot in DEBUG MODE
if (p->chroot != NULL) {
if (chroot(root) == -1)
fatal("run_proc: chroot");
if (chdir("/") == -1)
fatal("run_proc: chdir(\"/\")");
}
#endif
iked_process = p->id;
setproctitle("%s", p->title);
#ifndef DEBUG
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("run_proc: cannot drop privileges");
#endif
event_init();
signal_set(&env->sc_evsigint, SIGINT, proc_sig_handler, env);
signal_set(&env->sc_evsigterm, SIGTERM, proc_sig_handler, env);
signal_set(&env->sc_evsigchld, SIGCHLD, proc_sig_handler, env);
signal_set(&env->sc_evsighup, SIGHUP, proc_sig_handler, env);
signal_set(&env->sc_evsigpipe, SIGPIPE, proc_sig_handler, env);
signal_add(&env->sc_evsigint, NULL);
signal_add(&env->sc_evsigterm, NULL);
signal_add(&env->sc_evsigchld, NULL);
signal_add(&env->sc_evsighup, NULL);
signal_add(&env->sc_evsigpipe, NULL);
config_pipes(env, procs, nproc);
config_procs(env, procs, nproc);
arc4random_buf(seed, sizeof(seed));
RAND_seed(seed, sizeof(seed));
if (p->id == PROC_CONTROL) {
TAILQ_INIT(&ctl_conns);
if (control_listen(&env->sc_csock) == -1)
fatalx(p->title);
}
if (init != NULL)
init(env, arg);
event_dispatch();
proc_shutdown(p);
return (0);
}
void
dispatch_proc(int fd, short event, void *arg)
{
struct iked_proc *p = (struct iked_proc *)arg;
struct iked *env = p->env;
struct imsgev *iev;
struct imsgbuf *ibuf;
struct imsg imsg;
ssize_t n;
int verbose;
iev = &env->sc_ievs[p->id];
ibuf = &iev->ibuf;
if (event & EV_READ) {
if ((n = imsg_read(ibuf)) == -1)
fatal(p->title);
if (n == 0) {
/* this pipe is dead, so remove the event handler */
event_del(&iev->ev);
event_loopexit(NULL);
return;
}
}
if (event & EV_WRITE) {
if (msgbuf_write(&ibuf->w) == -1)
fatal(p->title);
}
for (;;) {
if ((n = imsg_get(ibuf, &imsg)) == -1)
fatal(p->title);
if (n == 0)
break;
/*
* Check the message with the program callback
*/
if ((p->cb)(fd, p, &imsg) == 0) {
/* Message was handled by the callback, continue */
imsg_free(&imsg);
continue;
}
/*
* Generic message handling
*/
switch (imsg.hdr.type) {
case IMSG_CTL_VERBOSE:
IMSG_SIZE_CHECK(&imsg, &verbose);
memcpy(&verbose, imsg.data, sizeof(verbose));
log_verbose(verbose);
break;
default:
log_warnx("%s: %s got imsg %d", __func__, p->title,
imsg.hdr.type);
fatalx(p->title);
}
imsg_free(&imsg);
}
imsg_event_add(iev);
}
|