Project

General

Profile

Download (24.4 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 8f2f85c3 Luiz Otavio O Souza
 * Copyright (c) 2014-2022 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 573ec19d Renato Botelho do Couto
	$towrite .= "# This file was automatically generated\n# by the {$g['product_label']} 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 013cbaaa Viktor G
	if (isset($config['ipsec']['pkcs11support'])) {
274
		$pconfig = array();
275
		$pconfig['name'] = "pcscd";
276
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
277
		$pconfig['enabled'] = is_service_enabled("pcscd");
278
		$pconfig['status'] = get_service_status($pconfig);
279
		$services[] = $pconfig;
280
	}
281 e881843a Viktor Gurov
282 b5d5da0c Viktor Gurov
	if (is_array($config['ntpd']) && ($config['ntpd']['enable'] != 'disabled')) {
283
		$pconfig = array();
284
		$pconfig['name'] = "ntpd";
285
		$pconfig['description'] = gettext("NTP clock sync");
286 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("ntpd");
287
		$pconfig['status'] = get_service_status($pconfig);
288 b5d5da0c Viktor Gurov
		$services[] = $pconfig;
289
	}
290 e48cdc01 jim-p
291 e3f68ab9 doktornotor
	$pconfig = array();
292
	$pconfig['name'] = "syslogd";
293
	$pconfig['description'] = gettext("System Logger Daemon");
294 f2b9ea9a Steve Beaver
	$pconfig['enabled'] = is_service_enabled("syslogd");
295
	$pconfig['status'] = get_service_status($pconfig);
296 e3f68ab9 doktornotor
	$services[] = $pconfig;
297
298 e48cdc01 jim-p
	if (is_array($config['captiveportal'])) {
299
		foreach ($config['captiveportal'] as $zone => $setting) {
300
			if (isset($setting['enable'])) {
301
				$pconfig = array();
302
				$pconfig['name'] = "captiveportal";
303
				$pconfig['zone'] = $zone;
304 e8c516a0 Phil Davis
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
305 e48cdc01 jim-p
				$services[] = $pconfig;
306
			}
307
		}
308
	}
309
310
	$iflist = array();
311
	$ifdescrs = get_configured_interface_list();
312
	foreach ($ifdescrs as $if) {
313
		$oc = $config['interfaces'][$if];
314 61e047a5 Phil Davis
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
315 e48cdc01 jim-p
			$iflist[$if] = $if;
316 61e047a5 Phil Davis
		}
317 e48cdc01 jim-p
	}
318
319 33232486 Warren Baker
	if (isset($config['dhcrelay']['enable'])) {
320 e48cdc01 jim-p
		$pconfig = array();
321
		$pconfig['name'] = "dhcrelay";
322
		$pconfig['description'] = gettext("DHCP Relay");
323 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dhcprelay");
324
		$pconfig['status'] = get_service_status($pconfig);
325 e48cdc01 jim-p
		$services[] = $pconfig;
326
	}
327
328 33232486 Warren Baker
	if (isset($config['dhcrelay6']['enable'])) {
329 9590e0de Phil Davis
		$pconfig = array();
330
		$pconfig['name'] = "dhcrelay6";
331
		$pconfig['description'] = gettext("DHCPv6 Relay");
332 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dhcrelay6");
333
		$pconfig['status'] = get_service_status($pconfig);
334 9590e0de Phil Davis
		$services[] = $pconfig;
335
	}
336
337 33232486 Warren Baker
	if (is_dhcp_server_enabled()) {
338 e48cdc01 jim-p
		$pconfig = array();
339
		$pconfig['name'] = "dhcpd";
340
		$pconfig['description'] = gettext("DHCP Service");
341 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dhcpd");
342
		$pconfig['status'] = get_service_status($pconfig);
343 e48cdc01 jim-p
		$services[] = $pconfig;
344
	}
345
346 b05a8f35 jim-p
	$gateways_arr = return_gateways_array();
347
	if (is_array($gateways_arr)) {
348
		$pconfig = array();
349 69eefb50 Renato Botelho
		$pconfig['name'] = "dpinger";
350 b05a8f35 jim-p
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
351 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("dpinger");
352
		$pconfig['status'] = get_service_status($pconfig);
353 b05a8f35 jim-p
		$services[] = $pconfig;
354
	}
355
356 33232486 Warren Baker
	if (isset($config['snmpd']['enable'])) {
357 e48cdc01 jim-p
		$pconfig = array();
358
		$pconfig['name'] = "bsnmpd";
359
		$pconfig['description'] = gettext("SNMP Service");
360 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("bsnmpd");
361
		$pconfig['status'] = get_service_status($pconfig);
362 e48cdc01 jim-p
		$services[] = $pconfig;
363
	}
