Project

General

Profile

Download (24.2 KB) Statistics
| Branch: | Tag: | Revision:
1 c83068f6 Colin Smith
<?php
2 09221bc3 Renato Botelho
/*
3 ac24dc24 Renato Botelho
 * service-utils.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 38809d47 Renato Botelho do Couto
 * Copyright (c) 2005-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 0284d79e jim-p
 * Copyright (c) 2014-2020 Rubicon Communications, LLC (Netgate)
9 c5d81585 Renato Botelho
 * Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
10 ac24dc24 Renato Botelho
 * All rights reserved.
11
 *
12 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
13
 * you may not use this file except in compliance with the License.
14
 * You may obtain a copy of the License at
15 ac24dc24 Renato Botelho
 *
16 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
17 ac24dc24 Renato Botelho
 *
18 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
19
 * distributed under the License is distributed on an "AS IS" BASIS,
20
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
 * See the License for the specific language governing permissions and
22
 * limitations under the License.
23 ac24dc24 Renato Botelho
 */
24 c83068f6 Colin Smith
25 ee6e6011 jim-p
require_once("captiveportal.inc");
26 e3f68ab9 doktornotor
require_once("globals.inc");
27
require_once("gwlb.inc");
28 ee6e6011 jim-p
require_once("ipsec.inc");
29 e3f68ab9 doktornotor
require_once("openvpn.inc");
30
require_once("system.inc");
31
require_once("util.inc");
32 ee6e6011 jim-p
require_once("vpn.inc");
33 523855b0 Scott Ullrich
34 e9d66ed4 phildd
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
35 c83068f6 Colin Smith
function write_rcfile($params) {
36 c2151973 Ermal
	global $g;
37 af799b48 Renato Botelho
38 6f931ad2 Phil Davis
	safe_mkdir(RCFILEPREFIX);
39 e9d66ed4 phildd
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
40 61e047a5 Phil Davis
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
41 af799b48 Renato Botelho
		return false;
42 61e047a5 Phil Davis
	}
43 c2151973 Ermal
44 61e047a5 Phil Davis
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
45 ba8495f0 Ermal
		return false;
46 61e047a5 Phil Davis
	}
47 af799b48 Renato Botelho
48 ba8495f0 Ermal
	$towrite = "#!/bin/sh\n";
49 4dddf8c3 Warren Baker
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
50 ba8495f0 Ermal
51 c83068f6 Colin Smith
	/* write our rc functions */
52 ba8495f0 Ermal
	$towrite .= "rc_start() {\n";
53
	$towrite .= "\t{$params['start']}\n";
54
	$towrite .= "}\n\n";
55 61e047a5 Phil Davis
	if (!empty($params['stop'])) {
56 c6c398c6 jim-p
		$tokill = &$params['stop'];
57 61e047a5 Phil Davis
	} else if (!empty($params['executable'])) {
58 a8dbad89 Colin Smith
		/* just nuke the executable */
59 873c1701 Renato Botelho
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
60 c83068f6 Colin Smith
	} else {
61 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
62 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
63
	}
64 ba8495f0 Ermal
	$towrite .= "rc_stop() {\n";
65
	$towrite .= "\t{$tokill}\n";
66
	$towrite .= "}\n\n";
67 4685e464 Scott Ullrich
68 c83068f6 Colin Smith
	/* begin rcfile logic */
69 ba8495f0 Ermal
	$towrite .= "case \$1 in\n\tstart)\n\t\trc_start\n\t\t;;\n\tstop)\n\t\trc_stop\n\t\t;;\n\trestart)\n\t\trc_stop\n\t\trc_start\n\t\t;;\nesac\n\n";
70
71 44b19298 Ermal
	@file_put_contents($rcfile_fullname, $towrite);
72
	unset($towrite);
73 e9d66ed4 phildd
	@chmod("{$rcfile_fullname}", 0755);
74 ba8495f0 Ermal
75 c83068f6 Colin Smith
	return;
