Project

General

Profile

Download (22.1 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-2018 Rubicon Communications, LLC (Netgate)
7
 * Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
8
 * All rights reserved.
9
 *
10
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13
 *
14
 * http://www.apache.org/licenses/LICENSE-2.0
15
 *
16
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21
 */
22

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

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

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

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

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

    
50
	/* write our rc functions */
51
	$towrite .= "rc_start() {\n";
52
	$towrite .= "\t{$params['start']}\n";
53
	$towrite .= "}\n\n";
54
	if (!empty($params['stop'])) {
55
		$tokill =& $params['stop'];
56
	} else if (!empty($params['executable'])) {
57
		/* just nuke the executable */
58
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
59
	} else {
60
		/* make an educated guess (bad) */
61
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
62
	}
63
	$towrite .= "rc_stop() {\n";
64
	$towrite .= "\t{$tokill}\n";
65
	$towrite .= "}\n\n";
66

    
67
	/* begin rcfile logic */
68
	$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";
69

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

    
74
	return;
75
}
76

    
77
function start_service($name) {
78
	global $config;
79

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

    
84
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
85
		foreach ($config['installedpackages']['service'] as $service) {
86
			if (strtolower($service['name']) == strtolower($name)) {
87
				if ($service['rcfile']) {
88
					$prefix = RCFILEPREFIX;
89
					if (!empty($service['prefix'])) {
90
						$prefix =& $service['prefix'];
91
					}
92
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
93
						mwexec_bg("{$prefix}{$service['rcfile']} start");
94
					}
95
				}
96
				if (!empty($service['startcmd'])) {
97
					eval($service['startcmd']);
98
				}
99
				break;
100
			}
101
		}
102
	}
103
}
104

    
105
function stop_service($name) {
106
	global $config;
107

    
108
	if (empty($name)) {
109
		return;
110
	}
111

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

    
131
				break;
132
			}
133
		}
134
	}
135
}
136

    
137
function restart_service($name) {
138
	global $config;
139

    
140
	if (empty($name)) {
141
		return;
142
	}
143

    
144
	if (is_service_running($name)) {
145
		stop_service($name);
146
	}
147
	start_service($name);
148

    
149
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
150
		foreach ($config['installedpackages']['service'] as $service) {
151
			if (strtolower($service['name']) == strtolower($name)) {
152
				if ($service['restartcmd']) {
153
					eval($service['restartcmd']);
154
				}
155
				break;
156
			}
157
		}
158
	}
159
}
160

    
161
function is_pid_running($pidfile) {
162
	if (!file_exists($pidfile)) {
163
		return false;
164
	}
165

    
166
	return (isvalidpid($pidfile));
167
}
168

    
169
function is_dhcp_running($interface) {
170
	$status = find_dhclient_process($interface);
171
	if ($status != 0) {
172
		return true;
173
	}
174
	return false;
175
}
176

    
177
function restart_service_if_running($service) {
178
	global $config;
179
	if (is_service_running($service)) {
180
		restart_service($service);
181
	}
182
	return;
183
}
184

    
185
function is_service_enabled($service_name) {
186
	global $config;
187
	if ($service_name == "") {
188
		return false;
189
	}
190
	if (is_array($config['installedpackages'])) {
191
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
192
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
193
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
194
			return false;
195
		}
196
	}
197
	return true;
