From 09a5d8165cdf77643fbbb239aadcb11d2ca90595 Mon Sep 17 00:00:00 2001 From: Adam Julis Date: Tue, 9 Jul 2024 17:23:02 +0200 Subject: [PATCH] network: allow "modify" option for DNS hostname The "modify" command allows you to replace an existing record (its hostname, sub-elements). IP address acts as the primary key. If it is not found, the attempt ends with an error message. If the XML contains a duplicate address, it will select the last one. Tests in networkxml2xmlupdatetest.c contain replacements of an existing DNS-Host record and failure due to non-existing record. Resolves: https://gitlab.com/libvirt/libvirt/-/issues/639 Signed-off-by: Adam Julis Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- src/conf/network_conf.c | 30 ++++++++++++++----- .../dns-host-modify-not-existing.xml | 4 +++ .../dns-host-modify.xml | 5 ++++ .../nat-network-dns-hosts-modified.xml | 28 +++++++++++++++++ tests/networkxml2xmlupdatetest.c | 9 ++++++ 5 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 tests/networkxml2xmlupdatein/dns-host-modify-not-existing.xml create mode 100644 tests/networkxml2xmlupdatein/dns-host-modify.xml create mode 100644 tests/networkxml2xmlupdateout/nat-network-dns-hosts-modified.xml diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c index f5ccf4bd12..3c19ff4ca5 100644 --- a/src/conf/network_conf.c +++ b/src/conf/network_conf.c @@ -3138,19 +3138,15 @@ virNetworkDefUpdateDNSHost(virNetworkDef *def, unsigned int fflags G_GNUC_UNUSED) { size_t i, j, k; - int foundIdx = -1, ret = -1; + int foundIdx = -1; + int foundIdxModify = -1; + int ret = -1; virNetworkDNSDef *dns = &def->dns; virNetworkDNSHostDef host = { 0 }; bool isAdd = (command == VIR_NETWORK_UPDATE_COMMAND_ADD_FIRST || command == VIR_NETWORK_UPDATE_COMMAND_ADD_LAST); int foundCt = 0; - if (command == VIR_NETWORK_UPDATE_COMMAND_MODIFY) { - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", - _("DNS HOST records cannot be modified, only added or deleted")); - goto cleanup; - } - if (virNetworkDefUpdateCheckElementName(def, ctxt->node, "host") < 0) goto cleanup; @@ -3163,6 +3159,12 @@ virNetworkDefUpdateDNSHost(virNetworkDef *def, if (virSocketAddrEqual(&host.ip, &dns->hosts[i].ip)) foundThisTime = true; + /* modify option required index of matching ip-address, the loop under + * this comment could affect results of found index foundThisTime, + * so the foundIdxModify is there used instead */ + if (foundThisTime) + foundIdxModify = i; + /* when adding we want to only check duplicates of address since having * multiple addresses with the same hostname is a legitimate configuration */ if (!isAdd) { @@ -3213,6 +3215,20 @@ virNetworkDefUpdateDNSHost(virNetworkDef *def, virNetworkDNSHostDefClear(&dns->hosts[foundIdx]); VIR_DELETE_ELEMENT(dns->hosts, foundIdx, dns->nhosts); + } else if (command == VIR_NETWORK_UPDATE_COMMAND_MODIFY) { + + if (foundCt == 0) { + virReportError(VIR_ERR_OPERATION_INVALID, + _("couldn't locate a matching DNS HOST record in network %1$s"), + def->name); + goto cleanup; + } + + virNetworkDNSHostDefClear(&dns->hosts[foundIdxModify]); + + memcpy(&dns->hosts[foundIdxModify], &host, sizeof(virNetworkDNSHostDef)); + memset(&host, 0, sizeof(virNetworkDNSHostDef)); + } else { virNetworkDefUpdateUnknownCommand(command); goto cleanup; diff --git a/tests/networkxml2xmlupdatein/dns-host-modify-not-existing.xml b/tests/networkxml2xmlupdatein/dns-host-modify-not-existing.xml new file mode 100644 index 0000000000..125dc2ab10 --- /dev/null +++ b/tests/networkxml2xmlupdatein/dns-host-modify-not-existing.xml @@ -0,0 +1,4 @@ + + shared + names + diff --git a/tests/networkxml2xmlupdatein/dns-host-modify.xml b/tests/networkxml2xmlupdatein/dns-host-modify.xml new file mode 100644 index 0000000000..c30ac30f1a --- /dev/null +++ b/tests/networkxml2xmlupdatein/dns-host-modify.xml @@ -0,0 +1,5 @@ + + Another + decent + names + diff --git a/tests/networkxml2xmlupdateout/nat-network-dns-hosts-modified.xml b/tests/networkxml2xmlupdateout/nat-network-dns-hosts-modified.xml new file mode 100644 index 0000000000..8fcaad15d1 --- /dev/null +++ b/tests/networkxml2xmlupdateout/nat-network-dns-hosts-modified.xml @@ -0,0 +1,28 @@ + + default + 81ff0d90-c91e-6742-64da-4a736edb9a9c + + + + + + + pudding + + + host + gateway + + + Another + decent + names + + + shared + names + + + + + diff --git a/tests/networkxml2xmlupdatetest.c b/tests/networkxml2xmlupdatetest.c index afe2b1f574..383cbf85ce 100644 --- a/tests/networkxml2xmlupdatetest.c +++ b/tests/networkxml2xmlupdatetest.c @@ -276,6 +276,15 @@ mymain(void) "nat-network-dns-hosts", "nat-network-no-hosts", VIR_NETWORK_UPDATE_COMMAND_DELETE); + DO_TEST("modify-dns-host", + "dns-host-modify", + "nat-network-dns-hosts", + "nat-network-dns-hosts-modified", + VIR_NETWORK_UPDATE_COMMAND_MODIFY); + DO_TEST_FAIL("modify-dns-host-not-existing", + "dns-host-modify-not-existing", + "nat-network-dns-hosts", + VIR_NETWORK_UPDATE_COMMAND_MODIFY); section = VIR_NETWORK_SECTION_DNS_TXT;