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 | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 67 | struct in6_addr addr6; |
Simon Kelley | 903650a | 2013-10-03 11:43:09 +0100 | [diff] [blame] | 68 | #ifdef HAVE_DHCP6 |
Dominik DL6ER | 456a319 | 2019-10-20 18:51:52 +0200 | [diff] [blame^] | 69 | int vendorclass_count; |
| 70 | unsigned int iaid; |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 71 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 72 | unsigned char hwaddr[DHCP_CHADDR_MAX]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 73 | char interface[IF_NAMESIZE]; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 76 | static struct script_data *buf = NULL; |
| 77 | static size_t bytes_in_buf = 0, buf_size = 0; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 78 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 79 | 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] | 80 | { |
| 81 | pid_t pid; |
| 82 | int i, pipefd[2]; |
| 83 | struct sigaction sigact; |
Simon Kelley | 69bc947 | 2019-08-14 20:44:50 +0100 | [diff] [blame] | 84 | unsigned char *alloc_buff = NULL; |
| 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 | 69bc947 | 2019-08-14 20:44:50 +0100 | [diff] [blame] | 191 | unsigned char *end, *extradata; |
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 | 69bc947 | 2019-08-14 20:44:50 +0100 | [diff] [blame] | 195 | /* Free rarely-allocated memory from previous iteration. */ |
| 196 | if (alloc_buff) |
| 197 | { |
| 198 | free(alloc_buff); |
| 199 | alloc_buff = NULL; |
| 200 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 201 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 202 | /* we read zero bytes when pipe closed: this is our signal to exit */ |
| 203 | if (!read_write(pipefd[0], (unsigned char *)&data, sizeof(data), 1)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 204 | { |
| 205 | #ifdef HAVE_LUASCRIPT |
| 206 | if (daemon->luascript) |
| 207 | { |
| 208 | lua_getglobal(lua, "shutdown"); |
| 209 | if (lua_type(lua, -1) == LUA_TFUNCTION) |
| 210 | lua_call(lua, 0, 0); |
| 211 | } |
| 212 | #endif |
| 213 | _exit(0); |
| 214 | } |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 215 | |
| 216 | is6 = !!(data.flags & (LEASE_TA | LEASE_NA)); |
| 217 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 218 | if (data.action == ACTION_DEL) |
| 219 | action_str = "del"; |
| 220 | else if (data.action == ACTION_ADD) |
| 221 | action_str = "add"; |
| 222 | else if (data.action == ACTION_OLD || data.action == ACTION_OLD_HOSTNAME) |
| 223 | action_str = "old"; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 224 | else if (data.action == ACTION_TFTP) |
| 225 | { |
| 226 | action_str = "tftp"; |
| 227 | is6 = (data.flags != AF_INET); |
| 228 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 229 | else if (data.action == ACTION_ARP) |
| 230 | { |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 231 | action_str = "arp-add"; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 232 | is6 = (data.flags != AF_INET); |
| 233 | } |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 234 | else if (data.action == ACTION_ARP_DEL) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 235 | { |
Simon Kelley | e6e751b | 2016-02-01 17:59:07 +0000 | [diff] [blame] | 236 | action_str = "arp-del"; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 237 | is6 = (data.flags != AF_INET); |
| 238 | data.action = ACTION_ARP; |
| 239 | } |
| 240 | else |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 241 | continue; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 242 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 243 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 244 | /* stringify MAC into dhcp_buff */ |
| 245 | p = daemon->dhcp_buff; |
| 246 | if (data.hwaddr_type != ARPHRD_ETHER || data.hwaddr_len == 0) |
| 247 | p += sprintf(p, "%.2x-", data.hwaddr_type); |
| 248 | for (i = 0; (i < data.hwaddr_len) && (i < DHCP_CHADDR_MAX); i++) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 249 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 250 | p += sprintf(p, "%.2x", data.hwaddr[i]); |
| 251 | if (i != data.hwaddr_len - 1) |
| 252 | p += sprintf(p, ":"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 253 | } |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 254 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 255 | /* supplied data may just exceed normal buffer (unlikely) */ |
| 256 | if ((data.hostname_len + data.ed_len + data.clid_len) > MAXDNAME && |
| 257 | !(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] | 258 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 259 | |
| 260 | if (!read_write(pipefd[0], buf, |
| 261 | data.hostname_len + data.ed_len + data.clid_len, 1)) |
| 262 | continue; |
| 263 | |
| 264 | /* CLID into packet */ |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 265 | for (p = daemon->packet, i = 0; i < data.clid_len; i++) |
| 266 | { |
| 267 | p += sprintf(p, "%.2x", buf[i]); |
| 268 | if (i != data.clid_len - 1) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 269 | p += sprintf(p, ":"); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 272 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 273 | if (is6) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 274 | { |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 275 | /* or IAID and server DUID for IPv6 */ |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 276 | sprintf(daemon->dhcp_buff3, "%s%u", data.flags & LEASE_TA ? "T" : "", data.iaid); |
| 277 | 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] | 278 | { |
| 279 | p += sprintf(p, "%.2x", daemon->duid[i]); |
| 280 | if (i != daemon->duid_len - 1) |
| 281 | p += sprintf(p, ":"); |
| 282 | } |
Simon Kelley | caa9438 | 2012-02-15 10:29:50 +0000 | [diff] [blame] | 283 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 284 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 285 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 286 | |
| 287 | buf += data.clid_len; |
| 288 | |
| 289 | if (data.hostname_len != 0) |
| 290 | { |
| 291 | char *dot; |
| 292 | hostname = (char *)buf; |
| 293 | hostname[data.hostname_len - 1] = 0; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 294 | if (data.action != ACTION_TFTP) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 295 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 296 | if (!legal_hostname(hostname)) |
| 297 | hostname = NULL; |
| 298 | else if ((dot = strchr(hostname, '.'))) |
| 299 | { |
| 300 | domain = dot+1; |
| 301 | *dot = 0; |
| 302 | } |
| 303 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | extradata = buf + data.hostname_len; |
| 307 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 308 | if (!is6) |
| 309 | inet_ntop(AF_INET, &data.addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 310 | else |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 311 | inet_ntop(AF_INET6, &data.addr6, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 312 | |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 313 | #ifdef HAVE_TFTP |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 314 | /* file length */ |
| 315 | if (data.action == ACTION_TFTP) |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 316 | sprintf(is6 ? daemon->packet : daemon->dhcp_buff, "%lu", (unsigned long)data.file_len); |
| 317 | #endif |
| 318 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 319 | #ifdef HAVE_LUASCRIPT |
| 320 | if (daemon->luascript) |
| 321 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 322 | if (data.action == ACTION_TFTP) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 323 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 324 | lua_getglobal(lua, "tftp"); |
| 325 | if (lua_type(lua, -1) != LUA_TFUNCTION) |
| 326 | lua_pop(lua, 1); /* tftp function optional */ |
| 327 | else |
| 328 | { |
| 329 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 330 | lua_newtable(lua); /* arg2 - data table */ |
| 331 | lua_pushstring(lua, daemon->addrbuff); |
| 332 | lua_setfield(lua, -2, "destination_address"); |
| 333 | lua_pushstring(lua, hostname); |
| 334 | lua_setfield(lua, -2, "file_name"); |
Simon Kelley | b5d9a36 | 2013-09-24 09:44:33 +0100 | [diff] [blame] | 335 | lua_pushstring(lua, is6 ? daemon->packet : daemon->dhcp_buff); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 336 | lua_setfield(lua, -2, "file_size"); |
| 337 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
| 338 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 339 | } |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 340 | else if (data.action == ACTION_ARP) |
| 341 | { |
| 342 | lua_getglobal(lua, "arp"); |
| 343 | if (lua_type(lua, -1) != LUA_TFUNCTION) |
| 344 | lua_pop(lua, 1); /* arp function optional */ |
| 345 | else |
| 346 | { |
| 347 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 348 | lua_newtable(lua); /* arg2 - data table */ |
| 349 | lua_pushstring(lua, daemon->addrbuff); |
| 350 | lua_setfield(lua, -2, "client_address"); |
| 351 | lua_pushstring(lua, daemon->dhcp_buff); |
| 352 | lua_setfield(lua, -2, "mac_address"); |
| 353 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
| 354 | } |
| 355 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 356 | else |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 357 | { |
| 358 | lua_getglobal(lua, "lease"); /* function to call */ |
| 359 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 360 | lua_newtable(lua); /* arg2 - data table */ |
| 361 | |
| 362 | if (is6) |
| 363 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 364 | lua_pushstring(lua, daemon->packet); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 365 | lua_setfield(lua, -2, "client_duid"); |
| 366 | lua_pushstring(lua, daemon->dhcp_packet.iov_base); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 367 | lua_setfield(lua, -2, "server_duid"); |
| 368 | lua_pushstring(lua, daemon->dhcp_buff3); |
| 369 | lua_setfield(lua, -2, "iaid"); |
| 370 | } |
| 371 | |
| 372 | if (!is6 && data.clid_len != 0) |
| 373 | { |
| 374 | lua_pushstring(lua, daemon->packet); |
| 375 | lua_setfield(lua, -2, "client_id"); |
| 376 | } |
| 377 | |
| 378 | if (strlen(data.interface) != 0) |
| 379 | { |
| 380 | lua_pushstring(lua, data.interface); |
| 381 | lua_setfield(lua, -2, "interface"); |
| 382 | } |
| 383 | |
| 384 | #ifdef HAVE_BROKEN_RTC |
| 385 | lua_pushnumber(lua, data.length); |
| 386 | lua_setfield(lua, -2, "lease_length"); |
| 387 | #else |
| 388 | lua_pushnumber(lua, data.expires); |
| 389 | lua_setfield(lua, -2, "lease_expires"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 390 | #endif |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 391 | |
| 392 | if (hostname) |
| 393 | { |
| 394 | lua_pushstring(lua, hostname); |
| 395 | lua_setfield(lua, -2, "hostname"); |
| 396 | } |
| 397 | |
| 398 | if (domain) |
| 399 | { |
| 400 | lua_pushstring(lua, domain); |
| 401 | lua_setfield(lua, -2, "domain"); |
| 402 | } |
| 403 | |
| 404 | end = extradata + data.ed_len; |
| 405 | buf = extradata; |
| 406 | |
| 407 | if (!is6) |
| 408 | buf = grab_extradata_lua(buf, end, "vendor_class"); |
| 409 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 410 | else if (data.vendorclass_count != 0) |
| 411 | { |
| 412 | sprintf(daemon->dhcp_buff2, "vendor_class_id"); |
| 413 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 414 | for (i = 0; i < data.vendorclass_count - 1; i++) |
| 415 | { |
| 416 | sprintf(daemon->dhcp_buff2, "vendor_class%i", i); |
| 417 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 418 | } |
| 419 | } |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 420 | #endif |
| 421 | |
| 422 | buf = grab_extradata_lua(buf, end, "supplied_hostname"); |
| 423 | |
| 424 | if (!is6) |
| 425 | { |
| 426 | buf = grab_extradata_lua(buf, end, "cpewan_oui"); |
| 427 | buf = grab_extradata_lua(buf, end, "cpewan_serial"); |
| 428 | buf = grab_extradata_lua(buf, end, "cpewan_class"); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 429 | buf = grab_extradata_lua(buf, end, "circuit_id"); |
| 430 | buf = grab_extradata_lua(buf, end, "subscriber_id"); |
| 431 | buf = grab_extradata_lua(buf, end, "remote_id"); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | buf = grab_extradata_lua(buf, end, "tags"); |
| 435 | |
| 436 | if (is6) |
| 437 | buf = grab_extradata_lua(buf, end, "relay_address"); |
| 438 | else if (data.giaddr.s_addr != 0) |
| 439 | { |
| 440 | lua_pushstring(lua, inet_ntoa(data.giaddr)); |
| 441 | lua_setfield(lua, -2, "relay_address"); |
| 442 | } |
| 443 | |
| 444 | for (i = 0; buf; i++) |
| 445 | { |
| 446 | sprintf(daemon->dhcp_buff2, "user_class%i", i); |
| 447 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 448 | } |
| 449 | |
| 450 | if (data.action != ACTION_DEL && data.remaining_time != 0) |
| 451 | { |
| 452 | lua_pushnumber(lua, data.remaining_time); |
| 453 | lua_setfield(lua, -2, "time_remaining"); |
| 454 | } |
| 455 | |
| 456 | if (data.action == ACTION_OLD_HOSTNAME && hostname) |
| 457 | { |
| 458 | lua_pushstring(lua, hostname); |
| 459 | lua_setfield(lua, -2, "old_hostname"); |
| 460 | } |
| 461 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 462 | if (!is6 || data.hwaddr_len != 0) |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 463 | { |
| 464 | lua_pushstring(lua, daemon->dhcp_buff); |
| 465 | lua_setfield(lua, -2, "mac_address"); |
| 466 | } |
| 467 | |
| 468 | lua_pushstring(lua, daemon->addrbuff); |
| 469 | lua_setfield(lua, -2, "ip_address"); |
| 470 | |
| 471 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 472 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 473 | } |
| 474 | #endif |
| 475 | |
| 476 | /* no script, just lua */ |
| 477 | if (!daemon->lease_change_command) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 478 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 479 | |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 480 | /* Pipe to capture stdout and stderr from script */ |
| 481 | if (!option_bool(OPT_DEBUG) && pipe(pipeout) == -1) |
| 482 | continue; |
| 483 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 484 | /* possible fork errors are all temporary resource problems */ |
| 485 | while ((pid = fork()) == -1 && (errno == EAGAIN || errno == ENOMEM)) |
| 486 | sleep(2); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 487 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 488 | if (pid == -1) |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 489 | { |
| 490 | if (!option_bool(OPT_DEBUG)) |
| 491 | { |
| 492 | close(pipeout[0]); |
| 493 | close(pipeout[1]); |
| 494 | } |
| 495 | continue; |
| 496 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 497 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 498 | /* wait for child to complete */ |
| 499 | if (pid != 0) |
| 500 | { |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 501 | if (!option_bool(OPT_DEBUG)) |
| 502 | { |
| 503 | FILE *fp; |
| 504 | |
| 505 | close(pipeout[1]); |
| 506 | |
| 507 | /* Read lines sent to stdout/err by the script and pass them back to be logged */ |
| 508 | if (!(fp = fdopen(pipeout[0], "r"))) |
| 509 | close(pipeout[0]); |
| 510 | else |
| 511 | { |
| 512 | while (fgets(daemon->packet, daemon->packet_buff_sz, fp)) |
| 513 | { |
| 514 | /* do not include new lines, log will append them */ |
| 515 | size_t len = strlen(daemon->packet); |
| 516 | if (len > 0) |
| 517 | { |
| 518 | --len; |
| 519 | if (daemon->packet[len] == '\n') |
| 520 | daemon->packet[len] = 0; |
| 521 | } |
| 522 | send_event(event_fd, EVENT_SCRIPT_LOG, 0, daemon->packet); |
| 523 | } |
| 524 | fclose(fp); |
| 525 | } |
| 526 | } |
| 527 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 528 | /* reap our children's children, if necessary */ |
| 529 | while (1) |
| 530 | { |
| 531 | int status; |
| 532 | pid_t rc = wait(&status); |
| 533 | |
| 534 | if (rc == pid) |
| 535 | { |
| 536 | /* On error send event back to main process for logging */ |
| 537 | if (WIFSIGNALED(status)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 538 | send_event(event_fd, EVENT_KILLED, WTERMSIG(status), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 539 | else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 540 | send_event(event_fd, EVENT_EXITED, WEXITSTATUS(status), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | |
| 544 | if (rc == -1 && errno != EINTR) |
| 545 | break; |
| 546 | } |
| 547 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 548 | continue; |
| 549 | } |
Petr MenÅ¡Ãk | c77fb9d | 2017-04-16 20:20:08 +0100 | [diff] [blame] | 550 | |
| 551 | if (!option_bool(OPT_DEBUG)) |
| 552 | { |
| 553 | /* map stdout/stderr of script to pipeout */ |
| 554 | close(pipeout[0]); |
| 555 | dup2(pipeout[1], STDOUT_FILENO); |
| 556 | dup2(pipeout[1], STDERR_FILENO); |
| 557 | close(pipeout[1]); |
| 558 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 559 | |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 560 | if (data.action != ACTION_TFTP && data.action != ACTION_ARP) |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 561 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 562 | #ifdef HAVE_DHCP6 |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 563 | my_setenv("DNSMASQ_IAID", is6 ? daemon->dhcp_buff3 : NULL, &err); |
| 564 | my_setenv("DNSMASQ_SERVER_DUID", is6 ? daemon->dhcp_packet.iov_base : NULL, &err); |
| 565 | 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] | 566 | #endif |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 567 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 568 | my_setenv("DNSMASQ_CLIENT_ID", !is6 && data.clid_len != 0 ? daemon->packet : NULL, &err); |
| 569 | my_setenv("DNSMASQ_INTERFACE", strlen(data.interface) != 0 ? data.interface : NULL, &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 570 | |
| 571 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 208fb61 | 2013-02-21 22:26:18 +0000 | [diff] [blame] | 572 | sprintf(daemon->dhcp_buff2, "%u", data.length); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 573 | my_setenv("DNSMASQ_LEASE_LENGTH", daemon->dhcp_buff2, &err); |
| 574 | #else |
Simon Kelley | 208fb61 | 2013-02-21 22:26:18 +0000 | [diff] [blame] | 575 | sprintf(daemon->dhcp_buff2, "%lu", (unsigned long)data.expires); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 576 | my_setenv("DNSMASQ_LEASE_EXPIRES", daemon->dhcp_buff2, &err); |
| 577 | #endif |
| 578 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 579 | my_setenv("DNSMASQ_DOMAIN", domain, &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 580 | |
| 581 | end = extradata + data.ed_len; |
| 582 | buf = extradata; |
| 583 | |
| 584 | if (!is6) |
| 585 | buf = grab_extradata(buf, end, "DNSMASQ_VENDOR_CLASS", &err); |
| 586 | #ifdef HAVE_DHCP6 |
| 587 | else |
| 588 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 589 | if (data.vendorclass_count != 0) |
Simon Kelley | a5c72ab | 2012-02-10 13:42:47 +0000 | [diff] [blame] | 590 | { |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 591 | buf = grab_extradata(buf, end, "DNSMASQ_VENDOR_CLASS_ID", &err); |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 592 | for (i = 0; i < data.vendorclass_count - 1; i++) |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 593 | { |
| 594 | sprintf(daemon->dhcp_buff2, "DNSMASQ_VENDOR_CLASS%i", i); |
| 595 | buf = grab_extradata(buf, end, daemon->dhcp_buff2, &err); |
| 596 | } |
Simon Kelley | a5c72ab | 2012-02-10 13:42:47 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 599 | #endif |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 600 | |
| 601 | buf = grab_extradata(buf, end, "DNSMASQ_SUPPLIED_HOSTNAME", &err); |
| 602 | |
| 603 | if (!is6) |
| 604 | { |
| 605 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_OUI", &err); |
| 606 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_SERIAL", &err); |
| 607 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_CLASS", &err); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 608 | buf = grab_extradata(buf, end, "DNSMASQ_CIRCUIT_ID", &err); |
| 609 | buf = grab_extradata(buf, end, "DNSMASQ_SUBSCRIBER_ID", &err); |
| 610 | buf = grab_extradata(buf, end, "DNSMASQ_REMOTE_ID", &err); |
ZHAO Yu | f89cae3 | 2016-12-22 22:32:31 +0000 | [diff] [blame] | 611 | buf = grab_extradata(buf, end, "DNSMASQ_REQUESTED_OPTIONS", &err); |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | buf = grab_extradata(buf, end, "DNSMASQ_TAGS", &err); |
Simon Kelley | dd1721c | 2013-02-18 21:04:04 +0000 | [diff] [blame] | 615 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 616 | if (is6) |
| 617 | buf = grab_extradata(buf, end, "DNSMASQ_RELAY_ADDRESS", &err); |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 618 | else |
| 619 | 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] | 620 | |
| 621 | for (i = 0; buf; i++) |
| 622 | { |
| 623 | sprintf(daemon->dhcp_buff2, "DNSMASQ_USER_CLASS%i", i); |
| 624 | buf = grab_extradata(buf, end, daemon->dhcp_buff2, &err); |
| 625 | } |
| 626 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 627 | sprintf(daemon->dhcp_buff2, "%u", data.remaining_time); |
| 628 | 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] | 629 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 630 | my_setenv("DNSMASQ_OLD_HOSTNAME", data.action == ACTION_OLD_HOSTNAME ? hostname : NULL, &err); |
| 631 | if (data.action == ACTION_OLD_HOSTNAME) |
| 632 | hostname = NULL; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 633 | |
| 634 | 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] | 635 | } |
| 636 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 637 | /* we need to have the event_fd around if exec fails */ |
| 638 | if ((i = fcntl(event_fd, F_GETFD)) != -1) |
| 639 | fcntl(event_fd, F_SETFD, i | FD_CLOEXEC); |
| 640 | close(pipefd[0]); |
| 641 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 642 | p = strrchr(daemon->lease_change_command, '/'); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 643 | if (err == 0) |
| 644 | { |
| 645 | execl(daemon->lease_change_command, |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 646 | p ? p+1 : daemon->lease_change_command, action_str, |
| 647 | (is6 && data.action != ACTION_ARP) ? daemon->packet : daemon->dhcp_buff, |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 648 | daemon->addrbuff, hostname, (char*)NULL); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 649 | err = errno; |
| 650 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 651 | /* failed, send event so the main process logs the problem */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 652 | send_event(event_fd, EVENT_EXEC_ERR, err, NULL); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 653 | _exit(0); |
| 654 | } |
| 655 | } |
| 656 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 657 | static void my_setenv(const char *name, const char *value, int *error) |
| 658 | { |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 659 | if (*error == 0) |
| 660 | { |
| 661 | if (!value) |
| 662 | unsetenv(name); |
| 663 | else if (setenv(name, value, 1) != 0) |
| 664 | *error = errno; |
| 665 | } |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 668 | static unsigned char *grab_extradata(unsigned char *buf, unsigned char *end, char *env, int *err) |
| 669 | { |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 670 | unsigned char *next = NULL; |
| 671 | char *val = NULL; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 672 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 673 | if (buf && (buf != end)) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 674 | { |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 675 | for (next = buf; ; next++) |
| 676 | if (next == end) |
| 677 | { |
| 678 | next = NULL; |
| 679 | break; |
| 680 | } |
| 681 | else if (*next == 0) |
| 682 | break; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 683 | |
Simon Kelley | d4da20f | 2013-10-04 10:12:49 +0100 | [diff] [blame] | 684 | if (next && (next != buf)) |
| 685 | { |
| 686 | char *p; |
| 687 | /* No "=" in value */ |
| 688 | if ((p = strchr((char *)buf, '='))) |
| 689 | *p = 0; |
| 690 | val = (char *)buf; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | my_setenv(env, val, err); |
| 695 | |
| 696 | return next ? next + 1 : NULL; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 699 | #ifdef HAVE_LUASCRIPT |
| 700 | static unsigned char *grab_extradata_lua(unsigned char *buf, unsigned char *end, char *field) |
| 701 | { |
| 702 | unsigned char *next; |
| 703 | |
| 704 | if (!buf || (buf == end)) |
| 705 | return NULL; |
| 706 | |
| 707 | for (next = buf; *next != 0; next++) |
| 708 | if (next == end) |
| 709 | return NULL; |
| 710 | |
| 711 | if (next != buf) |
| 712 | { |
| 713 | lua_pushstring(lua, (char *)buf); |
| 714 | lua_setfield(lua, -2, field); |
| 715 | } |
| 716 | |
| 717 | return next + 1; |
| 718 | } |
| 719 | #endif |
| 720 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 721 | static void buff_alloc(size_t size) |
| 722 | { |
| 723 | if (size > buf_size) |
| 724 | { |
| 725 | struct script_data *new; |
| 726 | |
| 727 | /* start with reasonable size, will almost never need extending. */ |
| 728 | if (size < sizeof(struct script_data) + 200) |
| 729 | size = sizeof(struct script_data) + 200; |
| 730 | |
| 731 | if (!(new = whine_malloc(size))) |
| 732 | return; |
| 733 | if (buf) |
| 734 | free(buf); |
| 735 | buf = new; |
| 736 | buf_size = size; |
| 737 | } |
| 738 | } |
| 739 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 740 | /* pack up lease data into a buffer */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 741 | 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] | 742 | { |
| 743 | unsigned char *p; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 744 | unsigned int hostname_len = 0, clid_len = 0, ed_len = 0; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 745 | int fd = daemon->dhcpfd; |
Simon Kelley | 6f9aaa9 | 2013-04-10 10:25:26 +0100 | [diff] [blame] | 746 | #ifdef HAVE_DHCP6 |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 747 | if (!daemon->dhcp) |
| 748 | fd = daemon->dhcp6fd; |
| 749 | #endif |
| 750 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 751 | /* no script */ |
| 752 | if (daemon->helperfd == -1) |
| 753 | return; |
| 754 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 755 | if (lease->extradata) |
| 756 | ed_len = lease->extradata_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 757 | if (lease->clid) |
| 758 | clid_len = lease->clid_len; |
| 759 | if (hostname) |
| 760 | hostname_len = strlen(hostname) + 1; |
| 761 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 762 | buff_alloc(sizeof(struct script_data) + clid_len + ed_len + hostname_len); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 763 | |
| 764 | buf->action = action; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 765 | buf->flags = lease->flags; |
Simon Kelley | 6f9aaa9 | 2013-04-10 10:25:26 +0100 | [diff] [blame] | 766 | #ifdef HAVE_DHCP6 |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 767 | buf->vendorclass_count = lease->vendorclass_count; |
| 768 | buf->addr6 = lease->addr6; |
| 769 | buf->iaid = lease->iaid; |
Simon Kelley | 6f9aaa9 | 2013-04-10 10:25:26 +0100 | [diff] [blame] | 770 | #endif |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 771 | buf->hwaddr_len = lease->hwaddr_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 772 | buf->hwaddr_type = lease->hwaddr_type; |
| 773 | buf->clid_len = clid_len; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 774 | buf->ed_len = ed_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 775 | buf->hostname_len = hostname_len; |
| 776 | buf->addr = lease->addr; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 777 | buf->giaddr = lease->giaddr; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 778 | memcpy(buf->hwaddr, lease->hwaddr, DHCP_CHADDR_MAX); |
| 779 | if (!indextoname(fd, lease->last_interface, buf->interface)) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 780 | buf->interface[0] = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 781 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 782 | #ifdef HAVE_BROKEN_RTC |
| 783 | buf->length = lease->length; |
| 784 | #else |
| 785 | buf->expires = lease->expires; |
| 786 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 787 | |
| 788 | if (lease->expires != 0) |
| 789 | buf->remaining_time = (unsigned int)difftime(lease->expires, now); |
| 790 | else |
| 791 | buf->remaining_time = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 792 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 793 | p = (unsigned char *)(buf+1); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 794 | if (clid_len != 0) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 795 | { |
| 796 | memcpy(p, lease->clid, clid_len); |
| 797 | p += clid_len; |
| 798 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 799 | if (hostname_len != 0) |
| 800 | { |
| 801 | memcpy(p, hostname, hostname_len); |
| 802 | p += hostname_len; |
| 803 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 804 | if (ed_len != 0) |
| 805 | { |
| 806 | memcpy(p, lease->extradata, ed_len); |
| 807 | p += ed_len; |
| 808 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 809 | bytes_in_buf = p - (unsigned char *)buf; |
| 810 | } |
| 811 | |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 812 | #ifdef HAVE_TFTP |
| 813 | /* This nastily re-uses DHCP-fields for TFTP stuff */ |
| 814 | void queue_tftp(off_t file_len, char *filename, union mysockaddr *peer) |
| 815 | { |
| 816 | unsigned int filename_len; |
| 817 | |
| 818 | /* no script */ |
| 819 | if (daemon->helperfd == -1) |
| 820 | return; |
| 821 | |
| 822 | filename_len = strlen(filename) + 1; |
| 823 | buff_alloc(sizeof(struct script_data) + filename_len); |
| 824 | memset(buf, 0, sizeof(struct script_data)); |
| 825 | |
| 826 | buf->action = ACTION_TFTP; |
| 827 | buf->hostname_len = filename_len; |
Simon Kelley | 2f9fd1d | 2013-10-01 09:54:41 +0100 | [diff] [blame] | 828 | buf->file_len = file_len; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 829 | |
| 830 | if ((buf->flags = peer->sa.sa_family) == AF_INET) |
| 831 | buf->addr = peer->in.sin_addr; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 832 | else |
Simon Kelley | b5d9a36 | 2013-09-24 09:44:33 +0100 | [diff] [blame] | 833 | buf->addr6 = peer->in6.sin6_addr; |
Simon Kelley | a953096 | 2012-03-20 22:07:35 +0000 | [diff] [blame] | 834 | |
| 835 | memcpy((unsigned char *)(buf+1), filename, filename_len); |
| 836 | |
| 837 | bytes_in_buf = sizeof(struct script_data) + filename_len; |
| 838 | } |
| 839 | #endif |
| 840 | |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 841 | void queue_arp(int action, unsigned char *mac, int maclen, int family, union all_addr *addr) |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 842 | { |
| 843 | /* no script */ |
| 844 | if (daemon->helperfd == -1) |
| 845 | return; |
| 846 | |
| 847 | buff_alloc(sizeof(struct script_data)); |
| 848 | memset(buf, 0, sizeof(struct script_data)); |
| 849 | |
| 850 | buf->action = action; |
| 851 | buf->hwaddr_len = maclen; |
| 852 | buf->hwaddr_type = ARPHRD_ETHER; |
| 853 | if ((buf->flags = family) == AF_INET) |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 854 | buf->addr = addr->addr4; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 855 | else |
Simon Kelley | cc921df | 2019-01-02 22:48:59 +0000 | [diff] [blame] | 856 | buf->addr6 = addr->addr6; |
Simon Kelley | 33702ab | 2015-12-28 23:17:15 +0000 | [diff] [blame] | 857 | |
| 858 | memcpy(buf->hwaddr, mac, maclen); |
| 859 | |
| 860 | bytes_in_buf = sizeof(struct script_data); |
| 861 | } |
| 862 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 863 | int helper_buf_empty(void) |
| 864 | { |
| 865 | return bytes_in_buf == 0; |
| 866 | } |
| 867 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 868 | void helper_write(void) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 869 | { |
| 870 | ssize_t rc; |
| 871 | |
| 872 | if (bytes_in_buf == 0) |
| 873 | return; |
| 874 | |
| 875 | if ((rc = write(daemon->helperfd, buf, bytes_in_buf)) != -1) |
| 876 | { |
| 877 | if (bytes_in_buf != (size_t)rc) |
| 878 | memmove(buf, buf + rc, bytes_in_buf - rc); |
| 879 | bytes_in_buf -= rc; |
| 880 | } |
| 881 | else |
| 882 | { |
| 883 | if (errno == EAGAIN || errno == EINTR) |
| 884 | return; |
| 885 | bytes_in_buf = 0; |
| 886 | } |
| 887 | } |
| 888 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 889 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 890 | |
| 891 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 892 | |