76
}
77 31f346a8 Colin Smith
78 880107d2 jim-p
function start_service($name, $after_sync = false) {
79 669e1adb Bill Marquette
	global $config;
80 ba8495f0 Ermal
81 61e047a5 Phil Davis
	if (empty($name)) {
82 b27ade8e Ermal
		return;
83 61e047a5 Phil Davis
	}
84 b27ade8e Ermal
85 44b19298 Ermal
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
86 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
87 b88050bb jim-p
			if (isset($service['name']) && (strtolower($service['name']) == strtolower($name))) {
88 5d4f4900 jim-p
				/* Avoid starting twice if this is called just after a
89
				 * package sync which starts the service itself. */
90
				if ($after_sync && isset($service['starts_on_sync'])) {
91
					break;
92
				}
93 61e047a5 Phil Davis
				if ($service['rcfile']) {
94 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
95 ba8495f0 Ermal
					if (!empty($service['prefix'])) {
96 c6c398c6 jim-p
						$prefix = &$service['prefix'];
97 31f346a8 Colin Smith
					}
98 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
99 ba8495f0 Ermal
						mwexec_bg("{$prefix}{$service['rcfile']} start");
100 a4ea3340 Scott Ullrich
					}
101 31f346a8 Colin Smith
				}
102 61e047a5 Phil Davis
				if (!empty($service['startcmd'])) {
103 31f346a8 Colin Smith
					eval($service['startcmd']);
104 61e047a5 Phil Davis
				}
105 31f346a8 Colin Smith
				break;
106
			}
107
		}
108
	}
109
}
110
111 58826c73 Colin Smith
function stop_service($name) {
112 cd72ded3 Timo Boettcher
	global $config;
113 246a887a Ermal
114 61e047a5 Phil Davis
	if (empty($name)) {
115 8bf2e9e5 Ermal
		return;
116 61e047a5 Phil Davis
	}
117 8bf2e9e5 Ermal
118 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
119 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
120
			if (strtolower($service['name']) == strtolower($name)) {
121
				if ($service['rcfile']) {
122 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
123 61e047a5 Phil Davis
					if (!empty($service['prefix'])) {
124 c6c398c6 jim-p
						$prefix = &$service['prefix'];
125 cd72ded3 Timo Boettcher
					}
126 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
127 6ae78f08 Marcello Coutinho
						mwexec("{$prefix}{$service['rcfile']} stop");
128 098820e2 Ermal
					}
129
					return;
130 cd72ded3 Timo Boettcher
				}
131 61e047a5 Phil Davis
				if (!empty($service['stopcmd'])) {
132 cd72ded3 Timo Boettcher
					eval($service['stopcmd']);
133 b8877138 doktornotor
				} elseif (!empty($service['executable'])) {
134
					mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
135 61e047a5 Phil Davis
				}
136 ba8495f0 Ermal
137 cd72ded3 Timo Boettcher
				break;
138
			}
139
		}
140
	}
141 31f346a8 Colin Smith
}
142
143 58826c73 Colin Smith
function restart_service($name) {
144 cd72ded3 Timo Boettcher
	global $config;
145 ba8495f0 Ermal
146 61e047a5 Phil Davis
	if (empty($name)) {
147 8bf2e9e5 Ermal
		return;
148 61e047a5 Phil Davis
	}
149 8bf2e9e5 Ermal
150 6855f25d doktornotor
	if (is_service_running($name)) {
151
		stop_service($name);
152
	}
153 636ab238 Colin Smith
	start_service($name);
154 ba8495f0 Ermal
155 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
156 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
157
			if (strtolower($service['name']) == strtolower($name)) {
158
				if ($service['restartcmd']) {
159 cd72ded3 Timo Boettcher
					eval($service['restartcmd']);
160
				}
161
				break;
162
			}
163
		}
164
	}
165 31f346a8 Colin Smith
}
166 ec4e071a Colin Smith
167 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
168 61e047a5 Phil Davis
	if (!file_exists($pidfile)) {
169 ba8495f0 Ermal
		return false;
170 61e047a5 Phil Davis
	}
171 ba8495f0 Ermal
172 17098641 Ermal
	return (isvalidpid($pidfile));
173 cd72ded3 Timo Boettcher
}
174
175 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
176 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
177 61e047a5 Phil Davis
	if ($status != 0) {
178 ca88e48f Scott Ullrich
		return true;
179 61e047a5 Phil Davis
	}
180 ca88e48f Scott Ullrich
	return false;
