Project

General

Profile

Download (25.4 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-2022 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 = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
63
	}
64
	$towrite .= "rc_stop() {\n";
65
	$towrite .= "\t{$tokill}\n";
66
	$towrite .= "}\n\n";
67
	if (!empty($params['restart'])) {
68
		$torestart = &$params['restart'];
69
	} else {
70
		$torestart = "\trc_stop\n";
71
		$torestart .= "\trc_start\n";
72
	}
73
	$towrite .= "rc_restart() {\n";
74
	$towrite .= "{$torestart}\n";
75
	$towrite .= "}\n\n";
76

    
77
	/* begin rcfile logic */
78
	$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

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

    
84
	return;
85
}
86

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

    
92
	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
				}
107
				if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
108
					mwexec_bg("{$prefix}{$service['rcfile']} start");
109
				}
110
			}
111
			if (!empty($service['startcmd'])) {
112
				eval($service['startcmd']);
113
			}
114
			break;
115
		}
116
	}
117
}
118

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

    
124
	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
				}
134
				if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
135
					mwexec("{$prefix}{$service['rcfile']} stop");
136
				}
137
				return;
138
			}
139
			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
		}
147
	}
148
}
149

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

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

    
160
	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
			}
168
			break;
169
		}
170
	}
171
}
172

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

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

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

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

    
196
function is_service_enabled($service_name) {
197
	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
	}
251
	/* 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
		return false;
257
	}
258

    
259
	/* Unknown service, for compatibility reasons, return true. */
260
	return true;
261
}
262

    
263
function is_service_running($service, $ps = "") {
264
	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
				return false;
275
			}
276
			if (is_process_running($aservice['executable'])) {
277
				return true;
278
			}
279
			return false;
280
		}
281
	}
282
	if (is_process_running($service)) {
283
		return true;
284
	}
285
	return false;
286
}
287

    
288
function get_services() {
289
	$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
	}
298

    
299
	/*
300
	 * Add services that are in the base.
301
	 */
302
	if (is_radvd_enabled()) {
303
		$pconfig = array();
304
		$pconfig['name'] = "radvd";
305
		$pconfig['description'] = gettext("Router Advertisement Daemon");
306
		$pconfig['enabled'] = true;
307
		$pconfig['status'] = get_service_status($pconfig);
308
		$services[] = $pconfig;
309
	}
310

    
311
	if (config_path_enabled('dnsmasq')) {
312
		$pconfig = array();
313
		$pconfig['name'] = "dnsmasq";
314
		$pconfig['description'] = gettext("DNS Forwarder");
315
		$pconfig['enabled'] = true;
316
		$pconfig['status'] = get_service_status($pconfig);
317
		$services[] = $pconfig;
318
	}
319

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

    
329
	if (config_path_enabled('ipsec', 'pkcs11support')) {
330
		$pconfig = array();
331
		$pconfig['name'] = "pcscd";
332
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
333
		$pconfig['enabled'] = true;
334
		$pconfig['status'] = get_service_status($pconfig);
335
		$services[] = $pconfig;
336
	}
337

    
338
	if (config_get_path('ntpd/enable') != 'disabled') {
339
		$pconfig = array();
340
		$pconfig['name'] = "ntpd";
341
		$pconfig['description'] = gettext("NTP clock sync");
342
		$pconfig['enabled'] = true;
343
		$pconfig['status'] = get_service_status($pconfig);
344
		$services[] = $pconfig;
345
	}
346

    
347
	$pconfig = array();
348
	$pconfig['name'] = "syslogd";
349
	$pconfig['description'] = gettext("System Logger Daemon");
350
	$pconfig['enabled'] = is_service_enabled("syslogd");
351
	$pconfig['status'] = get_service_status($pconfig);
352
	$services[] = $pconfig;
353

    
354
	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
			$pconfig['enabled'] = true;
363
			$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
364
			$services[] = $pconfig;
365
		}
366
	}
367

    
368
	$iflist = array();
369
	$ifdescrs = get_configured_interface_list();
370
	foreach ($ifdescrs as $if) {
371
		if (config_get_path("interfaces/{$if}/if") &&
372
		    !link_interface_to_bridge($if)) {
373
			$iflist[$if] = $if;
374
		}
375
	}
