Project

General

Profile

Download (24.7 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
	global $config;
89

    
90
	if (empty($name)) {
91
		return;
92
	}
93

    
94
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
95
		foreach ($config['installedpackages']['service'] as $service) {
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

    
120
function stop_service($name) {
121
	global $config;
122

    
123
	if (empty($name)) {
124
		return;
125
	}
126

    
127
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
128
		foreach ($config['installedpackages']['service'] as $service) {
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

    
152
function restart_service($name) {
153
	global $config;
154

    
155
	if (empty($name)) {
156
		return;
157
	}
158

    
159
	if (is_service_running($name)) {
160
		stop_service($name);
161
	}
162
	start_service($name);
163

    
164
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
165
		foreach ($config['installedpackages']['service'] as $service) {
166
			if (strtolower($service['name']) == strtolower($name)) {
167
				if ($service['restartcmd']) {
168
					eval($service['restartcmd']);
169
				}
170
				break;
171
			}
172
		}
173
	}
174
}
175

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

    
181
	return (isvalidpid($pidfile));
182
}
183

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

    
192
function restart_service_if_running($service) {
193
	global $config;
194
	if (is_service_running($service)) {
195
		restart_service($service);
196
	}
197
	return;
198
}
199

    
200
function is_service_enabled($service_name) {
201
	global $config;
202
	if ($service_name == "") {
203
		return false;
204
	}
205
	if (is_array($config['installedpackages'])) {
206
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
207
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
208
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
209
			return false;
210
		}
211
	}
212
	return true;
213
}
214

    
215
function is_service_running($service, $ps = "") {
216
	global $config;
217

    
218
	if (is_array($config['installedpackages']['service'])) {
219
		foreach ($config['installedpackages']['service'] as $aservice) {
220
			if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) {
221
				if ($aservice['custom_php_service_status_command'] <> "") {
222
					eval("\$rc={$aservice['custom_php_service_status_command']};");
223
					return $rc;
224
				}
225
				if (empty($aservice['executable'])) {
226
					return false;
227
				}
228
				if (is_process_running($aservice['executable'])) {
229
					return true;
230
				}
231

    
232
				return false;
233
			}
234
		}
235
	}
236

    
237
	if (is_process_running($service)) {
238
		return true;
239
	}
240

    
241
	return false;
242
}
243

    
244
function get_services() {
245
	global $config;
246
	if (is_array($config['installedpackages']['service'])) {
247
		$services = $config['installedpackages']['service'];
248
	} else {
249
		$services = array();
250
	}
251

    
252
	/*
253
	 * Add services that are in the base.
254
	 */
255
	if (is_radvd_enabled()) {
256
		$pconfig = array();
257
		$pconfig['name'] = "radvd";
258
		$pconfig['description'] = gettext("Router Advertisement Daemon");
259
		$pconfig['enabled'] = is_service_enabled("radvd");
260
		$pconfig['status'] = get_service_status($pconfig);
261
		$services[] = $pconfig;
262
	}
263

    
264
	if (isset($config['dnsmasq']['enable'])) {
265
		$pconfig = array();
266
		$pconfig['name'] = "dnsmasq";
267
		$pconfig['description'] = gettext("DNS Forwarder");
268
		$pconfig['enabled'] = is_service_enabled("dnsmasq");
269
		$pconfig['status'] = get_service_status($pconfig);
270
		$services[] = $pconfig;
271
	}
272

    
273
	if (isset($config['unbound']['enable'])) {
274
		$pconfig = array();
275
		$pconfig['name'] = "unbound";
276
		$pconfig['description'] = gettext("DNS Resolver");
277
		$pconfig['enabled'] = is_service_enabled("unbound");
278
		$pconfig['status'] = get_service_status($pconfig);
279
		$services[] = $pconfig;
280
	}
281

    
282
	if (isset($config['ipsec']['pkcs11support'])) {
283
		$pconfig = array();
284
		$pconfig['name'] = "pcscd";
285
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
286
		$pconfig['enabled'] = is_service_enabled("pcscd");
287
		$pconfig['status'] = get_service_status($pconfig);
288
		$services[] = $pconfig;
289
	}
290

    
291
	init_config_arr(array('ntpd'));
292
	if ($config['ntpd']['enable'] != 'disabled') {
293
		$pconfig = array();
294
		$pconfig['name'] = "ntpd";
295
		$pconfig['description'] = gettext("NTP clock sync");
296
		$pconfig['enabled'] = is_service_enabled("ntpd");
297
		$pconfig['status'] = get_service_status($pconfig);
298
		$services[] = $pconfig;
299
	}
300

    
301
	$pconfig = array();
302
	$pconfig['name'] = "syslogd";
303
	$pconfig['description'] = gettext("System Logger Daemon");
304
	$pconfig['enabled'] = is_service_enabled("syslogd");
305
	$pconfig['status'] = get_service_status($pconfig);
306
	$services[] = $pconfig;
307

    
308
	if (is_array($config['captiveportal'])) {
309
		foreach ($config['captiveportal'] as $zone => $setting) {
310
			if (isset($setting['enable'])) {
311
				$pconfig = array();
312
				$pconfig['name'] = "captiveportal";
313
				$pconfig['zone'] = $zone;
314
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
315
				$services[] = $pconfig;
316
			}
317
		}
318
	}
319

    
320
	$iflist = array();
321
	$ifdescrs = get_configured_interface_list();
322
	foreach ($ifdescrs as $if) {
323
		$oc = $config['interfaces'][$if];
324
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
325
			$iflist[$if] = $if;
326
		}
327
	}
