1 /* dnsmasq is Copyright (c) 2000-2011 Simon Kelley
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
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
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.
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/>.
21 static struct dhcp_lease *leases = NULL, *old_leases = NULL;
22 static int dns_dirty, file_dirty, leases_left;
24 void lease_init(time_t now)
28 struct dhcp_lease *lease;
29 int clid_len, hw_len, hw_type;
32 /* These each hold a DHCP option max size 255
33 and get a terminating zero added */
34 daemon->dhcp_buff = safe_malloc(256);
35 daemon->dhcp_buff2 = safe_malloc(256);
36 daemon->dhcp_buff3 = safe_malloc(256);
38 leases_left = daemon->dhcp_max;
40 if (option_bool(OPT_LEASE_RO))
42 /* run "<lease_change_script> init" once to get the
43 initial state of the database. If leasefile-ro is
44 set without a script, we just do without any
47 if (daemon->lease_change_command)
49 strcpy(daemon->dhcp_buff, daemon->lease_change_command);
50 strcat(daemon->dhcp_buff, " init");
51 leasestream = popen(daemon->dhcp_buff, "r");
56 file_dirty = dns_dirty = 0;
63 /* NOTE: need a+ mode to create file if it doesn't exist */
64 leasestream = daemon->lease_stream = fopen(daemon->lease_file, "a+");
67 die(_("cannot open or create lease file %s: %s"), daemon->lease_file, EC_FILE);
69 /* a+ mode leaves pointer at end. */
73 /* client-id max length is 255 which is 255*2 digits + 254 colons
74 borrow DNS packet buffer which is always larger than 1000 bytes */
76 while (fscanf(leasestream, "%lu %255s %16s %255s %764s",
77 &ei, daemon->dhcp_buff2, daemon->namebuff,
78 daemon->dhcp_buff, daemon->packet) == 5)
80 hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
81 /* For backwards compatibility, no explict MAC address type means ether. */
82 if (hw_type == 0 && hw_len != 0)
83 hw_type = ARPHRD_ETHER;
85 addr.s_addr = inet_addr(daemon->namebuff);
87 /* decode hex in place */
89 if (strcmp(daemon->packet, "*") != 0)
90 clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL);
92 if (!(lease = lease_allocate(addr)))
93 die (_("too many stored leases"), NULL, EC_MISC);
95 #ifdef HAVE_BROKEN_RTC
97 lease->expires = (time_t)ei + now;
99 lease->expires = (time_t)0;
102 /* strictly time_t is opaque, but this hack should work on all sane systems,
103 even when sizeof(time_t) == 8 */
104 lease->expires = (time_t)ei;
107 lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet, hw_len, hw_type, clid_len);
109 if (strcmp(daemon->dhcp_buff, "*") != 0)
110 lease_set_hostname(lease, daemon->dhcp_buff, 0);
112 /* set these correctly: the "old" events are generated later from
113 the startup synthesised SIGHUP. */
114 lease->new = lease->changed = 0;
118 if (!daemon->lease_stream)
122 /* shell returns 127 for "command not found", 126 for bad permissions. */
123 if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
125 if (WEXITSTATUS(rc) == 127)
127 else if (WEXITSTATUS(rc) == 126)
129 die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command, EC_FILE);
132 if (WEXITSTATUS(rc) != 0)
134 sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
135 die(_("lease-init script returned exit code %s"), daemon->dhcp_buff, WEXITSTATUS(rc) + EC_INIT_OFFSET);
140 /* Some leases may have expired */
142 lease_prune(NULL, now);
146 void lease_update_from_configs(void)
148 /* changes to the config may change current leases. */
150 struct dhcp_lease *lease;
151 struct dhcp_config *config;
154 for (lease = leases; lease; lease = lease->next)
155 if ((config = find_config(daemon->dhcp_conf, NULL, lease->clid, lease->clid_len,
156 lease->hwaddr, lease->hwaddr_len, lease->hwaddr_type, NULL)) &&
157 (config->flags & CONFIG_NAME) &&
158 (!(config->flags & CONFIG_ADDR) || config->addr.s_addr == lease->addr.s_addr))
159 lease_set_hostname(lease, config->hostname, 1);
160 else if ((name = host_from_dns(lease->addr)))
161 lease_set_hostname(lease, name, 1); /* updates auth flag only */
164 static void ourprintf(int *errp, char *format, ...)
168 va_start(ap, format);
169 if (!(*errp) && vfprintf(daemon->lease_stream, format, ap) < 0)
174 void lease_update_file(time_t now)
176 struct dhcp_lease *lease;
180 if (file_dirty != 0 && daemon->lease_stream)
183 rewind(daemon->lease_stream);
184 if (errno != 0 || ftruncate(fileno(daemon->lease_stream), 0) != 0)
187 for (lease = leases; lease; lease = lease->next)
189 #ifdef HAVE_BROKEN_RTC
190 ourprintf(&err, "%u ", lease->length);
192 ourprintf(&err, "%lu ", (unsigned long)lease->expires);
194 if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
195 ourprintf(&err, "%.2x-", lease->hwaddr_type);
196 for (i = 0; i < lease->hwaddr_len; i++)
198 ourprintf(&err, "%.2x", lease->hwaddr[i]);
199 if (i != lease->hwaddr_len - 1)
200 ourprintf(&err, ":");
203 ourprintf(&err, " %s ", inet_ntoa(lease->addr));
204 ourprintf(&err, "%s ", lease->hostname ? lease->hostname : "*");
206 if (lease->clid && lease->clid_len != 0)
208 for (i = 0; i < lease->clid_len - 1; i++)
209 ourprintf(&err, "%.2x:", lease->clid[i]);
210 ourprintf(&err, "%.2x\n", lease->clid[i]);
213 ourprintf(&err, "*\n");
216 if (fflush(daemon->lease_stream) != 0 ||
217 fsync(fileno(daemon->lease_stream)) < 0)
224 /* Set alarm for when the first lease expires + slop. */
225 for (next_event = 0, lease = leases; lease; lease = lease->next)
226 if (lease->expires != 0 &&
227 (next_event == 0 || difftime(next_event, lease->expires + 10) > 0.0))
228 next_event = lease->expires + 10;
232 if (next_event == 0 || difftime(next_event, LEASE_RETRY + now) > 0.0)
233 next_event = LEASE_RETRY + now;
235 my_syslog(MS_DHCP | LOG_ERR, _("failed to write %s: %s (retry in %us)"),
236 daemon->lease_file, strerror(err),
237 (unsigned int)difftime(next_event, now));
241 alarm((unsigned)difftime(next_event, now));
244 void lease_update_dns(void)
246 struct dhcp_lease *lease;
248 if (daemon->port != 0 && dns_dirty)
252 for (lease = leases; lease; lease = lease->next)
255 cache_add_dhcp_entry(lease->fqdn, &lease->addr, lease->expires);
257 if (!option_bool(OPT_DHCP_FQDN) && lease->hostname)
258 cache_add_dhcp_entry(lease->hostname, &lease->addr, lease->expires);
265 void lease_prune(struct dhcp_lease *target, time_t now)
267 struct dhcp_lease *lease, *tmp, **up;
269 for (lease = leases, up = &leases; lease; lease = tmp)
272 if ((lease->expires != 0 && difftime(now, lease->expires) > 0) || lease == target)
278 *up = lease->next; /* unlink */
280 /* Put on old_leases list 'till we
281 can run the script */
282 lease->next = old_leases;
293 struct dhcp_lease *lease_find_by_client(unsigned char *hwaddr, int hw_len, int hw_type,
294 unsigned char *clid, int clid_len)
296 struct dhcp_lease *lease;
299 for (lease = leases; lease; lease = lease->next)
300 if (lease->clid && clid_len == lease->clid_len &&
301 memcmp(clid, lease->clid, clid_len) == 0)
304 for (lease = leases; lease; lease = lease->next)
305 if ((!lease->clid || !clid) &&
307 lease->hwaddr_len == hw_len &&
308 lease->hwaddr_type == hw_type &&
309 memcmp(hwaddr, lease->hwaddr, hw_len) == 0)
315 struct dhcp_lease *lease_find_by_addr(struct in_addr addr)
317 struct dhcp_lease *lease;
319 for (lease = leases; lease; lease = lease->next)
320 if (lease->addr.s_addr == addr.s_addr)
327 struct dhcp_lease *lease_allocate(struct in_addr addr)
329 struct dhcp_lease *lease;
330 if (!leases_left || !(lease = whine_malloc(sizeof(struct dhcp_lease))))
333 memset(lease, 0, sizeof(struct dhcp_lease));
336 lease->hwaddr_len = 256; /* illegal value */
338 #ifdef HAVE_BROKEN_RTC
339 lease->length = 0xffffffff; /* illegal value */
341 lease->next = leases;
350 void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
352 time_t exp = now + (time_t)len;
354 if (len == 0xffffffff)
360 if (exp != lease->expires)
363 lease->expires = exp;
364 #ifndef HAVE_BROKEN_RTC
365 lease->aux_changed = file_dirty = 1;
369 #ifdef HAVE_BROKEN_RTC
370 if (len != lease->length)
373 lease->aux_changed = file_dirty = 1;
378 void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
379 unsigned char *clid, int hw_len, int hw_type, int clid_len)
381 if (hw_len != lease->hwaddr_len ||
382 hw_type != lease->hwaddr_type ||
383 (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
385 memcpy(lease->hwaddr, hwaddr, hw_len);
386 lease->hwaddr_len = hw_len;
387 lease->hwaddr_type = hw_type;
388 lease->changed = file_dirty = 1; /* run script on change */
391 /* only update clid when one is available, stops packets
392 without a clid removing the record. Lease init uses
393 clid_len == 0 for no clid. */
394 if (clid_len != 0 && clid)
399 if (lease->clid_len != clid_len)
401 lease->aux_changed = file_dirty = 1;
403 if (!(lease->clid = whine_malloc(clid_len)))
406 else if (memcmp(lease->clid, clid, clid_len) != 0)
407 lease->aux_changed = file_dirty = 1;
409 lease->clid_len = clid_len;
410 memcpy(lease->clid, clid, clid_len);
415 static void kill_name(struct dhcp_lease *lease)
417 /* run script to say we lost our old name */
419 /* this shouldn't happen unless updates are very quick and the
420 script very slow, we just avoid a memory leak if it does. */
421 free(lease->old_hostname);
423 /* If we know the fqdn, pass that. The helper will derive the
424 unqualified name from it, free the unqulaified name here. */
428 lease->old_hostname = lease->fqdn;
429 free(lease->hostname);
432 lease->old_hostname = lease->hostname;
434 lease->hostname = lease->fqdn = NULL;
437 void lease_set_hostname(struct dhcp_lease *lease, char *name, int auth)
439 struct dhcp_lease *lease_tmp;
440 char *new_name = NULL, *new_fqdn = NULL;
442 if (lease->hostname && name && hostname_isequal(lease->hostname, name))
444 lease->auth_name = auth;
448 if (!name && !lease->hostname)
451 /* If a machine turns up on a new net without dropping the old lease,
452 or two machines claim the same name, then we end up with two interfaces with
453 the same name. Check for that here and remove the name from the old lease.
454 Don't allow a name from the client to override a name from dnsmasq config. */
458 if ((new_name = whine_malloc(strlen(name) + 1)))
460 char *suffix = get_domain(lease->addr);
461 strcpy(new_name, name);
462 if (suffix && (new_fqdn = whine_malloc(strlen(new_name) + strlen(suffix) + 2)))
464 strcpy(new_fqdn, name);
465 strcat(new_fqdn, ".");
466 strcat(new_fqdn, suffix);
470 /* Depending on mode, we check either unqualified name or FQDN. */
471 for (lease_tmp = leases; lease_tmp; lease_tmp = lease_tmp->next)
473 if (option_bool(OPT_DHCP_FQDN))
475 if (!new_fqdn || !lease_tmp->fqdn || !hostname_isequal(lease_tmp->fqdn, new_fqdn) )
480 if (!new_name || !lease_tmp->hostname || !hostname_isequal(lease_tmp->hostname, new_name) )
484 if (lease_tmp->auth_name && !auth)
491 kill_name(lease_tmp);
499 lease->hostname = new_name;
500 lease->fqdn = new_fqdn;
501 lease->auth_name = auth;
505 lease->changed = 1; /* run script on change */
508 void lease_set_interface(struct dhcp_lease *lease, int interface)
510 if (lease->last_interface == interface)
513 lease->last_interface = interface;
517 void rerun_scripts(void)
519 struct dhcp_lease *lease;
521 for (lease = leases; lease; lease = lease->next)
525 /* deleted leases get transferred to the old_leases list.
526 remove them here, after calling the lease change
527 script. Also run the lease change script on new/modified leases.
529 Return zero if nothing to do. */
530 int do_script_run(time_t now)
532 struct dhcp_lease *lease;
535 /* If we're going to be sending DBus signals, but the connection is not yet up,
536 delay everything until it is. */
537 if (option_bool(OPT_DBUS) && !daemon->dbus)
545 /* If the lease still has an old_hostname, do the "old" action on that first */
546 if (lease->old_hostname)
549 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
551 free(lease->old_hostname);
552 lease->old_hostname = NULL;
559 queue_script(ACTION_DEL, lease, lease->old_hostname, now);
562 emit_dbus_signal(ACTION_DEL, lease, lease->old_hostname);
564 old_leases = lease->next;
566 free(lease->old_hostname);
568 free(lease->extradata);
575 /* make sure we announce the loss of a hostname before its new location. */
576 for (lease = leases; lease; lease = lease->next)
577 if (lease->old_hostname)
580 queue_script(ACTION_OLD_HOSTNAME, lease, lease->old_hostname, now);
582 free(lease->old_hostname);
583 lease->old_hostname = NULL;
587 for (lease = leases; lease; lease = lease->next)
588 if (lease->new || lease->changed ||
589 (lease->aux_changed && option_bool(OPT_LEASE_RO)))
592 queue_script(lease->new ? ACTION_ADD : ACTION_OLD, lease,
593 lease->fqdn ? lease->fqdn : lease->hostname, now);
596 emit_dbus_signal(lease->new ? ACTION_ADD : ACTION_OLD, lease,
597 lease->fqdn ? lease->fqdn : lease->hostname);
599 lease->new = lease->changed = lease->aux_changed = 0;
601 /* this is used for the "add" call, then junked, since they're not in the database */
602 free(lease->extradata);
603 lease->extradata = NULL;
608 return 0; /* nothing to do */