Project

General

Profile

Download (26.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * service-utils.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2005-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2025 Rubicon Communications, LLC (Netgate)
9
 * Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
10
 * All rights reserved.
11
 *
12
 * 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
 *
16
 * http://www.apache.org/licenses/LICENSE-2.0
17
 *
18
 * 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
 */
24

    
25
require_once("captiveportal.inc");
26
require_once("globals.inc");
27
require_once("gwlb.inc");
28
require_once("ipsec.inc");
29
require_once("openvpn.inc");
30
require_once("system.inc");
31
require_once("util.inc");
32
require_once("vpn.inc");
33

    
34
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
35
function write_rcfile($params) {
36
	global $g;
37

    
38
	safe_mkdir(RCFILEPREFIX);
39
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
40
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
41
		return false;
42
	}
43

    
44
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
45
		return false;
46
	}
47

    
48
	$towrite = "#!/bin/sh\n";
49
	$towrite .= "# This file was automatically generated\n# by the {$g['product_label']} service handler.\n\n";
50

    
51
	/* write our rc functions */
52
	$towrite .= "rc_start() {\n";
53
	$towrite .= "\t{$params['start']}\n";
54
	$towrite .= "}\n\n";
55
	if (!empty($params['stop'])) {
56
		$tokill = &$params['stop'];
57
	} else if (!empty($params['executable'])) {
58
		/* just nuke the executable */
59
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
60
	} else {
61
		/* make an educated guess (bad) */
62
		$tokill = explode(' ', $params['start']);
63
		$tokill = array_pop(explode('/', array_shift($tokill)));
64
	}
65
	$towrite .= "rc_stop() {\n";
66
	$towrite .= "\t{$tokill}\n";
67
	$towrite .= "}\n\n";
68
	if (!empty($params['restart'])) {
69
		$torestart = &$params['restart'];
70
	} else {
71
		$torestart = "\trc_stop\n";
72
		$torestart .= "\trc_start\n";
73
	}
74
	$towrite .= "rc_restart() {\n";
75
	$towrite .= "{$torestart}\n";
76
	$towrite .= "}\n\n";
77

    
78
	/* begin rcfile logic */
79
	$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";
80

    
81
	@file_put_contents($rcfile_fullname, $towrite);
82
	unset($towrite);
83
	@chmod("{$rcfile_fullname}", 0755);
84

    
85
	return;
86
}
87

    
88
function start_service($name, $after_sync = false) {
89
	if (empty($name)) {
90
		return;
91
	}
92

    
93
	foreach (config_get_path('installedpackages/service', []) as $service) {
94
		if (empty($service)) {
95
			continue;
96
		}
97
		if (isset($service['name']) && (strtolower($service['name']) == strtolower($name))) {
98
			/* Avoid starting twice if this is called just after a
99
			 * package sync which starts the service itself. */
100
			if ($after_sync && isset($service['starts_on_sync'])) {
101
				break;
102
			}
103
			if ($service['rcfile']) {
104
				$prefix = RCFILEPREFIX;
105
				if (!empty($service['prefix'])) {
106
					$prefix = &$service['prefix'];
107
				}
108
				if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
109
					mwexec_bg("{$prefix}{$service['rcfile']} start");
110
				}
111
			}
112
			if (!empty($service['startcmd'])) {
113
				eval($service['startcmd']);
114
			}
115
			break;
116
		}
117
	}
118
}
119

    
120
function stop_service($name) {
121
	if (empty($name)) {
122
		return;
123
	}
124

    
125
	foreach (config_get_path('installedpackages/service', []) as $service) {
126
		if (empty($service)) {
127
			continue;
128
		}
129
		if (strtolower($service['name']) == strtolower($name)) {
130
			if ($service['rcfile']) {
131
				$prefix = RCFILEPREFIX;
132
				if (!empty($service['prefix'])) {
133
					$prefix = &$service['prefix'];
134
				}
135
				if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
136
					mwexec("{$prefix}{$service['rcfile']} stop");
137
				}
138
				return;
139
			}
140
			if (!empty($service['stopcmd'])) {
141
				eval($service['stopcmd']);
142
			} elseif (!empty($service['executable'])) {
143
				mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
144
			}
145

    
146
			break;
147
		}
148
	}
