blob: 57fde0fa960aa7c5a239e5efedec3365b302c3ac [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
Simon Kelley9e4abcb2004-01-22 19:47:41 +00002
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 Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley9e4abcb2004-01-22 19:47:41 +00008 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 Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 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 Kelley9e4abcb2004-01-22 19:47:41 +000015*/
16
Simon Kelley9e4abcb2004-01-22 19:47:41 +000017#include "dnsmasq.h"
18
Simon Kelley7622fc02009-06-04 20:32:05 +010019#ifdef HAVE_DHCP
20
Simon Kelley5aabfc72007-08-29 11:24:47 +010021static struct dhcp_lease *leases = NULL, *old_leases = NULL;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010022static int dns_dirty, file_dirty, leases_left;
Simon Kelley9e4abcb2004-01-22 19:47:41 +000023
Simon Kelley5aabfc72007-08-29 11:24:47 +010024void lease_init(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +000025{
Simon Kelley9e4abcb2004-01-22 19:47:41 +000026 unsigned long ei;
Simon Kelleyc72daea2012-01-05 21:33:27 +000027 struct all_addr addr;
Simon Kelley9e4abcb2004-01-22 19:47:41 +000028 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +010029 int clid_len, hw_len, hw_type;
Simon Kelley208b65c2006-08-05 21:41:37 +010030 FILE *leasestream;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +010031
Simon Kelley3be34542004-09-11 19:12:13 +010032 leases_left = daemon->dhcp_max;
Simon Kelleyceae00d2012-02-09 21:28:14 +000033
Simon Kelley28866e92011-02-14 20:19:14 +000034 if (option_bool(OPT_LEASE_RO))
Simon Kelley208b65c2006-08-05 21:41:37 +010035 {
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 Kelley1f15b812009-10-13 17:49:32 +010040#ifdef HAVE_SCRIPT
41 if (daemon->lease_change_command)
Simon Kelley208b65c2006-08-05 21:41:37 +010042 {
Simon Kelley1f15b812009-10-13 17:49:32 +010043 strcpy(daemon->dhcp_buff, daemon->lease_change_command);
44 strcat(daemon->dhcp_buff, " init");
45 leasestream = popen(daemon->dhcp_buff, "r");
Simon Kelley208b65c2006-08-05 21:41:37 +010046 }
Simon Kelley1f15b812009-10-13 17:49:32 +010047 else
48#endif
49 {
50 file_dirty = dns_dirty = 0;
51 return;
52 }
53
Simon Kelley208b65c2006-08-05 21:41:37 +010054 }
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 Kelley5aabfc72007-08-29 11:24:47 +010061 die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +010062
Simon Kelley7622fc02009-06-04 20:32:05 +010063 /* a+ mode leaves pointer at end. */
Simon Kelley208b65c2006-08-05 21:41:37 +010064 rewind(leasestream);
65 }
Simon Kelley7cebd202006-05-06 14:13:33 +010066
Simon Kelley0a852542005-03-23 20:28:59 +000067 /* 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 Kelley208b65c2006-08-05 21:41:37 +010069 if (leasestream)
Simon Kelleyceae00d2012-02-09 21:28:14 +000070 while (fscanf(leasestream, "%255s %255s", daemon->dhcp_buff3, daemon->dhcp_buff2) == 2)
Simon Kelley208b65c2006-08-05 21:41:37 +010071 {
Simon Kelley3d7b5502012-02-10 17:35:37 +000072#ifdef HAVE_DHCP6
Simon Kelleyceae00d2012-02-09 21:28:14 +000073 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 Kelley3d7b5502012-02-10 17:35:37 +000080#endif
81
Simon Kelleyceae00d2012-02-09 21:28:14 +000082 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 Kelleyc72daea2012-01-05 21:33:27 +0000105#ifdef HAVE_DHCP6
Simon Kelleyceae00d2012-02-09 21:28:14 +0000106 else if (inet_pton(AF_INET6, daemon->namebuff, &addr.addr.addr6))
Simon Kelley4cb1b322012-02-06 14:30:41 +0000107 {
108 char *s = daemon->dhcp_buff2;
Simon Kelleyceae00d2012-02-09 21:28:14 +0000109 int lease_type = LEASE_NA;
110
Simon Kelley4cb1b322012-02-06 14:30:41 +0000111 if (s[0] == 'T')
112 {
113 lease_type = LEASE_TA;
114 s++;
115 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000116
117 hw_type = atoi(s);
Simon Kelleyceae00d2012-02-09 21:28:14 +0000118
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 Kelley4cb1b322012-02-06 14:30:41 +0000126 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000127#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000128 else
Simon Kelleyceae00d2012-02-09 21:28:14 +0000129 break;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000130
Simon Kelleyc72daea2012-01-05 21:33:27 +0000131 if (!lease)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100132 die (_("too many stored leases"), NULL, EC_MISC);
133
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100134#ifdef HAVE_BROKEN_RTC
Simon Kelley208b65c2006-08-05 21:41:37 +0100135 if (ei != 0)
136 lease->expires = (time_t)ei + now;
137 else
138 lease->expires = (time_t)0;
139 lease->length = ei;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100140#else
Simon Kelley208b65c2006-08-05 21:41:37 +0100141 /* 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 Kelley5e9e0ef2006-04-17 14:24:29 +0100144#endif
Simon Kelley208b65c2006-08-05 21:41:37 +0100145
Simon Kelley5aabfc72007-08-29 11:24:47 +0100146 /* set these correctly: the "old" events are generated later from
147 the startup synthesised SIGHUP. */
Simon Kelleyceae00d2012-02-09 21:28:14 +0000148 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED);
Simon Kelley208b65c2006-08-05 21:41:37 +0100149 }
150
Simon Kelley1f15b812009-10-13 17:49:32 +0100151#ifdef HAVE_SCRIPT
Simon Kelley208b65c2006-08-05 21:41:37 +0100152 if (!daemon->lease_stream)
153 {
154 int rc = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000155
Simon Kelley208b65c2006-08-05 21:41:37 +0100156 /* 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 Kelley5aabfc72007-08-29 11:24:47 +0100163 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +0100164 }
165
166 if (WEXITSTATUS(rc) != 0)
167 {
168 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100169 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
Simon Kelley208b65c2006-08-05 21:41:37 +0100170 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000171 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100172#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100173
174 /* Some leases may have expired */
175 file_dirty = 0;
176 lease_prune(NULL, now);
177 dns_dirty = 1;
Simon Kelley6aef6002012-02-11 22:01:50 +0000178
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 Kelley44a2a312004-03-10 20:04:35 +0000188}
189
Simon Kelley5aabfc72007-08-29 11:24:47 +0100190void lease_update_from_configs(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000191{
192 /* changes to the config may change current leases. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000193
Simon Kelley44a2a312004-03-10 20:04:35 +0000194 struct dhcp_lease *lease;
195 struct dhcp_config *config;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000196 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000197
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000198 for (lease = leases; lease; lease = lease->next)
Simon Kelleycdeda282006-03-16 20:16:06 +0000199 if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
200 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000201 (config->flags & CONFIG_NAME) &&
202 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000203 lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100204 else if ((name = host_from_dns(lease->addr)))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000205 lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000206}
Simon Kelley4cb1b322012-02-06 14:30:41 +0000207
Simon Kelley5aabfc72007-08-29 11:24:47 +0100208static void ourprintf(int *errp, char *format, ...)
Simon Kelley7cebd202006-05-06 14:13:33 +0100209{
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 Kelley5aabfc72007-08-29 11:24:47 +0100218void lease_update_file(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000219{
220 struct dhcp_lease *lease;
Simon Kelley7cebd202006-05-06 14:13:33 +0100221 time_t next_event;
222 int i, err = 0;
223
Simon Kelley208b65c2006-08-05 21:41:37 +0100224 if (file_dirty != 0 && daemon->lease_stream)
Simon Kelley44a2a312004-03-10 20:04:35 +0000225 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000226 errno = 0;
227 rewind(daemon->lease_stream);
228 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100229 err = errno;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000230
231 for (lease = leases; lease; lease = lease->next)
232 {
Simon Kelleyc72daea2012-01-05 21:33:27 +0000233
234#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000235 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000236 continue;
237#endif
238
Simon Kelley44a2a312004-03-10 20:04:35 +0000239#ifdef HAVE_BROKEN_RTC
Simon Kelley5aabfc72007-08-29 11:24:47 +0100240 ourprintf(&err, "%u ", lease->length);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100241#else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100242 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100243#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000244
Simon Kelley7cebd202006-05-06 14:13:33 +0100245 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100246 ourprintf(&err, "%.2x-", lease->hwaddr_type);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100247 for (i = 0; i < lease->hwaddr_len; i++)
248 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100249 ourprintf(&err, "%.2x", lease->hwaddr[i]);
Simon Kelley7cebd202006-05-06 14:13:33 +0100250 if (i != lease->hwaddr_len - 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100251 ourprintf(&err, ":");
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100252 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000253
254 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100255
Simon Kelleyc72daea2012-01-05 21:33:27 +0000256 ourprintf(&err, " %s ", daemon->addrbuff);
Simon Kelley1f15b812009-10-13 17:49:32 +0100257 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
258
Simon Kelley0a852542005-03-23 20:28:59 +0000259 if (lease->clid && lease->clid_len != 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000260 {
261 for (i = 0; i < lease->clid_len - 1; i++)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100262 ourprintf(&err, "%.2x:", lease->clid[i]);
263 ourprintf(&err, "%.2x\n", lease->clid[i]);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000264 }
265 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100266 ourprintf(&err, "*\n");
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000267 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100268
Simon Kelleyc72daea2012-01-05 21:33:27 +0000269#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 Kelley4cb1b322012-02-06 14:30:41 +0000280 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000281 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 Kelley4cb1b322012-02-06 14:30:41 +0000291 ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "",
292 lease->hwaddr_type, daemon->addrbuff);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000293 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 Kelley7cebd202006-05-06 14:13:33 +0100307 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 Kelley9e4abcb2004-01-22 19:47:41 +0000313 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100314
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 Kelley7622fc02009-06-04 20:32:05 +0100326 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100327 daemon->lease_file, strerror(err),
328 (unsigned int)difftime(next_event, now));
Simon Kelley7cebd202006-05-06 14:13:33 +0100329 }
330
331 if (next_event != 0)
332 alarm((unsigned)difftime(next_event, now));
Simon Kelley44a2a312004-03-10 20:04:35 +0000333}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000334
Simon Kelley5aabfc72007-08-29 11:24:47 +0100335void lease_update_dns(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000336{
337 struct dhcp_lease *lease;
338
Simon Kelley824af852008-02-12 20:43:05 +0000339 if (daemon->port != 0 && dns_dirty)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000340 {
341 cache_unhash_dhcp();
Simon Kelley44a2a312004-03-10 20:04:35 +0000342
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000343 for (lease = leases; lease; lease = lease->next)
344 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000345 int prot = AF_INET;
346#ifdef HAVE_DHCP6
347 if (lease->flags & (LEASE_TA | LEASE_NA))
348 prot = AF_INET6;
349#endif
350
Simon Kelley9009d742008-11-14 20:04:27 +0000351 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000352 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 Kelley9009d742008-11-14 20:04:27 +0000355
Simon Kelley28866e92011-02-14 20:19:14 +0000356 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000357 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 Kelley9e4abcb2004-01-22 19:47:41 +0000360 }
361
362 dns_dirty = 0;
363 }
364}
365
366void 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 Kelley5e9e0ef2006-04-17 14:24:29 +0100375 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000376 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100377 dns_dirty = 1;
378
Simon Kelleyc72daea2012-01-05 21:33:27 +0000379 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100380
381 /* Put on old_leases list 'till we
382 can run the script */
383 lease->next = old_leases;
384 old_leases = lease;
385
Simon Kelley44a2a312004-03-10 20:04:35 +0000386 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000387 }
388 else
389 up = &lease->next;
390 }
391}
392
393
Simon Kelleycdeda282006-03-16 20:16:06 +0000394struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000395 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000396{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000397 struct dhcp_lease *lease;
398
Simon Kelley0a852542005-03-23 20:28:59 +0000399 if (clid)
400 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000401 {
402#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000403 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000404 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 Kelley0a852542005-03-23 20:28:59 +0000410
411 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000412 {
413#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000414 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000415 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 Kelley9e4abcb2004-01-22 19:47:41 +0000425 return NULL;
426}
427
428struct 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 Kelleyc72daea2012-01-05 21:33:27 +0000433 {
434#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000435 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000436 continue;
437#endif
438 if (lease->addr.s_addr == addr.s_addr)
439 return lease;
440 }
441
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000442 return NULL;
443}
444
Simon Kelley52b92f42012-01-22 16:05:15 +0000445#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000446/* addr or clid may be NULL for "don't care, both NULL resets "USED" flags both
447 set activates USED check */
448struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
449 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000450{
451 struct dhcp_lease *lease;
452
453 for (lease = leases; lease; lease = lease->next)
454 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000455 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
456 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000457
Simon Kelley4cb1b322012-02-06 14:30:41 +0000458 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 Kelley52b92f42012-01-22 16:05:15 +0000476 }
477
478 return NULL;
479}
480
481struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
482{
483 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000484
Simon Kelley52b92f42012-01-22 16:05:15 +0000485 for (lease = leases; lease; lease = lease->next)
486 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000487 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000488 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000489
Simon Kelley52b92f42012-01-22 16:05:15 +0000490 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 Kelley07933802012-02-14 20:55:25 +0000496}
497
498/* Find largest assigned address in context */
499u64 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 Kelley07933802012-02-14 20:55:25 +0000507 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
508 continue;
Simon Kelley6caacac2012-02-15 21:58:33 +0000509
Simon Kelley07933802012-02-14 20:55:25 +0000510 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 Kelley52b92f42012-01-22 16:05:15 +0000520#endif
521
Simon Kelley7de060b2011-08-26 17:24:52 +0100522/* Find largest assigned address in context */
523struct 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 Kelleyc72daea2012-01-05 21:33:27 +0000530 {
531#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000532 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000533 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 Kelley7de060b2011-08-26 17:24:52 +0100540
541 return addr;
542}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000543
Simon Kelleyc72daea2012-01-05 21:33:27 +0000544static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000545{
546 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100547 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000548 return NULL;
549
Simon Kelley7cebd202006-05-06 14:13:33 +0100550 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000551 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000552 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100553#ifdef HAVE_BROKEN_RTC
554 lease->length = 0xffffffff; /* illegal value */
555#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000556 lease->next = leases;
557 leases = lease;
558
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100559 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000560 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000561
562 return lease;
563}
564
Simon Kelley52b92f42012-01-22 16:05:15 +0000565struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000566{
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 Kelley4cb1b322012-02-06 14:30:41 +0000575struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000576{
577 struct dhcp_lease *lease = lease_allocate();
578 memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000579 lease->flags |= lease_type;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000580
581 return lease;
582}
583#endif
584
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100585void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000586{
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100587 time_t exp = now + (time_t)len;
588
589 if (len == 0xffffffff)
590 {
591 exp = 0;
592 len = 0;
593 }
594
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000595 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000596 {
Simon Kelley0a852542005-03-23 20:28:59 +0000597 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100598 lease->expires = exp;
599#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000600 lease->flags |= LEASE_AUX_CHANGED;
601 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100602#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000603 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100604
605#ifdef HAVE_BROKEN_RTC
606 if (len != lease->length)
607 {
608 lease->length = len;
Simon Kelley208b65c2006-08-05 21:41:37 +0100609 lease->aux_changed = file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100610 }
611#endif
612}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000613
Simon Kelley7cebd202006-05-06 14:13:33 +0100614void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
Simon Kelleycdeda282006-03-16 20:16:06 +0000615 unsigned char *clid, int hw_len, int hw_type, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000616{
Simon Kelleycdeda282006-03-16 20:16:06 +0000617 if (hw_len != lease->hwaddr_len ||
618 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100619 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000620 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000621 if (hw_len != 0)
622 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000623 lease->hwaddr_len = hw_len;
624 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000625 lease->flags |= LEASE_CHANGED;
626 file_dirty = 1; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000627 }
Simon Kelley0a852542005-03-23 20:28:59 +0000628
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 Kelley4cb1b322012-02-06 14:30:41 +0000639 lease->flags |= LEASE_AUX_CHANGED;
640 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100641 free(lease->clid);
642 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100643 return;
Simon Kelley0a852542005-03-23 20:28:59 +0000644 }
645 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000646 {
647 lease->flags |= LEASE_AUX_CHANGED;
648 file_dirty = 1;
649 }
650
Simon Kelley0a852542005-03-23 20:28:59 +0000651 lease->clid_len = clid_len;
652 memcpy(lease->clid, clid, clid_len);
653 }
Simon Kelley208b65c2006-08-05 21:41:37 +0100654
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000655}
656
Simon Kelley9009d742008-11-14 20:04:27 +0000657static 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 Kelley4cb1b322012-02-06 14:30:41 +0000666 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000667
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 Kelley70c5e3e2012-02-06 22:05:15 +0000679void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000680{
681 struct dhcp_lease *lease_tmp;
682 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000683
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 Kelley9009d742008-11-14 20:04:27 +0000686
Simon Kelleya2226412004-05-13 20:27:08 +0100687 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000688 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000689 if (auth)
690 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000691 return;
692 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100693
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000694 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 Kelleyb8187c82005-11-26 21:46:27 +0000699 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000700 Note that IPv6 leases are different. All the leases to the same DUID are
701 allowed the same name.
702
Simon Kelleyb8187c82005-11-26 21:46:27 +0000703 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000704
705 if (name)
706 {
Simon Kelley9009d742008-11-14 20:04:27 +0000707 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000708 {
Simon Kelley9009d742008-11-14 20:04:27 +0000709 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000710 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000711 {
712 strcpy(new_fqdn, name);
713 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000714 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000715 }
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 Kelley28866e92011-02-14 20:19:14 +0000721 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000722 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000723 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000724 continue;
725 }
726 else
727 {
728 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
729 continue;
730 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000731
732 if (lease->flags & (LEASE_TA | LEASE_NA))
733 {
734 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
735 continue;
736
Simon Kelleyceae00d2012-02-09 21:28:14 +0000737 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000738 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 Kelley9009d742008-11-14 20:04:27 +0000747 {
748 free(new_name);
749 free(new_fqdn);
750 return;
751 }
752
753 kill_name(lease_tmp);
754 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000755 }
756 }
757
758 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000759 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +0100760
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000761 lease->hostname = new_name;
762 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000763
764 if (auth)
765 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000766
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100767 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100768 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000769 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000770}
771
Simon Kelley824af852008-02-12 20:43:05 +0000772void 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 Kelley4cb1b322012-02-06 14:30:41 +0000778 lease->flags |= LEASE_CHANGED;
Simon Kelley824af852008-02-12 20:43:05 +0000779}
780
Simon Kelley5aabfc72007-08-29 11:24:47 +0100781void rerun_scripts(void)
782{
783 struct dhcp_lease *lease;
784
785 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000786 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100787}
788
Simon Kelley7cebd202006-05-06 14:13:33 +0100789/* deleted leases get transferred to the old_leases list.
790 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +0100791 script. Also run the lease change script on new/modified leases.
792
793 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100794int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +0100795{
796 struct dhcp_lease *lease;
797
Simon Kelley9009d742008-11-14 20:04:27 +0000798#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 Kelley28866e92011-02-14 20:19:14 +0000801 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +0000802 return 0;
803#endif
804
Simon Kelley16972692006-10-16 20:04:18 +0100805 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +0100806 {
Simon Kelley7cebd202006-05-06 14:13:33 +0100807 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +0100808
809 /* If the lease still has an old_hostname, do the "old" action on that first */
810 if (lease->old_hostname)
811 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100812#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100813 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
814#endif
Simon Kelley16972692006-10-16 20:04:18 +0100815 free(lease->old_hostname);
816 lease->old_hostname = NULL;
817 return 1;
818 }
819 else
820 {
Simon Kelley9009d742008-11-14 20:04:27 +0000821 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +0100822#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +0000823 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100824#endif
Simon Kelley1f15b812009-10-13 17:49:32 +0100825#ifdef HAVE_DBUS
826 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
827#endif
Simon Kelley16972692006-10-16 20:04:18 +0100828 old_leases = lease->next;
829
Simon Kelley9009d742008-11-14 20:04:27 +0000830 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100831 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +0000832 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +0100833 free(lease);
834
835 return 1;
836 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100837 }
Simon Kelley16972692006-10-16 20:04:18 +0100838
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 Kelley1f15b812009-10-13 17:49:32 +0100843#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100844 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
845#endif
Simon Kelley16972692006-10-16 20:04:18 +0100846 free(lease->old_hostname);
847 lease->old_hostname = NULL;
848 return 1;
849 }
850
Simon Kelley7cebd202006-05-06 14:13:33 +0100851 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000852 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
853 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100854 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100855#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +0000856 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +0000857 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100858#endif
Simon Kelley1f15b812009-10-13 17:49:32 +0100859#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +0000860 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +0100861 lease->fqdn ? lease->fqdn : lease->hostname);
862#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000863 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +0100864
Simon Kelley316e2732010-01-22 20:16:09 +0000865 /* 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 Kelley16972692006-10-16 20:04:18 +0100868
869 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100870 }
Simon Kelley16972692006-10-16 20:04:18 +0100871
872 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +0100873}
Simon Kelley7622fc02009-06-04 20:32:05 +0100874
Simon Kelleyceae00d2012-02-09 21:28:14 +0000875#ifdef HAVE_SCRIPT
876void 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 Kelley7622fc02009-06-04 20:32:05 +0100913#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100914
915
916
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000917