Project

General

Profile

Download (24.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

    
68
	/* begin rcfile logic */
69
	$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_stop\n\t\trc_start\n\t\t;;\nesac\n\n";
70

    
71
	@file_put_contents($rcfile_fullname, $towrite);
72
	unset($towrite);
73
	@chmod("{$rcfile_fullname}", 0755);
74

    
75
	return;
76
}
77

    
78
function start_service($name, $after_sync = false) {
79
	global $config;
80

    
81
	if (empty($name)) {
82
		return;
83
	}
84

    
85
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
86
		foreach ($config['installedpackages']['service'] as $service) {
87
			if (isset($service['name']) && (strtolower($service['name']) == strtolower($name))) {
88
				/* Avoid starting twice if this is called just after a
89
				 * package sync which starts the service itself. */
90
				if ($after_sync && isset($service['starts_on_sync'])) {
91
					break;
92
				}
93
				if ($service['rcfile']) {
94
					$prefix = RCFILEPREFIX;
95
					if (!empty($service['prefix'])) {
96
						$prefix = &$service['prefix'];
97
					}
98
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
99
						mwexec_bg("{$prefix}{$service['rcfile']} start");
100
					}
101
				}
102
				if (!empty($service['startcmd'])) {
103
					eval($service['startcmd']);
104
				}
105
				break;
106
			}
107
		}
108
	}
109
}
110

    
111
function stop_service($name) {
112
	global $config;
113

    
114
	if (empty($name)) {
115
		return;
116
	}
117

    
118
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
119
		foreach ($config['installedpackages']['service'] as $service) {
120
			if (strtolower($service['name']) == strtolower($name)) {
121
				if ($service['rcfile']) {
122
					$prefix = RCFILEPREFIX;
123
					if (!empty($service['prefix'])) {
124
						$prefix = &$service['prefix'];
125
					}
126
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
127
						mwexec("{$prefix}{$service['rcfile']} stop");
128
					}
129
					return;
130
				}
131
				if (!empty($service['stopcmd'])) {
132
					eval($service['stopcmd']);
133
				} elseif (!empty($service['executable'])) {
134
					mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
135
				}
136

    
137
				break;
138
			}
139
		}
140
	}
141
}
142

    
143
function restart_service($name) {
144
	global $config;
145

    
146
	if (empty($name)) {
147
		return;
148
	}
149

    
150
	if (is_service_running($name)) {
151
		stop_service($name);
152
	}
153
	start_service($name);
154

    
155
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
156
		foreach ($config['installedpackages']['service'] as $service) {
157
			if (strtolower($service['name']) == strtolower($name)) {
158
				if ($service['restartcmd']) {
159
					eval($service['restartcmd']);
160
				}
161
				break;
162
			}
163
		}
164
	}
165
}
166

    
167
function is_pid_running($pidfile) {
168
	if (!file_exists($pidfile)) {
169
		return false;
170
	}
171

    
172
	return (isvalidpid($pidfile));
173
}
174

    
175
function is_dhcp_running($interface) {
176
	$status = find_dhclient_process($interface);
177
	if ($status != 0) {
178
		return true;
179
	}
180
	return false;
181
}
182

    
183
function restart_service_if_running($service) {
184
	global $config;
185
	if (is_service_running($service)) {
186
		restart_service($service);
187
	}
188
	return;
189
}
190

    
191
function is_service_enabled($service_name) {
192
	global $config;
193
	if ($service_name == "") {
194
		return false;
195
	}
196
	if (is_array($config['installedpackages'])) {
197
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
198
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
199
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
200
			return false;
201
		}
202
	}
203
	return true;
204
}
205

    
206
function is_service_running($service, $ps = "") {
207
	global $config;
208

    
209
	if (is_array($config['installedpackages']['service'])) {
210
		foreach ($config['installedpackages']['service'] as $aservice) {
211
			if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) {
212
				if ($aservice['custom_php_service_status_command'] <> "") {
213
					eval("\$rc={$aservice['custom_php_service_status_command']};");
214
					return $rc;
215
				}
216
				if (empty($aservice['executable'])) {
217
					return false;
218
				}
219
				if (is_process_running($aservice['executable'])) {
220
					return true;
221
				}
222

    
223
				return false;
224
			}
225
		}
226
	}
