1 |
|
|
/* $OpenBSD: control.c,v 1.25 2017/01/17 22:10:55 krw Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org> |
5 |
|
|
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org> |
6 |
|
|
* |
7 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
8 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
9 |
|
|
* copyright notice and this permission notice appear in all copies. |
10 |
|
|
* |
11 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
16 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
17 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include <sys/queue.h> |
21 |
|
|
#include <sys/stat.h> |
22 |
|
|
#include <sys/socket.h> |
23 |
|
|
#include <sys/un.h> |
24 |
|
|
#include <sys/tree.h> |
25 |
|
|
|
26 |
|
|
#include <net/if.h> |
27 |
|
|
|
28 |
|
|
#include <errno.h> |
29 |
|
|
#include <event.h> |
30 |
|
|
#include <fcntl.h> |
31 |
|
|
#include <stdlib.h> |
32 |
|
|
#include <string.h> |
33 |
|
|
#include <unistd.h> |
34 |
|
|
#include <signal.h> |
35 |
|
|
|
36 |
|
|
#include "iked.h" |
37 |
|
|
|
38 |
|
|
#define CONTROL_BACKLOG 5 |
39 |
|
|
|
40 |
|
|
struct ctl_connlist ctl_conns; |
41 |
|
|
|
42 |
|
|
void |
43 |
|
|
control_accept(int, short, void *); |
44 |
|
|
struct ctl_conn |
45 |
|
|
*control_connbyfd(int); |
46 |
|
|
void control_close(int, struct control_sock *); |
47 |
|
|
void control_dispatch_imsg(int, short, void *); |
48 |
|
|
void control_dispatch_parent(int, short, void *); |
49 |
|
|
void control_imsg_forward(struct imsg *); |
50 |
|
|
void control_run(struct privsep *, struct privsep_proc *, void *); |
51 |
|
|
|
52 |
|
|
static struct privsep_proc procs[] = { |
53 |
|
|
{ "parent", PROC_PARENT, NULL } |
54 |
|
|
}; |
55 |
|
|
|
56 |
|
|
pid_t |
57 |
|
|
control(struct privsep *ps, struct privsep_proc *p) |
58 |
|
|
{ |
59 |
|
|
return (proc_run(ps, p, procs, nitems(procs), control_run, NULL)); |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
void |
63 |
|
|
control_run(struct privsep *ps, struct privsep_proc *p, void *arg) |
64 |
|
|
{ |
65 |
|
|
/* |
66 |
|
|
* pledge in the control process: |
67 |
|
|
* stdio - for malloc and basic I/O including events. |
68 |
|
|
* cpath - for unlinking the control socket. |
69 |
|
|
* unix - for the control socket. |
70 |
|
|
*/ |
71 |
|
|
if (pledge("stdio cpath unix flock rpath wpath", NULL) == -1) |
72 |
|
|
fatal("pledge"); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
int |
76 |
|
|
control_init(struct privsep *ps, struct control_sock *cs) |
77 |
|
|
{ |
78 |
|
|
struct iked *env = ps->ps_env; |
79 |
|
|
struct sockaddr_un sun; |
80 |
|
|
int fd; |
81 |
|
|
mode_t old_umask, mode; |
82 |
|
|
|
83 |
|
|
if (cs->cs_name == NULL) |
84 |
|
|
return (0); |
85 |
|
|
|
86 |
|
|
if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) { |
87 |
|
|
log_warn("%s: socket", __func__); |
88 |
|
|
return (-1); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
sun.sun_family = AF_UNIX; |
92 |
|
|
if (strlcpy(sun.sun_path, cs->cs_name, |
93 |
|
|
sizeof(sun.sun_path)) >= sizeof(sun.sun_path)) { |
94 |
|
|
log_warn("%s: %s name too long", __func__, cs->cs_name); |
95 |
|
|
close(fd); |
96 |
|
|
return (-1); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
if (unlink(cs->cs_name) == -1) |
100 |
|
|
if (errno != ENOENT) { |
101 |
|
|
log_warn("%s: unlink %s", __func__, cs->cs_name); |
102 |
|
|
close(fd); |
103 |
|
|
return (-1); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
if (cs->cs_restricted) { |
107 |
|
|
old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH); |
108 |
|
|
mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; |
109 |
|
|
} else { |
110 |
|
|
old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH); |
111 |
|
|
mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) { |
115 |
|
|
log_warn("%s: bind: %s", __func__, cs->cs_name); |
116 |
|
|
close(fd); |
117 |
|
|
(void)umask(old_umask); |
118 |
|
|
return (-1); |
119 |
|
|
} |
120 |
|
|
(void)umask(old_umask); |
121 |
|
|
|
122 |
|
|
if (chmod(cs->cs_name, mode) == -1) { |
123 |
|
|
log_warn("%s: chmod", __func__); |
124 |
|
|
close(fd); |
125 |
|
|
(void)unlink(cs->cs_name); |
126 |
|
|
return (-1); |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
cs->cs_fd = fd; |
130 |
|
|
cs->cs_env = env; |
131 |
|
|
|
132 |
|
|
return (0); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
int |
136 |
|
|
control_listen(struct control_sock *cs) |
137 |
|
|
{ |
138 |
|
|
if (cs->cs_name == NULL) |
139 |
|
|
return (0); |
140 |
|
|
|
141 |
|
|
if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) { |
142 |
|
|
log_warn("%s: listen", __func__); |
143 |
|
|
return (-1); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
event_set(&cs->cs_ev, cs->cs_fd, EV_READ, |
147 |
|
|
control_accept, cs); |
148 |
|
|
event_add(&cs->cs_ev, NULL); |
149 |
|
|
evtimer_set(&cs->cs_evt, control_accept, cs); |
150 |
|
|
|
151 |
|
|
return (0); |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
void |
155 |
|
|
control_cleanup(struct control_sock *cs) |
156 |
|
|
{ |
157 |
|
|
if (cs->cs_name == NULL) |
158 |
|
|
return; |
159 |
|
|
event_del(&cs->cs_ev); |
160 |
|
|
event_del(&cs->cs_evt); |
161 |
|
|
(void)unlink(cs->cs_name); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/* ARGSUSED */ |
165 |
|
|
void |
166 |
|
|
control_accept(int listenfd, short event, void *arg) |
167 |
|
|
{ |
168 |
|
|
struct control_sock *cs = arg; |
169 |
|
|
int connfd; |
170 |
|
|
socklen_t len; |
171 |
|
|
struct sockaddr_un sun; |
172 |
|
|
struct ctl_conn *c; |
173 |
|
|
|
174 |
|
|
event_add(&cs->cs_ev, NULL); |
175 |
|
|
if ((event & EV_TIMEOUT)) |
176 |
|
|
return; |
177 |
|
|
|
178 |
|
|
len = sizeof(sun); |
179 |
|
|
if ((connfd = accept4(listenfd, |
180 |
|
|
(struct sockaddr *)&sun, &len, SOCK_NONBLOCK)) == -1) { |
181 |
|
|
/* |
182 |
|
|
* Pause accept if we are out of file descriptors, or |
183 |
|
|
* libevent will haunt us here too. |
184 |
|
|
*/ |
185 |
|
|
if (errno == ENFILE || errno == EMFILE) { |
186 |
|
|
struct timeval evtpause = { 1, 0 }; |
187 |
|
|
|
188 |
|
|
event_del(&cs->cs_ev); |
189 |
|
|
evtimer_add(&cs->cs_evt, &evtpause); |
190 |
|
|
} else if (errno != EWOULDBLOCK && errno != EINTR && |
191 |
|
|
errno != ECONNABORTED) |
192 |
|
|
log_warn("%s: accept", __func__); |
193 |
|
|
return; |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) { |
197 |
|
|
log_warn("%s", __func__); |
198 |
|
|
close(connfd); |
199 |
|
|
return; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
imsg_init(&c->iev.ibuf, connfd); |
203 |
|
|
c->iev.handler = control_dispatch_imsg; |
204 |
|
|
c->iev.events = EV_READ; |
205 |
|
|
c->iev.data = cs; |
206 |
|
|
event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events, |
207 |
|
|
c->iev.handler, c->iev.data); |
208 |
|
|
event_add(&c->iev.ev, NULL); |
209 |
|
|
|
210 |
|
|
TAILQ_INSERT_TAIL(&ctl_conns, c, entry); |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
struct ctl_conn * |
214 |
|
|
control_connbyfd(int fd) |
215 |
|
|
{ |
216 |
|
|
struct ctl_conn *c; |
217 |
|
|
|
218 |
|
|
TAILQ_FOREACH(c, &ctl_conns, entry) { |
219 |
|
|
if (c->iev.ibuf.fd == fd) |
220 |
|
|
break; |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
return (c); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
void |
227 |
|
|
control_close(int fd, struct control_sock *cs) |
228 |
|
|
{ |
229 |
|
|
struct ctl_conn *c; |
230 |
|
|
|
231 |
|
|
if ((c = control_connbyfd(fd)) == NULL) { |
232 |
|
|
log_warn("%s: fd %d: not found", __func__, fd); |
233 |
|
|
return; |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
msgbuf_clear(&c->iev.ibuf.w); |
237 |
|
|
TAILQ_REMOVE(&ctl_conns, c, entry); |
238 |
|
|
|
239 |
|
|
event_del(&c->iev.ev); |
240 |
|
|
close(c->iev.ibuf.fd); |
241 |
|
|
|
242 |
|
|
/* Some file descriptors are available again. */ |
243 |
|
|
if (evtimer_pending(&cs->cs_evt, NULL)) { |
244 |
|
|
evtimer_del(&cs->cs_evt); |
245 |
|
|
event_add(&cs->cs_ev, NULL); |
246 |
|
|
} |
247 |
|
|
|
248 |
|
|
free(c); |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
/* ARGSUSED */ |
252 |
|
|
void |
253 |
|
|
control_dispatch_imsg(int fd, short event, void *arg) |
254 |
|
|
{ |
255 |
|
|
struct control_sock *cs = arg; |
256 |
|
|
struct iked *env = cs->cs_env; |
257 |
|
|
struct ctl_conn *c; |
258 |
|
|
struct imsg imsg; |
259 |
|
|
int n, v; |
260 |
|
|
|
261 |
|
|
if ((c = control_connbyfd(fd)) == NULL) { |
262 |
|
|
log_warn("%s: fd %d: not found", __func__, fd); |
263 |
|
|
return; |
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
if (event & EV_READ) { |
267 |
|
|
if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) || |
268 |
|
|
n == 0) { |
269 |
|
|
control_close(fd, cs); |
270 |
|
|
return; |
271 |
|
|
} |
272 |
|
|
} |
273 |
|
|
if (event & EV_WRITE) { |
274 |
|
|
if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) { |
275 |
|
|
control_close(fd, cs); |
276 |
|
|
return; |
277 |
|
|
} |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
for (;;) { |
281 |
|
|
if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) { |
282 |
|
|
control_close(fd, cs); |
283 |
|
|
return; |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
if (n == 0) |
287 |
|
|
break; |
288 |
|
|
|
289 |
|
|
control_imsg_forward(&imsg); |
290 |
|
|
|
291 |
|
|
switch (imsg.hdr.type) { |
292 |
|
|
case IMSG_CTL_NOTIFY: |
293 |
|
|
if (c->flags & CTL_CONN_NOTIFY) { |
294 |
|
|
log_debug("%s: " |
295 |
|
|
"client requested notify more than once", |
296 |
|
|
__func__); |
297 |
|
|
imsg_compose_event(&c->iev, IMSG_CTL_FAIL, |
298 |
|
|
0, 0, -1, NULL, 0); |
299 |
|
|
break; |
300 |
|
|
} |
301 |
|
|
c->flags |= CTL_CONN_NOTIFY; |
302 |
|
|
break; |
303 |
|
|
case IMSG_CTL_VERBOSE: |
304 |
|
|
IMSG_SIZE_CHECK(&imsg, &v); |
305 |
|
|
|
306 |
|
|
memcpy(&v, imsg.data, sizeof(v)); |
307 |
|
|
log_setverbose(v); |
308 |
|
|
|
309 |
|
|
proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1); |
310 |
|
|
break; |
311 |
|
|
case IMSG_CTL_RELOAD: |
312 |
|
|
case IMSG_CTL_RESET: |
313 |
|
|
case IMSG_CTL_COUPLE: |
314 |
|
|
case IMSG_CTL_DECOUPLE: |
315 |
|
|
case IMSG_CTL_ACTIVE: |
316 |
|
|
case IMSG_CTL_PASSIVE: |
317 |
|
|
proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1); |
318 |
|
|
break; |
319 |
|
|
default: |
320 |
|
|
log_debug("%s: error handling imsg %d", |
321 |
|
|
__func__, imsg.hdr.type); |
322 |
|
|
break; |
323 |
|
|
} |
324 |
|
|
imsg_free(&imsg); |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
imsg_event_add(&c->iev); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
void |
331 |
|
|
control_imsg_forward(struct imsg *imsg) |
332 |
|
|
{ |
333 |
|
|
struct ctl_conn *c; |
334 |
|
|
|
335 |
|
|
TAILQ_FOREACH(c, &ctl_conns, entry) |
336 |
|
|
if (c->flags & CTL_CONN_NOTIFY) |
337 |
|
|
imsg_compose_event(&c->iev, imsg->hdr.type, |
338 |
|
|
0, imsg->hdr.pid, -1, imsg->data, |
339 |
|
|
imsg->hdr.len - IMSG_HEADER_SIZE); |
340 |
|
|
} |