Project

General

Profile

Download (23.7 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-2006 Colin Smith (ethethlay@gmail.com)
7
	Copyright (c) 2005-2016 Electric Sheep Fencing, LLC.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in
18
	   the documentation and/or other materials provided with the
19
	   distribution.
20

    
21
	3. All advertising materials mentioning features or use of this software
22
	   must display the following acknowledgment:
23
	   "This product includes software developed by the pfSense Project
24
	   for use in the pfSense® software distribution. (http://www.pfsense.org/).
25

    
26
	4. The names "pfSense" and "pfSense Project" must not be used to
27
	   endorse or promote products derived from this software without
28
	   prior written permission. For written permission, please contact
29
	   coreteam@pfsense.org.
30

    
31
	5. Products derived from this software may not be called "pfSense"
32
	   nor may "pfSense" appear in their names without prior written
33
	   permission of the Electric Sheep Fencing, LLC.
34

    
35
	6. Redistributions of any form whatsoever must retain the following
36
	   acknowledgment:
37

    
38
	"This product includes software developed by the pfSense Project
39
	for use in the pfSense software distribution (http://www.pfsense.org/).
40

    
41
	THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
42
	EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
	IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44
	PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
45
	ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46
	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47
	NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48
	LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52
	OF THE POSSIBILITY OF SUCH DAMAGE.
53
*/
54

    
55
require_once("globals.inc");
56
require_once("captiveportal.inc");
57
require_once("openvpn.inc");
58
require_once("ipsec.inc");
59
require_once("vpn.inc");
60
require_once("vslb.inc");
61
require_once("gwlb.inc");
62

    
63
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
64
function write_rcfile($params) {
65
	global $g;
66

    
67
	safe_mkdir(RCFILEPREFIX);
68
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
69
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
70
		return false;
71
	}
72

    
73
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
74
		return false;
75
	}
76

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

    
80
	/* write our rc functions */
81
	$towrite .= "rc_start() {\n";
82
	$towrite .= "\t{$params['start']}\n";
83
	$towrite .= "}\n\n";
84
	if (!empty($params['stop'])) {
85
		$tokill =& $params['stop'];
86
	} else if (!empty($params['executable'])) {
87
		/* just nuke the executable */
88
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
89
	} else {
90
		/* make an educated guess (bad) */
91
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
92
	}
93
	$towrite .= "rc_stop() {\n";
94
	$towrite .= "\t{$tokill}\n";
95
	$towrite .= "}\n\n";
96

    
97
	/* begin rcfile logic */
98
	$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";
99

    
100
	@file_put_contents($rcfile_fullname, $towrite);
101
	unset($towrite);
102
	@chmod("{$rcfile_fullname}", 0755);
103

    
104
	return;
105
}
106

    
107
function start_service($name) {
108
	global $config;
109

    
110
	if (empty($name)) {
111
		return;
112
	}
113

    
114
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
115
		foreach ($config['installedpackages']['service'] as $service) {
116
			if (strtolower($service['name']) == strtolower($name)) {
117
				if ($service['rcfile']) {
118
					$prefix = RCFILEPREFIX;
119
					if (!empty($service['prefix'])) {
120
						$prefix =& $service['prefix'];
121
					}
122
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
123
						mwexec_bg("{$prefix}{$service['rcfile']} start");
124
					}
125
				}
126
				if (!empty($service['startcmd'])) {
127
					eval($service['startcmd']);
128
				}
129
				break;
130
			}
131
		}
132
	}
133
}
134

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

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

    
142
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
143
		foreach ($config['installedpackages']['service'] as $service) {
144
			if (strtolower($service['name']) == strtolower($name)) {
145
				if ($service['rcfile']) {
146
					$prefix = RCFILEPREFIX;
147
					if (!empty($service['prefix'])) {
148
						$prefix =& $service['prefix'];
149
					}
150
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
151
						mwexec("{$prefix}{$service['rcfile']} stop");
152
					}
153
					return;
154
				}
155
				if (!empty($service['stopcmd'])) {
156
					eval($service['stopcmd']);
157
				} elseif (!empty($service['executable'])) {
158
					mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
159
				}
160

    
161
				break;
162
			}
163
		}
164
	}
165
}
166

    
167
function restart_service($name) {
168
	global $config;
169

    
170
	if (empty($name)) {
171
		return;
172
	}
173

    
174
	if (is_service_running($name)) {
175
		stop_service($name);
176
	}
177
	start_service($name);
178

    
179
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
180
		foreach ($config['installedpackages']['service'] as $service) {
181
			if (strtolower($service['name']) == strtolower($name)) {
182
				if ($service['restartcmd']) {
183
					eval($service['restartcmd']);
184
				}
185
				break;
186
			}
187
		}
188
	}
