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