1 /* stSPDX-License-Identifier: BSD-2-Clause */
3 * dhcpcd - DHCP client daemon
4 * Copyright (c) 2006-2020 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[] = {
80 const char * const *p;
82 for (p = if_params; *p; p++)
83 printf(" - %s\n", *p);
87 script_exec(char *const *argv, char *const *env)
90 posix_spawnattr_t attr;
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)
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 posix_spawnattr_setsigdefault(&attr, &defsigs);
114 r = posix_spawn(&pid, argv[0], NULL, &attr, argv, env);
115 posix_spawnattr_destroy(&attr);
125 append_config(FILE *fp, const char *prefix, const char *const *config)
132 /* Do we need to replace existing config rather than append? */
133 for (i = 0; config[i] != NULL; i++) {
134 if (efprintf(fp, "%s_%s", prefix, config[i]) == -1)
144 #define PROTO_IPV4LL 2
146 #define PROTO_DHCP6 4
147 #define PROTO_STATIC6 5
148 static const char *protocols[] = {
158 efprintf(FILE *fp, const char *fmt, ...)
164 r = vfprintf(fp, fmt, args);
168 /* Write a trailing NULL so we can easily create env strings. */
169 if (fputc('\0', fp) == EOF)
175 script_buftoenv(struct dhcpcd_ctx *ctx, char *buf, size_t len)
177 char **env, **envp, *bufp, *endp;
180 /* Count the terminated env strings.
181 * Assert that the terminations are correct. */
184 for (bufp = buf; bufp < endp; bufp++) {
188 assert(*(bufp + 1) != '\0');
193 assert(*(bufp - 1) == '\0');
195 if (ctx->script_envlen < nenv) {
196 env = reallocarray(ctx->script_env, nenv + 1, sizeof(*env));
199 ctx->script_env = env;
200 ctx->script_envlen = nenv;
204 envp = ctx->script_env;
206 endp--; /* Avoid setting the last \0 to an invalid pointer */
207 for (; bufp < endp; bufp++) {
213 return ctx->script_env;
217 make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp,
223 int protocol = PROTO_LINK;
224 const struct if_options *ifo;
225 const struct interface *ifp2;
228 const struct dhcp_state *state;
230 const struct ipv4ll_state *istate;
234 const struct dhcp6_state *d6_state;
237 #ifdef HAVE_OPEN_MEMSTREAM
238 if (ctx->script_fp == NULL) {
239 fp = open_memstream(&ctx->script_buf, &ctx->script_buflen);
248 char tmpfile[] = "/tmp/dhcpcd-script-env-XXXXXX";
252 tmpfd = mkstemp(tmpfile);
256 fp = fdopen(tmpfd, "w+");
263 /* Needed for scripts */
264 path = getenv("PATH");
265 if (efprintf(fp, "PATH=%s", path == NULL ? DEFAULT_PATH:path) == -1)
267 if (efprintf(fp, "reason=%s", reason) == -1)
269 if (efprintf(fp, "pid=%d", getpid()) == -1)
273 if (ctx->options & DHCPCD_PRIVSEP && ctx->ps_user != NULL) {
274 if (efprintf(fp, "chroot=%s", ctx->ps_user->pw_dir) == -1)
277 if (strcmp(reason, "CHROOT") == 0)
283 state = D_STATE(ifp);
285 istate = IPV4LL_CSTATE(ifp);
289 d6_state = D6_CSTATE(ifp);
291 if (strcmp(reason, "TEST") == 0) {
293 /* This space left intentionally blank
294 * as all the below statements are optional. */
298 else if (d6_state && d6_state->new)
299 protocol = PROTO_DHCP6;
301 else if (ipv6nd_hasra(ifp))
306 else if (istate && istate->addr != NULL)
307 protocol = PROTO_IPV4LL;
310 protocol = PROTO_DHCP;
314 else if (strcmp(reason, "STATIC6") == 0)
315 protocol = PROTO_STATIC6;
317 else if (reason[strlen(reason) - 1] == '6')
318 protocol = PROTO_DHCP6;
320 else if (strcmp(reason, "ROUTERADVERT") == 0)
323 else if (strcmp(reason, "PREINIT") == 0 ||
324 strcmp(reason, "CARRIER") == 0 ||
325 strcmp(reason, "NOCARRIER") == 0 ||
326 strcmp(reason, "UNKNOWN") == 0 ||
327 strcmp(reason, "DEPARTED") == 0 ||
328 strcmp(reason, "STOPPED") == 0)
329 protocol = PROTO_LINK;
332 else if (strcmp(reason, "IPV4LL") == 0)
333 protocol = PROTO_IPV4LL;
336 protocol = PROTO_DHCP;
340 if (efprintf(fp, "interface=%s", ifp->name) == -1)
342 if (ifp->ctx->options & DHCPCD_DUMPLEASE)
344 if (efprintf(fp, "ifcarrier=%s",
345 ifp->carrier == LINK_UNKNOWN ? "unknown" :
346 ifp->carrier == LINK_UP ? "up" : "down") == -1)
348 if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1)
350 if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1)
352 if (efprintf(fp, "ifflags=%u", ifp->flags) == -1)
354 if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1)
357 if (fprintf(fp, "interface_order=") == -1)
359 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
360 if (ifp2 != TAILQ_FIRST(ifp->ctx->ifaces)) {
361 if (fputc(' ', fp) == EOF)
364 if (fprintf(fp, "%s", ifp2->name) == -1)
367 if (fputc('\0', fp) == EOF)
370 if (strcmp(reason, "STOPPED") == 0) {
371 if (efprintf(fp, "if_up=false") == -1)
373 if (efprintf(fp, "if_down=%s",
374 ifo->options & DHCPCD_RELEASE ? "true" : "false") == -1)
376 } else if (strcmp(reason, "TEST") == 0 ||
377 strcmp(reason, "PREINIT") == 0 ||
378 strcmp(reason, "CARRIER") == 0 ||
379 strcmp(reason, "UNKNOWN") == 0)
381 if (efprintf(fp, "if_up=false") == -1)
383 if (efprintf(fp, "if_down=false") == -1)
385 } else if (1 == 2 /* appease ifdefs */
387 || (protocol == PROTO_DHCP && state && state->new)
389 || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp))
393 || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp))
395 || (protocol == PROTO_DHCP6 && d6_state && d6_state->new)
397 || (protocol == PROTO_RA && ipv6nd_hasra(ifp))
401 if (efprintf(fp, "if_up=true") == -1)
403 if (efprintf(fp, "if_down=false") == -1)
406 if (efprintf(fp, "if_up=false") == -1)
408 if (efprintf(fp, "if_down=true") == -1)
411 if (protocols[protocol] != NULL) {
412 if (efprintf(fp, "protocol=%s", protocols[protocol]) == -1)
415 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) {
416 if (efprintf(fp, "if_afwaiting=%d", af) == -1)
419 if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) {
420 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) {
421 if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX)
426 if (efprintf(fp, "af_waiting=%d", af) == -1)
429 if (ifo->options & DHCPCD_DEBUG) {
430 if (efprintf(fp, "syslog_debug=true") == -1)
433 if (*ifp->profile != '\0') {
434 if (efprintf(fp, "profile=%s", ifp->profile) == -1)
438 char pssid[IF_SSIDLEN * 4];
440 if (print_string(pssid, sizeof(pssid), OT_ESCSTRING,
441 ifp->ssid, ifp->ssid_len) != -1)
443 if (efprintf(fp, "ifssid=%s", pssid) == -1)
448 if (protocol == PROTO_DHCP && state && state->old) {
449 if (dhcp_env(fp, "old", ifp,
450 state->old, state->old_len) == -1)
452 if (append_config(fp, "old",
453 (const char *const *)ifo->config) == -1)
458 if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) {
459 if (dhcp6_env(fp, "old", ifp,
460 d6_state->old, d6_state->old_len) == -1)
468 if (protocol == PROTO_IPV4LL && istate) {
469 if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1)
473 if (protocol == PROTO_DHCP && state && state->new) {
474 if (dhcp_env(fp, "new", ifp,
475 state->new, state->new_len) == -1)
477 if (append_config(fp, "new",
478 (const char *const *)ifo->config) == -1)
483 if (protocol == PROTO_STATIC6) {
484 if (ipv6_env(fp, "new", ifp) == -1)
488 if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) {
489 if (dhcp6_env(fp, "new", ifp,
490 d6_state->new, d6_state->new_len) == -1)
494 if (protocol == PROTO_RA) {
495 if (ipv6nd_env(fp, ifp) == -1)
500 /* Add our base environment */
502 for (i = 0; ifo->environ[i] != NULL; i++)
503 if (efprintf(fp, "%s", ifo->environ[i]) == -1)
510 /* Convert buffer to argv */
519 #ifndef HAVE_OPEN_MEMSTREAM
520 size_t buf_len = (size_t)buf_pos;
521 if (ctx->script_buflen < buf_len) {
522 char *buf = realloc(ctx->script_buf, buf_len);
525 ctx->script_buf = buf;
526 ctx->script_buflen = buf_len;
529 if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len)
535 if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL)
542 #ifndef HAVE_OPEN_MEMSTREAM
550 send_interface1(struct fd_list *fd, const struct interface *ifp,
553 struct dhcpcd_ctx *ctx = ifp->ctx;
556 len = make_env(ifp->ctx, ifp, reason);
559 return control_queue(fd, ctx->script_buf, (size_t)len, 1);
563 send_interface(struct fd_list *fd, const struct interface *ifp, int af)
567 const struct dhcp_state *d;
570 const struct dhcp6_state *d6;
574 #define AF_LINK AF_PACKET
577 if (af == AF_UNSPEC || af == AF_LINK) {
580 switch (ifp->carrier) {
585 case LINK_DOWN_IFFUP:
586 reason = "NOCARRIER";
593 if (send_interface1(fd, ifp, reason) == -1)
600 if (af == AF_UNSPEC || af == AF_INET) {
601 if (D_STATE_RUNNING(ifp)) {
604 if (send_interface1(fd, ifp, d->reason) == -1)
610 if (IPV4LL_STATE_RUNNING(ifp)) {
612 if (send_interface1(fd, ifp, "IPV4LL") == -1)
622 if (af == AF_UNSPEC || af == AF_INET6) {
623 if (IPV6_STATE_RUNNING(ifp)) {
625 if (send_interface1(fd, ifp, "STATIC6") == -1)
630 if (RS_STATE_RUNNING(ifp)) {
632 if (send_interface1(fd, ifp,
633 "ROUTERADVERT") == -1)
639 if (D6_STATE_RUNNING(ifp)) {
642 if (send_interface1(fd, ifp, d6->reason) == -1)
655 script_run(struct dhcpcd_ctx *ctx, char **argv)
660 pid = script_exec(argv, ctx->script_env);
662 logerr("%s: %s", __func__, argv[0]);
664 /* Wait for the script to finish */
665 while (waitpid(pid, &status, 0) == -1) {
666 if (errno != EINTR) {
667 logerr("%s: waitpid", __func__);
672 if (WIFEXITED(status)) {
673 if (WEXITSTATUS(status))
674 logerrx("%s: %s: WEXITSTATUS %d",
675 __func__, argv[0], WEXITSTATUS(status));
676 } else if (WIFSIGNALED(status))
677 logerrx("%s: %s: %s",
678 __func__, argv[0], strsignal(WTERMSIG(status)));
681 return WEXITSTATUS(status);
685 script_runreason(const struct interface *ifp, const char *reason)
687 struct dhcpcd_ctx *ctx = ifp->ctx;
692 if (ifp->options->script == NULL &&
693 TAILQ_FIRST(&ifp->ctx->control_fds) == NULL)
697 if (make_env(ifp->ctx, ifp, reason) == -1) {
702 if (ifp->options->script == NULL)
705 argv[0] = ifp->options->script;
707 logdebugx("%s: executing `%s' %s", ifp->name, argv[0], reason);
710 if (ctx->options & DHCPCD_PRIVSEP) {
711 if (ps_root_script(ifp,
712 ctx->script_buf, ctx->script_buflen) == -1)
718 status = script_run(ctx, argv);
721 /* Send to our listeners */
723 TAILQ_FOREACH(fd, &ctx->control_fds, next) {
724 if (!(fd->flags & FD_LISTEN))
726 if (control_queue(fd, ctx->script_buf, ctx->script_buflen,
728 logerr("%s: control_queue", __func__);
738 script_runchroot(struct dhcpcd_ctx *ctx, char *script)
743 if (make_env(ctx, NULL, "CHROOT") == -1) {
750 logdebugx("executing `%s' %s", argv[0], "CHROOT");
752 return script_run(ctx, argv);