]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Add the option hostname_short, which will force the sending of the
authorRoy Marples <roy@marples.name>
Thu, 25 Jul 2013 15:10:16 +0000 (15:10 +0000)
committerRoy Marples <roy@marples.name>
Thu, 25 Jul 2013 15:10:16 +0000 (15:10 +0000)
short hostname so DDNS will always work, even if the hostname domain
is different from the DHCP servers.

Change hostname_fqdn to true/false/unset value.
unset means use what the DHCP server supplies
 (DHCPv4/DHCPv6 maybe inconsistent depending on server setup)
true/false force the hostname to be either a FQDN or a short name.

common.c
common.h
dhcp.c
dhcp6.c
dhcpcd-hooks/30-hostname
dhcpcd.conf.5.in
if-options.c
if-options.h

index 1740e29e9022a4d1b1c5ec8168a0b210abdd11bb..4bf52bad72ccf22ca654af9c25ab77d273d88aec 100644 (file)
--- a/common.c
+++ b/common.c
@@ -136,8 +136,9 @@ set_nonblock(int fd)
 }
 
 const char *
-get_hostname(void)
+get_hostname(int short_hostname)
 {
+       char *p;
 
        gethostname(hostname_buffer, sizeof(hostname_buffer));
        hostname_buffer[sizeof(hostname_buffer) - 1] = '\0';
@@ -146,6 +147,13 @@ get_hostname(void)
            strncmp(hostname_buffer, "localhost.", strlen("localhost.")) == 0 ||
            hostname_buffer[0] == '.')
                return NULL;
+
+       if (short_hostname) {
+               p = strchr(hostname_buffer, '.');
+               if (p)
+                       *p = '\0';
+       }
+
        return hostname_buffer;
 }
 
index b7851cb456bd242aa2a22a7414e1658b922f1e3b..392125a2f532b15ef717687de2f6fc2444cc57ed 100644 (file)
--- a/common.h
+++ b/common.h
 int set_cloexec(int);
 int set_nonblock(int);
 char *get_line(FILE * __restrict);
-const char *get_hostname(void);
+const char *get_hostname(int);
 extern int clock_monotonic;
 int get_monotonic(struct timeval *);
 ssize_t setvar(char ***, const char *, const char *, const char *);
diff --git a/dhcp.c b/dhcp.c
index 06ed6111d66435054b5949615e6bb1410aa79b2a..30c1ecd638bb72379c3776a3f41c36b03b57d31b 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -845,7 +845,6 @@ make_message(struct dhcp_message **message,
        uint32_t ul;
        uint16_t sz;
        size_t len;
-       const char *hp;
        const struct dhcp_opt *opt;
        const struct if_options *ifo = iface->options;
        const struct dhcp_state *state = D_CSTATE(iface);
@@ -987,7 +986,8 @@ make_message(struct dhcp_message **message,
                }
 
                if (ifo->hostname[0] == '\0')
-                       hostname = get_hostname();
+                       hostname = get_hostname(ifo->options &
+                           DHCPCD_HOSTNAME_SHORT ? 1 : 0);
                else
                        hostname = ifo->hostname;
                if (ifo->fqdn != FQDN_DISABLE) {
@@ -1018,17 +1018,7 @@ make_message(struct dhcp_message **message,
                        }
                } else if (ifo->options & DHCPCD_HOSTNAME && hostname) {
                        *p++ = DHO_HOSTNAME;
-                       /*
-                        * Regardless of RFC2132, we should always send a
-                        * hostname upto the first dot (the short hostname) as
-                        * a FQDN confuses some DHCP servers when updating DNS.
-                        * The FQDN option should be used if a FQDN is wanted.
-                        */
-                       hp = strchr(hostname, '.');
-                       if (hp)
-                               len = hp - hostname;
-                       else
-                               len = strlen(hostname);
+                       len = strlen(hostname);
                        *p++ = len;
                        memcpy(p, hostname, len);
                        p += len;
diff --git a/dhcp6.c b/dhcp6.c
index 9e95b2793e9447f04fff39b066137db7f218a597..3cf69f3909a75519fdb54ef533f77849e6c5459e 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -398,7 +398,8 @@ dhcp6_makemessage(struct interface *ifp)
                if (fqdn == FQDN_DISABLE)
                        fqdn = FQDN_BOTH;
                if (ifo->hostname[0] == '\0')
-                       hostname = get_hostname();
+                       hostname = get_hostname(ifo->options &
+                           DHCPCD_HOSTNAME_SHORT ? 1 : 0);
                else
                        hostname = ifo->hostname;
        }
index 42120ceb218612154573d077ca049c645d9a742a..6098f6205843c52987a57f753619af0ed2c9b49b 100644 (file)
@@ -1,7 +1,9 @@
 # Set the hostname from DHCP data if required
 
-# Generally we should not set the system hostname to be fully qualified
-: ${hostname_fqdn:=false}
+# Set one of the below to override the DHCP supplied hostname information
+# to be either a short hostname or a FQDN.
+#hostname_fqdn=false
+#hostname_fqdn=true
 
 # Some systems don't have hostname(1)
 _hostname()
@@ -41,7 +43,7 @@ _hostname()
 
 need_hostname()
 {
-       local hostname hfqdn
+       local hostname hfqdn=false hshort=false
 
        case "$force_hostname" in
        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1) return 0;;
@@ -54,13 +56,14 @@ need_hostname()
        
        case "$hostname_fqdn" in
        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|1)        hfqdn=true;;
