Skip to content

Commit 949d8ba

Browse files
author
Jared Hendrickson
committed
v0.0.2 commit includes optimization for /services/unbound/ calls, allowed configuration to be added without applying changes at the same time, added separate API call to apply unbound configuration
1 parent 84554e1 commit 949d8ba

8 files changed

Lines changed: 97 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
2+
*.DS_Store
23

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
22
# Introduction
3-
pfSense API is a fast, safe, full-fledged API based on REST architecture. This works by leveraging the same PHP functions and processes used by pfSense's webConfigurator into API endpoints to create, read, update and delete pfSense configurations. All API endpoints enforce input validation to prevent invalid configurations from being made. Configurations made via API are properly written to the master XML configuration and the correct backend configurations are made preventing the need for a reboot. All this results in the fastest, safest, and easiest way to automate pfSense!
3+
pfSense API is a fast, safe, full-fledged HTTP API. This works by leveraging the same PHP functions and processes used by pfSense's webConfigurator into API endpoints to create, read, update and delete pfSense configurations. All API endpoints enforce input validation to prevent invalid configurations from being made. Configurations made via API are properly written to the master XML configuration and the correct backend configurations are made preventing the need for a reboot. All this results in the fastest, safest, and easiest way to automate pfSense!
44

55
# Installation
66
To install pfSense API, simply run the following command from the pfSense shell:<br>
7-
`pkg add https://github.com/jaredhendrickson13/pfsense-api/releases/v0.0.1/pfSense-pkg-API-0.0_1.txz`<br>
7+
`pkg add https://github.com/jaredhendrickson13/pfsense-api/releases/v0.0.2/pfSense-2-4-pkg-API-0.0_2.txz`<br>
88

99
To uninstall, run the following command:<br>
1010
`pkg delete pfSense-pkg-API`<br>

pfSense-pkg-API/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
PORTNAME= pfSense-pkg-API
44
PORTVERSION= 0.0
5-
PORTREVISION= 1
5+
PORTREVISION= 2
66
CATEGORIES= sysutils
77
MASTER_SITES= # empty
88
DISTFILES= # empty
@@ -412,6 +412,10 @@ do-install:
412412
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/unbound/modify/hosts
413413
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/unbound/modify/hosts/index.php \
414414
${STAGEDIR}${PREFIX}/www/api/v1/services/unbound/modify/hosts
415+
# Unbound apply
416+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/unbound/apply
417+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/unbound/apply/index.php \
418+
${STAGEDIR}${PREFIX}/www/api/v1/services/unbound/apply
415419

416420
# INSTALL OUR PKG INFO
417421
${MKDIR} ${STAGEDIR}${DATADIR}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,19 @@ function get_arp_entry($search, $value) {
445445
return $arp_match;
446446
}
447447

