From cf5fab8f55b1f54e28da8b78bc02c65d29134787 Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Fri, 28 Apr 2017 11:40:51 +0200 Subject: [PATCH] dhcp: allow FQDNs in ipv4.dhcp-hostname If users wrote a FQDN in ipv4.dhcp-hostname presumably it's because they really want to send the full value, not only the host part, so let's send it as-is. This obviously is a change in behavior, but only for users that have a FQDN in ipv4.dhcp-hostname, where it's not clear if they really want the domain to be stripped. When the property is unset, we keep sending only the host part of the system hostname to maintain backwards compatibility. This commit aligns NM behavior to initscripts. --- src/dhcp/nm-dhcp-dhclient-utils.c | 14 ++--------- src/dhcp/nm-dhcp-dhcpcd.c | 13 +++------- src/dhcp/nm-dhcp-manager.c | 40 +++++++++++++++++++++---------- src/dhcp/nm-dhcp-systemd.c | 30 +++++++---------------- 4 files changed, 40 insertions(+), 57 deletions(-) diff --git a/src/dhcp/nm-dhcp-dhclient-utils.c b/src/dhcp/nm-dhcp-dhclient-utils.c index d26726798..11f868e26 100644 --- a/src/dhcp/nm-dhcp-dhclient-utils.c +++ b/src/dhcp/nm-dhcp-dhclient-utils.c @@ -95,24 +95,14 @@ grab_request_options (GPtrArray *store, const char* line) static void add_hostname4 (GString *str, const char *hostname, gboolean use_fqdn) { - char *plain_hostname, *dot; - if (hostname) { if (use_fqdn) { g_string_append_printf (str, FQDN_FORMAT "\n", hostname); g_string_append (str, "send fqdn.encoded on;\n" "send fqdn.server-update on;\n"); - } else { - plain_hostname = g_strdup (hostname); - dot = strchr (plain_hostname, '.'); - /* get rid of the domain */ - if (dot) - *dot = '\0'; - - g_string_append_printf (str, HOSTNAME4_FORMAT "\n", plain_hostname); - g_free (plain_hostname); - } + } else + g_string_append_printf (str, HOSTNAME4_FORMAT "\n", hostname); } } diff --git a/src/dhcp/nm-dhcp-dhcpcd.c b/src/dhcp/nm-dhcp-dhcpcd.c index 54af2d430..66a31acf1 100644 --- a/src/dhcp/nm-dhcp-dhcpcd.c +++ b/src/dhcp/nm-dhcp-dhcpcd.c @@ -88,9 +88,8 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last GPtrArray *argv = NULL; pid_t pid = -1; GError *error = NULL; - char *pid_contents = NULL, *binary_name, *cmd_str, *dot; + char *pid_contents = NULL, *binary_name, *cmd_str; const char *iface, *dhcpcd_path, *hostname; - gs_free char *prefix = NULL; g_return_val_if_fail (priv->pid_file == NULL, FALSE); @@ -146,14 +145,8 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last g_ptr_array_add (argv, (gpointer) "-F"); g_ptr_array_add (argv, (gpointer) "both"); } else { - prefix = strdup (hostname); - dot = strchr (prefix, '.'); - /* get rid of the domain */ - if (dot) - *dot = '\0'; - - g_ptr_array_add (argv, (gpointer) "-h"); /* Send hostname to DHCP server */ - g_ptr_array_add (argv, (gpointer) prefix); + g_ptr_array_add (argv, (gpointer) "-h"); + g_ptr_array_add (argv, (gpointer) hostname); } } diff --git a/src/dhcp/nm-dhcp-manager.c b/src/dhcp/nm-dhcp-manager.c index c3018ef5c..fff9f9ec3 100644 --- a/src/dhcp/nm-dhcp-manager.c +++ b/src/dhcp/nm-dhcp-manager.c @@ -219,15 +219,6 @@ client_start (NMDhcpManager *self, return client; } -static const char * -get_send_hostname (NMDhcpManager *self, const char *setting_hostname) -{ - NMDhcpManagerPrivate *priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - - /* Always prefer the explicit dhcp-send-hostname if given */ - return setting_hostname ? setting_hostname : priv->default_hostname; -} - /* Caller owns a reference to the NMDhcpClient on return */ NMDhcpClient * nm_dhcp_manager_start_ip4 (NMDhcpManager *self, @@ -244,17 +235,36 @@ nm_dhcp_manager_start_ip4 (NMDhcpManager *self, const char *dhcp_anycast_addr, const char *last_ip_address) { + NMDhcpManagerPrivate *priv; const char *hostname = NULL; + gs_free char *hostname_tmp = NULL; gboolean use_fqdn = FALSE; + char *dot; g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); + priv = NM_DHCP_MANAGER_GET_PRIVATE (self); if (send_hostname) { + /* Use, in order of preference: + * 1. FQDN from configuration + * 2. hostname from configuration + * 3. system hostname (only host part) + */ if (dhcp_fqdn) { hostname = dhcp_fqdn; use_fqdn = TRUE; - } else - hostname = get_send_hostname (self, dhcp_hostname); + } else if (dhcp_hostname) + hostname = dhcp_hostname; + else { + hostname = priv->default_hostname; + if (hostname) { + hostname_tmp = g_strdup (hostname); + dot = strchr (hostname_tmp, '.'); + if (dot) + *dot = '\0'; + hostname = hostname_tmp; + } + } } return client_start (self, iface, ifindex, hwaddr, uuid, priority, FALSE, NULL, @@ -279,12 +289,16 @@ nm_dhcp_manager_start_ip6 (NMDhcpManager *self, NMSettingIP6ConfigPrivacy privacy, guint needed_prefixes) { + NMDhcpManagerPrivate *priv; const char *hostname = NULL; g_return_val_if_fail (NM_IS_DHCP_MANAGER (self), NULL); + priv = NM_DHCP_MANAGER_GET_PRIVATE (self); - if (send_hostname) - hostname = get_send_hostname (self, dhcp_hostname); + if (send_hostname) { + /* Always prefer the explicit dhcp-hostname if given */ + hostname = dhcp_hostname ? dhcp_hostname : priv->default_hostname; + } return client_start (self, iface, ifindex, hwaddr, uuid, priority, TRUE, ll_addr, NULL, timeout, dhcp_anycast_addr, hostname, TRUE, info_only, privacy, NULL, needed_prefixes); diff --git a/src/dhcp/nm-dhcp-systemd.c b/src/dhcp/nm-dhcp-systemd.c index 96be65ae1..aa9027010 100644 --- a/src/dhcp/nm-dhcp-systemd.c +++ b/src/dhcp/nm-dhcp-systemd.c @@ -687,28 +687,14 @@ ip4_start (NMDhcpClient *client, const char *dhcp_anycast_addr, const char *last hostname = nm_dhcp_client_get_hostname (client); if (hostname) { - if (nm_dhcp_client_get_use_fqdn (client)) { - r = sd_dhcp_client_set_hostname (priv->client4, hostname); - if (r < 0) { - _LOGW ("failed to set DHCP FQDN (%d)", r); - goto error; - } - } else { - char *prefix, *dot; - - prefix = strdup (hostname); - dot = strchr (prefix, '.'); - /* get rid of the domain */ - if (dot) - *dot = '\0'; - - r = sd_dhcp_client_set_hostname (priv->client4, prefix); - free (prefix); - - if (r < 0) { - _LOGW ("failed to set DHCP hostname (%d)", r); - goto error; - } + /* FIXME: sd-dhcp decides which hostname/FQDN option to send (12 or 81) + * only based on whether the hostname has a domain part or not. At the + * moment there is no way to force one or another. + */ + r = sd_dhcp_client_set_hostname (priv->client4, hostname); + if (r < 0) { + _LOGW ("failed to set DHCP hostname to '%s' (%d)", hostname, r); + goto error; } }