376

    
377
	if (config_path_enabled('dhcrelay')) {
378
		$pconfig = array();
379
		$pconfig['name'] = "dhcrelay";
380
		$pconfig['description'] = gettext("DHCP Relay");
381
		$pconfig['enabled'] = true;
382
		$pconfig['status'] = get_service_status($pconfig);
383
		$services[] = $pconfig;
384
	}
385

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

    
395
	if (is_dhcp_server_enabled()) {
396
		$pconfig = array();
397
		$pconfig['name'] = "dhcpd";
398
		$pconfig['description'] = gettext("DHCP Service");
399
		$pconfig['enabled'] = true;
400
		$pconfig['status'] = get_service_status($pconfig);
401
		$services[] = $pconfig;
402
	}
403

    
404
	$gateways_arr = return_gateways_array();
405
	if (is_array($gateways_arr) && !empty($gateways_arr)) {
406
		$pconfig = array();
407
		$pconfig['name'] = "dpinger";
408
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
409
		$pconfig['enabled'] = is_service_enabled("dpinger");
410
		$pconfig['status'] = get_service_status($pconfig);
411
		$services[] = $pconfig;
412
	}
413

    
414
	if (config_path_enabled('snmpd')) {
415
		$pconfig = array();
416
		$pconfig['name'] = "bsnmpd";
417
		$pconfig['description'] = gettext("SNMP Service");
418
		$pconfig['enabled'] = true;
419
		$pconfig['status'] = get_service_status($pconfig);
420
		$services[] = $pconfig;
421
	}
422

    
423
	if (!empty(config_get_path('igmpproxy/igmpentry', []))) {
424
		$pconfig = array();
425
		$pconfig['name'] = "igmpproxy";
426
		$pconfig['description'] = gettext("IGMP proxy");
427
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
428
		$pconfig['status'] = get_service_status($pconfig);
429
		$services[] = $pconfig;
430
	}
431

    
432
	if (config_path_enabled('installedpackages/miniupnpd/config/0')) {
433
		$pconfig = array();
434
		$pconfig['name'] = "miniupnpd";
435
		$pconfig['description'] = gettext("UPnP Service");
436
		$pconfig['enabled'] = true;
437
		$pconfig['status'] = get_service_status($pconfig);
438
		$services[] = $pconfig;
439
	}
440

    
441
	if (ipsec_enabled()) {
442
		$pconfig = array();
443
		$pconfig['name'] = "ipsec";
444
		$pconfig['description'] = gettext("IPsec VPN");
445
		$pconfig['enabled'] = true;
446
		$pconfig['status'] = get_service_status($pconfig);
447
		$services[] = $pconfig;
448
	}
449

    
450
	if (config_path_enabled('system/ssh')) {
451
		$pconfig = array();
452
		$pconfig['name'] = "sshd";
453
		$pconfig['description'] = gettext("Secure Shell Daemon");
454
		$pconfig['enabled'] = true;
455
		$pconfig['status'] = get_service_status($pconfig);
456
		$services[] = $pconfig;
457
	}
458

    
459
	foreach (array('server', 'client') as $mode) {
460
		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
				$pconfig['enabled'] = true;
469
				$pconfig['status'] = get_service_status($pconfig);
470
				$services[] = $pconfig;
471
			}
472
		}
473
	}
474

    
475
	return $services;
476
}
477

    
478
function find_service_by_name($name) {
479
	$services = get_services();
480
	
481
	foreach ($services as $service) {
482
		if (isset($service["name"]) && ($service["name"] == $name)) {
483
			return $service;
484
		}
485
	}
486
	return array();
487
}
488

    
489
function find_service_by_openvpn_vpnid($vpnid) {
490
	$services = get_services();
491
	foreach ($services as $service) {
492
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
493
			return $service;
494
		}
495
	}
496
	return array();
497
}
498

    
499
function find_service_by_cp_zone($zone) {
500
	$services = get_services();
501
	foreach ($services as $service) {
502
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
503
			return $service;
504
		}
505
	}
506
	return array();
