network manager dhcp ntp server issue



Hi,

on opensuse 12.2 i found out that NetworkManager is not able to save ntp servers provided by dhcp requests into /var/run/netconfig/NetworkManager.netconfig, which is used by netconfig to setup network configuration (see the following example)

linux-vaba:~ # netconfig update -m ntp-runtime -v
debug: lockfile created (/var/run/netconfig.pid) for PID 27683
debug: lockfile created
debug: Module order: dns-resolver dns-bind dns-dnsmasq nis ntp-runtime
debug: dns-resolver module skipped
debug: dns-bind module skipped
debug: dns-dnsmasq module skipped
debug: nis module skipped
debug: ntp-runtime Module called
debug: Static Fallback
debug: Use NetworkManager policy merged settings
debug: exec get_ntp_settings: /var/run/netconfig/NetworkManager.netconfig
debug:      get_ntp_settings: NTP_SERVER_LIST=''
debug: exit get_ntp_settings: /var/run/netconfig/NetworkManager.netconfig
linux-vaba:~ #

The appended patch (based on master branch) fixes this NetworkManager issue.

linux-vaba:~ # netconfig update -m ntp-runtime -v
debug: lockfile created (/var/run/netconfig.pid) for PID 28013
debug: lockfile created
debug: Module order: dns-resolver dns-bind dns-dnsmasq nis ntp-runtime
debug: dns-resolver module skipped
debug: dns-bind module skipped
debug: dns-dnsmasq module skipped
debug: nis module skipped
debug: ntp-runtime Module called
debug: Static Fallback
debug: Use NetworkManager policy merged settings
debug: exec get_ntp_settings: /var/run/netconfig/NetworkManager.netconfig
debug:      get_ntp_settings: NTP_SERVER_LIST='192.168.1.224'
debug: exit get_ntp_settings: /var/run/netconfig/NetworkManager.netconfig
debug: write_ntp_servers: 192.168.1.224
debug: Installing new /var/run/ntp/servers-netconfig

I tested this patch on opensuse 12.2

Best Regards
Ralf

>From 51e40014a0e59534f3bd9c4b6f893850f373c46d Mon Sep 17 00:00:00 2001
From: Ralf Habacker <ralf habacker freenet de>
Date: Wed, 16 Jan 2013 00:09:49 +0100
Subject: [PATCH] added ntp server to NetworkManager,netconfig file

---
 src/dhcp-manager/nm-dhcp-client.c |   15 +++++++++++
 src/dns-manager/nm-dns-manager.c  |   31 +++++++++++++++++++++-
 src/nm-ip4-config.c               |   51 +++++++++++++++++++++++++++++++++++++
 src/nm-ip4-config.h               |    6 +++++
 4 Dateien geändert, 102 Zeilen hinzugefügt(+), 1 Zeile entfernt(-)

diff --git a/src/dhcp-manager/nm-dhcp-client.c b/src/dhcp-manager/nm-dhcp-client.c
index f721adf..3c3d3ee 100644
--- a/src/dhcp-manager/nm-dhcp-client.c
+++ b/src/dhcp-manager/nm-dhcp-client.c
@@ -1158,6 +1158,21 @@ ip4_options_to_config (NMDHCPClient *self)
 		g_strfreev (searches);
 	}
 
+	str = g_hash_table_lookup (priv->options, "new_ntp_servers");
+	if (str) {
+		char **searches = g_strsplit (str, " ", 0);
+		char **s;
+
+		for (s = searches; *s; s++) {
+			if (inet_pton (AF_INET, *s, &tmp_addr) > 0) {
+				nm_ip4_config_add_ntp_server (ip4_config, tmp_addr.s_addr);
+				nm_log_info (LOGD_DHCP4, "  ntp '%s'", *s);
+			} else
+				nm_log_warn (LOGD_DHCP4, "ignoring invalid NTP server '%s'", *s);
+		}
+		g_strfreev (searches);
+	}
+
 	return ip4_config;
 
 error:
diff --git a/src/dns-manager/nm-dns-manager.c b/src/dns-manager/nm-dns-manager.c
index 38691f3..93ad8f1 100644
--- a/src/dns-manager/nm-dns-manager.c
+++ b/src/dns-manager/nm-dns-manager.c
@@ -88,6 +88,7 @@ typedef struct {
 	GPtrArray *searches;
 	const char *nis_domain;
 	GPtrArray *nis_servers;
+	GPtrArray *ntp_servers;
 } NMResolvConfData;
 
 static void