328

    
329
	if (isset($config['dhcrelay']['enable'])) {
330
		$pconfig = array();
331
		$pconfig['name'] = "dhcrelay";
332
		$pconfig['description'] = gettext("DHCP Relay");
333
		$pconfig['enabled'] = is_service_enabled("dhcprelay");
334
		$pconfig['status'] = get_service_status($pconfig);
335
		$services[] = $pconfig;
336
	}
337

    
338
	if (isset($config['dhcrelay6']['enable'])) {
339
		$pconfig = array();
340
		$pconfig['name'] = "dhcrelay6";
341
		$pconfig['description'] = gettext("DHCPv6 Relay");
342
		$pconfig['enabled'] = is_service_enabled("dhcrelay6");
343
		$pconfig['status'] = get_service_status($pconfig);
344
		$services[] = $pconfig;
345
	}
346

    
347
	if (is_dhcp_server_enabled()) {
348
		$pconfig = array();
349
		$pconfig['name'] = "dhcpd";
350
		$pconfig['description'] = gettext("DHCP Service");
351
		$pconfig['enabled'] = is_service_enabled("dhcpd");
352
		$pconfig['status'] = get_service_status($pconfig);
353
		$services[] = $pconfig;
354
	}
355

    
356
	$gateways_arr = return_gateways_array();
357
	if (is_array($gateways_arr)) {
358
		$pconfig = array();
359
		$pconfig['name'] = "dpinger";
360
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
361
		$pconfig['enabled'] = is_service_enabled("dpinger");
362
		$pconfig['status'] = get_service_status($pconfig);
363
		$services[] = $pconfig;
364
	}
365

    
366
	if (isset($config['snmpd']['enable'])) {
367
		$pconfig = array();
368
		$pconfig['name'] = "bsnmpd";
369
		$pconfig['description'] = gettext("SNMP Service");
370
		$pconfig['enabled'] = is_service_enabled("bsnmpd");
371
		$pconfig['status'] = get_service_status($pconfig);
372
		$services[] = $pconfig;
373
	}
374

    
375
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
376
		$pconfig = array();
377
		$pconfig['name'] = "igmpproxy";
378
		$pconfig['description'] = gettext("IGMP proxy");
379
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
380
		$pconfig['status'] = get_service_status($pconfig);
381
		$services[] = $pconfig;
382
	}
383

    
384
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
385
		$pconfig = array();
386
		$pconfig['name'] = "miniupnpd";
387
		$pconfig['description'] = gettext("UPnP Service");
388
		$pconfig['enabled'] = is_service_enabled("miniupnpd");
389
		$pconfig['status'] = get_service_status($pconfig);
390
		$services[] = $pconfig;
391
	}
392

    
393
	if (ipsec_enabled()) {
394
		$pconfig = array();
395
		$pconfig['name'] = "ipsec";
396
		$pconfig['description'] = gettext("IPsec VPN");
397
		$pconfig['enabled'] = is_service_enabled("ipsec");
398
		$pconfig['status'] = get_service_status($pconfig);
399
		$services[] = $pconfig;
400
	}