181 8ff8520f Scott Ullrich
}
182
183 0661a033 Colin Smith
function restart_service_if_running($service) {
184
	global $config;
185 61e047a5 Phil Davis
	if (is_service_running($service)) {
186 0661a033 Colin Smith
		restart_service($service);
187 61e047a5 Phil Davis
	}
188 0661a033 Colin Smith
	return;
189
}
190
191 ccfc0269 phildd
function is_service_enabled($service_name) {
192
	global $config;
193 61e047a5 Phil Davis
	if ($service_name == "") {
194 ccfc0269 phildd
		return false;
195 61e047a5 Phil Davis
	}
196 762e8cf9 jim-p
	if (is_array($config['installedpackages'])) {
197
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
198 61e047a5 Phil Davis
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
199
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
200 762e8cf9 jim-p
			return false;
201
		}
202
	}
203 ccfc0269 phildd
	return true;
204
}
205
206 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
207
	global $config;
208 ba8495f0 Ermal
209 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
210
		foreach ($config['installedpackages']['service'] as $aservice) {
211 b88050bb jim-p
			if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) {
212 ba8495f0 Ermal
				if ($aservice['custom_php_service_status_command'] <> "") {
213
					eval("\$rc={$aservice['custom_php_service_status_command']};");
214 c2d49311 Eirik Oeverby
					return $rc;
215
				}
216 61e047a5 Phil Davis
				if (empty($aservice['executable'])) {
217 ec4e071a Colin Smith
					return false;
218 61e047a5 Phil Davis
				}
219
				if (is_process_running($aservice['executable'])) {
220 ba8495f0 Ermal
					return true;
221 61e047a5 Phil Davis
				}
222 ba8495f0 Ermal
223
				return false;
224 cd72ded3 Timo Boettcher
			}
225
		}
226
	}
227 ba8495f0 Ermal
228 61e047a5 Phil Davis
	if (is_process_running($service)) {
229 ba8495f0 Ermal
		return true;
230 61e047a5 Phil Davis
	}
231 ba8495f0 Ermal
232
	return false;
233 ec4e071a Colin Smith
}
234 ff074bf9 Scott Ullrich
235 e48cdc01 jim-p
function get_services() {
236
	global $config;
237 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
238 e48cdc01 jim-p
		$services = $config['installedpackages']['service'];
239 61e047a5 Phil Davis
	} else {
240 e48cdc01 jim-p
		$services = array();
241 61e047a5 Phil Davis
	}
242 e48cdc01 jim-p
243 61e047a5 Phil Davis
	/*
244
	 * Add services that are in the base.
245 e48cdc01 jim-p
	 */
246 33232486 Warren Baker
	if (is_radvd_enabled()) {
247 e48cdc01 jim-p
		$pconfig = array();
248
		$pconfig['name'] = "radvd";
249
		$pconfig['description'] = gettext("Router Advertisement Daemon");
250 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("radvd");
251
		$pconfig['status'] = get_service_status($pconfig);
252 e48cdc01 jim-p
		$services[] = $pconfig;
253
	}
254
255 33232486 Warren Baker
	if (isset($config['dnsmasq']['enable'])) {
256 e48cdc01 jim-p
		$pconfig = array();
257
		$pconfig['name'] = "dnsmasq";
258
		$pconfig['description'] = gettext("DNS Forwarder");
259 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dnsmasq");
260
		$pconfig['status'] = get_service_status($pconfig);
261 e48cdc01 jim-p
		$services[] = $pconfig;
262
	}
263
264 33232486 Warren Baker
	if (isset($config['unbound']['enable'])) {
265
		$pconfig = array();
266
		$pconfig['name'] = "unbound";
267 a3fb1412 Phil Davis
		$pconfig['description'] = gettext("DNS Resolver");
268 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("unbound");
269
		$pconfig['status'] = get_service_status($pconfig);
270 33232486 Warren Baker
		$services[] = $pconfig;
271
	}
272
273 e881843a Viktor Gurov
	$pconfig = array();
274
	$pconfig['name'] = "pcscd";
275
	$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
276 f2b9ea9a Steve Beaver
	$pconfig['enabled'] = is_service_enabled("pcscd");
277
	$pconfig['status'] = get_service_status($pconfig);
278 e881843a Viktor Gurov
	$services[] = $pconfig;
