Skip to content

Commit 15c0bd5

Browse files
Reorganized DNS server endpoints, pfSense 2.5 compatibility updates, fixed PHP warning in manage.php restore() function
1 parent 6db03db commit 15c0bd5

5 files changed

Lines changed: 113 additions & 56 deletions

File tree

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,21 @@ class APISystemDNSRead extends APIModel {
3939
if (array_key_exists("dnsallowoverride", $this->config["system"])) {
4040
$this->validated_data["dnsallowoverride"] = true;
4141
}
42-
if (array_key_exists("dnslocalhost", $this->config["system"])) {
43-
$this->validated_data["dnslocalhost"] = true;
42+
43+
# Validate this field as needed for pfSense 2.4.x
44+
# TODO: remove this conditional once pfSense 2.4 nears EOL
45+
if (APITools\get_pfsense_version()["program"] < 250) {
46+
if (array_key_exists("dnslocalhost", $this->config["system"])) {
47+
$this->validated_data["dnslocalhost"] = true;
48+
}
49+
}
50+
else {
51+
if ($this->config["system"]["dnslocalhost"] === "remote") {
52+
$this->validated_data["dnslocalhost"] = false;
53+
} else {
54+
$this->validated_data["dnslocalhost"] = true;
55+
}
4456
}
57+
4558
}
4659
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class APISystemDNSServerCreate extends APIModel {
4040
$this->__validate_dnsserver();
4141
}
4242

43-
private function __validate_dnsserver() {
43+
public function __validate_dnsserver() {
44+
# Validate the optional `dnsserver` payload value
4445
if (isset($this->initial_data['dnsserver'])) {
4546
# If values are not an array, convert it
4647
if (!is_array($this->initial_data["dnsserver"])) {

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

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,56 @@ class APISystemDNSServerDelete extends APIModel {
2727
}
2828

2929
public function action() {
30-
$this->write_config(); // Apply our configuration change
31-
// Update a slew of backend services
32-
system_resolvconf_generate();
33-
if (isset($this->config['dnsmasq']['enable'])) {
34-
services_dnsmasq_configure();
35-
} elseif (isset($this->config['unbound']['enable'])) {
36-
services_unbound_configure();
37-
}
38-
send_event("service reload dns");
39-
filter_configure();
30+
$this->config["system"]["dnsserver"] = $this->validated_data["dnsserver"];
31+
$this->write_config();
32+
$this->apply_backend_changes();
4033
return APIResponse\get(0, $this->validated_data);
4134
}
4235

4336
public function validate_payload() {
37+
$this->validated_data["dnsserver"] = $this->config["system"]["dnsserver"];
38+
$this->__validate_dnsserver();
39+
}
40+
41+
private function __validate_dnsserver() {
42+
# Validate the optional `dnsserver` payload value
4443
if (isset($this->initial_data['dnsserver'])) {
4544
$del_server = $this->initial_data['dnsserver'];
46-
$curr_servers = $this->config["system"]["dnsserver"];
4745
$del_server = (!is_array($del_server)) ? array($del_server) : $del_server;
46+
47+
# Ensure our config is array
48+
if (!is_array($this->validated_data["dnsserver"])) {
49+
$this->validated_data["dnsserver"] = array($this->validated_data["dnsserver"]);
50+
}
51+
52+
# Loop through each requested DNS server to delete
4853
foreach ($del_server as $ds) {
49-
// Ensure our config is array
50-
if (!is_array($curr_servers)) {
51-
$curr_servers = array($this->config["system"]["dnsserver"]);
52-
}
53-
// Loop through each server and check for matches, delete on match
54-
foreach ($curr_servers as $id => $cs) {
54+
# Loop through each server and check for matches, delete on match
55+
foreach ($this->validated_data["dnsserver"] as $id => $cs) {
5556
if ($ds === $cs) {
56-
$this->validated_data["dnsserver"][] = $ds;
57-
unset($this->config["system"]["dnsserver"][$id]);
57+
unset($this->validated_data["dnsserver"][$id]);
5858
}
5959
}
6060
}
61+
62+
# Ensure array values are unique, reindexed and purge duplicate items
63+
$this->validated_data["dnsserver"] = array_filter($this->validated_data["dnsserver"]);
64+
$this->validated_data["dnsserver"] = array_unique($this->validated_data["dnsserver"]);
65+
$this->validated_data["dnsserver"] = array_values($this->validated_data["dnsserver"]);
6166
}
6267
}
68+
69+
public function apply_backend_changes() {
70+
# Update a slew of backend services
71+
system_resolvconf_generate();
72+
if (isset($this->config['dnsmasq']['enable'])) {
73+
services_dnsmasq_configure();
74+
} elseif (isset($this->config['unbound']['enable'])) {
75+
services_unbound_configure();
76+
}
77+
78+
# Reload DNS services and firewall filter
79+
send_event("service reload dns");
80+
filter_configure();
81+
}
6382
}

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

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,47 +27,67 @@ class APISystemDNSUpdate extends APIModel {
2727
}
2828

2929
public function action() {
30-
$this->write_config(); // Apply our configuration change
31-
// Update a slew of backend services
32-
system_resolvconf_generate();
33-
if (isset($this->config['dnsmasq']['enable'])) {
34-
services_dnsmasq_configure();
35-
} elseif (isset($this->config['unbound']['enable'])) {
36-
services_unbound_configure();
37-
}
38-
39-
// Reload DNS services and firewall filter
40-
send_event("service reload dns");
41-
filter_configure();
30+
$this->write_config();
31+
$this->apply_backend_changes();
4232
return APIResponse\get(0, (new APISystemDNSRead())->action()["data"]);
4333
}
4434

4535
public function validate_payload() {
46-
if (isset($this->initial_data['dnsserver'])) {
47-
$this->initial_data["dnsserver"] = $this->initial_data['dnsserver'];
48-
// If value is not an array, convert it
49-
if (!is_array($this->initial_data["dnsserver"])) {
50-
$this->initial_data["dnsserver"] = array($this->initial_data["dnsserver"]);
51-
}
52-
// Loop through our DNS servers and check that entry is valid
53-
foreach ($this->initial_data["dnsserver"] as $ds) {
54-
// Check if our DNS server is valid
55-
if (!is_ipaddrv4($ds) and !is_ipaddrv6($ds)) {
56-
$this->errors[] = APIResponse\get(1007);
57-
}
58-
}
59-
// Add our system DNS values to validated data
60-
$this->config["system"]["dnsserver"] = $this->initial_data["dnsserver"];
61-
}
36+
$this->__validate_dnsserver();
37+
$this->__validate_dnsallowoverride();
38+
$this->__validate_dnslocalhost();
39+
}
40+
41+
private function __validate_dnsallowoverride() {
6242
if ($this->initial_data['dnsallowoverride'] === true) {
6343
$this->config["system"]["dnsallowoverride"] = "";
6444
} elseif ($this->initial_data['dnsallowoverride'] === false) {
6545
unset($this->config["system"]["dnsallowoverride"]);
6646
}
67-
if ($this->initial_data['dnslocalhost'] === true) {
68-
$this->config["system"]["dnslocalhost"] = "";
69-
} elseif ($this->initial_data['dnslocalhost'] === false) {
70-
unset($this->config["system"]["dnslocalhost"]);
47+
}
48+
49+
private function __validate_dnslocalhost() {
50+
# Validate this field as needed for pfSense 2.4.x
51+
# TODO: remove this conditional once pfSense 2.4 nears EOL
52+
if (APITools\get_pfsense_version()["program"] < 250) {
53+
if ($this->initial_data['dnslocalhost'] === true) {
54+
$this->config["system"]["dnslocalhost"] = "";
55+
} elseif ($this->initial_data['dnslocalhost'] === false) {
56+
unset($this->config["system"]["dnslocalhost"]);
57+
}
7158
}
59+
# Validate this field as needed for pfSense 2.5+
60+
else {
61+
if ($this->initial_data['dnslocalhost'] === true) {
62+
unset($this->config["system"]["dnslocalhost"]);
63+
} elseif ($this->initial_data['dnslocalhost'] === false) {
64+
$this->config["system"]["dnslocalhost"] = "remote";
65+
}
66+
}
67+
}
68+
69+
private function __validate_dnsserver() {
70+
if (isset($this->initial_data['dnsserver'])) {
71+
# Use an internal API call to the APISystemDNSServerCreate model
72+
$dns_server_c = new APISystemDNSServerCreate();
73+
$dns_server_c->initial_data["dnsserver"] = $this->initial_data["dnsserver"];
74+
$dns_server_c->__validate_dnsserver();
75+
$this->errors = $this->errors + $dns_server_c->errors;
76+
$this->config["system"]["dnsserver"] = $dns_server_c->validated_data["dnsserver"];
77+
}
78+
}
79+
80+
public function apply_backend_changes() {
81+
# Update a slew of backend services
82+
system_resolvconf_generate();
83+
if (isset($this->config['dnsmasq']['enable'])) {
84+
services_dnsmasq_configure();
85+
} elseif (isset($this->config['unbound']['enable'])) {
86+
services_unbound_configure();
87+
}
88+
89+
# Reload DNS services and firewall filter
90+
send_event("service reload dns");
91+
filter_configure();
7292
}
7393
}

pfSense-pkg-API/files/usr/local/share/pfSense-pkg-API/manage.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ function restore() {
5858

5959
# Local Variables
6060
$api_conf = APITools\get_api_config();
61-
$backup_api_conf_json = file_get_contents("/usr/local/share/pfSense-pkg-API/backup.json");
62-
$backup_api_conf = json_decode($backup_api_conf_json, true);;
61+
62+
# Only retrieve file contents if it exists
63+
if (is_file("/usr/local/share/pfSense-pkg-API/backup.json")) {
64+
$backup_api_conf_json = file_get_contents("/usr/local/share/pfSense-pkg-API/backup.json");
65+
$backup_api_conf = json_decode($backup_api_conf_json, true);;
66+
}
6367

6468
# Restore our API configuration if the backup exists
6569
if (!empty($backup_api_conf_json)) {

0 commit comments

Comments
 (0)