149
}
150

    
151
function restart_service($name) {
152
	if (empty($name)) {
153
		return;
154
	}
155

    
156
	if (is_service_running($name)) {
157
		stop_service($name);
158
	}
159
	start_service($name);
160

    
161
	foreach (config_get_path('installedpackages/service', []) as $service) {
162
		if (empty($service)) {
163
			continue;
164
		}
165
		if (strtolower($service['name']) == strtolower($name)) {
166
			if ($service['restartcmd']) {
167
				eval($service['restartcmd']);
168
			}
169
			break;
170
		}
171
	}
172
}
173

    
174
function is_pid_running($pidfile) {
175
	if (!file_exists($pidfile)) {
176
		return false;
177
	}
178

    
179
	return (isvalidpid($pidfile));
180
}
181

    
182
function is_dhcp_running($interface) {
183
	$status = find_dhclient_process($interface);
184
	if ($status != 0) {
185
		return true;
186
	}
187
	return false;
188
}
189

    
190
function restart_service_if_running($service) {
191
	if (is_service_running($service)) {
192
		restart_service($service);
193
	}
194
	return;
195
}
196

    
197
function is_service_enabled($service_name) {
198
	switch ($service_name) {
199
		case 'bsnmpd':
200
			return config_path_enabled('snmpd');
201
			break;
202
		case 'dhcrelay':
203
			return config_path_enabled('dhcrelay');
204
			break;
205
		case 'dhcrelay6':
206
			return config_path_enabled('dhcrelay6');
207
			break;
208
		case 'dhcpd':
209
			return (dhcp_is_backend('isc') && is_dhcp_server_enabled());
210
			break;
211
		case 'kea-dhcp4':
212
			return (dhcp_is_backend('kea') && is_dhcp_server_enabled());
213
			break;
214
		case 'kea-dhcp6':
215
			return (dhcp_is_backend('kea') && is_dhcpv6_server_enabled());
216
			break;
217
		case 'dnsmasq':
218
			return config_path_enabled('dnsmasq');
219
			break;
220
		case 'dpinger':
221
			/* TODO: Should loop through gateways and check to make
222
			 *       sure they don't all have monitoring disabled. */
223
			return !empty(get_gateways());
224
			break;
225
		case 'igmpproxy':
226
			return (config_path_enabled('igmpproxy') && !empty(config_get_path('igmpproxy/igmpentry', [])));
227
			break;
228
		case 'ipsec':
229
			return ipsec_enabled();
230
			break;
231
		case 'miniupnpd':
232
			return (config_get_path('installedpackages/miniupnpd/config/0/enable') == 'on');
233
			break;
234
		case 'ntpd':
235
			return (config_get_path('ntpd/enable') != 'disabled');
236
			break;
237
		case 'pcscd':
238
			return config_path_enabled('ipsec', 'pkcs11support');
239
			break;
240
		case 'radvd':
241
			return is_radvd_enabled();
242
			break;
243
		case 'sshd':
244
			return config_path_enabled('system/ssh');
245
			break;
246
		case 'syslogd':
247
			$local_enabled = !config_path_enabled('syslog', 'disablelocallogging');
248
			$remote_enabled = !empty(system_syslogd_get_remote_servers(config_get_path('syslog', [])));
249
			return ($local_enabled || $remote_enabled);
250
			break;
251
		case 'unbound':
252
			return config_path_enabled('unbound');
253
			break;
254
		default:
255
			/* Do nothing since we can't determine for certain. */
256
	}
257
	/* TODO: The service name isn't likely to match the config tag,
258
	 *       needs better logic or pkg plugin to probe */
259
	$pkg_enabled = config_get_path("installedpackages/{$service_name}/config/0/enable", null);
260
	if (($pkg_enabled !== null) &&
261
	    (empty($pkg_enabled) || ($pkg_enabled === 'off'))) {
262
		/* Only return false for cases where the config area exists and
263
		 * appears to be disabled. */
264
		return false;
265
	}
266

    
267
	/* Unknown service, for compatibility reasons, return true. */
268
	return true;
269
}
270

    
271
function is_service_running($service, $ps = "") {
272
	foreach (config_get_path('installedpackages/service', []) as $aservice) {
273
		if (empty($aservice)) {
274
			continue;
275
		}
276
		if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) {
277
			if ($aservice['custom_php_service_status_command'] <> "") {
278
				eval("\$rc={$aservice['custom_php_service_status_command']};");
279
				return $rc;
280
			}
281
			if (empty($aservice['executable'])) {
282
				return false;
283
			}
284
			if (is_process_running($aservice['executable'])) {
285
				return true;
286
			}
287
			return false;
288
		}
289
	}
