blob: b5f9926d687f4c74d47f26514a47451fd7adebf8 [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 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000150
151#ifdef HAVE_DHCP6
Simon Kelleyceae00d2012-02-09 21:28:14 +0000152 /* If we're not doing DHCPv6, and there are not v6 leases, don't add the DUID to the database */
153 if (!daemon->duid && daemon->dhcp6)
154 make_duid(now);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000155#endif
Simon Kelley208b65c2006-08-05 21:41:37 +0100156
Simon Kelley1f15b812009-10-13 17:49:32 +0100157#ifdef HAVE_SCRIPT
Simon Kelley208b65c2006-08-05 21:41:37 +0100158 if (!daemon->lease_stream)
159 {
160 int rc = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000161
Simon Kelley208b65c2006-08-05 21:41:37 +0100162 /* shell returns 127 for "command not found", 126 for bad permissions. */
163 if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
164 {
165 if (WEXITSTATUS(rc) == 127)
166 errno = ENOENT;
167 else if (WEXITSTATUS(rc) == 126)
168 errno = EACCES;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100169 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
Simon Kelley208b65c2006-08-05 21:41:37 +0100170 }
171
172 if (WEXITSTATUS(rc) != 0)
173 {
174 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100175 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
Simon Kelley208b65c2006-08-05 21:41:37 +0100176 }
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000177 }
Simon Kelley1f15b812009-10-13 17:49:32 +0100178#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100179
180 /* Some leases may have expired */
181 file_dirty = 0;
182 lease_prune(NULL, now);
183 dns_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000184}
185
Simon Kelley5aabfc72007-08-29 11:24:47 +0100186void lease_update_from_configs(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000187{
188 /* changes to the config may change current leases. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000189
Simon Kelley44a2a312004-03-10 20:04:35 +0000190 struct dhcp_lease *lease;
191 struct dhcp_config *config;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000192 char *name;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000193
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000194 for (lease = leases; lease; lease = lease->next)
Simon Kelleycdeda282006-03-16 20:16:06 +0000195 if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
196 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
Simon Kelleyb8187c82005-11-26 21:46:27 +0000197 (config->flags & CONFIG_NAME) &&
198 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000199 lease_set_hostname(lease, config->hostname, 1, get_domain(lease->addr), NULL);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100200 else if ((name = host_from_dns(lease->addr)))
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000201 lease_set_hostname(lease, name, 1, get_domain(lease->addr), NULL); /* updates auth flag only */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000202}
Simon Kelley4cb1b322012-02-06 14:30:41 +0000203
Simon Kelley5aabfc72007-08-29 11:24:47 +0100204static void ourprintf(int *errp, char *format, ...)
Simon Kelley7cebd202006-05-06 14:13:33 +0100205{
206 va_list ap;
207
208 va_start(ap, format);
209 if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0)
210 *errp = errno;
211 va_end(ap);
212}
213
Simon Kelley5aabfc72007-08-29 11:24:47 +0100214void lease_update_file(time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000215{
216 struct dhcp_lease *lease;
Simon Kelley7cebd202006-05-06 14:13:33 +0100217 time_t next_event;
218 int i, err = 0;
219
Simon Kelley208b65c2006-08-05 21:41:37 +0100220 if (file_dirty != 0 && daemon->lease_stream)
Simon Kelley44a2a312004-03-10 20:04:35 +0000221 {
Simon Kelleycdeda282006-03-16 20:16:06 +0000222 errno = 0;
223 rewind(daemon->lease_stream);
224 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
Simon Kelley7cebd202006-05-06 14:13:33 +0100225 err = errno;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000226
227 for (lease = leases; lease; lease = lease->next)
228 {
Simon Kelleyc72daea2012-01-05 21:33:27 +0000229
230#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000231 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000232 continue;
233#endif
234
Simon Kelley44a2a312004-03-10 20:04:35 +0000235#ifdef HAVE_BROKEN_RTC
Simon Kelley5aabfc72007-08-29 11:24:47 +0100236 ourprintf(&err, "%u ", lease->length);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100237#else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100238 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100239#endif
Simon Kelleyc72daea2012-01-05 21:33:27 +0000240
Simon Kelley7cebd202006-05-06 14:13:33 +0100241 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100242 ourprintf(&err, "%.2x-", lease->hwaddr_type);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100243 for (i = 0; i < lease->hwaddr_len; i++)
244 {
Simon Kelley5aabfc72007-08-29 11:24:47 +0100245 ourprintf(&err, "%.2x", lease->hwaddr[i]);
Simon Kelley7cebd202006-05-06 14:13:33 +0100246 if (i != lease->hwaddr_len - 1)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100247 ourprintf(&err, ":");
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100248 }
Simon Kelleyc72daea2012-01-05 21:33:27 +0000249
250 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100251
Simon Kelleyc72daea2012-01-05 21:33:27 +0000252 ourprintf(&err, " %s ", daemon->addrbuff);
Simon Kelley1f15b812009-10-13 17:49:32 +0100253 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
254
Simon Kelley0a852542005-03-23 20:28:59 +0000255 if (lease->clid && lease->clid_len != 0)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000256 {
257 for (i = 0; i < lease->clid_len - 1; i++)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100258 ourprintf(&err, "%.2x:", lease->clid[i]);
259 ourprintf(&err, "%.2x\n", lease->clid[i]);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000260 }
261 else
Simon Kelley5aabfc72007-08-29 11:24:47 +0100262 ourprintf(&err, "*\n");
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000263 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100264
Simon Kelleyc72daea2012-01-05 21:33:27 +0000265#ifdef HAVE_DHCP6
266 if (daemon->duid)
267 {
268 ourprintf(&err, "duid ");
269 for (i = 0; i < daemon->duid_len - 1; i++)
270 ourprintf(&err, "%.2x:", daemon->duid[i]);
271 ourprintf(&err, "%.2x\n", daemon->duid[i]);
272
273 for (lease = leases; lease; lease = lease->next)
274 {
275
Simon Kelley4cb1b322012-02-06 14:30:41 +0000276 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000277 continue;
278
279#ifdef HAVE_BROKEN_RTC
280 ourprintf(&err, "%u ", lease->length);
281#else
282 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
283#endif
284
285 inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN);
286
Simon Kelley4cb1b322012-02-06 14:30:41 +0000287 ourprintf(&err, "%s%u %s ", (lease->flags & LEASE_TA) ? "T" : "",
288 lease->hwaddr_type, daemon->addrbuff);
Simon Kelleyc72daea2012-01-05 21:33:27 +0000289 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
290
291 if (lease->clid && lease->clid_len != 0)
292 {
293 for (i = 0; i < lease->clid_len - 1; i++)
294 ourprintf(&err, "%.2x:", lease->clid[i]);
295 ourprintf(&err, "%.2x\n", lease->clid[i]);
296 }
297 else
298 ourprintf(&err, "*\n");
299 }
300 }
301#endif
302
Simon Kelley7cebd202006-05-06 14:13:33 +0100303 if (fflush(daemon->lease_stream) != 0 ||
304 fsync(fileno(daemon->lease_stream)) < 0)
305 err = errno;
306
307 if (!err)
308 file_dirty = 0;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000309 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100310
311 /* Set alarm for when the first lease expires + slop. */
312 for (next_event = 0, lease = leases; lease; lease = lease->next)
313 if (lease->expires != 0 &&
314 (next_event == 0 || difftime(next_event, lease->expires + 10) > 0.0))
315 next_event = lease->expires + 10;
316
317 if (err)
318 {
319 if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0)
320 next_event = LEASE_RETRY + now;
321
Simon Kelley7622fc02009-06-04 20:32:05 +0100322 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
Simon Kelleyf2621c72007-04-29 19:47:21 +0100323 daemon->lease_file, strerror(err),
324 (unsigned int)difftime(next_event, now));
Simon Kelley7cebd202006-05-06 14:13:33 +0100325 }
326
327 if (next_event != 0)
328 alarm((unsigned)difftime(next_event, now));
Simon Kelley44a2a312004-03-10 20:04:35 +0000329}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000330
Simon Kelley5aabfc72007-08-29 11:24:47 +0100331void lease_update_dns(void)
Simon Kelley44a2a312004-03-10 20:04:35 +0000332{
333 struct dhcp_lease *lease;
334
Simon Kelley824af852008-02-12 20:43:05 +0000335 if (daemon->port != 0 && dns_dirty)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000336 {
337 cache_unhash_dhcp();
Simon Kelley44a2a312004-03-10 20:04:35 +0000338
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000339 for (lease = leases; lease; lease = lease->next)
340 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000341 int prot = AF_INET;
342#ifdef HAVE_DHCP6
343 if (lease->flags & (LEASE_TA | LEASE_NA))
344 prot = AF_INET6;
345#endif
346
Simon Kelley9009d742008-11-14 20:04:27 +0000347 if (lease->fqdn)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000348 cache_add_dhcp_entry(lease->fqdn, prot,
349 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr,
350 lease->expires);
Simon Kelley9009d742008-11-14 20:04:27 +0000351
Simon Kelley28866e92011-02-14 20:19:14 +0000352 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000353 cache_add_dhcp_entry(lease->hostname, prot,
354 prot == AF_INET ? (struct all_addr *)&lease->addr : (struct all_addr *)&lease->hwaddr,
355 lease->expires);
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000356 }
357
358 dns_dirty = 0;
359 }
360}
361
362void lease_prune(struct dhcp_lease *target, time_t now)
363{
364 struct dhcp_lease *lease, *tmp, **up;
365
366 for (lease = leases, up = &leases; lease; lease = tmp)
367 {
368 tmp = lease->next;
369 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
370 {
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100371 file_dirty = 1;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000372 if (lease->hostname)
Simon Kelley7cebd202006-05-06 14:13:33 +0100373 dns_dirty = 1;
374
Simon Kelleyc72daea2012-01-05 21:33:27 +0000375 *up = lease->next; /* unlink */
Simon Kelley7cebd202006-05-06 14:13:33 +0100376
377 /* Put on old_leases list 'till we
378 can run the script */
379 lease->next = old_leases;
380 old_leases = lease;
381
Simon Kelley44a2a312004-03-10 20:04:35 +0000382 leases_left++;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000383 }
384 else
385 up = &lease->next;
386 }
387}
388
389
Simon Kelleycdeda282006-03-16 20:16:06 +0000390struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
Simon Kelley0a852542005-03-23 20:28:59 +0000391 unsigned char *clid, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000392{
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000393 struct dhcp_lease *lease;
394
Simon Kelley0a852542005-03-23 20:28:59 +0000395 if (clid)
396 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000397 {
398#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000399 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000400 continue;
401#endif
402 if (lease->clid && clid_len == lease->clid_len &&
403 memcmp(clid, lease->clid, clid_len) == 0)
404 return lease;
405 }
Simon Kelley0a852542005-03-23 20:28:59 +0000406
407 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000408 {
409#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000410 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000411 continue;
412#endif
413 if ((!lease->clid || !clid) &&
414 hw_len != 0 &&
415 lease->hwaddr_len == hw_len &&
416 lease->hwaddr_type == hw_type &&
417 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
418 return lease;
419 }
420
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000421 return NULL;
422}
423
424struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
425{
426 struct dhcp_lease *lease;
427
428 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000429 {
430#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000431 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000432 continue;
433#endif
434 if (lease->addr.s_addr == addr.s_addr)
435 return lease;
436 }
437
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000438 return NULL;
439}
440
Simon Kelley52b92f42012-01-22 16:05:15 +0000441#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000442/* addr or clid may be NULL for "don't care, both NULL resets "USED" flags both
443 set activates USED check */
444struct dhcp_lease *lease6_find(unsigned char *clid, int clid_len,
445 int lease_type, int iaid, struct in6_addr *addr)
Simon Kelley52b92f42012-01-22 16:05:15 +0000446{
447 struct dhcp_lease *lease;
448
449 for (lease = leases; lease; lease = lease->next)
450 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000451 if (!(lease->flags & lease_type) || lease->hwaddr_type != iaid)
452 continue;
Simon Kelley52b92f42012-01-22 16:05:15 +0000453
Simon Kelley4cb1b322012-02-06 14:30:41 +0000454 if (clid && addr && (lease->flags & LEASE_USED))
455 continue;
456
457 if (addr && memcmp(lease->hwaddr, addr, IN6ADDRSZ) != 0)
458 continue;
459
460 if (clid &&
461 (clid_len != lease->clid_len ||
462 memcmp(clid, lease->clid, clid_len) != 0))
463 continue;
464
465 if (clid || addr)
466 {
467 lease->flags |= LEASE_USED;
468 return lease;
469 }
470 else
471 lease->flags &= ~LEASE_USED;
Simon Kelley52b92f42012-01-22 16:05:15 +0000472 }
473
474 return NULL;
475}
476
477struct dhcp_lease *lease6_find_by_addr(struct in6_addr *net, int prefix, u64 addr)
478{
479 struct dhcp_lease *lease;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000480
Simon Kelley52b92f42012-01-22 16:05:15 +0000481 for (lease = leases; lease; lease = lease->next)
482 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000483 if (!(lease->flags & (LEASE_TA | LEASE_NA)))
Simon Kelley52b92f42012-01-22 16:05:15 +0000484 continue;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000485
Simon Kelley52b92f42012-01-22 16:05:15 +0000486 if (is_same_net6((struct in6_addr *)lease->hwaddr, net, prefix) &&
487 (prefix == 128 || addr6part((struct in6_addr *)lease->hwaddr) == addr))
488 return lease;
489 }
490
491 return NULL;
492}
Simon Kelley52b92f42012-01-22 16:05:15 +0000493#endif
494
Simon Kelley7de060b2011-08-26 17:24:52 +0100495/* Find largest assigned address in context */
496struct in_addr lease_find_max_addr(struct dhcp_context *context)
497{
498 struct dhcp_lease *lease;
499 struct in_addr addr = context->start;
500
501 if (!(context->flags & (CONTEXT_STATIC | CONTEXT_PROXY)))
502 for (lease = leases; lease; lease = lease->next)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000503 {
504#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000505 if (lease->flags & (LEASE_TA | LEASE_NA))
Simon Kelleyc72daea2012-01-05 21:33:27 +0000506 continue;
507#endif
508 if (((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(context->start.s_addr)) &&
509 ((unsigned)ntohl(lease->addr.s_addr)) <= ((unsigned)ntohl(context->end.s_addr)) &&
510 ((unsigned)ntohl(lease->addr.s_addr)) > ((unsigned)ntohl(addr.s_addr)))
511 addr = lease->addr;
512 }
Simon Kelley7de060b2011-08-26 17:24:52 +0100513
514 return addr;
515}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000516
Simon Kelleyc72daea2012-01-05 21:33:27 +0000517static struct dhcp_lease *lease_allocate(void)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000518{
519 struct dhcp_lease *lease;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100520 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000521 return NULL;
522
Simon Kelley7cebd202006-05-06 14:13:33 +0100523 memset(lease, 0, sizeof(struct dhcp_lease));
Simon Kelley4cb1b322012-02-06 14:30:41 +0000524 lease->flags = LEASE_NEW;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000525 lease->expires = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100526#ifdef HAVE_BROKEN_RTC
527 lease->length = 0xffffffff; /* illegal value */
528#endif
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000529 lease->next = leases;
530 leases = lease;
531
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100532 file_dirty = 1;
Simon Kelley44a2a312004-03-10 20:04:35 +0000533 leases_left--;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000534
535 return lease;
536}
537
Simon Kelley52b92f42012-01-22 16:05:15 +0000538struct dhcp_lease *lease4_allocate(struct in_addr addr)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000539{
540 struct dhcp_lease *lease = lease_allocate();
541 lease->addr = addr;
542 lease->hwaddr_len = 256; /* illegal value */
543
544 return lease;
545}
546
547#ifdef HAVE_DHCP6
Simon Kelley4cb1b322012-02-06 14:30:41 +0000548struct dhcp_lease *lease6_allocate(struct in6_addr *addrp, int lease_type)
Simon Kelleyc72daea2012-01-05 21:33:27 +0000549{
550 struct dhcp_lease *lease = lease_allocate();
551 memcpy(lease->hwaddr, addrp, sizeof(*addrp)) ;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000552 lease->flags |= lease_type;
Simon Kelleyc72daea2012-01-05 21:33:27 +0000553
554 return lease;
555}
556#endif
557
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100558void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000559{
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100560 time_t exp = now + (time_t)len;
561
562 if (len == 0xffffffff)
563 {
564 exp = 0;
565 len = 0;
566 }
567
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000568 if (exp != lease->expires)
Simon Kelley0a852542005-03-23 20:28:59 +0000569 {
Simon Kelley0a852542005-03-23 20:28:59 +0000570 dns_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100571 lease->expires = exp;
572#ifndef HAVE_BROKEN_RTC
Simon Kelley4cb1b322012-02-06 14:30:41 +0000573 lease->flags |= LEASE_AUX_CHANGED;
574 file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100575#endif
Simon Kelley0a852542005-03-23 20:28:59 +0000576 }
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100577
578#ifdef HAVE_BROKEN_RTC
579 if (len != lease->length)
580 {
581 lease->length = len;
Simon Kelley208b65c2006-08-05 21:41:37 +0100582 lease->aux_changed = file_dirty = 1;
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100583 }
584#endif
585}
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000586
Simon Kelley7cebd202006-05-06 14:13:33 +0100587void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
Simon Kelleycdeda282006-03-16 20:16:06 +0000588 unsigned char *clid, int hw_len, int hw_type, int clid_len)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000589{
Simon Kelleycdeda282006-03-16 20:16:06 +0000590 if (hw_len != lease->hwaddr_len ||
591 hw_type != lease->hwaddr_type ||
Simon Kelley7cebd202006-05-06 14:13:33 +0100592 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000593 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000594 if (hw_len != 0)
595 memcpy(lease->hwaddr, hwaddr, hw_len);
Simon Kelleycdeda282006-03-16 20:16:06 +0000596 lease->hwaddr_len = hw_len;
597 lease->hwaddr_type = hw_type;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000598 lease->flags |= LEASE_CHANGED;
599 file_dirty = 1; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000600 }
Simon Kelley0a852542005-03-23 20:28:59 +0000601
602 /* only update clid when one is available, stops packets
603 without a clid removing the record. Lease init uses
604 clid_len == 0 for no clid. */
605 if (clid_len != 0 && clid)
606 {
607 if (!lease->clid)
608 lease->clid_len = 0;
609
610 if (lease->clid_len != clid_len)
611 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000612 lease->flags |= LEASE_AUX_CHANGED;
613 file_dirty = 1;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100614 free(lease->clid);
615 if (!(lease->clid = whine_malloc(clid_len)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100616 return;
Simon Kelley0a852542005-03-23 20:28:59 +0000617 }
618 else if (memcmp(lease->clid, clid, clid_len) != 0)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000619 {
620 lease->flags |= LEASE_AUX_CHANGED;
621 file_dirty = 1;
622 }
623
Simon Kelley0a852542005-03-23 20:28:59 +0000624 lease->clid_len = clid_len;
625 memcpy(lease->clid, clid, clid_len);
626 }
Simon Kelley208b65c2006-08-05 21:41:37 +0100627
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000628}
629
Simon Kelley9009d742008-11-14 20:04:27 +0000630static void kill_name(struct dhcp_lease *lease)
631{
632 /* run script to say we lost our old name */
633
634 /* this shouldn't happen unless updates are very quick and the
635 script very slow, we just avoid a memory leak if it does. */
636 free(lease->old_hostname);
637
638 /* If we know the fqdn, pass that. The helper will derive the
Simon Kelley4cb1b322012-02-06 14:30:41 +0000639 unqualified name from it, free the unqualified name here. */
Simon Kelley9009d742008-11-14 20:04:27 +0000640
641 if (lease->fqdn)
642 {
643 lease->old_hostname = lease->fqdn;
644 free(lease->hostname);
645 }
646 else
647 lease->old_hostname = lease->hostname;
648
649 lease->hostname = lease->fqdn = NULL;
650}
651
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000652void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth, char *domain, char *config_domain)
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000653{
654 struct dhcp_lease *lease_tmp;
655 char *new_name = NULL, *new_fqdn = NULL;
Simon Kelley70c5e3e2012-02-06 22:05:15 +0000656
657 if (config_domain && (!domain || !hostname_isequal(domain, config_domain)))
658 my_syslog(MS_DHCP | LOG_WARNING, _("Ignoring domain %s for DHCP host name %s"), config_domain, name);
Simon Kelley9009d742008-11-14 20:04:27 +0000659
Simon Kelleya2226412004-05-13 20:27:08 +0100660 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000661 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000662 if (auth)
663 lease->flags |= LEASE_AUTH_NAME;
Simon Kelleyb8187c82005-11-26 21:46:27 +0000664 return;
665 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100666
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000667 if (!name && !lease->hostname)
668 return;
669
670 /* If a machine turns up on a new net without dropping the old lease,
671 or two machines claim the same name, then we end up with two interfaces with
Simon Kelleyb8187c82005-11-26 21:46:27 +0000672 the same name. Check for that here and remove the name from the old lease.
Simon Kelley4cb1b322012-02-06 14:30:41 +0000673 Note that IPv6 leases are different. All the leases to the same DUID are
674 allowed the same name.
675
Simon Kelleyb8187c82005-11-26 21:46:27 +0000676 Don't allow a name from the client to override a name from dnsmasq config. */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000677
678 if (name)
679 {
Simon Kelley9009d742008-11-14 20:04:27 +0000680 if ((new_name = whine_malloc(strlen(name) + 1)))
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000681 {
Simon Kelley9009d742008-11-14 20:04:27 +0000682 strcpy(new_name, name);
Simon Kelley4cb1b322012-02-06 14:30:41 +0000683 if (domain && (new_fqdn = whine_malloc(strlen(new_name) + strlen(domain) + 2)))
Simon Kelley9009d742008-11-14 20:04:27 +0000684 {
685 strcpy(new_fqdn, name);
686 strcat(new_fqdn, ".");
Simon Kelley4cb1b322012-02-06 14:30:41 +0000687 strcat(new_fqdn, domain);
Simon Kelley9009d742008-11-14 20:04:27 +0000688 }
689 }
690
691 /* Depending on mode, we check either unqualified name or FQDN. */
692 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
693 {
Simon Kelley28866e92011-02-14 20:19:14 +0000694 if (option_bool(OPT_DHCP_FQDN))
Simon Kelley9009d742008-11-14 20:04:27 +0000695 {
Simon Kelley4cb1b322012-02-06 14:30:41 +0000696 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn))
Simon Kelley9009d742008-11-14 20:04:27 +0000697 continue;
698 }
699 else
700 {
701 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
702 continue;
703 }
Simon Kelley4cb1b322012-02-06 14:30:41 +0000704
705 if (lease->flags & (LEASE_TA | LEASE_NA))
706 {
707 if (!(lease_tmp->flags & (LEASE_TA | LEASE_NA)))
708 continue;
709
Simon Kelleyceae00d2012-02-09 21:28:14 +0000710 /* another lease for the same DUID is OK for IPv6 */
Simon Kelley4cb1b322012-02-06 14:30:41 +0000711 if (lease->clid_len == lease_tmp->clid_len &&
712 lease->clid && lease_tmp->clid &&
713 memcmp(lease->clid, lease_tmp->clid, lease->clid_len) == 0)
714 continue;
715 }
716 else if (lease_tmp->flags & (LEASE_TA | LEASE_NA))
717 continue;
718
719 if ((lease_tmp->flags & LEASE_AUTH_NAME) && !auth)
Simon Kelley9009d742008-11-14 20:04:27 +0000720 {
721 free(new_name);
722 free(new_fqdn);
723 return;
724 }
725
726 kill_name(lease_tmp);
727 break;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000728 }
729 }
730
731 if (lease->hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000732 kill_name(lease);
Simon Kelley16972692006-10-16 20:04:18 +0100733
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000734 lease->hostname = new_name;
735 lease->fqdn = new_fqdn;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000736
737 if (auth)
738 lease->flags |= LEASE_AUTH_NAME;
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000739
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100740 file_dirty = 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100741 dns_dirty = 1;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000742 lease->flags |= LEASE_CHANGED; /* run script on change */
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000743}
744
Simon Kelley824af852008-02-12 20:43:05 +0000745void lease_set_interface(struct dhcp_lease *lease, int interface)
746{
747 if (lease->last_interface == interface)
748 return;
749
750 lease->last_interface = interface;
Simon Kelley4cb1b322012-02-06 14:30:41 +0000751 lease->flags |= LEASE_CHANGED;
Simon Kelley824af852008-02-12 20:43:05 +0000752}
753
Simon Kelley5aabfc72007-08-29 11:24:47 +0100754void rerun_scripts(void)
755{
756 struct dhcp_lease *lease;
757
758 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000759 lease->flags |= LEASE_CHANGED;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100760}
761
Simon Kelley7cebd202006-05-06 14:13:33 +0100762/* deleted leases get transferred to the old_leases list.
763 remove them here, after calling the lease change
Simon Kelley16972692006-10-16 20:04:18 +0100764 script. Also run the lease change script on new/modified leases.
765
766 Return zero if nothing to do. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100767int do_script_run(time_t now)
Simon Kelley7cebd202006-05-06 14:13:33 +0100768{
769 struct dhcp_lease *lease;
770
Simon Kelley9009d742008-11-14 20:04:27 +0000771#ifdef HAVE_DBUS
772 /* If we're going to be sending DBus signals, but the connection is not yet up,
773 delay everything until it is. */
Simon Kelley28866e92011-02-14 20:19:14 +0000774 if (option_bool(OPT_DBUS) && !daemon->dbus)
Simon Kelley9009d742008-11-14 20:04:27 +0000775 return 0;
776#endif
777
Simon Kelley16972692006-10-16 20:04:18 +0100778 if (old_leases)
Simon Kelley7cebd202006-05-06 14:13:33 +0100779 {
Simon Kelley7cebd202006-05-06 14:13:33 +0100780 lease = old_leases;
Simon Kelley16972692006-10-16 20:04:18 +0100781
782 /* If the lease still has an old_hostname, do the "old" action on that first */
783 if (lease->old_hostname)
784 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100785#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100786 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
787#endif
Simon Kelley16972692006-10-16 20:04:18 +0100788 free(lease->old_hostname);
789 lease->old_hostname = NULL;
790 return 1;
791 }
792 else
793 {
Simon Kelley9009d742008-11-14 20:04:27 +0000794 kill_name(lease);
Simon Kelley1f15b812009-10-13 17:49:32 +0100795#ifdef HAVE_SCRIPT
Simon Kelley9009d742008-11-14 20:04:27 +0000796 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100797#endif
Simon Kelley1f15b812009-10-13 17:49:32 +0100798#ifdef HAVE_DBUS
799 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
800#endif
Simon Kelley16972692006-10-16 20:04:18 +0100801 old_leases = lease->next;
802
Simon Kelley9009d742008-11-14 20:04:27 +0000803 free(lease->old_hostname);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100804 free(lease->clid);
Simon Kelley316e2732010-01-22 20:16:09 +0000805 free(lease->extradata);
Simon Kelley16972692006-10-16 20:04:18 +0100806 free(lease);
807
808 return 1;
809 }
Simon Kelley7cebd202006-05-06 14:13:33 +0100810 }
Simon Kelley16972692006-10-16 20:04:18 +0100811
812 /* make sure we announce the loss of a hostname before its new location. */
813 for (lease = leases; lease; lease = lease->next)
814 if (lease->old_hostname)
815 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100816#ifdef HAVE_SCRIPT
Simon Kelley5aabfc72007-08-29 11:24:47 +0100817 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
818#endif
Simon Kelley16972692006-10-16 20:04:18 +0100819 free(lease->old_hostname);
820 lease->old_hostname = NULL;
821 return 1;
822 }
823
Simon Kelley7cebd202006-05-06 14:13:33 +0100824 for (lease = leases; lease; lease = lease->next)
Simon Kelley4cb1b322012-02-06 14:30:41 +0000825 if ((lease->flags & (LEASE_NEW | LEASE_CHANGED)) ||
826 ((lease->flags & LEASE_AUX_CHANGED) && option_bool(OPT_LEASE_RO)))
Simon Kelley7cebd202006-05-06 14:13:33 +0100827 {
Simon Kelley1f15b812009-10-13 17:49:32 +0100828#ifdef HAVE_SCRIPT
Simon Kelley4cb1b322012-02-06 14:30:41 +0000829 queue_script((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley9009d742008-11-14 20:04:27 +0000830 lease->fqdn ? lease->fqdn : lease->hostname, now);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100831#endif
Simon Kelley1f15b812009-10-13 17:49:32 +0100832#ifdef HAVE_DBUS
Simon Kelley4cb1b322012-02-06 14:30:41 +0000833 emit_dbus_signal((lease->flags & LEASE_NEW) ? ACTION_ADD : ACTION_OLD, lease,
Simon Kelley1f15b812009-10-13 17:49:32 +0100834 lease->fqdn ? lease->fqdn : lease->hostname);
835#endif
Simon Kelley4cb1b322012-02-06 14:30:41 +0000836 lease->flags &= ~(LEASE_NEW | LEASE_CHANGED | LEASE_AUX_CHANGED);
Simon Kelley16972692006-10-16 20:04:18 +0100837
Simon Kelley316e2732010-01-22 20:16:09 +0000838 /* this is used for the "add" call, then junked, since they're not in the database */
839 free(lease->extradata);
840 lease->extradata = NULL;
Simon Kelley16972692006-10-16 20:04:18 +0100841
842 return 1;
Simon Kelley7cebd202006-05-06 14:13:33 +0100843 }
Simon Kelley16972692006-10-16 20:04:18 +0100844
845 return 0; /* nothing to do */
Simon Kelley7cebd202006-05-06 14:13:33 +0100846}
Simon Kelley7622fc02009-06-04 20:32:05 +0100847
Simon Kelleyceae00d2012-02-09 21:28:14 +0000848#ifdef HAVE_SCRIPT
849void lease_add_extradata(struct dhcp_lease *lease, unsigned char *data, unsigned int len, int delim)
850{
851 unsigned int i;
852
853 /* check for embeded NULLs */
854 for (i = 0; i < len; i++)
855 if (data[i] == 0)
856 {
857 len = i;
858 break;
859 }
860
861 if ((lease->extradata_size - lease->extradata_len) < (len + 1))
862 {
863 size_t newsz = lease->extradata_len + len + 100;
864 unsigned char *new = whine_malloc(newsz);
865
866 if (!new)
867 return;
868
869 if (lease->extradata)
870 {
871 memcpy(new, lease->extradata, lease->extradata_len);
872 free(lease->extradata);
873 }
874
875 lease->extradata = new;
876 lease->extradata_size = newsz;
877 }
878
879 if (len != 0)
880 memcpy(lease->extradata + lease->extradata_len, data, len);
881 lease->extradata[lease->extradata_len + len] = delim;
882 lease->extradata_len += len + 1;
883}
884#endif
885
Simon Kelley7622fc02009-06-04 20:32:05 +0100886#endif
Simon Kelley7cebd202006-05-06 14:13:33 +0100887
888
889
Simon Kelley9e4abcb2004-01-22 19:47:41 +0000890