Project

General

Profile

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