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