198
}
199

    
200
function is_service_running($service, $ps = "") {
201
	global $config;
202

    
203
	if (is_array($config['installedpackages']['service'])) {
204
		foreach ($config['installedpackages']['service'] as $aservice) {
205
			if (strtolower($service) == strtolower($aservice['name'])) {
206
				if ($aservice['custom_php_service_status_command'] <> "") {
207
					eval("\$rc={$aservice['custom_php_service_status_command']};");
208
					return $rc;
209
				}
210
				if (empty($aservice['executable'])) {
211
					return false;
212
				}
213
				if (is_process_running($aservice['executable'])) {
214
					return true;
215
				}
216

    
217
				return false;
218
			}
219
		}
220
	}
221

    
222
	if (is_process_running($service)) {
223
		return true;
224
	}
225

    
226
	return false;
227
}
228

    
229
function get_services() {
230
	global $config;
231
	if (is_array($config['installedpackages']['service'])) {
232
		$services = $config['installedpackages']['service'];
233
	} else {
234
		$services = array();
235
	}
236

    
237
	/*
238
	 * Add services that are in the base.
239
	 */
240
	if (is_radvd_enabled()) {
241
		$pconfig = array();
242
		$pconfig['name'] = "radvd";
243
		$pconfig['description'] = gettext("Router Advertisement Daemon");
244
		$services[] = $pconfig;
245
	}
246

    
247
	if (isset($config['dnsmasq']['enable'])) {
248
		$pconfig = array();
249
		$pconfig['name'] = "dnsmasq";
250
		$pconfig['description'] = gettext("DNS Forwarder");
251
		$services[] = $pconfig;
252
	}
253

    
254
	if (isset($config['unbound']['enable'])) {
255
		$pconfig = array();
256
		$pconfig['name'] = "unbound";
257
		$pconfig['description'] = gettext("DNS Resolver");
258
		$services[] = $pconfig;
259
	}
260

    
261
	$pconfig = array();
262
	$pconfig['name'] = "ntpd";
263
	$pconfig['description'] = gettext("NTP clock sync");
264
	$services[] = $pconfig;
265

    
266
	$pconfig = array();
267
	$pconfig['name'] = "syslogd";
268
	$pconfig['description'] = gettext("System Logger Daemon");
269
	$services[] = $pconfig;
270

    
271
	if (is_array($config['captiveportal'])) {
272
		foreach ($config['captiveportal'] as $zone => $setting) {
273
			if (isset($setting['enable'])) {
274
				$pconfig = array();
275
				$pconfig['name'] = "captiveportal";
276
				$pconfig['zone'] = $zone;
277
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
278
				$services[] = $pconfig;
279
			}
280
		}
281
	}
282

    
283
	$iflist = array();
284
	$ifdescrs = get_configured_interface_list();
285
	foreach ($ifdescrs as $if) {
286
		$oc = $config['interfaces'][$if];
287
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
288
			$iflist[$if] = $if;
289
		}
290
	}
291

    
292
	if (isset($config['dhcrelay']['enable'])) {
293
		$pconfig = array();
294
		$pconfig['name'] = "dhcrelay";
295
		$pconfig['description'] = gettext("DHCP Relay");
296
		$services[] = $pconfig;
297
	}
298

    
299
	if (isset($config['dhcrelay6']['enable'])) {
300
		$pconfig = array();
301
		$pconfig['name'] = "dhcrelay6";
302
		$pconfig['description'] = gettext("DHCPv6 Relay");
303
		$services[] = $pconfig;
304
	}
305

    
306
	if (is_dhcp_server_enabled()) {
307
		$pconfig = array();
308
		$pconfig['name'] = "dhcpd";
309
		$pconfig['description'] = gettext("DHCP Service");
310
		$services[] = $pconfig;
311
	}
312

    
313
	$gateways_arr = return_gateways_array();
314
	if (is_array($gateways_arr)) {
315
		$pconfig = array();
316
		$pconfig['name'] = "dpinger";
317
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
318
		$services[] = $pconfig;
319
	}
320

    
321
	if (isset($config['snmpd']['enable'])) {
322
		$pconfig = array();
323
		$pconfig['name'] = "bsnmpd";
324
		$pconfig['description'] = gettext("SNMP Service");
325
		$services[] = $pconfig;
326
	}
327

    
328
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
329
		$pconfig = array();
330
		$pconfig['name'] = "igmpproxy";
331
		$pconfig['description'] = gettext("IGMP proxy");
332
		$services[] = $pconfig;
333
	}
334

    
335
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
336
		$pconfig = array();
337
		$pconfig['name'] = "miniupnpd";
338
		$pconfig['description'] = gettext("UPnP Service");
339
		$services[] = $pconfig;
340
	}
341

    
342
	if (ipsec_enabled()) {
343
		$pconfig = array();
344
		$pconfig['name'] = "ipsec";
345
		$pconfig['description'] = gettext("IPsec VPN");
346
		$services[] = $pconfig;
347
	}
348

    
349
	if (isset($config['system']['enablesshd'])) {
350
		$pconfig = array();
351
		$pconfig['name'] = "sshd";
352
		$pconfig['description'] = gettext("Secure Shell Daemon");
353
		$services[] = $pconfig;
354
	}
