Project

General

Profile

Download (3.05 KB) Statistics
| Branch: | Tag: | Revision:
1 ac24dc24 Renato Botelho
/*
2
 * svc
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5 38809d47 Renato Botelho do Couto
 * Copyright (c) 2016 Electric Sheep Fencing
6 a68f7a3d Luiz Otavio O Souza
 * Copyright (c) 2016-2024 Rubicon Communications, LLC (Netgate)
7 ac24dc24 Renato Botelho
 * All rights reserved.
8
 *
9 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 *
13 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
14 ac24dc24 Renato Botelho
 *
15 b12ea3fb Renato Botelho
 * 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 ac24dc24 Renato Botelho
 */
21
22 ab1cf455 jim-p
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 1bfcf6f1 Phil Davis
if (is_array($command_split)) {
38 ab1cf455 jim-p
	$args = array_slice($command_split, 2);
39 1bfcf6f1 Phil Davis
} else {
40 ab1cf455 jim-p
	$args = array_slice($argv, 3);
41 1bfcf6f1 Phil Davis
}
42 ab1cf455 jim-p
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 6990ad35 Phil Davis
// Handle service-specific options.
56 ab1cf455 jim-p
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 7cbfc265 jim-p
		echo "Attempting to issue {$action} to {$svc_name} service...\n";
94 ab1cf455 jim-p
		$savemsg = service_control_restart($svc_name, $extras);
95
		break;
96
	case "start":
97 7cbfc265 jim-p
		echo "Attempting to issue {$action} to {$svc_name} service...\n";
98 ab1cf455 jim-p
		$savemsg = service_control_start($svc_name, $extras);
99
		break;
100
	case "stop":
101 7cbfc265 jim-p
		echo "Attempting to issue {$action} to {$svc_name} service...\n";
102 ab1cf455 jim-p
		$savemsg = service_control_stop($svc_name, $extras);
103
		break;
104 7cbfc265 jim-p
	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 ab1cf455 jim-p
}
119
120
echo "\n{$savemsg}\n";