@@ -155,6 +156,17 @@ merge_one_ip4_config (NMResolvConfData *rc, NMIP4Config *src)
 		if (!rc->nis_domain)
 			rc->nis_domain = nm_ip4_config_get_nis_domain (src);
 	}
+
+	/* NTP stuff */
+	num = nm_ip4_config_get_num_ntp_servers (src);
+	for (i = 0; i < num; i++) {
+		struct in_addr addr;
+		char buf[INET_ADDRSTRLEN];
+
+		addr.s_addr = nm_ip4_config_get_ntp_server (src, i);
+		if (inet_ntop (AF_INET, &addr, buf, INET_ADDRSTRLEN) > 0)
+			add_string_item (rc->ntp_servers, buf);
+	}
 }
 
 static void
@@ -261,6 +273,7 @@ dispatch_netconfig (const char *domain,
                     char **nameservers,
                     const char *nis_domain,
                     char **nis_servers,
+                    char **ntp_servers,
                     const char *iface,
                     GError **error)
 {
@@ -312,6 +325,12 @@ dispatch_netconfig (const char *domain,
 		g_free (str);
 	}
 
+	if (ntp_servers) {
+		str = g_strjoinv (" ", ntp_servers);
+		write_to_netconfig (fd, "NTPSERVERS", str);
+		g_free (str);
+	}
+
 	close (fd);
 
 	/* Wait until the process exits */
@@ -581,6 +600,7 @@ update_dns (NMDnsManager *self,
 	char **searches = NULL;
 	char **nameservers = NULL;
 	char **nis_servers = NULL;
+	char **ntp_servers = NULL;
 	int num, i, len;
 	gboolean success = FALSE, caching = FALSE;
 
@@ -604,6 +624,7 @@ update_dns (NMDnsManager *self,
 	rc.searches = g_ptr_array_new ();
 	rc.nis_domain = NULL;
 	rc.nis_servers = g_ptr_array_new ();
+	rc.ntp_servers = g_ptr_array_new ();
 
 	if (priv->ip4_vpn_config)
 		merge_one_ip4_config (&rc, priv->ip4_vpn_config);
@@ -680,6 +701,12 @@ update_dns (NMDnsManager *self,
 
 	nis_domain = rc.nis_domain;
 
+	if (rc.ntp_servers->len) {
+		g_ptr_array_add (rc.ntp_servers, NULL);
+		ntp_servers = (char **) g_ptr_array_free (rc.ntp_servers, FALSE);
+	} else
+		g_ptr_array_free (rc.ntp_servers, TRUE);
+
 	/* Build up config lists for plugins; we use the raw configs here, not the
 	 * merged information that we write to resolv.conf so that the plugins can
 	 * still use the domain information in each config to provide split DNS if
@@ -753,7 +780,7 @@ update_dns (NMDnsManager *self,
 #ifdef NETCONFIG_PATH
 	if (success == FALSE) {
 		success = dispatch_netconfig (domain, searches, nameservers,
-		                              nis_domain, nis_servers,
+		                              nis_domain, nis_servers, ntp_servers,
 		                              iface, error);
 	}
 #endif
@@ -767,6 +794,8 @@ update_dns (NMDnsManager *self,
 		g_strfreev (nameservers);
 	if (nis_servers)
 		g_strfreev (nis_servers);
+	if (ntp_servers)
+		g_strfreev (ntp_servers);
 
 	return success;
 }
diff --git a/src/nm-ip4-config.c b/src/nm-ip4-config.c
index 0722981..79f61a0 100644
--- a/src/nm-ip4-config.c
+++ b/src/nm-ip4-config.c
@@ -59,6 +59,8 @@ typedef struct {
 	GArray *nis;
 	char * nis_domain;
 
+	GArray *ntp;
+
 	GSList *routes;
 
 	gboolean never_default;
@@ -569,6 +571,50 @@ nm_ip4_config_get_nis_domain (NMIP4Config *config)
 	return NM_IP4_CONFIG_GET_PRIVATE (config)->nis_domain;
 }
 
+void nm_ip4_config_add_ntp_server (NMIP4Config *config, guint32 ntp)
+{
+	NMIP4ConfigPrivate *priv;
+	int i;
+
+	g_return_if_fail (NM_IS_IP4_CONFIG (config));
+	g_return_if_fail (ntp > 0);
+
+	priv = NM_IP4_CONFIG_GET_PRIVATE (config);
+	for (i = 0; i < priv->ntp->len; i++) {
+		guint32 s = g_array_index (priv->ntp, guint32, i);
+
+		/* No dupes */
+		g_return_if_fail (ntp != s);
+	}
+
+	g_array_append_val (priv->ntp, ntp);
+}
+
+guint32 nm_ip4_config_get_ntp_server (NMIP4Config *config, guint i)
+{
+	g_return_val_if_fail (NM_IS_IP4_CONFIG (config), 0);
+
+	return g_array_index (NM_IP4_CONFIG_GET_PRIVATE (config)->ntp, guint32, i);
+}
+
+guint32 nm_ip4_config_get_num_ntp_servers (NMIP4Config *config)
+{
+	g_return_val_if_fail (NM_IS_IP4_CONFIG (config), 0);
+
+	return NM_IP4_CONFIG_GET_PRIVATE (config)->ntp->len;
+}
+
+void nm_ip4_config_reset_ntp_servers (NMIP4Config *config)
+{
+	NMIP4ConfigPrivate *priv;
+
+	g_return_if_fail (NM_IS_IP4_CONFIG (config));
+
+	priv = NM_IP4_CONFIG_GET_PRIVATE (config);
+	if (priv->ntp->len)
+		g_array_remove_range (priv->ntp, 0, priv->ntp->len);
+}
+
 /* libnl convenience/conversion functions */
 
 static int ip4_addr_to_rtnl_local (guint32 ip4_address, struct rtnl_addr *addr)
@@ -856,6 +902,9 @@ nm_ip4_config_hash (NMIP4Config *config, GChecksum *sum, gboolean dns_only)
 		s = nm_ip4_config_get_nis_domain (config);
 		if (s)
 			g_checksum_update (sum, (const guint8 *) s, strlen (s));
+
+		for (i = 0; i < nm_ip4_config_get_num_ntp_servers (config); i++)
+			hash_u32 (sum, nm_ip4_config_get_ntp_server (config, i));
 	}
 
 	for (i = 0; i < nm_ip4_config_get_num_nameservers (config); i++)
@@ -885,6 +934,7 @@ nm_ip4_config_init (NMIP4Config *config)
 	priv->domains = g_ptr_array_sized_new (3);
 	priv->searches = g_ptr_array_sized_new (3);
 	priv->nis = g_array_new (FALSE, TRUE, sizeof (guint32));
+	priv->ntp = g_array_new (FALSE, TRUE, sizeof (guint32));
 }
 
 static void
