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