Skip to content

Commit d8c8e10

Browse files
- Added basic API call to read and configured system DNS servers to fix bug hindering development
1 parent 115dacc commit d8c8e10

6 files changed

Lines changed: 145 additions & 0 deletions

File tree

pfSense-pkg-API/Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ do-install:
135135
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/system/hostname/modify
136136
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/system/hostname/modify/index.php \
137137
${STAGEDIR}${PREFIX}/www/api/v1/system/hostname/modify
138+
# DNS base
139+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/system/dns
140+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/system/dns/index.php \
141+
${STAGEDIR}${PREFIX}/www/api/v1/system/dns
142+
# DNS modify
143+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/system/dns/modify
144+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/system/dns/modify/index.php \
145+
${STAGEDIR}${PREFIX}/www/api/v1/system/dns/modify
138146
# Certificates base
139147
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/system/certificates
140148
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/system/certificates/index.php \

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,79 @@ function api_system_hostname() {
22022202
}
22032203
}
22042204

2205+
function api_system_dns_modify() {
2206+
# VARIABLES
2207+
global $err_lib, $config, $api_resp, $client_params, $client_id;
2208+
$read_only_action = false; // Set whether this action requires read only access
2209+
$req_privs = array("page-all", "page-system"); // Array of privs allowed
2210+
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
2211+
$dns_ent = [];
2212+
# RUN TIME
2213+
// Check that client is authenticated and authorized
2214+
if (api_authorized($req_privs, $read_only_action)) {
2215+
// Check that our HTTP method is POST (UPDATE)
2216+
if ($http_method === 'POST') {
2217+
if (isset($client_params['dnsserver'])) {
2218+
$dns_servers = $client_params['dnsserver'];
2219+
// If value is not an array, convert it
2220+
if (!is_array($dns_servers)) {
2221+
$dns_servers = array($dns_servers);
2222+
}
2223+
// Loop through our DNS servers and check that entry is valid
2224+
foreach ($dns_servers as $ds) {
2225+
// Check if our DNS server is valid
2226+
if (!is_ipaddrv4($ds) and !is_ipaddrv6($ds)) {
2227+
$api_resp = array("status" => "bad request", "code" => 400, "return" => 1007);
2228+
$api_resp["message"] = $err_lib[$api_resp["return"]];
2229+
return $api_resp;
2230+
}
2231+
}
2232+
// Add our system DNS values to config and track the change
2233+
$config["system"]["dnsserver"] = $dns_servers;
2234+
$dns_ent["dnsserver"] = $dns_servers;
2235+
}
2236+
if ($client_params['dnsallowoverride'] === true) {
2237+
$config["system"]["dnsallowoverride"] = "";
2238+
$dns_ent["dnsallowoverride"] = $client_params['dnsallowoverride'];
2239+
} elseif ($client_params['dnsallowoverride'] === false) {
2240+
$dns_ent["dnsallowoverride"] = $client_params['dnsallowoverride'];
2241+
unset($config["system"]["dnsallowoverride"]);
2242+
}
2243+
if ($client_params['dnslocalhost'] === true) {
2244+
$config["system"]["dnslocalhost"] = "";
2245+
$dns_ent["dnslocalhost"] = $client_params['dnslocalhost'];
2246+
} elseif ($client_params['dnslocalhost'] === false) {
2247+
$dns_ent["dnslocalhost"] = $client_params['dnslocalhost'];
2248+
unset($config["system"]["dnslocalhost"]);
2249+
}
2250+
// Write our new hostname
2251+
$_SESSION["Username"] = $client_id; // Save our CLIENT ID to session data for logging
2252+
$change_note = " Modified system DNS servers via API"; // Add a change note
2253+
write_config(sprintf(gettext($change_note))); // Apply our configuration change
2254+
// Update a slew of backend services
2255+
system_resolvconf_generate();
2256+
if (isset($config['dnsmasq']['enable'])) {
2257+
services_dnsmasq_configure();
2258+
} elseif (isset($config['unbound']['enable'])) {
2259+
services_unbound_configure();
2260+
}
2261+
send_event("service reload dns");
2262+
filter_configure();
2263+
// Print our JSON response
2264+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
2265+
$api_resp["message"] = "Successfully modified system DNS servers";
2266+
$api_resp["data"] = $dns_ent;
2267+
return $api_resp;
2268+
} else {
2269+
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2);
2270+
$api_resp["message"] = $err_lib[$api_resp["return"]];
2271+
return $api_resp;
2272+
}
2273+
} else {
2274+
return $api_resp;
2275+
}
2276+
}
2277+
22052278
function api_system_hostname_modify() {
22062279
# VARIABLES
22072280
global $err_lib, $config, $api_resp, $client_params, $client_id;
@@ -2268,6 +2341,44 @@ function api_system_hostname_modify() {
22682341
}
22692342
}
22702343

