1 /* SPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2021 Roy Marples <roy@marples.name>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
53 #include "if-options.h"
60 #define DEFAULT_PATH "/usr/bin:/usr/sbin:/bin:/sbin"
62 static const char * const if_params[] = {
77 static const char * true_str = "true";
78 static const char * false_str = "false";
83 const char * const *p;
85 for (p = if_params; *p; p++)
86 printf(" - %s\n", *p);
90 script_exec(char *const *argv, char *const *env)
93 posix_spawnattr_t attr;
103 /* posix_spawn is a safe way of executing another image
104 * and changing signals back to how they should be. */
105 if (posix_spawnattr_init(&attr) == -1)
108 flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF;
109 posix_spawnattr_setflags(&attr, flags);
110 sigemptyset(&defsigs);
111 posix_spawnattr_setsigmask(&attr, &defsigs);
112 for (i = 0; i < dhcpcd_signals_len; i++)
113 sigaddset(&defsigs, dhcpcd_signals[i]);
114 for (i = 0; i < dhcpcd_signals_ignore_len; i++)
115 sigaddset(&defsigs, dhcpcd_signals_ignore[i]);
116 posix_spawnattr_setsigdefault(&attr, &defsigs);
119 r = posix_spawn(&pid, argv[0], NULL, &attr, argv, env);
120 posix_spawnattr_destroy(&attr);
130 append_config(FILE *fp, const char *prefix, const char *const *config)
137 /* Do we need to replace existing config rather than append? */
138 for (i = 0; config[i] != NULL; i++) {
139 if (efprintf(fp, "%s_%s", prefix, config[i]) == -1)
149 #define PROTO_IPV4LL 2
151 #define PROTO_DHCP6 4
152 #define PROTO_STATIC6 5
153 static const char *protocols[] = {
163 efprintf(FILE *fp, const char *fmt, ...)
169 r = vfprintf(fp, fmt, args);
173 /* Write a trailing NULL so we can easily create env strings. */
174 if (fputc('\0', fp) == EOF)
180 script_buftoenv(struct dhcpcd_ctx *ctx, char *buf, size_t len)
182 char **env, **envp, *bufp, *endp;
185 /* Count the terminated env strings.
186 * Assert that the terminations are correct. */
189 for (bufp = buf; bufp < endp; bufp++) {
193 assert(*(bufp + 1) != '\0');
198 assert(*(bufp - 1) == '\0');
202 if (ctx->script_envlen < nenv) {
203 env = reallocarray(ctx->script_env, nenv + 1, sizeof(*env));
206 ctx->script_env = env;
207 ctx->script_envlen = nenv;
211 envp = ctx->script_env;
213 endp--; /* Avoid setting the last \0 to an invalid pointer */
214 for (; bufp < endp; bufp++) {
220 return ctx->script_env;
224 make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp,
230 int protocol = PROTO_LINK;
231 const struct if_options *ifo;
232 const struct interface *ifp2;
234 bool is_stdin = ifp->name[0] == '\0';
235 const char *if_up, *if_down;
239 const struct dhcp_state *state;
241 const struct ipv4ll_state *istate;
245 const struct dhcp6_state *d6_state;
248 #ifdef HAVE_OPEN_MEMSTREAM
249 if (ctx->script_fp == NULL) {
250 fp = open_memstream(&ctx->script_buf, &ctx->script_buflen);
259 char tmpfile[] = "/tmp/dhcpcd-script-env-XXXXXX";
263 tmpfd = mkstemp(tmpfile);
265 logerr("%s: mkstemp", __func__);
269 fp = fdopen(tmpfd, "w+");
276 if (!(ifp->ctx->options & DHCPCD_DUMPLEASE)) {
277 /* Needed for scripts */
278 path = getenv("PATH");
279 if (efprintf(fp, "PATH=%s",
280 path == NULL ? DEFAULT_PATH : path) == -1)
282 if (efprintf(fp, "pid=%d", getpid()) == -1)
287 if (efprintf(fp, "reason=%s", reason) == -1)
293 state = D_STATE(ifp);
295 istate = IPV4LL_CSTATE(ifp);
299 d6_state = D6_CSTATE(ifp);
301 if (strcmp(reason, "TEST") == 0) {
303 /* This space left intentionally blank
304 * as all the below statements are optional. */
308 else if (d6_state && d6_state->new)
309 protocol = PROTO_DHCP6;
311 else if (ipv6nd_hasra(ifp))
316 else if (istate && istate->addr != NULL)
317 protocol = PROTO_IPV4LL;
320 protocol = PROTO_DHCP;
324 else if (strcmp(reason, "STATIC6") == 0)
325 protocol = PROTO_STATIC6;
327 else if (reason[strlen(reason) - 1] == '6')
328 protocol = PROTO_DHCP6;
330 else if (strcmp(reason, "ROUTERADVERT") == 0)
333 else if (strcmp(reason, "PREINIT") == 0 ||
334 strcmp(reason, "CARRIER") == 0 ||
335 strcmp(reason, "NOCARRIER") == 0 ||
336 strcmp(reason, "NOCARRIER_ROAMING") == 0 ||
337 strcmp(reason, "UNKNOWN") == 0 ||
338 strcmp(reason, "DEPARTED") == 0 ||
339 strcmp(reason, "STOPPED") == 0)
340 protocol = PROTO_LINK;
343 else if (strcmp(reason, "IPV4LL") == 0)
344 protocol = PROTO_IPV4LL;
347 protocol = PROTO_DHCP;
351 if (efprintf(fp, "interface=%s", ifp->name) == -1)
353 if (protocols[protocol] != NULL) {
354 if (efprintf(fp, "protocol=%s",
355 protocols[protocol]) == -1)
359 if (ifp->ctx->options & DHCPCD_DUMPLEASE && protocol != PROTO_LINK)
361 if (efprintf(fp, "if_configured=%s",
362 ifo->options & DHCPCD_CONFIGURE ? "true" : "false") == -1)
364 if (efprintf(fp, "ifcarrier=%s",
365 ifp->carrier == LINK_UNKNOWN ? "unknown" :
366 ifp->carrier == LINK_UP ? "up" : "down") == -1)
368 if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1)
370 if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1)
372 if (efprintf(fp, "ifflags=%u", ifp->flags) == -1)
374 if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1)
377 char pssid[IF_SSIDLEN * 4];
379 if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
380 ifp->ssid, ifp->ssid_len) != -1)
382 if (efprintf(fp, "ifssid=%s", pssid) == -1)
386 if (*ifp->profile != '\0') {
387 if (efprintf(fp, "profile=%s", ifp->profile) == -1)
390 if (ifp->ctx->options & DHCPCD_DUMPLEASE)
393 ifp->ctx->rt_order = 0;
394 rb_tree_init(&ifaces, &rt_compare_proto_ops);
395 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
398 rt = rt_new(UNCONST(ifp2));
401 if (rt_proto_add(&ifaces, rt) != rt)
404 if (fprintf(fp, "interface_order=") == -1)
406 RB_TREE_FOREACH(rt, &ifaces) {
407 if (rt != RB_TREE_MIN(&ifaces) &&
408 fprintf(fp, "%s", " ") == -1)
410 if (fprintf(fp, "%s", rt->rt_ifp->name) == -1)
413 rt_headclear(&ifaces, AF_UNSPEC);
414 if (fputc('\0', fp) == EOF)
417 if (strcmp(reason, "STOPPED") == 0) {
419 if_down = ifo->options & DHCPCD_RELEASE ? true_str : false_str;
420 } else if (strcmp(reason, "TEST") == 0 ||
421 strcmp(reason, "PREINIT") == 0 ||
422 strcmp(reason, "CARRIER") == 0 ||
423 strcmp(reason, "UNKNOWN") == 0)
427 } else if (strcmp(reason, "NOCARRIER") == 0) {
430 } else if (strcmp(reason, "NOCARRIER_ROAMING") == 0) {
433 } else if (1 == 2 /* appease ifdefs */
435 || (protocol == PROTO_DHCP && state && state->new)
437 || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
441 || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
443 || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
445 || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
455 if (efprintf(fp, "if_up=%s", if_up) == -1)
457 if (efprintf(fp, "if_down=%s", if_down) == -1)
460 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
461 if (efprintf(fp, "if_afwaiting=%d", af) == -1)
464 if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
465 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
466 if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
471 if (efprintf(fp, "af_waiting=%d", af) == -1)
474 if (ifo->options & DHCPCD_DEBUG) {
475 if (efprintf(fp, "syslog_debug=true") == -1)
479 if (protocol == PROTO_DHCP && state && state->old) {
480 if (dhcp_env(fp, "old", ifp,
481 state->old, state->old_len) == -1)
483 if (append_config(fp, "old",
484 (const char *const *)ifo->config) == -1)
489 if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
490 if (dhcp6_env(fp, "old", ifp,
491 d6_state->old, d6_state->old_len) == -1)
499 if (protocol == PROTO_IPV4LL && istate) {
500 if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1)
504 if (protocol == PROTO_DHCP && state && state->new) {
505 if (dhcp_env(fp, "new", ifp,
506 state->new, state->new_len) == -1)
508 if (append_config(fp, "new",
509 (const char *const *)ifo->config) == -1)
514 if (protocol == PROTO_STATIC6) {
515 if (ipv6_env(fp, "new", ifp) == -1)
519 if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
520 if (dhcp6_env(fp, "new", ifp,
521 d6_state->new, d6_state->new_len) == -1)
525 if (protocol == PROTO_RA) {
526 if (ipv6nd_env(fp, ifp) == -1)
531 /* Add our base environment */
533 for (i = 0; ifo->environ[i] != NULL; i++)
534 if (efprintf(fp, "%s", ifo->environ[i]) == -1)
538 /* Convert buffer to argv */
547 #ifndef HAVE_OPEN_MEMSTREAM
548 size_t buf_len = (size_t)buf_pos;
549 if (ctx->script_buflen < buf_len) {
550 char *buf = realloc(ctx->script_buf, buf_len);
553 ctx->script_buf = buf;
554 ctx->script_buflen = buf_len;
557 if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len)
566 if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL)
573 #ifndef HAVE_OPEN_MEMSTREAM
581 send_interface1(struct fd_list *fd, const struct interface *ifp,
584 struct dhcpcd_ctx *ctx = ifp->ctx;
587 len = make_env(ifp->ctx, ifp, reason);
590 return control_queue(fd, ctx->script_buf, (size_t)len);
594 send_interface(struct fd_list *fd, const struct interface *ifp, int af)
598 const struct dhcp_state *d;
601 const struct dhcp6_state *d6;
605 #define AF_LINK AF_PACKET
608 if (af == AF_UNSPEC || af == AF_LINK) {
611 switch (ifp->carrier) {
616 reason = "NOCARRIER";
623 if (send_interface1(fd, ifp, reason) == -1)
630 if (af == AF_UNSPEC || af == AF_INET) {
631 if (D_STATE_RUNNING(ifp)) {
634 if (send_interface1(fd, ifp, d->reason) == -1)
640 if (IPV4LL_STATE_RUNNING(ifp)) {
642 if (send_interface1(fd, ifp, "IPV4LL") == -1)
652 if (af == AF_UNSPEC || af == AF_INET6) {
653 if (IPV6_STATE_RUNNING(ifp)) {
655 if (send_interface1(fd, ifp, "STATIC6") == -1)
660 if (RS_STATE_RUNNING(ifp)) {
662 if (send_interface1(fd, ifp,
663 "ROUTERADVERT") == -1)
669 if (D6_STATE_RUNNING(ifp)) {
672 if (send_interface1(fd, ifp, d6->reason) == -1)
685 script_run(struct dhcpcd_ctx *ctx, char **argv)
690 pid = script_exec(argv, ctx->script_env);
692 logerr("%s: %s", __func__, argv[0]);
694 /* Wait for the script to finish */
695 while (waitpid(pid, &status, 0) == -1) {
696 if (errno != EINTR) {
697 logerr("%s: waitpid", __func__);
702 if (WIFEXITED(status)) {
703 if (WEXITSTATUS(status))
704 logerrx("%s: %s: WEXITSTATUS %d",
705 __func__, argv[0], WEXITSTATUS(status));
706 } else if (WIFSIGNALED(status))
707 logerrx("%s: %s: %s",
708 __func__, argv[0], strsignal(WTERMSIG(status)));
711 return WEXITSTATUS(status);
715 script_dump(const char *env, size_t len)
717 const char *ep = env + len;
722 if (*(ep - 1) != '\0') {
727 for (; env < ep; env += strlen(env) + 1) {
728 if (strncmp(env, "new_", 4) == 0)
736 script_runreason(const struct interface *ifp, const char *reason)
738 struct dhcpcd_ctx *ctx = ifp->ctx;
744 if (ctx->script == NULL &&
745 TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
749 if ((buflen = make_env(ifp->ctx, ifp, reason)) == -1) {
754 if (strncmp(reason, "DUMP", 4) == 0)
755 return script_dump(ctx->script_buf, (size_t)buflen);
757 if (ctx->script == NULL)
760 argv[0] = ctx->script;
762 logdebugx("%s: executing: %s %s", ifp->name, argv[0], reason);
765 if (ctx->options & DHCPCD_PRIVSEP) {
766 if (ps_root_script(ctx,
767 ctx->script_buf, ctx->script_buflen) == -1)
773 script_run(ctx, argv);
776 /* Send to our listeners */
778 TAILQ_FOREACH(fd, &ctx->control_fds, next) {
779 if (!(fd->flags & FD_LISTEN))
781 if (control_queue(fd, ctx->script_buf, ctx->script_buflen)== -1)
782 logerr("%s: control_queue", __func__);