279
280 b5d5da0c Viktor Gurov
	if (is_array($config['ntpd']) && ($config['ntpd']['enable'] != 'disabled')) {
281
		$pconfig = array();
282
		$pconfig['name'] = "ntpd";
283
		$pconfig['description'] = gettext("NTP clock sync");
284 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("ntpd");
285
		$pconfig['status'] = get_service_status($pconfig);
286 b5d5da0c Viktor Gurov
		$services[] = $pconfig;
287
	}
288 e48cdc01 jim-p
289 e3f68ab9 doktornotor
	$pconfig = array();
290
	$pconfig['name'] = "syslogd";
291
	$pconfig['description'] = gettext("System Logger Daemon");
292 f2b9ea9a Steve Beaver
	$pconfig['enabled'] = is_service_enabled("syslogd");
293
	$pconfig['status'] = get_service_status($pconfig);
294 e3f68ab9 doktornotor
	$services[] = $pconfig;
295
296 e48cdc01 jim-p
	if (is_array($config['captiveportal'])) {
297
		foreach ($config['captiveportal'] as $zone => $setting) {
298
			if (isset($setting['enable'])) {
299
				$pconfig = array();
300
				$pconfig['name'] = "captiveportal";
301
				$pconfig['zone'] = $zone;
302 e8c516a0 Phil Davis
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
303 e48cdc01 jim-p
				$services[] = $pconfig;
304
			}
305
		}
306
	}
307
308
	$iflist = array();
309
	$ifdescrs = get_configured_interface_list();
310
	foreach ($ifdescrs as $if) {
311
		$oc = $config['interfaces'][$if];
312 61e047a5 Phil Davis
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
313 e48cdc01 jim-p
			$iflist[$if] = $if;
314 61e047a5 Phil Davis
		}
315 e48cdc01 jim-p
	}
316
317 33232486 Warren Baker
	if (isset($config['dhcrelay']['enable'])) {
318 e48cdc01 jim-p
		$pconfig = array();
319
		$pconfig['name'] = "dhcrelay";
320
		$pconfig['description'] = gettext("DHCP Relay");
321 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dhcprelay");
322
		$pconfig['status'] = get_service_status($pconfig);
323 e48cdc01 jim-p
		$services[] = $pconfig;
324
	}
325
326 33232486 Warren Baker
	if (isset($config['dhcrelay6']['enable'])) {
327 9590e0de Phil Davis
		$pconfig = array();
328
		$pconfig['name'] = "dhcrelay6";
329
		$pconfig['description'] = gettext("DHCPv6 Relay");
330 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dhcrelay6");
331
		$pconfig['status'] = get_service_status($pconfig);
332 9590e0de Phil Davis
		$services[] = $pconfig;
333
	}
334
335 33232486 Warren Baker
	if (is_dhcp_server_enabled()) {
336 e48cdc01 jim-p
		$pconfig = array();
337
		$pconfig['name'] = "dhcpd";
338
		$pconfig['description'] = gettext("DHCP Service");
339 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dhcpd");
340
		$pconfig['status'] = get_service_status($pconfig);
341 e48cdc01 jim-p
		$services[] = $pconfig;
342
	}
343
344 b05a8f35 jim-p
	$gateways_arr = return_gateways_array();
345
	if (is_array($gateways_arr)) {
346
		$pconfig = array();
347 69eefb50 Renato Botelho
		$pconfig['name'] = "dpinger";
348 b05a8f35 jim-p
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
349 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dpinger");
350
		$pconfig['status'] = get_service_status($pconfig);
351 b05a8f35 jim-p
		$services[] = $pconfig;
352
	}
353
354 33232486 Warren Baker
	if (isset($config['snmpd']['enable'])) {
355 e48cdc01 jim-p
		$pconfig = array();
356
		$pconfig['name'] = "bsnmpd";
357
		$pconfig['description'] = gettext("SNMP Service");
358 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("bsnmpd");
359
		$pconfig['status'] = get_service_status($pconfig);
360 e48cdc01 jim-p
		$services[] = $pconfig;
361
	}
362
363
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
364
		$pconfig = array();
365
		$pconfig['name'] = "igmpproxy";
366
		$pconfig['description'] = gettext("IGMP proxy");
367 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
368
		$pconfig['status'] = get_service_status($pconfig);
369 e48cdc01 jim-p
		$services[] = $pconfig;
370
	}
371
372
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
373
		$pconfig = array();
