Skip to content

Commit be6786d

Browse files
author
Jared Hendrickson
committed
Added host override unique constraint tolerance for unchanged value updates
1 parent ee93f81 commit be6786d

4 files changed

Lines changed: 19 additions & 15 deletions

File tree

pfSense-pkg-API/files/etc/inc/api/framework/APITools.inc

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -709,28 +709,32 @@ function unbound_reload_config() {
709709
}
710710

711711
// Check if a DNS Resolver (Unbound) host override already exists
712-
function unbound_host_override_exists($hostname, $domain) {
713-
// Local variables
714-
global $err_lib, $config;
715-
$curr_hosts = array();
712+
function is_unbound_fqdn($hostname, $domain, $instance_id=null) {
713+
# Local variables
714+
global $config;
715+
$curr_hosts = (array_key_exists("hosts", $config["unbound"])) ? $config["unbound"]["hosts"] : [];
716716
$host_exists = false;
717-
// Check if host override already exists
718-
if (array_key_exists("hosts", $config["unbound"])) {
719-
$curr_hosts = $config["unbound"]["hosts"];
720-
}
717+
$index = 0;
718+
719+
# Loop through each host override and check if the FQDN already exists
721720
foreach ($curr_hosts as $host_ent) {
721+
# Check the FQDN matches this entry
722722
if ($host_ent["host"] === $hostname and $host_ent["domain"] === $domain) {
723-
$host_exists = true;
724-
break;
723+
# If we are working with an existing instance, allow existing FQDN if ID matches
724+
if ($index !== $instance_id) {
725+
return true;
726+
}
725727
}
728+
729+
# Check FQDN within host override aliases as well
726730
if (is_array($host_ent["aliases"])) {
727731
foreach ($host_ent["aliases"]["item"] as $alias_ent) {
728732
if ($alias_ent["host"] === $hostname and $alias_ent["domain"] === $domain) {
729-
$host_exists = true;
730-
break;
733+
return true;
731734
}
732735
}
733736
}
737+
$index++;
734738
}
735739
return $host_exists;
736740
}

pfSense-pkg-API/files/etc/inc/api/models/APIServicesUnboundHostOverrideAliasCreate.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class APIServicesUnboundHostOverrideAliasCreate extends APIModel {
7373
}
7474

7575
# Ensure this hostname doesn't already exis
76-
if (APITools\unbound_host_override_exists($this->validated_data["host"], $this->validated_data["domain"])) {
76+
if (APITools\is_unbound_fqdn($this->validated_data["host"], $this->validated_data["domain"])) {
7777
$this->errors[] = APIResponse\get(2009);
7878
}
7979
}

pfSense-pkg-API/files/etc/inc/api/models/APIServicesUnboundHostOverrideCreate.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class APIServicesUnboundHostOverrideCreate extends APIModel {
5151
# Check for our required 'domain' payload value
5252
if (isset($this->initial_data['domain'])) {
5353
$this->validated_data["domain"] = trim($this->initial_data['domain']);
54-
if (APITools\unbound_host_override_exists($this->validated_data["host"], $this->validated_data["domain"])) {
54+
if (APITools\is_unbound_fqdn($this->validated_data["host"], $this->validated_data["domain"])) {
5555
$this->errors[] = APIResponse\get(2010);
5656
}
5757
} else {

pfSense-pkg-API/files/etc/inc/api/models/APIServicesUnboundHostOverrideUpdate.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class APIServicesUnboundHostOverrideUpdate extends APIModel {
7575

7676
# If host or domain was updated, ensure this host override doesn't already exist
7777
if (isset($this->initial_data["host"]) or isset($this->initial_data["domain"])) {
78-
if (APITools\unbound_host_override_exists($this->validated_data["host"], $this->validated_data["domain"])) {
78+
if (APITools\is_unbound_fqdn($this->validated_data["host"], $this->validated_data["domain"], $this->id)) {
7979
$this->errors[] = APIResponse\get(2010);
8080
}
8181
}

0 commit comments

Comments
 (0)