Project

General

Profile

Download (23.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-2016 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("globals.inc");
24
require_once("captiveportal.inc");
25
require_once("openvpn.inc");
26
require_once("ipsec.inc");
27
require_once("vpn.inc");
28
require_once("vslb.inc");
29
require_once("gwlb.inc");
30

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

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

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

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

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

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

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

    
72
	return;
73
}
74

    
75
function start_service($name) {
76
	global $config;
77

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

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

    
103
function stop_service($name) {
104
	global $config;
105

    
106
	if (empty($name)) {
107
		return;
108
	}
109

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

    
129
				break;
130
			}
131
		}
132
	}
133
}
134

    
135
function restart_service($name) {
136
	global $config;
137

    
138
	if (empty($name)) {
139
		return;
140
	}
141

    
142
	if (is_service_running($name)) {
143
		stop_service($name);
144
	}
145
	start_service($name);
146

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

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

    
164
	return (isvalidpid($pidfile));
165
}
166

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

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

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

    
198
function is_service_running($service, $ps = "") {
199
	global $config;
200

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

    
215
				return false;
216
			}
217
		}
218
	}
219

    
220
	if (is_process_running($service)) {
221
		return true;
222
	}
223

    
224
	return false;
225
}
226

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

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

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

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

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

    
264
	if (is_array($config['captiveportal'])) {
265
		foreach ($config['captiveportal'] as $zone => $setting) {
266
			if (isset($setting['enable'])) {
267
				$pconfig = array();
268
				$pconfig['name'] = "captiveportal";
269
				$pconfig['zone'] = $zone;
270
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
271
				$services[] = $pconfig;
272
			}
273
		}
274
	}
275

    
276
	$iflist = array();
277
	$ifdescrs = get_configured_interface_list();
278
	foreach ($ifdescrs as $if) {
279
		$oc = $config['interfaces'][$if];
280
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
281
			$iflist[$if] = $if;
282
		}
283
	}
284

    
285
	if (isset($config['dhcrelay']['enable'])) {
286
		$pconfig = array();
287
		$pconfig['name'] = "dhcrelay";
288
		$pconfig['description'] = gettext("DHCP Relay");
289
		$services[] = $pconfig;
290
	}
291

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

    
299
	if (is_dhcp_server_enabled()) {
300
		$pconfig = array();
301
		$pconfig['name'] = "dhcpd";
302
		$pconfig['description'] = gettext("DHCP Service");
303
		$services[] = $pconfig;
304
	}
305

    
306
	$gateways_arr = return_gateways_array();
307
	if (is_array($gateways_arr)) {
308
		$pconfig = array();
309
		$pconfig['name'] = "dpinger";
310
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
311
		$services[] = $pconfig;
312
	}
313

    
314
	if (isset($config['snmpd']['enable'])) {
315
		$pconfig = array();
316
		$pconfig['name'] = "bsnmpd";
317
		$pconfig['description'] = gettext("SNMP Service");
318
		$services[] = $pconfig;
319
	}
320

    
321
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
322
		$pconfig = array();
323
		$pconfig['name'] = "igmpproxy";
324
		$pconfig['description'] = gettext("IGMP proxy");
325
		$services[] = $pconfig;
326
	}
327

    
328
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
329
		$pconfig = array();
330
		$pconfig['name'] = "miniupnpd";
331
		$pconfig['description'] = gettext("UPnP Service");
332
		$services[] = $pconfig;
333
	}
334

    
335
	if (ipsec_enabled()) {
336
		$pconfig = array();
337
		$pconfig['name'] = "ipsec";
338
		$pconfig['description'] = gettext("IPsec VPN");
339
		$services[] = $pconfig;
340
	}
341

    
342
	if (isset($config['system']['enablesshd'])) {
343
		$pconfig = array();
344
		$pconfig['name'] = "sshd";
345
		$pconfig['description'] = gettext("Secure Shell Daemon");
346
		$services[] = $pconfig;
347
	}
348

    
349
	foreach (array('server', 'client') as $mode) {
350
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
351
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
352
				if (!isset($setting['disable'])) {
353
					$pconfig = array();
354
					$pconfig['name'] = "openvpn";
355
					$pconfig['mode'] = $mode;
356
					$pconfig['id'] = $id;
357
					$pconfig['vpnid'] = $setting['vpnid'];
358
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
359
					$services[] = $pconfig;
360
				}
361
			}
362
		}
363
	}
364

    
365
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
366
		$pconfig = array();
367
		$pconfig['name'] = "relayd";
368
		$pconfig['description'] = gettext("Server load balancing daemon");
369
		$services[] = $pconfig;
370
	}
371
	return $services;
372
}
373

    
374
function find_service_by_name($name) {
375
	$services = get_services();
376
	foreach ($services as $service) {
377
		if ($service["name"] == $name) {
378
			return $service;
379
		}
380
	}
381
	return array();
382
}
383

    
384
function find_service_by_openvpn_vpnid($vpnid) {
385
	$services = get_services();
386
	foreach ($services as $service) {
387
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
388
			return $service;
389
		}
390
	}
391
	return array();
392
}
393

    
394
function find_service_by_cp_zone($zone) {
395
	$services = get_services();
396
	foreach ($services as $service) {
397
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
398
			return $service;
399
		}
400
	}
401
	return array();