374
		$pconfig['name'] = "miniupnpd";
375
		$pconfig['description'] = gettext("UPnP Service");
376 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("miniupnpd");
377
		$pconfig['status'] = get_service_status($pconfig);
378 e48cdc01 jim-p
		$services[] = $pconfig;
379
	}
380
381 dc0f709e Luiz Otavio O Souza
	if (ipsec_enabled()) {
382 e48cdc01 jim-p
		$pconfig = array();
383 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
384 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
385 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("ipsec");
386
		$pconfig['status'] = get_service_status($pconfig);
387 e48cdc01 jim-p
		$services[] = $pconfig;
388
	}
389
390 ec439957 jim-p
	if (isset($config['system']['ssh']['enable'])) {
391 aaa78416 jim-p
		$pconfig = array();
392
		$pconfig['name'] = "sshd";
393
		$pconfig['description'] = gettext("Secure Shell Daemon");
394 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("sshd");
395
		$pconfig['status'] = get_service_status($pconfig);
396 aaa78416 jim-p
		$services[] = $pconfig;
397
	}
398
399 e48cdc01 jim-p
	foreach (array('server', 'client') as $mode) {
400 dbc1b8ee jim-p
		init_config_arr(array('openvpn'));
401 e48cdc01 jim-p
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
402
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
403
				if (!isset($setting['disable'])) {
404
					$pconfig = array();
405
					$pconfig['name'] = "openvpn";
406
					$pconfig['mode'] = $mode;
407
					$pconfig['id'] = $id;
408
					$pconfig['vpnid'] = $setting['vpnid'];
409 e8c516a0 Phil Davis
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
410 f2b9ea9a Steve Beaver
					$pconfig['enabled'] = is_service_enabled("openvpn");
411
					$pconfig['status'] = get_service_status($pconfig);
412 e48cdc01 jim-p
					$services[] = $pconfig;
413
				}
414
			}
415
		}
416
	}
417
418
	return $services;
419
}
420
421 76692ad2 jim-p
function find_service_by_name($name) {
422
	$services = get_services();
423 f2b9ea9a Steve Beaver
	
424 61e047a5 Phil Davis
	foreach ($services as $service) {
425 144863e3 jim-p
		if (isset($service["name"]) && ($service["name"] == $name)) {
426 76692ad2 jim-p
			return $service;
427 61e047a5 Phil Davis
		}
428
	}
429 76692ad2 jim-p
	return array();
430
}
431
432 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
433
	$services = get_services();
434 61e047a5 Phil Davis
	foreach ($services as $service) {
435 144863e3 jim-p
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
436 6d9b1074 jim-p
			return $service;
437 61e047a5 Phil Davis
		}
438
	}
439 6d9b1074 jim-p
	return array();
440
}
441
442
function find_service_by_cp_zone($zone) {
443
	$services = get_services();
444 61e047a5 Phil Davis
	foreach ($services as $service) {
445 144863e3 jim-p
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
446 6d9b1074 jim-p
			return $service;
447 61e047a5 Phil Davis
		}
448
	}
449 6d9b1074 jim-p
	return array();
450
}
451
452 258fc75b jim-p
function service_description_compare($a, $b) {
453
	if (strtolower($a['description']) == strtolower($b['description'])) {
454
		return 0;
455
	}
456
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
457
}
458
459 e48cdc01 jim-p
function service_name_compare($a, $b) {
460 144863e3 jim-p
	if (!isset($a['name']) || !isset($b['name'])) {
461
		return -1;
462
	}
463 258fc75b jim-p
	/* If the names are equal, fall back to sorting by description */
464 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
465 258fc75b jim-p
		return service_description_compare($a, $b);
466 61e047a5 Phil Davis
	}
467 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
468
}
469
470 095edfeb Stephen Beaver
function service_dispname_compare($a, $b) {
471 258fc75b jim-p
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
472
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
473
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
474
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
475
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
476
			if ($adn2 == $bdn2) {
477
				return 0;
478
			}
479
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
480
		}
481
	}
482
	/* If the display names are equal, compare the internal name */
483 095edfeb Stephen Beaver
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
484 258fc75b jim-p
		return service_name_compare($a, $b);
485 095edfeb Stephen Beaver
	}
