From 844bf3d1a239f409d12ea437dbe866b66f5f1af4 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 25 May 2017 15:00:27 +0200 Subject: [PATCH] ifcfg-rh: fix preserving NETMASK key in write_ip4_setting() To support legacy scripts, we want to write out the NETMASK key whenever the ifcfg file has a NETMASK key previously. Note, that we anyway always write the relevant PREFIX key. The NETMASK is redundant, only there to help legacy scripts. That was broken, because we would svUnsetValue("NETMASK") before checking whether the NETMASK key is present. Also, when saving a connection to ifcfg-rh file that was created by other tools, we might mix up the numbering. E.g. we never write out IPADDR0. Hence, turn on legacy mode whenever the ifcfg-rh file has any key starting with "NETMASK". --- .../plugins/ifcfg-rh/nms-ifcfg-rh-writer.c | 32 ++++++++++--------- .../network-scripts/ifcfg-netmask-1.cexpected | 1 + 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c index c298f4e715..8409b883a9 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -2056,6 +2056,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) int timeout; GString *searches; const char *method = NULL; + gboolean has_netmask; s_ip4 = nm_connection_get_setting_ip4_config (connection); if (!s_ip4) { @@ -2128,16 +2129,7 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) else if (!strcmp (method, NM_SETTING_IP4_CONFIG_METHOD_SHARED)) svSetValueStr (ifcfg, "BOOTPROTO", "shared"); - /* Clear out un-numbered IP address fields */ - svUnsetValue (ifcfg, "IPADDR"); - svUnsetValue (ifcfg, "PREFIX"); - svUnsetValue (ifcfg, "NETMASK"); - svUnsetValue (ifcfg, "GATEWAY"); - /* Clear out zero-indexed IP address fields */ - svUnsetValue (ifcfg, "IPADDR0"); - svUnsetValue (ifcfg, "PREFIX0"); - svUnsetValue (ifcfg, "NETMASK0"); - svUnsetValue (ifcfg, "GATEWAY0"); + has_netmask = !!svFindFirstKeyWithPrefix (ifcfg, "NETMASK"); /* Write out IPADDR, PREFIX, GATEWAY for current IP addresses * without labels. Unset obsolete NETMASK. @@ -2179,20 +2171,30 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) svSetValueInt64 (ifcfg, prefix_key, prefix); /* If the legacy "NETMASK" is present, keep it. */ - if (svGetValue (ifcfg, netmask_key, &tmp)) { + if (has_netmask) { char buf[INET_ADDRSTRLEN]; - g_free (tmp); svSetValueStr (ifcfg, netmask_key, nm_utils_inet4_ntop (nm_utils_ip4_prefix_to_netmask (prefix), buf)); - } + } else + svUnsetValue (ifcfg, netmask_key); svUnsetValue (ifcfg, gw_key); n++; } - /* Clear remaining IPADDR, etc */ - for (i = n; i < 256; i++) { + svUnsetValue (ifcfg, "IPADDR0"); + svUnsetValue (ifcfg, "PREFIX0"); + svUnsetValue (ifcfg, "NETMASK0"); + svUnsetValue (ifcfg, "GATEWAY0"); + if (n == 0) { + svUnsetValue (ifcfg, "IPADDR"); + svUnsetValue (ifcfg, "PREFIX"); + svUnsetValue (ifcfg, "NETMASK"); + i = 1; + } else + i = n; + for (; i < 256; i++) { nm_sprintf_buf (addr_key, "IPADDR%u", i); nm_sprintf_buf (prefix_key, "PREFIX%u", i); nm_sprintf_buf (netmask_key, "NETMASK%u", i); diff --git a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-netmask-1.cexpected b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-netmask-1.cexpected index 4c2294afd4..5dfdce4d0a 100644 --- a/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-netmask-1.cexpected +++ b/src/settings/plugins/ifcfg-rh/tests/network-scripts/ifcfg-netmask-1.cexpected @@ -1,6 +1,7 @@ DNS1=192.0.2.1 IPADDR=102.0.2.2 GATEWAY=192.0.2.1 +NETMASK=255.254.0.0 BOOTPROTO="static" DEVICE=eth1 ONBOOT=yes