401

    
402
	if (isset($config['system']['ssh']['enable'])) {
403
		$pconfig = array();
404
		$pconfig['name'] = "sshd";
405
		$pconfig['description'] = gettext("Secure Shell Daemon");
406
		$pconfig['enabled'] = is_service_enabled("sshd");
407
		$pconfig['status'] = get_service_status($pconfig);
408
		$services[] = $pconfig;
409
	}
410

    
411
	foreach (array('server', 'client') as $mode) {
412
		init_config_arr(array('openvpn'));
413
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
414
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
415
				if (!isset($setting['disable'])) {
416
					$pconfig = array();
417
					$pconfig['name'] = "openvpn";
418
					$pconfig['mode'] = $mode;
419
					$pconfig['id'] = $id;
420
					$pconfig['vpnid'] = $setting['vpnid'];
421
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
422
					$pconfig['enabled'] = is_service_enabled("openvpn");
423
					$pconfig['status'] = get_service_status($pconfig);
424
					$services[] = $pconfig;
425
				}
426
			}
427
		}
428
	}
429

    
430
	return $services;
431
}
432

    
433
function find_service_by_name($name) {
434
	$services = get_services();
435
	
436
	foreach ($services as $service) {
437
		if (isset($service["name"]) && ($service["name"] == $name)) {
438
			return $service;
439
		}
440
	}
441
	return array();
442
}
443

    
444
function find_service_by_openvpn_vpnid($vpnid) {
445
	$services = get_services();
446
	foreach ($services as $service) {
447
		if (isset($service["name"]) && ($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
448
			return $service;
449
		}
450
	}
451
	return array();
452
}
453

    
454
function find_service_by_cp_zone($zone) {
455
	$services = get_services();
456
	foreach ($services as $service) {
457
		if (isset($service["name"]) && ($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
458
			return $service;
459
		}
460
	}
461
	return array();
462
}
463

    
464
function service_description_compare($a, $b) {
465
	if (strtolower($a['description']) == strtolower($b['description'])) {
466
		return 0;
467
	}
468
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
469
}
470

    
471
function service_name_compare($a, $b) {
472
	if (!isset($a['name']) || !isset($b['name'])) {
473
		return -1;
474
	}
475
	/* If the names are equal, fall back to sorting by description */
476
	if (strtolower($a['name']) == strtolower($b['name'])) {
477
		return service_description_compare($a, $b);
478
	}
479
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
480
}
481

    
482
function service_dispname_compare($a, $b) {
483
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
484
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
485
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
486
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
487
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
488
			if ($adn2 == $bdn2) {
489
				return 0;
490
			}
491
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
492
		}
493
	}
494
	/* If the display names are equal, compare the internal name */
495
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
496
		return service_name_compare($a, $b);
497
	}
498
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
499
}
500

    
501
function get_pkg_descr($package_name) {
502
	global $config;
503
	if (is_array($config['installedpackages']['package'])) {
504
		foreach ($config['installedpackages']['package'] as $pkg) {
505
			if ($pkg['name'] == $package_name) {
506
				return $pkg['descr'];
507
			}
508
		}
509
	}
510
	return gettext("Not available.");
511
}
512

    
513
function get_service_status($service) {
514
	global $g;
515
	switch ($service['name']) {
516
		case "openvpn":
517
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
518
			break;
519
		case "captiveportal":
520
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
521
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
522
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
523
			}
524
			break;
525
		case "vhosts-http":
526
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
527
			break;
528
		case "dhcrelay6":
529
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
530
			break;
531
		case 'ipsec':
532
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
533
			break;
534
		default:
535
			$running = is_service_running($service['name']);
536
	}
537
	return $running;
538
}
539

    
540
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
541
	$output = "";
542

    
543
	if (get_service_status($service)) {
544
		$statustext = gettext("Running");
545
		$text_class = "text-success";
546
		$fa_class = "fa fa-check-circle";
547
		$fa_class_thumbs = "fa fa-thumbs-o-up";
548
		$Thumbs_UpDown = "Thumbs up";
549
	} else {
550
		if (is_service_enabled($service['name'])) {
551
			$statustext = gettext("Stopped");
552
			$text_class = "text-danger";
553
			$fa_class = "fa fa-times-circle";
554
		} else {
555
			$statustext = gettext("Disabled");
556
			$text_class = "text-warning";
557
			$fa_class = "fa fa-ban";
558
		}
559
		$fa_class_thumbs = "fa fa-thumbs-o-down";
560
		$Thumbs_UpDown = "Thumbs down";
561
	}