486 258fc75b jim-p
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
487 095edfeb Stephen Beaver
}
488
489 e48cdc01 jim-p
function get_pkg_descr($package_name) {
490
	global $config;
491
	if (is_array($config['installedpackages']['package'])) {
492 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
493
			if ($pkg['name'] == $package_name) {
494 e48cdc01 jim-p
				return $pkg['descr'];
495 61e047a5 Phil Davis
			}
496 e48cdc01 jim-p
		}
497
	}
498
	return gettext("Not available.");
499
}
500
501
function get_service_status($service) {
502
	global $g;
503
	switch ($service['name']) {
504
		case "openvpn":
505
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
506
			break;
507
		case "captiveportal":
508 cd41643d Renato Botelho
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
509 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
510 cd41643d Renato Botelho
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
511 61e047a5 Phil Davis
			}
512 e48cdc01 jim-p
			break;
513 18725085 Ermal
		case "vhosts-http":
514
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
515
			break;
516 9590e0de Phil Davis
		case "dhcrelay6":
517
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
518
			break;
519 e381cc01 Ermal
		case 'ipsec':
520 60e34dde Viktor G
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
521 e381cc01 Ermal
			break;
522 e48cdc01 jim-p
		default:
523
			$running = is_service_running($service['name']);
524
	}
525
	return $running;
526
}
527
528 0a9d81fb NOYB
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
529 e48cdc01 jim-p
	$output = "";
530 0a9d81fb NOYB
531 61e047a5 Phil Davis
	if (get_service_status($service)) {
532 92421710 jim-p
		$statustext = gettext("Running");
533 0a9d81fb NOYB
		$text_class = "text-success";
534 a03162c8 Stephen Beaver
		$fa_class = "fa fa-check-circle";
535 0a9d81fb NOYB
		$fa_class_thumbs = "fa fa-thumbs-o-up";
536
		$Thumbs_UpDown = "Thumbs up";
537 e48cdc01 jim-p
	} else {
538 0a9d81fb NOYB
		if (is_service_enabled($service['name'])) {
539
			$statustext = gettext("Stopped");
540
			$text_class = "text-danger";
541 a03162c8 Stephen Beaver
			$fa_class = "fa fa-times-circle";
542 0a9d81fb NOYB
		} else {
543
			$statustext = gettext("Disabled");
544
			$text_class = "text-warning";
545
			$fa_class = "fa fa-ban";
546 61e047a5 Phil Davis
		}
547 0a9d81fb NOYB
		$fa_class_thumbs = "fa fa-thumbs-o-down";
548
		$Thumbs_UpDown = "Thumbs down";
549
	}
550
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
551
552
	if ($title == "state") {
553 446505a9 NOYB
		$title = $statustext;
554 0a9d81fb NOYB
	} elseif ($title == "service_state") {
555 446505a9 NOYB
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
556 0a9d81fb NOYB
	} elseif ($title == "description_state") {
557 446505a9 NOYB
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
558 0a9d81fb NOYB
	} elseif ($title == "description_service_state") {
559 446505a9 NOYB
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
560 e48cdc01 jim-p
	}
561 0a9d81fb NOYB
562 446505a9 NOYB
	$spacer = ($withthumbs || $withtext) ? " " : "";
563
564
	$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"{$title}\"><span style=\"display: none\">{$statustext}</span></i>{$spacer}";
565
566 0a9d81fb NOYB
	$spacer = ($withtext) ? " " : "";
567
	if ($withthumbs) {
568
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
569
	}
570
571
	if ($withtext) {
572
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
573
	}
574
575 e48cdc01 jim-p
	return $output;
576
}
577
578 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
579 49840447 PiBa-NL
	global $g;
580 e48cdc01 jim-p
	$output = "";
581 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
582 1180e4f0 Sjon Hortensius
583
	switch ($service['name']) {
584
		case "openvpn":
585 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
586 1180e4f0 Sjon Hortensius
		break;
587
		case "captiveportal":
588 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
589 1180e4f0 Sjon Hortensius
		break;
590
		default:
591 ab7e04c7 Colin Fleming
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
592 1180e4f0 Sjon Hortensius
	}
