Use the 20-resolv.conf hook from dhcpcd rather than the minimal one.

Extract configured information from resolv.conf and hostname instead
of scraping the last dhcpcd script run.
This will allow the use of DHCPv6 and IPv6RA DNS details in the future.
This commit is contained in:
roy 2014-09-12 20:42:13 +00:00
parent 4fabb2a304
commit ec4a0efb33
5 changed files with 47 additions and 97 deletions

View File

@ -1,22 +0,0 @@
# Minimal version using shell builtins.
make_resolv_conf()
{
if [ -z "${new_domain_name_servers}" -a \
-z "${new_domain_name}" -a \
-z "${new_domain_search}" ]; then
return 0
fi
if [ -n "${new_domain_search}" ]; then
echo "search ${new_domain_search}" >> /etc/resolv.conf
elif [ -n "${new_domain_name}" ]; then
echo "search ${new_domain_name}" >> /etc/resolv.conf
fi
for x in ${new_domain_name_servers}; do
echo "nameserver ${x}" >> /etc/resolv.conf
done
}
case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) make_resolv_conf;;
esac

View File

@ -1,11 +0,0 @@
# $NetBSD: 99-print-sysinst,v 1.1 2009/04/07 11:49:17 joerg Exp $
case "${reason}" in
BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)
cat > /tmp/dhcpcd-lease << EOF
host-name=${new_host_name}
domain-name=${new_domain_name}
EOF
set > /tmp/dhcpcd-lease-all
;;
esac

View File

@ -1,8 +1,9 @@
# $NetBSD: Makefile.dhcpcd,v 1.1 2009/04/07 11:49:17 joerg Exp $
# $NetBSD: Makefile.dhcpcd,v 1.2 2014/09/12 20:42:13 roy Exp $
IMAGEDEPENDS+= \
${DESTDIR}/etc/dhcpcd.conf \
${DESTDIR}/libexec/dhcpcd-run-hooks \
${DESTDIR}/libexec/dhcpcd-hooks/20-resolv.conf \
${DESTDIR}/libexec/dhcpcd-hooks/30-hostname
LISTS+= ${DISTRIBDIR}/common/list.dhcpcd

View File

@ -1,4 +1,4 @@
# $NetBSD: list.dhcpcd,v 1.2 2011/05/31 06:19:11 christos Exp $
# $NetBSD: list.dhcpcd,v 1.3 2014/09/12 20:42:13 roy Exp $
#
# list file (c.f. parselist.awk) for DHCP-enabled install media.
#
@ -8,7 +8,5 @@ SPECIAL dhcpcd srcdir external/bsd/dhcpcd/sbin/dhcpcd
COPY ${DESTDIR}/etc/dhcpcd.conf etc/dhcpcd.conf
COPY ${DESTDIR}/libexec/dhcpcd-run-hooks libexec/dhcpcd-run-hooks 555
COPY ${NETBSDSRCDIR}/distrib/common/10-resolv.conf libexec/dhcpcd-hooks/10-resolv.conf
COPY ${DESTDIR}/libexec/dhcpcd-hooks/20-resolv.conf libexec/dhcpcd-hooks/20-resolv.conf
COPY ${DESTDIR}/libexec/dhcpcd-hooks/30-hostname libexec/dhcpcd-hooks/30-hostname
COPY ${NETBSDSRCDIR}/distrib/common/99-print-sysinst libexec/dhcpcd-hooks/99-print-sysinst

View File

