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 | |
| 100 | lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet, hw_len, hw_type, clid_len); |
| 101 | |
| 102 | if (strcmp(daemon->dhcp_buff, "*") != 0) |
| 103 | lease_set_hostname(lease, daemon->dhcp_buff, 0, get_domain(lease->addr), NULL); |
| 104 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 105 | #ifdef HAVE_DHCP6 |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 106 | else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr.addr6)) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 107 | { |
| 108 | char *s = daemon->dhcp_buff2; |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 109 | int lease_type = LEASE_NA; |
| 110 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 111 | if (s[0] == 'T') |
| 112 | { |
| 113 | lease_type = LEASE_TA; |
| 114 | s++; |
| 115 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 116 | |
| 117 | hw_type = atoi(s); |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 118 | |
| 119 | if ((lease = lease6_allocate(&addr.addr.addr6, lease_type))) |
| 120 | { |
| 121 | lease_set_hwaddr(lease, NULL, (unsigned char *)daemon->packet, 0, hw_type, clid_len); |
| 122 | |
| 123 | if (strcmp(daemon->dhcp_buff, "*") != 0) |
| 124 | lease_set_hostname(lease, daemon->dhcp_buff, 0, get_domain6((struct in6_addr *)lease->hwaddr), NULL); |
| 125 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 126 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 127 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 128 | else |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 129 | break; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 130 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 131 | if (!lease) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 132 | die (_("too many stored leases"), NULL, EC_MISC); |
| 133 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 134 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 135 | if (ei != 0) |
| 136 | lease->expires = (time_t)ei + now; |
| 137 | else |
| 138 | lease->expires = (time_t)0; |
| 139 | lease->length = ei; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 140 | #else |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 141 | /* strictly time_t is opaque, but this hack should work on all sane systems, |
| 142 | even when sizeof(time_t) == 8 */ |
| 143 | lease->expires = (time_t)ei; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 144 | #endif |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 145 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 146 | /* set these correctly: the "old" events are generated later from |
| 147 | the startup synthesised SIGHUP. */ |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 148 | lease->flags &= ~(LEASE_NEW | LEASE_CHANGED); |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 149 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 150 | |
| 151 | #ifdef HAVE_DHCP6 |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 152 | /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */ |
| 153 | if (!daemon->duid && daemon->dhcp6) |
| 154 | make_duid(now); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 155 | #endif |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 156 | |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 157 | #ifdef HAVE_SCRIPT |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 158 | if (!daemon->lease_stream) |
| 159 | { |
| 160 | int rc = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 161 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 162 | /* shell returns 127 for "command not found", 126 for bad permissions. */ |
| 163 | if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126) |
| 164 | { |
| 165 | if (WEXITSTATUS(rc) == 127) |
| 166 | errno = ENOENT; |
| 167 | else if (WEXITSTATUS(rc) == 126) |
| 168 | errno = EACCES; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 169 | 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] | 170 | } |
| 171 | |
| 172 | if (WEXITSTATUS(rc) != 0) |
| 173 | { |
| 174 | sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc)); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 175 | 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] | 176 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 177 | } |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 178 | #endif |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 179 | |
| 180 | /* Some leases may have expired */ |
| 181 | file_dirty = 0; |
| 182 | lease_prune(NULL, now); |
| 183 | dns_dirty = 1; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 186 | void lease_update_from_configs(void) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 187 | { |
| 188 | /* changes to the config may change current leases. */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 189 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 190 | struct dhcp_lease *lease; |
| 191 | struct dhcp_config *config; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 192 | char *name; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 193 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 194 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 195 | if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len, |
| 196 | lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) && |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 197 | (config->flags & CONFIG_NAME) && |
| 198 | (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr)) |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 199 | lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 200 | else if ((name = host_from_dns(lease->addr))) |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 201 | 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] | 202 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 203 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 204 | static void ourprintf(int *errp, char *format, ...) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 205 | { |
| 206 | va_list ap; |
| 207 | |
| 208 | va_start(ap, format); |
| 209 | if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0) |
| 210 | *errp = errno; |
| 211 | va_end(ap); |
| 212 | } |
| 213 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 214 | void lease_update_file(time_t now) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 215 | { |
| 216 | struct dhcp_lease *lease; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 217 | time_t next_event; |
| 218 | int i, err = 0; |
| 219 | |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 220 | if (file_dirty != 0 && daemon->lease_stream) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 221 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 222 | errno = 0; |
| 223 | rewind(daemon->lease_stream); |
| 224 | if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 225 | err = errno; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 226 | |
| 227 | for (lease = leases; lease; lease = lease->next) |
| 228 | { |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 229 | |
| 230 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 231 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 232 | continue; |
| 233 | #endif |
| 234 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 235 | #ifdef HAVE_BROKEN_RTC |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 236 | ourprintf(&err, "%u ", lease->length); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 237 | #else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 238 | ourprintf(&err, "%lu ", (unsigned long)lease->expires); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 239 | #endif |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 240 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 241 | if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 242 | ourprintf(&err, "%.2x-", lease->hwaddr_type); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 243 | for (i = 0; i < lease->hwaddr_len; i++) |
| 244 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 245 | ourprintf(&err, "%.2x", lease->hwaddr[i]); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 246 | if (i != lease->hwaddr_len - 1) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 247 | ourprintf(&err, ":"); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 248 | } |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 249 | |
| 250 | inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 251 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 252 | ourprintf(&err, " %s ", daemon->addrbuff); |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 253 | ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*"); |
| 254 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 255 | if (lease->clid && lease->clid_len != 0) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 256 | { |
| 257 | for (i = 0; i < lease->clid_len - 1; i++) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 258 | ourprintf(&err, "%.2x:", lease->clid[i]); |
| 259 | ourprintf(&err, "%.2x\n", lease->clid[i]); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 260 | } |
| 261 | else |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 262 | ourprintf(&err, "*\n"); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 263 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 264 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 265 | #ifdef HAVE_DHCP6 |
| 266 | if (daemon->duid) |
| 267 | { |
| 268 | ourprintf(&err, "duid "); |
| 269 | for (i = 0; i < daemon->duid_len - 1; i++) |
| 270 | ourprintf(&err, "%.2x:", daemon->duid[i]); |
| 271 | ourprintf(&err, "%.2x\n", daemon->duid[i]); |
| 272 | |
| 273 | for (lease = leases; lease; lease = lease->next) |
| 274 | { |
| 275 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 276 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 277 | continue; |
| 278 | |
| 279 | #ifdef HAVE_BROKEN_RTC |
| 280 | ourprintf(&err, "%u ", lease->length); |
| 281 | #else |
| 282 | ourprintf(&err, "%lu ", (unsigned long)lease->expires); |
| 283 | #endif |
| 284 | |
| 285 | inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN); |
| 286 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 287 | ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "", |
| 288 | lease->hwaddr_type, daemon->addrbuff); |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 289 | ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*"); |
| 290 | |
| 291 | if (lease->clid && lease->clid_len != 0) |
| 292 | { |
| 293 | for (i = 0; i < lease->clid_len - 1; i++) |
| 294 | ourprintf(&err, "%.2x:", lease->clid[i]); |
| 295 | ourprintf(&err, "%.2x\n", lease->clid[i]); |
| 296 | } |
| 297 | else |
| 298 | ourprintf(&err, "*\n"); |
| 299 | } |
| 300 | } |
| 301 | #endif |
| 302 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 303 | if (fflush(daemon->lease_stream) != 0 || |
| 304 | fsync(fileno(daemon->lease_stream)) < 0) |
| 305 | err = errno; |
| 306 | |
| 307 | if (!err) |
| 308 | file_dirty = 0; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 309 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 310 | |
| 311 | /* Set alarm for when the first lease expires + slop. */ |
| 312 | for (next_event = 0, lease = leases; lease; lease = lease->next) |
| 313 | if (lease->expires != 0 && |
| 314 | (next_event == 0 || difftime(next_event, lease->expires + 10) > 0.0)) |
| 315 | next_event = lease->expires + 10; |
| 316 | |
| 317 | if (err) |
| 318 | { |
| 319 | if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0) |
| 320 | next_event = LEASE_RETRY + now; |
| 321 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 322 | 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] | 323 | daemon->lease_file, strerror(err), |
| 324 | (unsigned int)difftime(next_event, now)); |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | if (next_event != 0) |
| 328 | alarm((unsigned)difftime(next_event, now)); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 329 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 330 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 331 | void lease_update_dns(void) |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 332 | { |
| 333 | struct dhcp_lease *lease; |
| 334 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 335 | if (daemon->port != 0 && dns_dirty) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 336 | { |
| 337 | cache_unhash_dhcp(); |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 338 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 339 | for (lease = leases; lease; lease = lease->next) |
| 340 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 341 | int prot = AF_INET; |
| 342 | #ifdef HAVE_DHCP6 |
| 343 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 344 | prot = AF_INET6; |
| 345 | #endif |
| 346 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 347 | if (lease->fqdn) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 348 | cache_add_dhcp_entry(lease->fqdn, prot, |
| 349 | prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr, |
| 350 | lease->expires); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 351 | |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 352 | if (!option_bool(OPT_DHCP_FQDN) && lease->hostname) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 353 | cache_add_dhcp_entry(lease->hostname, prot, |
| 354 | prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr, |
| 355 | lease->expires); |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | dns_dirty = 0; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void lease_prune(struct dhcp_lease *target, time_t now) |
| 363 | { |
| 364 | struct dhcp_lease *lease, *tmp, **up; |
| 365 | |
| 366 | for (lease = leases, up = &leases; lease; lease = tmp) |
| 367 | { |
| 368 | tmp = lease->next; |
| 369 | if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target) |
| 370 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 371 | file_dirty = 1; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 372 | if (lease->hostname) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 373 | dns_dirty = 1; |
| 374 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 375 | *up = lease->next; /* unlink */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 376 | |
| 377 | /* Put on old_leases list 'till we |
| 378 | can run the script */ |
| 379 | lease->next = old_leases; |
| 380 | old_leases = lease; |
| 381 | |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 382 | leases_left++; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 383 | } |
| 384 | else |
| 385 | up = &lease->next; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 390 | 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] | 391 | unsigned char *clid, int clid_len) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 392 | { |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 393 | struct dhcp_lease *lease; |
| 394 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 395 | if (clid) |
| 396 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 397 | { |
| 398 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 399 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 400 | continue; |
| 401 | #endif |
| 402 | if (lease->clid && clid_len == lease->clid_len && |
| 403 | memcmp(clid, lease->clid, clid_len) == 0) |
| 404 | return lease; |
| 405 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 406 | |
| 407 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 408 | { |
| 409 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 410 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 411 | continue; |
| 412 | #endif |
| 413 | if ((!lease->clid || !clid) && |
| 414 | hw_len != 0 && |
| 415 | lease->hwaddr_len == hw_len && |
| 416 | lease->hwaddr_type == hw_type && |
| 417 | memcmp(hwaddr, lease->hwaddr, hw_len) == 0) |
| 418 | return lease; |
| 419 | } |
| 420 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | struct dhcp_lease *lease_find_by_addr(struct in_addr addr) |
| 425 | { |
| 426 | struct dhcp_lease *lease; |
| 427 | |
| 428 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 429 | { |
| 430 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 431 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 432 | continue; |
| 433 | #endif |
| 434 | if (lease->addr.s_addr == addr.s_addr) |
| 435 | return lease; |
| 436 | } |
| 437 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 438 | return NULL; |
| 439 | } |
| 440 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 441 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 442 | /* addr or clid may be NULL for "don't care, both NULL resets "USED" flags both |
| 443 | set activates USED check */ |
| 444 | struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len, |
| 445 | int lease_type, int iaid, struct in6_addr *addr) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 446 | { |
| 447 | struct dhcp_lease *lease; |
| 448 | |
| 449 | for (lease = leases; lease; lease = lease->next) |
| 450 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 451 | if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid) |
| 452 | continue; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 453 | |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 454 | if (clid && addr && (lease->flags & LEASE_USED)) |
| 455 | continue; |
| 456 | |
| 457 | if (addr && memcmp(lease->hwaddr, addr, IN6ADDRSZ) != 0) |
| 458 | continue; |
| 459 | |
| 460 | if (clid && |
| 461 | (clid_len != lease->clid_len || |
| 462 | memcmp(clid, lease->clid, clid_len) != 0)) |
| 463 | continue; |
| 464 | |
| 465 | if (clid || addr) |
| 466 | { |
| 467 | lease->flags |= LEASE_USED; |
| 468 | return lease; |
| 469 | } |
| 470 | else |
| 471 | lease->flags &= ~LEASE_USED; |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | return NULL; |
| 475 | } |
| 476 | |
| 477 | struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr) |
| 478 | { |
| 479 | struct dhcp_lease *lease; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 480 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 481 | for (lease = leases; lease; lease = lease->next) |
| 482 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 483 | if (!(lease->flags & (LEASE_TA | LEASE_NA))) |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 484 | continue; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 485 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 486 | if (is_same_net6((struct in6_addr *)lease->hwaddr, net, prefix) && |
| 487 | (prefix == 128 || addr6part((struct in6_addr *)lease->hwaddr) == addr)) |
| 488 | return lease; |
| 489 | } |
| 490 | |
| 491 | return NULL; |
| 492 | } |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 493 | #endif |
| 494 | |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 495 | /* Find largest assigned address in context */ |
| 496 | struct in_addr lease_find_max_addr(struct dhcp_context *context) |
| 497 | { |
| 498 | struct dhcp_lease *lease; |
| 499 | struct in_addr addr = context->start; |
| 500 | |
| 501 | if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY))) |
| 502 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 503 | { |
| 504 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 505 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 506 | continue; |
| 507 | #endif |
| 508 | if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) && |
| 509 | ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) && |
| 510 | ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr))) |
| 511 | addr = lease->addr; |
| 512 | } |
Simon Kelley | 7de060b | 2011-08-26 17:24:52 +0100 | [diff] [blame] | 513 | |
| 514 | return addr; |
| 515 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 516 | |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 517 | static struct dhcp_lease *lease_allocate(void) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 518 | { |
| 519 | struct dhcp_lease *lease; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 520 | if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease)))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 521 | return NULL; |
| 522 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 523 | memset(lease, 0, sizeof(struct dhcp_lease)); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 524 | lease->flags = LEASE_NEW; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 525 | lease->expires = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 526 | #ifdef HAVE_BROKEN_RTC |
| 527 | lease->length = 0xffffffff; /* illegal value */ |
| 528 | #endif |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 529 | lease->next = leases; |
| 530 | leases = lease; |
| 531 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 532 | file_dirty = 1; |
Simon Kelley | 44a2a31 | 2004-03-10 20:04:35 +0000 | [diff] [blame] | 533 | leases_left--; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 534 | |
| 535 | return lease; |
| 536 | } |
| 537 | |
Simon Kelley | 52b92f4 | 2012-01-22 16:05:15 +0000 | [diff] [blame] | 538 | struct dhcp_lease *lease4_allocate(struct in_addr addr) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 539 | { |
| 540 | struct dhcp_lease *lease = lease_allocate(); |
| 541 | lease->addr = addr; |
| 542 | lease->hwaddr_len = 256; /* illegal value */ |
| 543 | |
| 544 | return lease; |
| 545 | } |
| 546 | |
| 547 | #ifdef HAVE_DHCP6 |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 548 | struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type) |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 549 | { |
| 550 | struct dhcp_lease *lease = lease_allocate(); |
| 551 | memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 552 | lease->flags |= lease_type; |
Simon Kelley | c72daea | 2012-01-05 21:33:27 +0000 | [diff] [blame] | 553 | |
| 554 | return lease; |
| 555 | } |
| 556 | #endif |
| 557 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 558 | 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] | 559 | { |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 560 | time_t exp = now + (time_t)len; |
| 561 | |
| 562 | if (len == 0xffffffff) |
| 563 | { |
| 564 | exp = 0; |
| 565 | len = 0; |
| 566 | } |
| 567 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 568 | if (exp != lease->expires) |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 569 | { |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 570 | dns_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 571 | lease->expires = exp; |
| 572 | #ifndef HAVE_BROKEN_RTC |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 573 | lease->flags |= LEASE_AUX_CHANGED; |
| 574 | file_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 575 | #endif |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 576 | } |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 577 | |
| 578 | #ifdef HAVE_BROKEN_RTC |
| 579 | if (len != lease->length) |
| 580 | { |
| 581 | lease->length = len; |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 582 | lease->aux_changed = file_dirty = 1; |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 583 | } |
| 584 | #endif |
| 585 | } |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 586 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 587 | void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr, |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 588 | unsigned char *clid, int hw_len, int hw_type, int clid_len) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 589 | { |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 590 | if (hw_len != lease->hwaddr_len || |
| 591 | hw_type != lease->hwaddr_type || |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 592 | (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0)) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 593 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 594 | if (hw_len != 0) |
| 595 | memcpy(lease->hwaddr, hwaddr, hw_len); |
Simon Kelley | cdeda28 | 2006-03-16 20:16:06 +0000 | [diff] [blame] | 596 | lease->hwaddr_len = hw_len; |
| 597 | lease->hwaddr_type = hw_type; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 598 | lease->flags |= LEASE_CHANGED; |
| 599 | file_dirty = 1; /* run script on change */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 600 | } |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 601 | |
| 602 | /* only update clid when one is available, stops packets |
| 603 | without a clid removing the record. Lease init uses |
| 604 | clid_len == 0 for no clid. */ |
| 605 | if (clid_len != 0 && clid) |
| 606 | { |
| 607 | if (!lease->clid) |
| 608 | lease->clid_len = 0; |
| 609 | |
| 610 | if (lease->clid_len != clid_len) |
| 611 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 612 | lease->flags |= LEASE_AUX_CHANGED; |
| 613 | file_dirty = 1; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 614 | free(lease->clid); |
| 615 | if (!(lease->clid = whine_malloc(clid_len))) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 616 | return; |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 617 | } |
| 618 | else if (memcmp(lease->clid, clid, clid_len) != 0) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 619 | { |
| 620 | lease->flags |= LEASE_AUX_CHANGED; |
| 621 | file_dirty = 1; |
| 622 | } |
| 623 | |
Simon Kelley | 0a85254 | 2005-03-23 20:28:59 +0000 | [diff] [blame] | 624 | lease->clid_len = clid_len; |
| 625 | memcpy(lease->clid, clid, clid_len); |
| 626 | } |
Simon Kelley | 208b65c | 2006-08-05 21:41:37 +0100 | [diff] [blame] | 627 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 630 | static void kill_name(struct dhcp_lease *lease) |
| 631 | { |
| 632 | /* run script to say we lost our old name */ |
| 633 | |
| 634 | /* this shouldn't happen unless updates are very quick and the |
| 635 | script very slow, we just avoid a memory leak if it does. */ |
| 636 | free(lease->old_hostname); |
| 637 | |
| 638 | /* If we know the fqdn, pass that. The helper will derive the |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 639 | unqualified name from it, free the unqualified name here. */ |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 640 | |
| 641 | if (lease->fqdn) |
| 642 | { |
| 643 | lease->old_hostname = lease->fqdn; |
| 644 | free(lease->hostname); |
| 645 | } |
| 646 | else |
| 647 | lease->old_hostname = lease->hostname; |
| 648 | |
| 649 | lease->hostname = lease->fqdn = NULL; |
| 650 | } |
| 651 | |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 652 | 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] | 653 | { |
| 654 | struct dhcp_lease *lease_tmp; |
| 655 | char *new_name = NULL, *new_fqdn = NULL; |
Simon Kelley | 70c5e3e | 2012-02-06 22:05:15 +0000 | [diff] [blame] | 656 | |
| 657 | if (config_domain && (!domain || !hostname_isequal(domain, config_domain))) |
| 658 | 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] | 659 | |
Simon Kelley | a222641 | 2004-05-13 20:27:08 +0100 | [diff] [blame] | 660 | if (lease->hostname && name && hostname_isequal(lease->hostname, name)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 661 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 662 | if (auth) |
| 663 | lease->flags |= LEASE_AUTH_NAME; |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 666 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 667 | if (!name && !lease->hostname) |
| 668 | return; |
| 669 | |
| 670 | /* If a machine turns up on a new net without dropping the old lease, |
| 671 | 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] | 672 | 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] | 673 | Note that IPv6 leases are different. All the leases to the same DUID are |
| 674 | allowed the same name. |
| 675 | |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 676 | 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] | 677 | |
| 678 | if (name) |
| 679 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 680 | if ((new_name = whine_malloc(strlen(name) + 1))) |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 681 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 682 | strcpy(new_name, name); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 683 | if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 684 | { |
| 685 | strcpy(new_fqdn, name); |
| 686 | strcat(new_fqdn, "."); |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 687 | strcat(new_fqdn, domain); |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
| 691 | /* Depending on mode, we check either unqualified name or FQDN. */ |
| 692 | for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next) |
| 693 | { |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 694 | if (option_bool(OPT_DHCP_FQDN)) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 695 | { |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 696 | 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] | 697 | continue; |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) ) |
| 702 | continue; |
| 703 | } |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 704 | |
| 705 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 706 | { |
| 707 | if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA))) |
| 708 | continue; |
| 709 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 710 | /* another lease for the same DUID is OK for IPv6 */ |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 711 | if (lease->clid_len == lease_tmp->clid_len && |
| 712 | lease->clid && lease_tmp->clid && |
| 713 | memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0) |
| 714 | continue; |
| 715 | } |
| 716 | else if (lease_tmp->flags & (LEASE_TA | LEASE_NA)) |
| 717 | continue; |
| 718 | |
| 719 | if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 720 | { |
| 721 | free(new_name); |
| 722 | free(new_fqdn); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | kill_name(lease_tmp); |
| 727 | break; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
| 731 | if (lease->hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 732 | kill_name(lease); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 733 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 734 | lease->hostname = new_name; |
| 735 | lease->fqdn = new_fqdn; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 736 | |
| 737 | if (auth) |
| 738 | lease->flags |= LEASE_AUTH_NAME; |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 739 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 740 | file_dirty = 1; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 741 | dns_dirty = 1; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 742 | lease->flags |= LEASE_CHANGED; /* run script on change */ |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 745 | void lease_set_interface(struct dhcp_lease *lease, int interface) |
| 746 | { |
| 747 | if (lease->last_interface == interface) |
| 748 | return; |
| 749 | |
| 750 | lease->last_interface = interface; |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 751 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 754 | void rerun_scripts(void) |
| 755 | { |
| 756 | struct dhcp_lease *lease; |
| 757 | |
| 758 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 759 | lease->flags |= LEASE_CHANGED; |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 760 | } |
| 761 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 762 | /* deleted leases get transferred to the old_leases list. |
| 763 | remove them here, after calling the lease change |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 764 | script. Also run the lease change script on new/modified leases. |
| 765 | |
| 766 | Return zero if nothing to do. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 767 | int do_script_run(time_t now) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 768 | { |
| 769 | struct dhcp_lease *lease; |
| 770 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 771 | #ifdef HAVE_DBUS |
| 772 | /* If we're going to be sending DBus signals, but the connection is not yet up, |
| 773 | delay everything until it is. */ |
Simon Kelley | 28866e9 | 2011-02-14 20:19:14 +0000 | [diff] [blame] | 774 | if (option_bool(OPT_DBUS) && !daemon->dbus) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 775 | return 0; |
| 776 | #endif |
| 777 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 778 | if (old_leases) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 779 | { |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 780 | lease = old_leases; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 781 | |
| 782 | /* If the lease still has an old_hostname, do the "old" action on that first */ |
| 783 | if (lease->old_hostname) |
| 784 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 785 | #ifdef HAVE_SCRIPT |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 786 | queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); |
| 787 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 788 | free(lease->old_hostname); |
| 789 | lease->old_hostname = NULL; |
| 790 | return 1; |
| 791 | } |
| 792 | else |
| 793 | { |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 794 | kill_name(lease); |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 795 | #ifdef HAVE_SCRIPT |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 796 | queue_script(ACTION_DEL, lease, lease->old_hostname, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 797 | #endif |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 798 | #ifdef HAVE_DBUS |
| 799 | emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname); |
| 800 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 801 | old_leases = lease->next; |
| 802 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 803 | free(lease->old_hostname); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 804 | free(lease->clid); |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 805 | free(lease->extradata); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 806 | free(lease); |
| 807 | |
| 808 | return 1; |
| 809 | } |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 810 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 811 | |
| 812 | /* make sure we announce the loss of a hostname before its new location. */ |
| 813 | for (lease = leases; lease; lease = lease->next) |
| 814 | if (lease->old_hostname) |
| 815 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 816 | #ifdef HAVE_SCRIPT |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 817 | queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now); |
| 818 | #endif |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 819 | free(lease->old_hostname); |
| 820 | lease->old_hostname = NULL; |
| 821 | return 1; |
| 822 | } |
| 823 | |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 824 | for (lease = leases; lease; lease = lease->next) |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 825 | if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) || |
| 826 | ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO))) |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 827 | { |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 828 | #ifdef HAVE_SCRIPT |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 829 | queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 830 | lease->fqdn ? lease->fqdn : lease->hostname, now); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 831 | #endif |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 832 | #ifdef HAVE_DBUS |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 833 | emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease, |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 834 | lease->fqdn ? lease->fqdn : lease->hostname); |
| 835 | #endif |
Simon Kelley | 4cb1b32 | 2012-02-06 14:30:41 +0000 | [diff] [blame] | 836 | lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED); |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 837 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 838 | /* this is used for the "add" call, then junked, since they're not in the database */ |
| 839 | free(lease->extradata); |
| 840 | lease->extradata = NULL; |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 841 | |
| 842 | return 1; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 843 | } |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 844 | |
| 845 | return 0; /* nothing to do */ |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 846 | } |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 847 | |
Simon Kelley | ceae00d | 2012-02-09 21:28:14 +0000 | [diff] [blame] | 848 | #ifdef HAVE_SCRIPT |
| 849 | void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim) |
| 850 | { |
| 851 | unsigned int i; |
| 852 | |
| 853 | /* check for embeded NULLs */ |
| 854 | for (i = 0; i < len; i++) |
| 855 | if (data[i] == 0) |
| 856 | { |
| 857 | len = i; |
| 858 | break; |
| 859 | } |
| 860 | |
| 861 | if ((lease->extradata_size - lease->extradata_len) < (len + 1)) |
| 862 | { |
| 863 | size_t newsz = lease->extradata_len + len + 100; |
| 864 | unsigned char *new = whine_malloc(newsz); |
| 865 | |
| 866 | if (!new) |
| 867 | return; |
| 868 | |
| 869 | if (lease->extradata) |
| 870 | { |
| 871 | memcpy(new, lease->extradata, lease->extradata_len); |
| 872 | free(lease->extradata); |
| 873 | } |
| 874 | |
| 875 | lease->extradata = new; |
| 876 | lease->extradata_size = newsz; |
| 877 | } |
| 878 | |
| 879 | if (len != 0) |
| 880 | memcpy(lease->extradata + lease->extradata_len, data, len); |
| 881 | lease->extradata[lease->extradata_len + len] = delim; |
| 882 | lease->extradata_len += len + 1; |
| 883 | } |
| 884 | #endif |
| 885 | |
Simon Kelley | 7622fc0 | 2009-06-04 20:32:05 +0100 | [diff] [blame] | 886 | #endif |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 887 | |
| 888 | |
| 889 | |
Simon Kelley | 9e4abcb | 2004-01-22 19:47:41 +0000 | [diff] [blame] | 890 | |