--- embedaddon/dnsmasq/src/helper.c 2016/11/02 09:57:01 1.1.1.3 +++ embedaddon/dnsmasq/src/helper.c 2021/03/17 00:56:46 1.1.1.4 @@ -1,4 +1,4 @@ -/* dnsmasq is Copyright (c) 2000-2016 Simon Kelley +/* dnsmasq is Copyright (c) 2000-2021 Simon Kelley This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -18,7 +18,7 @@ #ifdef HAVE_SCRIPT -/* This file has code to fork a helper process which recieves data via a pipe +/* This file has code to fork a helper process which receives data via a pipe shared with the main process and which is responsible for calling a script when DHCP leases change. @@ -64,11 +64,10 @@ struct script_data #ifdef HAVE_TFTP off_t file_len; #endif -#ifdef HAVE_IPV6 struct in6_addr addr6; -#endif #ifdef HAVE_DHCP6 - int iaid, vendorclass_count; + int vendorclass_count; + unsigned int iaid; #endif unsigned char hwaddr[DHCP_CHADDR_MAX]; char interface[IF_NAMESIZE]; @@ -82,7 +81,8 @@ int create_helper(int event_fd, int err_fd, uid_t uid, pid_t pid; int i, pipefd[2]; struct sigaction sigact; - + unsigned char *alloc_buff = NULL; + /* create the pipe through which the main program sends us commands, then fork our process. */ if (pipe(pipefd) == -1 || !fix_fd(pipefd[1]) || (pid = fork()) == -1) @@ -97,13 +97,14 @@ int create_helper(int event_fd, int err_fd, uid_t uid, return pipefd[1]; } - /* ignore SIGTERM, so that we can clean up when the main process gets hit + /* ignore SIGTERM and SIGINT, so that we can clean up when the main process gets hit and SIGALRM so that we can use sleep() */ sigact.sa_handler = SIG_IGN; sigact.sa_flags = 0; sigemptyset(&sigact.sa_mask); sigaction(SIGTERM, &sigact, NULL); sigaction(SIGALRM, &sigact, NULL); + sigaction(SIGINT, &sigact, NULL); if (!option_bool(OPT_DEBUG) && uid != 0) { @@ -130,11 +131,7 @@ int create_helper(int event_fd, int err_fd, uid_t uid, Don't close err_fd, in case the lua-init fails. Note that we have to do this before lua init so we don't close any lua fds. */ - for (max_fd--; max_fd >= 0; max_fd--) - if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO && - max_fd != STDIN_FILENO && max_fd != pipefd[0] && - max_fd != event_fd && max_fd != err_fd) - close(max_fd); + close_fds(max_fd, pipefd[0], event_fd, err_fd); #ifdef HAVE_LUASCRIPT if (daemon->luascript) @@ -187,10 +184,16 @@ int create_helper(int event_fd, int err_fd, uid_t uid, struct script_data data; char *p, *action_str, *hostname = NULL, *domain = NULL; unsigned char *buf = (unsigned char *)daemon->namebuff; - unsigned char *end, *extradata, *alloc_buff = NULL; + unsigned char *end, *extradata; int is6, err = 0; + int pipeout[2]; - free(alloc_buff); + /* Free rarely-allocated memory from previous iteration. */ + if (alloc_buff) + { + free(alloc_buff); + alloc_buff = NULL; + } /* we read zero bytes when pipe closed: this is our signal to exit */ if (!read_write(pipefd[0], (unsigned char *)&data, sizeof(data), 1)) @@ -300,10 +303,8 @@ int create_helper(int event_fd, int err_fd, uid_t uid, if (!is6) inet_ntop(AF_INET, &data.addr, daemon->addrbuff, ADDRSTRLEN); -#ifdef HAVE_IPV6 else inet_ntop(AF_INET6, &data.addr6, daemon->addrbuff, ADDRSTRLEN); -#endif #ifdef HAVE_TFTP /* file length */ @@ -472,16 +473,54 @@ int create_helper(int event_fd, int err_fd, uid_t uid, if (!daemon->lease_change_command) continue; + /* Pipe to capture stdout and stderr from script */ + if (!option_bool(OPT_DEBUG) && pipe(pipeout) == -1) + continue; + /* possible fork errors are all temporary resource problems */ while ((pid = fork()) == -1 && (errno == EAGAIN || errno == ENOMEM)) sleep(2); if (pid == -1) - continue; + { + if (!option_bool(OPT_DEBUG)) + { + close(pipeout[0]); + close(pipeout[1]); + } + continue; + } /* wait for child to complete */ if (pid != 0) { + if (!option_bool(OPT_DEBUG)) + { + FILE *fp; + + close(pipeout[1]); + + /* Read lines sent to stdout/err by the script and pass them back to be logged */ + if (!(fp = fdopen(pipeout[0], "r"))) + close(pipeout[0]); + else + { + while (fgets(daemon->packet, daemon->packet_buff_sz, fp)) + { + /* do not include new lines, log will append them */ + size_t len = strlen(daemon->packet); + if (len > 0) + { + --len; + if (daemon->packet[len] == '\n') + daemon->packet[len] = 0; + } + send_event(event_fd, EVENT_SCRIPT_LOG, 0, daemon->packet); + } + fclose(fp); + } + } + /* reap our children's children, if necessary */ while (1) { @@ -504,6 +543,15 @@ int create_helper(int event_fd, int err_fd, uid_t uid, continue; } + + if (!option_bool(OPT_DEBUG)) + { + /* map stdout/stderr of script to pipeout */ + close(pipeout[0]); + dup2(pipeout[1], STDOUT_FILENO); + dup2(pipeout[1], STDERR_FILENO); + close(pipeout[1]); + } if (data.action != ACTION_TFTP && data.action != ACTION_ARP) { @@ -556,6 +604,7 @@ int create_helper(int event_fd, int err_fd, uid_t uid, buf = grab_extradata(buf, end, "DNSMASQ_CIRCUIT_ID", &err); buf = grab_extradata(buf, end, "DNSMASQ_SUBSCRIBER_ID", &err); buf = grab_extradata(buf, end, "DNSMASQ_REMOTE_ID", &err); + buf = grab_extradata(buf, end, "DNSMASQ_REQUESTED_OPTIONS", &err); } buf = grab_extradata(buf, end, "DNSMASQ_TAGS", &err); @@ -579,7 +628,8 @@ int create_helper(int event_fd, int err_fd, uid_t uid, hostname = NULL; my_setenv("DNSMASQ_LOG_DHCP", option_bool(OPT_LOG_OPTS) ? "1" : NULL, &err); - } + } + /* we need to have the event_fd around if exec fails */ if ((i = fcntl(event_fd, F_GETFD)) != -1) fcntl(event_fd, F_SETFD, i | FD_CLOEXEC); @@ -775,10 +825,8 @@ void queue_tftp(off_t file_len, char *filename, union if ((buf->flags = peer->sa.sa_family) == AF_INET) buf->addr = peer->in.sin_addr; -#ifdef HAVE_IPV6 else buf->addr6 = peer->in6.sin6_addr; -#endif memcpy((unsigned char *)(buf+1), filename, filename_len); @@ -786,7 +834,7 @@ void queue_tftp(off_t file_len, char *filename, union } #endif -void queue_arp(int action, unsigned char *mac, int maclen, int family, struct all_addr *addr) +void queue_arp(int action, unsigned char *mac, int maclen, int family, union all_addr *addr) { /* no script */ if (daemon->helperfd == -1) @@ -799,11 +847,9 @@ void queue_arp(int action, unsigned char *mac, int mac buf->hwaddr_len = maclen; buf->hwaddr_type = ARPHRD_ETHER; if ((buf->flags = family) == AF_INET) - buf->addr = addr->addr.addr4; -#ifdef HAVE_IPV6 + buf->addr = addr->addr4; else - buf->addr6 = addr->addr.addr6; -#endif + buf->addr6 = addr->addr6; memcpy(buf->hwaddr, mac, maclen);