Hi Andrew,

Tomas has a point here.
While the part regarding the invalid variables was fixed in 4.1.1-17,
the logic still looks a bit fishy:
  "only set the host name if the old one and the new one are given"
(I am aware that I am the one to blame ;-)

Please find attached a patch that hopefully fixes the issue for good:
Here's the logic it tries to implement:

For the non-udeb case, set the hostname in Linux & kfreebsd only if
1) the new hostname isn't empty
   <=> make sure that we do not reset the hostname to empty

2) the current hostname is empty (or '(none)' or 'localhost') or
   matches the old hostname from DHCP
   <=> make sure that we only set the host name if there is
       no valid one or if the old hostname came from DHCP too

3) the current hostname is empty or
   the new hostname from DHCP differs from the old one from DHCP
   <=> make sure that we set the host name only if it really changed

If you think it is the correct solution, please consider including it in the 
next release of isc-dhcp for Debian

Best regards
Peter

-- 
Peter Marschall
pe...@adpm.de
From 6c9eef8085b833ed181db1505eefadf9d32f8eca Mon Sep 17 00:00:00 2001
From: Peter Marschall <pe...@adpm.de>
Date: Sat, 18 Jun 2011 18:06:17 +0200
Subject: [PATCH] dhclient-script.*: harmonize logic for setting the host name

For the non-udeb case, set the hostname in Linux & kfreebsd only if
1) the new hostname isn't empty
   <=> make sure that we do not reset the hostname to empty

2) the current hostname is empty (or '(none)' or 'localhost') or
   matches the old hostname from DHCP
   <=> make sure that we only set the host name if there is
       no valid one or if the old hostname came from DHCP too

3) the current hostname is empty or
   the new hostname from DHCP differs from the old one from DHCP
   <=> make sure that we set the host name only if it really changed

Signed-off-by: Peter Marschall <pe...@adpm.de>
---
 debian/dhclient-script.kfreebsd      |   27 +++++++++++++++++++++++----
 debian/dhclient-script.kfreebsd.udeb |    1 +
 debian/dhclient-script.linux         |   28 +++++++++++++++++++++++-----
 debian/dhclient-script.linux.udeb    |    1 +
 4 files changed, 48 insertions(+), 9 deletions(-)

diff --git a/debian/dhclient-script.kfreebsd b/debian/dhclient-script.kfreebsd
index f3f93bd..5f2a014 100644
--- a/debian/dhclient-script.kfreebsd
+++ b/debian/dhclient-script.kfreebsd
@@ -38,6 +38,27 @@ make_resolv_conf() {
     fi
 }
 
+# set host name
+set_hostname() {
+    local current_hostname
+
+    if [ -n "$new_host_name" ]; then
+        current_hostname=$(hostname)
+
+        # treat host names '(none)' and 'localhost' the same as the empty host name
+        if [ "$current_hostname" = '(none)' ] || [ "$current_hostname" = 'localhost' ]; then
+            current_hostname=''
+
+        # current host name is empty or the same as the old one from DHCP
+        if [ -z "$current_hostname" ] || [ "$current_hostname" = "$old_host_name" ]; then
+            # current host name is empty or new one from DHCP differs from old one
+            if [ -z "$current_hostname" ] || [ "$new_host_name" != "$old_host_name" ]; then
+                hostname "$new_host_name"
+            fi
+        fi
+    fi
+}
+
 # run given script
 run_hook() {
     local script
@@ -138,10 +159,8 @@ case "$reason" in
         ;;
 
     BOUND|RENEW|REBIND|REBOOT)
-        if [ -n "$old_host_name" ] && [ -n "$new_host_name" ] &&
-           [ "$old_host_name" != "$new_host_name" ]; then
-            hostname "$new_host_name"
-        fi
+        # set changed hostname
+        set_hostname
 
         if [ -n "$old_ip_address" ] && [ -n "$alias_ip_address" ] &&
            [ "$alias_ip_address" != "$old_ip_address" ]; then
diff --git a/debian/dhclient-script.kfreebsd.udeb b/debian/dhclient-script.kfreebsd.udeb
index 335ba28..645ec03 100644
--- a/debian/dhclient-script.kfreebsd.udeb
+++ b/debian/dhclient-script.kfreebsd.udeb
@@ -30,6 +30,7 @@ make_resolv_conf() {
     fi
 }
 
+# set host name
 set_hostname() {
     local current_hostname
     current_hostname=$(hostname)
diff --git a/debian/dhclient-script.linux b/debian/dhclient-script.linux
index 624a17e..c2b05f1 100644
--- a/debian/dhclient-script.linux
+++ b/debian/dhclient-script.linux
@@ -78,6 +78,27 @@ make_resolv_conf() {
     fi
 }
 
+# set host name
+set_hostname() {
+    local current_hostname
+
+    if [ -n "$new_host_name" ]; then
+        current_hostname=$(hostname)
+
+        # treat host names '(none)' and 'localhost' the same as the empty host name
+        if [ "$current_hostname" = '(none)' ] || [ "$current_hostname" = 'localhost' ]; then
+            current_hostname=''
+
+        # current host name is empty or the same as the old one from DHCP
+        if [ -z "$current_hostname" ] || [ "$current_hostname" = "$old_host_name" ]; then
+            # current host name is empty or new one from DHCP differs from old one
+            if [ -z "$current_hostname" ] || [ "$new_host_name" != "$old_host_name" ]; then
+                hostname "$new_host_name"
+            fi
+        fi
+    fi
+}
+
 # run given script
 run_hook() {
     local script
@@ -187,11 +208,8 @@ case "$reason" in
         ;;
 
     BOUND|RENEW|REBIND|REBOOT)
-        if [ -n "$old_host_name" ] && [ -n "$new_host_name" ] &&
-           [ "$old_host_name" != "$new_host_name" ]; then
-            # hostname changed => set it
-            hostname "$new_host_name"
-        fi
+        # set changed hostname
+        set_hostname
 
         if [ -n "$old_ip_address" ] && [ -n "$alias_ip_address" ] &&
            [ "$alias_ip_address" != "$old_ip_address" ]; then
diff --git a/debian/dhclient-script.linux.udeb b/debian/dhclient-script.linux.udeb
index 505b035..cef67fc 100644
--- a/debian/dhclient-script.linux.udeb
+++ b/debian/dhclient-script.linux.udeb
@@ -30,6 +30,7 @@ make_resolv_conf() {
     fi
 }
 
+# set host name
 set_hostname() {
     local current_hostname
     current_hostname=$(cat /proc/sys/kernel/hostname)
-- 
1.7.5.3

Reply via email to