562
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
563

    
564
	if ($title == "state") {
565
		$title = $statustext;
566
	} elseif ($title == "service_state") {
567
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
568
	} elseif ($title == "description_state") {
569
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
570
	} elseif ($title == "description_service_state") {
571
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
572
	}
573

    
574
	$spacer = ($withthumbs || $withtext) ? " " : "";
575

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

    
578
	$spacer = ($withtext) ? " " : "";
579
	if ($withthumbs) {
580
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
581
	}
582

    
583
	if ($withtext) {
584
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
585
	}
586

    
587
	return $output;
588
}
589

    
590
function get_service_control_links($service, $addname = false) {
591
	global $g;
592
	$output = "";
593
	$stitle = ($addname) ? $service['name'] . " " : "";
594

    
595
	switch ($service['name']) {
596
		case "openvpn":
597
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
598
		break;
599
		case "captiveportal":
600
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
601
		break;
602
		default:
603
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
604
	}
605

    
606
	if (get_service_status($service)) {
607
		switch ($service['name']) {
608
			case "openvpn":
609
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
610
				break;
611
			case "captiveportal":
612
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
613
				break;
614
			default:
615
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
616
		}
617

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

    
620
		switch ($service['name']) {
621
			case "openvpn":
622
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
623
				break;
624
			case "captiveportal":
625
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
626
				break;
627
			default:
628
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
629
		}
630

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

    
633
	} else {
634
		$service_enabled = is_service_enabled($service['name']);
635

    
636
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
637
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
638
			$output .= '<i class="fa fa-play-circle"></i></a> ';
639
		}
640
	}
641

    
642
	return $output;
643
}
644

    
645
function service_control_start($name, $extras) {
646
	global $g;
647
	switch ($name) {
648
		case 'radvd':
649
			services_radvd_configure();
650
			break;
651
		case 'captiveportal':
652
			$zone = htmlspecialchars($extras['zone']);
653
			captiveportal_init_webgui_zonename($zone);
654
			break;
655
		case 'ntpd':
656
			system_ntp_configure();
657
			break;
658
		case 'dpinger':
659
			setup_gateways_monitor();
660
			break;
661
		case 'bsnmpd':
662
			services_snmpd_configure();
663
			break;
664
		case 'dhcrelay':
665
			services_dhcrelay_configure();
666
			break;
667
		case 'dhcrelay6':
668
			services_dhcrelay6_configure();
669
			break;
670
		case 'dnsmasq':
671
			services_dnsmasq_configure();
672
			break;
673
		case 'unbound':
674
			services_unbound_configure();
675
			break;
676
		case 'dhcpd':
677
			services_dhcpd_configure();
678
			break;
679
		case 'igmpproxy':
680
			services_igmpproxy_configure();
681
			break;
682
		case 'miniupnpd':
683
			upnp_action('start');
684
			break;
685
		case 'ipsec':
686
			ipsec_force_reload();
687
			break;
688
		case 'sshd':
689
			send_event("service restart sshd");
690
			break;
691
		case 'pcscd':
692
			ipsec_force_reload();
693
			break;
694
		case 'openvpn':
695
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
696
			if (($vpnmode == "server") || ($vpnmode == "client")) {
697
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
698
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
699
				if (file_exists($configfile)) {
700
					openvpn_restart_by_vpnid($vpnmode, $id);
701
				}
702
			}
703
			break;
704
		case 'syslogd':
705
			system_syslogd_start();
706
			break;
707
		default:
708
			start_service($name);
709
			break;
710
	}
711
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
712
}
713
function service_control_stop($name, $extras) {
714
	global $g;
715
	switch ($name) {
716
		case 'radvd':
717
			killbypid("{$g['varrun_path']}/radvd.pid");
718
			break;
719
		case 'captiveportal':
720
			$zone = htmlspecialchars($extras['zone']);
721
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
722
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
723
			break;
724
		case 'ntpd':
725
			killbyname("ntpd");
726
			break;
727
		case 'openntpd':
728
			killbyname("openntpd");
729
			break;
730
		case 'dpinger':
731
			stop_dpinger();
732
			break;
733
		case 'bsnmpd':
734
			killbypid("{$g['varrun_path']}/snmpd.pid");
735
			break;
736
		case 'choparp':
737
			killbyname("choparp");
738
			break;
739
		case 'dhcpd':
740
			killbyname("dhcpd");
741
			break;
742
		case 'dhcrelay':
743
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
744
			break;
745
		case 'dhcrelay6':
746
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
747
			break;
748
		case 'dnsmasq':
749
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
750
			break;
751
		case 'unbound':
752
			killbypid("{$g['varrun_path']}/unbound.pid");
753
			break;
754
		case 'igmpproxy':
755
			killbyname("igmpproxy");
756
			break;
757
		case 'miniupnpd':
758
			upnp_action('stop');
759
			break;
760
		case 'sshd':
761
			killbyname("sshd");
762
			break;
763
		case 'pcscd':
764
		case 'ipsec':
765
			exec("/usr/local/sbin/strongswanrc stop");
766
			if (isvalidproc("pcscd")) {
767
				killbyname("pcscd");
768
			}
769
			break;
770
		case 'openvpn':
771
			$vpnmode = htmlspecialchars($extras['vpnmode']);
772
			if (($vpnmode == "server") or ($vpnmode == "client")) {
773
				$id = htmlspecialchars($extras['id']);
774
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
775
				killbypid($pidfile);
776
				openvpn_delete_tmp($vpnmode, $id);
777
			}
778
			break;
779
		case 'syslogd':
780
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
781
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
782
				usleep(100000);
783
			}
784
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
785
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
786
				usleep(100000);
787
			}