448+
// Reload our unbound configuration, restart associated services and clear config locks
449+
function unbound_reload_config() {
450+
$reload_unbound = 0;
451+
$reload_unbound |= services_unbound_configure();
452+
// Check if application was successful
453+
if ($reload_unbound === 0) {
454+
system_resolvconf_generate(); // Update resolveconf
455+
system_dhcpleases_configure(); // Update DHCPD
456+
clear_subsystem_dirty("unbound");
457+
return true;
458+
}
459+
}
460+
448461
// Check if a DNS Resolver (Unbound) host override already exists
449462
function unbound_host_override_exists($hostname, $domain) {
450463
// Local variables

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

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5059,6 +5059,9 @@ function api_services_unbound_delete_hosts() {
50595059
if ($client_params['aliases'] === true) {
50605060
$a_mode = true;
50615061
}
5062+
if ($client_params['apply'] === true) {
5063+
$apply = $client_params['apply'];
5064+
}
50625065
// Determine criteria for deletion
50635066
if ($h_mode and !$d_mode and !$i_mode) {
50645067
$del_mode = "h";
@@ -5091,6 +5094,8 @@ function api_services_unbound_delete_hosts() {
50915094
echo var_dump($a_mode) . PHP_EOL;
50925095
echo "MODE:" . PHP_EOL;
50935096
echo var_dump($del_mode) . PHP_EOL;
5097+
echo "APPLY:" . PHP_EOL;
5098+
echo var_dump($apply) . PHP_EOL;
50945099
}
50955100
// Check that our configuration is a list and loop through each item, otherwise return ok resp
50965101
if (array_key_exists("hosts", $config["unbound"]) and is_array($config["unbound"]["hosts"])) {
@@ -5173,11 +5178,13 @@ function api_services_unbound_delete_hosts() {
51735178
usort($hosts_conf, "strcmp");
51745179
$config["unbound"]["hosts"] = $hosts_conf;
51755180
write_config(sprintf(gettext($change_note)));
5176-
$reload_unbound = 0;
5177-
$reload_unbound |= services_unbound_configure();
5178-
if ($reload_unbound == 0) {
5179-
system_resolvconf_generate(); // Update resolveconf
5180-
system_dhcpleases_configure(); // Update DHCPD
5181+
mark_subsystem_dirty("unbound");
5182+
# If user requests immediately application
5183+
if ($apply === true) {
5184+
$applied = unbound_reload_config();
5185+
}
5186+
// Return success if our function was successful
5187+
if ($applied === true or $apply !== true) {
51815188
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
51825189
$api_resp["message"] = "host override deleted";
51835190
$api_resp["data"] = $del_list;
@@ -5255,6 +5262,9 @@ function api_services_unbound_modify_hosts() {
52555262
if (isset($client_params['aliases'])) {
52565263
$aliases = $client_params['aliases'];
52575264
}
5265+
if ($client_params['apply'] === true) {
5266+
$apply = $client_params['apply'];
5267+
}
52585268
// Add debug data if requested
52595269
if (array_key_exists("debug", $client_params)) {
52605270
echo "HOSTNAME:" . PHP_EOL;
@@ -5273,6 +5283,8 @@ function api_services_unbound_modify_hosts() {
52735283
echo var_dump($descr) . PHP_EOL;
52745284
echo "ALIASES:" . PHP_EOL;
52755285
echo var_dump($aliases) . PHP_EOL;
5286+
echo "APPLY:" . PHP_EOL;
5287+
echo var_dump($aliases) . PHP_EOL;
52765288
}
52775289
// Validate our input against our exist configuration
52785290
if (unbound_host_override_exists($hostname, $domain) or $i_mode) {
@@ -5377,11 +5389,13 @@ function api_services_unbound_modify_hosts() {
53775389
usort($hosts_conf, "strcmp");
53785390
$config["unbound"]["hosts"] = $hosts_conf;
53795391
write_config(sprintf(gettext($change_note)));
5380-
$reload_unbound = 0;
5381-
$reload_unbound |= services_unbound_configure();
5382-
if ($reload_unbound == 0) {
5383-
system_resolvconf_generate(); // Update resolveconf
5384-
system_dhcpleases_configure(); // Update DHCPD
5392+
mark_subsystem_dirty("unbound");
5393+
# If user requests immediately application
5394+
if ($apply === true) {
5395+
$applied = unbound_reload_config();
5396+
}
5397+
// Return success if our function was successful
5398+
if ($applied === true or $apply !== true) {
53855399
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
53865400
$api_resp["message"] = "Successfully updated unbound host override";
53875401
$api_resp["data"] = $update_list;
@@ -5598,6 +5612,9 @@ function api_services_unbound_add_hosts() {
55985612
if (isset($client_params['aliases'])) {
55995613
$aliases = $client_params['aliases'];
56005614
}
5615+
if ($client_params['apply'] === true) {
5616+
$apply = $client_params['apply'];
5617+
}
56015618
// Add debug data if requested
56025619
if (array_key_exists("debug", $client_params)) {
56035620
echo "HOSTNAME:" . PHP_EOL;
@@ -5610,6 +5627,8 @@ function api_services_unbound_add_hosts() {
56105627
echo var_dump($descr) . PHP_EOL;
56115628
echo "ALIASES:" . PHP_EOL;
56125629
echo var_dump($aliases) . PHP_EOL;
5630+
echo "APPLY:" . PHP_EOL;
5631+
echo var_dump($apply) . PHP_EOL;
56135632
}
56145633
// Validate our input against our exist configuration
56155634
if (!unbound_host_override_exists($hostname, $domain)) {
@@ -5627,11 +5646,13 @@ function api_services_unbound_add_hosts() {
56275646
$config["unbound"]["hosts"][] = $host_ent;
56285647
usort($config["unbound"]["hosts"], "host_cmp");
56295648
write_config(sprintf(gettext($change_note)));
5630-
$reload_unbound = 0;
5631-
$reload_unbound |= services_unbound_configure();
5632-
if ($reload_unbound == 0) {
5633-
system_resolvconf_generate(); // Update resolveconf
5634-
system_dhcpleases_configure(); // Update DHCPD
5649+
mark_subsystem_dirty("unbound");
5650+
# If user requests immediately application
5651+
if ($apply === true) {
5652+
$applied = unbound_reload_config();
5653+
}
5654+
// Return success if our function was successful
5655+
if ($applied === true or $apply !== true) {
56355656
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
56365657
$api_resp["message"] = "Successfully added unbound host override";
56375658
$api_resp["data"] = $host_ent;
@@ -5662,6 +5683,33 @@ function api_services_unbound_add_hosts() {
56625683
}
56635684
}
56645685

5686+
function api_services_unbound_apply() {
5687+
# VARIABLES;
5688+
global $err_lib;
5689+
$read_only_action = false; // Set whether this action requires read only access
5690+
$req_privs = array("page-all", "page-services-dnsresolver-edithost"); // Array of privileges allowing this action
5691+
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
5692+
# RUN TIME
5693+
// Check that client is authenticated and authorized
5694+
if (api_authorized($req_privs, $read_only_action)) {
5695+
// Check that our HTTP method is POST (CREATE)
5696+
if ($http_method === 'POST') {
5697+
// Check if application was successful
5698+
if (unbound_reload_config() === true) {
5699+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
5700+
$api_resp["message"] = "Successfully applied unbound configuration";
5701+
$api_resp["data"] = "";
5702+
return $api_resp;
5703+
} else {
5704+
$api_resp = array("status" => "server error", "code" => 500, "return" => 1);
5705+
$api_resp["message"] = $err_lib[$api_resp["return"]];
5706+
return $api_resp;
5707+
}
5708+
}
5709+
}
5710+
}
5711+
5712+
56655713
function api_interfaces_vlans() {
56665714
# VARIABLES
56675715
global $err_lib, $g, $config, $argv, $userindex, $api_resp, $client_params;

pfSense-pkg-API/files/usr/local/share/pfSense-pkg-API/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<name>API</name>
55
<internal_name>api</internal_name>
66
<descr><![CDATA[A full API for pfSense]]></descr>
7-
<website>https://github.com/jaredhendrickson13</website>
7+
<website>pfsense-api.jaredhendrickson.com</website>
88
<category>System</category>
99
<version>%%PKGVERSION%%</version>
1010
<configurationfile>api.xml</configurationfile>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
# Copyright 2020 - Jared Hendrickson
3+
# IMPORTS
4+
require_once("apicalls.inc");
5+
6+
# RUN API CALL
7+
$resp = api_services_unbound_apply();
8+
http_response_code($resp["code"]);
9+
echo json_encode($resp) . PHP_EOL;
10+
exit();

pfSense-pkg-API/pkg-plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
@dir /usr/local/www/api/v1/services/unbound/start
182182
@dir /usr/local/www/api/v1/services/unbound/stop
183183
@dir /usr/local/www/api/v1/services/unbound/restart
184+
@dir /usr/local/www/api/v1/services/unbound/apply
184185
@dir /usr/local/www/api/v1/services/unbound/add/hosts
185186
@dir /usr/local/www/api/v1/services/unbound/delete/hosts
186187
@dir /usr/local/www/api/v1/services/unbound/modify/hosts
@@ -209,6 +210,7 @@
209210
/usr/local/www/api/v1/services/unbound/start/index.php
210211
/usr/local/www/api/v1/services/unbound/stop/index.php
211212
/usr/local/www/api/v1/services/unbound/restart/index.php
213+
/usr/local/www/api/v1/services/unbound/apply/index.php
212214
/usr/local/www/api/v1/services/unbound/add/hosts/index.php
213215
/usr/local/www/api/v1/services/unbound/delete/hosts/index.php
214216
/usr/local/www/api/v1/services/unbound/modify/hosts/index.php

0 commit comments

Comments
 (0)