xref: /dragonfly/contrib/dhcpcd/src/control.c (revision a9783bc6)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * dhcpcd - DHCP client daemon
4  * Copyright (c) 2006-2019 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, int 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 unpriviledged 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 int
267 control_stop(struct dhcpcd_ctx *ctx)
268 {
269 	int retval = 0;
270 	struct fd_list *l;
271 
272 	if (ctx->options & DHCPCD_FORKED)
273 		goto freeit;
274 
275 	if (ctx->control_fd == -1)
276 		return 0;
277 
278 	control_close(ctx);
279 	if (unlink(ctx->control_sock) == -1)
280 		retval = -1;
281 
282 	if (ctx->control_unpriv_fd != -1) {
283 		eloop_event_delete(ctx->eloop, ctx->control_unpriv_fd);
284 		close(ctx->control_unpriv_fd);
285 		ctx->control_unpriv_fd = -1;
286 		if (unlink(UNPRIVSOCKET) == -1)
287 			retval = -1;
288 	}
289 
290 freeit:
291 	while ((l = TAILQ_FIRST(&ctx->control_fds))) {
292 		TAILQ_REMOVE(&ctx->control_fds, l, next);
293 		eloop_event_delete(ctx->eloop, l->fd);
294 		close(l->fd);
295 		control_queue_free(l);
296 		free(l);
297 	}
298 
299 	return retval;
300 }
301 
302 int
303 control_open(const char *ifname)
304 {
305 	struct sockaddr_un sa;
306 	int fd;
307 
308 	if ((fd = make_sock(&sa, ifname, 0)) != -1) {
309 		socklen_t len;
310 
311 		len = (socklen_t)SUN_LEN(&sa);
312 		if (connect(fd, (struct sockaddr *)&sa, len) == -1) {
313 			close(fd);
314 			fd = -1;
315 		}
316 	}
317 	return fd;
318 }
319 
320 ssize_t
321 control_send(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
322 {
323 	char buffer[1024];
324 	int i;
325 	size_t len, l;
326 
327 	if (argc > 255) {
328 		errno = ENOBUFS;
329 		return -1;
330 	}
331 	len = 0;
332 	for (i = 0; i < argc; i++) {
333 		l = strlen(argv[i]) + 1;
334 		if (len + l > sizeof(buffer)) {
335 			errno = ENOBUFS;
336 			return -1;
337 		}
338 		memcpy(buffer + len, argv[i], l);
339 		len += l;
340 	}
341 	return write(ctx->control_fd, buffer, len);
342 }
343 
344 static void
345 control_writeone(void *arg)
346 {
347 	struct fd_list *fd;
348 	struct iovec iov[2];
349 	struct fd_data *data;
350 
351 	fd = arg;
352 	data = TAILQ_FIRST(&fd->queue);
353 	iov[0].iov_base = &data->data_len;
354 	iov[0].iov_len = sizeof(size_t);
355 	iov[1].iov_base = data->data;
356 	iov[1].iov_len = data->data_len;
357 	if (writev(fd->fd, iov, 2) == -1) {
358 		logerr(__func__);
359 		if (errno != EINTR && errno != EAGAIN)
360 			control_delete(fd);
361 		return;
362 	}
363 
364 	TAILQ_REMOVE(&fd->queue, data, next);
365 	fd->queue_len--;
366 #ifdef CTL_FREE_LIST
367 	TAILQ_INSERT_TAIL(&fd->free_queue, data, next);
368 #else
369 	if (data->data_size != 0)
370 		free(data->data);
371 	free(data);
372 #endif
373 
374 	if (TAILQ_FIRST(&fd->queue) == NULL)
375 		eloop_event_remove_writecb(fd->ctx->eloop, fd->fd);
376 }
377 
378 int
379 control_queue(struct fd_list *fd, void *data, size_t data_len, bool fit)
380 {
381 	struct fd_data *d;
382 
383 	if (data_len == 0)
384 		return 0;
385 
386 #ifdef CTL_FREE_LIST
387 	struct fd_data *df;
388 
389 	d = NULL;
390 	TAILQ_FOREACH(df, &fd->free_queue, next) {
391 		if (!fit) {
392 			if (df->data_size == 0) {
393 				d = df;
394 				break;
395 			}
396 			continue;
397 		}
398 		if (d == NULL || d->data_size < df->data_size) {
399 			d = df;
400 			if (d->data_size <= data_len)
401 				break;
402 		}
403 	}
404 	if (d != NULL)
405 		TAILQ_REMOVE(&fd->free_queue, d, next);
406 	else
407 #endif
408 	{
409 		if (fd->queue_len == CONTROL_QUEUE_MAX) {
410 			errno = ENOBUFS;
411 			return -1;
412 		}
413 		fd->queue_len++;
414 		d = calloc(1, sizeof(*d));
415 		if (d == NULL)
416 			return -1;
417 	}
418 
419 	if (!fit) {
420 #ifdef CTL_FREE_LIST
421 		if (d->data_size != 0) {
422 			free(d->data);
423 			d->data_size = 0;
424 		}
425 #endif
426 		d->data = data;
427 		d->data_len = data_len;
428 		goto queue;
429 	}
430 
431 	if (d->data_size == 0)
432 		d->data = NULL;
433 	if (d->data_size < data_len) {
434 		void *nbuf = realloc(d->data, data_len);
435 		if (nbuf == NULL) {
436 			free(d->data);
437 			free(d);
438 			return -1;
439 		}
440 		d->data = nbuf;
441 		d->data_size = data_len;
442 	}
443 	memcpy(d->data, data, data_len);
444 	d->data_len = data_len;
445 
446 queue:
447 	TAILQ_INSERT_TAIL(&fd->queue, d, next);
448 	eloop_event_add_w(fd->ctx->eloop, fd->fd, control_writeone, fd);
449 	return 0;
450 }
451 
452 void
453 control_close(struct dhcpcd_ctx *ctx)
454 {
455 
456 	if (ctx->control_fd != -1) {
457 		eloop_event_delete(ctx->eloop, ctx->control_fd);
458 		close(ctx->control_fd);
459 		ctx->control_fd = -1;
460 	}
461 }
462