Project

General

Profile

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