@@ -900,6 +950,7 @@ finalize (GObject *object)
 	g_ptr_array_free (priv->searches, TRUE);
 	g_array_free (priv->nis, TRUE);
 	g_free (priv->nis_domain);
+	g_array_free (priv->ntp, TRUE);
 
 	G_OBJECT_CLASS (nm_ip4_config_parent_class)->finalize (object);
 }
diff --git a/src/nm-ip4-config.h b/src/nm-ip4-config.h
index 5433768..4534b8a 100644
--- a/src/nm-ip4-config.h
+++ b/src/nm-ip4-config.h
@@ -107,6 +107,11 @@ void          nm_ip4_config_reset_nis_servers   (NMIP4Config *config);
 void          nm_ip4_config_set_nis_domain      (NMIP4Config *config, const char *domain);
 const char *  nm_ip4_config_get_nis_domain      (NMIP4Config *config);
 
+void          nm_ip4_config_add_ntp_server      (NMIP4Config *config, guint32 ntp);
+guint32       nm_ip4_config_get_ntp_server      (NMIP4Config *config, guint i);
+guint32       nm_ip4_config_get_num_ntp_servers (NMIP4Config *config);
+void          nm_ip4_config_reset_ntp_servers   (NMIP4Config *config);
+
 
 /* Flags for nm_ip4_config_to_rtnl_addr() */
 #define NM_RTNL_ADDR_NONE		0x0000
@@ -133,6 +138,7 @@ typedef enum {
 	NM_IP4_COMPARE_FLAG_WINS_SERVERS= 0x00000100,
 	NM_IP4_COMPARE_FLAG_NIS_SERVERS = 0x00000200,
 	NM_IP4_COMPARE_FLAG_NIS_DOMAIN  = 0x00000400,
+	NM_IP4_COMPARE_FLAG_NTP_SERVERS = 0x00000800,
 	NM_IP4_COMPARE_FLAG_ALL         = 0xFFFFFFFF   /* match everything */
 } NMIP4ConfigCompareFlags;
 
-- 
1.7.10.4




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]