227

    
228
	if (is_process_running($service)) {
229
		return true;
230
	}
231

    
232
	return false;
233
}
234

    
235
function get_services() {
236
	global $config;
237
	if (is_array($config['installedpackages']['service'])) {
238
		$services = $config['installedpackages']['service'];
239
	} else {
240
		$services = array();
241
	}
242

    
243
	/*
244
	 * Add services that are in the base.
245
	 */
246
	if (is_radvd_enabled()) {
247
		$pconfig = array();
248
		$pconfig['name'] = "radvd";
249
		$pconfig['description'] = gettext("Router Advertisement Daemon");
250
		$pconfig['enabled'] = is_service_enabled("radvd");
251
		$pconfig['status'] = get_service_status($pconfig);
252
		$services[] = $pconfig;
253
	}
254

    
255
	if (isset($config['dnsmasq']['enable'])) {
256
		$pconfig = array();
257
		$pconfig['name'] = "dnsmasq";
258
		$pconfig['description'] = gettext("DNS Forwarder");
259
		$pconfig['enabled'] = is_service_enabled("dnsmasq");
260
		$pconfig['status'] = get_service_status($pconfig);
261
		$services[] = $pconfig;
262
	}
263

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

    
273
	if (isset($config['ipsec']['pkcs11support'])) {
274
		$pconfig = array();
275
		$pconfig['name'] = "pcscd";
276
		$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
277
		$pconfig['enabled'] = is_service_enabled("pcscd");
278
		$pconfig['status'] = get_service_status($pconfig);
279
		$services[] = $pconfig;
280
	}
281

    
282
	if (is_array($config['ntpd']) && ($config['ntpd']['enable'] != 'disabled')) {
283
		$pconfig = array();
284
		$pconfig['name'] = "ntpd";
285
		$pconfig['description'] = gettext("NTP clock sync");
286
		$pconfig['enabled'] = is_service_enabled("ntpd");
287
		$pconfig['status'] = get_service_status($pconfig);
288
		$services[] = $pconfig;
289
	}
290

    
291
	$pconfig = array();
292
	$pconfig['name'] = "syslogd";
293
	$pconfig['description'] = gettext("System Logger Daemon");
294
	$pconfig['enabled'] = is_service_enabled("syslogd");
295
	$pconfig['status'] = get_service_status($pconfig);
296
	$services[] = $pconfig;
297

    
298
	if (is_array($config['captiveportal'])) {
299
		foreach ($config['captiveportal'] as $zone => $setting) {
300
			if (isset($setting['enable'])) {
301
				$pconfig = array();
302
				$pconfig['name'] = "captiveportal";
303
				$pconfig['zone'] = $zone;
304
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
305
				$services[] = $pconfig;
306
			}
307
		}
308
	}
309

    
310
	$iflist = array();
311
	$ifdescrs = get_configured_interface_list();
312
	foreach ($ifdescrs as $if) {
313
		$oc = $config['interfaces'][$if];
314
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
315
			$iflist[$if] = $if;
316
		}
317
	}
318

    
319
	if (isset($config['dhcrelay']['enable'])) {
320
		$pconfig = array();
321
		$pconfig['name'] = "dhcrelay";
322
		$pconfig['description'] = gettext("DHCP Relay");
323
		$pconfig['enabled'] = is_service_enabled("dhcprelay");
324
		$pconfig['status'] = get_service_status($pconfig);
325
		$services[] = $pconfig;
326
	}
327

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

    
337
	if (is_dhcp_server_enabled()) {
338
		$pconfig = array();
339
		$pconfig['name'] = "dhcpd";
340
		$pconfig['description'] = gettext("DHCP Service");
341
		$pconfig['enabled'] = is_service_enabled("dhcpd");
342
		$pconfig['status'] = get_service_status($pconfig);
343
		$services[] = $pconfig;
344
	}