290
	if (is_process_running($service)) {
291
		return true;
292
	}
293
	return false;
294
}
295

    
296
function get_services() {
297
	$services = config_get_path('installedpackages/service', []);
298

    
299
	/* Clean up any empty services */
300
	foreach ($services as $k => &$s) {
301
		if (empty($s)) {
302
			config_del_path('installedpackages/services/' . $k);
303
			unset($s);
304
		}
305
	}
306

    
307
	/*
308
	 * Add services that are in the base.
309
	 */
310
	if (is_radvd_enabled()) {
311
		$pconfig = array();
312
		$pconfig['name'] = "radvd";
313
		$pconfig['description'] = gettext("Router Advertisement Daemon");
314
		$pconfig['enabled'] = true;
315
		$pconfig['status'] = get_service_status($pconfig);
316
		$services[] = $pconfig;
317
	}
318

    
319
	if (config_path_enabled('dnsmasq')) {
320
		$pconfig = array();
321
		$pconfig['name'] = "dnsmasq";
322
		$pconfig['description'] = gettext("DNS Forwarder");
323
		$pconfig['enabled'] = true;
324
		$pconfig['status'] = get_service_status($pconfig);
325
		$services[] = $pconfig;
326
	}
327

    
328
	if (config_path_enabled('unbound')) {
329
		$pconfig = array();
330
		$pconfig['name'] = "unbound";
331
		$pconfig['description'] = gettext("DNS Resolver");
332
		$pconfig['enabled'] = true;
333
		$pconfig['status'] = get_service_status($pconfig);
334
		$services[] = $pconfig;
335
	}
336

    
337
	if (config_path_enabled('ipsec', 'pkcs11support')) {
338
		$pconfig = array();
339
		$pconfig['name'] = "pcscd";
340
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
341
		$pconfig['enabled'] = true;
342
		$pconfig['status'] = get_service_status($pconfig);
343
		$services[] = $pconfig;
344
	}
345

    
346
	if (config_get_path('ntpd/enable') != 'disabled') {
347
		$pconfig = array();
348
		$pconfig['name'] = "ntpd";
349
		$pconfig['description'] = gettext("NTP clock sync");
350
		$pconfig['enabled'] = true;
351
		$pconfig['status'] = get_service_status($pconfig);
352
		$services[] = $pconfig;
353
	}
354

    
355
	$pconfig = array();
356
	$pconfig['name'] = "syslogd";
357
	$pconfig['description'] = gettext("System Logger Daemon");
358
	$pconfig['enabled'] = is_service_enabled("syslogd");
359
	$pconfig['status'] = get_service_status($pconfig);
360
	$services[] = $pconfig;
361

    
362
	foreach (config_get_path('captiveportal', []) as $zone => $setting) {
363
		if (empty($setting)) {
364
			continue;
365
		}
366
		if (isset($setting['enable'])) {
367
			$pconfig = array();
368
			$pconfig['name'] = "captiveportal";
369
			$pconfig['zone'] = $zone;
370
			$pconfig['enabled'] = true;
371
			$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($zone . ($setting['descr'] ? " ({$setting['descr']})" : ''));
372
			$services[] = $pconfig;
373
		}
374
	}
375

    
376
	$iflist = array();
377
	$ifdescrs = get_configured_interface_list();
378
	foreach ($ifdescrs as $if) {
379
		if (config_get_path("interfaces/{$if}/if") &&
380
		    !link_interface_to_bridge($if)) {
381
			$iflist[$if] = $if;
382
		}
383
	}
384

    
385
	if (config_path_enabled('dhcrelay')) {
386
		$pconfig = array();
387
		$pconfig['name'] = "dhcrelay";
388
		$pconfig['description'] = gettext('ISC DHCP Relay');
389
		$pconfig['enabled'] = true;
390
		$pconfig['status'] = get_service_status($pconfig);
391
		$services[] = $pconfig;
392
	}
393

    
394
	if (config_path_enabled('dhcrelay6')) {
395
		$pconfig = array();
396
		$pconfig['name'] = "dhcrelay6";
397
		$pconfig['description'] = gettext('ISC DHCPv6 Relay');
398
		$pconfig['enabled'] = true;
399
		$pconfig['status'] = get_service_status($pconfig);
400
		$services[] = $pconfig;
401
	}
402

    
403
	if (dhcp_is_backend('isc') && is_dhcp_server_enabled()) {
404
		$pconfig = array();
405
		$pconfig['name'] = "dhcpd";
406
		$pconfig['description'] = gettext('ISC DHCP Server');
407
		$pconfig['enabled'] = true;
408
		$pconfig['status'] = get_service_status($pconfig);
409
		$services[] = $pconfig;
410
	}
411

    
412
	if (dhcp_is_backend('kea') && is_dhcp_server_enabled()) {
413
		$pconfig = array();
414
		$pconfig['name'] = 'kea-dhcp4';
415
		$pconfig['description'] = gettext('Kea DHCP Server');
416
		$pconfig['enabled'] = true;
417
		$pconfig['status'] = get_service_status($pconfig);
418
		$services[] = $pconfig;
419
	}
420

    
421
	if (dhcp_is_backend('kea') && is_dhcpv6_server_enabled()) {
422
		$pconfig = array();
423
		$pconfig['name'] = 'kea-dhcp6';
424
		$pconfig['description'] = gettext('Kea DHCPv6 Server');
425
		$pconfig['enabled'] = true;
426
		$pconfig['status'] = get_service_status($pconfig);
427
		$services[] = $pconfig;
428
	}
429

    
430
	if (!empty(get_gateways())) {
431
		$pconfig = array();
432
		$pconfig['name'] = "dpinger";
433
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
434
		$pconfig['enabled'] = is_service_enabled("dpinger");
435
		$pconfig['status'] = get_service_status($pconfig);
436
		$services[] = $pconfig;
437
	}
438

    
439
	if (config_path_enabled('snmpd')) {
440
		$pconfig = array();
441
		$pconfig['name'] = "bsnmpd";
442
		$pconfig['description'] = gettext("SNMP Service");
443
		$pconfig['enabled'] = true;
444
		$pconfig['status'] = get_service_status($pconfig);
445
		$services[] = $pconfig;
446
	}
447

    
448
	if (!empty(config_get_path('igmpproxy/igmpentry', []))) {
449
		$pconfig = array();
450
		$pconfig['name'] = "igmpproxy";
451
		$pconfig['description'] = gettext("IGMP proxy");
452
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
453
		$pconfig['status'] = get_service_status($pconfig);
454
		$services[] = $pconfig;
455
	}
456

    
457
	if (config_get_path('installedpackages/miniupnpd/config/0/enable') == 'on') {
458
		$pconfig = array();
459
		$pconfig['name'] = "miniupnpd";
460
		$pconfig['description'] = gettext("UPnP IGD & PCP Service");
461
		$pconfig['enabled'] = true;
462
		$pconfig['status'] = get_service_status($pconfig);
463
		$services[] = $pconfig;
464
	}
465

    
466
	if (ipsec_enabled()) {
467
		$pconfig = array();
468
		$pconfig['name'] = "ipsec";
469
		$pconfig['description'] = gettext("IPsec VPN");
470
		$pconfig['enabled'] = true;
471
		$pconfig['status'] = get_service_status($pconfig);
472
		$services[] = $pconfig;
473
	}
474

    
475
	if (config_path_enabled('system/ssh')) {
476
		$pconfig = array();
477
		$pconfig['name'] = "sshd";
478
		$pconfig['description'] = gettext("Secure Shell Daemon");
479
		$pconfig['enabled'] = true;
480
		$pconfig['status'] = get_service_status($pconfig);
481
		$services[] = $pconfig;
482
	}
483

    
484
	foreach (array('server', 'client') as $mode) {
485
		foreach (config_get_path("openvpn/openvpn-{$mode}", []) as $id => $setting) {
486
			if (!isset($setting['disable'])) {
487
				$pconfig = array();
488
				$pconfig['name'] = "openvpn";
489
				$pconfig['mode'] = $mode;
490
				$pconfig['id'] = $id;
491
				$pconfig['vpnid'] = $setting['vpnid'];
492
				$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
493
				$pconfig['enabled'] = true;
494
				$pconfig['status'] = get_service_status($pconfig);
495
				$services[] = $pconfig;
496
			}
497
		}
498
	}
499

    
500
	return $services;
501
}
502

    
503
function find_service_by_name($name) {
504
	$services = get_services();
505
	foreach ($services as $service) {
506
		if (isset($service["name"]) && ($service["name"] == $name)) {
507
			return $service;
508
		}
509
	}
510
	return array();
511
}
512

    
513
function find_service_by_openvpn_vpnid($vpnid) {
514
	$services = get_services();
515
	foreach ($services as $service) {
516
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
517
			return $service;
518
		}
519
	}
