Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 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 | |
| 118 | hw_type = atoi(s); |
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 | |
| 348 | static int find_interface_v4(struct in_addr local, int if_index, |
| 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 | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 353 | (void) broadcast; |
| 354 | (void) vparam; |
| 355 | |
| 356 | for (lease = leases; lease; lease = lease->next) |
| 357 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
| 358 | if (is_same_net(local, lease->addr, netmask)) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 359 | lease_set_interface(lease, if_index, *((time_t *)vparam)); |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 360 | |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | #ifdef HAVE_DHCP6 |
| 365 | static int find_interface_v6(struct in6_addr *local, int prefix, |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 366 | int scope, int if_index, int flags, |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 367 | int preferred, int valid, void *vparam) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 368 | { |
| 369 | struct dhcp_lease *lease; |
| 370 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 371 | (void)scope; |
Simon Kelley | bad7b87 | 2012-12-20 22:00:39 +0000 | [diff] [blame] | 372 | (void)flags; |
Simon Kelley | 1f77693 | 2012-12-16 19:46:08 +0000 | [diff] [blame] | 373 | (void)preferred; |
| 374 | (void)valid; |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 375 | |
| 376 | for (lease = leases; lease; lease = lease->next) |
| 377 | if ((lease->flags & (LEASE_TA | LEASE_NA))) |
| 378 | if (is_same_net6(local, (struct in6_addr *)&lease->hwaddr, prefix)) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 379 | lease_set_interface(lease, if_index, *((time_t *)vparam)); |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 380 | |
| 381 | return 1; |
| 382 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 383 | |
| 384 | void lease_ping_reply(struct in6_addr *sender, unsigned char *packet, char *interface) |
| 385 | { |
Simon Kelley | 5ef3327 | 2012-03-30 15:10:28 +0100 | [diff] [blame] | 386 | /* We may be doing RA but not DHCPv4, in which case the lease |
| 387 | database may not exist and we have nothing to do anyway */ |
| 388 | if (daemon->dhcp) |
| 389 | slaac_ping_reply(sender, packet, interface, leases); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Simon Kelley | 0c05024 | 2012-12-22 22:13:19 +0000 | [diff] [blame] | 392 | void lease_update_slaac(time_t now) |
| 393 | { |
| 394 | /* Called when we contruct a new RA-names context, to add putative |
| 395 | new SLAAC addresses to existing leases. */ |
| 396 | |
| 397 | struct dhcp_lease *lease; |
| 398 | |
| 399 | if (daemon->dhcp) |
| 400 | for (lease = leases; lease; lease = lease->next) |
| 401 | slaac_add_addrs(lease, now, 0); |
| 402 | } |
| 403 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 404 | #endif |
| 405 | |
| 406 | |
| 407 | /* Find interfaces associated with leases at start-up. This gets updated as |
| 408 | we do DHCP transactions, but information about directly-connected subnets |
| 409 | is useful from scrips and necessary for determining SLAAC addresses from |
| 410 | start-time. */ |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 411 | void lease_find_interfaces(time_t now) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 412 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 413 | iface_enumerate(AF_INET, &now, find_interface_v4); |
| 414 | #ifdef HAVE_DHCP6 |
| 415 | iface_enumerate(AF_INET6, &now, find_interface_v6); |
Simon Kelley | 8b37270 | 2012-03-09 17:45:10 +0000 | [diff] [blame] | 416 | |
| 417 | /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */ |
| 418 | if (!daemon->duid && daemon->dhcp6) |
| 419 | { |
| 420 | file_dirty = 1; |
| 421 | make_duid(now); |
| 422 | } |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 423 | #endif |
| 424 | } |
| 425 | |
| 426 | |
| 427 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 428 | void lease_update_dns(int force) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 429 | { |
| 430 | struct dhcp_lease *lease; |
| 431 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 432 | if (daemon->port != 0 && (dns_dirty || force)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 433 | { |
Simon Kelley | 8ff5567 | 2012-12-09 21:09:01 +0000 | [diff] [blame] | 434 | #ifndef HAVE_BROKEN_RTC |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 435 | /* force transfer to authoritative secondaries */ |
| 436 | daemon->soa_sn++; |
Simon Kelley | 8ff5567 | 2012-12-09 21:09:01 +0000 | [diff] [blame] | 437 | #endif |
Simon Kelley | e1ff419 | 2012-12-09 17:08:47 +0000 | [diff] [blame] | 438 | |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 439 | cache_unhash_dhcp(); |
| 440 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 441 | for (lease = leases; lease; lease = lease->next) |
| 442 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 443 | int prot = AF_INET; |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 444 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 445 | #ifdef HAVE_DHCP6 |
| 446 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 447 | prot = AF_INET6; |
Simon Kelley | f444cdd | 2012-03-07 10:15:57 +0000 | [diff] [blame] | 448 | else if (lease->hostname || lease->fqdn) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 449 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 450 | struct slaac_address *slaac; |
| 451 | |
| 452 | for (slaac = lease->slaac_address; slaac; slaac = slaac->next) |
| 453 | if (slaac->backoff == 0) |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 454 | { |
Simon Kelley | 801ca9a | 2012-03-06 19:30:17 +0000 | [diff] [blame] | 455 | if (lease->fqdn) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 456 | 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] | 457 | if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 458 | 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] | 459 | } |
| 460 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 461 | #endif |
| 462 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 463 | if (lease->fqdn) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 464 | cache_add_dhcp_entry(lease->fqdn, prot, |
| 465 | prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr, |
| 466 | lease->expires); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 467 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 468 | if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 469 | cache_add_dhcp_entry(lease->hostname, prot, |
| 470 | prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr, |
| 471 | lease->expires); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | dns_dirty = 0; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | void lease_prune(struct dhcp_lease *target, time_t now) |
| 479 | { |
| 480 | struct dhcp_lease *lease, *tmp, **up; |
| 481 | |
| 482 | for (lease = leases, up = &leases; lease; lease = tmp) |
| 483 | { |
| 484 | tmp = lease->next; |
| 485 | if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target) |
| 486 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 487 | file_dirty = 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 488 | if (lease->hostname) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 489 | dns_dirty = 1; |
| 490 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 491 | *up = lease->next; /* unlink */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 492 | |
| 493 | /* Put on old_leases list 'till we |
| 494 | can run the script */ |
| 495 | lease->next = old_leases; |
| 496 | old_leases = lease; |
| 497 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 498 | leases_left++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 499 | } |
| 500 | else |
| 501 | up = &lease->next; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 506 | 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] | 507 | unsigned char *clid, int clid_len) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 508 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 509 | struct dhcp_lease *lease; |
| 510 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 511 | if (clid) |
| 512 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 513 | { |
| 514 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 515 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 516 | continue; |
| 517 | #endif |
| 518 | if (lease->clid && clid_len == lease->clid_len && |
| 519 | memcmp(clid, lease->clid, clid_len) == 0) |
| 520 | return lease; |
| 521 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 522 | |
| 523 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 524 | { |
| 525 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 526 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 527 | continue; |
| 528 | #endif |
| 529 | if ((!lease->clid || !clid) && |
| 530 | hw_len != 0 && |
| 531 | lease->hwaddr_len == hw_len && |
| 532 | lease->hwaddr_type == hw_type && |
| 533 | memcmp(hwaddr, lease->hwaddr, hw_len) == 0) |
| 534 | return lease; |
| 535 | } |
| 536 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 537 | return NULL; |
| 538 | } |
| 539 | |
| 540 | struct dhcp_lease *lease_find_by_addr(struct in_addr addr) |
| 541 | { |
| 542 | struct dhcp_lease *lease; |
| 543 | |
| 544 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 545 | { |
| 546 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 547 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 548 | continue; |
| 549 | #endif |
| 550 | if (lease->addr.s_addr == addr.s_addr) |
| 551 | return lease; |
| 552 | } |
| 553 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 554 | return NULL; |
| 555 | } |
| 556 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 557 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 558 | /* addr or clid may be NULL for "don't care, both NULL resets "USED" flags both |
| 559 | set activates USED check */ |
| 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 | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 570 | if (clid && addr && (lease->flags & LEASE_USED)) |
| 571 | continue; |
| 572 | |
| 573 | if (addr && memcmp(lease->hwaddr, addr, IN6ADDRSZ) != 0) |
| 574 | continue; |
| 575 | |
| 576 | if (clid && |
| 577 | (clid_len != lease->clid_len || |
| 578 | memcmp(clid, lease->clid, clid_len) != 0)) |
| 579 | continue; |
| 580 | |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 581 | lease->flags |= LEASE_USED; |
| 582 | return lease; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | return NULL; |
| 586 | } |
| 587 | |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 588 | void lease6_filter(int lease_type, int iaid, struct dhcp_context *context) |
| 589 | { |
| 590 | struct dhcp_lease *lease; |
| 591 | |
| 592 | for (lease = leases; lease; lease = lease->next) |
| 593 | { |
| 594 | /* reset "USED flag */ |
| 595 | lease->flags &= ~LEASE_USED; |
| 596 | |
| 597 | if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid) |
| 598 | continue; |
| 599 | |
| 600 | /* leases on the wrong interface get filtered out here */ |
Simon Kelley | b269221 | 2012-09-16 22:15:56 +0100 | [diff] [blame] | 601 | if (!is_addr_in_context6(context, (struct in6_addr *)&lease->hwaddr)) |
Simon Kelley | 8b46061 | 2012-09-08 21:47:28 +0100 | [diff] [blame] | 602 | lease->flags |= LEASE_USED; |
| 603 | } |
| 604 | } |
| 605 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 606 | struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr) |
| 607 | { |
| 608 | struct dhcp_lease *lease; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 609 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 610 | for (lease = leases; lease; lease = lease->next) |
| 611 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 612 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 613 | continue; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 614 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 615 | if (is_same_net6((struct in6_addr *)lease->hwaddr, net, prefix) && |
| 616 | (prefix == 128 || addr6part((struct in6_addr *)lease->hwaddr) == addr)) |
| 617 | return lease; |
| 618 | } |
| 619 | |
| 620 | return NULL; |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* Find largest assigned address in context */ |
| 624 | u64 lease_find_max_addr6(struct dhcp_context *context) |
| 625 | { |
| 626 | struct dhcp_lease *lease; |
| 627 | u64 addr = addr6part(&context->start6); |
| 628 | |
| 629 | if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY))) |
| 630 | for (lease = leases; lease; lease = lease->next) |
| 631 | { |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 632 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
| 633 | continue; |
Simon Kelley | 6caacac | 2012-02-15 21:58:33 +0000 | [diff] [blame] | 634 | |
Simon Kelley | 0793380 | 2012-02-14 20:55:25 +0000 | [diff] [blame] | 635 | if (is_same_net6((struct in6_addr *)lease->hwaddr, &context->start6, 64) && |
| 636 | addr6part((struct in6_addr *)lease->hwaddr) > addr6part(&context->start6) && |
| 637 | addr6part((struct in6_addr *)lease->hwaddr) <= addr6part(&context->end6) && |
| 638 | addr6part((struct in6_addr *)lease->hwaddr) > addr) |
| 639 | addr = addr6part((struct in6_addr *)lease->hwaddr); |
| 640 | } |
| 641 | |
| 642 | return addr; |
| 643 | } |
| 644 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 645 | #endif |
| 646 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 647 | /* Find largest assigned address in context */ |
| 648 | struct in_addr lease_find_max_addr(struct dhcp_context *context) |
| 649 | { |
| 650 | struct dhcp_lease *lease; |
| 651 | struct in_addr addr = context->start; |
| 652 | |
| 653 | if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY))) |
| 654 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 655 | { |
| 656 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 657 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 658 | continue; |
| 659 | #endif |
| 660 | if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) && |
| 661 | ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) && |
| 662 | ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr))) |
| 663 | addr = lease->addr; |
| 664 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 665 | |
| 666 | return addr; |
| 667 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 668 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 669 | static struct dhcp_lease *lease_allocate(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 670 | { |
| 671 | struct dhcp_lease *lease; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 672 | if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 673 | return NULL; |
| 674 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 675 | memset(lease, 0, sizeof(struct dhcp_lease)); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 676 | lease->flags = LEASE_NEW; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 677 | lease->expires = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 678 | #ifdef HAVE_BROKEN_RTC |
| 679 | lease->length = 0xffffffff; /* illegal value */ |
| 680 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 681 | lease->next = leases; |
| 682 | leases = lease; |
| 683 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 684 | file_dirty = 1; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 685 | leases_left--; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 686 | |
| 687 | return lease; |
| 688 | } |
| 689 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 690 | struct dhcp_lease *lease4_allocate(struct in_addr addr) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 691 | { |
| 692 | struct dhcp_lease *lease = lease_allocate(); |
| 693 | lease->addr = addr; |
| 694 | lease->hwaddr_len = 256; /* illegal value */ |
| 695 | |
| 696 | return lease; |
| 697 | } |
| 698 | |
| 699 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 700 | struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 701 | { |
| 702 | struct dhcp_lease *lease = lease_allocate(); |
| 703 | memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 704 | lease->flags |= lease_type; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 705 | |
| 706 | return lease; |
| 707 | } |
| 708 | #endif |
| 709 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 710 | 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] | 711 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 712 | time_t exp = now + (time_t)len; |
| 713 | |
| 714 | if (len == 0xffffffff) |
| 715 | { |
| 716 | exp = 0; |
| 717 | len = 0; |
| 718 | } |
| 719 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 720 | if (exp != lease->expires) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 721 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 722 | dns_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 723 | lease->expires = exp; |
| 724 | #ifndef HAVE_BROKEN_RTC |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 725 | lease->flags |= LEASE_AUX_CHANGED; |
| 726 | file_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 727 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 728 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 729 | |
| 730 | #ifdef HAVE_BROKEN_RTC |
| 731 | if (len != lease->length) |
| 732 | { |
| 733 | lease->length = len; |
Simon Kelley | b7f4020 | 2012-02-29 21:43:37 +0000 | [diff] [blame] | 734 | lease->flags |= LEASE_AUX_CHANGED; |
| 735 | file_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 736 | } |
| 737 | #endif |
| 738 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 739 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 740 | void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr, |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 741 | unsigned char *clid, int hw_len, int hw_type, int clid_len, |
| 742 | time_t now, int force) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 743 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 744 | #ifdef HAVE_DHCP6 |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 745 | int change = force; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 746 | lease->flags |= LEASE_HAVE_HWADDR; |
| 747 | #endif |
| 748 | |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 749 | (void)force; |
| 750 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 751 | if (hw_len != lease->hwaddr_len || |
| 752 | hw_type != lease->hwaddr_type || |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 753 | (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 754 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 755 | if (hw_len != 0) |
| 756 | memcpy(lease->hwaddr, hwaddr, hw_len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 757 | lease->hwaddr_len = hw_len; |
| 758 | lease->hwaddr_type = hw_type; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 759 | lease->flags |= LEASE_CHANGED; |
| 760 | file_dirty = 1; /* run script on change */ |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 761 | #ifdef HAVE_DHCP6 |
| 762 | change = 1; |
| 763 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 764 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 765 | |
| 766 | /* only update clid when one is available, stops packets |
| 767 | without a clid removing the record. Lease init uses |
| 768 | clid_len == 0 for no clid. */ |
| 769 | if (clid_len != 0 && clid) |
| 770 | { |
| 771 | if (!lease->clid) |
| 772 | lease->clid_len = 0; |
| 773 | |
| 774 | if (lease->clid_len != clid_len) |
| 775 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 776 | lease->flags |= LEASE_AUX_CHANGED; |
| 777 | file_dirty = 1; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 778 | free(lease->clid); |
| 779 | if (!(lease->clid = whine_malloc(clid_len))) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 780 | return; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 781 | #ifdef HAVE_DHCP6 |
| 782 | change = 1; |
| 783 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 784 | } |
| 785 | else if (memcmp(lease->clid, clid, clid_len) != 0) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 786 | { |
| 787 | lease->flags |= LEASE_AUX_CHANGED; |
| 788 | file_dirty = 1; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 789 | #ifdef HAVE_DHCP6 |
| 790 | change = 1; |
| 791 | #endif |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 792 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 793 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 794 | lease->clid_len = clid_len; |
| 795 | memcpy(lease->clid, clid, clid_len); |
| 796 | } |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 797 | |
| 798 | #ifdef HAVE_DHCP6 |
| 799 | if (change) |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 800 | slaac_add_addrs(lease, now, force); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 801 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 804 | static void kill_name(struct dhcp_lease *lease) |
| 805 | { |
| 806 | /* run script to say we lost our old name */ |
| 807 | |
| 808 | /* this shouldn't happen unless updates are very quick and the |
| 809 | script very slow, we just avoid a memory leak if it does. */ |
| 810 | free(lease->old_hostname); |
| 811 | |
| 812 | /* If we know the fqdn, pass that. The helper will derive the |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 813 | unqualified name from it, free the unqualified name here. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 814 | |
| 815 | if (lease->fqdn) |
| 816 | { |
| 817 | lease->old_hostname = lease->fqdn; |
| 818 | free(lease->hostname); |
| 819 | } |
| 820 | else |
| 821 | lease->old_hostname = lease->hostname; |
| 822 | |
| 823 | lease->hostname = lease->fqdn = NULL; |
| 824 | } |
| 825 | |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 826 | 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] | 827 | { |
| 828 | struct dhcp_lease *lease_tmp; |
| 829 | char *new_name = NULL, *new_fqdn = NULL; |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 830 | |
| 831 | if (config_domain && (!domain || !hostname_isequal(domain, config_domain))) |
| 832 | 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] | 833 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 834 | if (lease->hostname && name && hostname_isequal(lease->hostname, name)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 835 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 836 | if (auth) |
| 837 | lease->flags |= LEASE_AUTH_NAME; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 838 | return; |
| 839 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 840 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 841 | if (!name && !lease->hostname) |
| 842 | return; |
| 843 | |
| 844 | /* If a machine turns up on a new net without dropping the old lease, |
| 845 | 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] | 846 | 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] | 847 | Note that IPv6 leases are different. All the leases to the same DUID are |
| 848 | allowed the same name. |
| 849 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 850 | 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] | 851 | |
| 852 | if (name) |
| 853 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 854 | if ((new_name = whine_malloc(strlen(name) + 1))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 855 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 856 | strcpy(new_name, name); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 857 | if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 858 | { |
| 859 | strcpy(new_fqdn, name); |
| 860 | strcat(new_fqdn, "."); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 861 | strcat(new_fqdn, domain); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | |
| 865 | /* Depending on mode, we check either unqualified name or FQDN. */ |
| 866 | for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next) |
| 867 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 868 | if (option_bool(OPT_DHCP_FQDN)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 869 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 870 | 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] | 871 | continue; |
| 872 | } |
| 873 | else |
| 874 | { |
| 875 | if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) ) |
| 876 | continue; |
| 877 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 878 | |
| 879 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 880 | { |
| 881 | if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA))) |
| 882 | continue; |
| 883 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 884 | /* another lease for the same DUID is OK for IPv6 */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 885 | if (lease->clid_len == lease_tmp->clid_len && |
| 886 | lease->clid && lease_tmp->clid && |
| 887 | memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0) |
| 888 | continue; |
| 889 | } |
| 890 | else if (lease_tmp->flags & (LEASE_TA | LEASE_NA)) |
| 891 | continue; |
| 892 | |
| 893 | if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 894 | { |
| 895 | free(new_name); |
| 896 | free(new_fqdn); |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | kill_name(lease_tmp); |
| 901 | break; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
| 905 | if (lease->hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 906 | kill_name(lease); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 907 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 908 | lease->hostname = new_name; |
| 909 | lease->fqdn = new_fqdn; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 910 | |
| 911 | if (auth) |
| 912 | lease->flags |= LEASE_AUTH_NAME; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 913 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 914 | file_dirty = 1; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 915 | dns_dirty = 1; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 916 | lease->flags |= LEASE_CHANGED; /* run script on change */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 919 | 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] | 920 | { |
| 921 | if (lease->last_interface == interface) |
| 922 | return; |
| 923 | |
| 924 | lease->last_interface = interface; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 925 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 926 | |
| 927 | #ifdef HAVE_DHCP6 |
Simon Kelley | a9ab732 | 2012-04-28 11:29:37 +0100 | [diff] [blame] | 928 | slaac_add_addrs(lease, now, 0); |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 929 | #endif |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 932 | void rerun_scripts(void) |
| 933 | { |
| 934 | struct dhcp_lease *lease; |
| 935 | |
| 936 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 937 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 940 | /* deleted leases get transferred to the old_leases list. |
| 941 | remove them here, after calling the lease change |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 942 | script. Also run the lease change script on new/modified leases. |
| 943 | |
| 944 | Return zero if nothing to do. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 945 | int do_script_run(time_t now) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 946 | { |
| 947 | struct dhcp_lease *lease; |
| 948 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 949 | #ifdef HAVE_DBUS |
| 950 | /* If we're going to be sending DBus signals, but the connection is not yet up, |
| 951 | delay everything until it is. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 952 | if (option_bool(OPT_DBUS) && !daemon->dbus) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 953 | return 0; |
| 954 | #endif |
| 955 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 956 | if (old_leases) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 957 | { |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 958 | lease = old_leases; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 959 | |
| 960 | /* If the lease still has an old_hostname, do the "old" action on that first */ |
| 961 | if (lease->old_hostname) |
| 962 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 963 | #ifdef HAVE_SCRIPT |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 964 | queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); |
| 965 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 966 | free(lease->old_hostname); |
| 967 | lease->old_hostname = NULL; |
| 968 | return 1; |
| 969 | } |
| 970 | else |
| 971 | { |
Simon Kelley | 353ae4d | 2012-03-19 20:07:51 +0000 | [diff] [blame] | 972 | #ifdef HAVE_DHCP6 |
| 973 | struct slaac_address *slaac, *tmp; |
| 974 | for (slaac = lease->slaac_address; slaac; slaac = tmp) |
| 975 | { |
| 976 | tmp = slaac->next; |
| 977 | free(slaac); |
| 978 | } |
| 979 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 980 | kill_name(lease); |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 981 | #ifdef HAVE_SCRIPT |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 982 | queue_script(ACTION_DEL, lease, lease->old_hostname, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 983 | #endif |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 984 | #ifdef HAVE_DBUS |
| 985 | emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname); |
| 986 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 987 | old_leases = lease->next; |
| 988 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 989 | free(lease->old_hostname); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 990 | free(lease->clid); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 991 | free(lease->extradata); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 992 | free(lease); |
| 993 | |
| 994 | return 1; |
| 995 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 996 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 997 | |
| 998 | /* make sure we announce the loss of a hostname before its new location. */ |
| 999 | for (lease = leases; lease; lease = lease->next) |
| 1000 | if (lease->old_hostname) |
| 1001 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1002 | #ifdef HAVE_SCRIPT |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1003 | queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); |
| 1004 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1005 | free(lease->old_hostname); |
| 1006 | lease->old_hostname = NULL; |
| 1007 | return 1; |
| 1008 | } |
| 1009 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1010 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1011 | if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) || |
| 1012 | ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO))) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1013 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1014 | #ifdef HAVE_SCRIPT |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1015 | queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 1016 | lease->fqdn ? lease->fqdn : lease->hostname, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 1017 | #endif |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1018 | #ifdef HAVE_DBUS |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1019 | emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 1020 | lease->fqdn ? lease->fqdn : lease->hostname); |
| 1021 | #endif |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 1022 | lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1023 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 1024 | /* this is used for the "add" call, then junked, since they're not in the database */ |
| 1025 | free(lease->extradata); |
| 1026 | lease->extradata = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1027 | |
| 1028 | return 1; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1029 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 1030 | |
| 1031 | return 0; /* nothing to do */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1032 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1033 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 1034 | #ifdef HAVE_SCRIPT |
| 1035 | void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim) |
| 1036 | { |
| 1037 | unsigned int i; |
| 1038 | |
| 1039 | /* check for embeded NULLs */ |
| 1040 | for (i = 0; i < len; i++) |
| 1041 | if (data[i] == 0) |
| 1042 | { |
| 1043 | len = i; |
| 1044 | break; |
| 1045 | } |
| 1046 | |
| 1047 | if ((lease->extradata_size - lease->extradata_len) < (len + 1)) |
| 1048 | { |
| 1049 | size_t newsz = lease->extradata_len + len + 100; |
| 1050 | unsigned char *new = whine_malloc(newsz); |
| 1051 | |
| 1052 | if (!new) |
| 1053 | return; |
| 1054 | |
| 1055 | if (lease->extradata) |
| 1056 | { |
| 1057 | memcpy(new, lease->extradata, lease->extradata_len); |
| 1058 | free(lease->extradata); |
| 1059 | } |
| 1060 | |
| 1061 | lease->extradata = new; |
| 1062 | lease->extradata_size = newsz; |
| 1063 | } |
| 1064 | |
| 1065 | if (len != 0) |
| 1066 | memcpy(lease->extradata + lease->extradata_len, data, len); |
| 1067 | lease->extradata[lease->extradata_len + len] = delim; |
| 1068 | lease->extradata_len += len + 1; |
| 1069 | } |
| 1070 | #endif |
| 1071 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 1072 | #endif |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 1073 | |
| 1074 | |
| 1075 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 1076 | |