@ -1,4 +1,4 @@
/* $NetBSD: net.c,v 1.5 2014/09/12 20:20:25 roy Exp $ */
/* $NetBSD: net.c,v 1.6 2014/09/12 20:42:13 roy Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -104,7 +104,6 @@ static void write_etc_hosts(FILE *f);
#define DHCPCD "/sbin/dhcpcd"
#include <signal.h>
static int config_dhcp(char *);
static void get_dhcp_value(char *, size_t, const char *);
#ifdef INET6
static int is_v6kernel (void);
@ -533,7 +532,6 @@ config_network(void)
int selected_net;
int i;
char dhcp_host[STRSIZE];
#ifdef INET6
int v6config = 1;
#endif
@ -645,6 +643,8 @@ again:
/* try a dhcp configuration */
dhcp_config = config_dhcp(net_dev);
if (dhcp_config) {
char *nl;
/* Get newly configured data off interface. */
get_ifinterface_info();
get_if6interface_info();
@ -660,41 +660,59 @@ again:
"/sbin/route -n show | "
"while read dest gateway flags;"
" do [ \"$dest\" = default ] && {"
" echo $gateway; break; };"
" echo \"$gateway\"; break; };"
" done" ) > 0)
strlcpy(net_defroute, textbuf,
sizeof net_defroute);
free(textbuf);
if ((nl = strchr(net_namesvr, '\n')))
*nl = '\0';
/* pull nameserver info out of /etc/resolv.conf */
if (collect(T_OUTPUT, &textbuf,
"cat /etc/resolv.conf 2>/dev/null |"
" while read keyword address rest;"
" do [ \"$keyword\" = nameserver "
" -a \"${address#*:}\" = "
"\"${address}\" ] && {"
" echo $address; break; };"
" do [ \"$keyword\" = nameserver ] &&"
" { echo \"$address\"; break; };"
" done" ) > 0)
strlcpy(net_namesvr, textbuf,
sizeof net_namesvr);
free(textbuf);
if ((nl = strchr(net_namesvr, '\n')))
*nl = '\0';
if (net_namesvr[0] != '\0')
net_dhcpconf |= DHCPCONF_NAMESVR;
/* pull domainname out of leases file */
get_dhcp_value(net_domain, sizeof(net_domain),
"domain-name");
/* pull domain info out of /etc/resolv.conf */
if (collect(T_OUTPUT, &textbuf,
"cat /etc/resolv.conf 2>/dev/null |"
" while read keyword domain rest;"
" do [ \"$keyword\" = domain ] &&"
" { echo \"$domain\"; break; };"
" done" ) > 0)
strlcpy(net_domain, textbuf,
sizeof net_domain);
free(textbuf);
if (net_domain[0] == '\0') {
/* pull domain info out of /etc/resolv.conf */
if (collect(T_OUTPUT, &textbuf,
"cat /etc/resolv.conf 2>/dev/null |"
" while read keyword search rest;"
" do [ \"$keyword\" = search ] &&"
" { echo \"$search\"; break; };"
" done" ) > 0)
strlcpy(net_domain, textbuf,
sizeof net_domain);
free(textbuf);
}
if ((nl = strchr(net_domain, '\n')))
*nl = '\0';
if (net_domain[0] != '\0')
net_dhcpconf |= DHCPCONF_DOMAIN;
/* pull hostname out of leases file */
dhcp_host[0] = 0;
get_dhcp_value(dhcp_host, sizeof(dhcp_host),
"host-name");
if (dhcp_host[0] != '\0') {
if (gethostname(net_host, sizeof(net_host)) == 0 &&
net_host[0] != 0)
net_dhcpconf |= DHCPCONF_HOST;
strlcpy(net_host, dhcp_host, sizeof net_host);
}
}
}
@ -1229,37 +1247,3 @@ config_dhcp(char *inter)
}
return 0;
}
static void
get_dhcp_value(char *targ, size_t l, const char *var)
{
static const char *lease_data = "/tmp/dhcpcd-lease";
FILE *fp;
char *line;
size_t len, var_len;
if ((fp = fopen(lease_data, "r")) == NULL) {
warn("Could not open %s", lease_data);
*targ = '\0';
return;
}
var_len = strlen(var);
while ((line = fgetln(fp, &len)) != NULL) {
if (line[len - 1] == '\n')
--len;
if (len <= var_len)
continue;
if (memcmp(line, var, var_len))
continue;
if (line[var_len] != '=')
continue;
line += var_len + 1;
len -= var_len + 1;
strlcpy(targ, line, l > len ? len + 1: l);
break;
}
fclose(fp);
}