520
	return array();
521
}
522

    
523
function find_service_by_cp_zone($zone) {
524
	$services = get_services();
525
	foreach ($services as $service) {
526
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
527
			return $service;
528
		}
529
	}
530
	return array();
531
}
532

    
533
function service_description_compare($a, $b) {
534
	if (strtolower($a['description']) == strtolower($b['description'])) {
535
		return 0;
536
	}
537
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
538
}
539

    
540
function service_name_compare($a, $b) {
541
	if (!isset($a['name']) || !isset($b['name'])) {
542
		return -1;
543
	}
544
	/* If the names are equal, fall back to sorting by description */
545
	if (strtolower($a['name']) == strtolower($b['name'])) {
546
		return service_description_compare($a, $b);
547
	}
548
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
549
}
550

    
551
function service_dispname_compare($a, $b) {
552
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
553
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
554
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
555
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
556
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
557
			if ($adn2 == $bdn2) {
558
				return 0;
559
			}
560
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
561
		}
562
	}
563
	/* If the display names are equal, compare the internal name */
564
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
565
		return service_name_compare($a, $b);
566
	}
567
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
568
}
569

    
570
function get_pkg_descr($package_name) {
571
	foreach (config_get_path('installedpackages/package', []) as $pkg) {
572
		if ($pkg['name'] == $package_name) {
573
			return $pkg['descr'];
574
		}
575
	}
576
	return gettext("Not available.");
577
}
578

    
579
function get_service_status($service) {
580
	global $g;
581
	switch ($service['name']) {
582
		case "openvpn":
583
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
584
			break;
585
		case "captiveportal":
586
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
587
			if (config_path_enabled("captiveportal/{$service['zone']}", 'httpslogin')) {
588
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
589
			}
590
			break;
591
		case "vhosts-http":
592
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
593
			break;
594
		case "dhcrelay6":
595
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
596
			break;
597
		case 'ipsec':
598
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
599
			break;
600
		case 'kea-dhcp4':
601
			$running = is_pid_running('/var/run/kea/kea-dhcp4.kea-dhcp4.pid');
602
			break;
603
		case 'kea-dhcp6':
604
			$running = is_pid_running('/var/run/kea/kea-dhcp6.kea-dhcp6.pid');
605
			break;
606
		default:
607
			$running = is_service_running($service['name']);
608
	}
609
	return $running;
610
}
611

    
612
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
613
	$output = "";