788
			/* Make sure sshguard stops as well */
789
			sigkillbyname("sshguard", "TERM");
790
			break;
791
		default:
792
			stop_service($name);
793
			break;
794
	}
795
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
796
}
797

    
798
function service_control_restart($name, $extras) {
799
	global $g;
800
	switch ($name) {
801
		case 'radvd':
802
			services_radvd_configure();
803
			break;
804
		case 'captiveportal':
805
			$zone = htmlspecialchars($extras['zone']);
806
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
807
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
808
			/* see https://redmine.pfsense.org/issues/12651 */
809
			sleep(1);
810
			captiveportal_init_webgui_zonename($zone);
811
			break;
812
		case 'ntpd':
813
		case 'openntpd':
814
			system_ntp_configure();
815
			break;
816
		case 'dpinger':
817
			setup_gateways_monitor();
818
			break;
819
		case 'bsnmpd':
820
			services_snmpd_configure();
821
			break;
822
		case 'dhcrelay':
823
			services_dhcrelay_configure();
824
			break;
825
		case 'dhcrelay6':
826
			services_dhcrelay6_configure();
827
			break;
828
		case 'dnsmasq':
829
			services_dnsmasq_configure();
830
			break;
831
		case 'unbound':
832
			services_unbound_configure();
833
			break;
834
		case 'dhcpd':
835
			services_dhcpd_configure();
836
			break;
837
		case 'igmpproxy':
838
			services_igmpproxy_configure();
839
			break;
840
		case 'miniupnpd':
841
			upnp_action('restart');
842
			break;
843
		case 'ipsec':
844
			ipsec_force_reload();
845
			break;
846
		case 'sshd':
847
			send_event("service restart sshd");
848
			break;
849
		case 'pcscd':
850
			exec("/usr/local/sbin/strongswanrc stop");
851
			if (isvalidproc("pcscd")) {
852
				killbyname("pcscd");
853
			}
854
			ipsec_force_reload();
855
			break;
856
		case 'openvpn':
857
			$vpnmode = htmlspecialchars($extras['vpnmode']);
858
			if ($vpnmode == "server" || $vpnmode == "client") {
859
				$id = htmlspecialchars($extras['id']);
860
				$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn";
861
				if (file_exists($configfile)) {
862
					openvpn_restart_by_vpnid($vpnmode, $id);
863
				}
864
			}
865
			break;
866
		case 'syslogd':
867
			system_syslogd_start();
868
			break;
869
		default:
870
			restart_service($name);
871
			break;
872
	}
873
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
874
}
875

    
876
?>
(45-45/61)