Simon Kelley | c49778d | 2016-01-06 18:52:33 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2016 Simon Kelley |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [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 | 9e4abcb | 2004-01-22 19:47:41 +0000 | [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 | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 17 | #include "dnsmasq.h" |
| 18 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 19 | #ifdef HAVE_DHCP |
| 20 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 21 | static struct dhcp_lease *leases = NULL, *old_leases = NULL; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 22 | static int dns_dirty, file_dirty, leases_left; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 23 | |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 24 | static int read_leases(time_t now, FILE *leasestream) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 25 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 26 | unsigned long ei; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 27 | struct all_addr addr; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 28 | struct dhcp_lease *lease; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 29 | int clid_len, hw_len, hw_type; |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 30 | int items; |
| 31 | char *domain = NULL; |
| 32 | |
| 33 | *daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0'; |
| 34 | |
| 35 | /* client-id max length is 255 which is 255*2 digits + 254 colons |
| 36 | borrow DNS packet buffer which is always larger than 1000 bytes |
| 37 | |
| 38 | Check various buffers are big enough for the code below */ |
| 39 | |
| 40 | #if (DHCP_BUFF_SZ < 255) || (MAXDNAME < 64) || (PACKETSZ+MAXDNAME+RRFIXEDSZ < 764) |
| 41 | # error Buffer size breakage in leasefile parsing. |
| 42 | #endif |
| 43 | |
| 44 | while ((items=fscanf(leasestream, "%255s %255s", daemon->dhcp_buff3, daemon->dhcp_buff2)) == 2) |
| 45 | { |
| 46 | *daemon->namebuff = *daemon->dhcp_buff = *daemon->packet = '\0'; |
| 47 | hw_len = hw_type = clid_len = 0; |
| 48 | |
| 49 | #ifdef HAVE_DHCP6 |
| 50 | if (strcmp(daemon->dhcp_buff3, "duid") == 0) |
| 51 | { |
| 52 | daemon->duid_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, 130, NULL, NULL); |
| 53 | if (daemon->duid_len < 0) |
| 54 | return 0; |
| 55 | daemon->duid = safe_malloc(daemon->duid_len); |
| 56 | memcpy(daemon->duid, daemon->dhcp_buff2, daemon->duid_len); |
| 57 | continue; |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | if (fscanf(leasestream, " %64s %255s %764s", |
| 62 | daemon->namebuff, daemon->dhcp_buff, daemon->packet) != 3) |
| 63 | return 0; |
| 64 | |
| 65 | if (inet_pton(AF_INET, daemon->namebuff, &addr.addr.addr4)) |
| 66 | { |
| 67 | if ((lease = lease4_allocate(addr.addr.addr4))) |
| 68 | domain = get_domain(lease->addr); |
| 69 | |
| 70 | hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type); |
| 71 | /* For backwards compatibility, no explicit MAC address type means ether. */ |
| 72 | if (hw_type == 0 && hw_len != 0) |
| 73 | hw_type = ARPHRD_ETHER; |
| 74 | } |
| 75 | #ifdef HAVE_DHCP6 |
| 76 | else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr.addr6)) |
| 77 | { |
| 78 | char *s = daemon->dhcp_buff2; |
| 79 | int lease_type = LEASE_NA; |
| 80 | |
| 81 | if (s[0] == 'T') |
| 82 | { |
| 83 | lease_type = LEASE_TA; |
| 84 | s++; |
| 85 | } |
| 86 | |
| 87 | if ((lease = lease6_allocate(&addr.addr.addr6, lease_type))) |
| 88 | { |
| 89 | lease_set_iaid(lease, strtoul(s, NULL, 10)); |
| 90 | domain = get_domain6((struct in6_addr *)lease->hwaddr); |
| 91 | } |
| 92 | } |
| 93 | #endif |
| 94 | else |
| 95 | return 0; |
| 96 | |
| 97 | if (!lease) |
| 98 | die (_("too many stored leases"), NULL, EC_MISC); |
| 99 | |
| 100 | if (strcmp(daemon->packet, "*") != 0) |
| 101 | clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL); |
| 102 | |
| 103 | lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet, |
| 104 | hw_len, hw_type, clid_len, now, 0); |
| 105 | |
| 106 | if (strcmp(daemon->dhcp_buff, "*") != 0) |
| 107 | lease_set_hostname(lease, daemon->dhcp_buff, 0, domain, NULL); |
| 108 | |
| 109 | ei = atol(daemon->dhcp_buff3); |
| 110 | |
| 111 | #ifdef HAVE_BROKEN_RTC |
| 112 | if (ei != 0) |
| 113 | lease->expires = (time_t)ei + now; |
| 114 | else |
| 115 | lease->expires = (time_t)0; |
| 116 | lease->length = ei; |
| 117 | #else |
| 118 | /* strictly time_t is opaque, but this hack should work on all sane systems, |
| 119 | even when sizeof(time_t) == 8 */ |
| 120 | lease->expires = (time_t)ei; |
| 121 | #endif |
| 122 | |
| 123 | /* set these correctly: the "old" events are generated later from |
| 124 | the startup synthesised SIGHUP. */ |
| 125 | lease->flags &= ~(LEASE_NEW | LEASE_CHANGED); |
| 126 | |
| 127 | *daemon->dhcp_buff3 = *daemon->dhcp_buff2 = '\0'; |
| 128 | } |
| 129 | |
| 130 | return (items == 0 || items == EOF); |
| 131 | } |
| 132 | |
| 133 | void lease_init(time_t now) |
| 134 | { |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 135 | FILE *leasestream; |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 136 | int readok = 0; |
| 137 | |
Simon Kelley | 3be3454 | 2004-09-11 19:12:13 +0100 | [diff] [blame] | 138 | leases_left = daemon->dhcp_max; |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 139 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 140 | if (option_bool(OPT_LEASE_RO)) |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 141 | { |
| 142 | /* run "<lease_change_script> init" once to get the |
| 143 | initial state of the database. If leasefile-ro is |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 144 | set without a script, we just do without any |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 145 | lease database. */ |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 146 | #ifdef HAVE_SCRIPT |
| 147 | if (daemon->lease_change_command) |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 148 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 149 | strcpy(daemon->dhcp_buff, daemon->lease_change_command); |
| 150 | strcat(daemon->dhcp_buff, " init"); |
| 151 | leasestream = popen(daemon->dhcp_buff, "r"); |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 152 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 153 | else |
| 154 | #endif |
| 155 | { |
| 156 | file_dirty = dns_dirty = 0; |
| 157 | return; |
| 158 | } |
| 159 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 160 | } |
| 161 | else |
| 162 | { |
| 163 | /* NOTE: need a+ mode to create file if it doesn't exist */ |
| 164 | leasestream = daemon->lease_stream = fopen(daemon->lease_file, "a+"); |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 165 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 166 | if (!leasestream) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 167 | die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_FILE); |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 168 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 169 | /* a+ mode leaves pointer at end. */ |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 170 | rewind(leasestream); |
| 171 | } |
Simon Kelley | bf4e62c | 2016-07-22 21:37:59 +0100 | [diff] [blame] | 172 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 173 | if (leasestream) |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 174 | { |
| 175 | readok = read_leases(now, leasestream); |
| 176 | if (ferror(leasestream)) |
| 177 | die(_("failed to read lease file %s: %s"), daemon->lease_file, EC_FILE); |
Simon Kelley | 3d7b550 | 2012-02-10 17:35:37 +0000 | [diff] [blame] | 178 | |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 179 | } |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 180 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 181 | #ifdef HAVE_SCRIPT |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 182 | if (!daemon->lease_stream) |
| 183 | { |
| 184 | int rc = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 185 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 186 | /* shell returns 127 for "command not found", 126 for bad permissions. */ |
| 187 | if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126) |
| 188 | { |
| 189 | if (WEXITSTATUS(rc) == 127) |
| 190 | errno = ENOENT; |
| 191 | else if (WEXITSTATUS(rc) == 126) |
| 192 | errno = EACCES; |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 193 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 194 | die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE); |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (WEXITSTATUS(rc) != 0) |
| 198 | { |
| 199 | sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 200 | die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET); |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 201 | } |
Petr Menšík | 3a8b0f6 | 2017-04-23 14:12:37 +0100 | [diff] [blame] | 202 | |
| 203 | /* Only complain if we stopped reading due to a non-parsed line when running script, |
| 204 | this is expected behaviour when reading from a file, if the file was written with IPv6 data |
| 205 | and we are not compiled to understand that. */ |
| 206 | if (!readok) |
| 207 | { |
| 208 | my_syslog(MS_DHCP | LOG_ERR, _("aborting lease-init script, invalid line: %s %s %s %s ..."), |
| 209 | daemon->dhcp_buff3, daemon->dhcp_buff2, |
| 210 | daemon->namebuff, daemon->dhcp_buff); |
| 211 | die(_("failed to parse lease-init script output"), NULL, EC_FILE); |
| 212 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 213 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 214 | #endif |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 215 | |
| 216 | /* Some leases may have expired */ |
| 217 | file_dirty = 0; |
| 218 | lease_prune(NULL, now); |
| 219 | dns_dirty = 1; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 222 | void lease_update_from_configs(void) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 223 | { |
| 224 | /* changes to the config may change current leases. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 225 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 226 | struct dhcp_lease *lease; |
| 227 | struct dhcp_config *config; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 228 | char *name; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 229 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 230 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 231 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 232 | continue; |
| 233 | else if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len, |
| 234 | lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) && |
| 235 | (config->flags & CONFIG_NAME) && |
| 236 | (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr)) |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 237 | lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 238 | else if ((name = host_from_dns(lease->addr))) |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 239 | lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 240 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 241 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 242 | static void ourprintf(int *errp, char *format, ...) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 243 | { |
| 244 | va_list ap; |
| 245 | |
| 246 | va_start(ap, format); |
| 247 | if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0) |
| 248 | *errp = errno; |
| 249 | va_end(ap); |
| 250 | } |
| 251 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 252 | void lease_update_file(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 253 | { |
| 254 | struct dhcp_lease *lease; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 255 | time_t next_event; |
| 256 | int i, err = 0; |
| 257 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 258 | if (file_dirty != 0 && daemon->lease_stream) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 259 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 260 | errno = 0; |
| 261 | rewind(daemon->lease_stream); |
| 262 | if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 263 | err = errno; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 264 | |
| 265 | for (lease = leases; lease; lease = lease->next) |
| 266 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 267 | |
| 268 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 269 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 270 | continue; |
| 271 | #endif |
| 272 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 273 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 274 | ourprintf(&err, "%u ", lease->length); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 275 | #else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 276 | ourprintf(&err, "%lu ", (unsigned long)lease->expires); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 277 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 278 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 279 | if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 280 | ourprintf(&err, "%.2x-", lease->hwaddr_type); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 281 | for (i = 0; i < lease->hwaddr_len; i++) |
| 282 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 283 | ourprintf(&err, "%.2x", lease->hwaddr[i]); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 284 | if (i != lease->hwaddr_len - 1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 285 | ourprintf(&err, ":"); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 286 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 287 | |
| 288 | inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 289 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 290 | ourprintf(&err, " %s ", daemon->addrbuff); |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 291 | ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*"); |
| 292 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 293 | if (lease->clid && lease->clid_len != 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 294 | { |
| 295 | for (i = 0; i < lease->clid_len - 1; i++) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 296 | ourprintf(&err, "%.2x:", lease->clid[i]); |
| 297 | ourprintf(&err, "%.2x\n", lease->clid[i]); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 298 | } |
| 299 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 300 | ourprintf(&err, "*\n"); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 301 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 302 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 303 | #ifdef HAVE_DHCP6 |
| 304 | if (daemon->duid) |
| 305 | { |
| 306 | ourprintf(&err, "duid "); |
| 307 | for (i = 0; i < daemon->duid_len - 1; i++) |
| 308 | ourprintf(&err, "%.2x:", daemon->duid[i]); |
| 309 | ourprintf(&err, "%.2x\n", daemon->duid[i]); |
| 310 | |
| 311 | for (lease = leases; lease; lease = lease->next) |
| 312 | { |
| 313 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 314 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 315 | continue; |
| 316 | |
| 317 | #ifdef HAVE_BROKEN_RTC |
| 318 | ourprintf(&err, "%u ", lease->length); |
| 319 | #else |
| 320 | ourprintf(&err, "%lu ", (unsigned long)lease->expires); |
| 321 | #endif |
| 322 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 323 | inet_ntop(AF_INET6, &lease->addr6, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 324 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 325 | ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "", |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 326 | lease->iaid, daemon->addrbuff); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 327 | ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*"); |
| 328 | |
| 329 | if (lease->clid && lease->clid_len != 0) |
| 330 | { |
| 331 | for (i = 0; i < lease->clid_len - 1; i++) |
| 332 | ourprintf(&err, "%.2x:", lease->clid[i]); |
| 333 | ourprintf(&err, "%.2x\n", lease->clid[i]); |
| 334 | } |
| 335 | else |
| 336 | ourprintf(&err, "*\n"); |
| 337 | } |
| 338 | } |
| 339 | #endif |
| 340 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 341 | if (fflush(daemon->lease_stream) != 0 || |
| 342 | fsync(fileno(daemon->lease_stream)) < 0) |
| 343 | err = errno; |
| 344 | |
| 345 | if (!err) |
| 346 | file_dirty = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 347 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 348 | |
Simon Kelley | dd0e0a3 | 2014-01-22 11:16:59 +0000 | [diff] [blame] | 349 | /* Set alarm for when the first lease expires. */ |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 350 | next_event = 0; |
| 351 | |
| 352 | #ifdef HAVE_DHCP6 |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 353 | /* do timed RAs and determine when the next is, also pings to potential SLAAC addresses */ |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 354 | if (daemon->doing_ra) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 355 | { |
Simon Kelley | 919dd7c | 2012-05-12 15:23:09 +0100 | [diff] [blame] | 356 | time_t event; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 357 | |
Simon Kelley | 919dd7c | 2012-05-12 15:23:09 +0100 | [diff] [blame] | 358 | if ((event = periodic_slaac(now, leases)) != 0) |
| 359 | { |
| 360 | if (next_event == 0 || difftime(next_event, event) > 0.0) |
| 361 | next_event = event; |
| 362 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 363 | |
Simon Kelley | 919dd7c | 2012-05-12 15:23:09 +0100 | [diff] [blame] | 364 | if ((event = periodic_ra(now)) != 0) |
| 365 | { |
| 366 | if (next_event == 0 || difftime(next_event, event) > 0.0) |
| 367 | next_event = event; |
| 368 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 369 | } |
Simon Kelley | c5ad4e7 | 2012-02-24 16:06:20 +0000 | [diff] [blame] | 370 | #endif |
| 371 | |
| 372 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 373 | if (lease->expires != 0 && |
Simon Kelley | dd0e0a3 | 2014-01-22 11:16:59 +0000 | [diff] [blame] | 374 | (next_event == 0 || difftime(next_event, lease->expires) > 0.0)) |
| 375 | next_event = lease->expires; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 376 | |
| 377 | if (err) |
| 378 | { |
| 379 | if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0) |
| 380 | next_event = LEASE_RETRY + now; |
| 381 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 382 | my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"), |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 383 | daemon->lease_file, strerror(err), |
| 384 | (unsigned int)difftime(next_event, now)); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 387 | send_alarm(next_event, now); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 388 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 389 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 390 | |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 391 | static int find_interface_v4(struct in_addr local, int if_index, char *label, |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 392 | struct in_addr netmask, struct in_addr broadcast, void *vparam) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 393 | { |
| 394 | struct dhcp_lease *lease; |
Simon Kelley | 6d8e8ac | 2014-07-13 22:12:45 +0100 | [diff] [blame] | 395 | int prefix = netmask_length(netmask); |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 396 | |
Simon Kelley | 3f2873d | 2013-05-14 11:28:47 +0100 | [diff] [blame] | 397 | (void) label; |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 398 | (void) broadcast; |
| 399 | (void) vparam; |
| 400 | |
| 401 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 6d8e8ac | 2014-07-13 22:12:45 +0100 | [diff] [blame] | 402 | if (!(lease->flags & (LEASE_TA | LEASE_NA)) && |
| 403 | is_same_net(local, lease->addr, netmask) && |
| 404 | prefix > lease->new_prefixlen) |
| 405 | { |
| 406 | lease->new_interface = if_index; |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 407 | lease->new_prefixlen = prefix; |
| 408 | } |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 409 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | #ifdef HAVE_DHCP6 |
| 414 | static int find_interface_v6(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 415 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 416 | int preferred, int valid, void *vparam) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 417 | { |
| 418 | struct dhcp_lease *lease; |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 419 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 420 | (void)scope; |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 421 | (void)flags; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 422 | (void)preferred; |
| 423 | (void)valid; |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 424 | (void)vparam; |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 425 | |
| 426 | for (lease = leases; lease; lease = lease->next) |
| 427 | if ((lease->flags & (LEASE_TA | LEASE_NA))) |
Lung-Pin Chang | dc8a1b1 | 2014-07-02 10:48:05 +0800 | [diff] [blame] | 428 | if (is_same_net6(local, &lease->addr6, prefix) && prefix > lease->new_prefixlen) { |
| 429 | /* save prefix length for comparison, as we might get shorter matching |
| 430 | * prefix in upcoming netlink GETADDR responses |
| 431 | * */ |
| 432 | lease->new_interface = if_index; |
| 433 | lease->new_prefixlen = prefix; |
| 434 | } |
| 435 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 436 | return 1; |
| 437 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 438 | |
| 439 | void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface) |
| 440 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 441 | /* We may be doing RA but not DHCPv4, in which case the lease |
| 442 | database may not exist and we have nothing to do anyway */ |
| 443 | if (daemon->dhcp) |
| 444 | slaac_ping_reply(sender, packet, interface, leases); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 447 | void lease_update_slaac(time_t now) |
| 448 | { |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 449 | /* Called when we construct a new RA-names context, to add putative |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 450 | new SLAAC addresses to existing leases. */ |
| 451 | |
| 452 | struct dhcp_lease *lease; |
| 453 | |
| 454 | if (daemon->dhcp) |
| 455 | for (lease = leases; lease; lease = lease->next) |
| 456 | slaac_add_addrs(lease, now, 0); |
| 457 | } |
| 458 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 459 | #endif |
| 460 | |
| 461 | |
| 462 | /* Find interfaces associated with leases at start-up. This gets updated as |
| 463 | we do DHCP transactions, but information about directly-connected subnets |
| 464 | is useful from scrips and necessary for determining SLAAC addresses from |
| 465 | start-time. */ |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 466 | void lease_find_interfaces(time_t now) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 467 | { |
Simon Kelley | 6d8e8ac | 2014-07-13 22:12:45 +0100 | [diff] [blame] | 468 | struct dhcp_lease *lease; |
| 469 | |
| 470 | for (lease = leases; lease; lease = lease->next) |
| 471 | lease->new_prefixlen = lease->new_interface = 0; |
| 472 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 473 | iface_enumerate(AF_INET, &now, find_interface_v4); |
| 474 | #ifdef HAVE_DHCP6 |
| 475 | iface_enumerate(AF_INET6, &now, find_interface_v6); |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 476 | #endif |
Simon Kelley | 6d8e8ac | 2014-07-13 22:12:45 +0100 | [diff] [blame] | 477 | |
| 478 | for (lease = leases; lease; lease = lease->next) |
| 479 | if (lease->new_interface != 0) |
| 480 | lease_set_interface(lease, lease->new_interface, now); |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 481 | } |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 482 | |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 483 | #ifdef HAVE_DHCP6 |
| 484 | void lease_make_duid(time_t now) |
| 485 | { |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 486 | /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */ |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 487 | if (!daemon->duid && daemon->doing_dhcp6) |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 488 | { |
| 489 | file_dirty = 1; |
| 490 | make_duid(now); |
| 491 | } |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 492 | } |
Simon Kelley | 3511a92 | 2013-11-07 10:28:11 +0000 | [diff] [blame] | 493 | #endif |
| 494 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 495 | |
| 496 | |
| 497 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 498 | void lease_update_dns(int force) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 499 | { |
| 500 | struct dhcp_lease *lease; |
| 501 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 502 | if (daemon->port != 0 && (dns_dirty || force)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 503 | { |
Simon Kelley | 8ff5567 | 2012-12-09 21:09:01 +0000 | [diff] [blame] | 504 | #ifndef HAVE_BROKEN_RTC |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 505 | /* force transfer to authoritative secondaries */ |
| 506 | daemon->soa_sn++; |
Simon Kelley | 8ff5567 | 2012-12-09 21:09:01 +0000 | [diff] [blame] | 507 | #endif |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 508 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 509 | cache_unhash_dhcp(); |
| 510 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 511 | for (lease = leases; lease; lease = lease->next) |
| 512 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 513 | int prot = AF_INET; |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 514 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 515 | #ifdef HAVE_DHCP6 |
| 516 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 517 | prot = AF_INET6; |
Simon Kelley | f444cdd | 2012-03-07 10:15:57 +0000 | [diff] [blame] | 518 | else if (lease->hostname || lease->fqdn) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 519 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 520 | struct slaac_address *slaac; |
| 521 | |
| 522 | for (slaac = lease->slaac_address; slaac; slaac = slaac->next) |
| 523 | if (slaac->backoff == 0) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 524 | { |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 525 | if (lease->fqdn) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 526 | cache_add_dhcp_entry(lease->fqdn, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires); |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 527 | if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 528 | cache_add_dhcp_entry(lease->hostname, AF_INET6, (struct all_addr *)&slaac->addr, lease->expires); |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 531 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 532 | if (lease->fqdn) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 533 | cache_add_dhcp_entry(lease->fqdn, prot, |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 534 | prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->addr6, |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 535 | lease->expires); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 536 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 537 | if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 538 | cache_add_dhcp_entry(lease->hostname, prot, |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 539 | prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->addr6, |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 540 | lease->expires); |
Simon Kelley | 91543f4 | 2013-09-23 12:41:20 +0100 | [diff] [blame] | 541 | |
| 542 | #else |
| 543 | if (lease->fqdn) |
| 544 | cache_add_dhcp_entry(lease->fqdn, prot, (struct all_addr *)&lease->addr, lease->expires); |
| 545 | |
| 546 | if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) |
| 547 | cache_add_dhcp_entry(lease->hostname, prot, (struct all_addr *)&lease->addr, lease->expires); |
| 548 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | dns_dirty = 0; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | void lease_prune(struct dhcp_lease *target, time_t now) |
| 556 | { |
| 557 | struct dhcp_lease *lease, *tmp, **up; |
| 558 | |
| 559 | for (lease = leases, up = &leases; lease; lease = tmp) |
| 560 | { |
| 561 | tmp = lease->next; |
| 562 | if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target) |
| 563 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 564 | file_dirty = 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 565 | if (lease->hostname) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 566 | dns_dirty = 1; |
| 567 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 568 | *up = lease->next; /* unlink */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 569 | |
| 570 | /* Put on old_leases list 'till we |
| 571 | can run the script */ |
| 572 | lease->next = old_leases; |
| 573 | old_leases = lease; |
| 574 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 575 | leases_left++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 576 | } |
| 577 | else |
| 578 | up = &lease->next; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 583 | struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type, |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 584 | unsigned char *clid, int clid_len) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 585 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 586 | struct dhcp_lease *lease; |
| 587 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 588 | if (clid) |
| 589 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 590 | { |
| 591 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 592 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 593 | continue; |
| 594 | #endif |
| 595 | if (lease->clid && clid_len == lease->clid_len && |
| 596 | memcmp(clid, lease->clid, clid_len) == 0) |
| 597 | return lease; |
| 598 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 599 | |
| 600 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 601 | { |
| 602 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 603 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 604 | continue; |
| 605 | #endif |
| 606 | if ((!lease->clid || !clid) && |
| 607 | hw_len != 0 && |
| 608 | lease->hwaddr_len == hw_len && |
| 609 | lease->hwaddr_type == hw_type && |
| 610 | memcmp(hwaddr, lease->hwaddr, hw_len) == 0) |
| 611 | return lease; |
| 612 | } |
| 613 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | struct dhcp_lease *lease_find_by_addr(struct in_addr addr) |
| 618 | { |
| 619 | struct dhcp_lease *lease; |
| 620 | |
| 621 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 622 | { |
| 623 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 624 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 625 | continue; |
| 626 | #endif |
| 627 | if (lease->addr.s_addr == addr.s_addr) |
| 628 | return lease; |
| 629 | } |
| 630 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 631 | return NULL; |
| 632 | } |
| 633 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 634 | #ifdef HAVE_DHCP6 |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 635 | /* find address for {CLID, IAID, address} */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 636 | struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len, |
| 637 | int lease_type, int iaid, struct in6_addr *addr) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 638 | { |
| 639 | struct dhcp_lease *lease; |
| 640 | |
| 641 | for (lease = leases; lease; lease = lease->next) |
| 642 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 643 | if (!(lease->flags & lease_type) || lease->iaid != iaid) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 644 | continue; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 645 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 646 | if (!IN6_ARE_ADDR_EQUAL(&lease->addr6, addr)) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 647 | continue; |
| 648 | |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 649 | if ((clid_len != lease->clid_len || |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 650 | memcmp(clid, lease->clid, clid_len) != 0)) |
| 651 | continue; |
| 652 | |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 653 | return lease; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return NULL; |
| 657 | } |
| 658 | |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 659 | /* reset "USED flags */ |
| 660 | void lease6_reset(void) |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 661 | { |
| 662 | struct dhcp_lease *lease; |
| 663 | |
| 664 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 665 | lease->flags &= ~LEASE_USED; |
| 666 | } |
| 667 | |
| 668 | /* enumerate all leases belonging to {CLID, IAID} */ |
| 669 | struct dhcp_lease *lease6_find_by_client(struct dhcp_lease *first, int lease_type, unsigned char *clid, int clid_len, int iaid) |
| 670 | { |
| 671 | struct dhcp_lease *lease; |
| 672 | |
| 673 | if (!first) |
| 674 | first = leases; |
Simon Kelley | 27cb314 | 2013-04-02 20:06:39 +0100 | [diff] [blame] | 675 | else |
| 676 | first = first->next; |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 677 | |
| 678 | for (lease = first; lease; lease = lease->next) |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 679 | { |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 680 | if (lease->flags & LEASE_USED) |
| 681 | continue; |
| 682 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 683 | if (!(lease->flags & lease_type) || lease->iaid != iaid) |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 684 | continue; |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 685 | |
| 686 | if ((clid_len != lease->clid_len || |
| 687 | memcmp(clid, lease->clid, clid_len) != 0)) |
| 688 | continue; |
| 689 | |
| 690 | return lease; |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 691 | } |
Simon Kelley | a6ebfac | 2013-03-06 20:52:35 +0000 | [diff] [blame] | 692 | |
| 693 | return NULL; |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 694 | } |
| 695 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 696 | struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr) |
| 697 | { |
| 698 | struct dhcp_lease *lease; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 699 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 700 | for (lease = leases; lease; lease = lease->next) |
| 701 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 702 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 703 | continue; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 704 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 705 | if (is_same_net6(&lease->addr6, net, prefix) && |
| 706 | (prefix == 128 || addr6part(&lease->addr6) == addr)) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 707 | return lease; |
| 708 | } |
| 709 | |
| 710 | return NULL; |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /* Find largest assigned address in context */ |
| 714 | u64 lease_find_max_addr6(struct dhcp_context *context) |
| 715 | { |
| 716 | struct dhcp_lease *lease; |
| 717 | u64 addr = addr6part(&context->start6); |
| 718 | |
| 719 | if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY))) |
| 720 | for (lease = leases; lease; lease = lease->next) |
| 721 | { |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 722 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
| 723 | continue; |
Simon Kelley | 6caacac | 2012-02-15 21:58:33 +0000 | [diff] [blame] | 724 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 725 | if (is_same_net6(&lease->addr6, &context->start6, 64) && |
| 726 | addr6part(&lease->addr6) > addr6part(&context->start6) && |
| 727 | addr6part(&lease->addr6) <= addr6part(&context->end6) && |
| 728 | addr6part(&lease->addr6) > addr) |
| 729 | addr = addr6part(&lease->addr6); |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | return addr; |
| 733 | } |
| 734 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 735 | #endif |
| 736 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 737 | /* Find largest assigned address in context */ |
| 738 | struct in_addr lease_find_max_addr(struct dhcp_context *context) |
| 739 | { |
| 740 | struct dhcp_lease *lease; |
| 741 | struct in_addr addr = context->start; |
| 742 | |
| 743 | if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY))) |
| 744 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 745 | { |
| 746 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 747 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 748 | continue; |
| 749 | #endif |
| 750 | if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) && |
| 751 | ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) && |
| 752 | ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr))) |
| 753 | addr = lease->addr; |
| 754 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 755 | |
| 756 | return addr; |
| 757 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 758 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 759 | static struct dhcp_lease *lease_allocate(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 760 | { |
| 761 | struct dhcp_lease *lease; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 762 | if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 763 | return NULL; |
| 764 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 765 | memset(lease, 0, sizeof(struct dhcp_lease)); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 766 | lease->flags = LEASE_NEW; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 767 | lease->expires = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 768 | #ifdef HAVE_BROKEN_RTC |
| 769 | lease->length = 0xffffffff; /* illegal value */ |
| 770 | #endif |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 771 | lease->hwaddr_len = 256; /* illegal value */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 772 | lease->next = leases; |
| 773 | leases = lease; |
| 774 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 775 | file_dirty = 1; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 776 | leases_left--; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 777 | |
| 778 | return lease; |
| 779 | } |
| 780 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 781 | struct dhcp_lease *lease4_allocate(struct in_addr addr) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 782 | { |
| 783 | struct dhcp_lease *lease = lease_allocate(); |
Simon Kelley | 0b0a73c | 2013-04-11 14:07:02 +0100 | [diff] [blame] | 784 | if (lease) |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 785 | lease->addr = addr; |
| 786 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 787 | return lease; |
| 788 | } |
| 789 | |
| 790 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 791 | struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 792 | { |
| 793 | struct dhcp_lease *lease = lease_allocate(); |
Simon Kelley | 0b0a73c | 2013-04-11 14:07:02 +0100 | [diff] [blame] | 794 | |
| 795 | if (lease) |
| 796 | { |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 797 | lease->addr6 = *addrp; |
Simon Kelley | 0b0a73c | 2013-04-11 14:07:02 +0100 | [diff] [blame] | 798 | lease->flags |= lease_type; |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 799 | lease->iaid = 0; |
Simon Kelley | 0b0a73c | 2013-04-11 14:07:02 +0100 | [diff] [blame] | 800 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 801 | |
| 802 | return lease; |
| 803 | } |
| 804 | #endif |
| 805 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 806 | void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 807 | { |
Simon Kelley | dd0e0a3 | 2014-01-22 11:16:59 +0000 | [diff] [blame] | 808 | time_t exp; |
| 809 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 810 | if (len == 0xffffffff) |
| 811 | { |
| 812 | exp = 0; |
| 813 | len = 0; |
| 814 | } |
Simon Kelley | dd0e0a3 | 2014-01-22 11:16:59 +0000 | [diff] [blame] | 815 | else |
| 816 | { |
| 817 | exp = now + (time_t)len; |
| 818 | /* Check for 2038 overflow. Make the lease |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 819 | infinite in that case, as the least disruptive |
Simon Kelley | dd0e0a3 | 2014-01-22 11:16:59 +0000 | [diff] [blame] | 820 | thing we can do. */ |
| 821 | if (difftime(exp, now) <= 0.0) |
| 822 | exp = 0; |
| 823 | } |
| 824 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 825 | if (exp != lease->expires) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 826 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 827 | dns_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 828 | lease->expires = exp; |
| 829 | #ifndef HAVE_BROKEN_RTC |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 830 | lease->flags |= LEASE_AUX_CHANGED; |
| 831 | file_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 832 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 833 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 834 | |
| 835 | #ifdef HAVE_BROKEN_RTC |
| 836 | if (len != lease->length) |
| 837 | { |
| 838 | lease->length = len; |
Simon Kelley | b7f4020 | 2012-02-29 21:43:37 +0000 | [diff] [blame] | 839 | lease->flags |= LEASE_AUX_CHANGED; |
| 840 | file_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 841 | } |
| 842 | #endif |
| 843 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 844 | |
Simon Kelley | 89500e3 | 2013-09-20 16:29:20 +0100 | [diff] [blame] | 845 | #ifdef HAVE_DHCP6 |
| 846 | void lease_set_iaid(struct dhcp_lease *lease, int iaid) |
| 847 | { |
| 848 | if (lease->iaid != iaid) |
| 849 | { |
| 850 | lease->iaid = iaid; |
| 851 | lease->flags |= LEASE_CHANGED; |
| 852 | } |
| 853 | } |
| 854 | #endif |
| 855 | |
Nicolas Cavallari | 64bcff1 | 2015-04-28 21:55:18 +0100 | [diff] [blame] | 856 | void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr, |
| 857 | const unsigned char *clid, int hw_len, int hw_type, |
| 858 | int clid_len, time_t now, int force) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 859 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 860 | #ifdef HAVE_DHCP6 |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 861 | int change = force; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 862 | lease->flags |= LEASE_HAVE_HWADDR; |
| 863 | #endif |
| 864 | |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 865 | (void)force; |
Vladislav Grishenko | 408c368 | 2013-09-24 16:18:49 +0100 | [diff] [blame] | 866 | (void)now; |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 867 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 868 | if (hw_len != lease->hwaddr_len || |
| 869 | hw_type != lease->hwaddr_type || |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 870 | (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 871 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 872 | if (hw_len != 0) |
| 873 | memcpy(lease->hwaddr, hwaddr, hw_len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 874 | lease->hwaddr_len = hw_len; |
| 875 | lease->hwaddr_type = hw_type; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 876 | lease->flags |= LEASE_CHANGED; |
| 877 | file_dirty = 1; /* run script on change */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 878 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 879 | |
| 880 | /* only update clid when one is available, stops packets |
| 881 | without a clid removing the record. Lease init uses |
| 882 | clid_len == 0 for no clid. */ |
| 883 | if (clid_len != 0 && clid) |
| 884 | { |
| 885 | if (!lease->clid) |
| 886 | lease->clid_len = 0; |
| 887 | |
| 888 | if (lease->clid_len != clid_len) |
| 889 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 890 | lease->flags |= LEASE_AUX_CHANGED; |
| 891 | file_dirty = 1; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 892 | free(lease->clid); |
| 893 | if (!(lease->clid = whine_malloc(clid_len))) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 894 | return; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 895 | #ifdef HAVE_DHCP6 |
| 896 | change = 1; |
| 897 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 898 | } |
| 899 | else if (memcmp(lease->clid, clid, clid_len) != 0) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 900 | { |
| 901 | lease->flags |= LEASE_AUX_CHANGED; |
| 902 | file_dirty = 1; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 903 | #ifdef HAVE_DHCP6 |
| 904 | change = 1; |
| 905 | #endif |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 906 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 907 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 908 | lease->clid_len = clid_len; |
| 909 | memcpy(lease->clid, clid, clid_len); |
| 910 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 911 | |
| 912 | #ifdef HAVE_DHCP6 |
| 913 | if (change) |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 914 | slaac_add_addrs(lease, now, force); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 915 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 918 | static void kill_name(struct dhcp_lease *lease) |
| 919 | { |
| 920 | /* run script to say we lost our old name */ |
| 921 | |
| 922 | /* this shouldn't happen unless updates are very quick and the |
| 923 | script very slow, we just avoid a memory leak if it does. */ |
| 924 | free(lease->old_hostname); |
| 925 | |
| 926 | /* If we know the fqdn, pass that. The helper will derive the |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 927 | unqualified name from it, free the unqualified name here. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 928 | |
| 929 | if (lease->fqdn) |
| 930 | { |
| 931 | lease->old_hostname = lease->fqdn; |
| 932 | free(lease->hostname); |
| 933 | } |
| 934 | else |
| 935 | lease->old_hostname = lease->hostname; |
| 936 | |
| 937 | lease->hostname = lease->fqdn = NULL; |
| 938 | } |
| 939 | |
Nicolas Cavallari | 64bcff1 | 2015-04-28 21:55:18 +0100 | [diff] [blame] | 940 | void lease_set_hostname(struct dhcp_lease *lease, const char *name, int auth, char *domain, char *config_domain) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 941 | { |
| 942 | struct dhcp_lease *lease_tmp; |
| 943 | char *new_name = NULL, *new_fqdn = NULL; |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 944 | |
| 945 | if (config_domain && (!domain || !hostname_isequal(domain, config_domain))) |
| 946 | my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 947 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 948 | if (lease->hostname && name && hostname_isequal(lease->hostname, name)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 949 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 950 | if (auth) |
| 951 | lease->flags |= LEASE_AUTH_NAME; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 952 | return; |
| 953 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 954 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 955 | if (!name && !lease->hostname) |
| 956 | return; |
| 957 | |
| 958 | /* If a machine turns up on a new net without dropping the old lease, |
| 959 | or two machines claim the same name, then we end up with two interfaces with |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 960 | the same name. Check for that here and remove the name from the old lease. |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 961 | Note that IPv6 leases are different. All the leases to the same DUID are |
| 962 | allowed the same name. |
| 963 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 964 | Don't allow a name from the client to override a name from dnsmasq config. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 965 | |
| 966 | if (name) |
| 967 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 968 | if ((new_name = whine_malloc(strlen(name) + 1))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 969 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 970 | strcpy(new_name, name); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 971 | if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 972 | { |
| 973 | strcpy(new_fqdn, name); |
| 974 | strcat(new_fqdn, "."); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 975 | strcat(new_fqdn, domain); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
| 979 | /* Depending on mode, we check either unqualified name or FQDN. */ |
| 980 | for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next) |
| 981 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 982 | if (option_bool(OPT_DHCP_FQDN)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 983 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 984 | if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 985 | continue; |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) ) |
| 990 | continue; |
| 991 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 992 | |
| 993 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 994 | { |
| 995 | if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA))) |
| 996 | continue; |
| 997 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 998 | /* another lease for the same DUID is OK for IPv6 */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 999 | if (lease->clid_len == lease_tmp->clid_len && |
| 1000 | lease->clid && lease_tmp->clid && |
| 1001 | memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0) |
| 1002 | continue; |
| 1003 | } |
| 1004 | else if (lease_tmp->flags & (LEASE_TA | LEASE_NA)) |
| 1005 | continue; |
| 1006 | |
| 1007 | if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1008 | { |
| 1009 | free(new_name); |
| 1010 | free(new_fqdn); |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | kill_name(lease_tmp); |
| 1015 | break; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if (lease->hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1020 | kill_name(lease); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1021 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1022 | lease->hostname = new_name; |
| 1023 | lease->fqdn = new_fqdn; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (auth) |
| 1026 | lease->flags |= LEASE_AUTH_NAME; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1027 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 1028 | file_dirty = 1; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1029 | dns_dirty = 1; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1030 | lease->flags |= LEASE_CHANGED; /* run script on change */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 1033 | void lease_set_interface(struct dhcp_lease *lease, int interface, time_t now) |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1034 | { |
Vladislav Grishenko | 408c368 | 2013-09-24 16:18:49 +0100 | [diff] [blame] | 1035 | (void)now; |
| 1036 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1037 | if (lease->last_interface == interface) |
| 1038 | return; |
| 1039 | |
| 1040 | lease->last_interface = interface; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1041 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 1042 | |
| 1043 | #ifdef HAVE_DHCP6 |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 1044 | slaac_add_addrs(lease, now, 0); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 1045 | #endif |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1048 | void rerun_scripts(void) |
| 1049 | { |
| 1050 | struct dhcp_lease *lease; |
| 1051 | |
| 1052 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1053 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1056 | /* deleted leases get transferred to the old_leases list. |
| 1057 | remove them here, after calling the lease change |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1058 | script. Also run the lease change script on new/modified leases. |
| 1059 | |
| 1060 | Return zero if nothing to do. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1061 | int do_script_run(time_t now) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1062 | { |
| 1063 | struct dhcp_lease *lease; |
| 1064 | |
Vladislav Grishenko | 408c368 | 2013-09-24 16:18:49 +0100 | [diff] [blame] | 1065 | (void)now; |
| 1066 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1067 | #ifdef HAVE_DBUS |
| 1068 | /* If we're going to be sending DBus signals, but the connection is not yet up, |
| 1069 | delay everything until it is. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 1070 | if (option_bool(OPT_DBUS) && !daemon->dbus) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1071 | return 0; |
| 1072 | #endif |
| 1073 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1074 | if (old_leases) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1075 | { |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1076 | lease = old_leases; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1077 | |
| 1078 | /* If the lease still has an old_hostname, do the "old" action on that first */ |
| 1079 | if (lease->old_hostname) |
| 1080 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1081 | #ifdef HAVE_SCRIPT |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1082 | queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); |
| 1083 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1084 | free(lease->old_hostname); |
| 1085 | lease->old_hostname = NULL; |
| 1086 | return 1; |
| 1087 | } |
| 1088 | else |
| 1089 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 1090 | #ifdef HAVE_DHCP6 |
| 1091 | struct slaac_address *slaac, *tmp; |
| 1092 | for (slaac = lease->slaac_address; slaac; slaac = tmp) |
| 1093 | { |
| 1094 | tmp = slaac->next; |
| 1095 | free(slaac); |
| 1096 | } |
| 1097 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1098 | kill_name(lease); |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1099 | #ifdef HAVE_SCRIPT |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1100 | queue_script(ACTION_DEL, lease, lease->old_hostname, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1101 | #endif |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1102 | #ifdef HAVE_DBUS |
| 1103 | emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname); |
| 1104 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1105 | old_leases = lease->next; |
| 1106 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1107 | free(lease->old_hostname); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1108 | free(lease->clid); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1109 | free(lease->extradata); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1110 | free(lease); |
| 1111 | |
| 1112 | return 1; |
| 1113 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1114 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1115 | |
| 1116 | /* make sure we announce the loss of a hostname before its new location. */ |
| 1117 | for (lease = leases; lease; lease = lease->next) |
| 1118 | if (lease->old_hostname) |
| 1119 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1120 | #ifdef HAVE_SCRIPT |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1121 | queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); |
| 1122 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1123 | free(lease->old_hostname); |
| 1124 | lease->old_hostname = NULL; |
| 1125 | return 1; |
| 1126 | } |
| 1127 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1128 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1129 | if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) || |
| 1130 | ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO))) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1131 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1132 | #ifdef HAVE_SCRIPT |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1133 | queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1134 | lease->fqdn ? lease->fqdn : lease->hostname, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1135 | #endif |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1136 | #ifdef HAVE_DBUS |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1137 | emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1138 | lease->fqdn ? lease->fqdn : lease->hostname); |
| 1139 | #endif |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1140 | lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1141 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1142 | /* this is used for the "add" call, then junked, since they're not in the database */ |
| 1143 | free(lease->extradata); |
| 1144 | lease->extradata = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1145 | |
| 1146 | return 1; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1147 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1148 | |
| 1149 | return 0; /* nothing to do */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1150 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1151 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 1152 | #ifdef HAVE_SCRIPT |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 1153 | /* delim == -1 -> delim = 0, but embedded 0s, creating extra records, are OK. */ |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 1154 | void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim) |
| 1155 | { |
| 1156 | unsigned int i; |
| 1157 | |
Simon Kelley | a93bd4b | 2016-03-01 18:58:01 +0000 | [diff] [blame] | 1158 | if (delim == -1) |
| 1159 | delim = 0; |
| 1160 | else |
Josh Soref | 730c674 | 2017-02-06 16:14:04 +0000 | [diff] [blame] | 1161 | /* check for embedded NULLs */ |
Simon Kelley | a93bd4b | 2016-03-01 18:58:01 +0000 | [diff] [blame] | 1162 | for (i = 0; i < len; i++) |
| 1163 | if (data[i] == 0) |
| 1164 | { |
| 1165 | len = i; |
| 1166 | break; |
| 1167 | } |
| 1168 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 1169 | if ((lease->extradata_size - lease->extradata_len) < (len + 1)) |
| 1170 | { |
| 1171 | size_t newsz = lease->extradata_len + len + 100; |
| 1172 | unsigned char *new = whine_malloc(newsz); |
| 1173 | |
| 1174 | if (!new) |
| 1175 | return; |
| 1176 | |
| 1177 | if (lease->extradata) |
| 1178 | { |
| 1179 | memcpy(new, lease->extradata, lease->extradata_len); |
| 1180 | free(lease->extradata); |
| 1181 | } |
| 1182 | |
| 1183 | lease->extradata = new; |
| 1184 | lease->extradata_size = newsz; |
| 1185 | } |
| 1186 | |
| 1187 | if (len != 0) |
| 1188 | memcpy(lease->extradata + lease->extradata_len, data, len); |
| 1189 | lease->extradata[lease->extradata_len + len] = delim; |
| 1190 | lease->extradata_len += len + 1; |
| 1191 | } |
| 1192 | #endif |
| 1193 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1194 | #endif |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1195 | |
| 1196 | |
| 1197 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1198 | |