xref: /dragonfly/contrib/dhcpcd/src/script.c (revision 2b7dbe20)
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, "ifcarrier=%s",
354 	    ifp->carrier == LINK_UNKNOWN ? "unknown" :
355 	    ifp->carrier == LINK_UP ? "up" : "down") == -1)
356 		goto eexit;
357 	if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1)
358 		goto eexit;
359 	if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1)
360 		goto eexit;
361 	if (efprintf(fp, "ifflags=%u", ifp->flags) == -1)
362 		goto eexit;
363 	if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1)
364 		goto eexit;
365 	if (ifp->wireless) {
366 		char pssid[IF_SSIDLEN * 4];
367 
368 		if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
369 		    ifp->ssid, ifp->ssid_len) != -1)
370 		{
371 			if (efprintf(fp, "ifssid=%s", pssid) == -1)
372 				goto eexit;
373 		}
374 	}
375 	if (*ifp->profile != '\0') {
376 		if (efprintf(fp, "profile=%s", ifp->profile) == -1)
377 			goto eexit;
378 	}
379 	if (ifp->ctx->options & DHCPCD_DUMPLEASE)
380 		goto dumplease;
381 
382 	if (fprintf(fp, "interface_order=") == -1)
383 		goto eexit;
384 	TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
385 		if (ifp2 != TAILQ_FIRST(ifp->ctx->ifaces)) {
386 			if (fputc(' ', fp) == EOF)
387 				return -1;
388 		}
389 		if (fprintf(fp, "%s", ifp2->name) == -1)
390 			return -1;
391 	}
392 	if (fputc('\0', fp) == EOF)
393 		return -1;
394 
395 	if (strcmp(reason, "STOPPED") == 0) {
396 		if (efprintf(fp, "if_up=false") == -1)
397 			goto eexit;
398 		if (efprintf(fp, "if_down=%s",
399 		    ifo->options & DHCPCD_RELEASE ? "true" : "false") == -1)
400 			goto eexit;
401 	} else if (strcmp(reason, "TEST") == 0 ||
402 	    strcmp(reason, "PREINIT") == 0 ||
403 	    strcmp(reason, "CARRIER") == 0 ||
404 	    strcmp(reason, "UNKNOWN") == 0)
405 	{
406 		if (efprintf(fp, "if_up=false") == -1)
407 			goto eexit;
408 		if (efprintf(fp, "if_down=false") == -1)
409 			goto eexit;
410 	} else if (1 == 2 /* appease ifdefs */
411 #ifdef INET
412 	    || (protocol == PROTO_DHCP && state && state->new)
413 #ifdef IPV4LL
414 	    || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
415 #endif
416 #endif
417 #ifdef INET6
418 	    || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
419 #ifdef DHCP6
420 	    || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
421 #endif
422 	    || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
423 #endif
424 	    )
425 	{
426 		if (efprintf(fp, "if_up=true") == -1)
427 			goto eexit;
428 		if (efprintf(fp, "if_down=false") == -1)
429 			goto eexit;
430 	} else {
431 		if (efprintf(fp, "if_up=false") == -1)
432 			goto eexit;
433 		if (efprintf(fp, "if_down=true") == -1)
434 			goto eexit;
435 	}
436 	if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
437 		if (efprintf(fp, "if_afwaiting=%d", af) == -1)
438 			goto eexit;
439 	}
440 	if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
441 		TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
442 			if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
443 				break;
444 		}
445 	}
446 	if (af != AF_MAX) {
447 		if (efprintf(fp, "af_waiting=%d", af) == -1)
448 			goto eexit;
449 	}
450 	if (ifo->options & DHCPCD_DEBUG) {
451 		if (efprintf(fp, "syslog_debug=true") == -1)
452 			goto eexit;
453 	}
454 #ifdef INET
455 	if (protocol == PROTO_DHCP && state && state->old) {
456 		if (dhcp_env(fp, "old", ifp,
457 		    state->old, state->old_len) == -1)
458 			goto eexit;
459 		if (append_config(fp, "old",
460 		    (const char *const *)ifo->config) == -1)
461 			goto eexit;
462 	}
463 #endif
464 #ifdef DHCP6
465 	if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
466 		if (dhcp6_env(fp, "old", ifp,
467 		    d6_state->old, d6_state->old_len) == -1)
468 			goto eexit;
469 	}
470 #endif
471 
472 dumplease:
473 #ifdef INET
474 #ifdef IPV4LL
475 	if (protocol == PROTO_IPV4LL && istate) {
476 		if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1)
477 			goto eexit;
478 	}
479 #endif
480 	if (protocol == PROTO_DHCP && state && state->new) {
481 		if (dhcp_env(fp, "new", ifp,
482 		    state->new, state->new_len) == -1)
483 			goto eexit;
484 		if (append_config(fp, "new",
485 		    (const char *const *)ifo->config) == -1)
486 			goto eexit;
487 	}
488 #endif
489 #ifdef INET6
490 	if (protocol == PROTO_STATIC6) {
491 		if (ipv6_env(fp, "new", ifp) == -1)
492 			goto eexit;
493 	}
494 #ifdef DHCP6
495 	if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
496 		if (dhcp6_env(fp, "new", ifp,
497 		    d6_state->new, d6_state->new_len) == -1)
498 			goto eexit;
499 	}
500 #endif
501 	if (protocol == PROTO_RA) {
502 		if (ipv6nd_env(fp, ifp) == -1)
503 			goto eexit;
504 	}
505 #endif
506 
507 	/* Add our base environment */
508 	if (ifo->environ) {
509 		for (i = 0; ifo->environ[i] != NULL; i++)
510 			if (efprintf(fp, "%s", ifo->environ[i]) == -1)
511 				goto eexit;
512 	}
513 
514 	/* Convert buffer to argv */
515 	fflush(fp);
516 
517 	buf_pos = ftell(fp);
518 	if (buf_pos == -1) {
519 		logerr(__func__);
520 		goto eexit;
521 	}
522 
523 #ifndef HAVE_OPEN_MEMSTREAM
524 	size_t buf_len = (size_t)buf_pos;
525 	if (ctx->script_buflen < buf_len) {
526 		char *buf = realloc(ctx->script_buf, buf_len);
527 		if (buf == NULL)
528 			goto eexit;
529 		ctx->script_buf = buf;
530 		ctx->script_buflen = buf_len;
531 	}
532 	rewind(fp);
533 	if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len)
534 		goto eexit;
535 	fclose(fp);
536 	fp = NULL;
537 #endif
538 
539 	if (is_stdin)
540 		return buf_pos;
541 
542 	if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL)
543 		goto eexit;
544 
545 	return buf_pos;
546 
547 eexit:
548 	logerr(__func__);
549 #ifndef HAVE_OPEN_MEMSTREAM
550 	if (fp != NULL)
551 		fclose(fp);
552 #endif
553 	return -1;
554 }
555 
556 static int
557 send_interface1(struct fd_list *fd, const struct interface *ifp,
558     const char *reason)
559 {
560 	struct dhcpcd_ctx *ctx = ifp->ctx;
561 	long len;
562 
563 	len = make_env(ifp->ctx, ifp, reason);
564 	if (len == -1)
565 		return -1;
566 	return control_queue(fd, ctx->script_buf, (size_t)len);
567 }
568 
569 int
570 send_interface(struct fd_list *fd, const struct interface *ifp, int af)
571 {
572 	int retval = 0;
573 #ifdef INET
574 	const struct dhcp_state *d;
575 #endif
576 #ifdef DHCP6
577 	const struct dhcp6_state *d6;
578 #endif
579 
580 #ifndef AF_LINK
581 #define	AF_LINK	AF_PACKET
582 #endif
583 
584 	if (af == AF_UNSPEC || af == AF_LINK) {
585 		const char *reason;
586 
587 		switch (ifp->carrier) {
588 		case LINK_UP:
589 			reason = "CARRIER";
590 			break;
591 		case LINK_DOWN:
592 			reason = "NOCARRIER";
593 			break;
594 		default:
595 			reason = "UNKNOWN";
596 			break;
597 		}
598 		if (fd != NULL) {
599 			if (send_interface1(fd, ifp, reason) == -1)
600 				retval = -1;
601 		} else
602 			retval++;
603 	}
604 
605 #ifdef INET
606 	if (af == AF_UNSPEC || af == AF_INET) {
607 		if (D_STATE_RUNNING(ifp)) {
608 			d = D_CSTATE(ifp);
609 			if (fd != NULL) {
610 				if (send_interface1(fd, ifp, d->reason) == -1)
611 					retval = -1;
612 			} else
613 				retval++;
614 		}
615 #ifdef IPV4LL
616 		if (IPV4LL_STATE_RUNNING(ifp)) {
617 			if (fd != NULL) {
618 				if (send_interface1(fd, ifp, "IPV4LL") == -1)
619 					retval = -1;
620 			} else
621 				retval++;
622 		}
623 #endif
624 	}
625 #endif
626 
627 #ifdef INET6
628 	if (af == AF_UNSPEC || af == AF_INET6) {
629 		if (IPV6_STATE_RUNNING(ifp)) {
630 			if (fd != NULL) {
631 				if (send_interface1(fd, ifp, "STATIC6") == -1)
632 					retval = -1;
633 			} else
634 				retval++;
635 		}
636 		if (RS_STATE_RUNNING(ifp)) {
637 			if (fd != NULL) {
638 				if (send_interface1(fd, ifp,
639 				    "ROUTERADVERT") == -1)
640 					retval = -1;
641 			} else
642 				retval++;
643 		}
644 #ifdef DHCP6
645 		if (D6_STATE_RUNNING(ifp)) {
646 			d6 = D6_CSTATE(ifp);
647 			if (fd != NULL) {
648 				if (send_interface1(fd, ifp, d6->reason) == -1)
649 					retval = -1;
650 			} else
651 				retval++;
652 		}
653 #endif
654 	}
655 #endif
656 
657 	return retval;
658 }
659 
660 static int
661 script_run(struct dhcpcd_ctx *ctx, char **argv)
662 {
663 	pid_t pid;
664 	int status = 0;
665 
666 	pid = script_exec(argv, ctx->script_env);
667 	if (pid == -1)
668 		logerr("%s: %s", __func__, argv[0]);
669 	else if (pid != 0) {
670 		/* Wait for the script to finish */
671 		while (waitpid(pid, &status, 0) == -1) {
672 			if (errno != EINTR) {
673 				logerr("%s: waitpid", __func__);
674 				status = 0;
675 				break;
676 			}
677 		}
678 		if (WIFEXITED(status)) {
679 			if (WEXITSTATUS(status))
680 				logerrx("%s: %s: WEXITSTATUS %d",
681 				    __func__, argv[0], WEXITSTATUS(status));
682 		} else if (WIFSIGNALED(status))
683 			logerrx("%s: %s: %s",
684 			    __func__, argv[0], strsignal(WTERMSIG(status)));
685 	}
686 
687 	return WEXITSTATUS(status);
688 }
689 
690 int
691 script_dump(const char *env, size_t len)
692 {
693 	const char *ep = env + len;
694 
695 	if (len == 0)
696 		return 0;
697 
698 	if (*(ep - 1) != '\0') {
699 		errno = EINVAL;
700 		return -1;
701 	}
702 
703 	for (; env < ep; env += strlen(env) + 1) {
704 		if (strncmp(env, "new_", 4) == 0)
705 			env += 4;
706 		printf("%s\n", env);
707 	}
708 	return 0;
709 }
710 
711 int
712 script_runreason(const struct interface *ifp, const char *reason)
713 {
714 	struct dhcpcd_ctx *ctx = ifp->ctx;
715 	char *argv[2];
716 	int status = 0;
717 	struct fd_list *fd;
718 	long buflen;
719 
720 	if (ctx->script == NULL &&
721 	    TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
722 		return 0;
723 
724 	/* Make our env */
725 	if ((buflen = make_env(ifp->ctx, ifp, reason)) == -1) {
726 		logerr(__func__);
727 		return -1;
728 	}
729 
730 	if (strncmp(reason, "DUMP", 4) == 0)
731 		return script_dump(ctx->script_buf, (size_t)buflen);
732 
733 	if (ctx->script == NULL)
734 		goto send_listeners;
735 
736 	argv[0] = ctx->script;
737 	argv[1] = NULL;
738 	logdebugx("%s: executing: %s %s", ifp->name, argv[0], reason);
739 
740 #ifdef PRIVSEP
741 	if (ctx->options & DHCPCD_PRIVSEP) {
742 		if (ps_root_script(ctx,
743 		    ctx->script_buf, ctx->script_buflen) == -1)
744 			logerr(__func__);
745 		goto send_listeners;
746 	}
747 #endif
748 
749 	script_run(ctx, argv);
750 
751 send_listeners:
752 	/* Send to our listeners */
753 	status = 0;
754 	TAILQ_FOREACH(fd, &ctx->control_fds, next) {
755 		if (!(fd->flags & FD_LISTEN))
756 			continue;
757 		if (control_queue(fd, ctx->script_buf, ctx->script_buflen)== -1)
758 			logerr("%s: control_queue", __func__);
759 		else
760 			status = 1;
761 	}
762 
763 	return status;
764 }
765