507
}
508

    
509
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
function service_name_compare($a, $b) {
517
	if (!isset($a['name']) || !isset($b['name'])) {
518
		return -1;
519
	}
520
	/* If the names are equal, fall back to sorting by description */
521
	if (strtolower($a['name']) == strtolower($b['name'])) {
522
		return service_description_compare($a, $b);
523
	}
524
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
525
}
526

    
527
function service_dispname_compare($a, $b) {
528
	/* 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
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
541
		return service_name_compare($a, $b);
542
	}
543
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
544
}
545

    
546
function get_pkg_descr($package_name) {
547
	foreach (config_get_path('installedpackages/package', []) as $pkg) {
548
		if ($pkg['name'] == $package_name) {
549
			return $pkg['descr'];
550
		}
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
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
563
			if (config_path_enabled("captiveportal/{$service['zone']}", 'httpslogin')) {
564
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
565
			}
566
			break;
567
		case "vhosts-http":
568
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
569
			break;
570
		case "dhcrelay6":
571
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
572
			break;
573
		case 'ipsec':
574
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
575
			break;
576
		default:
577
			$running = is_service_running($service['name']);
578
	}
579
	return $running;
580
}
581

    
582
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
583
	$output = "";
584

    
585
	if (get_service_status($service)) {
586
		$statustext = gettext("Running");
587
		$text_class = "text-success";
588
		$fa_class = "fa fa-check-circle";
589
		$fa_class_thumbs = "fa fa-thumbs-o-up";
590
		$Thumbs_UpDown = "Thumbs up";
591
	} else {
592
		if (is_service_enabled($service['name'])) {
593
			$statustext = gettext("Stopped");
594
			$text_class = "text-danger";
595
			$fa_class = "fa fa-times-circle";
596
		} else {
597
			$statustext = gettext("Disabled");
598
			$text_class = "text-warning";
599
			$fa_class = "fa fa-ban";
600
		}
601
		$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
		$title = $statustext;
608
	} elseif ($title == "service_state") {
609
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
610
	} elseif ($title == "description_state") {
611
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
612
	} elseif ($title == "description_service_state") {
613
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
614
	}
615

    
616
	$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
	$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
	return $output;
630
}
631

    
632
function get_service_control_links($service, $addname = false) {
633
	global $g;
634
	$output = "";
635
	$stitle = ($addname) ? $service['name'] . " " : "";
636

    
637
	switch ($service['name']) {
638
		case "openvpn":
639
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
640
		break;
641
		case "captiveportal":
642
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
643
		break;
644
		default:
645
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
646
	}
647

    
648
	if (get_service_status($service)) {
649
		switch ($service['name']) {
650
			case "openvpn":
651
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
652
				break;
653
			case "captiveportal":
654
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
655
				break;
656
			default:
657
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
658
		}
659

    
660
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
661

    
662
		switch ($service['name']) {
663
			case "openvpn":
664
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
665
				break;
666
			case "captiveportal":
667
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
668
				break;
669
			default:
670
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
671
		}
672

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

    
675
	} else {
676
		$service_enabled = is_service_enabled($service['name']);
677

    
678
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
679
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
680
			$output .= '<i class="fa fa-play-circle"></i></a> ';
681
		}
682
	}
683

    
684
	return $output;
685
}
686

    
687
function service_control_start($name, $extras) {
688
	global $g;
689
	switch ($name) {
690
		case 'radvd':
691
			services_radvd_configure();
692
			break;
693
		case 'captiveportal':
694
			$zone = htmlspecialchars($extras['zone']);
695
			captiveportal_init_webgui_zonename($zone);
696
			break;
697
		case 'ntpd':
698
			system_ntp_configure();
699
			break;
700
		case 'dpinger':
701
			setup_gateways_monitor();
702
			break;
703
		case 'bsnmpd':
704
			services_snmpd_configure();
705
			break;
706
		case 'dhcrelay':
707
			services_dhcrelay_configure();
708
			break;
709
		case 'dhcrelay6':
710
			services_dhcrelay6_configure();
711
			break;
712
		case 'dnsmasq':
713
			services_dnsmasq_configure();
714
			break;
715
		case 'unbound':
716
			services_unbound_configure();
717
			break;
718
		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
		case 'ipsec':
728
			ipsec_force_reload();
729
			break;
730
		case 'sshd':
731
			send_event("service restart sshd");
732
			break;
733
		case 'pcscd':
734
			ipsec_force_reload();
735
			break;
736
		case 'openvpn':
737
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
738
			if (($vpnmode == "server") || ($vpnmode == "client")) {
739
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
740
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
741
				if (file_exists($configfile)) {
742
					openvpn_restart_by_vpnid($vpnmode, $id);
743
				}
744
			}
745
			break;
746
		case 'syslogd':
747
			system_syslogd_start();
748
			break;
749
		default:
750
			start_service($name);
751
			break;
752
	}
753
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
754
}
755
function service_control_stop($name, $extras) {
756
	global $g;
757
	switch ($name) {
758
		case 'radvd':
759
			killbypid("{$g['varrun_path']}/radvd.pid");
760
			break;
761
		case 'captiveportal':
762
			$zone = htmlspecialchars($extras['zone']);
763
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
764
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
765
			break;
766
		case 'ntpd':
767
			killbyname("ntpd");
768
			break;
769
		case 'openntpd':
770
			killbyname("openntpd");
771
			break;
772
		case 'dpinger':
773
			stop_dpinger();
774
			break;
775
		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
		case 'dhcrelay6':
788
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
789
			break;
790
		case 'dnsmasq':
791
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
792
			break;
793
		case 'unbound':
794
			killbypid("{$g['varrun_path']}/unbound.pid");
795
			break;
796
		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
		case 'pcscd':
806
		case 'ipsec':
807
			exec("/usr/local/sbin/strongswanrc stop");
808
			if (isvalidproc("pcscd")) {
809
				killbyname("pcscd");
810
			}
811
			break;
812
		case 'openvpn':
813
			$vpnmode = htmlspecialchars($extras['vpnmode']);
814
			if (($vpnmode == "server") or ($vpnmode == "client")) {
815
				$id = htmlspecialchars($extras['id']);
816
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
817
				killbypid($pidfile);
818
				openvpn_delete_tmp($vpnmode, $id);
819
			}
820
			break;
821
		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
				usleep(100000);
829
			}
830
			/* Make sure sshguard stops as well */
831
			sigkillbyname("sshguard", "TERM");
832
			break;
833
		default:
834
			stop_service($name);
835
			break;
836
	}