614

    
615
	if (get_service_status($service)) {
616
		$statustext = gettext("Running");
617
		$text_class = "text-success";
618
		$fa_class = "fa-solid fa-check-circle";
619
		$fa_class_thumbs = "fa-regular fa-thumbs-up";
620
		$Thumbs_UpDown = "Thumbs up";
621
	} else {
622
		if (is_service_enabled($service['name'])) {
623
			$statustext = gettext("Stopped");
624
			$text_class = "text-danger";
625
			$fa_class = "fa-solid fa-times-circle";
626
		} else {
627
			$statustext = gettext("Disabled");
628
			$text_class = "text-warning";
629
			$fa_class = "fa-solid fa-ban";
630
		}
631
		$fa_class_thumbs = "fa-regular fa-thumbs-down";
632
		$Thumbs_UpDown = "Thumbs down";
633
	}
634
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
635

    
636
	if ($title == "state") {
637
		$title = $statustext;
638
	} elseif ($title == "service_state") {
639
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
640
	} elseif ($title == "description_state") {
641
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
642
	} elseif ($title == "description_service_state") {
643
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
644
	}
645

    
646
	$spacer = ($withthumbs || $withtext) ? " " : "";
647

    
648
	$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"{$title}\"><span style=\"display: none\">{$statustext}</span></i>{$spacer}";