364
365
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
366
		$pconfig = array();
367
		$pconfig['name'] = "igmpproxy";
368
		$pconfig['description'] = gettext("IGMP proxy");
369 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
370
		$pconfig['status'] = get_service_status($pconfig);
371 e48cdc01 jim-p
		$services[] = $pconfig;
372
	}
373
374
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
375
		$pconfig = array();
376
		$pconfig['name'] = "miniupnpd";
377
		$pconfig['description'] = gettext("UPnP Service");
378 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("miniupnpd");
379
		$pconfig['status'] = get_service_status($pconfig);
380 e48cdc01 jim-p
		$services[] = $pconfig;
381
	}
382
383 dc0f709e Luiz Otavio O Souza
	if (ipsec_enabled()) {
384 e48cdc01 jim-p
		$pconfig = array();
385 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
386 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
387 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("ipsec");
388
		$pconfig['status'] = get_service_status($pconfig);
389 e48cdc01 jim-p
		$services[] = $pconfig;
390
	}
391
392 ec439957 jim-p
	if (isset($config['system']['ssh']['enable'])) {
393 aaa78416 jim-p
		$pconfig = array();
394
		$pconfig['name'] = "sshd";
395
		$pconfig['description'] = gettext("Secure Shell Daemon");
396 f2b9ea9a Steve Beaver
		$pconfig['enabled'] = is_service_enabled("sshd");
397
		$pconfig['status'] = get_service_status($pconfig);
398 aaa78416 jim-p
		$services[] = $pconfig;
399
	}
400
401 e48cdc01 jim-p
	foreach (array('server', 'client') as $mode) {
402 dbc1b8ee jim-p
		init_config_arr(array('openvpn'));
403 e48cdc01 jim-p
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
404
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
405
				if (!isset($setting['disable'])) {
406
					$pconfig = array();
407
					$pconfig['name'] = "openvpn";
408
					$pconfig['mode'] = $mode;
409
					$pconfig['id'] = $id;
410
					$pconfig['vpnid'] = $setting['vpnid'];
411 e8c516a0 Phil Davis
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
412 f2b9ea9a Steve Beaver
					$pconfig['enabled'] = is_service_enabled("openvpn");
413
					$pconfig['status'] = get_service_status($pconfig);
414 e48cdc01 jim-p
					$services[] = $pconfig;
415
				}
416
			}
417
		}
418
	}
419
420
	return $services;
421
}
422
423 76692ad2 jim-p
function find_service_by_name($name) {
424
	$services = get_services();
425 f2b9ea9a Steve Beaver
	
426 61e047a5 Phil Davis
	foreach ($services as $service) {
427 144863e3 jim-p
		if (isset($service["name"]) && ($service["name"] == $name)) {
428 76692ad2 jim-p
			return $service;
429 61e047a5 Phil Davis
		}
430
	}
431 76692ad2 jim-p
	return array();
432
}
433
434 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
435
	$services = get_services();
436 61e047a5 Phil Davis
	foreach ($services as $service) {
437 144863e3 jim-p
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
438 6d9b1074 jim-p
			return $service;
439 61e047a5 Phil Davis
		}
440
	}
441 6d9b1074 jim-p
	return array();
442
}
443
444
function find_service_by_cp_zone($zone) {
445
	$services = get_services();
446 61e047a5 Phil Davis
	foreach ($services as $service) {
447 144863e3 jim-p
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
448 6d9b1074 jim-p
			return $service;
449 61e047a5 Phil Davis
		}
450
	}
451 6d9b1074 jim-p
	return array();
452
}
453
454 258fc75b jim-p
function service_description_compare($a, $b) {
455
	if (strtolower($a['description']) == strtolower($b['description'])) {
456
		return 0;
457
	}
458
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
459
}
460
461 e48cdc01 jim-p
function service_name_compare($a, $b) {
462 144863e3 jim-p
	if (!isset($a['name']) || !isset($b['name'])) {
463
		return -1;
464
	}
465 258fc75b jim-p
	/* If the names are equal, fall back to sorting by description */
466 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
467 258fc75b jim-p
		return service_description_compare($a, $b);
468 61e047a5 Phil Davis
	}
469 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
470
}
471
472 095edfeb Stephen Beaver
function service_dispname_compare($a, $b) {
473 258fc75b jim-p
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
474
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
475
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
476
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
477
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
478
			if ($adn2 == $bdn2) {
479
				return 0;
480
			}
481
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
482
		}