837
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
838
}
839

    
840
function service_control_restart($name, $extras) {
841
	global $g;
842
	switch ($name) {
843
		case 'radvd':
844
			services_radvd_configure();
845
			break;
846
		case 'captiveportal':
847
			$zone = htmlspecialchars($extras['zone']);
848
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
849
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
850
			/* see https://redmine.pfsense.org/issues/12651 */
851
			sleep(1);
852
			captiveportal_init_webgui_zonename($zone);
853
			break;
854
		case 'ntpd':
855
		case 'openntpd':
856
			system_ntp_configure();
857
			break;
858
		case 'dpinger':
859
			setup_gateways_monitor();
860
			break;
861
		case 'bsnmpd':
862
			services_snmpd_configure();
863
			break;
864
		case 'dhcrelay':
865
			services_dhcrelay_configure();
866
			break;
867
		case 'dhcrelay6':
868
			services_dhcrelay6_configure();
869
			break;
870
		case 'dnsmasq':
871
			services_dnsmasq_configure();
872
			break;
873
		case 'unbound':
874
			services_unbound_configure();
875
			break;
876
		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
		case 'ipsec':
886
			ipsec_force_reload();
887
			break;
888
		case 'sshd':
889
			send_event("service restart sshd");
890
			break;
891
		case 'pcscd':
892
			exec("/usr/local/sbin/strongswanrc stop");
893
			if (isvalidproc("pcscd")) {
894
				killbyname("pcscd");
895
			}
896
			ipsec_force_reload();
897
			break;
898
		case 'openvpn':
899
			$vpnmode = htmlspecialchars($extras['vpnmode']);
900
			if (($vpnmode == "server") || ($vpnmode == "client")) {
901
				$id = htmlspecialchars($extras['id']);
902
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
903
				if (file_exists($configfile)) {
904
					openvpn_restart_by_vpnid($vpnmode, $id);
905
				}
906
			}
907
			break;
908
		case 'syslogd':
909
			system_syslogd_start();
910
			break;
911
		default:
912
			restart_service($name);
913
			break;
914
	}
915
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
916
}
917

    
918
?>
(46-46/62)