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