Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 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 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 21 | /* This file has code to fork a helper process which recieves data via a pipe |
| 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 |
| 37 | #include <lua.h> |
| 38 | #include <lualib.h> |
| 39 | #include <lauxlib.h> |
| 40 | |
| 41 | lua_State *lua; |
| 42 | |
| 43 | static unsigned char *grab_extradata_lua(unsigned char *buf, unsigned char *end, char *field); |
| 44 | #endif |
| 45 | |
| 46 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 47 | struct script_data |
| 48 | { |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 49 | int flags; |
| 50 | int action, hwaddr_len, hwaddr_type; |
| 51 | int clid_len, hostname_len, ed_len; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 52 | struct in_addr addr, giaddr; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 53 | unsigned int remaining_time; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 54 | #ifdef HAVE_BROKEN_RTC |
| 55 | unsigned int length; |
| 56 | #else |
| 57 | time_t expires; |
| 58 | #endif |
| 59 | unsigned char hwaddr[DHCP_CHADDR_MAX]; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 60 | char interface[IF_NAMESIZE]; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 61 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 64 | static struct script_data *buf = NULL; |
| 65 | static size_t bytes_in_buf = 0, buf_size = 0; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 66 | |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 67 | 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] | 68 | { |
| 69 | pid_t pid; |
| 70 | int i, pipefd[2]; |
| 71 | struct sigaction sigact; |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 72 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 73 | /* create the pipe through which the main program sends us commands, |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 74 | then fork our process. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 75 | if (pipe(pipefd) == -1 || !fix_fd(pipefd[1]) || (pid = fork()) == -1) |
| 76 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 77 | send_event(err_fd, EVENT_PIPE_ERR, errno, NULL); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 78 | _exit(0); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 81 | if (pid != 0) |
| 82 | { |
| 83 | close(pipefd[0]); /* close reader side */ |
| 84 | return pipefd[1]; |
| 85 | } |
| 86 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 87 | /* ignore SIGTERM, so that we can clean up when the main process gets hit |
| 88 | and SIGALRM so that we can use sleep() */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 89 | sigact.sa_handler = SIG_IGN; |
| 90 | sigact.sa_flags = 0; |
| 91 | sigemptyset(&sigact.sa_mask); |
| 92 | sigaction(SIGTERM, &sigact, NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 93 | sigaction(SIGALRM, &sigact, NULL); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 94 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 95 | if (!option_bool(OPT_DEBUG) && uid != 0) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 96 | { |
| 97 | gid_t dummy; |
| 98 | if (setgroups(0, &dummy) == -1 || |
| 99 | setgid(gid) == -1 || |
| 100 | setuid(uid) == -1) |
| 101 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 102 | if (option_bool(OPT_NO_FORK)) |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 103 | /* send error to daemon process if no-fork */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 104 | send_event(event_fd, EVENT_USER_ERR, errno, daemon->scriptuser); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 105 | else |
| 106 | { |
| 107 | /* kill daemon */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 108 | send_event(event_fd, EVENT_DIE, 0, NULL); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 109 | /* return error */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 110 | send_event(err_fd, EVENT_USER_ERR, errno, daemon->scriptuser); |
Simon Kelley | 1a6bca8 | 2008-07-11 11:11:42 +0100 | [diff] [blame] | 111 | } |
| 112 | _exit(0); |
| 113 | } |
| 114 | } |
| 115 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 116 | /* close all the sockets etc, we don't need them here. |
| 117 | Don't close err_fd, in case the lua-init fails. |
| 118 | Note that we have to do this before lua init |
| 119 | so we don't close any lua fds. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 120 | for (max_fd--; max_fd >= 0; max_fd--) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 121 | if (max_fd != STDOUT_FILENO && max_fd != STDERR_FILENO && |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 122 | max_fd != STDIN_FILENO && max_fd != pipefd[0] && |
| 123 | max_fd != event_fd && max_fd != err_fd) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 124 | close(max_fd); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 125 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 126 | #ifdef HAVE_LUASCRIPT |
| 127 | if (daemon->luascript) |
| 128 | { |
| 129 | const char *lua_err = NULL; |
| 130 | lua = lua_open(); |
| 131 | luaL_openlibs(lua); |
| 132 | |
| 133 | /* get Lua to load our script file */ |
| 134 | if (luaL_dofile(lua, daemon->luascript) != 0) |
| 135 | lua_err = lua_tostring(lua, -1); |
| 136 | else |
| 137 | { |
| 138 | lua_getglobal(lua, "lease"); |
| 139 | if (lua_type(lua, -1) != LUA_TFUNCTION) |
| 140 | lua_err = _("lease() function missing in Lua script"); |
| 141 | } |
| 142 | |
| 143 | if (lua_err) |
| 144 | { |
| 145 | if (option_bool(OPT_NO_FORK) || option_bool(OPT_DEBUG)) |
| 146 | /* send error to daemon process if no-fork */ |
| 147 | send_event(event_fd, EVENT_LUA_ERR, 0, (char *)lua_err); |
| 148 | else |
| 149 | { |
| 150 | /* kill daemon */ |
| 151 | send_event(event_fd, EVENT_DIE, 0, NULL); |
| 152 | /* return error */ |
| 153 | send_event(err_fd, EVENT_LUA_ERR, 0, (char *)lua_err); |
| 154 | } |
| 155 | _exit(0); |
| 156 | } |
| 157 | |
| 158 | lua_pop(lua, 1); /* remove nil from stack */ |
| 159 | lua_getglobal(lua, "init"); |
| 160 | if (lua_type(lua, -1) == LUA_TFUNCTION) |
| 161 | lua_call(lua, 0, 0); |
| 162 | else |
| 163 | lua_pop(lua, 1); /* remove nil from stack */ |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | /* All init done, close our copy of the error pipe, so that main process can return */ |
| 168 | if (err_fd != -1) |
| 169 | close(err_fd); |
| 170 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 171 | /* loop here */ |
| 172 | while(1) |
| 173 | { |
| 174 | struct script_data data; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 175 | char *p, *action_str, *hostname = NULL, *domain = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 176 | unsigned char *buf = (unsigned char *)daemon->namebuff; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 177 | unsigned char *end, *extradata, *alloc_buff = NULL; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 178 | int is6, err = 0; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 179 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 180 | free(alloc_buff); |
| 181 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 182 | /* we read zero bytes when pipe closed: this is our signal to exit */ |
| 183 | if (!read_write(pipefd[0], (unsigned char *)&data, sizeof(data), 1)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 184 | { |
| 185 | #ifdef HAVE_LUASCRIPT |
| 186 | if (daemon->luascript) |
| 187 | { |
| 188 | lua_getglobal(lua, "shutdown"); |
| 189 | if (lua_type(lua, -1) == LUA_TFUNCTION) |
| 190 | lua_call(lua, 0, 0); |
| 191 | } |
| 192 | #endif |
| 193 | _exit(0); |
| 194 | } |
| 195 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 196 | if (data.action == ACTION_DEL) |
| 197 | action_str = "del"; |
| 198 | else if (data.action == ACTION_ADD) |
| 199 | action_str = "add"; |
| 200 | else if (data.action == ACTION_OLD || data.action == ACTION_OLD_HOSTNAME) |
| 201 | action_str = "old"; |
| 202 | else |
| 203 | continue; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 204 | |
| 205 | is6 = !!(data.flags & (LEASE_TA | LEASE_NA)); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 206 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 207 | if (!is6) |
| 208 | { |
| 209 | /* stringify MAC into dhcp_buff */ |
| 210 | p = daemon->dhcp_buff; |
| 211 | if (data.hwaddr_type != ARPHRD_ETHER || data.hwaddr_len == 0) |
| 212 | p += sprintf(p, "%.2x-", data.hwaddr_type); |
| 213 | for (i = 0; (i < data.hwaddr_len) && (i < DHCP_CHADDR_MAX); i++) |
| 214 | { |
| 215 | p += sprintf(p, "%.2x", data.hwaddr[i]); |
| 216 | if (i != data.hwaddr_len - 1) |
| 217 | p += sprintf(p, ":"); |
| 218 | } |
| 219 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 220 | |
| 221 | /* expiry or length into dhcp_buff2 */ |
| 222 | #ifdef HAVE_BROKEN_RTC |
| 223 | sprintf(daemon->dhcp_buff2, "%u", data.length); |
| 224 | #else |
| 225 | sprintf(daemon->dhcp_buff2, "%lu", (unsigned long)data.expires); |
| 226 | #endif |
| 227 | |
| 228 | /* supplied data may just exceed normal buffer (unlikely) */ |
| 229 | if ((data.hostname_len + data.ed_len + data.clid_len) > MAXDNAME && |
| 230 | !(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] | 231 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 232 | |
| 233 | if (!read_write(pipefd[0], buf, |
| 234 | data.hostname_len + data.ed_len + data.clid_len, 1)) |
| 235 | continue; |
| 236 | |
| 237 | /* CLID into packet */ |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 238 | if (!is6) |
| 239 | for (p = daemon->packet, i = 0; i < data.clid_len; i++) |
| 240 | { |
| 241 | p += sprintf(p, "%.2x", buf[i]); |
| 242 | if (i != data.clid_len - 1) |
| 243 | p += sprintf(p, ":"); |
| 244 | } |
| 245 | #ifdef HAVE_DHCP6 |
| 246 | else |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 247 | { |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 248 | /* or IAID and server DUID for IPv6 */ |
| 249 | sprintf(daemon->dhcp_buff3, "%s%u", data.flags & LEASE_TA ? "T" : "", data.hwaddr_type); |
| 250 | for (p = daemon->packet, i = 0; i < daemon->duid_len; i++) |
| 251 | { |
| 252 | p += sprintf(p, "%.2x", daemon->duid[i]); |
| 253 | if (i != daemon->duid_len - 1) |
| 254 | p += sprintf(p, ":"); |
| 255 | } |
Simon Kelley | caa9438 | 2012-02-15 10:29:50 +0000 | [diff] [blame] | 256 | |
| 257 | /* duid not MAC for IPv6 */ |
| 258 | for (p = daemon->dhcp_buff, i = 0; i < data.clid_len; i++) |
| 259 | { |
| 260 | p += sprintf(p, "%.2x", buf[i]); |
| 261 | if (i != data.clid_len - 1) |
| 262 | p += sprintf(p, ":"); |
| 263 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 264 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 265 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 266 | |
| 267 | buf += data.clid_len; |
| 268 | |
| 269 | if (data.hostname_len != 0) |
| 270 | { |
| 271 | char *dot; |
| 272 | hostname = (char *)buf; |
| 273 | hostname[data.hostname_len - 1] = 0; |
| 274 | if (!legal_hostname(hostname)) |
| 275 | hostname = NULL; |
| 276 | else if ((dot = strchr(hostname, '.'))) |
| 277 | { |
| 278 | domain = dot+1; |
| 279 | *dot = 0; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | extradata = buf + data.hostname_len; |
| 284 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 285 | if (!is6) |
| 286 | inet_ntop(AF_INET, &data.addr, daemon->addrbuff, ADDRSTRLEN); |
| 287 | #ifdef HAVE_DHCP6 |
| 288 | else |
| 289 | inet_ntop(AF_INET6, &data.hwaddr, daemon->addrbuff, ADDRSTRLEN); |
| 290 | #endif |
| 291 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 292 | #ifdef HAVE_LUASCRIPT |
| 293 | if (daemon->luascript) |
| 294 | { |
| 295 | lua_getglobal(lua, "lease"); /* function to call */ |
| 296 | lua_pushstring(lua, action_str); /* arg1 - action */ |
| 297 | lua_newtable(lua); /* arg2 - data table */ |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 298 | |
| 299 | if (is6) |
| 300 | { |
Simon Kelley | 57f460d | 2012-02-16 20:00:32 +0000 | [diff] [blame] | 301 | lua_pushstring(lua, daemon->dhcp_buff); |
| 302 | lua_setfield(lua, -2, "client_duid"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 303 | lua_pushstring(lua, daemon->packet); |
Simon Kelley | 57f460d | 2012-02-16 20:00:32 +0000 | [diff] [blame] | 304 | lua_setfield(lua, -2, "server_duid"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 305 | lua_pushstring(lua, daemon->dhcp_buff3); |
| 306 | lua_setfield(lua, -2, "iaid"); |
| 307 | } |
| 308 | |
| 309 | if (!is6 && data.clid_len != 0) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 310 | { |
| 311 | lua_pushstring(lua, daemon->packet); |
| 312 | lua_setfield(lua, -2, "client_id"); |
| 313 | } |
| 314 | |
| 315 | if (strlen(data.interface) != 0) |
| 316 | { |
| 317 | lua_pushstring(lua, data.interface); |
| 318 | lua_setfield(lua, -2, "interface"); |
| 319 | } |
| 320 | |
| 321 | #ifdef HAVE_BROKEN_RTC |
| 322 | lua_pushnumber(lua, data.length); |
| 323 | lua_setfield(lua, -2, "lease_length"); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 324 | #else |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 325 | lua_pushnumber(lua, data.expires); |
| 326 | lua_setfield(lua, -2, "lease_expires"); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 327 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 328 | |
| 329 | if (hostname) |
| 330 | { |
| 331 | lua_pushstring(lua, hostname); |
| 332 | lua_setfield(lua, -2, "hostname"); |
| 333 | } |
| 334 | |
| 335 | if (domain) |
| 336 | { |
| 337 | lua_pushstring(lua, domain); |
| 338 | lua_setfield(lua, -2, "domain"); |
| 339 | } |
| 340 | |
| 341 | end = extradata + data.ed_len; |
| 342 | buf = extradata; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 343 | |
| 344 | if (!is6) |
| 345 | buf = grab_extradata_lua(buf, end, "vendor_class"); |
| 346 | #ifdef HAVE_DHCP6 |
| 347 | else |
| 348 | for (i = 0; i < data.hwaddr_len; i++) |
| 349 | { |
| 350 | sprintf(daemon->dhcp_buff2, "vendor_class%i", i); |
| 351 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 352 | } |
| 353 | #endif |
| 354 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 355 | buf = grab_extradata_lua(buf, end, "supplied_hostname"); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 356 | |
| 357 | if (!is6) |
| 358 | { |
| 359 | buf = grab_extradata_lua(buf, end, "cpewan_oui"); |
| 360 | buf = grab_extradata_lua(buf, end, "cpewan_serial"); |
| 361 | buf = grab_extradata_lua(buf, end, "cpewan_class"); |
| 362 | } |
| 363 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 364 | buf = grab_extradata_lua(buf, end, "tags"); |
Simon Kelley | caa9438 | 2012-02-15 10:29:50 +0000 | [diff] [blame] | 365 | |
| 366 | if (is6) |
| 367 | buf = grab_extradata_lua(buf, end, "relay_address"); |
| 368 | else if (data.giaddr.s_addr != 0) |
| 369 | { |
| 370 | lua_pushstring(lua, inet_ntoa(data.giaddr)); |
| 371 | lua_setfield(lua, -2, "relay_address"); |
| 372 | } |
| 373 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 374 | for (i = 0; buf; i++) |
| 375 | { |
| 376 | sprintf(daemon->dhcp_buff2, "user_class%i", i); |
| 377 | buf = grab_extradata_lua(buf, end, daemon->dhcp_buff2); |
| 378 | } |
| 379 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 380 | if (data.action != ACTION_DEL && data.remaining_time != 0) |
| 381 | { |
| 382 | lua_pushnumber(lua, data.remaining_time); |
| 383 | lua_setfield(lua, -2, "time_remaining"); |
| 384 | } |
| 385 | |
| 386 | if (data.action == ACTION_OLD_HOSTNAME && hostname) |
| 387 | { |
| 388 | lua_pushstring(lua, hostname); |
| 389 | lua_setfield(lua, -2, "old_hostname"); |
| 390 | } |
| 391 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 392 | if (!is6) |
| 393 | { |
| 394 | lua_pushstring(lua, daemon->dhcp_buff); |
| 395 | lua_setfield(lua, -2, "mac_address"); |
| 396 | } |
| 397 | |
| 398 | lua_pushstring(lua, daemon->addrbuff); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 399 | lua_setfield(lua, -2, "ip_address"); |
| 400 | |
| 401 | lua_call(lua, 2, 0); /* pass 2 values, expect 0 */ |
| 402 | } |
| 403 | #endif |
| 404 | |
| 405 | /* no script, just lua */ |
| 406 | if (!daemon->lease_change_command) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 407 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 408 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 409 | /* possible fork errors are all temporary resource problems */ |
| 410 | while ((pid = fork()) == -1 && (errno == EAGAIN || errno == ENOMEM)) |
| 411 | sleep(2); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 412 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 413 | if (pid == -1) |
| 414 | continue; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 415 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 416 | /* wait for child to complete */ |
| 417 | if (pid != 0) |
| 418 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 419 | /* reap our children's children, if necessary */ |
| 420 | while (1) |
| 421 | { |
| 422 | int status; |
| 423 | pid_t rc = wait(&status); |
| 424 | |
| 425 | if (rc == pid) |
| 426 | { |
| 427 | /* On error send event back to main process for logging */ |
| 428 | if (WIFSIGNALED(status)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 429 | send_event(event_fd, EVENT_KILLED, WTERMSIG(status), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 430 | else if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 431 | send_event(event_fd, EVENT_EXITED, WEXITSTATUS(status), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | |
| 435 | if (rc == -1 && errno != EINTR) |
| 436 | break; |
| 437 | } |
| 438 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 439 | continue; |
| 440 | } |
| 441 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 442 | if (is6) |
| 443 | { |
| 444 | my_setenv("DNSMASQ_IAID", daemon->dhcp_buff3, &err); |
Simon Kelley | 57f460d | 2012-02-16 20:00:32 +0000 | [diff] [blame] | 445 | my_setenv("DNSMASQ_SERVER_DUID", daemon->packet, &err); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | if (!is6 && data.clid_len != 0) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 449 | my_setenv("DNSMASQ_CLIENT_ID", daemon->packet, &err); |
| 450 | |
| 451 | if (strlen(data.interface) != 0) |
| 452 | my_setenv("DNSMASQ_INTERFACE", data.interface, &err); |
| 453 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 454 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 455 | my_setenv("DNSMASQ_LEASE_LENGTH", daemon->dhcp_buff2, &err); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 456 | #else |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 457 | my_setenv("DNSMASQ_LEASE_EXPIRES", daemon->dhcp_buff2, &err); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 458 | #endif |
| 459 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 460 | if (domain) |
| 461 | my_setenv("DNSMASQ_DOMAIN", domain, &err); |
| 462 | |
| 463 | end = extradata + data.ed_len; |
| 464 | buf = extradata; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 465 | |
| 466 | if (!is6) |
| 467 | buf = grab_extradata(buf, end, "DNSMASQ_VENDOR_CLASS", &err); |
| 468 | #ifdef HAVE_DHCP6 |
| 469 | else |
Simon Kelley | a5c72ab | 2012-02-10 13:42:47 +0000 | [diff] [blame] | 470 | { |
| 471 | if (data.hwaddr_len != 0) |
| 472 | { |
| 473 | buf = grab_extradata(buf, end, "DNSMASQ_VENDOR_CLASS_ID", &err); |
| 474 | for (i = 0; i < data.hwaddr_len - 1; i++) |
| 475 | { |
| 476 | sprintf(daemon->dhcp_buff2, "DNSMASQ_VENDOR_CLASS%i", i); |
| 477 | buf = grab_extradata(buf, end, daemon->dhcp_buff2, &err); |
| 478 | } |
| 479 | } |
| 480 | } |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 481 | #endif |
| 482 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 483 | buf = grab_extradata(buf, end, "DNSMASQ_SUPPLIED_HOSTNAME", &err); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 484 | |
| 485 | if (!is6) |
| 486 | { |
| 487 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_OUI", &err); |
| 488 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_SERIAL", &err); |
| 489 | buf = grab_extradata(buf, end, "DNSMASQ_CPEWAN_CLASS", &err); |
| 490 | } |
| 491 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 492 | buf = grab_extradata(buf, end, "DNSMASQ_TAGS", &err); |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 493 | |
| 494 | if (is6) |
| 495 | buf = grab_extradata(buf, end, "DNSMASQ_RELAY_ADDRESS", &err); |
Simon Kelley | caa9438 | 2012-02-15 10:29:50 +0000 | [diff] [blame] | 496 | else if (data.giaddr.s_addr != 0) |
| 497 | my_setenv("DNSMASQ_RELAY_ADDRESS", inet_ntoa(data.giaddr), &err); |
| 498 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 499 | for (i = 0; buf; i++) |
| 500 | { |
| 501 | sprintf(daemon->dhcp_buff2, "DNSMASQ_USER_CLASS%i", i); |
| 502 | buf = grab_extradata(buf, end, daemon->dhcp_buff2, &err); |
| 503 | } |
| 504 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 505 | if (data.action != ACTION_DEL && data.remaining_time != 0) |
Simon Kelley | 8ef5ada | 2010-06-03 19:42:45 +0100 | [diff] [blame] | 506 | { |
| 507 | sprintf(daemon->dhcp_buff2, "%u", data.remaining_time); |
| 508 | my_setenv("DNSMASQ_TIME_REMAINING", daemon->dhcp_buff2, &err); |
| 509 | } |
| 510 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 511 | if (data.action == ACTION_OLD_HOSTNAME && hostname) |
| 512 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 513 | my_setenv("DNSMASQ_OLD_HOSTNAME", hostname, &err); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 514 | hostname = NULL; |
| 515 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 516 | |
| 517 | /* we need to have the event_fd around if exec fails */ |
| 518 | if ((i = fcntl(event_fd, F_GETFD)) != -1) |
| 519 | fcntl(event_fd, F_SETFD, i | FD_CLOEXEC); |
| 520 | close(pipefd[0]); |
| 521 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 522 | p = strrchr(daemon->lease_change_command, '/'); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 523 | if (err == 0) |
| 524 | { |
| 525 | execl(daemon->lease_change_command, |
| 526 | p ? p+1 : daemon->lease_change_command, |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 527 | action_str, daemon->dhcp_buff, daemon->addrbuff, hostname, (char*)NULL); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 528 | err = errno; |
| 529 | } |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 530 | /* failed, send event so the main process logs the problem */ |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 531 | send_event(event_fd, EVENT_EXEC_ERR, err, NULL); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 532 | _exit(0); |
| 533 | } |
| 534 | } |
| 535 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 536 | static void my_setenv(const char *name, const char *value, int *error) |
| 537 | { |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 538 | if (*error == 0 && setenv(name, value, 1) != 0) |
| 539 | *error = errno; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 542 | static unsigned char *grab_extradata(unsigned char *buf, unsigned char *end, char *env, int *err) |
| 543 | { |
| 544 | unsigned char *next; |
| 545 | |
| 546 | if (!buf || (buf == end)) |
| 547 | return NULL; |
| 548 | |
| 549 | for (next = buf; *next != 0; next++) |
| 550 | if (next == end) |
| 551 | return NULL; |
| 552 | |
| 553 | if (next != buf) |
| 554 | { |
| 555 | char *p; |
| 556 | /* No "=" in value */ |
| 557 | if ((p = strchr((char *)buf, '='))) |
| 558 | *p = 0; |
| 559 | my_setenv(env, (char *)buf, err); |
| 560 | } |
| 561 | |
| 562 | return next + 1; |
| 563 | } |
| 564 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 565 | #ifdef HAVE_LUASCRIPT |
| 566 | static unsigned char *grab_extradata_lua(unsigned char *buf, unsigned char *end, char *field) |
| 567 | { |
| 568 | unsigned char *next; |
| 569 | |
| 570 | if (!buf || (buf == end)) |
| 571 | return NULL; |
| 572 | |
| 573 | for (next = buf; *next != 0; next++) |
| 574 | if (next == end) |
| 575 | return NULL; |
| 576 | |
| 577 | if (next != buf) |
| 578 | { |
| 579 | lua_pushstring(lua, (char *)buf); |
| 580 | lua_setfield(lua, -2, field); |
| 581 | } |
| 582 | |
| 583 | return next + 1; |
| 584 | } |
| 585 | #endif |
| 586 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 587 | /* pack up lease data into a buffer */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 588 | 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] | 589 | { |
| 590 | unsigned char *p; |
| 591 | size_t size; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 592 | unsigned int hostname_len = 0, clid_len = 0, ed_len = 0; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 593 | int fd = daemon->dhcpfd; |
| 594 | |
| 595 | #ifdef HAVE_DHCP6 |
| 596 | if (!daemon->dhcp) |
| 597 | fd = daemon->dhcp6fd; |
| 598 | #endif |
| 599 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 600 | /* no script */ |
| 601 | if (daemon->helperfd == -1) |
| 602 | return; |
| 603 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 604 | if (lease->extradata) |
| 605 | ed_len = lease->extradata_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 606 | if (lease->clid) |
| 607 | clid_len = lease->clid_len; |
| 608 | if (hostname) |
| 609 | hostname_len = strlen(hostname) + 1; |
| 610 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 611 | size = sizeof(struct script_data) + clid_len + ed_len + hostname_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 612 | |
| 613 | if (size > buf_size) |
| 614 | { |
| 615 | struct script_data *new; |
| 616 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 617 | /* start with reasonable size, will almost never need extending. */ |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 618 | if (size < sizeof(struct script_data) + 200) |
| 619 | size = sizeof(struct script_data) + 200; |
| 620 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 621 | if (!(new = whine_malloc(size))) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 622 | return; |
| 623 | if (buf) |
| 624 | free(buf); |
| 625 | buf = new; |
| 626 | buf_size = size; |
| 627 | } |
| 628 | |
| 629 | buf->action = action; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 630 | buf->flags = lease->flags; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 631 | buf->hwaddr_len = lease->hwaddr_len; |
| 632 | buf->hwaddr_type = lease->hwaddr_type; |
| 633 | buf->clid_len = clid_len; |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 634 | buf->ed_len = ed_len; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 635 | buf->hostname_len = hostname_len; |
| 636 | buf->addr = lease->addr; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 637 | buf->giaddr = lease->giaddr; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 638 | memcpy(buf->hwaddr, lease->hwaddr, DHCP_CHADDR_MAX); |
| 639 | if (!indextoname(fd, lease->last_interface, buf->interface)) |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 640 | buf->interface[0] = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 641 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 642 | #ifdef HAVE_BROKEN_RTC |
| 643 | buf->length = lease->length; |
| 644 | #else |
| 645 | buf->expires = lease->expires; |
| 646 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 647 | |
| 648 | if (lease->expires != 0) |
| 649 | buf->remaining_time = (unsigned int)difftime(lease->expires, now); |
| 650 | else |
| 651 | buf->remaining_time = 0; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 652 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 653 | p = (unsigned char *)(buf+1); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 654 | if (clid_len != 0) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 655 | { |
| 656 | memcpy(p, lease->clid, clid_len); |
| 657 | p += clid_len; |
| 658 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 659 | if (hostname_len != 0) |
| 660 | { |
| 661 | memcpy(p, hostname, hostname_len); |
| 662 | p += hostname_len; |
| 663 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 664 | if (ed_len != 0) |
| 665 | { |
| 666 | memcpy(p, lease->extradata, ed_len); |
| 667 | p += ed_len; |
| 668 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 669 | bytes_in_buf = p - (unsigned char *)buf; |
| 670 | } |
| 671 | |
| 672 | int helper_buf_empty(void) |
| 673 | { |
| 674 | return bytes_in_buf == 0; |
| 675 | } |
| 676 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 677 | void helper_write(void) |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 678 | { |
| 679 | ssize_t rc; |
| 680 | |
| 681 | if (bytes_in_buf == 0) |
| 682 | return; |
| 683 | |
| 684 | if ((rc = write(daemon->helperfd, buf, bytes_in_buf)) != -1) |
| 685 | { |
| 686 | if (bytes_in_buf != (size_t)rc) |
| 687 | memmove(buf, buf + rc, bytes_in_buf - rc); |
| 688 | bytes_in_buf -= rc; |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | if (errno == EAGAIN || errno == EINTR) |
| 693 | return; |
| 694 | bytes_in_buf = 0; |
| 695 | } |
| 696 | } |
| 697 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 698 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 699 | |
| 700 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 701 | |