483
	}
484
	/* If the display names are equal, compare the internal name */
485 095edfeb Stephen Beaver
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
486 258fc75b jim-p
		return service_name_compare($a, $b);
487 095edfeb Stephen Beaver
	}
488 258fc75b jim-p
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
489 095edfeb Stephen Beaver
}
490
491 e48cdc01 jim-p
function get_pkg_descr($package_name) {
492
	global $config;
493
	if (is_array($config['installedpackages']['package'])) {
494 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
495
			if ($pkg['name'] == $package_name) {
496 e48cdc01 jim-p
				return $pkg['descr'];
497 61e047a5 Phil Davis
			}
498 e48cdc01 jim-p
		}
499
	}
500
	return gettext("Not available.");
501
}
502
503
function get_service_status($service) {
504
	global $g;
505
	switch ($service['name']) {
506
		case "openvpn":
507
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
508
			break;
509
		case "captiveportal":
510 cd41643d Renato Botelho
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
511 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
512 cd41643d Renato Botelho
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
513 61e047a5 Phil Davis
			}
514 e48cdc01 jim-p
			break;
515 18725085 Ermal
		case "vhosts-http":
516
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
517
			break;
518 9590e0de Phil Davis
		case "dhcrelay6":
519
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
520
			break;
521 e381cc01 Ermal
		case 'ipsec':
522 60e34dde Viktor G
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
523 e381cc01 Ermal
			break;
524 e48cdc01 jim-p
		default:
525
			$running = is_service_running($service['name']);
526
	}
527
	return $running;
528
}
529
530 0a9d81fb NOYB
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
531 e48cdc01 jim-p
	$output = "";
532 0a9d81fb NOYB
533 61e047a5 Phil Davis
	if (get_service_status($service)) {
534 92421710 jim-p
		$statustext = gettext("Running");
535 0a9d81fb NOYB
		$text_class = "text-success";
536 a03162c8 Stephen Beaver
		$fa_class = "fa fa-check-circle";
537 0a9d81fb NOYB
		$fa_class_thumbs = "fa fa-thumbs-o-up";
538
		$Thumbs_UpDown = "Thumbs up";
539 e48cdc01 jim-p
	} else {
540 0a9d81fb NOYB
		if (is_service_enabled($service['name'])) {
541
			$statustext = gettext("Stopped");
542
			$text_class = "text-danger";
543 a03162c8 Stephen Beaver
			$fa_class = "fa fa-times-circle";
544 0a9d81fb NOYB
		} else {
545
			$statustext = gettext("Disabled");
546
			$text_class = "text-warning";
547
			$fa_class = "fa fa-ban";
548 61e047a5 Phil Davis
		}
549 0a9d81fb NOYB
		$fa_class_thumbs = "fa fa-thumbs-o-down";
550
		$Thumbs_UpDown = "Thumbs down";
551
	}
552
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
553
554
	if ($title == "state") {
555 446505a9 NOYB
		$title = $statustext;
556 0a9d81fb NOYB
	} elseif ($title == "service_state") {
557 446505a9 NOYB
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
558 0a9d81fb NOYB
	} elseif ($title == "description_state") {
559 446505a9 NOYB
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
560 0a9d81fb NOYB
	} elseif ($title == "description_service_state") {
561 446505a9 NOYB
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
562 e48cdc01 jim-p
	}
563 0a9d81fb NOYB
564 446505a9 NOYB
	$spacer = ($withthumbs || $withtext) ? " " : "";
565
566
	$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"{$title}\"><span style=\"display: none\">{$statustext}</span></i>{$spacer}";
567
568 0a9d81fb NOYB
	$spacer = ($withtext) ? " " : "";
569
	if ($withthumbs) {
570
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
571
	}
572
573
	if ($withtext) {
574
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
575
	}
576
577 e48cdc01 jim-p
	return $output;
578
}
579
580 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
581 49840447 PiBa-NL
	global $g;
582 e48cdc01 jim-p
	$output = "";
583 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
584 1180e4f0 Sjon Hortensius
585
	switch ($service['name']) {
586
		case "openvpn":
587 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
588 1180e4f0 Sjon Hortensius
		break;
589
		case "captiveportal":
590 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
591 1180e4f0 Sjon Hortensius
		break;
592
		default:
593 ab7e04c7 Colin Fleming
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
594 1180e4f0 Sjon Hortensius
	}
