Skip to content

Commit 0bbd067

Browse files
Fixed timeserver limit constraint in APIServicesNTPdTimeServerCreate.inc, changed APIServicesNTPdTimeServerDelete.inc to assign the default timeserver if all timeservers were removed, fixed interface assignment in APIServicesNTPdUpdate.inc, add nested timeservers call in APIServicesNTPdUpdate.inc to create timeservers using APIServicesNTPdTimeServerCreate.inc
1 parent ab0613e commit 0bbd067

4 files changed

Lines changed: 47 additions & 12 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ class APIServicesNTPd extends APIEndpoint {
2323
protected function get() {
2424
return (new APIServicesNTPdRead())->call();
2525
}
26+
27+
protected function put() {
28+
return (new APIServicesNTPdUpdate())->call();
29+
}
2630
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ class APIServicesNTPdTimeServerCreate extends APIModel {
3535
$this->config["system"]["timeservers"] = implode(" ", $this->timeservers);
3636
$this->write_config();
3737

38-
# Reconfigure NTP to apply the changes. Since values are stored in two places, simply return the initial data.
38+
# Reconfigure NTP to apply the changes then return the response
3939
system_ntp_configure();
40-
return APIResponse\get(0, $this->initial_data);
40+
$this->validated_data["timeservers"] = $this->timeservers;
41+
return APIResponse\get(0, $this->validated_data);
4142
}
4243

4344
public function validate_payload() {
@@ -50,7 +51,7 @@ class APIServicesNTPdTimeServerCreate extends APIModel {
5051

5152
private function __validate_timeserver_limit() {
5253
# Before adding a new timeserver, ensure we are not already at our maximum limit
53-
if (count($this->timeservers) > 10) {
54+
if (count($this->timeservers) >= 10) {
5455
$this->errors[] = APIResponse\get(2050);
5556
}
5657
}

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class APIServicesNTPdTimeServerDelete extends APIModel {
3737

3838
# Reconfigure NTP to apply the changes. Since values are stored in two places, simply return the initial data.
3939
system_ntp_configure();
40-
return APIResponse\get(0, $this->initial_data);
40+
$this->validated_data["timeservers"] = $this->timeservers;
41+
return APIResponse\get(0, $this->validated_data);
4142
}
4243

4344
public function validate_payload() {
@@ -47,29 +48,35 @@ class APIServicesNTPdTimeServerDelete extends APIModel {
4748
private function __validate_timeserver() {
4849
# Before deleting the timeserver, ensure it exists
4950
if (in_array($this->initial_data["timeserver"], $this->timeservers)) {
50-
# Remove the timeserver from our system timeservers
51+
# Remove the timeserver from our system timeservers. Reindex the array afterwards.
5152
unset($this->timeservers[array_search($this->initial_data["timeserver"], $this->timeservers)]);
53+
$this->timeservers = array_values($this->timeservers);
5254

53-
# Remove the timeserver from the ntpd ispool
55+
# Remove the timeserver from the ntpd ispool if found
5456
$ispool_arr = explode(" ", $this->validated_data["ispool"]);
5557
if (in_array($this->initial_data["timeserver"], $ispool_arr)) {
5658
unset($ispool_arr[array_search($this->initial_data["timeserver"], $ispool_arr)]);
5759
$this->validated_data["ispool"] = implode(" ", $ispool_arr);
5860
}
5961

60-
# Remove the timeserver from the ntpd noselect
62+
# Remove the timeserver from the ntpd noselect if found
6163
$noselect_arr = explode(" ", $this->validated_data["noselect"]);
6264
if (in_array($this->initial_data["timeserver"], $noselect_arr)) {
6365
unset($noselect_arr[array_search($this->initial_data["timeserver"], $noselect_arr)]);
6466
$this->validated_data["noselect"] = implode(" ", $noselect_arr);
6567
}
6668

67-
# Remove the timeserver from the ntpd prefer
69+
# Remove the timeserver from the ntpd prefer if found
6870
$prefer_arr = explode(" ", $this->validated_data["prefer"]);
6971
if (in_array($this->initial_data["timeserver"], $prefer_arr)) {
7072
unset($prefer_arr[array_search($this->initial_data["timeserver"], $prefer_arr)]);
7173
$this->validated_data["prefer"] = implode(" ", $prefer_arr);
7274
}
75+
76+
# If the last timeserver was removed, assert the default to prevent killing the system time
77+
if (empty($this->timeservers)) {
78+
$this->timeservers[] = "pool.ntp.org";
79+
}
7380
}
7481
# Otherwise, return error if the request name server does not exist
7582
else {

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ class APIServicesNTPdUpdate extends APIModel {
2727
}
2828

2929
public function action() {
30-
# Save our updated configuration and apply the changes
30+
# Save our updated configuration
3131
$this->config["ntpd"] = $this->validated_data;
3232
$this->write_config();
33+
34+
# Apply our change and return the response. Include all configured timeservers from system > timeservers as it
35+
# is need to provide the full context of the configuration
3336
system_ntp_configure();
37+
$this->validated_data["timeservers"] = explode(" ", $this->config["system"]["timeservers"]);;
3438
return APIResponse\get(0, $this->validated_data);
3539
}
3640

@@ -57,15 +61,34 @@ class APIServicesNTPdUpdate extends APIModel {
5761
$if = (APITools\get_pfsense_if_id($if)) ? APITools\get_pfsense_if_id($if) : $if;
5862

5963
# Check that this interface exists
60-
if (array_key_exists($if, $this->__get_interfaces())) {
64+
if (in_array($if, $this->__get_interfaces())) {
6165
$this->validated_data["interface"][] = $if;
6266
} else {
6367
$this->errors[] = APIResponse\get(2047);
6468
}
6569
}
6670

6771
# Convert value to internal XML value
68-
$this->validated_data["allowed_interfaces"] = implode(",", $this->validated_data["allowed_interfaces"]);
72+
$this->validated_data["interface"] = implode(",", $this->validated_data["interface"]);
73+
}
74+
}
75+
76+
private function __validate_timeservers() {
77+
# Check for our optional 'timeservers' payload value
78+
if (isset($this->initial_data["timeservers"])) {
79+
# Loop through each requested timeserver and ensure it is valid
80+
foreach ($this->initial_data["timeservers"] as $timeserver) {
81+
# Use the APIServicesNTPdTimeServerCreate model to validate each requested time server. This essentially
82+
# creates a nested/internal API call to validate/create NTPd timeservers
83+
$cts = new APIServicesNTPdTimeServerCreate();
84+
$cts->initial_data = $timeserver;
85+
$cts->validate_payload();
86+
87+
# If errors were encountered while validating the payload, save those errors to this models error field
88+
if (!empty($cts->errors)) {
89+
$this->errors = $this->errors + $cts->errors;
90+
}
91+
}
6992
}
7093
}
7194

@@ -146,7 +169,7 @@ class APIServicesNTPdUpdate extends APIModel {
146169
private function __get_interfaces() {
147170
# Local variables
148171
$if_raw = get_configured_interface_with_descr();
149-
$if_list = [];
172+
$if_list = ["lo0"];
150173

151174
# Loop through each configured interface but only capture interfaces with valid IP addresses
152175
foreach ($if_raw as $if => $if_descr) {

0 commit comments

Comments
 (0)