593
594 61e047a5 Phil Davis
	if (get_service_status($service)) {
595 e48cdc01 jim-p
		switch ($service['name']) {
596
			case "openvpn":
597 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
598 e48cdc01 jim-p
				break;
599
			case "captiveportal":
600 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
601 e48cdc01 jim-p
				break;
602
			default:
603 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
604 e48cdc01 jim-p
		}
605 74b0f23e Stephen Beaver
606 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
607 74b0f23e Stephen Beaver
608 e48cdc01 jim-p
		switch ($service['name']) {
609
			case "openvpn":
610 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
611 e48cdc01 jim-p
				break;
612
			case "captiveportal":
613 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
614 e48cdc01 jim-p
				break;
615
			default:
616 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
617 e48cdc01 jim-p
		}
618 74b0f23e Stephen Beaver
619 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
620 74b0f23e Stephen Beaver
621 e48cdc01 jim-p
	} else {
622 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
623 1180e4f0 Sjon Hortensius
624 46bb8a0b Sjon Hortensius
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
625 1180e4f0 Sjon Hortensius
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
626 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
627 e48cdc01 jim-p
		}
628
	}
629 1180e4f0 Sjon Hortensius
630 e48cdc01 jim-p
	return $output;
631
}
632 ee6e6011 jim-p
633
function service_control_start($name, $extras) {
634
	global $g;
635 61e047a5 Phil Davis
	switch ($name) {
636 ee6e6011 jim-p
		case 'radvd':
637
			services_radvd_configure();
638
			break;
639
		case 'captiveportal':
640 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
641 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
642
			break;
643
		case 'ntpd':
644
			system_ntp_configure();
645
			break;
646 69eefb50 Renato Botelho
		case 'dpinger':
647 b05a8f35 jim-p
			setup_gateways_monitor();
648
			break;
649 ee6e6011 jim-p
		case 'bsnmpd':
650
			services_snmpd_configure();
651
			break;
652 9590e0de Phil Davis
		case 'dhcrelay':
653
			services_dhcrelay_configure();
654
			break;
655
		case 'dhcrelay6':
656
			services_dhcrelay6_configure();
657
			break;
658 ee6e6011 jim-p
		case 'dnsmasq':
659
			services_dnsmasq_configure();
660
			break;
661 a8604dc6 Phil Davis
		case 'unbound':
662
			services_unbound_configure();
663
			break;
664 ee6e6011 jim-p
		case 'dhcpd':
665
			services_dhcpd_configure();
666
			break;
667
		case 'igmpproxy':
668
			services_igmpproxy_configure();
669
			break;
670
		case 'miniupnpd':
671
			upnp_action('start');
672
			break;
673 8b4abd59 Ermal
		case 'ipsec':
674 c6220dcf jim-p
			ipsec_force_reload();
675 ee6e6011 jim-p
			break;
676 aaa78416 jim-p
		case 'sshd':
677
			send_event("service restart sshd");
678
			break;
679 ee6e6011 jim-p
		case 'openvpn':
680 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
681 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
682 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
683 348c2af1 jim-p
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
684 61e047a5 Phil Davis
				if (file_exists($configfile)) {
685 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
686 61e047a5 Phil Davis
				}
687 ee6e6011 jim-p
			}
688
			break;
689 e3f68ab9 doktornotor
		case 'syslogd':
690
			system_syslogd_start();
691
			break;
692 e881843a Viktor Gurov
		case 'pcscd':
693
			mwexec_bg("service pcscd onestart");
694
			break;
695 ee6e6011 jim-p
		default:
696
			start_service($name);
697
			break;
698
	}