355

    
356
	foreach (array('server', 'client') as $mode) {
357
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
358
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
359
				if (!isset($setting['disable'])) {
360
					$pconfig = array();
361
					$pconfig['name'] = "openvpn";
362
					$pconfig['mode'] = $mode;
363
					$pconfig['id'] = $id;
364
					$pconfig['vpnid'] = $setting['vpnid'];
365
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
366
					$services[] = $pconfig;
367
				}
368
			}
369
		}
370
	}
371

    
372
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
373
		$pconfig = array();
374
		$pconfig['name'] = "relayd";
375
		$pconfig['description'] = gettext("Server load balancing daemon");
376
		$services[] = $pconfig;
377
	}
378
	return $services;
379
}
380

    
381
function find_service_by_name($name) {
382
	$services = get_services();
383
	foreach ($services as $service) {
384
		if ($service["name"] == $name) {
385
			return $service;
386
		}
387
	}
388
	return array();
389
}
390

    
391
function find_service_by_openvpn_vpnid($vpnid) {
392
	$services = get_services();
393
	foreach ($services as $service) {
394
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
395
			return $service;
396
		}
397
	}
398
	return array();
399
}
400

    
401
function find_service_by_cp_zone($zone) {
402
	$services = get_services();
403
	foreach ($services as $service) {
404
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
405
			return $service;
406
		}
407
	}
408
	return array();
409
}
410

    
411
function service_description_compare($a, $b) {
412
	if (strtolower($a['description']) == strtolower($b['description'])) {
413
		return 0;
414
	}
415
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
416
}
417

    
418
function service_name_compare($a, $b) {
419
	/* If the names are equal, fall back to sorting by description */
420
	if (strtolower($a['name']) == strtolower($b['name'])) {
421
		return service_description_compare($a, $b);
422
	}
423
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
424
}
425

    
426
function service_dispname_compare($a, $b) {
427
	/* If two items have an instance suffix, perform an integer comparison to avoid awkward sorting */
428
	if ((strpos($a['dispname'], '_') > 0) && (strpos($b['dispname'], '_') > 0)) {
429
		list($adn1, $adn2) = explode('_', $a['dispname'], 2);
430
		list($bdn1, $bdn2) = explode('_', $b['dispname'], 2);
431
		if (($adn1 == $bdn1) && is_numeric($adn2) && is_numeric($bdn2)) {
432
			if ($adn2 == $bdn2) {
433
				return 0;
434
			}
435
			return ((int)$adn2 < (int)$bdn2) ? -1 : 1;
436
		}
437
	}
438
	/* If the display names are equal, compare the internal name */
439
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
440
		return service_name_compare($a, $b);
441
	}
442
	return (strtolower($a['dispname']) < strtolower($b['dispname'])) ? -1 : 1;
443
}
444

    
445
function get_pkg_descr($package_name) {
446
	global $config;
447
	if (is_array($config['installedpackages']['package'])) {
448
		foreach ($config['installedpackages']['package'] as $pkg) {
449
			if ($pkg['name'] == $package_name) {
450
				return $pkg['descr'];
451
			}
452
		}
453
	}
454
	return gettext("Not available.");
455
}
456

    
457
function get_service_status($service) {
458
	global $g;
459
	switch ($service['name']) {
460
		case "openvpn":
461
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
462
			break;
463
		case "captiveportal":
464
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
465
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
466
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
467
			}
468
			break;
469
		case "vhosts-http":
470
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
471
			break;
472
		case "dhcrelay6":
473
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
474
			break;
475
		case 'ipsec':
476
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
477
			break;
478
		default:
479
			$running = is_service_running($service['name']);
480
	}
481
	return $running;
482
}
483

    
484
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
485
	$output = "";
486

    
487
	if (get_service_status($service)) {
488
		$statustext = gettext("Running");
489
		$text_class = "text-success";
490
		$fa_class = "fa fa-check-circle";
491
		$fa_class_thumbs = "fa fa-thumbs-o-up";
492
		$Thumbs_UpDown = "Thumbs up";
493
	} else {
494
		if (is_service_enabled($service['name'])) {
495
			$statustext = gettext("Stopped");
496
			$text_class = "text-danger";
497
			$fa_class = "fa fa-times-circle";
498
		} else {
499
			$statustext = gettext("Disabled");
500
			$text_class = "text-warning";
501
			$fa_class = "fa fa-ban";
502
		}
503
		$fa_class_thumbs = "fa fa-thumbs-o-down";
504
		$Thumbs_UpDown = "Thumbs down";
505
	}
