Skip to content

Commit 12f0d9c

Browse files
Fixed bug that sometimes prevented port forwards ports from updating, added ability to set firewall rule destination to this firewall
1 parent b72a990 commit 12f0d9c

7 files changed

Lines changed: 24 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function get_pfsense_if_id($interface) {
348348
// Check if input is valid for rule source and destination
349349
function is_valid_rule_addr($addr, $direction) {
350350
// Variables
351-
$addr_types = array("any", "pppoe", "l2tp"); // Array of special src/dst types
351+
$addr_types = array("any", "pppoe", "l2tp", "(self)"); // Array of special src/dst types
352352
$ret_val = array("valid" => true, "data" => array());
353353
// Check if our source values are valid
354354
if (is_string($addr)) {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
2121
private $nat_reflection;
2222
private $updated_by_msg;
2323
private $port_required;
24+
private $port_protocol;
2425

2526
# Create our method constructor
2627
public function __construct() {
@@ -30,6 +31,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
3031
$this->protocols = ["tcp", "udp", "tcp/udp", "icmp", "esp", "ah", "gre", "ipv6", "igmp", "pim", "ospf"];
3132
$this->nat_reflection = ["enable", "disable", "purenat"];
3233
$this->port_required = false;
34+
$this->port_protocol = false;
3335
}
3436

3537
public function action() {
@@ -48,6 +50,11 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
4850
if (array_key_exists($this->initial_data["id"], $this->config["nat"]["rule"])) {
4951
$this->id = $this->initial_data["id"];
5052
$this->validated_data = $this->config["nat"]["rule"][$this->id];
53+
54+
# Check if current protocol is a port based protocol
55+
if (in_array($this->validated_data["protocol"], ["tcp", "udp", "tcp/udp"])) {
56+
$this->port_protocol = true;
57+
}
5158
} else {
5259
$this->errors[] = APIResponse\get(4016);
5360
}
@@ -74,11 +81,15 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
7481
if (isset($this->initial_data['protocol'])) {
7582
# Require protocol to be a known/supported protocol
7683
if (in_array($this->initial_data['protocol'], $this->protocols)) {
77-
# Only require ports if updating to port protocol from non-port protocol
84+
# Check if we are updating to a port based protocol
7885
if (in_array($this->initial_data["protocol"], ["tcp", "udp", "tcp/udp"])) {
86+
$this->port_protocol = true;
87+
# Only require ports if updating to port protocol from non-port protocol
7988
if (!in_array($this->validated_data["protocol"], ["tcp", "udp", "tcp/udp"])) {
8089
$this->port_required = true;
8190
}
91+
} else {
92+
$this->port_protocol = false;
8293
}
8394
$this->validated_data["protocol"] = $this->initial_data['protocol'];
8495
} else {
@@ -103,7 +114,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
103114

104115
private function __validate_local_port() {
105116
# Only require a local port if the protocol requires a port
106-
if ($this->port_required) {
117+
if ($this->port_required or (isset($this->initial_data['local-port']) and $this->port_protocol)) {
107118
# Require client to pass in a local port to forward to the target
108119
if (isset($this->initial_data['local-port'])) {
109120
# Require the port to be a valid TCP/UDP port or range
@@ -159,7 +170,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
159170

160171
private function __validate_srcport() {
161172
# Only require a source port value if our protocol requires ports
162-
if ($this->port_required) {
173+
if ($this->port_required or (isset($this->initial_data['srcport']) and $this->port_protocol)) {
163174
$this->initial_data['srcport'] = str_replace("-", ":", $this->initial_data['srcport']);
164175
# Require port to be a valid port or range, or be any
165176
if (!is_port_or_range($this->initial_data['srcport']) and $this->initial_data['srcport'] !== "any") {
@@ -174,7 +185,7 @@ class APIFirewallNATPortForwardUpdate extends APIModel {
174185

175186
private function __validate_dstport() {
176187
# Only require a destination port value if our protocol requires ports
177-
if ($this->port_required) {
188+
if ($this->port_required or (isset($this->initial_data['dstport']) and $this->port_protocol)) {
178189
$this->initial_data['dstport'] = str_replace("-", ":", $this->initial_data['dstport']);
179190
# Require port to be a valid port or range, or be any
180191
if (!is_port_or_range($this->initial_data['dstport']) and $this->initial_data['dstport'] !== "any") {

pfSense-pkg-API/files/pkg-deinstall.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ fi
1212

1313
# Restore overriden files to their original state
1414
/bin/mv /etc/inc/system.inc.original /etc/inc/system.inc
15+
echo "Restoring file overrides to their original state... done."

pfSense-pkg-API/files/pkg-install.in

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ fi
55

66
# Make this package known to pfSense
77
/usr/local/bin/php -f /etc/rc.packages %%PORTNAME%% ${2}
8-
echo "Creating backups of files to override...done."
8+
echo "Creating backups of files to override... done."
99

1010
# Backup original files before overriding
1111
/bin/cp /etc/inc/system.inc /etc/inc/system.inc.original
1212

1313
# Check the local systems version of pfSense before assigning file overrides
1414
PFSENSE_VERSION=$(/bin/cat /etc/version)
15-
echo "Checking pfSense version...done."
15+
echo "Checking pfSense version... done."
1616

1717
# Use the corresponding pfSense version's file overrides if they exist. Otherwise print warning and use default.
1818
if [ -d "/etc/inc/api/framework/overrides/${PFSENSE_VERSION}" ]
1919
then
2020
/bin/cp "/etc/inc/api/framework/overrides/${PFSENSE_VERSION}/system.inc" "/etc/inc/system.inc"
21-
echo "Installing file overrides for ${PFSENSE_VERSION}...done."
21+
echo "Installing file overrides for ${PFSENSE_VERSION}... done."
2222
else
2323
echo "WARNING: No overrides exist for ${PFSENSE_VERSION}, it may be unsupported. Using default overrides."
2424
/bin/cp "/etc/inc/api/framework/overrides/default/system.inc" "/etc/inc/system.inc"
25-
echo "Installing default file overrides...done."
25+
echo "Installing default file overrides... done."
2626
fi
2727

2828
# Setup the pfsense-api command line tool

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ function build_endpoints() {
3737

3838
# Print success output if file now exists, otherwise output error and exit on non-zero code
3939
if (!is_null($endpoint_obj->url) and is_file("/usr/local/www".$endpoint_obj->url."/index.php")) {
40-
echo "Builing ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"... done.".PHP_EOL;
40+
echo "Building ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"... done.".PHP_EOL;
4141
} else {
42-
echo "Builing ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"... failed.".PHP_EOL;
42+
echo "Building ".$endpoint_class." endpoint at URL \"".$endpoint_obj->url."\"... failed.".PHP_EOL;
4343
exit(1);
4444
}
4545
}

tests/test_api_v1_firewall_rule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class APIUnitTestFirewallRule(unit_test_framework.APIUnitTest):
4040
"protocol": "tcp/udp",
4141
"src": "172.16.77.125",
4242
"srcport": "8080-8081",
43-
"dst": "127.0.0.1",
43+
"dst": "(self)",
4444
"dstport": "2222-4444",
4545
"descr": "Updated Unit test",
4646
"gateway": "WAN_DHCP",
6.76 KB
Binary file not shown.

0 commit comments

Comments
 (0)