Blob Blame History Raw
From e554ef606f0dfd5ab07057824bb3d3dd52170228 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes@redhat.com>
Date: Thu, 19 Jun 2014 15:43:51 +0200
Subject: [PATCH] ifcfg-rh: write GATEWAY instead of GATEWAY0 to be
 ifup-compatible (rh #771673)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

https://bugzilla.redhat.com/show_bug.cgi?id=771673
https://bugzilla.redhat.com/show_bug.cgi?id=1105770

Signed-off-by: Jiří Klimeš <jklimes@redhat.com>
---
 .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c         | 147 +++++++++++++++++++++
 src/settings/plugins/ifcfg-rh/writer.c             |  25 +++-
 2 files changed, 168 insertions(+), 4 deletions(-)

diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c
index 0e5be5d..ff45550 100644
--- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c
+++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c
@@ -8284,6 +8284,151 @@ test_write_wired_aliases (void)
 }
 
 static void
+test_write_gateway (void)
+{
+	NMConnection *connection, *reread;
+	NMSettingConnection *s_con;
+	NMSettingWired *s_wired;
+	NMSettingIP4Config *s_ip4;
+	char *uuid, *testfile = NULL, *val;
+	gboolean success;
+	GError *error = NULL;
+	shvarFile *f;
+	NMIP4Address *addr;
+	const char *ip1_str = "1.1.1.3";
+	const char *ip2_str = "2.2.2.5";
+	const char *gw1_str = "1.1.1.254";
+	const char *gw2_str = "2.2.2.254";
+	struct in_addr ip1, ip2, gw1, gw2;
+	const guint32 prefix = 24;
+
+	connection = nm_connection_new ();
+
+	/* Connection setting */
+	s_con = (NMSettingConnection *) nm_setting_connection_new ();
+	nm_connection_add_setting (connection, NM_SETTING (s_con));
+
+	uuid = nm_utils_uuid_generate ();
+	g_object_set (s_con,
+	              NM_SETTING_CONNECTION_ID, "Test Write Static Addresses Gateway",
+	              NM_SETTING_CONNECTION_UUID, uuid,
+	              NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME,
+	              NULL);
+	g_free (uuid);
+
+	/* Wired setting */
+	s_wired = (NMSettingWired *) nm_setting_wired_new ();
+	nm_connection_add_setting (connection, NM_SETTING (s_wired));
+
+	/* IP4 setting */
+	s_ip4 = (NMSettingIP4Config *) nm_setting_ip4_config_new ();
+	nm_connection_add_setting (connection, NM_SETTING (s_ip4));
+
+	g_object_set (s_ip4,
+	              NM_SETTING_IP4_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_MANUAL,
+	              NM_SETTING_IP4_CONFIG_MAY_FAIL, TRUE,
+	              NULL);
+
+	inet_pton (AF_INET, ip1_str, &ip1);
+	inet_pton (AF_INET, ip2_str, &ip2);
+	inet_pton (AF_INET, gw1_str, &gw1);
+	inet_pton (AF_INET, gw2_str, &gw2);
+
+	addr = nm_ip4_address_new ();
+	nm_ip4_address_set_address (addr, ip1.s_addr);
+	nm_ip4_address_set_prefix (addr, prefix);
+	nm_ip4_address_set_gateway (addr, gw1.s_addr);
+	nm_setting_ip4_config_add_address (s_ip4, addr);
+	nm_ip4_address_unref (addr);
+
+	addr = nm_ip4_address_new ();
+	nm_ip4_address_set_address (addr, ip2.s_addr);
+	nm_ip4_address_set_prefix (addr, prefix);
+	nm_ip4_address_set_gateway (addr, gw2.s_addr);
+	nm_setting_ip4_config_add_address (s_ip4, addr);
+	nm_ip4_address_unref (addr);
+
+	success = nm_connection_verify (connection, &error);
+	g_assert_no_error (error);
+	g_assert (success);
+
+	/* Save the ifcfg */
+	success = writer_new_connection (connection,
+	                                 TEST_SCRATCH_DIR "/network-scripts/",
+	                                 &testfile,
+	                                 &error);
+	g_assert_no_error (error);
+	g_assert (success);
+
+	f = svNewFile (testfile);
+	g_assert (f);
+
+	/* re-read the file to check that the keys was written as IPADDR, GATEWAY and IPADDR1, GATEWAY1 */
+	val = svGetValue (f, "IPADDR", FALSE);
+	g_assert (val);
+	g_assert_cmpstr (val, ==, ip1_str);
+	g_free (val);
+
+	val = svGetValue (f, "IPADDR1", FALSE);
+	g_assert (val);
+	g_assert_cmpstr (val, ==, ip2_str);
+	g_free (val);
+
+	val = svGetValue (f, "IPADDR0", FALSE);
+	g_assert (val == NULL);
+
+	val = svGetValue (f, "PREFIX", FALSE);
+	g_assert (val);
+	g_assert_cmpstr (val, ==, "24");
+	g_free (val);
+
+	val = svGetValue (f, "PREFIX1", FALSE);
+	g_assert (val);
+	g_assert_cmpstr (val, ==, "24");
+	g_free (val);
+
+	val = svGetValue (f, "PREFIX0", FALSE);
+	g_assert (val == NULL);
+
+	val = svGetValue (f, "GATEWAY", FALSE);
+	g_assert (val);
+	g_assert_cmpstr (val, ==, gw1_str);
+	g_free (val);
+
+	val = svGetValue (f, "GATEWAY1", FALSE);
+	g_assert (val);
+	g_assert_cmpstr (val, ==, gw2_str);
+	g_free (val);
+
+	val = svGetValue (f, "GATEWAY0", FALSE);
+	g_assert (val == NULL);
+
+
+	svCloseFile (f);
+
+	/* reread will be normalized, so we must normalize connection too. */
+	nm_utils_normalize_connection (connection, TRUE);
+
+	/* re-read the connection for comparison */
+	reread = connection_from_file (testfile, NULL, TYPE_WIRELESS, NULL,
+	                               NULL, NULL, NULL, NULL, &error, NULL);
+	unlink (testfile);
+	g_assert_no_error (error);
+	g_assert (reread);
+
+	success = nm_connection_verify (reread, &error);
+	g_assert_no_error (error);
+	g_assert (success);
+
+	g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT));
+
+	g_free (testfile);
+	g_object_unref (connection);
+	g_object_unref (reread);
+}
+
+
+static void
 test_write_wifi_open (void)
 {
 	NMConnection *connection;
@@ -14459,6 +14605,7 @@ int main (int argc, char **argv)
 	test_write_wired_8021x_tls (NM_SETTING_802_1X_CK_SCHEME_PATH, NM_SETTING_SECRET_FLAG_AGENT_OWNED | NM_SETTING_SECRET_FLAG_NOT_SAVED);
 	test_write_wired_8021x_tls (NM_SETTING_802_1X_CK_SCHEME_BLOB, NM_SETTING_SECRET_FLAG_NONE);
 	test_write_wired_aliases ();
+	g_test_add_func (TPATH "ipv4/write-static-addresses-GATEWAY", test_write_gateway);
 	test_write_wifi_open ();
 	test_write_wifi_open_hex_ssid ();
 	test_write_wifi_wep ();
diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c
index f583366..b9c108c 100644
--- a/src/settings/plugins/ifcfg-rh/writer.c
+++ b/src/settings/plugins/ifcfg-rh/writer.c
@@ -1913,6 +1913,11 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
 	svSetValue (ifcfg, "PREFIX", NULL, FALSE);
 	svSetValue (ifcfg, "NETMASK", NULL, FALSE);
 	svSetValue (ifcfg, "GATEWAY", NULL, FALSE);
+	/* Clear out zero-indexed IP address fields */
+	svSetValue (ifcfg, "IPADDR0", NULL, FALSE);
+	svSetValue (ifcfg, "PREFIX0", NULL, FALSE);
+	svSetValue (ifcfg, "NETMASK0", NULL, FALSE);
+	svSetValue (ifcfg, "GATEWAY0", NULL, FALSE);
 
 	/* Write out IPADDR<n>, PREFIX<n>, GATEWAY<n> for current IP addresses
 	 * without labels. Unset obsolete NETMASK<n>.
@@ -1926,10 +1931,22 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error)
 		if (i > 0 && NM_UTIL_PRIVATE_CALL (nm_setting_ip4_config_get_address_label (s_ip4, i)))
 			continue;
 
-		addr_key = g_strdup_printf ("IPADDR%d", n);
-		prefix_key = g_strdup_printf ("PREFIX%d", n);
-		netmask_key = g_strdup_printf ("NETMASK%d", n);
-		gw_key = g_strdup_printf ("GATEWAY%d", n);
+		if (n == 0) {
+			/* Instead of index 0 use un-numbered variables.
+			 * It's needed for compatibility with ifup that only recognizes 'GATEAWAY'
+			 * See https://bugzilla.redhat.com/show_bug.cgi?id=771673
+			 * and https://bugzilla.redhat.com/show_bug.cgi?id=1105770
+			 */
+			addr_key = g_strdup ("IPADDR");
+			prefix_key = g_strdup ("PREFIX");
+			netmask_key = g_strdup ("NETMASK");
+			gw_key = g_strdup ("GATEWAY");
+		} else {
+			addr_key = g_strdup_printf ("IPADDR%d", n);
+			prefix_key = g_strdup_printf ("PREFIX%d", n);
+			netmask_key = g_strdup_printf ("NETMASK%d", n);
+			gw_key = g_strdup_printf ("GATEWAY%d", n);
+		}
 
 		addr = nm_setting_ip4_config_get_address (s_ip4, i);
 
-- 
1.7.11.7