xref: /dragonfly/contrib/dhcpcd/src/control.c (revision f0dba201)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2020 Roy Marples <roy@marples.name>
5  * All rights reserved
6 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/socket.h>
30 #include <sys/stat.h>
31 #include <sys/uio.h>
32 #include <sys/un.h>
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <unistd.h>
41 
42 #include "config.h"
43 #include "common.h"
44 #include "dhcpcd.h"
45 #include "control.h"
46 #include "eloop.h"
47 #include "if.h"
48 #include "logerr.h"
49 #include "privsep.h"
50 
51 #ifndef SUN_LEN
52 #define SUN_LEN(su) \
53             (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
54 #endif
55 
56 static void
57 control_queue_free(struct fd_list *fd)
58 {
59 	struct fd_data *fdp;
60 
61 	while ((fdp = TAILQ_FIRST(&fd->queue))) {
62 		TAILQ_REMOVE(&fd->queue, fdp, next);
63 		if (fdp->data_size != 0)
64 			free(fdp->data);
65 		free(fdp);
66 	}
67 
68 #ifdef CTL_FREE_LIST
69 	while ((fdp = TAILQ_FIRST(&fd->free_queue))) {
70 		TAILQ_REMOVE(&fd->free_queue, fdp, next);
71 		if (fdp->data_size != 0)
72 			free(fdp->data);
73 		free(fdp);
74 	}
75 #endif
76 }
77 
78 void
79 control_free(struct fd_list *fd)
80 {
81 
82 #ifdef PRIVSEP
83 	if (fd->ctx->ps_control_client == fd)
84 		fd->ctx->ps_control_client = NULL;
85 #endif
86 
87 	eloop_event_remove_writecb(fd->ctx->eloop, fd->fd);
88 	TAILQ_REMOVE(&fd->ctx->control_fds, fd, next);
89 	control_queue_free(fd);
90 	free(fd);
91 }
92 
93 void
94 control_delete(struct fd_list *fd)
95 {
96 
97 #ifdef PRIVSEP
98 	if (IN_PRIVSEP_SE(fd->ctx))
99 		return;
100 #endif
101 
102 	eloop_event_delete(fd->ctx->eloop, fd->fd);
103 	close(fd->fd);
104 	control_free(fd);
105 }
106 
107 static void
108 control_handle_data(void *arg)
109 {
110 	struct fd_list *fd = arg;
111 	char buffer[1024];
112 	ssize_t bytes;
113 
114 	bytes = read(fd->fd, buffer, sizeof(buffer) - 1);
115 
116 	if (bytes == -1 || bytes == 0) {
117 		/* Control was closed or there was an error.
118 		 * Remove it from our list. */
119 		control_delete(fd);
120 		return;
121 	}
122 
123 #ifdef PRIVSEP
124 	if (IN_PRIVSEP(fd->ctx)) {
125 		ssize_t err;
126 
127 		fd->flags |= FD_SENDLEN;
128 		err = ps_ctl_handleargs(fd, buffer, (size_t)bytes);
129 		fd->flags &= ~FD_SENDLEN;
130 		if (err == -1) {
131 			logerr(__func__);
132 			return;
133 		}
134 		if (err == 1 &&
135 		    ps_ctl_sendargs(fd, buffer, (size_t)bytes) == -1) {
136 			logerr(__func__);
137 			control_delete(fd);
138 		}
139 		return;
140 	}
141 #endif
142 
143 	control_recvdata(fd, buffer, (size_t)bytes);
144 }
145 
146 void
147 control_recvdata(struct fd_list *fd, char *data, size_t len)
148 {
149 	char *p = data, *e;
150 	char *argvp[255], **ap;
151 	int argc;
152 
153 	/* Each command is \n terminated
154 	 * Each argument is NULL separated */
155 	while (len != 0) {
156 		argc = 0;
157 		ap = argvp;
158 		while (len != 0) {
159 			if (*p == '\0') {
160 				p++;
161 				len--;
162 				continue;
163 			}
164 			e = memchr(p, '\0', len);
165 			if (e == NULL) {
166 				errno = EINVAL;
167 				logerrx("%s: no terminator", __func__);
168 				return;
169 			}
170 			if ((size_t)argc >= sizeof(argvp) / sizeof(argvp[0])) {
171 				errno = ENOBUFS;
172 				logerrx("%s: no arg buffer", __func__);
173 				return;
174 			}
175 			*ap++ = p;
176 			argc++;
177 			e++;
178 			len -= (size_t)(e - p);
179 			p = e;
180 			e--;
181 			if (*(--e) == '\n') {
182 				*e = '\0';
183 				break;
184 			}
185 		}
186 		if (argc == 0) {
187 			logerrx("%s: no args", __func__);
188 			continue;
189 		}
190 		*ap = NULL;
191 		if (dhcpcd_handleargs(fd->ctx, fd, argc, argvp) == -1) {
192 			logerr(__func__);
193 			if (errno != EINTR && errno != EAGAIN) {
194 				control_delete(fd);
195 				return;
196 			}
197 		}
198 	}
199 }
200 
201 struct fd_list *
202 control_new(struct dhcpcd_ctx *ctx, int fd, unsigned int flags)
203 {
204 	struct fd_list *l;
205 
206 	l = malloc(sizeof(*l));
207 	if (l == NULL)
208 		return NULL;
209 
210 	l->ctx = ctx;
211 	l->fd = fd;
212 	l->flags = flags;
213 	TAILQ_INIT(&l->queue);
214 #ifdef CTL_FREE_LIST
215 	TAILQ_INIT(&l->free_queue);
216 #endif
217 	TAILQ_INSERT_TAIL(&ctx->control_fds, l, next);
218 	return l;
219 }
220 
221 static void
222 control_handle1(struct dhcpcd_ctx *ctx, int lfd, unsigned int fd_flags)
223 {
224 	struct sockaddr_un run;
225 	socklen_t len;
226 	struct fd_list *l;
227 	int fd, flags;
228 
229 	len = sizeof(run);
230 	if ((fd = accept(lfd, (struct sockaddr *)&run, &len)) == -1)
231 		goto error;
232 	if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
233 	    fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
234 		goto error;
235 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1 ||
236 	    fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
237 		goto error;
238 
239 #ifdef PRIVSEP
240 	if (IN_PRIVSEP(ctx) && !IN_PRIVSEP_SE(ctx))
241 		;
242 	else
243 #endif
244 	fd_flags |= FD_SENDLEN;
245 
246 	l = control_new(ctx, fd, fd_flags);
247 	if (l == NULL)
248 		goto error;
249 
250 	if (eloop_event_add(ctx->eloop, l->fd, control_handle_data, l) == -1)
251 		logerr(__func__);
252 	return;
253 
254 error:
255 	logerr(__func__);
256 	if (fd != -1)
257 		close(fd);
258 }
259 
260 static void
261 control_handle(void *arg)
262 {
263 	struct dhcpcd_ctx *ctx = arg;
264 
265 	control_handle1(ctx, ctx->control_fd, 0);
266 }
267 
268 static void
269 control_handle_unpriv(void *arg)
270 {
271 	struct dhcpcd_ctx *ctx = arg;
272 
273 	control_handle1(ctx, ctx->control_unpriv_fd, FD_UNPRIV);
274 }
275 
276 static int
277 make_path(char *path, size_t len, const char *ifname, sa_family_t family)
278 {
279 	const char *per;
280 
281 	switch(family) {
282 	case AF_INET:
283 		per = "-4";
284 		break;
285 	case AF_INET6:
286 		per = "-6";
287 		break;
288 	default:
289 		per = "";
290 		break;
291 	}
292 	return snprintf(path, len, CONTROLSOCKET,
293 	    ifname ? ifname : "", ifname ? per : "", ifname ? "." : "");
294 }
295 
296 static int
297 make_sock(struct sockaddr_un *sa, const char *ifname, sa_family_t family,
298     bool unpriv)
299 {
300 	int fd;
301 
302 	if ((fd = xsocket(AF_UNIX, SOCK_STREAM | SOCK_CXNB, 0)) == -1)
303 		return -1;
304 	memset(sa, 0, sizeof(*sa));
305 	sa->sun_family = AF_UNIX;
306 	if (unpriv)
307 		strlcpy(sa->sun_path, UNPRIVSOCKET, sizeof(sa->sun_path));
308 	else
309 		make_path(sa->sun_path, sizeof(sa->sun_path), ifname, family);
310 	return fd;
311 }
312 
313 #define S_PRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
314 #define S_UNPRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
315 
316 static int
317 control_start1(struct dhcpcd_ctx *ctx, const char *ifname, sa_family_t family,
318     mode_t fmode)
319 {
320 	struct sockaddr_un sa;
321 	int fd;
322 	socklen_t len;
323 
324 	fd = make_sock(&sa, ifname, family, (fmode & S_UNPRIV) == S_UNPRIV);
325 	if (fd == -1)
326 		return -1;
327 
328 	len = (socklen_t)SUN_LEN(&sa);
329 	unlink(sa.sun_path);
330 	if (bind(fd, (struct sockaddr *)&sa, len) == -1 ||
331 	    chmod(sa.sun_path, fmode) == -1 ||
332 	    (ctx->control_group &&
333 	    chown(sa.sun_path, geteuid(), ctx->control_group) == -1) ||
334 	    listen(fd, sizeof(ctx->control_fds)) == -1)
335 	{
336 		close(fd);
337 		unlink(sa.sun_path);
338 		return -1;
339 	}
340 
341 #ifdef PRIVSEP_RIGHTS
342 	if (IN_PRIVSEP(ctx) && ps_rights_limit_fd_fctnl(fd) == -1) {
343 		close(fd);
344 		unlink(sa.sun_path);
345 		return -1;
346 	}
347 #endif
348 
349 	if ((fmode & S_UNPRIV) != S_UNPRIV)
350 		strlcpy(ctx->control_sock, sa.sun_path,
351 		    sizeof(ctx->control_sock));
352 	return fd;
353 }
354 
355 int
356 control_start(struct dhcpcd_ctx *ctx, const char *ifname, sa_family_t family)
357 {
358 	int fd;
359 
360 #ifdef PRIVSEP
361 	if (IN_PRIVSEP_SE(ctx)) {
362 		make_path(ctx->control_sock, sizeof(ctx->control_sock),
363 		    ifname, family);
364 		return 0;
365 	}
366 #endif
367 
368 	if ((fd = control_start1(ctx, ifname, family, S_PRIV)) == -1)
369 		return -1;
370 
371 	ctx->control_fd = fd;
372 	eloop_event_add(ctx->eloop, fd, control_handle, ctx);
373 
374 	if (ifname == NULL &&
375 	    (fd = control_start1(ctx, NULL, AF_UNSPEC, S_UNPRIV)) != -1)
376 	{
377 		/* We must be in master mode, so create an unprivileged socket
378 		 * to allow normal users to learn the status of dhcpcd. */
379 		ctx->control_unpriv_fd = fd;
380 		eloop_event_add(ctx->eloop, fd, control_handle_unpriv, ctx);
381 	}
382 	return ctx->control_fd;
383 }
384 
385 static int
386 control_unlink(struct dhcpcd_ctx *ctx, const char *file)
387 {
388 	int retval = 0;
389 
390 	errno = 0;
391 #ifdef PRIVSEP
392 	if (IN_PRIVSEP(ctx))
393 		retval = (int)ps_root_unlink(ctx, file);
394 	else
395 #else
396 		UNUSED(ctx);
397 #endif
398 		retval = unlink(file);
399 
400 	return retval == -1 && errno != ENOENT ? -1 : 0;
401 }
402 
403 int
404 control_stop(struct dhcpcd_ctx *ctx)
405 {
406 	int retval = 0;
407 	struct fd_list *l;
408 
409 	while ((l = TAILQ_FIRST(&ctx->control_fds)) != NULL) {
410 		control_free(l);
411 	}
412 
413 #ifdef PRIVSEP
414 	if (IN_PRIVSEP_SE(ctx)) {
415 		if (ps_root_unlink(ctx, ctx->control_sock) == -1)
416 			retval = -1;
417 		if (ctx->options & DHCPCD_MASTER &&
418 		    control_unlink(ctx, UNPRIVSOCKET) == -1)
419 			retval = -1;
420 		return retval;
421 	} else if (ctx->options & DHCPCD_FORKED)
422 		return retval;
423 #endif
424 
425 	if (ctx->control_fd != -1) {
426 		eloop_event_delete(ctx->eloop, ctx->control_fd);
427 		close(ctx->control_fd);
428 		ctx->control_fd = -1;
429 		if (control_unlink(ctx, ctx->control_sock) == -1)
430 			retval = -1;
431 	}
432 
433 	if (ctx->control_unpriv_fd != -1) {
434 		eloop_event_delete(ctx->eloop, ctx->control_unpriv_fd);
435 		close(ctx->control_unpriv_fd);
436 		ctx->control_unpriv_fd = -1;
437 		if (control_unlink(ctx, UNPRIVSOCKET) == -1)
438 			retval = -1;
439 	}
440 
441 	return retval;
442 }
443 
444 int
445 control_open(const char *ifname, sa_family_t family, bool unpriv)
446 {
447 	struct sockaddr_un sa;
448 	int fd;
449 
450 	if ((fd = make_sock(&sa, ifname, family, unpriv)) != -1) {
451 		socklen_t len;
452 
453 		len = (socklen_t)SUN_LEN(&sa);
454 		if (connect(fd, (struct sockaddr *)&sa, len) == -1) {
455 			close(fd);
456 			fd = -1;
457 		}
458 	}
459 	return fd;
460 }
461 
462 ssize_t
463 control_send(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
464 {
465 	char buffer[1024];
466 	int i;
467 	size_t len, l;
468 
469 	if (argc > 255) {
470 		errno = ENOBUFS;
471 		return -1;
472 	}
473 	len = 0;
474 	for (i = 0; i < argc; i++) {
475 		l = strlen(argv[i]) + 1;
476 		if (len + l > sizeof(buffer)) {
477 			errno = ENOBUFS;
478 			return -1;
479 		}
480 		memcpy(buffer + len, argv[i], l);
481 		len += l;
482 	}
483 	return write(ctx->control_fd, buffer, len);
484 }
485 
486 static void
487 control_writeone(void *arg)
488 {
489 	struct fd_list *fd;
490 	struct iovec iov[2];
491 	int iov_len;
492 	struct fd_data *data;
493 
494 	fd = arg;
495 	data = TAILQ_FIRST(&fd->queue);
496 
497 	if (data->data_flags & FD_SENDLEN) {
498 		iov[0].iov_base = &data->data_len;
499 		iov[0].iov_len = sizeof(size_t);
500 		iov[1].iov_base = data->data;
501 		iov[1].iov_len = data->data_len;
502 		iov_len = 2;
503 	} else {
504 		iov[0].iov_base = data->data;
505 		iov[0].iov_len = data->data_len;
506 		iov_len = 1;
507 	}
508 
509 	if (writev(fd->fd, iov, iov_len) == -1) {
510 		logerr("%s: write", __func__);
511 		control_delete(fd);
512 		return;
513 	}
514 
515 	TAILQ_REMOVE(&fd->queue, data, next);
516 #ifdef CTL_FREE_LIST
517 	TAILQ_INSERT_TAIL(&fd->free_queue, data, next);
518 #else
519 	if (data->data_size != 0)
520 		free(data->data);
521 	free(data);
522 #endif
523 
524 	if (TAILQ_FIRST(&fd->queue) != NULL)
525 		return;
526 
527 	eloop_event_remove_writecb(fd->ctx->eloop, fd->fd);
528 #ifdef PRIVSEP
529 	if (IN_PRIVSEP_SE(fd->ctx) && !(fd->flags & FD_LISTEN)) {
530 		if (ps_ctl_sendeof(fd) == -1)
531 			logerr(__func__);
532 		control_free(fd);
533 	}
534 #endif
535 }
536 
537 int
538 control_queue(struct fd_list *fd, void *data, size_t data_len)
539 {
540 	struct fd_data *d;
541 
542 	if (data_len == 0) {
543 		errno = EINVAL;
544 		return -1;
545 	}
546 
547 #ifdef CTL_FREE_LIST
548 	struct fd_data *df;
549 
550 	d = NULL;
551 	TAILQ_FOREACH(df, &fd->free_queue, next) {
552 		if (d == NULL || d->data_size < df->data_size) {
553 			d = df;
554 			if (d->data_size <= data_len)
555 				break;
556 		}
557 	}
558 	if (d != NULL)
559 		TAILQ_REMOVE(&fd->free_queue, d, next);
560 	else
561 #endif
562 	{
563 		d = calloc(1, sizeof(*d));
564 		if (d == NULL)
565 			return -1;
566 	}
567 
568 	if (d->data_size == 0)
569 		d->data = NULL;
570 	if (d->data_size < data_len) {
571 		void *nbuf = realloc(d->data, data_len);
572 		if (nbuf == NULL) {
573 			free(d->data);
574 			free(d);
575 			return -1;
576 		}
577 		d->data = nbuf;
578 		d->data_size = data_len;
579 	}
580 	memcpy(d->data, data, data_len);
581 	d->data_len = data_len;
582 	d->data_flags = fd->flags & FD_SENDLEN;
583 
584 	TAILQ_INSERT_TAIL(&fd->queue, d, next);
585 	eloop_event_add_w(fd->ctx->eloop, fd->fd, control_writeone, fd);
586 	return 0;
587 }
588