Skip to content

Commit dda17c2

Browse files
Added endpoints for /firewall/nat/portforwards/add/ and /firewall/nat/portforwards/delete/, added UI/config control to only allow read access from API, fixed permissions issue where read only permissions allowed services to start and stop
1 parent 2b51600 commit dda17c2

9 files changed

Lines changed: 524 additions & 47 deletions

File tree

pfSense-pkg-API/Makefile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,22 @@ do-install:
137137

138138
# FIREWALL API ENPOINTS
139139
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/firewall
140+
# NAT base
141+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat
142+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/firewall/nat/index.php \
143+
${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat
144+
# NAT port forwards base
145+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards
146+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/index.php \
147+
${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards
148+
# NAT port forwards add
149+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/add
150+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/add/index.php \
151+
${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/add
152+
# NAT port forwards add
153+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/delete
154+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/delete/index.php \
155+
${STAGEDIR}${PREFIX}/www/api/v1/firewall/nat/portforwards/delete
140156
# Virtual IP base
141157
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/firewall/virtualips
142158
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/firewall/virtualips/index.php \
@@ -312,4 +328,4 @@ do-install:
312328
@${REINPLACE_CMD} -i '' -e "s|%%PKGVERSION%%|${PKGVERSION}|" \
313329
${STAGEDIR}${DATADIR}/info.xml
314330

315-
.include <bsd.port.mk>
331+
.include <bsd.port.mk>

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

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,21 @@ function api_authorized($req_privs, $read_only=false) {
169169
if (api_authenticate() === true) {
170170
$client_config =& getUserEntry($client_id);;
171171
$client_privs = get_user_privileges($client_config);
172-
// Loop through each of our req privs and ensure the client has them, also check if access is read only
173-
foreach ($req_privs as &$priv) {
174-
// Check if action is not read only
175-
if ($read_only === false) {
176-
if (array_diff($read_priv, $client_privs) and in_array($priv, $client_privs, true)) {
177-
$authorized = true;
178-
break;
179-
}
180-
} else {
181-
if (in_array($priv, $client_privs)) {
182-
$authorized = true;
183-
break;
172+
// Check if API is in read-only mode
173+
if (is_api_read_only() === $read_only or $read_only) {
174+
// Loop through each of our req privs and ensure the client has them, also check if access is read only
175+
foreach ($req_privs as &$priv) {
176+
// Check if action is not read only
177+
if ($read_only === false) {
178+
if (array_diff($read_priv, $client_privs) and in_array($priv, $client_privs, true)) {
179+
$authorized = true;
180+
break;
181+
}
182+
} else {
183+
if (in_array($priv, $client_privs)) {
184+
$authorized = true;
185+
break;
186+
}
184187
}
185188
}
186189
}
@@ -231,6 +234,17 @@ function api_enabled() {
231234
}
232235
}
233236

237+
// Check if the API is in read-only mode
238+
function is_api_read_only() {
239+
// Local variables
240+
$api_config = get_api_configuration()[1]; // Save our current API config
241+
if (array_key_exists("readonly", $api_config)) {
242+
return true;
243+
} else {
244+
return false;
245+
}
246+
}
247+
234248
// Check if server IP is allowed to answer API calls. Redirects to login if not
235249
function api_whitelist_check() {
236250
global $config;
@@ -594,6 +608,33 @@ function sort_firewall_rules($mode=null, $data=null) {
594608
$config["filter"]["rule"] = $master_arr;
595609
}
596610

611+
// Sorts nat rules by specified criteria and reloads the filter
612+
function sort_nat_rules($mode=null, $data=null) {
613+
// Variables
614+
global $config;
615+
$sort_arr = [];
616+
$master_arr = [];
617+
foreach ($config["nat"]["rule"] as $idx => $fre) {
618+
$curr_iface = $fre["interface"]; // Save our current entries interface
619+
// Create our interface array if does not exist
620+
if (!isset($sort_arr[$curr_iface])) {
621+
$sort_arr[$curr_iface] = [];
622+
}
623+
// Check if user requested this rule to be placed at the top of array
624+
if ($mode === "top" and $idx === $data) {
625+
array_unshift($sort_arr[$curr_iface], $fre);
626+
} else {
627+
$sort_arr[$curr_iface][] = $fre;
628+
}
629+
}
630+
foreach ($sort_arr as $if) {
631+
foreach ($if as $rule) {
632+
$master_arr[] = $rule;
633+
}
634+
}
635+
$config["nat"]["rule"] = $master_arr;
636+
}
637+
597638
// Checks if inputted routing gateway exists
598639
function is_gateway($gw) {
599640
// Local variables

0 commit comments

Comments
 (0)