Skip to content

Commit 53203f0

Browse files
Add start, stop, and restart endpoints for all services (/services/start, /services/stop, /services/restart), changed format of individual start, stop, restart service enpoints to self identify the service and action being requested. Updated pkg-plist and Makefile
1 parent a0fd778 commit 53203f0

23 files changed

Lines changed: 1125 additions & 61 deletions

File tree

pfSense-pkg-API/Makefile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ do-install:
189189
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services
190190
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/index.php \
191191
${STAGEDIR}${PREFIX}/www/api/v1/services
192+
# Base start
193+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/start
194+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/start/index.php \
195+
${STAGEDIR}${PREFIX}/www/api/v1/services/start
196+
# Base stop
197+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/stop
198+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/stop/index.php \
199+
${STAGEDIR}${PREFIX}/www/api/v1/services/stop
200+
# Base restart
201+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/restart
202+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/restart/index.php \
203+
${STAGEDIR}${PREFIX}/www/api/v1/services/restart
192204
# DHCPD base
193205
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/dhcpd
194206
# DHCPD start
@@ -203,6 +215,34 @@ do-install:
203215
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/dhcpd/restart
204216
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/dhcpd/restart/index.php \
205217
${STAGEDIR}${PREFIX}/www/api/v1/services/dhcpd/restart
218+
# DPINGER base
219+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger
220+
# DPINGER start
221+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger/start
222+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/dpinger/start/index.php \
223+
${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger/start
224+
# DPINGER stop
225+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger/stop
226+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/dpinger/stop/index.php \
227+
${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger/stop
228+
# DPINGER restart
229+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger/restart
230+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/dpinger/restart/index.php \
231+
${STAGEDIR}${PREFIX}/www/api/v1/services/dpinger/restart
232+
# NTPD base
233+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd
234+
# NTPD start
235+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd/start
236+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/ntpd/start/index.php \
237+
${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd/start
238+
# NTPD stop
239+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd/stop
240+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/ntpd/stop/index.php \
241+
${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd/stop
242+
# NTPD restart
243+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd/restart
244+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/ntpd/restart/index.php \
245+
${STAGEDIR}${PREFIX}/www/api/v1/services/ntpd/restart
206246
# SSHD base
207247
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/sshd
208248
# SSHD start
@@ -217,6 +257,20 @@ do-install:
217257
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/sshd/restart
218258
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/sshd/restart/index.php \
219259
${STAGEDIR}${PREFIX}/www/api/v1/services/sshd/restart
260+
# SYSLOGD base
261+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd
262+
# SYSLOGD start
263+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd/start
264+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/syslogd/start/index.php \
265+
${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd/start
266+
# SYSLOGD stop
267+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd/stop
268+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/syslogd/stop/index.php \
269+
${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd/stop
270+
# SYSLOGD restart
271+
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd/restart
272+
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/syslogd/restart/index.php \
273+
${STAGEDIR}${PREFIX}/www/api/v1/services/syslogd/restart
220274
# Unbound base
221275
${MKDIR} ${STAGEDIR}${PREFIX}/www/api/v1/services/unbound
222276
${INSTALL_DATA} ${FILESDIR}${PREFIX}/www/api/v1/services/unbound/index.php \

pfSense-pkg-API/files/usr/local/www/api/v1/services/dhcpd/restart/index.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,45 @@
1414
// VARIABLES------------------------------------------------------------------------------------------------------------
1515
global $config, $api_resp, $client_params;
1616
$read_only_action = true; // Set whether this action requires read only access
17-
$req_privs = array("page-all", "page-services-dhcp"); // Array of privs allowed
17+
$req_privs = array("page-all", "page-status-services"); // Array of privs allowed
1818
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
19+
$uri_parse = explode("/", $_SERVER["REQUEST_URI"]); // Save our URI
20+
$service = $uri_parse[4]; // Save our service name
21+
$action = $uri_parse[5]; // Save our action (start, stop, restart)
1922

2023
// RUN TIME-------------------------------------------------------------------------------------------------------------
2124
// Check that client is authenticated and authorized
2225
if (api_authorized($req_privs, $read_only_action)) {
2326
// Check that our HTTP method is POST (UPDATE)
2427
if ($http_method === 'POST') {
25-
$stop_serv = service_control_restart("dhcpd", []); // Restart our service
26-
// Print our JSON response
27-
$api_resp = array("status" => "ok", "code" => 200, "return" => 0, "message" => "service dhcpd has been restarted");
28-
http_response_code(200);
29-
echo json_encode($api_resp) . PHP_EOL;
30-
die();
28+
# Check our aciton
29+
if ($action === "start") {
30+
$set_service = service_control_start($service, []); // Start our service
31+
$act_str = "started";
32+
} elseif ($action === "stop") {
33+
$set_service = service_control_stop($service, []); // Stop our service
34+
$act_str = "stopped";
35+
} elseif ($action === "restart") {
36+
$set_service = service_control_restart($service, []); // Restart our service
37+
$act_str = "restarted";
38+
} else {
39+
$act_failed = true;
40+
}
41+
// Check if our action succeeded or failed
42+
if (!$act_failed) {
43+
// Print our success response
44+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
45+
$api_resp["message"] = $service." has been ".$act_str;
46+
http_response_code(200);
47+
echo json_encode($api_resp) . PHP_EOL;
48+
die();
49+
} else {
50+
// Print our fail response
51+
$api_resp = array("status" => "server error", "code" => 500, "return" => 1, "message" => "process failed");
52+
http_response_code(500);
53+
echo json_encode($api_resp) . PHP_EOL;
54+
die();
55+
}
3156
} else {
3257
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2, "message" => "invalid http method");
3358
http_response_code(400);

pfSense-pkg-API/files/usr/local/www/api/v1/services/dhcpd/start/index.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,45 @@
1414
// VARIABLES------------------------------------------------------------------------------------------------------------
1515
global $config, $api_resp, $client_params;
1616
$read_only_action = true; // Set whether this action requires read only access
17-
$req_privs = array("page-all", "page-services-dhcp"); // Array of privs allowed
17+
$req_privs = array("page-all", "page-status-services"); // Array of privs allowed
1818
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
19+
$uri_parse = explode("/", $_SERVER["REQUEST_URI"]); // Save our URI
20+
$service = $uri_parse[4]; // Save our service name
21+
$action = $uri_parse[5]; // Save our action (start, stop, restart)
1922

2023
// RUN TIME-------------------------------------------------------------------------------------------------------------
2124
// Check that client is authenticated and authorized
2225
if (api_authorized($req_privs, $read_only_action)) {
2326
// Check that our HTTP method is POST (UPDATE)
2427
if ($http_method === 'POST') {
25-
$stop_serv = service_control_start("dhcpd", []); // Start our service
26-
// Print our JSON response
27-
$api_resp = array("status" => "ok", "code" => 200, "return" => 0, "message" => "service dhcpd has been started");
28-
http_response_code(200);
29-
echo json_encode($api_resp) . PHP_EOL;
30-
die();
28+
# Check our aciton
29+
if ($action === "start") {
30+
$set_service = service_control_start($service, []); // Start our service
31+
$act_str = "started";
32+
} elseif ($action === "stop") {
33+
$set_service = service_control_stop($service, []); // Stop our service
34+
$act_str = "stopped";
35+
} elseif ($action === "restart") {
36+
$set_service = service_control_restart($service, []); // Restart our service
37+
$act_str = "restarted";
38+
} else {
39+
$act_failed = true;
40+
}
41+
// Check if our action succeeded or failed
42+
if (!$act_failed) {
43+
// Print our success response
44+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
45+
$api_resp["message"] = $service." has been ".$act_str;
46+
http_response_code(200);
47+
echo json_encode($api_resp) . PHP_EOL;
48+
die();
49+
} else {
50+
// Print our fail response
51+
$api_resp = array("status" => "server error", "code" => 500, "return" => 1, "message" => "process failed");
52+
http_response_code(500);
53+
echo json_encode($api_resp) . PHP_EOL;
54+
die();
55+
}
3156
} else {
3257
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2, "message" => "invalid http method");
3358
http_response_code(400);

pfSense-pkg-API/files/usr/local/www/api/v1/services/dhcpd/stop/index.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,45 @@
1414
// VARIABLES------------------------------------------------------------------------------------------------------------
1515
global $config, $api_resp, $client_params;
1616
$read_only_action = true; // Set whether this action requires read only access
17-
$req_privs = array("page-all", "page-services-dhcp"); // Array of privs allowed
17+
$req_privs = array("page-all", "page-status-services"); // Array of privs allowed
1818
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
19+
$uri_parse = explode("/", $_SERVER["REQUEST_URI"]); // Save our URI
20+
$service = $uri_parse[4]; // Save our service name
21+
$action = $uri_parse[5]; // Save our action (start, stop, restart)
1922

2023
// RUN TIME-------------------------------------------------------------------------------------------------------------
2124
// Check that client is authenticated and authorized
2225
if (api_authorized($req_privs, $read_only_action)) {
2326
// Check that our HTTP method is POST (UPDATE)
2427
if ($http_method === 'POST') {
25-
$stop_serv = service_control_stop("dhcpd", []); // Stop our service
26-
// Print our JSON response
27-
$api_resp = array("status" => "ok", "code" => 200, "return" => 0, "message" => "service dhcpd has been stopped");
28-
http_response_code(200);
29-
echo json_encode($api_resp) . PHP_EOL;
30-
die();
28+
# Check our aciton
29+
if ($action === "start") {
30+
$set_service = service_control_start($service, []); // Start our service
31+
$act_str = "started";
32+
} elseif ($action === "stop") {
33+
$set_service = service_control_stop($service, []); // Stop our service
34+
$act_str = "stopped";
35+
} elseif ($action === "restart") {
36+
$set_service = service_control_restart($service, []); // Restart our service
37+
$act_str = "restarted";
38+
} else {
39+
$act_failed = true;
40+
}
41+
// Check if our action succeeded or failed
42+
if (!$act_failed) {
43+
// Print our success response
44+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
45+
$api_resp["message"] = $service." has been ".$act_str;
46+
http_response_code(200);
47+
echo json_encode($api_resp) . PHP_EOL;
48+
die();
49+
} else {
50+
// Print our fail response
51+
$api_resp = array("status" => "server error", "code" => 500, "return" => 1, "message" => "process failed");
52+
http_response_code(500);
53+
echo json_encode($api_resp) . PHP_EOL;
54+
die();
55+
}
3156
} else {
3257
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2, "message" => "invalid http method");
3358
http_response_code(400);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
// IMPORTS--------------------------------------------------------------------------------------------------------------
4+
require_once("config.inc");
5+
require_once("auth.inc");
6+
require_once("functions.inc");
7+
require_once("api.inc");
8+
9+
// HEADERS--------------------------------------------------------------------------------------------------------------
10+
api_runtime_allowed(); // Check that our configuration allows this API call to run first
11+
header('Content-Type: application/json');
12+
header("Referer: no-referrer");
13+
14+
// VARIABLES------------------------------------------------------------------------------------------------------------
15+
global $config, $api_resp, $client_params;
16+
$read_only_action = true; // Set whether this action requires read only access
17+
$req_privs = array("page-all", "page-status-services"); // Array of privs allowed
18+
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
19+
$uri_parse = explode("/", $_SERVER["REQUEST_URI"]); // Save our URI
20+
$service = $uri_parse[4]; // Save our service name
21+
$action = $uri_parse[5]; // Save our action (start, stop, restart)
22+
23+
// RUN TIME-------------------------------------------------------------------------------------------------------------
24+
// Check that client is authenticated and authorized
25+
if (api_authorized($req_privs, $read_only_action)) {
26+
// Check that our HTTP method is POST (UPDATE)
27+
if ($http_method === 'POST') {
28+
# Check our aciton
29+
if ($action === "start") {
30+
$set_service = service_control_start($service, []); // Start our service
31+
$act_str = "started";
32+
} elseif ($action === "stop") {
33+
$set_service = service_control_stop($service, []); // Stop our service
34+
$act_str = "stopped";
35+
} elseif ($action === "restart") {
36+
$set_service = service_control_restart($service, []); // Restart our service
37+
$act_str = "restarted";
38+
} else {
39+
$act_failed = true;
40+
}
41+
// Check if our action succeeded or failed
42+
if (!$act_failed) {
43+
// Print our success response
44+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
45+
$api_resp["message"] = $service." has been ".$act_str;
46+
http_response_code(200);
47+
echo json_encode($api_resp) . PHP_EOL;
48+
die();
49+
} else {
50+
// Print our fail response
51+
$api_resp = array("status" => "server error", "code" => 500, "return" => 1, "message" => "process failed");
52+
http_response_code(500);
53+
echo json_encode($api_resp) . PHP_EOL;
54+
die();
55+
}
56+
} else {
57+
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2, "message" => "invalid http method");
58+
http_response_code(400);
59+
echo json_encode($api_resp) . PHP_EOL;
60+
die();
61+
}
62+
} else {
63+
http_response_code(401);
64+
echo json_encode($api_resp) . PHP_EOL;
65+
die();
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
// IMPORTS--------------------------------------------------------------------------------------------------------------
4+
require_once("config.inc");
5+
require_once("auth.inc");
6+
require_once("functions.inc");
7+
require_once("api.inc");
8+
9+
// HEADERS--------------------------------------------------------------------------------------------------------------
10+
api_runtime_allowed(); // Check that our configuration allows this API call to run first
11+
header('Content-Type: application/json');
12+
header("Referer: no-referrer");
13+
14+
// VARIABLES------------------------------------------------------------------------------------------------------------
15+
global $config, $api_resp, $client_params;
16+
$read_only_action = true; // Set whether this action requires read only access
17+
$req_privs = array("page-all", "page-status-services"); // Array of privs allowed
18+
$http_method = $_SERVER['REQUEST_METHOD']; // Save our HTTP method
19+
$uri_parse = explode("/", $_SERVER["REQUEST_URI"]); // Save our URI
20+
$service = $uri_parse[4]; // Save our service name
21+
$action = $uri_parse[5]; // Save our action (start, stop, restart)
22+
23+
// RUN TIME-------------------------------------------------------------------------------------------------------------
24+
// Check that client is authenticated and authorized
25+
if (api_authorized($req_privs, $read_only_action)) {
26+
// Check that our HTTP method is POST (UPDATE)
27+
if ($http_method === 'POST') {
28+
# Check our aciton
29+
if ($action === "start") {
30+
$set_service = service_control_start($service, []); // Start our service
31+
$act_str = "started";
32+
} elseif ($action === "stop") {
33+
$set_service = service_control_stop($service, []); // Stop our service
34+
$act_str = "stopped";
35+
} elseif ($action === "restart") {
36+
$set_service = service_control_restart($service, []); // Restart our service
37+
$act_str = "restarted";
38+
} else {
39+
$act_failed = true;
40+
}
41+
// Check if our action succeeded or failed
42+
if (!$act_failed) {
43+
// Print our success response
44+
$api_resp = array("status" => "ok", "code" => 200, "return" => 0);
45+
$api_resp["message"] = $service." has been ".$act_str;
46+
http_response_code(200);
47+
echo json_encode($api_resp) . PHP_EOL;
48+
die();
49+
} else {
50+
// Print our fail response
51+
$api_resp = array("status" => "server error", "code" => 500, "return" => 1, "message" => "process failed");
52+
http_response_code(500);
53+
echo json_encode($api_resp) . PHP_EOL;
54+
die();
55+
}
56+
} else {
57+
$api_resp = array("status" => "bad request", "code" => 400, "return" => 2, "message" => "invalid http method");
58+
http_response_code(400);
59+
echo json_encode($api_resp) . PHP_EOL;
60+
die();
61+
}
62+
} else {
63+
http_response_code(401);
64+
echo json_encode($api_resp) . PHP_EOL;
65+
die();
66+
}

0 commit comments

Comments
 (0)