core: fix deleting internal global DNS configuration

The tracking of variable "has_intern" in intern_config_read() is
wrong: we set it when adding any entry to the keyfile, but then we
remove the global DNS section without updating the variable.

The effect is that the function might return an empty keyfile instead
of NULL.

Fix this by moving the check on global DNS above.

Fixes: 55c204b9a3 ('core: add support for reading global DNS configuration from keyfile')
This commit is contained in:
Beniamino Galvani 2024-08-29 10:58:01 +02:00
parent 40ac7b1406
commit 07113dde30

View file

@ -1562,6 +1562,7 @@ intern_config_read(const char *filename,
gs_strfreev char **groups = NULL;
guint g, k;
gboolean has_intern = FALSE;
gboolean has_global_dns;
g_return_val_if_fail(filename, NULL);
@ -1579,6 +1580,8 @@ intern_config_read(const char *filename,
goto out;
}
has_global_dns = nm_config_keyfile_has_global_dns_config(keyfile_conf, FALSE);
groups = g_key_file_get_groups(keyfile, NULL);
for (g = 0; groups && groups[g]; g++) {
gs_strfreev char **keys = NULL;
@ -1595,6 +1598,21 @@ intern_config_read(const char *filename,
is_intern = NM_STR_HAS_PREFIX(group, NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN);
is_atomic = !is_intern && _is_atomic_section(atomic_section_prefixes, group);
if (has_global_dns
&& (nm_streq0(group, NM_CONFIG_KEYFILE_GROUP_INTERN_GLOBAL_DNS)
|| NM_STR_HAS_PREFIX_WITH_MORE(
group,
NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN_GLOBAL_DNS_DOMAIN))) {
/*
* If user configuration specifies global DNS options, the DNS
* options in internal configuration must be deleted. Otherwise, a
* deletion of options from user configuration may cause the
* internal options to appear again.
*/
needs_rewrite = TRUE;
continue;
}
if (is_atomic) {
gs_free char *conf_section_was = NULL;
gs_free char *conf_section_is = NULL;
@ -1688,26 +1706,6 @@ intern_config_read(const char *filename,
}
out:
/*
* If user configuration specifies global DNS options, the DNS
* options in internal configuration must be deleted. Otherwise, a
* deletion of options from user configuration may cause the
* internal options to appear again.
*/
if (nm_config_keyfile_has_global_dns_config(keyfile_conf, FALSE)) {
if (g_key_file_remove_group(keyfile_intern,
NM_CONFIG_KEYFILE_GROUP_INTERN_GLOBAL_DNS,
NULL))
needs_rewrite = TRUE;
for (g = 0; groups && groups[g]; g++) {
if (NM_STR_HAS_PREFIX(groups[g], NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN_GLOBAL_DNS_DOMAIN)
&& groups[g][NM_STRLEN(NM_CONFIG_KEYFILE_GROUPPREFIX_INTERN_GLOBAL_DNS_DOMAIN)]) {
g_key_file_remove_group(keyfile_intern, groups[g], NULL);
needs_rewrite = TRUE;
}
}
}
g_key_file_unref(keyfile);
if (out_needs_rewrite)