import of dnsmasq-2.33.tar.gz

This commit is contained in:
Simon Kelley
2006-08-05 21:41:37 +01:00
parent 849a8357ba
commit 208b65c5cf
32 changed files with 2364 additions and 1534 deletions

View File

@@ -21,60 +21,103 @@ void lease_init(struct daemon *daemon, time_t now)
struct in_addr addr;
struct dhcp_lease *lease;
int flags, clid_len, hw_len, hw_type;
FILE *leasestream;
leases = old_leases = NULL;
leases_left = daemon->dhcp_max;
/* NOTE: need a+ mode to create file if it doesn't exist */
if (!(daemon->lease_stream = fopen(daemon->lease_file, "a+")))
die(_("cannot open or create leases file: %s"), NULL);
flags = fcntl(fileno(daemon->lease_stream), F_GETFD);
if (flags != -1)
fcntl(fileno(daemon->lease_stream), F_SETFD, flags | FD_CLOEXEC);
if (daemon->options & OPT_LEASE_RO)
{
/* run "<lease_change_script> init" once to get the
initial state of the database. If leasefile-ro is
set without a script, we just do without any
lease database. */
if (!daemon->lease_change_command)
{
file_dirty = dns_dirty = 0;
return;
}
strcpy(daemon->dhcp_buff, daemon->lease_change_command);
strcat(daemon->dhcp_buff, " init");
leasestream = popen(daemon->dhcp_buff, "r");
}
else
{
/* NOTE: need a+ mode to create file if it doesn't exist */
leasestream = daemon->lease_stream = fopen(daemon->lease_file, "a+");
if (!leasestream)
die(_("cannot open or create lease file %s: %s"), daemon->lease_file);
flags = fcntl(fileno(leasestream), F_GETFD);
if (flags != -1)
fcntl(fileno(leasestream), F_SETFD, flags | FD_CLOEXEC);
/* a+ mode lease pointer at end. */
rewind(leasestream);
}
/* a+ mode lease pointer at end. */
rewind(daemon->lease_stream);
/* client-id max length is 255 which is 255*2 digits + 254 colons
borrow DNS packet buffer which is always larger than 1000 bytes */
while (fscanf(daemon->lease_stream, "%lu %255s %16s %255s %764s",
&ei, daemon->dhcp_buff2, daemon->namebuff,
daemon->dhcp_buff, daemon->packet) == 5)
{
hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
/* For backwards compatibility, no explict MAC address type means ether. */
if (hw_type == 0 && hw_len != 0)
hw_type = ARPHRD_ETHER;
addr.s_addr = inet_addr(daemon->namebuff);
/* decode hex in place */
clid_len = 0;
if (strcmp(daemon->packet, "*") != 0)
clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL);
if (!(lease = lease_allocate(addr)))
die (_("too many stored leases"), NULL);
/* not actually new */
lease->new = 0;
if (leasestream)
while (fscanf(leasestream, "%lu %255s %16s %255s %764s",
&ei, daemon->dhcp_buff2, daemon->namebuff,
daemon->dhcp_buff, daemon->packet) == 5)
{
hw_len = parse_hex(daemon->dhcp_buff2, (unsigned char *)daemon->dhcp_buff2, DHCP_CHADDR_MAX, NULL, &hw_type);
/* For backwards compatibility, no explict MAC address type means ether. */
if (hw_type == 0 && hw_len != 0)
hw_type = ARPHRD_ETHER;
addr.s_addr = inet_addr(daemon->namebuff);
/* decode hex in place */
clid_len = 0;
if (strcmp(daemon->packet, "*") != 0)
clid_len = parse_hex(daemon->packet, (unsigned char *)daemon->packet, 255, NULL, NULL);
if (!(lease = lease_allocate(addr)))
die (_("too many stored leases"), NULL);
/* not actually new */
lease->new = 0;
#ifdef HAVE_BROKEN_RTC
if (ei != 0)
lease->expires = (time_t)ei + now;
else
lease->expires = (time_t)0;
lease->length = ei;
if (ei != 0)
lease->expires = (time_t)ei + now;
else
lease->expires = (time_t)0;
lease->length = ei;
#else
/* strictly time_t is opaque, but this hack should work on all sane systems,
even when sizeof(time_t) == 8 */
lease->expires = (time_t)ei;
/* strictly time_t is opaque, but this hack should work on all sane systems,
even when sizeof(time_t) == 8 */
lease->expires = (time_t)ei;
#endif
lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet, hw_len, hw_type, clid_len);
if (strcmp(daemon->dhcp_buff, "*") != 0)
lease_set_hostname(lease, daemon->dhcp_buff, daemon->domain_suffix, 0);
}
if (!daemon->lease_stream)
{
int rc = 0;
lease_set_hwaddr(lease, (unsigned char *)daemon->dhcp_buff2, (unsigned char *)daemon->packet, hw_len, hw_type, clid_len);
if (strcmp(daemon->dhcp_buff, "*") != 0)
lease_set_hostname(lease, daemon->dhcp_buff, daemon->domain_suffix, 0);
/* shell returns 127 for "command not found", 126 for bad permissions. */
if (!leasestream || (rc = pclose(leasestream)) == -1 || WEXITSTATUS(rc) == 127 || WEXITSTATUS(rc) == 126)
{
if (WEXITSTATUS(rc) == 127)
errno = ENOENT;
else if (WEXITSTATUS(rc) == 126)
errno = EACCES;
die(_("cannot run lease-init script %s: %s"), daemon->lease_change_command);
}
if (WEXITSTATUS(rc) != 0)
{
sprintf(daemon->dhcp_buff, "%d", WEXITSTATUS(rc));
die(_("lease-init script returned exit code %s"), daemon->dhcp_buff);
}
}
/* Some leases may have expired */
@@ -117,7 +160,7 @@ void lease_update_file(struct daemon *daemon, time_t now)
time_t next_event;
int i, err = 0;
if (file_dirty != 0)
if (file_dirty != 0 && daemon->lease_stream)
{
errno = 0;
rewind(daemon->lease_stream);
@@ -298,7 +341,7 @@ void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
dns_dirty = 1;
lease->expires = exp;
#ifndef HAVE_BROKEN_RTC
file_dirty = 1;
lease->aux_changed = file_dirty = 1;
#endif
}
@@ -306,7 +349,7 @@ void lease_set_expires(struct dhcp_lease *lease, unsigned int len, time_t now)
if (len != lease->length)
{
lease->length = len;
file_dirty = 1;
lease->aux_changed = file_dirty = 1;
}
#endif
}
@@ -318,11 +361,10 @@ void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
hw_type != lease->hwaddr_type ||
(hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
{
file_dirty = 1;
memcpy(lease->hwaddr, hwaddr, hw_len);
lease->hwaddr_len = hw_len;
lease->hwaddr_type = hw_type;
lease->old = 1; /* run script on change */
lease->changed = file_dirty = 1; /* run script on change */
}
/* only update clid when one is available, stops packets
@@ -335,18 +377,19 @@ void lease_set_hwaddr(struct dhcp_lease *lease, unsigned char *hwaddr,
if (lease->clid_len != clid_len)
{
file_dirty = 1;
lease->aux_changed = file_dirty = 1;
if (lease->clid)
free(lease->clid);
if (!(lease->clid = malloc(clid_len)))
return;
}
else if (memcmp(lease->clid, clid, clid_len) != 0)
file_dirty = 1;
lease->aux_changed = file_dirty = 1;
lease->clid_len = clid_len;
memcpy(lease->clid, clid, clid_len);
}
}
void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix, int auth)
@@ -375,7 +418,7 @@ void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix, int
{
if (lease_tmp->auth_name && !auth)
return;
lease_tmp->old = 1; /* call script on change */
lease_tmp->changed = 1; /* call script on change */
new_name = lease_tmp->hostname;
lease_tmp->hostname = NULL;
if (lease_tmp->fqdn)
@@ -408,7 +451,7 @@ void lease_set_hostname(struct dhcp_lease *lease, char *name, char *suffix, int
file_dirty = 1;
dns_dirty = 1;
lease->old = 1; /* run script on change */
lease->changed = 1; /* run script on change */
}
@@ -417,19 +460,63 @@ static pid_t run_script(struct daemon *daemon, char *action, struct dhcp_lease *
{
if (daemon->lease_change_command)
{
char *mac = print_mac(daemon, lease->hwaddr, lease->hwaddr_len);
char *addr = inet_ntoa(lease->addr);
char *com = strrchr(daemon->lease_change_command, '/');
pid_t pid = fork();
char *p;
pid_t pid;
int i;
/* stringify MAC into dhcp_buff */
p = daemon->dhcp_buff;
if (lease->hwaddr_type != ARPHRD_ETHER || lease->hwaddr_len == 0)
p += sprintf(p, "%.2x-", lease->hwaddr_type);
for (i = 0; i < lease->hwaddr_len; i++)
{
p += sprintf(p, "%.2x", lease->hwaddr[i]);
if (i != lease->hwaddr_len - 1)
p += sprintf(p, ":");
}
/* and CLID into namebuff */
p = daemon->namebuff;
if (lease->clid)
for (i = 0; i < lease->clid_len; i++)
{
p += sprintf(p, "%.2x", lease->clid[i]);
if (i != lease->clid_len - 1)
p += sprintf(p, ":");
}
/* and expiry or length into dhcp_buff2 */
#ifdef HAVE_BROKEN_RTC
sprintf(daemon->dhcp_buff2, "%u ", lease->length);
#else
sprintf(daemon->dhcp_buff2, "%lu ", (unsigned long)lease->expires);
#endif
pid = fork();
if (pid == -1)
return 0; /* fork error */
else if (pid != 0)
return pid;
if (lease->clid && lease->clid_len != 0)
setenv("DNSMASQ_CLIENT_ID", daemon->namebuff, 1);
else
unsetenv("DNSMASQ_CLIENT_ID");
#ifdef HAVE_BROKEN_RTC
setenv("DNSMASQ_LEASE_LENGTH", daemon->dhcp_buff2, 1);
unsetenv("DNSMASQ_LEASE_EXPIRES");
#else
setenv("DNSMASQ_LEASE_EXPIRES", daemon->dhcp_buff2, 1);
unsetenv("DNSMASQ_LEASE_LENGTH");
#endif
execl(daemon->lease_change_command,
com ? com+1 : daemon->lease_change_command,
action, mac, addr, lease->hostname, (char*)NULL);
action, daemon->dhcp_buff, addr, lease->hostname, (char*)NULL);
/* log socket should still be open, right? */
syslog(LOG_ERR, _("failed to execute %s: %m"),
@@ -470,7 +557,8 @@ void lease_collect(struct daemon *daemon)
}
for (lease = leases; lease; lease = lease->next)
if (lease->new || lease->old)
if (lease->new || lease->changed ||
(lease->aux_changed && (daemon->options & OPT_LEASE_RO)))
{
if (daemon->script_pid != 0)
return; /* busy */
@@ -479,7 +567,7 @@ void lease_collect(struct daemon *daemon)
daemon->script_pid = run_script(daemon, lease->new ? "add" : "old", lease);
#endif
lease->new = lease->old = 0;
lease->new = lease->changed = lease->aux_changed = 0;
}
}