Project

General

Profile

Download (3.01 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * svc
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2016 Rubicon Communications, LLC (Netgate)
6
 * All rights reserved.
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

    
21
require_once("config.inc");
22
require_once("util.inc");
23
require_once("service-utils.inc");
24

    
25
function usage() {
26
	echo "Usage: playback svc <action> <service name> [service-specific options]\n\n";
27
	echo "Examples:\n";
28
	echo "playback svc stop dhcpd\n";
29
	echo "playback svc restart openvpn client 2\n";
30
	echo "playback svc stop captiveportal zone1\n";
31
	echo "\n";
32
}
33

    
34
global $g, $config, $argv, $command_split;
35

    
36
if (is_array($command_split)) {
37
	$args = array_slice($command_split, 2);
38
} else {
39
	$args = array_slice($argv, 3);
40
}
41

    
42
if (empty($args[0])) {
43
	usage();
44
}
45

    
46
$extras = array();
47

    
48
// start, stop, restart
49
$action = $args[0];
50

    
51
// dhcpd, openvpn, etc.
52
$svc_name = $args[1];
53

    
54
// Handle service-specific options.
55
switch ($svc_name) {
56
	case "openvpn":
57
		if (in_array($args[2], array("server", "client"))) {
58
			$extras['vpnmode'] = $args[2];
59
		} else {
60
			echo "Invalid OpenVPN mode (server, client)\n";
61
			return;
62
		}
63
		if (is_numeric($args[3])) {
64
			$extras['id'] = $args[3];
65
		} else {
66
			echo "Invalid OpenVPN ID, must be numeric\n";
67
			return;
68
		}
69
		$vpnsvc = find_service_by_openvpn_vpnid($extras['id']);
70
		if (empty($vpnsvc)) {
71
			echo "No OpenVPN client or server found with that ID.\n";
72
			return;
73
		}
74
		break;
75
	case "captiveportal":
76
		if (is_validaliasname($args[2])) {
77
			$extras['zone'] = $args[2];
78
		} else {
79
			echo "Invalid Captive Portal Zone.\n";
80
			return;
81
		}
82
		$cpzone = find_service_by_cp_zone($extras['zone']);
83
		if (empty($cpzone)) {
84
			echo "No Captive Portal Zone found with that name.\n";
85
			return;
86
		}
87
		break;
88
}
89

    
90
switch ($action) {
91
	case "restart":
92
		echo "Attempting to issue {$action} to {$svc_name} service...\n";
93
		$savemsg = service_control_restart($svc_name, $extras);
94
		break;
95
	case "start":
96
		echo "Attempting to issue {$action} to {$svc_name} service...\n";
97
		$savemsg = service_control_start($svc_name, $extras);
98
		break;
99
	case "stop":
100
		echo "Attempting to issue {$action} to {$svc_name} service...\n";
101
		$savemsg = service_control_stop($svc_name, $extras);
102
		break;
103
	case "status":
104
		switch ($svc_name) {
105
			case "openvpn":
106
				$service = $vpnsvc;
107
				break;
108
			case "captiveportal":
109
				$service = $cpzone;
110
				break;
111
			default:
112
				$service = find_service_by_name($svc_name);
113
				break;
114
		}
115
		$savemsg = get_service_status($service) ? "Service {$svc_name} is running." : "Service {$svc_name} is stopped.";
116
		break;
117
}
118

    
119
echo "\n{$savemsg}\n";
(23-23/24)