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