649

    
650
	$spacer = ($withtext) ? " " : "";
651
	if ($withthumbs) {
652
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
653
	}
654

    
655
	if ($withtext) {
656
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
657
	}
658

    
659
	return $output;
660
}
661

    
662
function get_service_control_links($service, $addname = false) {
663
	global $g;
664
	$output = "";
665
	$stitle = ($addname) ? $service['name'] . " " : "";
666

    
667
	switch ($service['name']) {
668
		case "openvpn":
669
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
670
		break;
671
		case "captiveportal":
672
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
673
		break;
674
		default:
675
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
676
	}
677

    
678
	if (get_service_status($service)) {
679
		switch ($service['name']) {
680
			case "openvpn":
681
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
682
				break;
683
			case "captiveportal":
684
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
685
				break;
686
			default:
687
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
688
		}
689

    
690
		$output .= "<i class=\"fa-solid fa-arrow-rotate-right\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
691

    
692
		switch ($service['name']) {
693
			case "openvpn":
694
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
695
				break;
696
			case "captiveportal":
697
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
698
				break;
699
			default:
700
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
701
		}
702

    
703
		$output .= "<i class=\"fa-regular fa-circle-stop\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
704

    
705
	} else {
706
		$service_enabled = is_service_enabled($service['name']);
707

    
708
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
709
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
710
			$output .= '<i class="fa-solid fa-play-circle"></i></a> ';
711
		}
712
	}
713

    
714
	return $output;
715
}
716

    
717
function service_control_start($name, $extras) {
718
	global $g;
719
	switch ($name) {
720
		case 'radvd':
721
			services_radvd_configure();
722
			break;
723
		case 'captiveportal':
724
			$zone = htmlspecialchars($extras['zone']);
725
			captiveportal_init_webgui_zonename($zone);
726
			break;
727
		case 'ntpd':
728
			system_ntp_configure();
729
			break;
730
		case 'dpinger':
731
			setup_gateways_monitor();
732
			break;
733
		case 'bsnmpd':
734
			services_snmpd_configure();
735
			break;
736
		case 'dhcrelay':
737
			services_dhcrelay_configure();
738
			break;
739
		case 'dhcrelay6':
740
			services_dhcrelay6_configure();
741
			break;
742
		case 'dnsmasq':
743
			services_dnsmasq_configure();
744
			break;
745
		case 'unbound':
746
			services_unbound_configure();
747
			break;
748
		case 'kea-dhcp4':
749
		case 'kea-dhcp6':
750
		case 'dhcpd':
751
			services_dhcpd_configure();
752
			break;
753
		case 'igmpproxy':
754
			services_igmpproxy_configure();
755
			break;
756
		case 'miniupnpd':
757
			upnp_action('start');
758
			break;
759
		case 'ipsec':
760
			ipsec_force_reload();
761
			break;
762
		case 'sshd':
763
			send_event("service restart sshd");
764
			break;
765
		case 'pcscd':
766
			ipsec_force_reload();
767
			break;
768
		case 'openvpn':
769
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
770
			if (($vpnmode == "server") || ($vpnmode == "client")) {
771
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
772
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
773
				if (file_exists($configfile)) {
774
					openvpn_restart_by_vpnid($vpnmode, $id);
775
				}
776
			}
777
			break;
778
		case 'syslogd':
779
			system_syslogd_start();
780
			break;
781
		default:
782
			start_service($name);
783
			break;
784
	}
785
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
786
}
787
function service_control_stop($name, $extras) {
788
	global $g;
789
	switch ($name) {
790
		case 'radvd':
791
			killbypid("{$g['varrun_path']}/radvd.pid");
792
			break;
793
		case 'captiveportal':
794
			$zone = htmlspecialchars($extras['zone']);
795
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
796
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
797
			break;
798
		case 'ntpd':
799
			killbyname("ntpd");
800
			break;
801
		case 'openntpd':
802
			killbyname("openntpd");
803
			break;
804
		case 'dpinger':
805
			stop_dpinger();
806
			break;
807
		case 'bsnmpd':
808
			killbypid("{$g['varrun_path']}/snmpd.pid");
809
			break;
810
		case 'choparp':
811
			killbyname("choparp");
812
			break;
813
		case 'kea-dhcp4':
814
			killbypid('/var/run/kea/kea-dhcp4.kea-dhcp4.pid');
815
			break;
816
		case 'kea-dhcp6':
817
			killbypid('/var/run/kea/kea-dhcp6.kea-dhcp6.pid');
818
			break;
819
		case 'dhcpd':
820
			killbyname("dhcpd");
821
			break;
822
		case 'dhcrelay':
823
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
824
			break;
825
		case 'dhcrelay6':
826
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
827
			break;
828
		case 'dnsmasq':
829
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
830
			break;
831
		case 'unbound':
832
			killbypid("{$g['varrun_path']}/unbound.pid");
833
			break;
834
		case 'igmpproxy':
835
			killbyname("igmpproxy");
836
			break;
837
		case 'miniupnpd':
838
			upnp_action('stop');
839
			break;
840
		case 'sshd':
841
			killbyname("sshd");
842
			break;
843
		case 'pcscd':
844
		case 'ipsec':
845
			exec("/usr/local/sbin/strongswanrc stop");
846
			if (isvalidproc("pcscd")) {
847
				killbyname("pcscd");
848
			}
849
			break;
850
		case 'openvpn':
851
			$vpnmode = htmlspecialchars($extras['vpnmode']);
852
			if (($vpnmode == "server") or ($vpnmode == "client")) {
853
				$id = htmlspecialchars($extras['id']);
854
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
855
				killbypid($pidfile);
856
				openvpn_delete_tmp($vpnmode, $id);
857
			}
858
			break;
859
		case 'syslogd':
860
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
861
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
862
				usleep(100000);
863
			}
864
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
865
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
866
				usleep(100000);
867
			}
