xref: /dragonfly/contrib/dhcpcd/src/control.c (revision dda92f98)
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 
50 #ifndef SUN_LEN
51 #define SUN_LEN(su) \
52             (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
53 #endif
54 
55 static void
56 control_queue_free(struct fd_list *fd)
57 {
58 	struct fd_data *fdp;
59 
60 	while ((fdp = TAILQ_FIRST(&fd->queue))) {
61 		TAILQ_REMOVE(&fd->queue, fdp, next);
62 		if (fdp->data_size != 0)
63 			free(fdp->data);
64 		free(fdp);
65 	}
66 	fd->queue_len = 0;
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 static void
79 control_delete(struct fd_list *fd)
80 {
81 
82 	TAILQ_REMOVE(&fd->ctx->control_fds, fd, next);
83 	eloop_event_delete(fd->ctx->eloop, fd->fd);
84 	close(fd->fd);
85 	control_queue_free(fd);
86 	free(fd);
87 }
88 
89 static void
90 control_handle_data(void *arg)
91 {
92 	struct fd_list *fd = arg;
93 	char buffer[1024], *e, *p, *argvp[255], **ap, *a;
94 	ssize_t bytes;
95 	size_t len;
96 	int argc;
97 
98 	bytes = read(fd->fd, buffer, sizeof(buffer) - 1);
99 	if (bytes == -1 || bytes == 0) {
100 		/* Control was closed or there was an error.
101 		 * Remove it from our list. */
102 		control_delete(fd);
103 		return;
104 	}
105 	buffer[bytes] = '\0';
106 	p = buffer;
107 	e = buffer + bytes;
108 
109 	/* Each command is \n terminated
110 	 * Each argument is NULL separated */
111 	while (p < e) {
112 		argc = 0;
113 		ap = argvp;
114 		while (p < e) {
115 			argc++;
116 			if ((size_t)argc >= sizeof(argvp) / sizeof(argvp[0])) {
117 				errno = ENOBUFS;
118 				return;
119 			}
120 			a = *ap++ = p;
121 			len = strlen(p);
122 			p += len + 1;
123 			if (len && a[len - 1] == '\n') {
124 				a[len - 1] = '\0';
125 				break;
126 			}
127 		}
128 		*ap = NULL;
129 		if (dhcpcd_handleargs(fd->ctx, fd, argc, argvp) == -1) {
130 			logerr(__func__);
131 			if (errno != EINTR && errno != EAGAIN) {
132 				control_delete(fd);
133 				return;
134 			}
135 		}
136 	}
137 }
138 
139 static void
140 control_handle1(struct dhcpcd_ctx *ctx, int lfd, unsigned int fd_flags)
141 {
142 	struct sockaddr_un run;
143 	socklen_t len;
144 	struct fd_list *l;
145 	int fd, flags;
146 
147 	len = sizeof(run);
148 	if ((fd = accept(lfd, (struct sockaddr *)&run, &len)) == -1)
149 		goto error;
150 	if ((flags = fcntl(fd, F_GETFD, 0)) == -1 ||
151 	    fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
152 		goto error;
153 	if ((flags = fcntl(fd, F_GETFL, 0)) == -1 ||
154 	    fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
155 		goto error;
156 
157 	l = malloc(sizeof(*l));
158 	if (l == NULL)
159 		goto error;
160 
161 	l->ctx = ctx;
162 	l->fd = fd;
163 	l->flags = fd_flags;
164 	TAILQ_INIT(&l->queue);
165 	l->queue_len = 0;
166 #ifdef CTL_FREE_LIST
167 	TAILQ_INIT(&l->free_queue);
168 #endif
169 	TAILQ_INSERT_TAIL(&ctx->control_fds, l, next);
170 	eloop_event_add(ctx->eloop, l->fd, control_handle_data, l);
171 	return;
172 
173 error:
174 	logerr(__func__);
175 	if (fd != -1)
176 		close(fd);
177 }
178 
179 static void
180 control_handle(void *arg)
181 {
182 	struct dhcpcd_ctx *ctx = arg;
183 
184 	control_handle1(ctx, ctx->control_fd, 0);
185 }
186 
187 static void
188 control_handle_unpriv(void *arg)
189 {
190 	struct dhcpcd_ctx *ctx = arg;
191 
192 	control_handle1(ctx, ctx->control_unpriv_fd, FD_UNPRIV);
193 }
194 
195 static int
196 make_sock(struct sockaddr_un *sa, const char *ifname, bool unpriv)
197 {
198 	int fd;
199 
200 #define SOCK_FLAGS	SOCK_CLOEXEC | SOCK_NONBLOCK
201 	if ((fd = xsocket(AF_UNIX, SOCK_STREAM | SOCK_FLAGS, 0)) == -1)
202 		return -1;
203 #undef SOCK_FLAGS
204 	memset(sa, 0, sizeof(*sa));
205 	sa->sun_family = AF_UNIX;
206 	if (unpriv)
207 		strlcpy(sa->sun_path, UNPRIVSOCKET, sizeof(sa->sun_path));
208 	else {
209 		snprintf(sa->sun_path, sizeof(sa->sun_path), CONTROLSOCKET,
210 		    ifname ? ifname : "", ifname ? "." : "");
211 	}
212 	return fd;
213 }
214 
215 #define S_PRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
216 #define S_UNPRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
217 
218 static int
219 control_start1(struct dhcpcd_ctx *ctx, const char *ifname, mode_t fmode)
220 {
221 	struct sockaddr_un sa;
222 	int fd;
223 	socklen_t len;
224 
225 	if ((fd = make_sock(&sa, ifname, (fmode & S_UNPRIV) == S_UNPRIV)) == -1)
226 		return -1;
227 	len = (socklen_t)SUN_LEN(&sa);
228 	unlink(sa.sun_path);
229 	if (bind(fd, (struct sockaddr *)&sa, len) == -1 ||
230 	    chmod(sa.sun_path, fmode) == -1 ||
231 	    (ctx->control_group &&
232 	    chown(sa.sun_path, geteuid(), ctx->control_group) == -1) ||
233 	    listen(fd, sizeof(ctx->control_fds)) == -1)
234 	{
235 		close(fd);
236 		unlink(sa.sun_path);
237 		return -1;
238 	}
239 
240 	if ((fmode & S_UNPRIV) != S_UNPRIV)
241 		strlcpy(ctx->control_sock, sa.sun_path,
242 		    sizeof(ctx->control_sock));
243 	return fd;
244 }
245 
246 int
247 control_start(struct dhcpcd_ctx *ctx, const char *ifname)
248 {
249 	int fd;
250 
251 	if ((fd = control_start1(ctx, ifname, S_PRIV)) == -1)
252 		return -1;
253 
254 	ctx->control_fd = fd;
255 	eloop_event_add(ctx->eloop, fd, control_handle, ctx);
256 
257 	if (ifname == NULL && (fd = control_start1(ctx, NULL, S_UNPRIV)) != -1){
258 		/* We must be in master mode, so create an unprivileged socket
259 		 * to allow normal users to learn the status of dhcpcd. */
260 		ctx->control_unpriv_fd = fd;
261 		eloop_event_add(ctx->eloop, fd, control_handle_unpriv, ctx);
262 	}
263 	return ctx->control_fd;
264 }
265 
266 static int
267 control_unlink(struct dhcpcd_ctx *ctx, const char *file)
268 {
269 	int retval = 0;
270 
271 	errno = 0;
272 #ifdef PRIVSEP
273 	if (ctx->options & DHCPCD_PRIVSEP)
274 		retval = (int)ps_root_unlink(ctx, file);
275 	else
276 #else
277 		UNUSED(ctx);
278 #endif
279 		retval = unlink(file);
280 
281 	return retval == -1 && errno != ENOENT ? -1 : 0;
282 }
283 
284 int
285 control_stop(struct dhcpcd_ctx *ctx)
286 {
287 	int retval = 0;
288 	struct fd_list *l;
289 
290 	if (ctx->options & DHCPCD_FORKED)
291 		return 0;
292 
293 	if (ctx->control_fd != -1) {
294 		eloop_event_delete(ctx->eloop, ctx->control_fd);
295 		close(ctx->control_fd);
296 		ctx->control_fd = -1;
297 		if (control_unlink(ctx, ctx->control_sock) == -1)
298 			retval = -1;
299 	}
300 
301 	if (ctx->control_unpriv_fd != -1) {
302 		eloop_event_delete(ctx->eloop, ctx->control_unpriv_fd);
303 		close(ctx->control_unpriv_fd);
304 		ctx->control_unpriv_fd = -1;
305 		if (control_unlink(ctx, UNPRIVSOCKET) == -1)
306 			retval = -1;
307 	}
308 
309 	while ((l = TAILQ_FIRST(&ctx->control_fds))) {
310 		TAILQ_REMOVE(&ctx->control_fds, l, next);
311 		eloop_event_delete(ctx->eloop, l->fd);
312 		close(l->fd);
313 		control_queue_free(l);
314 		free(l);
315 	}
316 
317 	return retval;
318 }
319 
320 int
321 control_open(const char *ifname, bool unpriv)
322 {
323 	struct sockaddr_un sa;
324 	int fd;
325 
326 	if ((fd = make_sock(&sa, ifname, unpriv)) != -1) {
327 		socklen_t len;
328 
329 		len = (socklen_t)SUN_LEN(&sa);
330 		if (connect(fd, (struct sockaddr *)&sa, len) == -1) {
331 			close(fd);
332 			fd = -1;
333 		}
334 	}
335 	return fd;
336 }
337 
338 ssize_t
339 control_send(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
340 {
341 	char buffer[1024];
342 	int i;
343 	size_t len, l;
344 
345 	if (argc > 255) {
346 		errno = ENOBUFS;
347 		return -1;
348 	}
349 	len = 0;
350 	for (i = 0; i < argc; i++) {
351 		l = strlen(argv[i]) + 1;
352 		if (len + l > sizeof(buffer)) {
353 			errno = ENOBUFS;
354 			return -1;
355 		}
356 		memcpy(buffer + len, argv[i], l);
357 		len += l;
358 	}
359 	return write(ctx->control_fd, buffer, len);
360 }
361 
362 static void
363 control_writeone(void *arg)
364 {
365 	struct fd_list *fd;
366 	struct iovec iov[2];
367 	struct fd_data *data;
368 
369 	fd = arg;
370 	data = TAILQ_FIRST(&fd->queue);
371 	iov[0].iov_base = &data->data_len;
372 	iov[0].iov_len = sizeof(size_t);
373 	iov[1].iov_base = data->data;
374 	iov[1].iov_len = data->data_len;
375 	if (writev(fd->fd, iov, 2) == -1) {
376 		logerr(__func__);
377 		if (errno != EINTR && errno != EAGAIN)
378 			control_delete(fd);
379 		return;
380 	}
381 
382 	TAILQ_REMOVE(&fd->queue, data, next);
383 	fd->queue_len--;
384 #ifdef CTL_FREE_LIST
385 	TAILQ_INSERT_TAIL(&fd->free_queue, data, next);
386 #else
387 	if (data->data_size != 0)
388 		free(data->data);
389 	free(data);
390 #endif
391 
392 	if (TAILQ_FIRST(&fd->queue) == NULL)
393 		eloop_event_remove_writecb(fd->ctx->eloop, fd->fd);
394 }
395 
396 int
397 control_queue(struct fd_list *fd, void *data, size_t data_len, bool fit)
398 {
399 	struct fd_data *d;
400 
401 	if (data_len == 0)
402 		return 0;
403 
404 #ifdef CTL_FREE_LIST
405 	struct fd_data *df;
406 
407 	d = NULL;
408 	TAILQ_FOREACH(df, &fd->free_queue, next) {
409 		if (!fit) {
410 			if (df->data_size == 0) {
411 				d = df;
412 				break;
413 			}
414 			continue;
415 		}
416 		if (d == NULL || d->data_size < df->data_size) {
417 			d = df;
418 			if (d->data_size <= data_len)
419 				break;
420 		}
421 	}
422 	if (d != NULL)
423 		TAILQ_REMOVE(&fd->free_queue, d, next);
424 	else
425 #endif
426 	{
427 		if (fd->queue_len == CONTROL_QUEUE_MAX) {
428 			errno = ENOBUFS;
429 			return -1;
430 		}
431 		fd->queue_len++;
432 		d = calloc(1, sizeof(*d));
433 		if (d == NULL)
434 			return -1;
435 	}
436 
437 	if (!fit) {
438 #ifdef CTL_FREE_LIST
439 		if (d->data_size != 0) {
440 			free(d->data);
441 			d->data_size = 0;
442 		}
443 #endif
444 		d->data = data;
445 		d->data_len = data_len;
446 		goto queue;
447 	}
448 
449 	if (d->data_size == 0)
450 		d->data = NULL;
451 	if (d->data_size < data_len) {
452 		void *nbuf = realloc(d->data, data_len);
453 		if (nbuf == NULL) {
454 			free(d->data);
455 			free(d);
456 			return -1;
457 		}
458 		d->data = nbuf;
459 		d->data_size = data_len;
460 	}
461 	memcpy(d->data, data, data_len);
462 	d->data_len = data_len;
463 
464 queue:
465 	TAILQ_INSERT_TAIL(&fd->queue, d, next);
466 	eloop_event_add_w(fd->ctx->eloop, fd->fd, control_writeone, fd);
467 	return 0;
468 }
469