506
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
507

    
508
	if ($title == "state") {
509
		$title = $statustext;
510
	} elseif ($title == "service_state") {
511
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
512
	} elseif ($title == "description_state") {
513
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
514
	} elseif ($title == "description_service_state") {
515
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
516
	}
517

    
518
	$spacer = ($withthumbs || $withtext) ? " " : "";
519

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

    
522
	$spacer = ($withtext) ? " " : "";
523
	if ($withthumbs) {
524
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
525
	}
526

    
527
	if ($withtext) {
528
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
529
	}
530

    
531
	return $output;
532
}
533

    
534
function get_service_control_links($service, $addname = false) {
535
	global $g;
536
	$output = "";
537
	$stitle = ($addname) ? $service['name'] . " " : "";
538

    
539
	switch ($service['name']) {
540
		case "openvpn":
541
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
542
		break;
543
		case "captiveportal":
544
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
545
		break;
546
		default:
547
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
548
	}
549

    
550
	if (get_service_status($service)) {
551
		switch ($service['name']) {
552
			case "openvpn":
553
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
554
				break;
555
			case "captiveportal":
556
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
557
				break;
558
			default:
559
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
560
		}
561

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

    
564
		switch ($service['name']) {
565
			case "openvpn":
566
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
567
				break;
568
			case "captiveportal":
569
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
570
				break;
571
			default:
572
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
573
		}
574

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

    
577
	} else {
578
		$service_enabled = is_service_enabled($service['name']);
579

    
580
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
581
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
582
			$output .= '<i class="fa fa-play-circle"></i></a> ';
583
		}
584
	}
585

    
586
	return $output;
587
}
588

    
589
function service_control_start($name, $extras) {
590
	global $g;
591
	switch ($name) {
592
		case 'radvd':
593
			services_radvd_configure();
594
			break;
595
		case 'captiveportal':
596
			$zone = htmlspecialchars($extras['zone']);
597
			captiveportal_init_webgui_zonename($zone);
598
			break;
599
		case 'ntpd':
600
			system_ntp_configure();
601
			break;
602
		case 'dpinger':
603
			setup_gateways_monitor();
604
			break;
605
		case 'bsnmpd':
606
			services_snmpd_configure();
607
			break;
608
		case 'dhcrelay':
609
			services_dhcrelay_configure();
610
			break;
611
		case 'dhcrelay6':
612
			services_dhcrelay6_configure();
613
			break;
614
		case 'dnsmasq':
615
			services_dnsmasq_configure();
616
			break;
617
		case 'unbound':
618
			services_unbound_configure();
619
			break;
620
		case 'dhcpd':
621
			services_dhcpd_configure();
622
			break;
623
		case 'igmpproxy':
624
			services_igmpproxy_configure();
625
			break;
626
		case 'miniupnpd':
627
			upnp_action('start');
628
			break;
629
		case 'ipsec':
630
			vpn_ipsec_force_reload();
631
			break;
632
		case 'sshd':
633
			send_event("service restart sshd");
634
			break;
635
		case 'openvpn':
636
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
637
			if (($vpnmode == "server") || ($vpnmode == "client")) {
638
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
639
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
640
				if (file_exists($configfile)) {
641
					openvpn_restart_by_vpnid($vpnmode, $id);
642
				}
643
			}
644
			break;
645
		case 'relayd':
646
			relayd_configure();
647
			filter_configure();
648
			break;
649
		case 'syslogd':
650
			system_syslogd_start();
651
			break;
652
		default:
653
			start_service($name);
654
			break;
655
	}
656
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
657
}
658
function service_control_stop($name, $extras) {
659
	global $g;
660
	switch ($name) {
661
		case 'radvd':
662
			killbypid("{$g['varrun_path']}/radvd.pid");
663
			break;
664
		case 'captiveportal':
665
			$zone = htmlspecialchars($extras['zone']);
666
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
667
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
668
			break;
669
		case 'ntpd':
670
			killbyname("ntpd");
671
			break;
672
		case 'openntpd':
673
			killbyname("openntpd");
674
			break;
675
		case 'dpinger':
676
			stop_dpinger();
677
			break;
678
		case 'bsnmpd':
679
			killbypid("{$g['varrun_path']}/snmpd.pid");
680
			break;
681
		case 'choparp':
682
			killbyname("choparp");
683
			break;
684
		case 'dhcpd':
685
			killbyname("dhcpd");
686
			break;
687
		case 'dhcrelay':
688
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
689
			break;
690
		case 'dhcrelay6':
691
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
692
			break;
693
		case 'dnsmasq':
694
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
695
			break;
696
		case 'unbound':
697
			killbypid("{$g['varrun_path']}/unbound.pid");
698
			break;
699
		case 'igmpproxy':
700
			killbyname("igmpproxy");
701
			break;
702
		case 'miniupnpd':
703
			upnp_action('stop');
704
			break;
705
		case 'sshd':
706
			killbyname("sshd");
707
			break;
708
		case 'ipsec':
709
			exec("/usr/local/sbin/ipsec stop");
710
			break;
711
		case 'openvpn':
712
			$vpnmode = htmlspecialchars($extras['vpnmode']);
713
			if (($vpnmode == "server") or ($vpnmode == "client")) {
714
				$id = htmlspecialchars($extras['id']);
715
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
716
				killbypid($pidfile);
717
			}
718
			break;
719
		case 'relayd':
720
			sigkillbyname("relayd", "TERM");
721
			break;
722
		case 'syslogd':
723
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
724
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "TERM");
725
				usleep(100000);
726
			}