345

    
346
	$gateways_arr = return_gateways_array();
347
	if (is_array($gateways_arr)) {
348
		$pconfig = array();
349
		$pconfig['name'] = "dpinger";
350
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
351
		$pconfig['enabled'] = is_service_enabled("dpinger");
352
		$pconfig['status'] = get_service_status($pconfig);
353
		$services[] = $pconfig;
354
	}
355

    
356
	if (isset($config['snmpd']['enable'])) {
357
		$pconfig = array();
358
		$pconfig['name'] = "bsnmpd";
359
		$pconfig['description'] = gettext("SNMP Service");
360
		$pconfig['enabled'] = is_service_enabled("bsnmpd");
361
		$pconfig['status'] = get_service_status($pconfig);
362
		$services[] = $pconfig;
363
	}
364

    
365
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
366
		$pconfig = array();
367
		$pconfig['name'] = "igmpproxy";
368
		$pconfig['description'] = gettext("IGMP proxy");
369
		$pconfig['enabled'] = is_service_enabled("igmpproxy");
370
		$pconfig['status'] = get_service_status($pconfig);
371
		$services[] = $pconfig;
372
	}
373

    
374
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
375
		$pconfig = array();
376
		$pconfig['name'] = "miniupnpd";
377
		$pconfig['description'] = gettext("UPnP Service");
378
		$pconfig['enabled'] = is_service_enabled("miniupnpd");
379
		$pconfig['status'] = get_service_status($pconfig);
380
		$services[] = $pconfig;
381
	}
382

    
383
	if (ipsec_enabled()) {
384
		$pconfig = array();
385
		$pconfig['name'] = "ipsec";
386
		$pconfig['description'] = gettext("IPsec VPN");
387
		$pconfig['enabled'] = is_service_enabled("ipsec");
388
		$pconfig['status'] = get_service_status($pconfig);
389
		$services[] = $pconfig;
390
	}
391

    
392
	if (isset($config['system']['ssh']['enable'])) {
393
		$pconfig = array();
394
		$pconfig['name'] = "sshd";
395
		$pconfig['description'] = gettext("Secure Shell Daemon");
396
		$pconfig['enabled'] = is_service_enabled("sshd");
397
		$pconfig['status'] = get_service_status($pconfig);
398
		$services[] = $pconfig;
399
	}
400

    
401
	foreach (array('server', 'client') as $mode) {
402
		init_config_arr(array('openvpn'));
403
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
404
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
405
				if (!isset($setting['disable'])) {
406
					$pconfig = array();
407
					$pconfig['name'] = "openvpn";
408
					$pconfig['mode'] = $mode;
409
					$pconfig['id'] = $id;
410
					$pconfig['vpnid'] = $setting['vpnid'];
411
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
412
					$pconfig['enabled'] = is_service_enabled("openvpn");
413
					$pconfig['status'] = get_service_status($pconfig);
414
					$services[] = $pconfig;
415
				}
416
			}
417
		}
418
	}
419

    
420
	return $services;
421
}
422

    
423
function find_service_by_name($name) {
424
	$services = get_services();
425
	
426
	foreach ($services as $service) {
427
		if (isset($service["name"]) && ($service["name"] == $name)) {
428
			return $service;
429
		}
430
	}
431
	return array();
432
}
433

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

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

    
454
function service_description_compare($a, $b) {
455
	if (strtolower($a['description']) == strtolower($b['description'])) {
456
		return 0;
457
	}
458
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
459
}
460

    
461
function service_name_compare($a, $b) {
462
	if (!isset($a['name']) || !isset($b['name'])) {
463
		return -1;
464
	}
465
	/* If the names are equal, fall back to sorting by description */
466
	if (strtolower($a['name']) == strtolower($b['name'])) {
467
		return service_description_compare($a, $b);
468
	}
469
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
470
}
471

    
472
function service_dispname_compare($a, $b) {
473
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
474
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
475
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
476
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
477
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
478
			if ($adn2 == $bdn2) {
479
				return 0;
480
			}
481
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
482
		}
