Project

General

Profile

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