From: Roy Marples Date: Fri, 13 Nov 2015 11:18:44 +0000 (+0000) Subject: Obey the hostname_short option even for FQDN hostnames passed via config. X-Git-Tag: v6.9.4~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d056e4b5d0eb6876c07f0f3312bbdd26f64bda3c;p=thirdparty%2Fdhcpcd.git Obey the hostname_short option even for FQDN hostnames passed via config. Fixes [c7c4e51c72] based on a patch by sem@altlinux.org. --- diff --git a/common.c b/common.c index f5bae137..f8553da1 100644 --- a/common.c +++ b/common.c @@ -53,29 +53,6 @@ # define _PATH_DEVNULL "/dev/null" #endif -const char * -get_hostname(char *buf, size_t buflen, int short_hostname) -{ - char *p; - - if (gethostname(buf, buflen) != 0) - return NULL; - buf[buflen - 1] = '\0'; - if (strcmp(buf, "(none)") == 0 || - strcmp(buf, "localhost") == 0 || - strncmp(buf, "localhost.", strlen("localhost.")) == 0 || - buf[0] == '.') - return NULL; - - if (short_hostname) { - p = strchr(buf, '.'); - if (p) - *p = '\0'; - } - - return buf; -} - #if USE_LOGFILE void logger_open(struct dhcpcd_ctx *ctx) diff --git a/common.h b/common.h index ebf529d4..bb6f5a7e 100644 --- a/common.h +++ b/common.h @@ -154,7 +154,6 @@ #endif void get_line_free(void); -const char *get_hostname(char *, size_t, int); extern int clock_monotonic; int get_monotonic(struct timespec *); diff --git a/dhcp-common.c b/dhcp-common.c index 285e29c0..870a0da6 100644 --- a/dhcp-common.c +++ b/dhcp-common.c @@ -52,6 +52,37 @@ #define NS_MAXLABEL MAXLABEL #endif +const char * +dhcp_get_hostname(char *buf, size_t buf_len, const struct if_options *ifo) +{ + char *hostname; + + if (ifo->hostname[0] == '\0') { + if (gethostname(buf, buf_len) != 0) + return NULL; + buf[buf_len - 1] = '\0'; + } else + strlcpy(buf, ifo->hostname, sizeof(buf)); + + /* Deny sending of these local hostnames */ + if (strcmp(buf, "(none)") == 0 || + strcmp(buf, "localhost") == 0 || + strncmp(buf, "localhost.", strlen("localhost.")) == 0 || + buf[0] == '.') + return NULL; + + /* Shorten the hostname if required */ + if (ifo->options & DHCPCD_HOSTNAME_SHORT) { + char *hp; + + hp = strchr(buf, '.'); + if (hp != NULL) + *hp = '\0'; + } + + return buf; +} + void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols) { diff --git a/dhcp-common.h b/dhcp-common.h index 37451b06..5f9f9e60 100644 --- a/dhcp-common.h +++ b/dhcp-common.h @@ -88,6 +88,7 @@ struct dhcp_opt { size_t encopts_len; }; +const char *dhcp_get_hostname(char *, size_t, const struct if_options *); struct dhcp_opt *vivso_find(uint32_t, const void *); ssize_t dhcp_vendor(char *, size_t); diff --git a/dhcp.c b/dhcp.c index cdfae5b9..92b39055 100644 --- a/dhcp.c +++ b/dhcp.c @@ -883,11 +883,7 @@ make_message(struct dhcp_message **message, } } - if (ifo->hostname[0] == '\0') - hostname = get_hostname(hbuf, sizeof(hbuf), - ifo->options & DHCPCD_HOSTNAME_SHORT ? 1 : 0); - else - hostname = ifo->hostname; + hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo); /* * RFC4702 3.1 States that if we send the Client FQDN option diff --git a/dhcp6.c b/dhcp6.c index 283563d3..d8d72ee5 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -559,13 +559,9 @@ dhcp6_makemessage(struct interface *ifp) * hostname and FQDN according to RFC4702 */ fqdn = FQDN_BOTH; } - if (fqdn != FQDN_DISABLE) { - if (ifo->hostname[0] == '\0') - hostname = get_hostname(hbuf, sizeof(hbuf), - ifo->options & DHCPCD_HOSTNAME_SHORT ? 1 : 0); - else - hostname = ifo->hostname; - } else + if (fqdn != FQDN_DISABLE) + hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo); + else hostname = NULL; /* appearse gcc */ /* Work out option size first */