-       *)                                      hfqdn=false;;
+       "")                                     ;;
+       *)                                      hshort=true;;
        esac
 
        if [ -n "$old_fqdn" ]; then
-               if ${hfqdn}; then
+               if ${hfqdn} || ! ${hsort}; then
                        [ "$hostname" = "$old_fqdn" ]
-               else    
+               else
                        [ "$hostname" = "${old_fqdn%%.*}" ]
                fi
        elif [ -n "$old_host_name" ]; then
@@ -73,8 +76,10 @@ need_hostname()
                        else
                                [ "$hostname" = "$old_host_name" ]
                        fi
-               else
+               elif ${hshort}; then
                        [ "$hostname" = "${old_host_name%%.*}" ]
+               else
+                       [ "$hostname" = "$old_host_name" ]
                fi
        fi
 }
@@ -101,7 +106,7 @@ set_hostname()
        esac
 
        if [ -n "$new_fqdn" ]; then
-               if ${hfqdn}; then
+               if ${hfqdn} || ! ${hsort}; then
                        try_hostname "$new_fqdn"
                else
                        try_hostname "${new_fqdn%%.*}"
@@ -115,8 +120,10 @@ set_hostname()
                        else
                                try_hostname "$new_host_name"
                        fi
-               else
+               elif ${hshort}; then
                        try_hostname "${new_host_name%%.*}"
+               else
+                       try_hostname "$new_host_name"
                fi
        fi
 }
index f2f7fe581e54e1f73f8d3405314d54bcfc030a03..cd14d3d94e85936c5abf39d04e4faa0022e53241 100644 (file)
@@ -22,7 +22,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd June 19, 2013
+.Dd July 25, 2013
 .Dt DHCPCD.CONF 5 SMM
 .Os
 .Sh NAME
@@ -133,6 +133,10 @@ is an empty string then the current system hostname is sent.
 If
 .Ar hostname
 is a FQDN (ie, contains a .) then it will be encoded as such.
+.It Ic hostname_short
+Sends the short hostname to the DHCP server instead of the FQDN.
+This is useful because DHCP servers will not register the FQDN in their
+DNS if the domain part does not match theirs.
 .It Ic ia_na Op Ar iaid
 Request a DHCPv6 Normal Address for
 .Ar iaid .
index d22af1be871fb5e21232a7303c2dc98f38d50539..9f359954110a208613b310a8818d20a49203d2eb 100644 (file)
@@ -67,6 +67,7 @@ unsigned long long options = 0;
 #define O_IA_NA                        O_BASE + 10
 #define O_IA_TA                        O_BASE + 11
 #define O_IA_PD                        O_BASE + 12
+#define O_HOSTNAME_SHORT       O_BASE + 13
 
 const struct option cf_options[] = {
        {"background",      no_argument,       NULL, 'b'},
@@ -128,6 +129,7 @@ const struct option cf_options[] = {
        {"ia_na",           no_argument,       NULL, O_IA_NA},
        {"ia_ta",           no_argument,       NULL, O_IA_TA},
        {"ia_pd",           no_argument,       NULL, O_IA_PD},
+       {"hostname_short",  no_argument,       NULL, O_HOSTNAME_SHORT},
        {NULL,              0,                 NULL, '\0'}
 };
 
@@ -1079,6 +1081,9 @@ got_iaid:
                        }
                }
                break;
+       case O_HOSTNAME_SHORT:
+               ifo->options |= DHCPCD_HOSTNAME_SHORT;
+               break;
 #endif
        default:
                return 0;
index 06cccdf0c16828b0279972a64bcad5c3e2f91212..a00dd769a3964def6b3fbe92081e033065c96fb5 100644 (file)
@@ -94,6 +94,7 @@
 #define DHCPCD_IA_FORCED               (1ULL << 40)
 #define DHCPCD_STOPPING                        (1ULL << 41)
 #define DHCPCD_DEPARTED                        (1ULL << 42)
+#define DHCPCD_HOSTNAME_SHORT          (1ULL << 43)
 
 extern const struct option cf_options[];