xref: /dragonfly/contrib/dhcpcd/src/script.c (revision c9c5aa9e)
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/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 <pwd.h>
40 #include <signal.h>
41 #include <spawn.h>
42 #include <stdarg.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "config.h"
48 #include "common.h"
49 #include "dhcp.h"
50 #include "dhcp6.h"
51 #include "eloop.h"
52 #include "if.h"
53 #include "if-options.h"
54 #include "ipv4ll.h"
55 #include "ipv6nd.h"
56 #include "logerr.h"
57 #include "privsep.h"
58 #include "script.h"
59 
60 #define DEFAULT_PATH	"/usr/bin:/usr/sbin:/bin:/sbin"
61 
62 static const char * const if_params[] = {
63 	"interface",
64 	"protocol",
65 	"reason",
66 	"pid",
67 	"ifcarrier",
68 	"ifmetric",
69 	"ifwireless",
70 	"ifflags",
71 	"ssid",
72 	"profile",
73 	"interface_order",
74 	NULL
75 };
76 
77 void
78 if_printoptions(void)
79 {
80 	const char * const *p;
81 
82 	for (p = if_params; *p; p++)
83 		printf(" -  %s\n", *p);
84 }
85 
86 pid_t
87 script_exec(char *const *argv, char *const *env)
88 {
89 	pid_t pid = 0;
90 	posix_spawnattr_t attr;
91 	int r;
92 #ifdef USE_SIGNALS
93 	size_t i;
94 	short flags;
95 	sigset_t defsigs;
96 #else
97 	UNUSED(ctx);
98 #endif
99 
100 	/* posix_spawn is a safe way of executing another image
101 	 * and changing signals back to how they should be. */
102 	if (posix_spawnattr_init(&attr) == -1)
103 		return -1;
104 #ifdef USE_SIGNALS
105 	flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
106 	posix_spawnattr_setflags(&attr, flags);
107 	sigemptyset(&defsigs);
108 	posix_spawnattr_setsigmask(&attr, &defsigs);
109 	for (i = 0; i < dhcpcd_signals_len; i++)
110 		sigaddset(&defsigs, dhcpcd_signals[i]);
111 	for (i = 0; i < dhcpcd_signals_ignore_len; i++)
112 		sigaddset(&defsigs, dhcpcd_signals_ignore[i]);
113 	posix_spawnattr_setsigdefault(&attr, &defsigs);
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 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 	if (nenv == 0)
197 		return NULL;
198 
199 	if (ctx->script_envlen < nenv) {
200 		env = reallocarray(ctx->script_env, nenv + 1, sizeof(*env));
201 		if (env == NULL)
202 			return NULL;
203 		ctx->script_env = env;
204 		ctx->script_envlen = nenv;
205 	}
206 
207 	bufp = buf;
208 	envp = ctx->script_env;
209 	*envp++ = bufp++;
210 	endp--; /* Avoid setting the last \0 to an invalid pointer */
211 	for (; bufp < endp; bufp++) {
212 		if (*bufp == '\0')
213 			*envp++ = bufp + 1;
214 	}
215 	*envp = NULL;
216 
217 	return ctx->script_env;
218 }
219 
220 static long
221 make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp,
222     const char *reason)
223 {
224 	FILE *fp;
225 	long buf_pos, i;
226 	char *path;
227 	int protocol = PROTO_LINK;
228 	const struct if_options *ifo;
229 	const struct interface *ifp2;
230 	int af;
231 #ifdef INET
232 	const struct dhcp_state *state;
233 #ifdef IPV4LL
234 	const struct ipv4ll_state *istate;
235 #endif
236 #endif
237 #ifdef DHCP6
238 	const struct dhcp6_state *d6_state;
239 #endif
240 	bool is_stdin = ifp->name[0] == '\0';
241 
242 #ifdef HAVE_OPEN_MEMSTREAM
243 	if (ctx->script_fp == NULL) {
244 		fp = open_memstream(&ctx->script_buf, &ctx->script_buflen);
245 		if (fp == NULL)
246 			goto eexit;
247 		ctx->script_fp = fp;
248 	} else {
249 		fp = ctx->script_fp;
250 		rewind(fp);
251 	}
252 #else
253 	char tmpfile[] = "/tmp/dhcpcd-script-env-XXXXXX";
254 	int tmpfd;
255 
256 	fp = NULL;
257 	tmpfd = mkstemp(tmpfile);
258 	if (tmpfd == -1) {
259 		logerr("%s: mkstemp", __func__);
260 		return -1;
261 	}
262 	unlink(tmpfile);
263 	fp = fdopen(tmpfd, "w+");
264 	if (fp == NULL) {
265 		close(tmpfd);
266 		goto eexit;
267 	}
268 #endif
269 
270 	if (!(ifp->ctx->options & DHCPCD_DUMPLEASE)) {
271 		/* Needed for scripts */
272 		path = getenv("PATH");
273 		if (efprintf(fp, "PATH=%s",
274 		    path == NULL ? DEFAULT_PATH : path) == -1)
275 			goto eexit;
276 		if (efprintf(fp, "pid=%d", getpid()) == -1)
277 			goto eexit;
278 	}
279 	if (!is_stdin) {
280 		if (efprintf(fp, "reason=%s", reason) == -1)
281 			goto eexit;
282 	}
283 
284 	ifo = ifp->options;
285 #ifdef INET
286 	state = D_STATE(ifp);
287 #ifdef IPV4LL
288 	istate = IPV4LL_CSTATE(ifp);
289 #endif
290 #endif
291 #ifdef DHCP6
292 	d6_state = D6_CSTATE(ifp);
293 #endif
294 	if (strcmp(reason, "TEST") == 0) {
295 		if (1 == 2) {
296 			/* This space left intentionally blank
297 			 * as all the below statements are optional. */
298 		}
299 #ifdef INET6
300 #ifdef DHCP6
301 		else if (d6_state && d6_state->new)
302 			protocol = PROTO_DHCP6;
303 #endif
304 		else if (ipv6nd_hasra(ifp))
305 			protocol = PROTO_RA;
306 #endif
307 #ifdef INET
308 #ifdef IPV4LL
309 		else if (istate && istate->addr != NULL)
310 			protocol = PROTO_IPV4LL;
311 #endif
312 		else
313 			protocol = PROTO_DHCP;
314 #endif
315 	}
316 #ifdef INET6
317 	else if (strcmp(reason, "STATIC6") == 0)
318 		protocol = PROTO_STATIC6;
319 #ifdef DHCP6
320 	else if (reason[strlen(reason) - 1] == '6')
321 		protocol = PROTO_DHCP6;
322 #endif
323 	else if (strcmp(reason, "ROUTERADVERT") == 0)
324 		protocol = PROTO_RA;
325 #endif
326 	else if (strcmp(reason, "PREINIT") == 0 ||
327 	    strcmp(reason, "CARRIER") == 0 ||
328 	    strcmp(reason, "NOCARRIER") == 0 ||
329 	    strcmp(reason, "UNKNOWN") == 0 ||
330 	    strcmp(reason, "DEPARTED") == 0 ||
331 	    strcmp(reason, "STOPPED") == 0)
332 		protocol = PROTO_LINK;
333 #ifdef INET
334 #ifdef IPV4LL
335 	else if (strcmp(reason, "IPV4LL") == 0)
336 		protocol = PROTO_IPV4LL;
337 #endif
338 	else
339 		protocol = PROTO_DHCP;
340 #endif
341 
342 	if (!is_stdin) {
343 		if (efprintf(fp, "interface=%s", ifp->name) == -1)
344 			goto eexit;
345 		if (protocols[protocol] != NULL) {
346 			if (efprintf(fp, "protocol=%s",
347 			    protocols[protocol]) == -1)
348 				goto eexit;
349 		}
350 	}
351 	if (ifp->ctx->options & DHCPCD_DUMPLEASE && protocol != PROTO_LINK)
352 		goto dumplease;
353 	if (efprintf(fp, "if_configured=%s",
354 	    ifo->options & DHCPCD_CONFIGURE ? "true" : "false") == -1)
355 		goto eexit;
356 	if (efprintf(fp, "ifcarrier=%s",
357 	    ifp->carrier == LINK_UNKNOWN ? "unknown" :
358 	    ifp->carrier == LINK_UP ? "up" : "down") == -1)
359 		goto eexit;
360 	if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1)
361 		goto eexit;
362 	if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1)
363 		goto eexit;
364 	if (efprintf(fp, "ifflags=%u", ifp->flags) == -1)
365 		goto eexit;
366 	if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1)
367 		goto eexit;
368 	if (ifp->wireless) {
369 		char pssid[IF_SSIDLEN * 4];
370 
371 		if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
372 		    ifp->ssid, ifp->ssid_len) != -1)
373 		{
374 			if (efprintf(fp, "ifssid=%s", pssid) == -1)
375 				goto eexit;
376 		}
377 	}
378 	if (*ifp->profile != '\0') {
379 		if (efprintf(fp, "profile=%s", ifp->profile) == -1)
380 			goto eexit;
381 	}
382 	if (ifp->ctx->options & DHCPCD_DUMPLEASE)
383 		goto dumplease;
384 
385 	if (fprintf(fp, "interface_order=") == -1)
386 		goto eexit;
387 	TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
388 		if (ifp2 != TAILQ_FIRST(ifp->ctx->ifaces)) {
389 			if (fputc(' ', fp) == EOF)
390 				return -1;
391 		}
392 		if (fprintf(fp, "%s", ifp2->name) == -1)
393 			return -1;
394 	}
395 	if (fputc('\0', fp) == EOF)
396 		return -1;
397 
398 	if (strcmp(reason, "STOPPED") == 0) {
399 		if (efprintf(fp, "if_up=false") == -1)
400 			goto eexit;
401 		if (efprintf(fp, "if_down=%s",
402 		    ifo->options & DHCPCD_RELEASE ? "true" : "false") == -1)
403 			goto eexit;
404 	} else if (strcmp(reason, "TEST") == 0 ||
405 	    strcmp(reason, "PREINIT") == 0 ||
406 	    strcmp(reason, "CARRIER") == 0 ||
407 	    strcmp(reason, "UNKNOWN") == 0)
408 	{
409 		if (efprintf(fp, "if_up=false") == -1)
410 			goto eexit;
411 		if (efprintf(fp, "if_down=false") == -1)
412 			goto eexit;
413 	} else if (1 == 2 /* appease ifdefs */
414 #ifdef INET
415 	    || (protocol == PROTO_DHCP && state && state->new)
416 #ifdef IPV4LL
417 	    || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
418 #endif
419 #endif
420 #ifdef INET6
421 	    || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
422 #ifdef DHCP6
423 	    || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
424 #endif
425 	    || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
426 #endif
427 	    )
428 	{
429 		if (efprintf(fp, "if_up=true") == -1)
430 			goto eexit;
431 		if (efprintf(fp, "if_down=false") == -1)
432 			goto eexit;
433 	} else {
434 		if (efprintf(fp, "if_up=false") == -1)
435 			goto eexit;
436 		if (efprintf(fp, "if_down=true") == -1)
437 			goto eexit;
438 	}
439 	if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
440 		if (efprintf(fp, "if_afwaiting=%d", af) == -1)
441 			goto eexit;
442 	}
443 	if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
444 		TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
445 			if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
446 				break;
447 		}
448 	}
449 	if (af != AF_MAX) {
450 		if (efprintf(fp, "af_waiting=%d", af) == -1)
451 			goto eexit;
452 	}
453 	if (ifo->options & DHCPCD_DEBUG) {
454 		if (efprintf(fp, "syslog_debug=true") == -1)
455 			goto eexit;
456 	}
457 #ifdef INET
458 	if (protocol == PROTO_DHCP && state && state->old) {
459 		if (dhcp_env(fp, "old", ifp,
460 		    state->old, state->old_len) == -1)
461 			goto eexit;
462 		if (append_config(fp, "old",
463 		    (const char *const *)ifo->config) == -1)
464 			goto eexit;
465 	}
466 #endif
467 #ifdef DHCP6
468 	if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
469 		if (dhcp6_env(fp, "old", ifp,
470 		    d6_state->old, d6_state->old_len) == -1)
471 			goto eexit;
472 	}
473 #endif
474 
475 dumplease:
476 #ifdef INET
477 #ifdef IPV4LL
478 	if (protocol == PROTO_IPV4LL && istate) {
479 		if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1)
480 			goto eexit;
481 	}
482 #endif
483 	if (protocol == PROTO_DHCP && state && state->new) {
484 		if (dhcp_env(fp, "new", ifp,
485 		    state->new, state->new_len) == -1)
486 			goto eexit;
487 		if (append_config(fp, "new",
488 		    (const char *const *)ifo->config) == -1)
489 			goto eexit;
490 	}
491 #endif
492 #ifdef INET6
493 	if (protocol == PROTO_STATIC6) {
494 		if (ipv6_env(fp, "new", ifp) == -1)
495 			goto eexit;
496 	}
497 #ifdef DHCP6
498 	if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
499 		if (dhcp6_env(fp, "new", ifp,
500 		    d6_state->new, d6_state->new_len) == -1)
501 			goto eexit;
502 	}
503 #endif
504 	if (protocol == PROTO_RA) {
505 		if (ipv6nd_env(fp, ifp) == -1)
506 			goto eexit;
507 	}
508 #endif
509 
510 	/* Add our base environment */
511 	if (ifo->environ) {
512 		for (i = 0; ifo->environ[i] != NULL; i++)
513 			if (efprintf(fp, "%s", ifo->environ[i]) == -1)
514 				goto eexit;
515 	}
516 
517 	/* Convert buffer to argv */
518 	fflush(fp);
519 
520 	buf_pos = ftell(fp);
521 	if (buf_pos == -1) {
522 		logerr(__func__);
523 		goto eexit;
524 	}
525 
526 #ifndef HAVE_OPEN_MEMSTREAM
527 	size_t buf_len = (size_t)buf_pos;
528 	if (ctx->script_buflen < buf_len) {
529 		char *buf = realloc(ctx->script_buf, buf_len);
530 		if (buf == NULL)
531 			goto eexit;
532 		ctx->script_buf = buf;
533 		ctx->script_buflen = buf_len;
534 	}
535 	rewind(fp);
536 	if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len)
537 		goto eexit;
538 	fclose(fp);
539 	fp = NULL;
540 #endif
541 
542 	if (is_stdin)
543 		return buf_pos;
544 
545 	if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL)
546 		goto eexit;
547 
548 	return buf_pos;
549 
550 eexit:
551 	logerr(__func__);
552 #ifndef HAVE_OPEN_MEMSTREAM
553 	if (fp != NULL)
554 		fclose(fp);
555 #endif
556 	return -1;
557 }
558 
559 static int
560 send_interface1(struct fd_list *fd, const struct interface *ifp,
561     const char *reason)
562 {
563 	struct dhcpcd_ctx *ctx = ifp->ctx;
564 	long len;
565 
566 	len = make_env(ifp->ctx, ifp, reason);
567 	if (len == -1)
568 		return -1;
569 	return control_queue(fd, ctx->script_buf, (size_t)len);
570 }
571 
572 int
573 send_interface(struct fd_list *fd, const struct interface *ifp, int af)
574 {
575 	int retval = 0;
576 #ifdef INET
577 	const struct dhcp_state *d;
578 #endif
579 #ifdef DHCP6
580 	const struct dhcp6_state *d6;
581 #endif
582 
583 #ifndef AF_LINK
584 #define	AF_LINK	AF_PACKET
585 #endif
586 
587 	if (af == AF_UNSPEC || af == AF_LINK) {
588 		const char *reason;
589 
590 		switch (ifp->carrier) {
591 		case LINK_UP:
592 			reason = "CARRIER";
593 			break;
594 		case LINK_DOWN:
595 			reason = "NOCARRIER";
596 			break;
597 		default:
598 			reason = "UNKNOWN";
599 			break;
600 		}
601 		if (fd != NULL) {
602 			if (send_interface1(fd, ifp, reason) == -1)
603 				retval = -1;
604 		} else
605 			retval++;
606 	}
607 
608 #ifdef INET
609 	if (af == AF_UNSPEC || af == AF_INET) {
610 		if (D_STATE_RUNNING(ifp)) {
611 			d = D_CSTATE(ifp);
612 			if (fd != NULL) {
613 				if (send_interface1(fd, ifp, d->reason) == -1)
614 					retval = -1;
615 			} else
616 				retval++;
617 		}
618 #ifdef IPV4LL
619 		if (IPV4LL_STATE_RUNNING(ifp)) {
620 			if (fd != NULL) {
621 				if (send_interface1(fd, ifp, "IPV4LL") == -1)
622 					retval = -1;
623 			} else
624 				retval++;
625 		}
626 #endif
627 	}
628 #endif
629 
630 #ifdef INET6
631 	if (af == AF_UNSPEC || af == AF_INET6) {
632 		if (IPV6_STATE_RUNNING(ifp)) {
633 			if (fd != NULL) {
634 				if (send_interface1(fd, ifp, "STATIC6") == -1)
635 					retval = -1;
636 			} else
637 				retval++;
638 		}
639 		if (RS_STATE_RUNNING(ifp)) {
640 			if (fd != NULL) {
641 				if (send_interface1(fd, ifp,
642 				    "ROUTERADVERT") == -1)
643 					retval = -1;
644 			} else
645 				retval++;
646 		}
647 #ifdef DHCP6
648 		if (D6_STATE_RUNNING(ifp)) {
649 			d6 = D6_CSTATE(ifp);
650 			if (fd != NULL) {
651 				if (send_interface1(fd, ifp, d6->reason) == -1)
652 					retval = -1;
653 			} else
654 				retval++;
655 		}
656 #endif
657 	}
658 #endif
659 
660 	return retval;
661 }
662 
663 static int
664 script_run(struct dhcpcd_ctx *ctx, char **argv)
665 {
666 	pid_t pid;
667 	int status = 0;
668 
669 	pid = script_exec(argv, ctx->script_env);
670 	if (pid == -1)
671 		logerr("%s: %s", __func__, argv[0]);
672 	else if (pid != 0) {
673 		/* Wait for the script to finish */
674 		while (waitpid(pid, &status, 0) == -1) {
675 			if (errno != EINTR) {
676 				logerr("%s: waitpid", __func__);
677 				status = 0;
678 				break;
679 			}
680 		}
681 		if (WIFEXITED(status)) {
682 			if (WEXITSTATUS(status))
683 				logerrx("%s: %s: WEXITSTATUS %d",
684 				    __func__, argv[0], WEXITSTATUS(status));
685 		} else if (WIFSIGNALED(status))
686 			logerrx("%s: %s: %s",
687 			    __func__, argv[0], strsignal(WTERMSIG(status)));
688 	}
689 
690 	return WEXITSTATUS(status);
691 }
692 
693 int
694 script_dump(const char *env, size_t len)
695 {
696 	const char *ep = env + len;
697 
698 	if (len == 0)
699 		return 0;
700 
701 	if (*(ep - 1) != '\0') {
702 		errno = EINVAL;
703 		return -1;
704 	}
705 
706 	for (; env < ep; env += strlen(env) + 1) {
707 		if (strncmp(env, "new_", 4) == 0)
708 			env += 4;
709 		printf("%s\n", env);
710 	}
711 	return 0;
712 }
713 
714 int
715 script_runreason(const struct interface *ifp, const char *reason)
716 {
717 	struct dhcpcd_ctx *ctx = ifp->ctx;
718 	char *argv[2];
719 	int status = 0;
720 	struct fd_list *fd;
721 	long buflen;
722 
723 	if (ctx->script == NULL &&
724 	    TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
725 		return 0;
726 
727 	/* Make our env */
728 	if ((buflen = make_env(ifp->ctx, ifp, reason)) == -1) {
729 		logerr(__func__);
730 		return -1;
731 	}
732 
733 	if (strncmp(reason, "DUMP", 4) == 0)
734 		return script_dump(ctx->script_buf, (size_t)buflen);
735 
736 	if (ctx->script == NULL)
737 		goto send_listeners;
738 
739 	argv[0] = ctx->script;
740 	argv[1] = NULL;
741 	logdebugx("%s: executing: %s %s", ifp->name, argv[0], reason);
742 
743 #ifdef PRIVSEP
744 	if (ctx->options & DHCPCD_PRIVSEP) {
745 		if (ps_root_script(ctx,
746 		    ctx->script_buf, ctx->script_buflen) == -1)
747 			logerr(__func__);
748 		goto send_listeners;
749 	}
750 #endif
751 
752 	script_run(ctx, argv);
753 
754 send_listeners:
755 	/* Send to our listeners */
756 	status = 0;
757 	TAILQ_FOREACH(fd, &ctx->control_fds, next) {
758 		if (!(fd->flags & FD_LISTEN))
759 			continue;
760 		if (control_queue(fd, ctx->script_buf, ctx->script_buflen)== -1)
761 			logerr("%s: control_queue", __func__);
762 		else
763 			status = 1;
764 	}
765 
766 	return status;
767 }
768