595
596 61e047a5 Phil Davis
	if (get_service_status($service)) {
597 e48cdc01 jim-p
		switch ($service['name']) {
598
			case "openvpn":
599 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
600 e48cdc01 jim-p
				break;
601
			case "captiveportal":
602 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
603 e48cdc01 jim-p
				break;
604
			default:
605 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
606 e48cdc01 jim-p
		}
607 74b0f23e Stephen Beaver
608 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
609 74b0f23e Stephen Beaver
610 e48cdc01 jim-p
		switch ($service['name']) {
611
			case "openvpn":
612 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
613 e48cdc01 jim-p
				break;
614
			case "captiveportal":
615 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
616 e48cdc01 jim-p
				break;
617
			default:
618 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
619 e48cdc01 jim-p
		}
620 74b0f23e Stephen Beaver
621 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
622 74b0f23e Stephen Beaver
623 e48cdc01 jim-p
	} else {
624 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
625 1180e4f0 Sjon Hortensius
626 46bb8a0b Sjon Hortensius
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
627 1180e4f0 Sjon Hortensius
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
628 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
629 e48cdc01 jim-p
		}
630
	}
631 1180e4f0 Sjon Hortensius
632 e48cdc01 jim-p
	return $output;
633
}
634 ee6e6011 jim-p
635
function service_control_start($name, $extras) {
636
	global $g;
637 61e047a5 Phil Davis
	switch ($name) {
638 ee6e6011 jim-p
		case 'radvd':
639
			services_radvd_configure();
640
			break;
641
		case 'captiveportal':
642 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
643 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
644
			break;
645
		case 'ntpd':
646
			system_ntp_configure();
647
			break;
648 69eefb50 Renato Botelho
		case 'dpinger':
649 b05a8f35 jim-p
			setup_gateways_monitor();
650
			break;
651 ee6e6011 jim-p
		case 'bsnmpd':
652
			services_snmpd_configure();
653
			break;
654 9590e0de Phil Davis
		case 'dhcrelay':
655
			services_dhcrelay_configure();
656
			break;
657
		case 'dhcrelay6':
658
			services_dhcrelay6_configure();
659
			break;
660 ee6e6011 jim-p
		case 'dnsmasq':
661
			services_dnsmasq_configure();
662
			break;
663 a8604dc6 Phil Davis
		case 'unbound':
664
			services_unbound_configure();
665
			break;
666 ee6e6011 jim-p
		case 'dhcpd':
667
			services_dhcpd_configure();
668
			break;
669
		case 'igmpproxy':
670
			services_igmpproxy_configure();
671
			break;
672
		case 'miniupnpd':
673
			upnp_action('start');
674
			break;
675 8b4abd59 Ermal
		case 'ipsec':
676 c6220dcf jim-p
			ipsec_force_reload();
677 ee6e6011 jim-p
			break;
678 aaa78416 jim-p
		case 'sshd':
679
			send_event("service restart sshd");
680
			break;
681 2c702751 Viktor G
		case 'pcscd':
682
			ipsec_force_reload();
683
			break;
684 ee6e6011 jim-p
		case 'openvpn':
685 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
686 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
687 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
688 348c2af1 jim-p
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
689 61e047a5 Phil Davis
				if (file_exists($configfile)) {
690 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
691 61e047a5 Phil Davis
				}
692 ee6e6011 jim-p
			}
693
			break;
694 e3f68ab9 doktornotor
		case 'syslogd':
695
			system_syslogd_start();
696
			break;
697 ee6e6011 jim-p
		default:
698
			start_service($name);
699
			break;
700
	}
