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