Project

General

Profile

Download (22 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-2019 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

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

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

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

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

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

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

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

    
73
	return;
74
}
75

    
76
function start_service($name, $after_sync = false) {
77
	global $config;
78

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

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

    
109
function stop_service($name) {
110
	global $config;
111

    
112
	if (empty($name)) {
113
		return;
114
	}
115

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

    
135
				break;
136
			}
137
		}
138
	}
139
}
140

    
141
function restart_service($name) {
142
	global $config;
143

    
144
	if (empty($name)) {
145
		return;
146
	}
147

    
148
	if (is_service_running($name)) {
149
		stop_service($name);
150
	}
151
	start_service($name);
152

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

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

    
170
	return (isvalidpid($pidfile));
171
}
172

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

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

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

    
204
function is_service_running($service, $ps = "") {
205
	global $config;
206

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

    
221
				return false;
222
			}
223
		}
224
	}
225

    
226
	if (is_process_running($service)) {
227
		return true;
228
	}
229

    
230
	return false;
231
}
232

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

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

    
251
	if (isset($config['dnsmasq']['enable'])) {
252
		$pconfig = array();
253
		$pconfig['name'] = "dnsmasq";
254
		$pconfig['description'] = gettext("DNS Forwarder");
255
		$services[] = $pconfig;
256
	}
257

    
258
	if (isset($config['unbound']['enable'])) {
259
		$pconfig = array();
260
		$pconfig['name'] = "unbound";
261
		$pconfig['description'] = gettext("DNS Resolver");
262
		$services[] = $pconfig;
263
	}
264

    
265
	$pconfig = array();
266
	$pconfig['name'] = "ntpd";
267
	$pconfig['description'] = gettext("NTP clock sync");
268
	$services[] = $pconfig;
269

    
270
	$pconfig = array();
271
	$pconfig['name'] = "syslogd";
272
	$pconfig['description'] = gettext("System Logger Daemon");
273
	$services[] = $pconfig;
274

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

    
287
	$iflist = array();
288
	$ifdescrs = get_configured_interface_list();
289
	foreach ($ifdescrs as $if) {
290
		$oc = $config['interfaces'][$if];
291
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
292
			$iflist[$if] = $if;
293
		}
294
	}
295

    
296
	if (isset($config['dhcrelay']['enable'])) {
297
		$pconfig = array();
298
		$pconfig['name'] = "dhcrelay";
299
		$pconfig['description'] = gettext("DHCP Relay");
300
		$services[] = $pconfig;
301
	}
302

    
303
	if (isset($config['dhcrelay6']['enable'])) {
304
		$pconfig = array();
305
		$pconfig['name'] = "dhcrelay6";
306
		$pconfig['description'] = gettext("DHCPv6 Relay");
307
		$services[] = $pconfig;
308
	}
309

    
310
	if (is_dhcp_server_enabled()) {
311
		$pconfig = array();
312
		$pconfig['name'] = "dhcpd";
313
		$pconfig['description'] = gettext("DHCP Service");
314
		$services[] = $pconfig;
315
	}
316

    
317
	$gateways_arr = return_gateways_array();
318
	if (is_array($gateways_arr)) {
319
		$pconfig = array();
320
		$pconfig['name'] = "dpinger";
321
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
322
		$services[] = $pconfig;
323
	}
324

    
325
	if (isset($config['snmpd']['enable'])) {
326
		$pconfig = array();
327
		$pconfig['name'] = "bsnmpd";
328
		$pconfig['description'] = gettext("SNMP Service");
329
		$services[] = $pconfig;
330
	}
331

    
332
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
333
		$pconfig = array();
334
		$pconfig['name'] = "igmpproxy";
335
		$pconfig['description'] = gettext("IGMP proxy");
336
		$services[] = $pconfig;
337
	}
338

    
339
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
340
		$pconfig = array();
341
		$pconfig['name'] = "miniupnpd";
342
		$pconfig['description'] = gettext("UPnP Service");
343
		$services[] = $pconfig;
344
	}
345

    
346
	if (ipsec_enabled()) {
347
		$pconfig = array();
348
		$pconfig['name'] = "ipsec";
349
		$pconfig['description'] = gettext("IPsec VPN");
350
		$services[] = $pconfig;
351
	}
352

    
353
	if (isset($config['system']['ssh']['enable'])) {
354
		$pconfig = array();
355
		$pconfig['name'] = "sshd";
356
		$pconfig['description'] = gettext("Secure Shell Daemon");
357
		$services[] = $pconfig;
358
	}
359

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

    
376
	return $services;
377
}
378

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
532
	return $output;
533
}
534

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

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

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

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

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

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

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

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

    
587
	return $output;
588
}
589

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

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

    
804
?>
(45-45/59)