701 086cf944 Phil Davis
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
702 ee6e6011 jim-p
}
703
function service_control_stop($name, $extras) {
704
	global $g;
705 61e047a5 Phil Davis
	switch ($name) {
706 ee6e6011 jim-p
		case 'radvd':
707
			killbypid("{$g['varrun_path']}/radvd.pid");
708
			break;
709
		case 'captiveportal':
710 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
711 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
712
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
713 ee6e6011 jim-p
			break;
714
		case 'ntpd':
715
			killbyname("ntpd");
716
			break;
717
		case 'openntpd':
718
			killbyname("openntpd");
719
			break;
720 69eefb50 Renato Botelho
		case 'dpinger':
721
			stop_dpinger();
722 b05a8f35 jim-p
			break;
723 ee6e6011 jim-p
		case 'bsnmpd':
724
			killbypid("{$g['varrun_path']}/snmpd.pid");
725
			break;
726
		case 'choparp':
727
			killbyname("choparp");
728
			break;
729
		case 'dhcpd':
730
			killbyname("dhcpd");
731
			break;
732
		case 'dhcrelay':
733
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
734
			break;
735 9590e0de Phil Davis
		case 'dhcrelay6':
736
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
737
			break;
738 ee6e6011 jim-p
		case 'dnsmasq':
739
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
740
			break;
741 33232486 Warren Baker
		case 'unbound':
742
			killbypid("{$g['varrun_path']}/unbound.pid");
743
			break;
744 ee6e6011 jim-p
		case 'igmpproxy':
745
			killbyname("igmpproxy");
746
			break;
747
		case 'miniupnpd':
748
			upnp_action('stop');
749
			break;
750
		case 'sshd':
751
			killbyname("sshd");
752
			break;
753 2c702751 Viktor G
		case 'pcscd':
754 8b4abd59 Ermal
		case 'ipsec':
755 c6220dcf jim-p
			exec("/usr/local/sbin/strongswanrc stop");
756 2c702751 Viktor G
			if (isvalidproc("pcscd")) {
757
				killbyname("pcscd");
758
			}
759 ee6e6011 jim-p
			break;
760
		case 'openvpn':
761 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
762 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
763 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
764 ee6e6011 jim-p
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
765
				killbypid($pidfile);
766 1f3baf61 jim-p
				openvpn_clean_rules($vpnmode, $id);
767 ee6e6011 jim-p
			}
768
			break;
769 e3f68ab9 doktornotor
		case 'syslogd':
770
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
771
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
772
				usleep(100000);
773
			}
774
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
775
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
776 726b889b doktornotor
				usleep(100000);
777 e3f68ab9 doktornotor
			}
778 b89270b7 Renato Botelho
			/* Make sure sshguard stops as well */
779
			sigkillbyname("sshguard", "TERM");
780 ee6e6011 jim-p
			break;
781
		default:
782
			stop_service($name);
783
			break;
784
	}
785
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
786
}
787 33232486 Warren Baker
788 ee6e6011 jim-p
function service_control_restart($name, $extras) {
789
	global $g;
790 61e047a5 Phil Davis
	switch ($name) {
791 ee6e6011 jim-p
		case 'radvd':
792
			services_radvd_configure();
793
			break;
794
		case 'captiveportal':
795 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
796 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
797
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
798 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
799
			break;
800
		case 'ntpd':
801
		case 'openntpd':
802
			system_ntp_configure();
803
			break;
804 69eefb50 Renato Botelho
		case 'dpinger':
805 b05a8f35 jim-p
			setup_gateways_monitor();
806
			break;
807 ee6e6011 jim-p
		case 'bsnmpd':
808
			services_snmpd_configure();
809
			break;
810 9590e0de Phil Davis
		case 'dhcrelay':
811
			services_dhcrelay_configure();
812
			break;
813
		case 'dhcrelay6':
814
			services_dhcrelay6_configure();
815
			break;
816 ee6e6011 jim-p
		case 'dnsmasq':
817
			services_dnsmasq_configure();
818
			break;
819 33232486 Warren Baker
		case 'unbound':
820
			services_unbound_configure();
821
			break;
822 ee6e6011 jim-p
		case 'dhcpd':
823
			services_dhcpd_configure();
824
			break;
825
		case 'igmpproxy':
826
			services_igmpproxy_configure();
827
			break;
828
		case 'miniupnpd':
829
			upnp_action('restart');
830
			break;
831 8b4abd59 Ermal
		case 'ipsec':
832 c6220dcf jim-p
			ipsec_force_reload();
833 ee6e6011 jim-p
			break;
834 aaa78416 jim-p
		case 'sshd':
835
			send_event("service restart sshd");
836
			break;
837 2c702751 Viktor G
		case 'pcscd':
838
			exec("/usr/local/sbin/strongswanrc stop");
839
			if (isvalidproc("pcscd")) {
840
				killbyname("pcscd");
841
			}
842
			ipsec_force_reload();
843
			break;
844 ee6e6011 jim-p
		case 'openvpn':
845 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
846 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
847 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
848 348c2af1 jim-p
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
849 61e047a5 Phil Davis
				if (file_exists($configfile)) {
850 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
851 61e047a5 Phil Davis
				}
852 ee6e6011 jim-p
			}
853
			break;
854 e3f68ab9 doktornotor
		case 'syslogd':
855
			system_syslogd_start();
856
			break;
857 ee6e6011 jim-p
		default:
858
			restart_service($name);
859
			break;
860
	}
861 086cf944 Phil Davis
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
862 ee6e6011 jim-p
}
863
864 17098641 Ermal
?>