1
|
/*
|
2
|
* svc
|
3
|
*
|
4
|
* part of pfSense (https://www.pfsense.org)
|
5
|
* Copyright (c) 2016 Electric Sheep Fencing
|
6
|
* Copyright (c) 2016-2024 Rubicon Communications, LLC (Netgate)
|
7
|
* All rights reserved.
|
8
|
*
|
9
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
10
|
* you may not use this file except in compliance with the License.
|
11
|
* You may obtain a copy of the License at
|
12
|
*
|
13
|
* http://www.apache.org/licenses/LICENSE-2.0
|
14
|
*
|
15
|
* Unless required by applicable law or agreed to in writing, software
|
16
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
17
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18
|
* See the License for the specific language governing permissions and
|
19
|
* limitations under the License.
|
20
|
*/
|
21
|
|
22
|
require_once("config.inc");
|
23
|
require_once("util.inc");
|
24
|
require_once("service-utils.inc");
|
25
|
|
26
|
function usage() {
|
27
|
echo "Usage: playback svc <action> <service name> [service-specific options]\n\n";
|
28
|
echo "Examples:\n";
|
29
|
echo "playback svc stop dhcpd\n";
|
30
|
echo "playback svc restart openvpn client 2\n";
|
31
|
echo "playback svc stop captiveportal zone1\n";
|
32
|
echo "\n";
|
33
|
}
|
34
|
|
35
|
global $g, $config, $argv, $command_split;
|
36
|
|
37
|
if (is_array($command_split)) {
|
38
|
$args = array_slice($command_split, 2);
|
39
|
} else {
|
40
|
$args = array_slice($argv, 3);
|
41
|
}
|
42
|
|
43
|
if (empty($args[0])) {
|
44
|
usage();
|
45
|
}
|
46
|
|
47
|
$extras = array();
|
48
|
|
49
|
// start, stop, restart
|
50
|
$action = $args[0];
|
51
|
|
52
|
// dhcpd, openvpn, etc.
|
53
|
$svc_name = $args[1];
|
54
|
|
55
|
// Handle service-specific options.
|
56
|
switch ($svc_name) {
|
57
|
case "openvpn":
|
58
|
if (in_array($args[2], array("server", "client"))) {
|
59
|
$extras['vpnmode'] = $args[2];
|
60
|
} else {
|
61
|
echo "Invalid OpenVPN mode (server, client)\n";
|
62
|
return;
|
63
|
}
|
64
|
if (is_numeric($args[3])) {
|
65
|
$extras['id'] = $args[3];
|
66
|
} else {
|
67
|
echo "Invalid OpenVPN ID, must be numeric\n";
|
68
|
return;
|
69
|
}
|
70
|
$vpnsvc = find_service_by_openvpn_vpnid($extras['id']);
|
71
|
if (empty($vpnsvc)) {
|
72
|
echo "No OpenVPN client or server found with that ID.\n";
|
73
|
return;
|
74
|
}
|
75
|
break;
|
76
|
case "captiveportal":
|
77
|
if (is_validaliasname($args[2])) {
|
78
|
$extras['zone'] = $args[2];
|
79
|
} else {
|
80
|
echo "Invalid Captive Portal Zone.\n";
|
81
|
return;
|
82
|
}
|
83
|
$cpzone = find_service_by_cp_zone($extras['zone']);
|
84
|
if (empty($cpzone)) {
|
85
|
echo "No Captive Portal Zone found with that name.\n";
|
86
|
return;
|
87
|
}
|
88
|
break;
|
89
|
}
|
90
|
|
91
|
switch ($action) {
|
92
|
case "restart":
|
93
|
echo "Attempting to issue {$action} to {$svc_name} service...\n";
|
94
|
$savemsg = service_control_restart($svc_name, $extras);
|
95
|
break;
|
96
|
case "start":
|
97
|
echo "Attempting to issue {$action} to {$svc_name} service...\n";
|
98
|
$savemsg = service_control_start($svc_name, $extras);
|
99
|
break;
|
100
|
case "stop":
|
101
|
echo "Attempting to issue {$action} to {$svc_name} service...\n";
|
102
|
$savemsg = service_control_stop($svc_name, $extras);
|
103
|
break;
|
104
|
case "status":
|
105
|
switch ($svc_name) {
|
106
|
case "openvpn":
|
107
|
$service = $vpnsvc;
|
108
|
break;
|
109
|
case "captiveportal":
|
110
|
$service = $cpzone;
|
111
|
break;
|
112
|
default:
|
113
|
$service = find_service_by_name($svc_name);
|
114
|
break;
|
115
|
}
|
116
|
$savemsg = get_service_status($service) ? "Service {$svc_name} is running." : "Service {$svc_name} is stopped.";
|
117
|
break;
|
118
|
}
|
119
|
|
120
|
echo "\n{$savemsg}\n";
|