483
	}
484
	/* If the display names are equal, compare the internal name */
485
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
486
		return service_name_compare($a, $b);
487
	}
488
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
489
}
490

    
491
function get_pkg_descr($package_name) {
492
	global $config;
493
	if (is_array($config['installedpackages']['package'])) {
494
		foreach ($config['installedpackages']['package'] as $pkg) {
495
			if ($pkg['name'] == $package_name) {
496
				return $pkg['descr'];
497
			}
498
		}
499
	}
500
	return gettext("Not available.");
501
}
502

    
503
function get_service_status($service) {
504
	global $g;
505
	switch ($service['name']) {
506
		case "openvpn":
507
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
508
			break;
509
		case "captiveportal":
510
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
511
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
512
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
513
			}
514
			break;
515
		case "vhosts-http":
516
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
517
			break;
518
		case "dhcrelay6":
519
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
520
			break;
521
		case 'ipsec':
522
			$running = (is_pid_running("{$g['varrun_path']}/charon.pid") || is_process_running('charon'));
523
			break;
524
		default:
525
			$running = is_service_running($service['name']);
526
	}
527
	return $running;
528
}
529

    
530
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
531
	$output = "";
532

    
533
	if (get_service_status($service)) {
534
		$statustext = gettext("Running");
535
		$text_class = "text-success";
536
		$fa_class = "fa fa-check-circle";
537
		$fa_class_thumbs = "fa fa-thumbs-o-up";
538
		$Thumbs_UpDown = "Thumbs up";
539
	} else {
540
		if (is_service_enabled($service['name'])) {
541
			$statustext = gettext("Stopped");
542
			$text_class = "text-danger";
543
			$fa_class = "fa fa-times-circle";
544
		} else {
545
			$statustext = gettext("Disabled");
546
			$text_class = "text-warning";
547
			$fa_class = "fa fa-ban";
548
		}
549
		$fa_class_thumbs = "fa fa-thumbs-o-down";
550
		$Thumbs_UpDown = "Thumbs down";
551
	}
552
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
553

    
554
	if ($title == "state") {
555
		$title = $statustext;
556
	} elseif ($title == "service_state") {
557
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
558
	} elseif ($title == "description_state") {
559
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
560
	} elseif ($title == "description_service_state") {
561
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
562
	}
563

    
564
	$spacer = ($withthumbs || $withtext) ? " " : "";
565

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

    
568
	$spacer = ($withtext) ? " " : "";
569
	if ($withthumbs) {
570
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
571
	}
572

    
573
	if ($withtext) {
574
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
575
	}
576

    
577
	return $output;
578
}
579

    
580
function get_service_control_links($service, $addname = false) {
581
	global $g;
582
	$output = "";
583
	$stitle = ($addname) ? $service['name'] . " " : "";
584

    
585
	switch ($service['name']) {
586
		case "openvpn":
587
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
588
		break;
589
		case "captiveportal":
590
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
591
		break;
592
		default:
593
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
594
	}
595

    
596
	if (get_service_status($service)) {
597
		switch ($service['name']) {
598
			case "openvpn":
599
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
600
				break;
601
			case "captiveportal":
602
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
603
				break;
604
			default:
605
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
606
		}
607

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

    
610
		switch ($service['name']) {
611
			case "openvpn":
612
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
613
				break;
614
			case "captiveportal":
615
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
616
				break;
617
			default:
618
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
619
		}
620

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

    
623
	} else {
624
		$service_enabled = is_service_enabled($service['name']);
625

    
626
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
627
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
628
			$output .= '<i class="fa fa-play-circle"></i></a> ';
629
		}
630
	}
631

    
632
	return $output;
633
}
634

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

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

    
864
?>
(45-45/61)