Simon Kelley | d1ced3a | 2018-01-01 22:18:03 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2018 Simon Kelley |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License as published by |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 19 | #ifdef HAVE_SCRIPT |
| 20 | |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 21 | /* This file has code to fork a helper process which receives data via a pipe |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 22 | shared with the main process and which is responsible for calling a script when |
| 23 | DHCP leases change. |
| 24 | |
| 25 | The helper process is forked before the main process drops root, so it retains root |
| 26 | privs to pass on to the script. For this reason it tries to be paranoid about |
| 27 | data received from the main process, in case that has been compromised. We don't |
| 28 | want the helper to give an attacker root. In particular, the script to be run is |
| 29 | not settable via the pipe, once the fork has taken place it is not alterable by the |
| 30 | main process. |
| 31 | */ |
| 32 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 33 | static void my_setenv(const char *name, const char *value, int *error); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 34 | static unsigned char *grab_extradata(unsigned char *buf, unsigned char *end, char *env, int *err); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 35 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 36 | #ifdef HAVE_LUASCRIPT |
Simon Kelley | 289a253 | 2012-09-20 15:29:35 +0100 | [diff] [blame] | 37 | #define LUA_COMPAT_ALL |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 38 | #include <lua.h> |
| 39 | #include <lualib.h> |
| 40 | #include <lauxlib.h> |
| 41 | |
Simon Kelley | 289a253 | 2012-09-20 15:29:35 +0100 | [diff] [blame] | 42 | #ifndef lua_open |
| 43 | #define lua_open() luaL_newstate() |
| 44 | #endif |
| 45 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 46 | lua_State *lua; |
| 47 | |
| 48 | static unsigned char *grab_extradata_lua(unsigned char *buf, unsigned char *end, char *field); |
| 49 | #endif |
| 50 | |
| 51 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 52 | struct script_data |
| 53 | { |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 54 | int flags; |
| 55 | int action, hwaddr_len, hwaddr_type; |
| 56 | int clid_len, hostname_len, ed_len; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 57 | struct in_addr addr, giaddr; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 58 | unsigned int remaining_time; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 59 | #ifdef HAVE_BROKEN_RTC |
| 60 | unsigned int length; |
| 61 | #else |
| 62 | time_t expires; |
| 63 | #endif |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 64 | #ifdef HAVE_TFTP |
| 65 | off_t file_len; |
| 66 | #endif |
Simon Kelley | 903650a | 2013-10-03 11:43:09 +0100 | [diff] [blame] | 67 | #ifdef HAVE_IPV6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 68 | struct in6_addr addr6; |
Simon Kelley | 903650a | 2013-10-03 11:43:09 +0100 | [diff] [blame] | 69 | #endif |
| 70 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 71 | int iaid, vendorclass_count; |
| 72 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 73 | unsigned char hwaddr[DHCP_CHADDR_MAX]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 74 | char interface[IF_NAMESIZE]; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 77 | static struct script_data *buf = NULL; |
| 78 | static size_t bytes_in_buf = 0, buf_size = 0; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 79 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 80 | int create_helper(int event_fd, int err_fd, uid_t uid, gid_t gid, long max_fd) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 81 | { |
| 82 | pid_t pid; |
| 83 | int i, pipefd[2]; |
| 84 | struct sigaction sigact; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 85 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 86 | /* create the pipe through which the main program sends us commands, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 87 | then fork our process. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 88 | if (pipe(pipefd) == -1 || !fix_fd(pipefd[1]) || (pid = fork()) == -1) |
| 89 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 90 | send_event(err_fd, EVENT_PIPE_ERR, errno, NULL); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 91 | _exit(0); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 94 | if (pid != 0) |
| 95 | { |
| 96 | close(pipefd[0]); /* close reader side */ |
| 97 | return pipefd[1]; |
| 98 | } |
| 99 | |
Simon Kelley | 3c973ad | 2018-01-14 21:05:37 +0000 | [diff] [blame] | 100 | /* ignore SIGTERM and SIGINT, so that we can clean up when the main process gets hit |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 101 | and SIGALRM so that we can use sleep() */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 102 | sigact.sa_handler = SIG_IGN; |
| 103 | sigact.sa_flags = 0; |
| 104 | sigemptyset(&sigact.sa_mask); |
| 105 | sigaction(SIGTERM, &sigact, NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 106 | sigaction(SIGALRM, &sigact, NULL); |
Simon Kelley | 3c973ad | 2018-01-14 21:05:37 +0000 | [diff] [blame] | 107 | sigaction(SIGINT, &sigact, NULL); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 108 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 109 | if (!option_bool(OPT_DEBUG) && uid != 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 110 | { |
| 111 | gid_t dummy; |
| 112 | if (setgroups(0, &dummy) == -1 || |
| 113 | setgid(gid) == -1 || |
| 114 | setuid(uid) == -1) |
| 115 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 116 | if (option_bool(OPT_NO_FORK)) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 117 | /* send error to daemon process if no-fork */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 118 | send_event(event_fd, EVENT_USER_ERR, errno, daemon->scriptuser); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 119 | else |
| 120 | { |
| 121 | /* kill daemon */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 122 | send_event(event_fd, EVENT_DIE, 0, NULL); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 123 | /* return error */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 124 | send_event(err_fd, EVENT_USER_ERR, errno, daemon->scriptuser); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 125 | } |
| 126 | _exit(0); |
| 127 | } |
| 128 | } |
| 129 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 130 | /* close all the sockets etc, we don't need them here. |
| 131 | Don't close err_fd, in case the lua-init fails. |
| 132 | Note that we have to do this before lua init |
| 133 | so we don't close any lua fds. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 134 | for (max_fd--; max_fd >= 0; max_fd--) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 135 | if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO && |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 136 | max_fd != STDIN_FILENO && max_fd != pipefd[0] && |
| 137 | max_fd != event_fd && max_fd != err_fd) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 138 | close(max_fd); |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 139 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 140 | #ifdef HAVE_LUASCRIPT |
| 141 | if (daemon->luascript) |
| 142 | { |
| 143 | const char *lua_err = NULL; |
| 144 | lua = lua_open(); |
| 145 | luaL_openlibs(lua); |
| 146 | |
| 147 | /* get Lua to load our script file */ |
| 148 | if (luaL_dofile(lua, daemon->luascript) != 0) |
| 149 | lua_err = lua_tostring(lua, -1); |
| 150 | else |
| 151 | { |
| 152 | lua_getglobal(lua, "lease"); |
| 153 | if (lua_type(lua, -1) != LUA_TFUNCTION) |
| 154 | lua_err = _("lease() function missing in Lua script"); |
| 155 | } |
| 156 | |
| 157 | if (lua_err) |
| 158 | { |
| 159 | if (option_bool(OPT_NO_FORK) || option_bool(OPT_DEBUG)) |
| 160 | /* send error to daemon process if no-fork */ |
| 161 | send_event(event_fd, EVENT_LUA_ERR, 0, (char *)lua_err); |
| 162 | else |
| 163 | { |
| 164 | /* kill daemon */ |
| 165 | send_event(event_fd, EVENT_DIE, 0, NULL); |
| 166 | /* return error */ |
| 167 | send_event(err_fd, EVENT_LUA_ERR, 0, (char *)lua_err); |
| 168 | } |
| 169 | _exit(0); |
| 170 | } |
| 171 | |
| 172 | lua_pop(lua, 1); /* remove nil from stack */ |
| 173 | lua_getglobal(lua, "init"); |
| 174 | if (lua_type(lua, -1) == LUA_TFUNCTION) |
| 175 | lua_call(lua, 0, 0); |
| 176 | else |
| 177 | lua_pop(lua, 1); /* remove nil from stack */ |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | /* All init done, close our copy of the error pipe, so that main process can return */ |
| 182 | if (err_fd != -1) |
| 183 | close(err_fd); |
| 184 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 185 | /* loop here */ |
| 186 | while(1) |
| 187 | { |
| 188 | struct script_data data; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 189 | char *p, *action_str, *hostname = NULL, *domain = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 190 | unsigned char *buf = (unsigned char *)daemon->namebuff; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 191 | unsigned char *end, *extradata, *alloc_buff = NULL; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 192 | int is6, err = 0; |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 193 | int pipeout[2]; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 194 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 195 | free(alloc_buff); |
| 196 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 197 | /* we read zero bytes when pipe closed: this is our signal to exit */ |
| 198 | if (!read_write(pipefd[0], (unsigned char *)&data, sizeof(data), 1)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 199 | { |
| 200 | #ifdef HAVE_LUASCRIPT |
| 201 | if (daemon->luascript) |
| 202 | { |
| 203 | lua_getglobal(lua, "shutdown"); |
| 204 | if (lua_type(lua, -1) == LUA_TFUNCTION) |
| 205 | lua_call(lua, 0, 0); |
| 206 | } |
| 207 | #endif |
| 208 | _exit(0); |
| 209 | } |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 210 | |
| 211 | is6 = !!(data.flags & (LEASE_TA | LEASE_NA)); |
| 212 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 213 | if (data.action == ACTION_DEL) |
| 214 | action_str = "del"; |
| 215 | else if (data.action == ACTION_ADD) |
| 216 | action_str = "add"; |
| 217 | else if (data.action == ACTION_OLD || data.action == ACTION_OLD_HOSTNAME) |
| 218 | action_str = "old"; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 219 | else if (data.action == ACTION_TFTP) |
| 220 | { |
| 221 | action_str = "tftp"; |
| 222 | is6 = (data.flags != AF_INET); |
| 223 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 224 | else if (data.action == ACTION_ARP) |
| 225 | { |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 226 | action_str = "arp-add"; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 227 | is6 = (data.flags != AF_INET); |
| 228 | } |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 229 | else if (data.action == ACTION_ARP_DEL) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 230 | { |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 231 | action_str = "arp-del"; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 232 | is6 = (data.flags != AF_INET); |
| 233 | data.action = ACTION_ARP; |
| 234 | } |
| 235 | else |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 236 | continue; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 237 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 238 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 239 | /* stringify MAC into dhcp_buff */ |
| 240 | p = daemon->dhcp_buff; |
| 241 | if (data.hwaddr_type != ARPHRD_ETHER || data.hwaddr_len == 0) |
| 242 | p += sprintf(p, "%.2x-", data.hwaddr_type); |
| 243 | for (i = 0; (i < data.hwaddr_len) && (i < DHCP_CHADDR_MAX); i++) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 244 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 245 | p += sprintf(p, "%.2x", data.hwaddr[i]); |
| 246 | if (i != data.hwaddr_len - 1) |
| 247 | p += sprintf(p, ":"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 248 | } |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 249 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 250 | /* supplied data may just exceed normal buffer (unlikely) */ |
| 251 | if ((data.hostname_len + data.ed_len + data.clid_len) > MAXDNAME && |
| 252 | !(alloc_buff = buf = malloc(data.hostname_len + data.ed_len + data.clid_len))) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 253 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 254 | |
| 255 | if (!read_write(pipefd[0], buf, |
| 256 | data.hostname_len + data.ed_len + data.clid_len, 1)) |
| 257 | continue; |
| 258 | |
| 259 | /* CLID into packet */ |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 260 | for (p = daemon->packet, i = 0; i < data.clid_len; i++) |
| 261 | { |
| 262 | p += sprintf(p, "%.2x", buf[i]); |
| 263 | if (i != data.clid_len - 1) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 264 | p += sprintf(p, ":"); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 265 | } |
| 266 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 267 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 268 | if (is6) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 269 | { |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 270 | /* or IAID and server DUID for IPv6 */ |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 271 | sprintf(daemon->dhcp_buff3, "%s%u", data.flags & LEASE_TA ? "T" : "", data.iaid); |
| 272 | for (p = daemon->dhcp_packet.iov_base, i = 0; i < daemon->duid_len; i++) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 273 | { |
| 274 | p += sprintf(p, "%.2x", daemon->duid[i]); |
| 275 | if (i != daemon->duid_len - 1) |
| 276 | p += sprintf(p, ":"); |
| 277 | } |
Simon Kelley | caa9438 | 2012-02-15 10:29:50 +0000 | [diff] [blame] | 278 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 279 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 280 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 281 | |
| 282 | buf += data.clid_len; |
| 283 | |
| 284 | if (data.hostname_len != 0) |
| 285 | { |
| 286 | char *dot; |
| 287 | hostname = (char *)buf; |
| 288 | hostname[data.hostname_len - 1] = 0; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 289 | if (data.action != ACTION_TFTP) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 290 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 291 | if (!legal_hostname(hostname)) |
| 292 | hostname = NULL; |
| 293 | else if ((dot = strchr(hostname, '.'))) |
| 294 | { |
| 295 | domain = dot+1; |
| 296 | *dot = 0; |
| 297 | } |
| 298 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | extradata = buf + data.hostname_len; |
| 302 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 303 | if (!is6) |
| 304 | inet_ntop(AF_INET, &data.addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 1566bac | 2016-02-05 14:38:06 +0000 | [diff] [blame] | 305 | #ifdef HAVE_IPV6 |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 306 | else |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 307 | inet_ntop(AF_INET6, &data.addr6, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 308 | #endif |
| 309 | |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 310 | #ifdef HAVE_TFTP |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 311 | /* file length */ |
| 312 | if (data.action == ACTION_TFTP) |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 313 | sprintf(is6 ? daemon->packet : daemon->dhcp_buff, "%lu", (unsigned long)data.file_len); |
| 314 | #endif |
| 315 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 316 | #ifdef HAVE_LUASCRIPT |
| 317 | if (daemon->luascript) |
| 318 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 319 | if (data.action == ACTION_TFTP) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 320 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 321 | lua_getglobal(lua, "tftp"); |
| 322 | if (lua_type(lua, -1) != LUA_TFUNCTION) |
| 323 | lua_pop(lua, 1); /* tftp function optional */ |
| 324 | else |
| 325 | { |
| 326 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 327 | lua_newtable(lua); /* arg2 - data table */ |
| 328 | lua_pushstring(lua, daemon->addrbuff); |
| 329 | lua_setfield(lua, -2, "destination_address"); |
| 330 | lua_pushstring(lua, hostname); |
| 331 | lua_setfield(lua, -2, "file_name"); |
Simon Kelley | b5d9a36 | 2013-09-24 09:44:33 +0100 | [diff] [blame] | 332 | lua_pushstring(lua, is6 ? daemon->packet : daemon->dhcp_buff); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 333 | lua_setfield(lua, -2, "file_size"); |
| 334 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
| 335 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 336 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 337 | else if (data.action == ACTION_ARP) |
| 338 | { |
| 339 | lua_getglobal(lua, "arp"); |
| 340 | if (lua_type(lua, -1) != LUA_TFUNCTION) |
| 341 | lua_pop(lua, 1); /* arp function optional */ |
| 342 | else |
| 343 | { |
| 344 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 345 | lua_newtable(lua); /* arg2 - data table */ |
| 346 | lua_pushstring(lua, daemon->addrbuff); |
| 347 | lua_setfield(lua, -2, "client_address"); |
| 348 | lua_pushstring(lua, daemon->dhcp_buff); |
| 349 | lua_setfield(lua, -2, "mac_address"); |
| 350 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
| 351 | } |
| 352 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 353 | else |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 354 | { |
| 355 | lua_getglobal(lua, "lease"); /* function to call */ |
| 356 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 357 | lua_newtable(lua); /* arg2 - data table */ |
| 358 | |
| 359 | if (is6) |
| 360 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 361 | lua_pushstring(lua, daemon->packet); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 362 | lua_setfield(lua, -2, "client_duid"); |
| 363 | lua_pushstring(lua, daemon->dhcp_packet.iov_base); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 364 | lua_setfield(lua, -2, "server_duid"); |
| 365 | lua_pushstring(lua, daemon->dhcp_buff3); |
| 366 | lua_setfield(lua, -2, "iaid"); |
| 367 | } |
| 368 | |
| 369 | if (!is6 && data.clid_len != 0) |
| 370 | { |
| 371 | lua_pushstring(lua, daemon->packet); |
| 372 | lua_setfield(lua, -2, "client_id"); |
| 373 | } |
| 374 | |
| 375 | if (strlen(data.interface) != 0) |
| 376 | { |
| 377 | lua_pushstring(lua, data.interface); |
| 378 | lua_setfield(lua, -2, "interface"); |
| 379 | } |
| 380 | |
| 381 | #ifdef HAVE_BROKEN_RTC |
| 382 | lua_pushnumber(lua, data.length); |
| 383 | lua_setfield(lua, -2, "lease_length"); |
| 384 | #else |
| 385 | lua_pushnumber(lua, data.expires); |
| 386 | lua_setfield(lua, -2, "lease_expires"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 387 | #endif |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 388 | |
| 389 | if (hostname) |
| 390 | { |
| 391 | lua_pushstring(lua, hostname); |
| 392 | lua_setfield(lua, -2, "hostname"); |
| 393 | } |
| 394 | |
| 395 | if (domain) |
| 396 | { |
| 397 | lua_pushstring(lua, domain); |
| 398 | lua_setfield(lua, -2, "domain"); |
| 399 | } |
| 400 | |
| 401 | end = extradata + data.ed_len; |
| 402 | buf = extradata; |
| 403 | |
| 404 | if (!is6) |
| 405 | buf = grab_extradata_lua(buf, end, "vendor_class"); |
| 406 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 407 | else if (data.vendorclass_count != 0) |
| 408 | { |
| 409 | sprintf(daemon->dhcp_buff2, "vendor_class_id"); |
| 410 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 411 | for (i = 0; i < data.vendorclass_count - 1; i++) |
| 412 | { |
| 413 | sprintf(daemon->dhcp_buff2, "vendor_class%i", i); |
| 414 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 415 | } |
| 416 | } |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 417 | #endif |
| 418 | |
| 419 | buf = grab_extradata_lua(buf, end, "supplied_hostname"); |
| 420 | |
| 421 | if (!is6) |
| 422 | { |
| 423 | buf = grab_extradata_lua(buf, end, "cpewan_oui"); |
| 424 | buf = grab_extradata_lua(buf, end, "cpewan_serial"); |
| 425 | buf = grab_extradata_lua(buf, end, "cpewan_class"); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 426 | buf = grab_extradata_lua(buf, end, "circuit_id"); |
| 427 | buf = grab_extradata_lua(buf, end, "subscriber_id"); |
| 428 | buf = grab_extradata_lua(buf, end, "remote_id"); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | buf = grab_extradata_lua(buf, end, "tags"); |
| 432 | |
| 433 | if (is6) |
| 434 | buf = grab_extradata_lua(buf, end, "relay_address"); |
| 435 | else if (data.giaddr.s_addr != 0) |
| 436 | { |
| 437 | lua_pushstring(lua, inet_ntoa(data.giaddr)); |
| 438 | lua_setfield(lua, -2, "relay_address"); |
| 439 | } |
| 440 | |
| 441 | for (i = 0; buf; i++) |
| 442 | { |
| 443 | sprintf(daemon->dhcp_buff2, "user_class%i", i); |
| 444 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 445 | } |
| 446 | |
| 447 | if (data.action != ACTION_DEL && data.remaining_time != 0) |
| 448 | { |
| 449 | lua_pushnumber(lua, data.remaining_time); |
| 450 | lua_setfield(lua, -2, "time_remaining"); |
| 451 | } |
| 452 | |
| 453 | if (data.action == ACTION_OLD_HOSTNAME && hostname) |
| 454 | { |
| 455 | lua_pushstring(lua, hostname); |
| 456 | lua_setfield(lua, -2, "old_hostname"); |
| 457 | } |
| 458 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 459 | if (!is6 || data.hwaddr_len != 0) |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 460 | { |
| 461 | lua_pushstring(lua, daemon->dhcp_buff); |
| 462 | lua_setfield(lua, -2, "mac_address"); |
| 463 | } |
| 464 | |
| 465 | lua_pushstring(lua, daemon->addrbuff); |
| 466 | lua_setfield(lua, -2, "ip_address"); |
| 467 | |
| 468 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 469 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 470 | } |
| 471 | #endif |
| 472 | |
| 473 | /* no script, just lua */ |
| 474 | if (!daemon->lease_change_command) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 475 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 476 | |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 477 | /* Pipe to capture stdout and stderr from script */ |
| 478 | if (!option_bool(OPT_DEBUG) && pipe(pipeout) == -1) |
| 479 | continue; |
| 480 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 481 | /* possible fork errors are all temporary resource problems */ |
| 482 | while ((pid = fork()) == -1 && (errno == EAGAIN || errno == ENOMEM)) |
| 483 | sleep(2); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 484 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 485 | if (pid == -1) |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 486 | { |
| 487 | if (!option_bool(OPT_DEBUG)) |
| 488 | { |
| 489 | close(pipeout[0]); |
| 490 | close(pipeout[1]); |
| 491 | } |
| 492 | continue; |
| 493 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 494 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 495 | /* wait for child to complete */ |
| 496 | if (pid != 0) |
| 497 | { |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 498 | if (!option_bool(OPT_DEBUG)) |
| 499 | { |
| 500 | FILE *fp; |
| 501 | |
| 502 | close(pipeout[1]); |
| 503 | |
| 504 | /* Read lines sent to stdout/err by the script and pass them back to be logged */ |
| 505 | if (!(fp = fdopen(pipeout[0], "r"))) |
| 506 | close(pipeout[0]); |
| 507 | else |
| 508 | { |
| 509 | while (fgets(daemon->packet, daemon->packet_buff_sz, fp)) |
| 510 | { |
| 511 | /* do not include new lines, log will append them */ |
| 512 | size_t len = strlen(daemon->packet); |
| 513 | if (len > 0) |
| 514 | { |
| 515 | --len; |
| 516 | if (daemon->packet[len] == '\n') |
| 517 | daemon->packet[len] = 0; |
| 518 | } |
| 519 | send_event(event_fd, EVENT_SCRIPT_LOG, 0, daemon->packet); |
| 520 | } |
| 521 | fclose(fp); |
| 522 | } |
| 523 | } |
| 524 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 525 | /* reap our children's children, if necessary */ |
| 526 | while (1) |
| 527 | { |
| 528 | int status; |
| 529 | pid_t rc = wait(&status); |
| 530 | |
| 531 | if (rc == pid) |
| 532 | { |
| 533 | /* On error send event back to main process for logging */ |
| 534 | if (WIFSIGNALED(status)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 535 | send_event(event_fd, EVENT_KILLED, WTERMSIG(status), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 536 | else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 537 | send_event(event_fd, EVENT_EXITED, WEXITSTATUS(status), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | |
| 541 | if (rc == -1 && errno != EINTR) |
| 542 | break; |
| 543 | } |
| 544 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 545 | continue; |
| 546 | } |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 547 | |
| 548 | if (!option_bool(OPT_DEBUG)) |
| 549 | { |
| 550 | /* map stdout/stderr of script to pipeout */ |
| 551 | close(pipeout[0]); |
| 552 | dup2(pipeout[1], STDOUT_FILENO); |
| 553 | dup2(pipeout[1], STDERR_FILENO); |
| 554 | close(pipeout[1]); |
| 555 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 556 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 557 | if (data.action != ACTION_TFTP && data.action != ACTION_ARP) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 558 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 559 | #ifdef HAVE_DHCP6 |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 560 | my_setenv("DNSMASQ_IAID", is6 ? daemon->dhcp_buff3 : NULL, &err); |
| 561 | my_setenv("DNSMASQ_SERVER_DUID", is6 ? daemon->dhcp_packet.iov_base : NULL, &err); |
| 562 | my_setenv("DNSMASQ_MAC", is6 && data.hwaddr_len != 0 ? daemon->dhcp_buff : NULL, &err); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 563 | #endif |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 564 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 565 | my_setenv("DNSMASQ_CLIENT_ID", !is6 && data.clid_len != 0 ? daemon->packet : NULL, &err); |
| 566 | my_setenv("DNSMASQ_INTERFACE", strlen(data.interface) != 0 ? data.interface : NULL, &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 567 | |
| 568 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 208fb61 | 2013-02-21 22:26:18 +0000 | [diff] [blame] | 569 | sprintf(daemon->dhcp_buff2, "%u", data.length); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 570 | my_setenv("DNSMASQ_LEASE_LENGTH", daemon->dhcp_buff2, &err); |
| 571 | #else |
Simon Kelley | 208fb61 | 2013-02-21 22:26:18 +0000 | [diff] [blame] | 572 | sprintf(daemon->dhcp_buff2, "%lu", (unsigned long)data.expires); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 573 | my_setenv("DNSMASQ_LEASE_EXPIRES", daemon->dhcp_buff2, &err); |
| 574 | #endif |
| 575 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 576 | my_setenv("DNSMASQ_DOMAIN", domain, &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 577 | |
| 578 | end = extradata + data.ed_len; |
| 579 | buf = extradata; |
| 580 | |
| 581 | if (!is6) |
| 582 | buf = grab_extradata(buf, end, "DNSMASQ_VENDOR_CLASS", &err); |
| 583 | #ifdef HAVE_DHCP6 |
| 584 | else |
| 585 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 586 | if (data.vendorclass_count != 0) |
Simon Kelley | a5c72ab | 2012-02-10 13:42:47 +0000 | [diff] [blame] | 587 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 588 | buf = grab_extradata(buf, end, "DNSMASQ_VENDOR_CLASS_ID", &err); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 589 | for (i = 0; i < data.vendorclass_count - 1; i++) |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 590 | { |
| 591 | sprintf(daemon->dhcp_buff2, "DNSMASQ_VENDOR_CLASS%i", i); |
| 592 | buf = grab_extradata(buf, end, daemon->dhcp_buff2, &err); |
| 593 | } |
Simon Kelley | a5c72ab | 2012-02-10 13:42:47 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 596 | #endif |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 597 | |
| 598 | buf = grab_extradata(buf, end, "DNSMASQ_SUPPLIED_HOSTNAME", &err); |
| 599 | |
| 600 | if (!is6) |
| 601 | { |
| 602 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_OUI", &err); |
| 603 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_SERIAL", &err); |
| 604 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_CLASS", &err); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 605 | buf = grab_extradata(buf, end, "DNSMASQ_CIRCUIT_ID", &err); |
| 606 | buf = grab_extradata(buf, end, "DNSMASQ_SUBSCRIBER_ID", &err); |
| 607 | buf = grab_extradata(buf, end, "DNSMASQ_REMOTE_ID", &err); |
ZHAO Yu | f89cae3 | 2016-12-22 22:32:31 +0000 | [diff] [blame] | 608 | buf = grab_extradata(buf, end, "DNSMASQ_REQUESTED_OPTIONS", &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | buf = grab_extradata(buf, end, "DNSMASQ_TAGS", &err); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 612 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 613 | if (is6) |
| 614 | buf = grab_extradata(buf, end, "DNSMASQ_RELAY_ADDRESS", &err); |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 615 | else |
| 616 | my_setenv("DNSMASQ_RELAY_ADDRESS", data.giaddr.s_addr != 0 ? inet_ntoa(data.giaddr) : NULL, &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 617 | |
| 618 | for (i = 0; buf; i++) |
| 619 | { |
| 620 | sprintf(daemon->dhcp_buff2, "DNSMASQ_USER_CLASS%i", i); |
| 621 | buf = grab_extradata(buf, end, daemon->dhcp_buff2, &err); |
| 622 | } |
| 623 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 624 | sprintf(daemon->dhcp_buff2, "%u", data.remaining_time); |
| 625 | my_setenv("DNSMASQ_TIME_REMAINING", data.action != ACTION_DEL && data.remaining_time != 0 ? daemon->dhcp_buff2 : NULL, &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 626 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 627 | my_setenv("DNSMASQ_OLD_HOSTNAME", data.action == ACTION_OLD_HOSTNAME ? hostname : NULL, &err); |
| 628 | if (data.action == ACTION_OLD_HOSTNAME) |
| 629 | hostname = NULL; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 630 | |
| 631 | my_setenv("DNSMASQ_LOG_DHCP", option_bool(OPT_LOG_OPTS) ? "1" : NULL, &err); |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 632 | } |
| 633 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 634 | /* we need to have the event_fd around if exec fails */ |
| 635 | if ((i = fcntl(event_fd, F_GETFD)) != -1) |
| 636 | fcntl(event_fd, F_SETFD, i | FD_CLOEXEC); |
| 637 | close(pipefd[0]); |
| 638 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 639 | p = strrchr(daemon->lease_change_command, '/'); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 640 | if (err == 0) |
| 641 | { |
| 642 | execl(daemon->lease_change_command, |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 643 | p ? p+1 : daemon->lease_change_command, action_str, |
| 644 | (is6 && data.action != ACTION_ARP) ? daemon->packet : daemon->dhcp_buff, |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 645 | daemon->addrbuff, hostname, (char*)NULL); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 646 | err = errno; |
| 647 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 648 | /* failed, send event so the main process logs the problem */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 649 | send_event(event_fd, EVENT_EXEC_ERR, err, NULL); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 650 | _exit(0); |
| 651 | } |
| 652 | } |
| 653 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 654 | static void my_setenv(const char *name, const char *value, int *error) |
| 655 | { |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 656 | if (*error == 0) |
| 657 | { |
| 658 | if (!value) |
| 659 | unsetenv(name); |
| 660 | else if (setenv(name, value, 1) != 0) |
| 661 | *error = errno; |
| 662 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 663 | } |
| 664 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 665 | static unsigned char *grab_extradata(unsigned char *buf, unsigned char *end, char *env, int *err) |
| 666 | { |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 667 | unsigned char *next = NULL; |
| 668 | char *val = NULL; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 669 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 670 | if (buf && (buf != end)) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 671 | { |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 672 | for (next = buf; ; next++) |
| 673 | if (next == end) |
| 674 | { |
| 675 | next = NULL; |
| 676 | break; |
| 677 | } |
| 678 | else if (*next == 0) |
| 679 | break; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 680 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 681 | if (next && (next != buf)) |
| 682 | { |
| 683 | char *p; |
| 684 | /* No "=" in value */ |
| 685 | if ((p = strchr((char *)buf, '='))) |
| 686 | *p = 0; |
| 687 | val = (char *)buf; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | my_setenv(env, val, err); |
| 692 | |
| 693 | return next ? next + 1 : NULL; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 696 | #ifdef HAVE_LUASCRIPT |
| 697 | static unsigned char *grab_extradata_lua(unsigned char *buf, unsigned char *end, char *field) |
| 698 | { |
| 699 | unsigned char *next; |
| 700 | |
| 701 | if (!buf || (buf == end)) |
| 702 | return NULL; |
| 703 | |
| 704 | for (next = buf; *next != 0; next++) |
| 705 | if (next == end) |
| 706 | return NULL; |
| 707 | |
| 708 | if (next != buf) |
| 709 | { |
| 710 | lua_pushstring(lua, (char *)buf); |
| 711 | lua_setfield(lua, -2, field); |
| 712 | } |
| 713 | |
| 714 | return next + 1; |
| 715 | } |
| 716 | #endif |
| 717 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 718 | static void buff_alloc(size_t size) |
| 719 | { |
| 720 | if (size > buf_size) |
| 721 | { |
| 722 | struct script_data *new; |
| 723 | |
| 724 | /* start with reasonable size, will almost never need extending. */ |
| 725 | if (size < sizeof(struct script_data) + 200) |
| 726 | size = sizeof(struct script_data) + 200; |
| 727 | |
| 728 | if (!(new = whine_malloc(size))) |
| 729 | return; |
| 730 | if (buf) |
| 731 | free(buf); |
| 732 | buf = new; |
| 733 | buf_size = size; |
| 734 | } |
| 735 | } |
| 736 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 737 | /* pack up lease data into a buffer */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 738 | void queue_script(int action, struct dhcp_lease *lease, char *hostname, time_t now) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 739 | { |
| 740 | unsigned char *p; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 741 | unsigned int hostname_len = 0, clid_len = 0, ed_len = 0; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 742 | int fd = daemon->dhcpfd; |
Simon Kelley | 6f9aaa9 | 2013-04-10 10:25:26 +0100 | [diff] [blame] | 743 | #ifdef HAVE_DHCP6 |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 744 | if (!daemon->dhcp) |
| 745 | fd = daemon->dhcp6fd; |
| 746 | #endif |
| 747 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 748 | /* no script */ |
| 749 | if (daemon->helperfd == -1) |
| 750 | return; |
| 751 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 752 | if (lease->extradata) |
| 753 | ed_len = lease->extradata_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 754 | if (lease->clid) |
| 755 | clid_len = lease->clid_len; |
| 756 | if (hostname) |
| 757 | hostname_len = strlen(hostname) + 1; |
| 758 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 759 | buff_alloc(sizeof(struct script_data) + clid_len + ed_len + hostname_len); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 760 | |
| 761 | buf->action = action; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 762 | buf->flags = lease->flags; |
Simon Kelley | 6f9aaa9 | 2013-04-10 10:25:26 +0100 | [diff] [blame] | 763 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 764 | buf->vendorclass_count = lease->vendorclass_count; |
| 765 | buf->addr6 = lease->addr6; |
| 766 | buf->iaid = lease->iaid; |
Simon Kelley | 6f9aaa9 | 2013-04-10 10:25:26 +0100 | [diff] [blame] | 767 | #endif |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 768 | buf->hwaddr_len = lease->hwaddr_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 769 | buf->hwaddr_type = lease->hwaddr_type; |
| 770 | buf->clid_len = clid_len; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 771 | buf->ed_len = ed_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 772 | buf->hostname_len = hostname_len; |
| 773 | buf->addr = lease->addr; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 774 | buf->giaddr = lease->giaddr; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 775 | memcpy(buf->hwaddr, lease->hwaddr, DHCP_CHADDR_MAX); |
| 776 | if (!indextoname(fd, lease->last_interface, buf->interface)) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 777 | buf->interface[0] = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 778 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 779 | #ifdef HAVE_BROKEN_RTC |
| 780 | buf->length = lease->length; |
| 781 | #else |
| 782 | buf->expires = lease->expires; |
| 783 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 784 | |
| 785 | if (lease->expires != 0) |
| 786 | buf->remaining_time = (unsigned int)difftime(lease->expires, now); |
| 787 | else |
| 788 | buf->remaining_time = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 789 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 790 | p = (unsigned char *)(buf+1); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 791 | if (clid_len != 0) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 792 | { |
| 793 | memcpy(p, lease->clid, clid_len); |
| 794 | p += clid_len; |
| 795 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 796 | if (hostname_len != 0) |
| 797 | { |
| 798 | memcpy(p, hostname, hostname_len); |
| 799 | p += hostname_len; |
| 800 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 801 | if (ed_len != 0) |
| 802 | { |
| 803 | memcpy(p, lease->extradata, ed_len); |
| 804 | p += ed_len; |
| 805 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 806 | bytes_in_buf = p - (unsigned char *)buf; |
| 807 | } |
| 808 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 809 | #ifdef HAVE_TFTP |
| 810 | /* This nastily re-uses DHCP-fields for TFTP stuff */ |
| 811 | void queue_tftp(off_t file_len, char *filename, union mysockaddr *peer) |
| 812 | { |
| 813 | unsigned int filename_len; |
| 814 | |
| 815 | /* no script */ |
| 816 | if (daemon->helperfd == -1) |
| 817 | return; |
| 818 | |
| 819 | filename_len = strlen(filename) + 1; |
| 820 | buff_alloc(sizeof(struct script_data) + filename_len); |
| 821 | memset(buf, 0, sizeof(struct script_data)); |
| 822 | |
| 823 | buf->action = ACTION_TFTP; |
| 824 | buf->hostname_len = filename_len; |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 825 | buf->file_len = file_len; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 826 | |
| 827 | if ((buf->flags = peer->sa.sa_family) == AF_INET) |
| 828 | buf->addr = peer->in.sin_addr; |
| 829 | #ifdef HAVE_IPV6 |
| 830 | else |
Simon Kelley | b5d9a36 | 2013-09-24 09:44:33 +0100 | [diff] [blame] | 831 | buf->addr6 = peer->in6.sin6_addr; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 832 | #endif |
| 833 | |
| 834 | memcpy((unsigned char *)(buf+1), filename, filename_len); |
| 835 | |
| 836 | bytes_in_buf = sizeof(struct script_data) + filename_len; |
| 837 | } |
| 838 | #endif |
| 839 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 840 | void queue_arp(int action, unsigned char *mac, int maclen, int family, struct all_addr *addr) |
| 841 | { |
| 842 | /* no script */ |
| 843 | if (daemon->helperfd == -1) |
| 844 | return; |
| 845 | |
| 846 | buff_alloc(sizeof(struct script_data)); |
| 847 | memset(buf, 0, sizeof(struct script_data)); |
| 848 | |
| 849 | buf->action = action; |
| 850 | buf->hwaddr_len = maclen; |
| 851 | buf->hwaddr_type = ARPHRD_ETHER; |
| 852 | if ((buf->flags = family) == AF_INET) |
| 853 | buf->addr = addr->addr.addr4; |
| 854 | #ifdef HAVE_IPV6 |
| 855 | else |
| 856 | buf->addr6 = addr->addr.addr6; |
| 857 | #endif |
| 858 | |
| 859 | memcpy(buf->hwaddr, mac, maclen); |
| 860 | |
| 861 | bytes_in_buf = sizeof(struct script_data); |
| 862 | } |
| 863 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 864 | int helper_buf_empty(void) |
| 865 | { |
| 866 | return bytes_in_buf == 0; |
| 867 | } |
| 868 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 869 | void helper_write(void) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 870 | { |
| 871 | ssize_t rc; |
| 872 | |
| 873 | if (bytes_in_buf == 0) |
| 874 | return; |
| 875 | |
| 876 | if ((rc = write(daemon->helperfd, buf, bytes_in_buf)) != -1) |
| 877 | { |
| 878 | if (bytes_in_buf != (size_t)rc) |
| 879 | memmove(buf, buf + rc, bytes_in_buf - rc); |
| 880 | bytes_in_buf -= rc; |
| 881 | } |
| 882 | else |
| 883 | { |
| 884 | if (errno == EAGAIN || errno == EINTR) |
| 885 | return; |
| 886 | bytes_in_buf = 0; |
| 887 | } |
| 888 | } |
| 889 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 890 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 891 | |
| 892 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 893 | |