diff options
author | Thomas Haller <thaller@redhat.com> | 2017-05-25 15:00:27 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2017-05-30 13:44:04 +0200 |
commit | 92a277dc6f08d66cf567b9bf6f1c32c513c7325e (patch) | |
tree | ae45a77c35591ecfeb8828b7fa6ab4a16e232965 | |
parent | 7ebc132864776a1c0ce4a33660408a2953e076c8 (diff) | |
download | NetworkManager-92a277dc6f08d66cf567b9bf6f1c32c513c7325e.tar.gz |
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".
(cherry picked from commit 844bf3d1a239f409d12ea437dbe866b66f5f1af4)
-rw-r--r-- | src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c | 32 | ||||
-rw-r--r-- | src/settings/plugins/ifcfg-rh/tests/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 c66af01097..d2b7ff67c2 100644 --- a/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c +++ b/src/settings/plugins/ifcfg-rh/nms-ifcfg-rh-writer.c @@ -2067,6 +2067,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) { @@ -2139,16 +2140,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<n>, PREFIX<n>, GATEWAY<n> for current IP addresses * without labels. Unset obsolete NETMASK<n>. @@ -2192,20 +2184,30 @@ write_ip4_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) g_free (tmp); /* 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<n..255>, 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 |