2344+
function api_system_dns() {
2345+
# VARIABLES
2346+
global $err_lib, $config, $api_resp, $client_params;
2347+
$read_only_action = true; // Set whether this action requires read only access
2348+
$req_privs = array("page-all", "page-system"); // Allowed privs
2349+
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
2350+
$dns_array = array(); // Init our return array
2351+
# RUN TIME
2352+
// Check that client is authenticated and authorized
2353+
if (api_authorized($req_privs, $read_only_action)) {
2354+
// Check that our HTTP method is GET (READ)
2355+
if ($http_method === 'GET') {
2356+
// Check that we have a configuration
2357+
if (!empty($config["system"])) {
2358+
$dns_keys = ["dnsserver", "dnsallowoverride", "dnslocalhost"];
2359+
foreach ($config["system"] as $key => $sv) {
2360+
if (in_array($key, $dns_keys)) {
2361+
$dns_array[$key] = $sv;
2362+
}
2363+
}
2364+
}
2365+
if (isset($client_params['search'])) {
2366+
$search = $client_params['search'];
2367+
$dns_array = api_extended_search($dns_array, $search);
2368+
}
2369+
// Print our JSON response
2370+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0, "message" => "", "data" => $dns_array);
2371+
return $api_resp;
2372+
} else {
2373+
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2);
2374+
$api_resp["message"] = $err_lib[$api_resp["return"]];
2375+
return $api_resp;
2376+
}
2377+
} else {
2378+
return $api_resp;
2379+
}
2380+
}
2381+
22712382
function api_system_certificates() {
22722383
# VARIABLES
22732384
global $err_lib, $config, $api_resp, $client_params;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function api_error_lib() {
2020
1004 => "Invalid system certificate key",
2121
1005 => "System certificate in use",
2222
1006 => "ARP IP required",
23+
1007 => "Invalid system DNS IP address",
2324
// 2000-2999 reserved for /services API calls
2425
2000 => "Invalid sshd enable value",
2526
2001 => "Invalid sshd key only mode",
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_system_dns();
8+
http_response_code($resp["code"]);
9+
echo json_encode($resp) . PHP_EOL;
10+
exit();
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_system_dns_modify();
8+
http_response_code($resp["code"]);
9+
echo json_encode($resp) . PHP_EOL;
10+
exit();

pfSense-pkg-API/pkg-plist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
/usr/local/www/api/v1/system/hostname/index.php
6363
/usr/local/www/api/v1/system/hostname/modify/index.php
6464

65+
@dir /usr/local/www/api/v1/system/dns
66+
@dir /usr/local/www/api/v1/system/dns/modify
67+
/usr/local/www/api/v1/system/dns/index.php
68+
/usr/local/www/api/v1/system/dns/modify/index.php
69+
6570
@dir /usr/local/www/api/v1/system/certificates
6671
@dir /usr/local/www/api/v1/system/certificates/add
6772
@dir /usr/local/www/api/v1/system/certificates/delete

0 commit comments

Comments
 (0)