control.c revision 1.27
1/*	$OpenBSD: control.c,v 1.27 2020/03/18 22:12:43 tobhe 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
40struct ctl_connlist ctl_conns;
41
42void
43	 control_accept(int, short, void *);
44struct ctl_conn
45	*control_connbyfd(int);
46void	 control_close(int, struct control_sock *);
47void	 control_dispatch_imsg(int, short, void *);
48void	 control_dispatch_parent(int, short, void *);
49void	 control_imsg_forward(struct imsg *);
50void	 control_run(struct privsep *, struct privsep_proc *, void *);
51
52static struct privsep_proc procs[] = {
53	{ "parent",	PROC_PARENT, NULL },
54	{ "ikev2",	PROC_IKEV2, NULL }
55};
56
57pid_t
58control(struct privsep *ps, struct privsep_proc *p)
59{
60	return (proc_run(ps, p, procs, nitems(procs), control_run, NULL));
61}
62
63void
64control_run(struct privsep *ps, struct privsep_proc *p, void *arg)
65{
66	/*
67	 * pledge in the control process:
68	 * stdio - for malloc and basic I/O including events.
69	 * unix - for the control socket.
70	 */
71	if (pledge("stdio unix", NULL) == -1)
72		fatal("pledge");
73}
74
75int
76control_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
135int
136control_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/* ARGSUSED */
155void
156control_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	 sun;
162	struct ctl_conn		*c;
163
164	event_add(&cs->cs_ev, NULL);
165	if ((event & EV_TIMEOUT))
166		return;
167
168	len = sizeof(sun);
169	if ((connfd = accept4(listenfd,
170	    (struct sockaddr *)&sun, &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
203struct ctl_conn *
204control_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
216void
217control_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/* ARGSUSED */
242void
243control_dispatch_imsg(int fd, short event, void *arg)
244{
245	struct control_sock	*cs = arg;
246	struct iked		*env = cs->cs_env;
247	struct ctl_conn		*c;
248	struct imsg		 imsg;
249	int			 n, v;
250
251	if ((c = control_connbyfd(fd)) == NULL) {
252		log_warn("%s: fd %d: not found", __func__, fd);
253		return;
254	}
255
256	if (event & EV_READ) {
257		if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
258		    n == 0) {
259			control_close(fd, cs);
260			return;
261		}
262	}
263	if (event & EV_WRITE) {
264		if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
265			control_close(fd, cs);
266			return;
267		}
268	}
269
270	for (;;) {
271		if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
272			control_close(fd, cs);
273			return;
274		}
275
276		if (n == 0)
277			break;
278
279		control_imsg_forward(&imsg);
280
281		switch (imsg.hdr.type) {
282		case IMSG_CTL_NOTIFY:
283			if (c->flags & CTL_CONN_NOTIFY) {
284				log_debug("%s: "
285				    "client requested notify more than once",
286				    __func__);
287				imsg_compose_event(&c->iev, IMSG_CTL_FAIL,
288				    0, 0, -1, NULL, 0);
289				break;
290			}
291			c->flags |= CTL_CONN_NOTIFY;
292			break;
293		case IMSG_CTL_VERBOSE:
294			IMSG_SIZE_CHECK(&imsg, &v);
295
296			memcpy(&v, imsg.data, sizeof(v));
297			log_setverbose(v);
298
299			proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1);
300			break;
301		case IMSG_CTL_RELOAD:
302		case IMSG_CTL_RESET:
303		case IMSG_CTL_COUPLE:
304		case IMSG_CTL_DECOUPLE:
305		case IMSG_CTL_ACTIVE:
306		case IMSG_CTL_PASSIVE:
307			proc_forward_imsg(&env->sc_ps, &imsg, PROC_PARENT, -1);
308			break;
309		case IMSG_CTL_RESET_ID:
310			proc_forward_imsg(&env->sc_ps, &imsg, PROC_IKEV2, -1);
311			break;
312		default:
313			log_debug("%s: error handling imsg %d",
314			    __func__, imsg.hdr.type);
315			break;
316		}
317		imsg_free(&imsg);
318	}
319
320	imsg_event_add(&c->iev);
321}
322
323void
324control_imsg_forward(struct imsg *imsg)
325{
326	struct ctl_conn *c;
327
328	TAILQ_FOREACH(c, &ctl_conns, entry)
329		if (c->flags & CTL_CONN_NOTIFY)
330			imsg_compose_event(&c->iev, imsg->hdr.type,
331			    0, imsg->hdr.pid, -1, imsg->data,
332			    imsg->hdr.len - IMSG_HEADER_SIZE);
333}
334