1 /* 2 * dhcpcd - DHCP client daemon 3 * SPDX-License-Identifier: BSD-2-Clause 4 * Copyright (c) 2006-2025 Roy Marples <roy (at) 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 35 #include <arpa/inet.h> 36 #include <ctype.h> 37 #include <errno.h> 38 #include <pwd.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 "eloop.h" 51 #include "if-options.h" 52 #include "if.h" 53 #include "ipv4ll.h" 54 #include "ipv6nd.h" 55 #include "logerr.h" 56 #include "privsep.h" 57 #include "script.h" 58 59 #define DEFAULT_PATH "/usr/bin:/usr/sbin:/bin:/sbin" 60 61 static const char *const if_params[] = { "interface", "ifxname", "protocol", 62 "reason", "pid", "ifcarrier", "ifmetric", "ifwireless", "ifflags", 63 "ssid", "profile", "interface_order", NULL }; 64 65 static const char *true_str = "true"; 66 static const char *false_str = "false"; 67 68 void 69 if_printoptions(void) 70 { 71 const char *const *p; 72 73 for (p = if_params; *p; p++) 74 printf(" - %s\n", *p); 75 } 76 77 pid_t 78 script_exec(char *const *argv, char *const *env) 79 { 80 pid_t pid = 0; 81 posix_spawnattr_t attr; 82 int r; 83 #ifdef USE_SIGNALS 84 size_t i; 85 short flags; 86 sigset_t defsigs; 87 #else 88 UNUSED(ctx); 89 #endif 90 91 /* posix_spawn is a safe way of executing another image 92 * and changing signals back to how they should be. */ 93 if (posix_spawnattr_init(&attr) == -1) 94 return -1; 95 #ifdef USE_SIGNALS 96 flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF; 97 posix_spawnattr_setflags(&attr, flags); 98 sigemptyset(&defsigs); 99 posix_spawnattr_setsigmask(&attr, &defsigs); 100 for (i = 0; i < dhcpcd_signals_len; i++) 101 sigaddset(&defsigs, dhcpcd_signals[i]); 102 for (i = 0; i < dhcpcd_signals_ignore_len; i++) 103 sigaddset(&defsigs, dhcpcd_signals_ignore[i]); 104 posix_spawnattr_setsigdefault(&attr, &defsigs); 105 #endif 106 errno = 0; 107 r = posix_spawn(&pid, argv[0], NULL, &attr, argv, env); 108 posix_spawnattr_destroy(&attr); 109 if (r) { 110 errno = r; 111 return -1; 112 } 113 return pid; 114 } 115 116 #ifdef INET 117 static int 118 append_config(FILE *fp, const char *prefix, const char *const *config) 119 { 120 size_t i; 121 122 if (config == NULL) 123 return 0; 124 125 /* Do we need to replace existing config rather than append? */ 126 for (i = 0; config[i] != NULL; i++) { 127 if (efprintf(fp, "%s_%s", prefix, config[i]) == -1) 128 return -1; 129 } 130 return 1; 131 } 132 133 #endif 134 135 #define PROTO_LINK 0 136 #define PROTO_DHCP 1 137 #define PROTO_IPV4LL 2 138 #define PROTO_RA 3 139 #define PROTO_DHCP6 4 140 #define PROTO_STATIC6 5 141 static const char *protocols[] = { "link", "dhcp", "ipv4ll", "ra", "dhcp6", 142 "static6" }; 143 144 int 145 efprintf(FILE *fp, const char *fmt, ...) 146 { 147 va_list args; 148 int r; 149 150 va_start(args, fmt); 151 r = vfprintf(fp, fmt, args); 152 va_end(args); 153 if (r == -1) 154 return -1; 155 /* Write a trailing NULL so we can easily create env strings. */ 156 if (fputc('\0', fp) == EOF) 157 return -1; 158 return r; 159 } 160 161 char ** 162 script_buftoenv(struct dhcpcd_ctx *ctx, char *buf, size_t len) 163 { 164 char **env, **envp, *bufp, *endp; 165 size_t nenv; 166 167 /* We should have something to process */ 168 if (buf == NULL || len == 0) { 169 errno = EINVAL; 170 return NULL; 171 } 172 173 /* Ensure the buffer ends with NUL */ 174 if (buf[len - 1] != '\0') { 175 errno = EINVAL; 176 return NULL; 177 } 178 179 /* Count the NUL-terminated env strings */ 180 nenv = 0; 181 endp = buf + len; 182 for (bufp = buf; bufp < endp; bufp++) { 183 if (*bufp == '\0') { 184 /* Skip consecutive NULs safely without crashing */ 185 while (bufp + 1 < endp && *(bufp + 1) == '\0') { 186 bufp++; 187 } 188 nenv++; 189 } 190 } 191 if (nenv == 0) 192 return NULL; 193 194 if (ctx->script_envlen < nenv) { 195 env = reallocarray(ctx->script_env, nenv + 1, sizeof(*env)); 196 if (env == NULL) 197 return NULL; 198 ctx->script_env = env; 199 ctx->script_envlen = nenv; 200 } 201 202 bufp = buf; 203 envp = ctx->script_env; 204 205 /* If first char is NUL, skip leading empty strings if any, 206 * or handle them gracefully */ 207 while (bufp < endp && *bufp == '\0') 208 bufp++; 209 if (bufp < endp) 210 *envp++ = bufp; 211 212 /* Walk and capture pointers to each environment variable */ 213 for (; bufp < endp - 1; bufp++) { 214 if (*bufp == '\0') { 215 /* Skip consecutive NULs */ 216 while (bufp < endp - 1 && *(bufp + 1) == '\0') 217 bufp++; 218 if (bufp + 1 < endp) 219 *envp++ = bufp + 1; 220 } 221 } 222 *envp = NULL; 223 224 return ctx->script_env; 225 } 226 227 static long 228 make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, 229 const char *reason) 230 { 231 FILE *fp; 232 long buf_pos, i; 233 char *path; 234 int protocol = PROTO_LINK; 235 const struct if_options *ifo; 236 const struct interface *ifp2; 237 int af; 238 bool is_stdin = ifp->name[0] == '\0'; 239 char if_name[sizeof(ifp->name) * 4]; 240 const char *if_up, *if_down; 241 rb_tree_t ifaces; 242 struct rt *rt; 243 #ifdef INET 244 const struct dhcp_state *state; 245 #ifdef IPV4LL 246 const struct ipv4ll_state *istate; 247 #endif 248 #endif 249 #ifdef DHCP6 250 const struct dhcp6_state *d6_state; 251 #endif 252 253 #ifdef HAVE_OPEN_MEMSTREAM 254 if (ctx->script_fp == NULL) { 255 fp = open_memstream(&ctx->script_buf, &ctx->script_buflen); 256 if (fp == NULL) 257 goto eexit; 258 ctx->script_fp = fp; 259 } else { 260 fp = ctx->script_fp; 261 rewind(fp); 262 } 263 #else 264 char tmpfile[] = "/tmp/dhcpcd-script-env-XXXXXX"; 265 int tmpfd; 266 267 fp = NULL; 268 tmpfd = mkstemp(tmpfile); 269 if (tmpfd == -1) { 270 logerr("%s: mkstemp", __func__); 271 return -1; 272 } 273 unlink(tmpfile); 274 fp = fdopen(tmpfd, "w+"); 275 if (fp == NULL) { 276 close(tmpfd); 277 goto eexit; 278 } 279 #endif 280 281 if (!(ifp->ctx->options & DHCPCD_DUMPLEASE)) { 282 /* Needed for scripts */ 283 path = getenv("PATH"); 284 if (efprintf(fp, "PATH=%s", 285 path == NULL ? DEFAULT_PATH : path) == -1) 286 goto eexit; 287 if (efprintf(fp, "pid=%d", (int)getpid()) == -1) 288 goto eexit; 289 } 290 291 if (!is_stdin) { 292 if (efprintf(fp, "reason=%s", reason) == -1) 293 goto eexit; 294 } 295 296 ifo = ifp->options; 297 #ifdef INET 298 state = D_STATE(ifp); 299 #ifdef IPV4LL 300 istate = IPV4LL_CSTATE(ifp); 301 #endif 302 #endif 303 #ifdef DHCP6 304 d6_state = D6_CSTATE(ifp); 305 #endif 306 if (strcmp(reason, "TEST") == 0) { 307 if (1 == 2) { 308 /* This space left intentionally blank 309 * as all the below statements are optional. */ 310 } 311 #ifdef INET6 312 #ifdef DHCP6 313 else if (d6_state && d6_state->new) 314 protocol = PROTO_DHCP6; 315 #endif 316 else if (ipv6nd_hasra(ifp)) 317 protocol = PROTO_RA; 318 #endif 319 #ifdef INET 320 #ifdef IPV4LL 321 else if (istate && istate->addr != NULL) 322 protocol = PROTO_IPV4LL; 323 #endif 324 else 325 protocol = PROTO_DHCP; 326 #endif 327 } 328 #ifdef INET6 329 else if (strcmp(reason, "STATIC6") == 0) 330 protocol = PROTO_STATIC6; 331 #ifdef DHCP6 332 else if (reason[strlen(reason) - 1] == '6') 333 protocol = PROTO_DHCP6; 334 #endif 335 else if (strcmp(reason, "ROUTERADVERT") == 0) 336 protocol = PROTO_RA; 337 #endif 338 else if (strcmp(reason, "PREINIT") == 0 || 339 strcmp(reason, "CARRIER") == 0 || 340 strcmp(reason, "NOCARRIER") == 0 || 341 strcmp(reason, "NOCARRIER_ROAMING") == 0 || 342 strcmp(reason, "UNKNOWN") == 0 || strcmp(reason, "DEPARTED") == 0 || 343 strcmp(reason, "STOPPED") == 0) 344 protocol = PROTO_LINK; 345 #ifdef INET 346 #ifdef IPV4LL 347 else if (strcmp(reason, "IPV4LL") == 0) 348 protocol = PROTO_IPV4LL; 349 #endif 350 else 351 protocol = PROTO_DHCP; 352 #endif 353 354 if (!is_stdin) { 355 /* 356 * $interface is used to create files. 357 * $ifxname is used to reference the actual interface. 358 * The distinction is important as the actual interface name 359 * could contain a path separator or an invalid path character. 360 */ 361 if (print_string(if_name, sizeof(if_name), OT_ESCFILE, 362 ifp->name, strlen(ifp->name)) == -1) 363 goto eexit; 364 if (efprintf(fp, "interface=%s", if_name) == -1) 365 goto eexit; 366 if (print_string(if_name, sizeof(if_name), OT_ESCSTRING, 367 ifp->name, strlen(ifp->name)) == -1) 368 goto eexit; 369 if (efprintf(fp, "ifxname=%s", if_name) == -1) 370 goto eexit; 371 if (protocols[protocol] != NULL) { 372 if (efprintf(fp, "protocol=%s", protocols[protocol]) == 373 -1) 374 goto eexit; 375 } 376 } 377 if (ifp->ctx->options & DHCPCD_DUMPLEASE && protocol != PROTO_LINK) 378 goto dumplease; 379 if (efprintf(fp, "if_configured=%s", 380 ifo->options & DHCPCD_CONFIGURE ? "true" : "false") == -1) 381 goto eexit; 382 if (efprintf(fp, "ifcarrier=%s", 383 ifp->carrier == LINK_UNKNOWN ? "unknown" : 384 ifp->carrier == LINK_UP ? "up" : 385 "down") == -1) 386 goto eexit; 387 if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1) 388 goto eexit; 389 if (efprintf(fp, "ifwireless=%d", ifp->wireless) == -1) 390 goto eexit; 391 if (efprintf(fp, "ifflags=%u", ifp->flags) == -1) 392 goto eexit; 393 if (efprintf(fp, "ifmtu=%d", if_getmtu(ifp)) == -1) 394 goto eexit; 395 if (ifp->wireless) { 396 char pssid[IF_SSIDLEN * 4]; 397 398 if (print_string(pssid, sizeof(pssid), OT_ESCSTRING, ifp->ssid, 399 ifp->ssid_len) != -1) { 400 if (efprintf(fp, "ifssid=%s", pssid) == -1) 401 goto eexit; 402 } 403 } 404 if (*ifp->profile != '\0') { 405 if (efprintf(fp, "profile=%s", ifp->profile) == -1) 406 goto eexit; 407 } 408 if (ifp->ctx->options & DHCPCD_DUMPLEASE) 409 goto dumplease; 410 411 ifp->ctx->rt_order = 0; 412 rb_tree_init(&ifaces, &rt_compare_proto_ops); 413 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) { 414 if (!ifp2->active) 415 continue; 416 rt = rt_new(UNCONST(ifp2)); 417 if (rt == NULL) 418 goto eexit; 419 if (rt_proto_add(&ifaces, rt) != rt) 420 goto eexit; 421 } 422 if (fprintf(fp, "interface_order=") == -1) 423 goto eexit; 424 RB_TREE_FOREACH(rt, &ifaces) 425 { 426 if (print_string(if_name, sizeof(if_name), OT_ESCFILE, 427 rt->rt_ifp->name, strlen(rt->rt_ifp->name)) == -1) 428 goto eexit; 429 if (rt != RB_TREE_MIN(&ifaces) && fprintf(fp, "%s", " ") == -1) 430 goto eexit; 431 if (fprintf(fp, "%s", if_name) == -1) 432 goto eexit; 433 } 434 rt_headclear(&ifaces, AF_UNSPEC); 435 if (fputc('\0', fp) == EOF) 436 goto eexit; 437 438 if (strcmp(reason, "STOPPED") == 0) { 439 if_up = false_str; 440 if_down = ifo->options & DHCPCD_RELEASE ? true_str : false_str; 441 } else if (strcmp(reason, "TEST") == 0 || 442 strcmp(reason, "PREINIT") == 0 || strcmp(reason, "CARRIER") == 0 || 443 strcmp(reason, "STOP") == 0 || strcmp(reason, "UNKNOWN") == 0) { 444 if_up = false_str; 445 if_down = false_str; 446 } else if (strcmp(reason, "NOCARRIER") == 0) { 447 if_up = false_str; 448 if_down = true_str; 449 } else if (strcmp(reason, "NOCARRIER_ROAMING") == 0) { 450 if_up = true_str; 451 if_down = false_str; 452 } else if (1 == 2 /* appease ifdefs */ 453 #ifdef INET 454 || (protocol == PROTO_DHCP && state && state->new) 455 #ifdef IPV4LL 456 || (protocol == PROTO_IPV4LL && IPV4LL_STATE_RUNNING(ifp)) 457 #endif 458 #endif 459 #ifdef INET6 460 || (protocol == PROTO_STATIC6 && IPV6_STATE_RUNNING(ifp)) 461 #ifdef DHCP6 462 || (protocol == PROTO_DHCP6 && d6_state && d6_state->new) 463 #endif 464 || (protocol == PROTO_RA && ipv6nd_hasra(ifp)) 465 #endif 466 ) { 467 if_up = true_str; 468 if_down = false_str; 469 } else { 470 if_up = false_str; 471 if_down = true_str; 472 } 473 if (efprintf(fp, "if_up=%s", if_up) == -1) 474 goto eexit; 475 if (efprintf(fp, "if_down=%s", if_down) == -1) 476 goto eexit; 477 478 if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) { 479 if (efprintf(fp, "if_afwaiting=%d", af) == -1) 480 goto eexit; 481 } 482 if ((af = dhcpcd_afwaiting(ifp->ctx)) != AF_MAX) { 483 TAILQ_FOREACH(ifp2, ifp->ctx->ifaces, next) { 484 if ((af = dhcpcd_ifafwaiting(ifp2)) != AF_MAX) 485 break; 486 } 487 } 488 if (af != AF_MAX) { 489 if (efprintf(fp, "af_waiting=%d", af) == -1) 490 goto eexit; 491 } 492 if (loggetopts() & LOGERR_DEBUG) { 493 if (efprintf(fp, "syslog_debug=true") == -1) 494 goto eexit; 495 } 496 #ifdef INET 497 if (protocol == PROTO_DHCP && state && state->old) { 498 if (dhcp_env(fp, "old", ifp, state->old, state->old_len) == -1) 499 goto eexit; 500 if (append_config(fp, "old", 501 (const char *const *)ifo->config) == -1) 502 goto eexit; 503 } 504 #endif 505 #ifdef DHCP6 506 if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) { 507 if (dhcp6_env(fp, "old", ifp, d6_state->old, 508 d6_state->old_len) == -1) 509 goto eexit; 510 } 511 #endif 512 513 dumplease: 514 #ifdef INET 515 #ifdef IPV4LL 516 if (protocol == PROTO_IPV4LL && istate) { 517 if (ipv4ll_env(fp, istate->down ? "old" : "new", ifp) == -1) 518 goto eexit; 519 } 520 #endif 521 if (protocol == PROTO_DHCP && state && state->new) { 522 if (dhcp_env(fp, "new", ifp, state->new, state->new_len) == -1) 523 goto eexit; 524 if (append_config(fp, "new", 525 (const char *const *)ifo->config) == -1) 526 goto eexit; 527 } 528 #endif 529 #ifdef INET6 530 if (protocol == PROTO_STATIC6) { 531 if (ipv6_env(fp, "new", ifp) == -1) 532 goto eexit; 533 } 534 #ifdef DHCP6 535 if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) { 536 if (dhcp6_env(fp, "new", ifp, d6_state->new, 537 d6_state->new_len) == -1) 538 goto eexit; 539 } 540 #endif 541 if (protocol == PROTO_RA) { 542 if (ipv6nd_env(fp, ifp) == -1) 543 goto eexit; 544 } 545 #endif 546 547 /* Add our base environment */ 548 if (ifo->environ) { 549 for (i = 0; ifo->environ[i] != NULL; i++) 550 if (efprintf(fp, "%s", ifo->environ[i]) == -1) 551 goto eexit; 552 } 553 554 /* Convert buffer to argv */ 555 fflush(fp); 556 557 buf_pos = ftell(fp); 558 if (buf_pos == -1) { 559 logerr(__func__); 560 goto eexit; 561 } 562 563 #ifndef HAVE_OPEN_MEMSTREAM 564 size_t buf_len = (size_t)buf_pos; 565 if (ctx->script_buflen < buf_len) { 566 char *buf = realloc(ctx->script_buf, buf_len); 567 if (buf == NULL) 568 goto eexit; 569 ctx->script_buf = buf; 570 ctx->script_buflen = buf_len; 571 } 572 rewind(fp); 573 if (fread(ctx->script_buf, sizeof(char), buf_len, fp) != buf_len) 574 goto eexit; 575 fclose(fp); 576 fp = NULL; 577 #endif 578 579 if (is_stdin) 580 return buf_pos; 581 582 if (script_buftoenv(ctx, ctx->script_buf, (size_t)buf_pos) == NULL) 583 goto eexit; 584 585 return buf_pos; 586 587 eexit: 588 logerr(__func__); 589 #ifndef HAVE_OPEN_MEMSTREAM 590 if (fp != NULL) 591 fclose(fp); 592 #endif 593 return -1; 594 } 595 596 static ssize_t 597 send_interface1(struct fd_list *fd, const struct interface *ifp, 598 const char *reason) 599 { 600 struct dhcpcd_ctx *ctx = ifp->ctx; 601 long len; 602 603 len = make_env(ifp->ctx, ifp, reason); 604 if (len == -1) 605 return -1; 606 return control_queue(fd, ctx->script_buf, (size_t)len); 607 } 608 609 int 610 send_interface(struct fd_list *fd, const struct interface *ifp, int af) 611 { 612 int retval = 0; 613 #ifdef INET 614 const struct dhcp_state *d; 615 #endif 616 #ifdef DHCP6 617 const struct dhcp6_state *d6; 618 #endif 619 620 if (af == AF_UNSPEC 621 #if defined(AF_LINK) 622 || af == AF_LINK 623 #elif defined(AF_PACKET) 624 || af == AF_PACKET 625 #endif 626 ) { 627 const char *reason; 628 629 switch (ifp->carrier) { 630 case LINK_UP: 631 reason = "CARRIER"; 632 break; 633 case LINK_DOWN: 634 reason = "NOCARRIER"; 635 break; 636 default: 637 reason = "UNKNOWN"; 638 break; 639 } 640 if (fd != NULL) { 641 if (send_interface1(fd, ifp, reason) == -1) 642 retval = -1; 643 } else 644 retval++; 645 } 646 647 #ifdef INET 648 if (af == AF_UNSPEC || af == AF_INET) { 649 if (D_STATE_RUNNING(ifp)) { 650 d = D_CSTATE(ifp); 651 if (fd != NULL) { 652 if (send_interface1(fd, ifp, d->reason) == -1) 653 retval = -1; 654 } else 655 retval++; 656 } 657 #ifdef IPV4LL 658 if (IPV4LL_STATE_RUNNING(ifp)) { 659 if (fd != NULL) { 660 if (send_interface1(fd, ifp, "IPV4LL") == -1) 661 retval = -1; 662 } else 663 retval++; 664 } 665 #endif 666 } 667 #endif 668 669 #ifdef INET6 670 if (af == AF_UNSPEC || af == AF_INET6) { 671 if (IPV6_STATE_RUNNING(ifp)) { 672 if (fd != NULL) { 673 if (send_interface1(fd, ifp, "STATIC6") == -1) 674 retval = -1; 675 } else 676 retval++; 677 } 678 if (RS_STATE_RUNNING(ifp)) { 679 if (fd != NULL) { 680 if (send_interface1(fd, ifp, "ROUTERADVERT") == 681 -1) 682 retval = -1; 683 } else 684 retval++; 685 } 686 #ifdef DHCP6 687 if (D6_STATE_RUNNING(ifp)) { 688 d6 = D6_CSTATE(ifp); 689 if (fd != NULL) { 690 if (send_interface1(fd, ifp, d6->reason) == -1) 691 retval = -1; 692 } else 693 retval++; 694 } 695 #endif 696 } 697 #endif 698 699 return retval; 700 } 701 702 static int 703 script_status(const char *script, int status) 704 { 705 if (WIFEXITED(status)) { 706 if (WEXITSTATUS(status)) 707 logerrx("%s: %s: WEXITSTATUS %d", __func__, script, 708 WEXITSTATUS(status)); 709 } else if (WIFSIGNALED(status)) 710 logerrx("%s: %s: %s", __func__, script, 711 strsignal(WTERMSIG(status))); 712 713 return WEXITSTATUS(status); 714 } 715 716 static int 717 script_run(struct dhcpcd_ctx *ctx, char **argv) 718 { 719 pid_t pid; 720 int status; 721 722 pid = script_exec(argv, ctx->script_env); 723 if (pid == -1) { 724 logerr("%s: %s", __func__, argv[0]); 725 return -1; 726 } else if (pid == 0) 727 return 0; 728 729 /* Wait for the script to finish */ 730 while (waitpid(pid, &status, 0) == -1) { 731 if (errno != EINTR) { 732 logerr("%s: waitpid", __func__); 733 status = 0; 734 break; 735 } 736 } 737 return script_status(argv[0], status); 738 } 739 740 int 741 script_dump(const char *env, size_t len) 742 { 743 const char *ep = env + len; 744 745 if (len == 0) 746 return 0; 747 748 if (*(ep - 1) != '\0') { 749 errno = EINVAL; 750 return -1; 751 } 752 753 for (; env < ep; env += strlen(env) + 1) { 754 if (strncmp(env, "new_", 4) == 0) 755 env += 4; 756 printf("%s\n", env); 757 } 758 fflush(stdout); 759 return 0; 760 } 761 762 int 763 script_runreason(const struct interface *ifp, const char *reason) 764 { 765 struct dhcpcd_ctx *ctx = ifp->ctx; 766 char *argv[2]; 767 int status = 0; 768 struct fd_list *fd; 769 long buflen; 770 771 if (ctx->script == NULL && TAILQ_FIRST(&ifp->ctx->control_fds) == NULL) 772 return 0; 773 774 /* Make our env */ 775 if ((buflen = make_env(ifp->ctx, ifp, reason)) == -1) { 776 logerr(__func__); 777 return -1; 778 } 779 780 if (strncmp(reason, "DUMP", 4) == 0) 781 return script_dump(ctx->script_buf, (size_t)buflen); 782 783 if (ctx->script == NULL) 784 goto send_listeners; 785 786 argv[0] = ctx->script; 787 argv[1] = NULL; 788 logdebugx("%s: executing: %s %s", ifp->name, argv[0], reason); 789 790 #ifdef PRIVSEP 791 if (IN_PRIVSEP(ctx)) { 792 ssize_t err; 793 794 err = ps_root_script(ctx, ctx->script_buf, (size_t)buflen); 795 if (err == -1) 796 logerr(__func__); 797 else 798 script_status(ctx->script, (int)err); 799 goto send_listeners; 800 } 801 #endif 802 803 script_run(ctx, argv); 804 805 send_listeners: 806 /* Send to our listeners */ 807 status = 0; 808 TAILQ_FOREACH(fd, &ctx->control_fds, next) { 809 if (!(fd->flags & FD_LISTEN)) 810 continue; 811 if (control_queue(fd, ctx->script_buf, (size_t)buflen) == -1) 812 logerr("%s: control_queue", __func__); 813 else 814 status = 1; 815 } 816 817 return status; 818 } 819