1/*	$OpenBSD: control.c,v 1.39 2024/05/21 05:00:47 jsg 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 <errno.h>
27#include <event.h>
28#include <fcntl.h>
29#include <stdlib.h>
30#include <string.h>
31#include <unistd.h>
32#include <signal.h>
33
34#include "iked.h"
35
36#define	CONTROL_BACKLOG	5
37
38struct ctl_connlist ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
39uint32_t ctl_peerid;
40
41void
42	 control_accept(int, short, void *);
43struct ctl_conn
44	*control_connbyfd(int);
45void	 control_close(int, struct control_sock *);
46void	 control_dispatch_imsg(int, short, void *);
47void	 control_imsg_forward(struct imsg *);
48void	 control_imsg_forward_peerid(struct imsg *);
49void	 control_run(struct privsep *, struct privsep_proc *, void *);
50int	 control_dispatch_ikev2(int, struct privsep_proc *, struct imsg *);
51int	 control_dispatch_ca(int, struct privsep_proc *, struct imsg *);
52
53static struct privsep_proc procs[] = {
54	{ "parent",	PROC_PARENT, NULL },
55	{ "ikev2",	PROC_IKEV2, control_dispatch_ikev2 },
56	{ "ca",		PROC_CERT, control_dispatch_ca },
57};
58
59void
60control(struct privsep *ps, struct privsep_proc *p)
61{
62	proc_run(ps, p, procs, nitems(procs), control_run, NULL);
63}
64
65void
66control_run(struct privsep *ps, struct privsep_proc *p, void *arg)
67{
68	/*
69	 * pledge in the control process:
70	 * stdio - for malloc and basic I/O including events.
71	 * unix - for the control socket.
72	 */
73	if (pledge("stdio unix recvfd", NULL) == -1)
74		fatal("pledge");
75}
76
77int
78control_init(struct privsep *ps, struct control_sock *cs)
79{
80	struct iked		*env = iked_env;
81	struct sockaddr_un	 s_un;
82	int			 fd;
83	mode_t			 old_umask, mode;
84
85	if (cs->cs_name == NULL)
86		return (0);
87
88	if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) == -1) {
89		log_warn("%s: socket", __func__);
90		return (-1);
91	}
92
93	s_un.sun_family = AF_UNIX;
94	if (strlcpy(s_un.sun_path, cs->cs_name,
95	    sizeof(s_un.sun_path)) >= sizeof(s_un.sun_path)) {
96		log_warn("%s: %s name too long", __func__, cs->cs_name);
97		close(fd);
98		return (-1);
99	}
100
101	if (unlink(cs->cs_name) == -1)
102		if (errno != ENOENT) {
103			log_warn("%s: unlink %s", __func__, cs->cs_name);
104			close(fd);
105			return (-1);
106		}
107
108	if (cs->cs_restricted) {
109		old_umask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
110		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
111	} else {
112		old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
113		mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP;
114	}
115
116	if (bind(fd, (struct sockaddr *)&s_un, sizeof(s_un)) == -1) {
117		log_warn("%s: bind: %s", __func__, cs->cs_name);
118		close(fd);
119		(void)umask(old_umask);
120		return (-1);
121	}
122	(void)umask(old_umask);
123
124	if (chmod(cs->cs_name, mode) == -1) {
125		log_warn("%s: chmod", __func__);
126		close(fd);
127		(void)unlink(cs->cs_name);
128		return (-1);
129	}
130
131	cs->cs_fd = fd;
132	cs->cs_env = env;
133
134	return (0);
135}
136
137int
138control_listen(struct control_sock *cs)
139{
140	if (cs->cs_name == NULL)
141		return (0);
142
143	if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) {
144		log_warn("%s: listen", __func__);
145		return (-1);
146	}
147
148	event_set(&cs->cs_ev, cs->cs_fd, EV_READ,
149	    control_accept, cs);
150	event_add(&cs->cs_ev, NULL);
151	evtimer_set(&cs->cs_evt, control_accept, cs);
152
153	return (0);
154}
155
156void
157control_accept(int listenfd, short event, void *arg)
158{
159	struct control_sock	*cs = arg;
160	int			 connfd;
161	socklen_t		 len;
162	struct sockaddr_un	 s_un;
163	struct ctl_conn		*c;
164	struct ctl_conn		*other;
165
166	event_add(&cs->cs_ev, NULL);
167	if ((event & EV_TIMEOUT))
168		return;
169
170	len = sizeof(s_un);
171	if ((connfd = accept4(listenfd,
172	    (struct sockaddr *)&s_un, &len, SOCK_NONBLOCK)) == -1) {
173		/*
174		 * Pause accept if we are out of file descriptors, or
175		 * libevent will haunt us here too.
176		 */
177		if (errno == ENFILE || errno == EMFILE) {
178			struct timeval evtpause = { 1, 0 };
179
180			event_del(&cs->cs_ev);
181			evtimer_add(&cs->cs_evt, &evtpause);
182		} else if (errno != EWOULDBLOCK && errno != EINTR &&
183		    errno != ECONNABORTED)
184			log_warn("%s: accept", __func__);
185		return;
186	}
187
188	if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
189		log_warn("%s", __func__);
190		close(connfd);
191		return;
192	}
193
194	imsg_init(&c->iev.ibuf, connfd);
195	c->iev.handler = control_dispatch_imsg;
196	c->iev.events = EV_READ;
197	c->iev.data = cs;
198	event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
199	    c->iev.handler, c->iev.data);
200	event_add(&c->iev.ev, NULL);
201
202	/* O(n^2), but n is small */
203	c->peerid = ctl_peerid++;
204	TAILQ_FOREACH(other, &ctl_conns, entry)
205		if (c->peerid == other->peerid)
206			c->peerid = ctl_peerid++;
207
208	TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
209}
210
211struct ctl_conn *
212control_connbyfd(int fd)
213{
214	struct ctl_conn	*c;
215
216	TAILQ_FOREACH(c, &ctl_conns, entry) {
217		if (c->iev.ibuf.fd == fd)
218			break;
219	}
220
221	return (c);
222}
223
224void
225control_close(int fd, struct control_sock *cs)
226{
227	struct ctl_conn	*c;
228
229	if ((c = control_connbyfd(fd)) == NULL) {
230		log_warn("%s: fd %d: not found", __func__, fd);
231		return;
232	}
233
234	msgbuf_clear(&c->iev.ibuf.w);
235	TAILQ_REMOVE(&ctl_conns, c, entry);
236
237	event_del(&c->iev.ev);
238	close(c->iev.ibuf.fd);
239
240	/* Some file descriptors are available again. */
241	if (evtimer_pending(&cs->cs_evt, NULL)) {
242		evtimer_del(&cs->cs_evt);
243		event_add(&cs->cs_ev, NULL);
244	}
245
246	free(c);
247}
248
249void
250control_dispatch_imsg(int fd, short event, void *arg)
251{
252	struct control_sock	*cs = arg;
253	struct iked		*env = cs->cs_env;
254	struct ctl_conn		*c;
255	struct imsg		 imsg;
256	int			 n, v;
257
258	if ((c = control_connbyfd(fd)) == NULL) {
259		log_warn("%s: fd %d: not found", __func__, fd);
260		return;
261	}
262
263	if (event & EV_READ) {
264		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
265		    n == 0) {
266			control_close(fd, cs);
267			return;
268		}
269	}
270	if (event & EV_WRITE) {
271		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
272			control_close(fd, cs);
273			return;
274		}
275	}
276
277	for (;;) {
278		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
279			control_close(fd, cs);
280			return;
281		}
282
283		if (n == 0)
284			break;
285
286		control_imsg_forward(&imsg);
287
288		/* record peerid of connection for reply */
289		imsg.hdr.peerid = c->peerid;
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		case IMSG_CTL_RESET_ID:
320			proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1);
321			break;
322		case IMSG_CTL_SHOW_SA:
323		case IMSG_CTL_SHOW_STATS:
324			proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1);
325			break;
326		case IMSG_CTL_SHOW_CERTSTORE:
327			proc_forward_imsg(&env->sc_ps, &imsg, PROC_CERT, -1);
328			break;
329		default:
330			log_debug("%s: error handling imsg %d",
331			    __func__, imsg.hdr.type);
332			break;
333		}
334		imsg_free(&imsg);
335	}
336
337	imsg_event_add(&c->iev);
338}
339
340void
341control_imsg_forward(struct imsg *imsg)
342{
343	struct ctl_conn *c;
344
345	TAILQ_FOREACH(c, &ctl_conns, entry)
346		if (c->flags & CTL_CONN_NOTIFY)
347			imsg_compose_event(&c->iev, imsg->hdr.type,
348			    0, imsg->hdr.pid, -1, imsg->data,
349			    imsg->hdr.len - IMSG_HEADER_SIZE);
350}
351
352void
353control_imsg_forward_peerid(struct imsg *imsg)
354{
355	struct ctl_conn *c;
356
357	TAILQ_FOREACH(c, &ctl_conns, entry)
358		if (c->peerid == imsg->hdr.peerid)
359			imsg_compose_event(&c->iev, imsg->hdr.type,
360			    0, imsg->hdr.pid, -1, imsg->data,
361			    imsg->hdr.len - IMSG_HEADER_SIZE);
362}
363
364int
365control_dispatch_ikev2(int fd, struct privsep_proc *p, struct imsg *imsg)
366{
367	switch (imsg->hdr.type) {
368	case IMSG_CTL_SHOW_SA:
369	case IMSG_CTL_SHOW_STATS:
370		control_imsg_forward_peerid(imsg);
371		return (0);
372	default:
373		break;
374	}
375
376	return (-1);
377}
378
379int
380control_dispatch_ca(int fd, struct privsep_proc *p, struct imsg *imsg)
381{
382	switch (imsg->hdr.type) {
383	case IMSG_CTL_SHOW_CERTSTORE:
384		control_imsg_forward_peerid(imsg);
385		return (0);
386	default:
387		break;
388	}
389
390	return (-1);
391}
392