core: keep empty groups from keyfile configs

When reading NetworkManager.conf and NetworkManager-intern.conf we might
need to know if a group is defined or not, even if it's empty. This is
the case, for example, for [global-dns]. If [global-dns] is defined in
NM.conf overwrites the config from NM-intern, and if it's defined in any
of them they overwrite the configs from connections.

Before this patch, defining it as an empty group was ignored:
```
[global-dns]
```

Instead, it was necessary to add at least one key-value to the group.
Otherwise the group was silently ignored.
```
[global-dns]
searches=
```

Keep empty groups so we can take better decissions about overwritting
configs from other sources.

(cherry picked from commit 4a46f454da)
This commit is contained in:
Íñigo Huguet 2025-09-04 16:54:21 +02:00
parent 58e776c3a8
commit 2de3de0c28
4 changed files with 43 additions and 1 deletions

View file

@ -18,6 +18,7 @@
#include "libnm-core-intern/nm-core-internal.h"
#include "libnm-core-intern/nm-keyfile-internal.h"
#include "libnm-core-intern/nm-keyfile-utils.h"
#include "libnm-glib-aux/nm-keyfile-aux.h"
#define DEFAULT_CONFIG_MAIN_FILE NMCONFDIR "/NetworkManager.conf"
#define DEFAULT_CONFIG_DIR NMCONFDIR "/conf.d"
@ -1046,6 +1047,10 @@ read_config(GKeyFile *keyfile,
/* internal groups cannot be set by user configuration. */
continue;
}
if (!g_key_file_has_group(keyfile, group))
nm_key_file_add_group(keyfile, group);
keys = g_key_file_get_keys(kf, group, &nkeys, NULL);
if (!keys)
continue;
@ -1639,6 +1644,12 @@ intern_config_read(const char *filename,
"");
}
if (!g_key_file_has_group(keyfile_intern, group)) {
nm_key_file_add_group(keyfile_intern, group);
if (is_intern)
has_intern = TRUE;
}
for (k = 0; keys[k]; k++) {
gs_free char *value_set = NULL;
const char *key = keys[k];
@ -1823,6 +1834,9 @@ intern_config_write(const char *filename,
}
}
if (!g_key_file_has_group(keyfile, group))
nm_key_file_add_group(keyfile, group);
for (k = 0; keys[k]; k++) {
const char *key = keys[k];
gs_free char *value_set = NULL;

View file

@ -0,0 +1,3 @@
# Good configuration, an empty global-dns section must be valid
[global-dns]

View file

@ -0,0 +1,5 @@
# Good configuration, an empty [global-dns] must be implicitly assumed because a domain is defined
[global-dns-domain-*]
servers=4.5.6.7
options=myoption1

View file

@ -387,7 +387,27 @@ test_config_global_dns(void)
g_assert(dns);
g_object_unref(config);
/* Check that a file with a domain domain, but without a default one gives a NULL configuration */
/* Check that a file with an empty global-dns section gives a good configuration.
* Check also that searches and options are not NULL, as this is how we expose to
* D-Bus that global-dns is defined. */
config =
setup_config(NULL, TEST_DIR "/global-dns-empty.conf", "", NULL, "/no/such/dir", "", NULL);
dns = nm_config_data_get_global_dns_config(nm_config_get_data_orig(config));
g_assert(dns);
g_assert(nm_global_dns_config_get_searches(dns));
g_assert(nm_global_dns_config_get_options(dns));
g_object_unref(config);
/* Check that a file with a domain, but no global-dns, assumes an implicit empty global-dns */
config =
setup_config(NULL, TEST_DIR "/global-dns-not-set.conf", "", NULL, "/no/such/dir", "", NULL);
dns = nm_config_data_get_global_dns_config(nm_config_get_data_orig(config));
g_assert(dns);
g_assert(nm_global_dns_config_get_searches(dns));
g_assert(nm_global_dns_config_get_options(dns));
g_object_unref(config);
/* Check that a file with a domain, but without a default one, gives a NULL configuration */
config =
setup_config(NULL, TEST_DIR "/global-dns-invalid.conf", "", NULL, "/no/such/dir", "", NULL);
dns = nm_config_data_get_global_dns_config(nm_config_get_data_orig(config));