189
}
190

    
191
function is_pid_running($pidfile) {
192
	if (!file_exists($pidfile)) {
193
		return false;
194
	}
195

    
196
	return (isvalidpid($pidfile));
197
}
198

    
199
function is_dhcp_running($interface) {
200
	$status = find_dhclient_process($interface);
201
	if ($status != 0) {
202
		return true;
203
	}
204
	return false;
205
}
206

    
207
function restart_service_if_running($service) {
208
	global $config;
209
	if (is_service_running($service)) {
210
		restart_service($service);
211
	}
212
	return;
213
}
214

    
215
function is_service_enabled($service_name) {
216
	global $config;
217
	if ($service_name == "") {
218
		return false;
219
	}
220
	if (is_array($config['installedpackages'])) {
221
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
222
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
223
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
224
			return false;
225
		}
226
	}
227
	return true;
228
}
229

    
230
function is_service_running($service, $ps = "") {
231
	global $config;
232

    
233
	if (is_array($config['installedpackages']['service'])) {
234
		foreach ($config['installedpackages']['service'] as $aservice) {
235
			if (strtolower($service) == strtolower($aservice['name'])) {
236
				if ($aservice['custom_php_service_status_command'] <> "") {
237
					eval("\$rc={$aservice['custom_php_service_status_command']};");
238
					return $rc;
239
				}
240
				if (empty($aservice['executable'])) {
241
					return false;
242
				}
243
				if (is_process_running($aservice['executable'])) {
244
					return true;
245
				}
246

    
247
				return false;
248
			}
249
		}
250
	}
251

    
252
	if (is_process_running($service)) {
253
		return true;
254
	}
255

    
256
	return false;
257
}
258

    
259
function get_services() {
260
	global $config;
261
	if (is_array($config['installedpackages']['service'])) {
262
		$services = $config['installedpackages']['service'];
263
	} else {
264
		$services = array();
265
	}
266

    
267
	/*
268
	 * Add services that are in the base.
269
	 */
270
	if (is_radvd_enabled()) {
271
		$pconfig = array();
272
		$pconfig['name'] = "radvd";
273
		$pconfig['description'] = gettext("Router Advertisement Daemon");
274
		$services[] = $pconfig;
275
	}
276

    
277
	if (isset($config['dnsmasq']['enable'])) {
278
		$pconfig = array();
279
		$pconfig['name'] = "dnsmasq";
280
		$pconfig['description'] = gettext("DNS Forwarder");
281
		$services[] = $pconfig;
282
	}
283

    
284
	if (isset($config['unbound']['enable'])) {
285
		$pconfig = array();
286
		$pconfig['name'] = "unbound";
287
		$pconfig['description'] = gettext("DNS Resolver");
288
		$services[] = $pconfig;
289
	}
290

    
291
	$pconfig = array();
292
	$pconfig['name'] = "ntpd";
293
	$pconfig['description'] = gettext("NTP clock sync");
294
	$services[] = $pconfig;
295

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

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

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

    
324
	if (isset($config['dhcrelay6']['enable'])) {
325
		$pconfig = array();
326
		$pconfig['name'] = "dhcrelay6";
327
		$pconfig['description'] = gettext("DHCPv6 Relay");
328
		$services[] = $pconfig;
329
	}
330

    
331
	if (is_dhcp_server_enabled()) {
332
		$pconfig = array();
333
		$pconfig['name'] = "dhcpd";
334
		$pconfig['description'] = gettext("DHCP Service");
335
		$services[] = $pconfig;
336
	}
337

    
338
	$gateways_arr = return_gateways_array();
339
	if (is_array($gateways_arr)) {
340
		$pconfig = array();
341
		$pconfig['name'] = "dpinger";
342
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
343
		$services[] = $pconfig;
344
	}
345

    
346
	if (isset($config['snmpd']['enable'])) {
347
		$pconfig = array();
348
		$pconfig['name'] = "bsnmpd";
349
		$pconfig['description'] = gettext("SNMP Service");
350
		$services[] = $pconfig;
351
	}
352

    
353
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
354
		$pconfig = array();
355
		$pconfig['name'] = "igmpproxy";
356
		$pconfig['description'] = gettext("IGMP proxy");
357
		$services[] = $pconfig;
358
	}
359

    
360
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
361
		$pconfig = array();
362
		$pconfig['name'] = "miniupnpd";
363
		$pconfig['description'] = gettext("UPnP Service");
364
		$services[] = $pconfig;
365
	}
366

    
367
	if (ipsec_enabled()) {
368
		$pconfig = array();
369
		$pconfig['name'] = "ipsec";
370
		$pconfig['description'] = gettext("IPsec VPN");
371
		$services[] = $pconfig;
372
	}