699 086cf944 Phil Davis
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
700 ee6e6011 jim-p
}
701
function service_control_stop($name, $extras) {
702
	global $g;
703 61e047a5 Phil Davis
	switch ($name) {
704 ee6e6011 jim-p
		case 'radvd':
705
			killbypid("{$g['varrun_path']}/radvd.pid");
706
			break;
707
		case 'captiveportal':
708 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
709 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
710
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
711 ee6e6011 jim-p
			break;
712
		case 'ntpd':
713
			killbyname("ntpd");
714
			break;
715
		case 'openntpd':
716
			killbyname("openntpd");
717
			break;
718 69eefb50 Renato Botelho
		case 'dpinger':
719
			stop_dpinger();
720 b05a8f35 jim-p
			break;
721 ee6e6011 jim-p
		case 'bsnmpd':
722
			killbypid("{$g['varrun_path']}/snmpd.pid");
723
			break;
724
		case 'choparp':
725
			killbyname("choparp");
726
			break;
727
		case 'dhcpd':
728
			killbyname("dhcpd");
729
			break;
730
		case 'dhcrelay':
731
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
732
			break;
733 9590e0de Phil Davis
		case 'dhcrelay6':
734
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
735
			break;
736 ee6e6011 jim-p
		case 'dnsmasq':
737
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
738
			break;
739 33232486 Warren Baker
		case 'unbound':
740
			killbypid("{$g['varrun_path']}/unbound.pid");
741
			break;
742 ee6e6011 jim-p
		case 'igmpproxy':
743
			killbyname("igmpproxy");
744
			break;
745
		case 'miniupnpd':
746
			upnp_action('stop');
747
			break;
748
		case 'sshd':
749
			killbyname("sshd");
750
			break;
751 8b4abd59 Ermal
		case 'ipsec':
752 c6220dcf jim-p
			exec("/usr/local/sbin/strongswanrc stop");
753 ee6e6011 jim-p
			break;
754
		case 'openvpn':
755 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
756 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
757 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
758 ee6e6011 jim-p
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
759
				killbypid($pidfile);
760
			}
761
			break;
762 e3f68ab9 doktornotor
		case 'syslogd':
763
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
764
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
765
				usleep(100000);
766
			}
767
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
768
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
769 726b889b doktornotor
				usleep(100000);
770 e3f68ab9 doktornotor
			}
771 b89270b7 Renato Botelho
			/* Make sure sshguard stops as well */
772
			sigkillbyname("sshguard", "TERM");
773 ee6e6011 jim-p
			break;
774 e881843a Viktor Gurov
		case 'pcscd':
775
			mwexec_bg("service pcscd onestop");
776
			break;
777 ee6e6011 jim-p
		default:
778
			stop_service($name);
779
			break;
780
	}
781
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
782
}
783 33232486 Warren Baker
784 ee6e6011 jim-p
function service_control_restart($name, $extras) {
785
	global $g;
786 61e047a5 Phil Davis
	switch ($name) {
787 ee6e6011 jim-p
		case 'radvd':
788
			services_radvd_configure();
789
			break;
790
		case 'captiveportal':
791 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
792 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
793
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
794 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
795
			break;
796
		case 'ntpd':
797
		case 'openntpd':
798
			system_ntp_configure();
799
			break;
800 69eefb50 Renato Botelho
		case 'dpinger':
801 b05a8f35 jim-p
			setup_gateways_monitor();
802
			break;
803 ee6e6011 jim-p
		case 'bsnmpd':
804
			services_snmpd_configure();
805
			break;
806 9590e0de Phil Davis
		case 'dhcrelay':
807
			services_dhcrelay_configure();
808
			break;
809
		case 'dhcrelay6':
810
			services_dhcrelay6_configure();
811
			break;
812 ee6e6011 jim-p
		case 'dnsmasq':
813
			services_dnsmasq_configure();
814
			break;
815 33232486 Warren Baker
		case 'unbound':
816
			services_unbound_configure();
817
			break;
818 ee6e6011 jim-p
		case 'dhcpd':
819
			services_dhcpd_configure();
820
			break;
821
		case 'igmpproxy':
822
			services_igmpproxy_configure();
823
			break;
824
		case 'miniupnpd':
825
			upnp_action('restart');
826
			break;
827 8b4abd59 Ermal
		case 'ipsec':
828 c6220dcf jim-p
			ipsec_force_reload();
829 ee6e6011 jim-p
			break;
830 aaa78416 jim-p
		case 'sshd':
831
			send_event("service restart sshd");
832
			break;
833 ee6e6011 jim-p
		case 'openvpn':
834 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
835 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
836 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
837 348c2af1 jim-p
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
838 61e047a5 Phil Davis
				if (file_exists($configfile)) {
839 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
840 61e047a5 Phil Davis
				}
841 ee6e6011 jim-p
			}
842
			break;
843 e3f68ab9 doktornotor
		case 'syslogd':
844
			system_syslogd_start();
845
			break;
846 ee6e6011 jim-p
		default:
847
			restart_service($name);
848
			break;
849
	}
850 086cf944 Phil Davis
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
851 ee6e6011 jim-p
}
852
853 17098641 Ermal
?>