402
}
403

    
404
function service_name_compare($a, $b) {
405
	if (strtolower($a['name']) == strtolower($b['name'])) {
406
		return 0;
407
	}
408
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
409
}
410

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

    
418
function get_pkg_descr($package_name) {
419
	global $config;
420
	if (is_array($config['installedpackages']['package'])) {
421
		foreach ($config['installedpackages']['package'] as $pkg) {
422
			if ($pkg['name'] == $package_name) {
423
				return $pkg['descr'];
424
			}
425
		}
426
	}
427
	return gettext("Not available.");
428
}
429

    
430
function get_service_status($service) {
431
	global $g;
432
	switch ($service['name']) {
433
		case "openvpn":
434
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
435
			break;
436
		case "captiveportal":
437
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
438
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
439
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
440
			}
441
			break;
442
		case "vhosts-http":
443
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
444
			break;
445
		case "dhcrelay6":
446
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
447
			break;
448
		case 'ipsec':
449
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
450
			break;
451
		default:
452
			$running = is_service_running($service['name']);
453
	}
454
	return $running;
455
}
456

    
457
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
458
	$output = "";
459

    
460
	if (get_service_status($service)) {
461
		$statustext = gettext("Running");
462
		$text_class = "text-success";
463
		$fa_class = "fa fa-check-circle";
464
		$fa_class_thumbs = "fa fa-thumbs-o-up";
465
		$Thumbs_UpDown = "Thumbs up";
466
	} else {
467
		if (is_service_enabled($service['name'])) {
468
			$statustext = gettext("Stopped");
469
			$text_class = "text-danger";
470
			$fa_class = "fa fa-times-circle";
471
		} else {
472
			$statustext = gettext("Disabled");
473
			$text_class = "text-warning";
474
			$fa_class = "fa fa-ban";
475
		}
476
		$fa_class_thumbs = "fa fa-thumbs-o-down";
477
		$Thumbs_UpDown = "Thumbs down";
478
	}
479
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
480

    
481
	$spacer = ($withthumbs || $withtext) ? " " : "";
482
	if ($title == "state") {
483
		$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"{$statustext}\"></i>{$spacer}";
484
	} elseif ($title == "service_state") {
485
		$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"" . sprintf(gettext("%s Service is %s"), $service["name"], $statustext) . "\"></i>{$spacer}";
486
	} elseif ($title == "description_state") {
487
		$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"" . sprintf(gettext("%s, %s Service is %s"), $service["description"], $statustext) . "\"></i>{$spacer}";
488
	} elseif ($title == "description_service_state") {
489
		$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"" . sprintf(gettext("%s, %s Service is %s"), $service["description"], $service["name"], $statustext) . "\"></i>{$spacer}";
490
	}
491

    
492
	$spacer = ($withtext) ? " " : "";
493
	if ($withthumbs) {
494
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
495
	}
496

    
497
	if ($withtext) {
498
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
499
	}
500

    
501
	return $output;
502
}
503

    
504
/* This function is no longer required since services now use the POST method via JavaScript
505
   Commenting out for now. It should be removed in the next version
506

    
507
// This version proved GET formatted links
508
function get_service_control_GET_links($service, $addname = false) {
509
	global $g;
510
	$output = "";
511
	$stitle = ($addname) ? $service['name'] . " " : "";
512

    
513
	switch ($service['name']) {
514
		case "openvpn":
515
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service='.$service['name'] . '&amp;vpnmode=' . $service['mode'] . '&amp;id=' . $service['vpnid'] . '">';
516
		break;
517
		case "captiveportal":
518
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service=' . $service['name'] . '&amp;zone=' . $service['zone'] . '">';
519
		break;
520
		default:
521
			$link = '<a title="%s" href="/status_services.php?mode=%s&amp;service=' . $service['name'] . '">';
522
	}
523

    
524
	if (get_service_status($service)) {
525
		switch ($service['name']) {
526
			case "openvpn":
527
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}\">";
528
				break;
529
			case "captiveportal":
530
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}&amp;zone={$service['zone']}\">";
531
				break;
532
			default:
533
				$output .= "<a href=\"/status_services.php?mode=restartservice&amp;service={$service['name']}\">";
534
		}
535
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
536
		switch ($service['name']) {
537
			case "openvpn":
538
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}\">";
539
				break;
540
			case "captiveportal":
541
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}&amp;zone={$service['zone']}\">";
542
				break;
543
			default:
544
				$output .= "<a href=\"/status_services.php?mode=stopservice&amp;service={$service['name']}\">";
545
		}
546
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
547
	} else {
548
		$service_enabled = is_service_enabled($service['name']);
549

    
550
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
551
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
552
			$output .= '<i class="fa fa-play-circle"></i></a> ';
553
		}
554
	}
555

    
556
	return $output;
557
}
558
*/
559

    
560
function get_service_control_links($service, $addname = false) {
561
	global $g;
562
	$output = "";
563
	$stitle = ($addname) ? $service['name'] . " " : "";
564

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

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

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

    
590
		switch ($service['name']) {
591
			case "openvpn":
592
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
593
				break;
594
			case "captiveportal":
595
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
596
				break;
597
			default:
598
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
599
		}
600

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

    
603
	} else {
604
		$service_enabled = is_service_enabled($service['name']);
605

    
606
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
607
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
608
			$output .= '<i class="fa fa-play-circle"></i></a> ';
609
		}
610
	}
611

    
612
	return $output;
613
}
614

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

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

    
821
?>
(37-37/51)