373

    
374
	if (isset($config['system']['enablesshd'])) {
375
		$pconfig = array();
376
		$pconfig['name'] = "sshd";
377
		$pconfig['description'] = gettext("Secure Shell Daemon");
378
		$services[] = $pconfig;
379
	}
380

    
381
	foreach (array('server', 'client') as $mode) {
382
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
383
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
384
				if (!isset($setting['disable'])) {
385
					$pconfig = array();
386
					$pconfig['name'] = "openvpn";
387
					$pconfig['mode'] = $mode;
388
					$pconfig['id'] = $id;
389
					$pconfig['vpnid'] = $setting['vpnid'];
390
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
391
					$services[] = $pconfig;
392
				}
393
			}
394
		}
395
	}
396

    
397
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
398
		$pconfig = array();
399
		$pconfig['name'] = "relayd";
400
		$pconfig['description'] = gettext("Server load balancing daemon");
401
		$services[] = $pconfig;
402
	}
403
	return $services;
404
}
405

    
406
function find_service_by_name($name) {
407
	$services = get_services();
408
	foreach ($services as $service) {
409
		if ($service["name"] == $name) {
410
			return $service;
411
		}
412
	}
413
	return array();
414
}
415

    
416
function find_service_by_openvpn_vpnid($vpnid) {
417
	$services = get_services();
418
	foreach ($services as $service) {
419
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
420
			return $service;
421
		}
422
	}
423
	return array();
424
}
425

    
426
function find_service_by_cp_zone($zone) {
427
	$services = get_services();
428
	foreach ($services as $service) {
429
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
430
			return $service;
431
		}
432
	}
433
	return array();
434
}
435

    
436
function service_name_compare($a, $b) {
437
	if (strtolower($a['name']) == strtolower($b['name'])) {
438
		return 0;
439
	}
440
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
441
}
442

    
443
function service_dispname_compare($a, $b) {
444
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
445
		return 0;
446
	}
447
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
448
}
449

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

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

    
489
function get_service_status_icon($service, $withtext = true, $smallicon = false) {
490
	global $g;
491
	$output = "";
492
	if (get_service_status($service)) {
493
		$statustext = gettext("Running");
494
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
495
		$output .= ($smallicon) ? "fa fa-play" : "fa fa-lg fa-play";
496
		$output .= "\" ></i></a>";
497
		if ($withtext) {
498
			$output .= "&nbsp;" . $statustext;
499
		}
500
	} else {
501
		$service_enabled = is_service_enabled($service['name']);
502
		$statustext = ($service_enabled) ? gettext("Stopped") : gettext("Disabled");
503
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
504
		$output .= ($smallicon) ? "fa fa-times" : "fa fa-lg fa-times";
505
		$output .= "\" ></i></a>";
506
		if ($withtext) {
507
			$output .= "&nbsp;" . $statustext;
508
		}
509
	}
510
	return $output;
511
}
512

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

    
516
// This version proved GET formatted links
517
function get_service_control_GET_links($service, $addname = false) {
518
	global $g;
519
	$output = "";
520
	$stitle = ($addname) ? $service['name'] . " " : "";
521

    
522
	switch ($service['name']) {
523
		case "openvpn":
524
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service='.$service['name'] . '&amp;vpnmode=' . $service['mode'] . '&amp;id=' . $service['vpnid'] . '">';
525
		break;
526
		case "captiveportal":
527
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service=' . $service['name'] . '&amp;zone=' . $service['zone'] . '">';
528
		break;
529
		default:
530
			$link = '<a title="%s" href="/status_services.php?mode=%s&amp;service=' . $service['name'] . '">';
531
	}
532

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

    
559
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
560
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
561
			$output .= '<i class="fa fa-play-circle"></i></a> ';
562
		}
563
	}
564

    
565
	return $output;
566
}
567
*/
568

    
569
function get_service_control_links($service, $addname = false) {
570
	global $g;
571
	$output = "";
572
	$stitle = ($addname) ? $service['name'] . " " : "";
573

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

    
585
	if (get_service_status($service)) {
586
		switch ($service['name']) {
587
			case "openvpn":
588
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
589
				break;
590
			case "captiveportal":
591
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
592
				break;
593
			default:
594
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
595
		}
596

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

    
599
		switch ($service['name']) {
600
			case "openvpn":
601
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
602
				break;
603
			case "captiveportal":
604
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
605
				break;
606
			default:
607
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
608
		}
609

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

    
612
	} else {
613
		$service_enabled = is_service_enabled($service['name']);
614

    
615
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
616
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
617
			$output .= '<i class="fa fa-play-circle"></i></a> ';
618
		}
619
	}
620

    
621
	return $output;
622
}
623

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

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

    
830
?>
(48-48/65)