keyfile: emit a warning when the gateway is set in different keys

The gateway can be set both in the "address*" and and in the "gateway"
keys. Raise a warning when it is set multiple times to different
values.
This commit is contained in:
Beniamino Galvani
2024-12-02 10:01:37 +01:00
committed by Fernando Fernandez Mancera
parent 38dca2f044
commit 69b18ce914
3 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
[connection]
id=Test Duplicate Gateways
uuid=5e2a7b1e-e4c8-4964-88c4-ca7255471aa1
type=802-3-ethernet
autoconnect=true
timestamp=6654332
[ipv4]
method=manual
address1=192.168.0.5/24;192.168.0.254
address2=192.0.2.1/16
gateway=192.168.0.253
[ipv6]
method=manual
gateway=fd01::bbbb
address1=fd01::1/64;fd01::aaaa

View File

@@ -403,6 +403,34 @@ test_read_valid_wired_connection(void)
nmtst_assert_route_attribute_string(route, NM_IP_ROUTE_ATTRIBUTE_FROM, "abce::/63");
}
static void
test_read_duplicate_gateways(void)
{
gs_unref_object NMConnection *connection = NULL;
NMSettingIPConfig *s_ip4;
NMSettingIPConfig *s_ip6;
NMTST_EXPECT_NM_WARN(
"*ipv4* ignoring gateway * from \"address*\" keys because the \"gateway\" key is set*");
NMTST_EXPECT_NM_WARN(
"*ipv6* ignoring gateway * from \"address*\" keys because the \"gateway\" key is set*");
connection = keyfile_read_connection_from_file(TEST_KEYFILES_DIR "/Test_Duplicate_Gateways");
g_test_assert_expected_messages();
s_ip4 = nm_connection_get_setting_ip4_config(connection);
g_assert(s_ip4);
g_assert_cmpint(nm_setting_ip_config_get_num_addresses(s_ip4), ==, 2);
check_ip_address(s_ip4, 0, "192.168.0.5", 24);
check_ip_address(s_ip4, 1, "192.0.2.1", 16);
g_assert_cmpstr(nm_setting_ip_config_get_gateway(s_ip4), ==, "192.168.0.253");
s_ip6 = nm_connection_get_setting_ip6_config(connection);
g_assert(s_ip6);
g_assert_cmpint(nm_setting_ip_config_get_num_addresses(s_ip6), ==, 1);
check_ip_address(s_ip6, 0, "fd01::1", 64);
g_assert_cmpstr(nm_setting_ip_config_get_gateway(s_ip6), ==, "fd01::bbbb");
}
static void
add_one_ip_address(NMSettingIPConfig *s_ip, const char *addr, guint32 prefix)
{
@@ -2899,6 +2927,8 @@ main(int argc, char **argv)
g_test_add_func("/keyfile/test_read_valid_wired_connection", test_read_valid_wired_connection);
g_test_add_func("/keyfile/test_write_wired_connection", test_write_wired_connection);
g_test_add_func("/keyfile/test_read_duplicate_gateways", test_read_duplicate_gateways);
g_test_add_func("/keyfile/test_read_ip6_wired_connection", test_read_ip6_wired_connection);
g_test_add_func("/keyfile/test_write_ip6_wired_connection", test_write_ip6_wired_connection);

View File

@@ -938,6 +938,31 @@ _build_list_create(GKeyFile *keyfile,
return g_steal_pointer(&build_list);
}
static void
gateway_parser(KeyfileReaderInfo *info, NMSetting *setting, const char *key)
{
const char *setting_name = nm_setting_get_name(setting);
gs_free char *gateway = NULL;
const char *old_gateway;
gateway = nm_keyfile_plugin_kf_get_string(info->keyfile, setting_name, key, NULL);
if (!gateway)
return;
old_gateway = nm_setting_ip_config_get_gateway(NM_SETTING_IP_CONFIG(setting));
if (old_gateway && !nm_streq0(gateway, old_gateway)) {
read_handle_warn(info,
key,
NM_SETTING_IP_CONFIG_GATEWAY,
NM_KEYFILE_WARN_SEVERITY_WARN,
_("ignoring gateway \"%s\" from \"address*\" keys because the "
"\"gateway\" key is set"),
old_gateway);
}
g_object_set(setting, NM_SETTING_IP_CONFIG_GATEWAY, gateway, NULL);
}
static void
ip_address_or_route_parser(KeyfileReaderInfo *info, NMSetting *setting, const char *setting_key)
{
@@ -3055,6 +3080,7 @@ static const ParseInfoSetting *const parse_infos[_NM_META_SETTING_TYPE_NUM] = {
.parser = ip_dns_parser,
.writer = dns_writer, ),
PARSE_INFO_PROPERTY(NM_SETTING_IP_CONFIG_DNS_OPTIONS, .always_write = TRUE, ),
PARSE_INFO_PROPERTY(NM_SETTING_IP_CONFIG_GATEWAY, .parser = gateway_parser, ),
PARSE_INFO_PROPERTY(NM_SETTING_IP_CONFIG_ROUTES,
.parser_no_check_key = TRUE,
.parser = ip_address_or_route_parser,
@@ -3082,6 +3108,7 @@ static const ParseInfoSetting *const parse_infos[_NM_META_SETTING_TYPE_NUM] = {
.parser = ip_dns_parser,
.writer = dns_writer, ),
PARSE_INFO_PROPERTY(NM_SETTING_IP_CONFIG_DNS_OPTIONS, .always_write = TRUE, ),
PARSE_INFO_PROPERTY(NM_SETTING_IP_CONFIG_GATEWAY, .parser = gateway_parser, ),
PARSE_INFO_PROPERTY(NM_SETTING_IP_CONFIG_ROUTES,
.parser_no_check_key = TRUE,
.parser = ip_address_or_route_parser,