727
			if (isvalidpid("{$g['varrun_path']}/syslog.pid")) {
728
				sigkillbypid("{$g['varrun_path']}/syslog.pid", "KILL");
729
				usleep(100000);
730
			}
731
			/* Make sure sshlockout_pf stops as well */
732
			sigkillbyname("sshlockout_pf", "TERM");
733
			break;
734
		default:
735
			stop_service($name);
736
			break;
737
	}
738
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
739
}
740

    
741
function service_control_restart($name, $extras) {
742
	global $g;
743
	switch ($name) {
744
		case 'radvd':
745
			services_radvd_configure();
746
			break;
747
		case 'captiveportal':
748
			$zone = htmlspecialchars($extras['zone']);
749
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
750
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
751
			captiveportal_init_webgui_zonename($zone);
752
			break;
753
		case 'ntpd':
754
		case 'openntpd':
755
			system_ntp_configure();
756
			break;
757
		case 'dpinger':
758
			setup_gateways_monitor();
759
			break;
760
		case 'bsnmpd':
761
			services_snmpd_configure();
762
			break;
763
		case 'dhcrelay':
764
			services_dhcrelay_configure();
765
			break;
766
		case 'dhcrelay6':
767
			services_dhcrelay6_configure();
768
			break;
769
		case 'dnsmasq':
770
			services_dnsmasq_configure();
771
			break;
772
		case 'unbound':
773
			services_unbound_configure();
774
			break;
775
		case 'dhcpd':
776
			services_dhcpd_configure();
777
			break;
778
		case 'igmpproxy':
779
			services_igmpproxy_configure();
780
			break;
781
		case 'miniupnpd':
782
			upnp_action('restart');
783
			break;
784
		case 'ipsec':
785
			vpn_ipsec_force_reload();
786
			break;
787
		case 'sshd':
788
			send_event("service restart sshd");
789
			break;
790
		case 'openvpn':
791
			$vpnmode = htmlspecialchars($extras['vpnmode']);
792
			if ($vpnmode == "server" || $vpnmode == "client") {
793
				$id = htmlspecialchars($extras['id']);
794
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
795
				if (file_exists($configfile)) {
796
					openvpn_restart_by_vpnid($vpnmode, $id);
797
				}
798
			}
799
			break;
800
		case 'relayd':
801
			relayd_configure(true);
802
			filter_configure();
803
			break;
804
		case 'syslogd':
805
			system_syslogd_start();
806
			break;
807
		default:
808
			restart_service($name);
809
			break;
810
	}
811
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
812
}
813

    
814
?>
(41-41/55)