xref: /dflybsd-src/contrib/dhcpcd/src/script.c (revision f354e0e64689159f00d07d7caa59dab0cea92fcb)
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/stat.h>
30 #include <sys/uio.h>
31 #include <sys/wait.h>
32 
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 
36 #include <assert.h>
37 #include <ctype.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <spawn.h>
41 #include <stdarg.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "config.h"
47 #include "common.h"
48 #include "dhcp.h"
49 #include "dhcp6.h"
50 #include "if.h"
51 #include "if-options.h"
52 #include "ipv4ll.h"
53 #include "ipv6nd.h"
54 #include "logerr.h"
55 #include "script.h"
56 
57 /* Allow the OS to define another script env var name */
58 #ifndef RC_SVCNAME
59 #define RC_SVCNAME "RC_SVCNAME"
60 #endif
61 
62 #define DEFAULT_PATH	"/usr/bin:/usr/sbin:/bin:/sbin"
63 
64 static const char * const if_params[] = {
65 	"interface",
66 	"protocol",
67 	"reason",
68 	"pid",
69 	"ifcarrier",
70 	"ifmetric",
71 	"ifwireless",
72 	"ifflags",
73 	"ssid",
74 	"profile",
75 	"interface_order",
76 	NULL
77 };
78 
79 void
80 if_printoptions(void)
81 {
82 	const char * const *p;
83 
84 	for (p = if_params; *p; p++)
85 		printf(" -  %s\n", *p);
86 }
87 
88 static int
89 script_exec(const struct dhcpcd_ctx *ctx, char *const *argv, char *const *env)
90 {
91 	pid_t pid = 0;
92 	posix_spawnattr_t attr;
93 	int r;
94 #ifdef USE_SIGNALS
95 	size_t i;
96 	short flags;
97 	sigset_t defsigs;
98 #else
99 	UNUSED(ctx);
100 #endif
101 
102 	/* posix_spawn is a safe way of executing another image
103 	 * and changing signals back to how they should be. */
104 	if (posix_spawnattr_init(&attr) == -1)
105 		return -1;
106 #ifdef USE_SIGNALS
107 	flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
108 	posix_spawnattr_setflags(&attr, flags);
109 	sigemptyset(&defsigs);
110 	for (i = 0; i < dhcpcd_signals_len; i++)
111 		sigaddset(&defsigs, dhcpcd_signals[i]);
112 	posix_spawnattr_setsigdefault(&attr, &defsigs);
113 	posix_spawnattr_setsigmask(&attr, &ctx->sigset);
114 #endif
115 	errno = 0;
116 	r = posix_spawn(&pid, argv[0], NULL, &attr, argv, env);
117 	posix_spawnattr_destroy(&attr);
118 	if (r) {
119 		errno = r;
120 		return -1;
121 	}
122 	return pid;
123 }
124 
125 #ifdef INET
126 static int
127 append_config(FILE *fp, const char *prefix, const char *const *config)
128 {
129 	size_t i;
130 
131 	if (config == NULL)
132 		return 0;
133 
134 	/* Do we need to replace existing config rather than append? */
135 	for (i = 0; config[i] != NULL; i++) {
136 		if (efprintf(fp, "%s_%s", prefix, config[i]) == -1)
137 			return -1;
138 	}
139 	return 1;
140 }
141 
142 #endif
143 
144 #define	PROTO_LINK	0
145 #define	PROTO_DHCP	1
146 #define	PROTO_IPV4LL	2
147 #define	PROTO_RA	3
148 #define	PROTO_DHCP6	4
149 #define	PROTO_STATIC6	5
150 static const char *protocols[] = {
151 	"link",
152 	"dhcp",
153 	"ipv4ll",
154 	"ra",
155 	"dhcp6",
156 	"static6"
157 };
158 
159 int
160 efprintf(FILE *fp, const char *fmt, ...)
161 {
162 	va_list args;
163 	int r;
164 
165 	va_start(args, fmt);
166 	r = vfprintf(fp, fmt, args);
167 	va_end(args);
168 	if (r == -1)
169 		return -1;
170 	/* Write a trailing NULL so we can easily create env strings. */
171 	if (fputc('\0', fp) == EOF)
172 		return -1;
173 	return r;
174 }
175 
176 static char **
177 script_buftoenv(struct dhcpcd_ctx *ctx, char *buf, size_t len)
178 {
179 	char **env, **envp, *bufp, *endp;
180 	size_t nenv;
181 
182 	/* Count the terminated env strings.
183 	 * Assert that the terminations are correct. */
184 	nenv = 0;
185 	endp = buf + len;
186 	for (bufp = buf; bufp < endp; bufp++) {
187 		if (*bufp == '\0') {
188 #ifndef NDEBUG
189 			if (bufp + 1 < endp)
190 				assert(*(bufp + 1) != '\0');
191 #endif
192 			nenv++;
193 		}
194 	}
195 	assert(*(bufp - 1) == '\0');
196 
197 	if (ctx->script_envlen < nenv) {
198 		env = reallocarray(ctx->script_env, nenv + 1, sizeof(*env));
199 		if (env == NULL)
200 			return NULL;
201 		ctx->script_env = env;
202 		ctx->script_envlen = nenv;
203 	}
204 
205 	bufp = buf;
206 	envp = ctx->script_env;
207 	*envp++ = bufp++;
208 	endp--; /* Avoid setting the last \0 to an invalid pointer */
209 	for (; bufp < endp; bufp++) {
210 		if (*bufp == '\0')
211 			*envp++ = bufp + 1;
212 	}
213 	*envp = NULL;
214 
215 	return ctx->script_env;
216 }
217 
218 static long
219 make_env(const struct interface *ifp, const char *reason)
220 {
221 	struct dhcpcd_ctx *ctx = ifp->ctx;
222 	FILE *fp;
223 	long buf_pos, i;
224 	char *path;
225 	int protocol = PROTO_LINK;
226 	const struct if_options *ifo = ifp->options;
227 	const struct interface *ifp2;
228 	int af;
229 #ifdef INET
230 	const struct dhcp_state *state;
231 #ifdef IPV4LL
232 	const struct ipv4ll_state *istate;
233 #endif
234 #endif
235 #ifdef DHCP6
236 	const struct dhcp6_state *d6_state;
237 #endif
238 
239 #ifdef HAVE_OPEN_MEMSTREAM
240 	if (ctx->script_fp == NULL) {
241 		fp = open_memstream(&ctx->script_buf, &ctx->script_buflen);
242 		if (fp == NULL)
243 			goto eexit;
244 		ctx->script_fp = fp;
245 	} else {
246 		fp = ctx->script_fp;
247 		rewind(fp);
248 	}
249 #else
250 	char tmpfile[] = "/tmp/dhcpcd-script-env-XXXXXX";
251 	int tmpfd;
252 
253 	fp = NULL;
254 	tmpfd = mkstemp(tmpfile);
255 	if (tmpfd == -1)
256 		goto eexit;
257 	unlink(tmpfile);
258 	fp = fdopen(tmpfd, "w+");
259 	if (fp == NULL) {
260 		close(tmpfd);
261 		goto eexit;
262 	}
263 #endif
264 
265 #ifdef INET
266 	state = D_STATE(ifp);
267 #ifdef IPV4LL
268 	istate = IPV4LL_CSTATE(ifp);
269 #endif
270 #endif
271 #ifdef DHCP6
272 	d6_state = D6_CSTATE(ifp);
273 #endif
274 	if (strcmp(reason, "TEST") == 0) {
275 		if (1 == 2) {}
276 #ifdef INET6
277 #ifdef DHCP6
278 		else if (d6_state && d6_state->new)
279 			protocol = PROTO_DHCP6;
280 #endif
281 		else if (ipv6nd_hasra(ifp))
282 			protocol = PROTO_RA;
283 #endif
284 #ifdef INET
285 #ifdef IPV4LL
286 		else if (istate && istate->addr != NULL)
287 			protocol = PROTO_IPV4LL;
288 #endif
289 		else
290 			protocol = PROTO_DHCP;
291 #endif
292 	}
293 #ifdef INET6
294 	else if (strcmp(reason, "STATIC6") == 0)
295 		protocol = PROTO_STATIC6;
296 #ifdef DHCP6
297 	else if (reason[strlen(reason) - 1] == '6')
298 		protocol = PROTO_DHCP6;
299 #endif
300 	else if (strcmp(reason, "ROUTERADVERT") == 0)
301 		protocol = PROTO_RA;
302 #endif
303 	else if (strcmp(reason, "PREINIT") == 0 ||
304 	    strcmp(reason, "CARRIER") == 0 ||
305 	    strcmp(reason, "NOCARRIER") == 0 ||
306 	    strcmp(reason, "UNKNOWN") == 0 ||
307 	    strcmp(reason, "DEPARTED") == 0 ||
308 	    strcmp(reason, "STOPPED") == 0)
309 		protocol = PROTO_LINK;
310 #ifdef INET
311 #ifdef IPV4LL
312 	else if (strcmp(reason, "IPV4LL") == 0)
313 		protocol = PROTO_IPV4LL;
314 #endif
315 	else
316 		protocol = PROTO_DHCP;
317 #endif
318 
319 	/* Needed for scripts */
320 	path = getenv("PATH");
321 	if (efprintf(fp, "PATH=%s", path == NULL ? DEFAULT_PATH:path) == -1)
322 		goto eexit;
323 
324 	if (efprintf(fp, "interface=%s", ifp->name) == -1)
325 		goto eexit;
326 	if (efprintf(fp, "reason=%s", reason) == -1)
327 		goto eexit;
328 	if (ifp->ctx->options & DHCPCD_DUMPLEASE)
329 		goto dumplease;
330 	if (efprintf(fp, "pid=%d", getpid()) == -1)
331 		goto eexit;
332 	if (efprintf(fp, "ifcarrier=%s",
333 	    ifp->carrier == LINK_UNKNOWN ? "unknown" :
334 	    ifp->carrier == LINK_UP ? "up" : "down") == -1)
335 		goto eexit;
336 	if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1)
337 		goto eexit;
338 	if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1)
339 		goto eexit;
340 	if (efprintf(fp, "ifflags=%u", ifp->flags) == -1)
341 		goto eexit;
342 	if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1)
343 		goto eexit;
344 
345 	if (fprintf(fp, "interface_order=") == -1)
346 		goto eexit;
347 	TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
348 		if (ifp2 != TAILQ_FIRST(ifp->ctx->ifaces)) {
349 			if (fputc(' ', fp) == EOF)
350 				return -1;
351 		}
352 		if (fprintf(fp, "%s", ifp2->name) == -1)
353 			return -1;
354 	}
355 	if (fputc('\0', fp) == EOF)
356 		return -1;
357 
358 	if (strcmp(reason, "STOPPED") == 0) {
359 		if (efprintf(fp, "if_up=false") == -1)
360 			goto eexit;
361 		if (efprintf(fp, "if_down=%s",
362 		    ifo->options & DHCPCD_RELEASE ? "true" : "false") == -1)
363 			goto eexit;
364 	} else if (strcmp(reason, "TEST") == 0 ||
365 	    strcmp(reason, "PREINIT") == 0 ||
366 	    strcmp(reason, "CARRIER") == 0 ||
367 	    strcmp(reason, "UNKNOWN") == 0)
368 	{
369 		if (efprintf(fp, "if_up=false") == -1)
370 			goto eexit;
371 		if (efprintf(fp, "if_down=false") == -1)
372 			goto eexit;
373 	} else if (1 == 2 /* appease ifdefs */
374 #ifdef INET
375 	    || (protocol == PROTO_DHCP && state && state->new)
376 #ifdef IPV4LL
377 	    || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
378 #endif
379 #endif
380 #ifdef INET6
381 	    || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
382 #ifdef DHCP6
383 	    || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
384 #endif
385 	    || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
386 #endif
387 	    )
388 	{
389 		if (efprintf(fp, "if_up=true") == -1)
390 			goto eexit;
391 		if (efprintf(fp, "if_down=false") == -1)
392 			goto eexit;
393 	} else {
394 		if (efprintf(fp, "if_up=false") == -1)
395 			goto eexit;
396 		if (efprintf(fp, "if_down=true") == -1)
397 			goto eexit;
398 	}
399 	if (protocols[protocol] != NULL) {
400 		if (efprintf(fp, "protocol=%s", protocols[protocol]) == -1)
401 			goto eexit;
402 	}
403 	if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
404 		if (efprintf(fp, "if_afwaiting=%d", af) == -1)
405 			goto eexit;
406 	}
407 	if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
408 		TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
409 			if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
410 				break;
411 		}
412 	}
413 	if (af != AF_MAX) {
414 		if (efprintf(fp, "af_waiting=%d", af) == -1)
415 			goto eexit;
416 	}
417 	if (ifo->options & DHCPCD_DEBUG) {
418 		if (efprintf(fp, "syslog_debug=true") == -1)
419 			goto eexit;
420 	}
421 	if (*ifp->profile != '\0') {
422 		if (efprintf(fp, "profile=%s", ifp->profile) == -1)
423 			goto eexit;
424 	}
425 	if (ifp->wireless) {
426 		char pssid[IF_SSIDLEN * 4];
427 
428 		if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
429 		    ifp->ssid, ifp->ssid_len) != -1)
430 		{
431 			if (efprintf(fp, "ifssid=%s", pssid) == -1)
432 				goto eexit;
433 		}
434 	}
435 #ifdef INET
436 	if (protocol == PROTO_DHCP && state && state->old) {
437 		if (dhcp_env(fp, "old", ifp,
438 		    state->old, state->old_len) == -1)
439 			goto eexit;
440 		if (append_config(fp, "old",
441 		    (const char *const *)ifo->config) == -1)
442 			goto eexit;
443 	}
444 #endif
445 #ifdef DHCP6
446 	if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
447 		if (dhcp6_env(fp, "old", ifp,
448 		    d6_state->old, d6_state->old_len) == -1)
449 			goto eexit;
450 	}
451 #endif
452 
453 dumplease:
454 #ifdef INET
455 #ifdef IPV4LL
456 	if (protocol == PROTO_IPV4LL) {
457 		if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1)
458 			goto eexit;
459 	}
460 #endif
461 	if (protocol == PROTO_DHCP && state && state->new) {
462 		if (dhcp_env(fp, "new", ifp,
463 		    state->new, state->new_len) == -1)
464 			goto eexit;
465 		if (append_config(fp, "new",
466 		    (const char *const *)ifo->config) == -1)
467 			goto eexit;
468 	}
469 #endif
470 #ifdef INET6
471 	if (protocol == PROTO_STATIC6) {
472 		if (ipv6_env(fp, "new", ifp) == -1)
473 			goto eexit;
474 	}
475 #ifdef DHCP6
476 	if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
477 		if (dhcp6_env(fp, "new", ifp,
478 		    d6_state->new, d6_state->new_len) == -1)
479 			goto eexit;
480 	}
481 #endif
482 	if (protocol == PROTO_RA) {
483 		if (ipv6nd_env(fp, ifp) == -1)
484 			goto eexit;
485 	}
486 #endif
487 
488 	/* Add our base environment */
489 	if (ifo->environ) {
490 		for (i = 0; ifo->environ[i] != NULL; i++)
491 			if (efprintf(fp, "%s", ifo->environ[i]) == -1)
492 				goto eexit;
493 	}
494 
495 	/* Convert buffer to argv */
496 	fflush(fp);
497 
498 	buf_pos = ftell(fp);
499 	if (buf_pos == -1) {
500 		logerr(__func__);
501 		goto eexit;
502 	}
503 
504 #ifndef HAVE_OPEN_MEMSTREAM
505 	size_t buf_len = (size_t)buf_pos;
506 	if (ctx->script_buflen < buf_len) {
507 		char *buf = realloc(ctx->script_buf, buf_len);
508 		if (buf == NULL)
509 			goto eexit;
510 		ctx->script_buf = buf;
511 		ctx->script_buflen = buf_len;
512 	}
513 	rewind(fp);
514 	if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len)
515 		goto eexit;
516 	fclose(fp);
517 	fp = NULL;
518 #endif
519 
520 	if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL)
521 		goto eexit;
522 
523 	return buf_pos - 1;
524 
525 eexit:
526 	logerr(__func__);
527 #ifndef HAVE_OPEN_MEMSTREAM
528 	if (fp != NULL)
529 		fclose(fp);
530 #endif
531 	return -1;
532 }
533 
534 static int
535 send_interface1(struct fd_list *fd, const struct interface *ifp,
536     const char *reason)
537 {
538 	struct dhcpcd_ctx *ctx = ifp->ctx;
539 	long len;
540 
541 	len = make_env(ifp, reason);
542 	if (len == -1)
543 		return -1;
544 	return control_queue(fd, ctx->script_buf,  (size_t)len, 1);
545 }
546 
547 int
548 send_interface(struct fd_list *fd, const struct interface *ifp)
549 {
550 	const char *reason;
551 	int retval = 0;
552 #ifdef INET
553 	const struct dhcp_state *d;
554 #endif
555 #ifdef DHCP6
556 	const struct dhcp6_state *d6;
557 #endif
558 
559 	switch (ifp->carrier) {
560 	case LINK_UP:
561 		reason = "CARRIER";
562 		break;
563 	case LINK_DOWN:
564 	case LINK_DOWN_IFFUP:
565 		reason = "NOCARRIER";
566 		break;
567 	default:
568 		reason = "UNKNOWN";
569 		break;
570 	}
571 	if (send_interface1(fd, ifp, reason) == -1)
572 		retval = -1;
573 #ifdef INET
574 	if (D_STATE_RUNNING(ifp)) {
575 		d = D_CSTATE(ifp);
576 		if (send_interface1(fd, ifp, d->reason) == -1)
577 			retval = -1;
578 	}
579 #ifdef IPV4LL
580 	if (IPV4LL_STATE_RUNNING(ifp)) {
581 		if (send_interface1(fd, ifp, "IPV4LL") == -1)
582 			retval = -1;
583 	}
584 #endif
585 #endif
586 
587 #ifdef INET6
588 	if (IPV6_STATE_RUNNING(ifp)) {
589 		if (send_interface1(fd, ifp, "STATIC6") == -1)
590 			retval = -1;
591 	}
592 	if (RS_STATE_RUNNING(ifp)) {
593 		if (send_interface1(fd, ifp, "ROUTERADVERT") == -1)
594 			retval = -1;
595 	}
596 #ifdef DHCP6
597 	if (D6_STATE_RUNNING(ifp)) {
598 		d6 = D6_CSTATE(ifp);
599 		if (send_interface1(fd, ifp, d6->reason) == -1)
600 			retval = -1;
601 	}
602 #endif
603 #endif
604 
605 	return retval;
606 }
607 
608 int
609 script_runreason(const struct interface *ifp, const char *reason)
610 {
611 	struct dhcpcd_ctx *ctx = ifp->ctx;
612 	char *argv[2];
613 	pid_t pid;
614 	int status = 0;
615 	struct fd_list *fd;
616 
617 	if (ifp->options->script == NULL &&
618 	    TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
619 		return 0;
620 
621 	/* Make our env */
622 	if (make_env(ifp, reason) == -1) {
623 		logerr(__func__);
624 		return -1;
625 	}
626 
627 	if (ifp->options->script == NULL)
628 		goto send_listeners;
629 
630 	argv[0] = ifp->options->script;
631 	argv[1] = NULL;
632 	logdebugx("%s: executing `%s' %s", ifp->name, argv[0], reason);
633 
634 	pid = script_exec(ctx, argv, ctx->script_env);
635 	if (pid == -1)
636 		logerr("%s: %s", __func__, argv[0]);
637 	else if (pid != 0) {
638 		/* Wait for the script to finish */
639 		while (waitpid(pid, &status, 0) == -1) {
640 			if (errno != EINTR) {
641 				logerr("%s: waitpid", __func__);
642 				status = 0;
643 				break;
644 			}
645 		}
646 		if (WIFEXITED(status)) {
647 			if (WEXITSTATUS(status))
648 				logerrx("%s: %s: WEXITSTATUS %d",
649 				    __func__, argv[0], WEXITSTATUS(status));
650 		} else if (WIFSIGNALED(status))
651 			logerrx("%s: %s: %s",
652 			    __func__, argv[0], strsignal(WTERMSIG(status)));
653 	}
654 
655 send_listeners:
656 	/* Send to our listeners */
657 	status = 0;
658 	TAILQ_FOREACH(fd, &ctx->control_fds, next) {
659 		if (!(fd->flags & FD_LISTEN))
660 			continue;
661 		if (control_queue(fd, ctx->script_buf, ctx->script_buflen,
662 		    true) == -1)
663 			logerr("%s: control_queue", __func__);
664 		else
665 			status = 1;
666 	}
667 
668 	return WEXITSTATUS(status);
669 }
670