Project

General

Profile

Download (24.2 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-2020 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_name']} 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
	$pconfig = array();
274
	$pconfig['name'] = "pcscd";
275
	$pconfig['description'] = gettext("PC/SC Smart Card Daemon");
276
	$pconfig['enabled'] = is_service_enabled("pcscd");
277
	$pconfig['status'] = get_service_status($pconfig);
278
	$services[] = $pconfig;
279

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
418
	return $services;
419
}
420

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

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

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

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

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

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

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

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

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

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

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

    
562
	$spacer = ($withthumbs || $withtext) ? " " : "";
563

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

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

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

    
575
	return $output;
576
}
577

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

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

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

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

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

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

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

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

    
630
	return $output;
631
}
632

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

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

    
853
?>
(46-46/62)