Skip to content

Commit f188f36

Browse files
Merge pull request #92 from jaredhendrickson13/v116_fixes
v1.1.6 Fixes & Features
2 parents e1299a8 + 2708f56 commit f188f36

9 files changed

Lines changed: 173 additions & 110 deletions

File tree

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,8 @@ class APIFirewallNATPortForwardCreate extends APIModel {
7979
private function __validate_target() {
8080
# Require client to pass in an internal target for the port forward
8181
if (isset($this->initial_data['target'])) {
82-
# Require the target to either be a valid IPv4 or IPv6 address
83-
if (is_ipaddrv4($this->initial_data['target'])) {
84-
$this->validated_data["target"] = $this->initial_data['target'];
85-
} elseif (alias_in_use($this->initial_data['target'])) {
82+
# Require the target to either be a valid IPv4 or alias
83+
if (in_array(is_ipaddroralias($this->initial_data['target']), [4, true])) {
8684
$this->validated_data["target"] = $this->initial_data['target'];
8785
} else {
8886
$this->errors[] = APIResponse\get(4009);
@@ -98,7 +96,7 @@ class APIFirewallNATPortForwardCreate extends APIModel {
9896
# Require client to pass in a local port to forward to the target
9997
if (isset($this->initial_data['local-port'])) {
10098
# Require the port to be a valid TCP/UDP port or range
101-
if (is_port_or_range(strval($this->initial_data['local-port']))) {
99+
if (is_port_or_range_or_alias(strval($this->initial_data['local-port']))) {
102100
$this->validated_data["local-port"] = $this->initial_data['local-port'];
103101
} else {
104102
$this->errors[] = APIResponse\get(4010);
@@ -142,7 +140,7 @@ class APIFirewallNATPortForwardCreate extends APIModel {
142140
if ($this->port_required) {
143141
$this->initial_data['srcport'] = str_replace("-", ":", $this->initial_data['srcport']);
144142
# Require port to be a valid port or range, or be any
145-
if (!is_port_or_range($this->initial_data['srcport']) and $this->initial_data['srcport'] !== "any") {
143+
if (!is_port_or_range_or_alias($this->initial_data['srcport']) and $this->initial_data['srcport'] !== "any") {
146144
$this->errors[] = APIResponse\get(4013);
147145
}
148146
# If our value is not any, replace the port range character with a - and save the value
@@ -157,7 +155,7 @@ class APIFirewallNATPortForwardCreate extends APIModel {
157155
if ($this->port_required) {
158156
$this->initial_data['dstport'] = str_replace("-", ":", $this->initial_data['dstport']);
159157
# Require port to be a valid port or range, or be any
160-
if (!is_port_or_range($this->initial_data['dstport']) and $this->initial_data['dstport'] !== "any") {
158+
if (!is_port_or_range_or_alias($this->initial_data['dstport']) and $this->initial_data['dstport'] !== "any") {
161159
$this->errors[] = APIResponse\get(4014);
162160
}
163161
# If our value is not any, replace the port range character with a - and save the value

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,8 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
101101
private function __validate_target() {
102102
# Optionally allow client to update internal target for the port forward
103103
if (isset($this->initial_data['target'])) {
104-
# Require the target to either be a valid IPv4 or IPv6 address
105-
if (is_ipaddrv4($this->initial_data['target'])) {
106-
$this->validated_data["target"] = $this->initial_data['target'];
107-
} elseif (alias_in_use($this->initial_data['target'])) {
104+
# Require the target to either be a valid IPv4 or alias
105+
if (in_array(is_ipaddroralias($this->initial_data['target']), [4, true])) {
108106
$this->validated_data["target"] = $this->initial_data['target'];
109107
} else {
110108
$this->errors[] = APIResponse\get(4009);
@@ -118,7 +116,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
118116
# Require client to pass in a local port to forward to the target
119117
if (isset($this->initial_data['local-port'])) {
120118
# Require the port to be a valid TCP/UDP port or range
121-
if (is_port_or_range(strval($this->initial_data['local-port']))) {
119+
if (is_port_or_range_or_alias(strval($this->initial_data['local-port']))) {
122120
$this->validated_data["local-port"] = $this->initial_data['local-port'];
123121
} else {
124122
$this->errors[] = APIResponse\get(4010);
@@ -173,7 +171,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
173171
if ($this->port_required or (isset($this->initial_data['srcport']) and $this->port_protocol)) {
174172
$this->initial_data['srcport'] = str_replace("-", ":", $this->initial_data['srcport']);
175173
# Require port to be a valid port or range, or be any
176-
if (!is_port_or_range($this->initial_data['srcport']) and $this->initial_data['srcport'] !== "any") {
174+
if (!is_port_or_range_or_alias($this->initial_data['srcport']) and $this->initial_data['srcport'] !== "any") {
177175
$this->errors[] = APIResponse\get(4013);
178176
}
179177
# If our value is not any, replace the port range character with a - and save the value
@@ -188,7 +186,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
188186
if ($this->port_required or (isset($this->initial_data['dstport']) and $this->port_protocol)) {
189187
$this->initial_data['dstport'] = str_replace("-", ":", $this->initial_data['dstport']);
190188
# Require port to be a valid port or range, or be any
191-
if (!is_port_or_range($this->initial_data['dstport']) and $this->initial_data['dstport'] !== "any") {
189+
if (!is_port_or_range_or_alias($this->initial_data['dstport']) and $this->initial_data['dstport'] !== "any") {
192190
$this->errors[] = APIResponse\get(4014);
193191
}
194192
# If our value is not any, replace the port range character with a - and save the value

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: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,28 @@ class APISystemDNSServerCreate extends APIModel {
2727
}
2828

2929
public function action() {
30+
# Write changes to config and apply backend changes
3031
$this->config["system"]["dnsserver"] = $this->validated_data["dnsserver"];
31-
$this->write_config(); // Apply our configuration change
32+
$this->write_config();
33+
$this->apply_backend_changes();
3234

33-
// Update a slew of backend services
34-
system_resolvconf_generate();
35-
if (isset($this->config['dnsmasq']['enable'])) {
36-
services_dnsmasq_configure();
37-
} elseif (isset($this->config['unbound']['enable'])) {
38-
services_unbound_configure();
39-
}
40-
41-
// Reload DNS services and firewall filter
42-
send_event("service reload dns");
43-
filter_configure();
4435
return APIResponse\get(0, $this->validated_data);
4536
}
4637

4738
public function validate_payload() {
48-
if (isset($this->initial_data['dnsserver'])) {
49-
$this->validated_data["dnsserver"] = $this->config["system"]["dnsserver"];
39+
$this->validated_data["dnsserver"] = $this->config["system"]["dnsserver"];
40+
$this->__validate_dnsserver();
41+
}
5042

43+
public function __validate_dnsserver() {
44+
# Validate the optional `dnsserver` payload value
45+
if (isset($this->initial_data['dnsserver'])) {
5146
# If values are not an array, convert it
5247
if (!is_array($this->initial_data["dnsserver"])) {
5348
$this->initial_data["dnsserver"] = array($this->initial_data["dnsserver"]);
5449
}
5550
if (!is_array($this->validated_data["dnsserver"])) {
5651
$this->validated_data["dnsserver"] = [$this->validated_data["dnsserver"]];
57-
5852
}
5953

6054
# Loop through each requested DNS server and ensure it is valid. Add this to our existing servers.
@@ -67,7 +61,25 @@ class APISystemDNSServerCreate extends APIModel {
6761
$this->validated_data["dnsserver"][] = $ds;
6862
}
6963
}
70-
$this->validated_data = array_filter($this->validated_data);
64+
65+
# Ensure array values are unique, reindexed and purge duplicate items
66+
$this->validated_data["dnsserver"] = array_filter($this->validated_data["dnsserver"]);
67+
$this->validated_data["dnsserver"] = array_unique($this->validated_data["dnsserver"]);
68+
$this->validated_data["dnsserver"] = array_values($this->validated_data["dnsserver"]);
69+
}
70+
}
71+
72+
public function apply_backend_changes() {
73+
# Update a slew of backend services
74+
system_resolvconf_generate();
75+
if (isset($this->config['dnsmasq']['enable'])) {
76+
services_dnsmasq_configure();
77+
} elseif (isset($this->config['unbound']['enable'])) {
78+
services_unbound_configure();
7179
}
80+
81+
# Reload DNS services and firewall filter
82+
send_event("service reload dns");
83+
filter_configure();
7284
}
7385
}

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
}

0 commit comments

Comments
 (0)