From: Roy Marples Date: Tue, 22 Apr 2008 10:41:57 +0000 (+0000) Subject: Move looking up the hostname in DNS to a sample hook file as it's not something we... X-Git-Tag: v4.0.2~459 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=396a9a22e5237deb721316ebe5e39f45bff4e605;p=thirdparty%2Fdhcpcd.git Move looking up the hostname in DNS to a sample hook file as it's not something we should really do by default. Rename our sample hooks to end with .sh so they get nice script highlights. --- diff --git a/README b/README index 77890a82..5c53ea0c 100644 --- a/README +++ b/README @@ -42,7 +42,8 @@ commandline compatible with prior versions. Hooks ----- -We provide example exit hooks for NTP (www.ntp.org) and ypbind (Linux). +We provide example exit hooks for looking up the hostname in DNS, +NTP (www.ntp.org) and ypbind (Linux). There are not installed by default as they are platform dependant. diff --git a/dhcpcd.8.in b/dhcpcd.8.in index 8ff4f605..fa4f00de 100644 --- a/dhcpcd.8.in +++ b/dhcpcd.8.in @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd Apr 13, 2008 +.Dd Apr 22, 2008 .Dt DHCPCD 8 SMM .Sh NAME .Nm dhcpcd @@ -71,8 +71,7 @@ if available, otherwise directly to .Pa /etc/resolv.conf . If the hostname is currenly blank, (null) or localhost then .Nm -will set the hostname to the one supplied by the DHCP server, or look it up -in DNS if none supplied. +will set the hostname to the one supplied by the DHCP server. .Nm then daemonises and waits for the lease renewal time to lapse. Then it attempts to renew its lease and reconfigure if the new lease changes. diff --git a/dhcpcd.sh.in b/dhcpcd.sh.in index 5915d00c..f7fb01bd 100644 --- a/dhcpcd.sh.in +++ b/dhcpcd.sh.in @@ -87,38 +87,15 @@ restore_resolv_conf() need_hostname() { case "$(hostname)" in - ""|"(none)"|localhost) return 0;; + ""|"(none)"|localhost|"${old_host_name}") return 0;; esac return 1 } -lookup_hostname() -{ - local h= - # Silly ISC programs love to send error text to stdout - if type host >/dev/null 2>&1; then - h=`host ${new_ip_address}` - if [ $? = 0 ]; then - echo "${h}" \ - | sed 's/.* domain name pointer \(.*\)./\1/' - return 0 - fi - elif type dig >/dev/null 2>&1; then - h=`dig +short -x ${new_ip_address}` - if [ $? = 0 ]; then - echo "${h}" | sed 's/\.$//' - return 0 - fi - fi - return 1 -} - set_hostname() { - if [ -n "${old_host_name}" ] || need_hostname; then - local name="${new_host_name}" - [ -z "${name}" ] && name="$(lookup_hostname)" - [ -n "${name}" ] && hostname "${name}" + if need_hostname; then + hostname "${new_host_name}" fi } diff --git a/hook-samples/dhcpcd-compat b/hook-samples/dhcpcd-compat.sh similarity index 100% rename from hook-samples/dhcpcd-compat rename to hook-samples/dhcpcd-compat.sh diff --git a/hook-samples/lookup_hostname.sh b/hook-samples/lookup_hostname.sh new file mode 100644 index 00000000..c829b2d6 --- /dev/null +++ b/hook-samples/lookup_hostname.sh @@ -0,0 +1,46 @@ +# Sample exit hook to lookup the hostname in DNS if not set + +lookup_hostname() +{ + local h= + # Silly ISC programs love to send error text to stdout + if type dig >/dev/null 2>&1; then + h=`dig +short -x ${new_ip_address}` + if [ $? = 0 ]; then + echo "${h}" | sed 's/\.$//' + return 0 + fi + elif type host >/dev/null 2>&1; then + h=`host ${new_ip_address}` + if [ $? = 0 ]; then + echo "${h}" \ + | sed 's/.* domain name pointer \(.*\)./\1/' + return 0 + fi + fi + return 1 +} + +do_hostname() +{ + if [ -z "${new_host_name}" ] && need_hostname; then + local hname="$(lookup_hostname)" + if [ -n "${hname}" ]; then + hostname "${hname}" + fi + fi +} + +case "${reason}" in + TEST) + ;; + BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT) + do_hostname + ;; + EXPIRE|FAIL|IPV4LL|RELEASE|STOP) + ;; + *) + echo "lookup_hostname: unsupported reason ${reason}" >&2 + false + ;; +esac diff --git a/hook-samples/ntp b/hook-samples/ntp.sh similarity index 99% rename from hook-samples/ntp rename to hook-samples/ntp.sh index c0a6a7b0..ff8e8fbb 100644 --- a/hook-samples/ntp +++ b/hook-samples/ntp.sh @@ -1,4 +1,3 @@ -#!/bin/sh # Sample exit hook script for ntp # Detect OpenRC or BSD rc diff --git a/hook-samples/ypbind b/hook-samples/ypbind.sh similarity index 99% rename from hook-samples/ypbind rename to hook-samples/ypbind.sh index 7637720b..64d96fef 100644 --- a/hook-samples/ypbind +++ b/hook-samples/ypbind.sh @@ -1,4 +1,3 @@ -#!/bin/sh # Sample exit hook for ypbind # This script is only suitable for the Linux version.