868
			/* Make sure sshguard stops as well */
869
			system_sshguard_stop();
870
			break;
871
		default:
872
			stop_service($name);
873
			break;
874
	}
875
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
876
}
877

    
878
function service_control_restart($name, $extras) {
879
	global $g;
880
	switch ($name) {
881
		case 'radvd':
882
			services_radvd_configure();
883
			break;
884
		case 'captiveportal':
885
			$zone = htmlspecialchars($extras['zone']);
886
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
887
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
888
			/* see https://redmine.pfsense.org/issues/12651 */
889
			sleep(1);
890
			captiveportal_init_webgui_zonename($zone);
891
			break;
892
		case 'ntpd':
893
		case 'openntpd':
894
			system_ntp_configure();
895
			break;
896
		case 'dpinger':
897
			setup_gateways_monitor();
898
			break;
899
		case 'bsnmpd':
900
			services_snmpd_configure();
901
			break;
902
		case 'dhcrelay':
903
			services_dhcrelay_configure();
904
			break;
905
		case 'dhcrelay6':
906
			services_dhcrelay6_configure();
907
			break;
908
		case 'dnsmasq':
909
			services_dnsmasq_configure();
910
			break;
911
		case 'unbound':
912
			services_unbound_configure();
913
			break;
914
		case 'kea-dhcp4':
915
		case 'kea-dhcp6':
916
		case 'dhcpd':
917
			services_dhcpd_configure();
918
			break;
919
		case 'igmpproxy':
920
			services_igmpproxy_configure();
921
			break;
922
		case 'miniupnpd':
923
			upnp_action('restart');
924
			break;
925
		case 'ipsec':
926
			ipsec_force_reload();
927
			break;
928
		case 'sshd':
929
			send_event("service restart sshd");
930
			break;
931
		case 'pcscd':
932
			exec("/usr/local/sbin/strongswanrc stop");
933
			if (isvalidproc("pcscd")) {
934
				killbyname("pcscd");
935
			}
936
			ipsec_force_reload();
937
			break;
938
		case 'openvpn':
939
			$vpnmode = htmlspecialchars($extras['vpnmode']);
940
			if (($vpnmode == "server") || ($vpnmode == "client")) {
941
				$id = htmlspecialchars($extras['id']);
942
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
943
				if (file_exists($configfile)) {
944
					openvpn_restart_by_vpnid($vpnmode, $id);
945
				}
946
			}
947
			break;
948
		case 'syslogd':
949
			system_syslogd_start();
950
			break;
951
		default:
952
			restart_service($name);
953
			break;
954
	}
955
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
956
}
(46-46/61)