Project

General

Profile

Download (22.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-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
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, $after_sync = false) {
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
				/* Avoid starting twice if this is called just after a
88
				 * package sync which starts the service itself. */
89
				if ($after_sync && isset($service['starts_on_sync'])) {
90
					break;
91
				}
92
				if ($service['rcfile']) {
93
					$prefix = RCFILEPREFIX;
94
					if (!empty($service['prefix'])) {
95
						$prefix = &$service['prefix'];
96
					}
97
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
98
						mwexec_bg("{$prefix}{$service['rcfile']} start");
99
					}
100
				}
101
				if (!empty($service['startcmd'])) {
102
					eval($service['startcmd']);
103
				}
104
				break;
105
			}
106
		}
107
	}
108
}
109

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
222
				return false;
223
			}
224
		}
225
	}
226

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

    
231
	return false;
232
}
233

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
377
	if (!is_array($config['load_balancer']['lbpool'])) {
378
		$config['load_balancer']['lbpool'] = array();
379
	}
380

    
381
	if (is_array($config['load_balancer']['virtual_server']) && count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
382
		$pconfig = array();
383
		$pconfig['name'] = "relayd";
384
		$pconfig['description'] = gettext("Server load balancing daemon");
385
		$services[] = $pconfig;
386
	}
387

    
388
	return $services;
389
}
390

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

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

    
411
function find_service_by_cp_zone($zone) {
412
	$services = get_services();
413
	foreach ($services as $service) {
414
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
415
			return $service;
416
		}
417
	}
418
	return array();
419
}
420

    
421
function service_description_compare($a, $b) {
422
	if (strtolower($a['description']) == strtolower($b['description'])) {
423
		return 0;
424
	}
425
	return (strtolower($a['description']) < strtolower($b['description'])) ? -1 : 1;
426
}
427

    
428
function service_name_compare($a, $b) {
429
	/* If the names are equal, fall back to sorting by description */
430
	if (strtolower($a['name']) == strtolower($b['name'])) {
431
		return service_description_compare($a, $b);
432
	}
433
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
434
}
435

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

    
455
function get_pkg_descr($package_name) {
456
	global $config;
457
	if (is_array($config['installedpackages']['package'])) {
458
		foreach ($config['installedpackages']['package'] as $pkg) {
459
			if ($pkg['name'] == $package_name) {
460
				return $pkg['descr'];
461
			}
462
		}
463
	}
464
	return gettext("Not available.");
465
}
466

    
467
function get_service_status($service) {
468
	global $g;
469
	switch ($service['name']) {
470
		case "openvpn":
471
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
472
			break;
473
		case "captiveportal":
474
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
475
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
476
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
477
			}
478
			break;
479
		case "vhosts-http":
480
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
481
			break;
482
		case "dhcrelay6":
483
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
484
			break;
485
		case 'ipsec':
486
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
487
			break;
488
		default:
489
			$running = is_service_running($service['name']);
490
	}
491
	return $running;
492
}
493

    
494
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
495
	$output = "";
496

    
497
	if (get_service_status($service)) {
498
		$statustext = gettext("Running");
499
		$text_class = "text-success";
500
		$fa_class = "fa fa-check-circle";
501
		$fa_class_thumbs = "fa fa-thumbs-o-up";
502
		$Thumbs_UpDown = "Thumbs up";
503
	} else {
504
		if (is_service_enabled($service['name'])) {
505
			$statustext = gettext("Stopped");
506
			$text_class = "text-danger";
507
			$fa_class = "fa fa-times-circle";
508
		} else {
509
			$statustext = gettext("Disabled");
510
			$text_class = "text-warning";
511
			$fa_class = "fa fa-ban";
512
		}
513
		$fa_class_thumbs = "fa fa-thumbs-o-down";
514
		$Thumbs_UpDown = "Thumbs down";
515
	}
516
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
517

    
518
	if ($title == "state") {
519
		$title = $statustext;
520
	} elseif ($title == "service_state") {
521
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
522
	} elseif ($title == "description_state") {
523
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
524
	} elseif ($title == "description_service_state") {
525
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
526
	}
527

    
528
	$spacer = ($withthumbs || $withtext) ? " " : "";
529

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

    
532
	$spacer = ($withtext) ? " " : "";
533
	if ($withthumbs) {
534
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
535
	}
536

    
537
	if ($withtext) {
538
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
539
	}
540

    
541
	return $output;
542
}
543

    
544
function get_service_control_links($service, $addname = false) {
545
	global $g;
546
	$output = "";
547
	$stitle = ($addname) ? $service['name'] . " " : "";
548

    
549
	switch ($service['name']) {
550
		case "openvpn":
551
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
552
		break;
553
		case "captiveportal":
554
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
555
		break;
556
		default:
557
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
558
	}
559

    
560
	if (get_service_status($service)) {
561
		switch ($service['name']) {
562
			case "openvpn":
563
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
564
				break;
565
			case "captiveportal":
566
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
567
				break;
568
			default:
569
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
570
		}
571

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

    
574
		switch ($service['name']) {
575
			case "openvpn":
576
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
577
				break;
578
			case "captiveportal":
579
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
580
				break;
581
			default:
582
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
583
		}
584

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

    
587
	} else {
588
		$service_enabled = is_service_enabled($service['name']);
589

    
590
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
591
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
592
			$output .= '<i class="fa fa-play-circle"></i></a> ';
593
		}
594
	}
595

    
596
	return $output;
597
}
598

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

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

    
824
?>
(45-45/60)