Project

General

Profile

Download (23.7 KB) Statistics
| Branch: | Tag: | Revision:
1 c83068f6 Colin Smith
<?php
2 09221bc3 Renato Botelho
/*
3
	service-utils.inc
4 61e047a5 Phil Davis
5 09221bc3 Renato Botelho
	part of pfSense (https://www.pfsense.org)
6 61e047a5 Phil Davis
	Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
7 09221bc3 Renato Botelho
	Copyright (c) 2005-2016 Electric Sheep Fencing, LLC.
8 61e047a5 Phil Davis
	All rights reserved.
9 09221bc3 Renato Botelho
10 61e047a5 Phil Davis
	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 09221bc3 Renato Botelho
	   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 c83068f6 Colin Smith
55 ab1cf455 jim-p
require_once("globals.inc");
56 ee6e6011 jim-p
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 b05a8f35 jim-p
require_once("gwlb.inc");
62 523855b0 Scott Ullrich
63 e9d66ed4 phildd
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
64 c83068f6 Colin Smith
function write_rcfile($params) {
65 c2151973 Ermal
	global $g;
66 af799b48 Renato Botelho
67 6f931ad2 Phil Davis
	safe_mkdir(RCFILEPREFIX);
68 e9d66ed4 phildd
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
69 61e047a5 Phil Davis
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
70 af799b48 Renato Botelho
		return false;
71 61e047a5 Phil Davis
	}
72 c2151973 Ermal
73 61e047a5 Phil Davis
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
74 ba8495f0 Ermal
		return false;
75 61e047a5 Phil Davis
	}
76 af799b48 Renato Botelho
77 ba8495f0 Ermal
	$towrite = "#!/bin/sh\n";
78 4dddf8c3 Warren Baker
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
79 ba8495f0 Ermal
80 c83068f6 Colin Smith
	/* write our rc functions */
81 ba8495f0 Ermal
	$towrite .= "rc_start() {\n";
82
	$towrite .= "\t{$params['start']}\n";
83
	$towrite .= "}\n\n";
84 61e047a5 Phil Davis
	if (!empty($params['stop'])) {
85 c83068f6 Colin Smith
		$tokill =& $params['stop'];
86 61e047a5 Phil Davis
	} else if (!empty($params['executable'])) {
87 a8dbad89 Colin Smith
		/* just nuke the executable */
88 873c1701 Renato Botelho
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
89 c83068f6 Colin Smith
	} else {
90 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
91 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
92
	}
93 ba8495f0 Ermal
	$towrite .= "rc_stop() {\n";
94
	$towrite .= "\t{$tokill}\n";
95
	$towrite .= "}\n\n";
96 4685e464 Scott Ullrich
97 c83068f6 Colin Smith
	/* begin rcfile logic */
98 ba8495f0 Ermal
	$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 44b19298 Ermal
	@file_put_contents($rcfile_fullname, $towrite);
101
	unset($towrite);
102 e9d66ed4 phildd
	@chmod("{$rcfile_fullname}", 0755);
103 ba8495f0 Ermal
104 c83068f6 Colin Smith
	return;
105
}
106 31f346a8 Colin Smith
107 58826c73 Colin Smith
function start_service($name) {
108 669e1adb Bill Marquette
	global $config;
109 ba8495f0 Ermal
110 61e047a5 Phil Davis
	if (empty($name)) {
111 b27ade8e Ermal
		return;
112 61e047a5 Phil Davis
	}
113 b27ade8e Ermal
114 44b19298 Ermal
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
115 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
116
			if (strtolower($service['name']) == strtolower($name)) {
117
				if ($service['rcfile']) {
118 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
119 ba8495f0 Ermal
					if (!empty($service['prefix'])) {
120 31f346a8 Colin Smith
						$prefix =& $service['prefix'];
121
					}
122 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
123 ba8495f0 Ermal
						mwexec_bg("{$prefix}{$service['rcfile']} start");
124 a4ea3340 Scott Ullrich
					}
125 31f346a8 Colin Smith
				}
126 61e047a5 Phil Davis
				if (!empty($service['startcmd'])) {
127 31f346a8 Colin Smith
					eval($service['startcmd']);
128 61e047a5 Phil Davis
				}
129 31f346a8 Colin Smith
				break;
130
			}
131
		}
132
	}
133
}
134
135 58826c73 Colin Smith
function stop_service($name) {
136 cd72ded3 Timo Boettcher
	global $config;
137 246a887a Ermal
138 61e047a5 Phil Davis
	if (empty($name)) {
139 8bf2e9e5 Ermal
		return;
140 61e047a5 Phil Davis
	}
141 8bf2e9e5 Ermal
142 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
143 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
144
			if (strtolower($service['name']) == strtolower($name)) {
145
				if ($service['rcfile']) {
146 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
147 61e047a5 Phil Davis
					if (!empty($service['prefix'])) {
148 cd72ded3 Timo Boettcher
						$prefix =& $service['prefix'];
149
					}
150 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
151 6ae78f08 Marcello Coutinho
						mwexec("{$prefix}{$service['rcfile']} stop");
152 098820e2 Ermal
					}
153
					return;
154 cd72ded3 Timo Boettcher
				}
155 61e047a5 Phil Davis
				if (!empty($service['stopcmd'])) {
156 cd72ded3 Timo Boettcher
					eval($service['stopcmd']);
157 b8877138 doktornotor
				} elseif (!empty($service['executable'])) {
158
					mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
159 61e047a5 Phil Davis
				}
160 ba8495f0 Ermal
161 cd72ded3 Timo Boettcher
				break;
162
			}
163
		}
164
	}
165 31f346a8 Colin Smith
}
166
167 58826c73 Colin Smith
function restart_service($name) {
168 cd72ded3 Timo Boettcher
	global $config;
169 ba8495f0 Ermal
170 61e047a5 Phil Davis
	if (empty($name)) {
171 8bf2e9e5 Ermal
		return;
172 61e047a5 Phil Davis
	}
173 8bf2e9e5 Ermal
174 6855f25d doktornotor
	if (is_service_running($name)) {
175
		stop_service($name);
176
	}
177 636ab238 Colin Smith
	start_service($name);
178 ba8495f0 Ermal
179 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
180 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
181
			if (strtolower($service['name']) == strtolower($name)) {
182
				if ($service['restartcmd']) {
183 cd72ded3 Timo Boettcher
					eval($service['restartcmd']);
184
				}
185
				break;
186
			}
187
		}
188
	}
189 31f346a8 Colin Smith
}
190 ec4e071a Colin Smith
191 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
192 61e047a5 Phil Davis
	if (!file_exists($pidfile)) {
193 ba8495f0 Ermal
		return false;
194 61e047a5 Phil Davis
	}
195 ba8495f0 Ermal
196 17098641 Ermal
	return (isvalidpid($pidfile));
197 cd72ded3 Timo Boettcher
}
198
199 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
200 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
201 61e047a5 Phil Davis
	if ($status != 0) {
202 ca88e48f Scott Ullrich
		return true;
203 61e047a5 Phil Davis
	}
204 ca88e48f Scott Ullrich
	return false;
205 8ff8520f Scott Ullrich
}
206
207 0661a033 Colin Smith
function restart_service_if_running($service) {
208
	global $config;
209 61e047a5 Phil Davis
	if (is_service_running($service)) {
210 0661a033 Colin Smith
		restart_service($service);
211 61e047a5 Phil Davis
	}
212 0661a033 Colin Smith
	return;
213
}
214
215 ccfc0269 phildd
function is_service_enabled($service_name) {
216
	global $config;
217 61e047a5 Phil Davis
	if ($service_name == "") {
218 ccfc0269 phildd
		return false;
219 61e047a5 Phil Davis
	}
220 762e8cf9 jim-p
	if (is_array($config['installedpackages'])) {
221
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
222 61e047a5 Phil Davis
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
223
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
224 762e8cf9 jim-p
			return false;
225
		}
226
	}
227 ccfc0269 phildd
	return true;
228
}
229
230 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
231
	global $config;
232 ba8495f0 Ermal
233 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
234
		foreach ($config['installedpackages']['service'] as $aservice) {
235
			if (strtolower($service) == strtolower($aservice['name'])) {
236 ba8495f0 Ermal
				if ($aservice['custom_php_service_status_command'] <> "") {
237
					eval("\$rc={$aservice['custom_php_service_status_command']};");
238 c2d49311 Eirik Oeverby
					return $rc;
239
				}
240 61e047a5 Phil Davis
				if (empty($aservice['executable'])) {
241 ec4e071a Colin Smith
					return false;
242 61e047a5 Phil Davis
				}
243
				if (is_process_running($aservice['executable'])) {
244 ba8495f0 Ermal
					return true;
245 61e047a5 Phil Davis
				}
246 ba8495f0 Ermal
247
				return false;
248 cd72ded3 Timo Boettcher
			}
249
		}
250
	}
251 ba8495f0 Ermal
252 61e047a5 Phil Davis
	if (is_process_running($service)) {
253 ba8495f0 Ermal
		return true;
254 61e047a5 Phil Davis
	}
255 ba8495f0 Ermal
256
	return false;
257 ec4e071a Colin Smith
}
258 ff074bf9 Scott Ullrich
259 e48cdc01 jim-p
function get_services() {
260
	global $config;
261 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
262 e48cdc01 jim-p
		$services = $config['installedpackages']['service'];
263 61e047a5 Phil Davis
	} else {
264 e48cdc01 jim-p
		$services = array();
265 61e047a5 Phil Davis
	}
266 e48cdc01 jim-p
267 61e047a5 Phil Davis
	/*
268
	 * Add services that are in the base.
269 e48cdc01 jim-p
	 */
270 33232486 Warren Baker
	if (is_radvd_enabled()) {
271 e48cdc01 jim-p
		$pconfig = array();
272
		$pconfig['name'] = "radvd";
273
		$pconfig['description'] = gettext("Router Advertisement Daemon");
274
		$services[] = $pconfig;
275
	}
276
277 33232486 Warren Baker
	if (isset($config['dnsmasq']['enable'])) {
278 e48cdc01 jim-p
		$pconfig = array();
279
		$pconfig['name'] = "dnsmasq";
280
		$pconfig['description'] = gettext("DNS Forwarder");
281
		$services[] = $pconfig;
282
	}
283
284 33232486 Warren Baker
	if (isset($config['unbound']['enable'])) {
285
		$pconfig = array();
286
		$pconfig['name'] = "unbound";
287 a3fb1412 Phil Davis
		$pconfig['description'] = gettext("DNS Resolver");
288 33232486 Warren Baker
		$services[] = $pconfig;
289
	}
290
291 e48cdc01 jim-p
	$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 e8c516a0 Phil Davis
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
303 e48cdc01 jim-p
				$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 61e047a5 Phil Davis
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
313 e48cdc01 jim-p
			$iflist[$if] = $if;
314 61e047a5 Phil Davis
		}
315 e48cdc01 jim-p
	}
316
317 33232486 Warren Baker
	if (isset($config['dhcrelay']['enable'])) {
318 e48cdc01 jim-p
		$pconfig = array();
319
		$pconfig['name'] = "dhcrelay";
320
		$pconfig['description'] = gettext("DHCP Relay");
321
		$services[] = $pconfig;
322
	}
323
324 33232486 Warren Baker
	if (isset($config['dhcrelay6']['enable'])) {
325 9590e0de Phil Davis
		$pconfig = array();
326
		$pconfig['name'] = "dhcrelay6";
327
		$pconfig['description'] = gettext("DHCPv6 Relay");
328
		$services[] = $pconfig;
329
	}
330
331 33232486 Warren Baker
	if (is_dhcp_server_enabled()) {
332 e48cdc01 jim-p
		$pconfig = array();
333
		$pconfig['name'] = "dhcpd";
334
		$pconfig['description'] = gettext("DHCP Service");
335
		$services[] = $pconfig;
336
	}
337
338 b05a8f35 jim-p
	$gateways_arr = return_gateways_array();
339
	if (is_array($gateways_arr)) {
340
		$pconfig = array();
341 69eefb50 Renato Botelho
		$pconfig['name'] = "dpinger";
342 b05a8f35 jim-p
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
343
		$services[] = $pconfig;
344
	}
345
346 33232486 Warren Baker
	if (isset($config['snmpd']['enable'])) {
347 e48cdc01 jim-p
		$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 dc0f709e Luiz Otavio O Souza
	if (ipsec_enabled()) {
368 e48cdc01 jim-p
		$pconfig = array();
369 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
370 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
371
		$services[] = $pconfig;
372
	}
373
374 aaa78416 jim-p
	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 e48cdc01 jim-p
	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 e8c516a0 Phil Davis
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
391 e48cdc01 jim-p
					$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 76692ad2 jim-p
function find_service_by_name($name) {
407
	$services = get_services();
408 61e047a5 Phil Davis
	foreach ($services as $service) {
409
		if ($service["name"] == $name) {
410 76692ad2 jim-p
			return $service;
411 61e047a5 Phil Davis
		}
412
	}
413 76692ad2 jim-p
	return array();
414
}
415
416 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
417
	$services = get_services();
418 61e047a5 Phil Davis
	foreach ($services as $service) {
419
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
420 6d9b1074 jim-p
			return $service;
421 61e047a5 Phil Davis
		}
422
	}
423 6d9b1074 jim-p
	return array();
424
}
425
426
function find_service_by_cp_zone($zone) {
427
	$services = get_services();
428 61e047a5 Phil Davis
	foreach ($services as $service) {
429
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
430 6d9b1074 jim-p
			return $service;
431 61e047a5 Phil Davis
		}
432
	}
433 6d9b1074 jim-p
	return array();
434
}
435
436 e48cdc01 jim-p
function service_name_compare($a, $b) {
437 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
438 e48cdc01 jim-p
		return 0;
439 61e047a5 Phil Davis
	}
440 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
441
}
442
443 095edfeb Stephen Beaver
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 e48cdc01 jim-p
function get_pkg_descr($package_name) {
451
	global $config;
452
	if (is_array($config['installedpackages']['package'])) {
453 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
454
			if ($pkg['name'] == $package_name) {
455 e48cdc01 jim-p
				return $pkg['descr'];
456 61e047a5 Phil Davis
			}
457 e48cdc01 jim-p
		}
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 cd41643d Renato Botelho
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
470 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
471 cd41643d Renato Botelho
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
472 61e047a5 Phil Davis
			}
473 e48cdc01 jim-p
			break;
474 18725085 Ermal
		case "vhosts-http":
475
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
476
			break;
477 9590e0de Phil Davis
		case "dhcrelay6":
478
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
479
			break;
480 e381cc01 Ermal
		case 'ipsec':
481
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
482
			break;
483 e48cdc01 jim-p
		default:
484
			$running = is_service_running($service['name']);
485
	}
486
	return $running;
487
}
488
489 36d1c798 jim-p
function get_service_status_icon($service, $withtext = true, $smallicon = false) {
490 e48cdc01 jim-p
	global $g;
491
	$output = "";
492 61e047a5 Phil Davis
	if (get_service_status($service)) {
493 92421710 jim-p
		$statustext = gettext("Running");
494 9f605c1c Hari
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
495 7ea65674 Jared Dillard
		$output .= ($smallicon) ? "fa fa-play" : "fa fa-lg fa-play";
496 9f605c1c Hari
		$output .= "\" ></i></a>";
497 61e047a5 Phil Davis
		if ($withtext) {
498 17b8c60a Colin Fleming
			$output .= "&nbsp;" . $statustext;
499 61e047a5 Phil Davis
		}
500 e48cdc01 jim-p
	} else {
501 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
502
		$statustext = ($service_enabled) ? gettext("Stopped") : gettext("Disabled");
503 9f605c1c Hari
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
504 7ea65674 Jared Dillard
		$output .= ($smallicon) ? "fa fa-times" : "fa fa-lg fa-times";
505 9f605c1c Hari
		$output .= "\" ></i></a>";
506 61e047a5 Phil Davis
		if ($withtext) {
507 9f605c1c Hari
			$output .= "&nbsp;" . $statustext;
508 61e047a5 Phil Davis
		}
509 e48cdc01 jim-p
	}
510
	return $output;
511
}
512
513 23a6f29b Stephen Beaver
/* 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 d8ed9328 Stephen Beaver
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 75daa54d PiBa-NL
			$link = '<a title="%s" href="/status_services.php?mode=%s&amp;service=' . $service['name'] . '">';
531 d8ed9328 Stephen Beaver
	}
532
533
	if (get_service_status($service)) {
534
		switch ($service['name']) {
535
			case "openvpn":
536 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}\">";
537 d8ed9328 Stephen Beaver
				break;
538
			case "captiveportal":
539 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}&amp;zone={$service['zone']}\">";
540 d8ed9328 Stephen Beaver
				break;
541
			default:
542 75daa54d PiBa-NL
				$output .= "<a href=\"/status_services.php?mode=restartservice&amp;service={$service['name']}\">";
543 d8ed9328 Stephen Beaver
		}
544 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
545 d8ed9328 Stephen Beaver
		switch ($service['name']) {
546
			case "openvpn":
547 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}\">";
548 d8ed9328 Stephen Beaver
				break;
549
			case "captiveportal":
550 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}&amp;zone={$service['zone']}\">";
551 d8ed9328 Stephen Beaver
				break;
552
			default:
553 75daa54d PiBa-NL
				$output .= "<a href=\"/status_services.php?mode=stopservice&amp;service={$service['name']}\">";
554 d8ed9328 Stephen Beaver
		}
555 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
556 d8ed9328 Stephen Beaver
	} 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 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
562 d8ed9328 Stephen Beaver
		}
563
	}
564
565
	return $output;
566
}
567 23a6f29b Stephen Beaver
*/
568 d8ed9328 Stephen Beaver
569 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
570 49840447 PiBa-NL
	global $g;
571 e48cdc01 jim-p
	$output = "";
572 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
573 1180e4f0 Sjon Hortensius
574
	switch ($service['name']) {
575
		case "openvpn":
576 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
577 1180e4f0 Sjon Hortensius
		break;
578
		case "captiveportal":
579 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
580 1180e4f0 Sjon Hortensius
		break;
581
		default:
582 ab7e04c7 Colin Fleming
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
583 1180e4f0 Sjon Hortensius
	}
584
585 61e047a5 Phil Davis
	if (get_service_status($service)) {
586 e48cdc01 jim-p
		switch ($service['name']) {
587
			case "openvpn":
588 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
589 e48cdc01 jim-p
				break;
590
			case "captiveportal":
591 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
592 e48cdc01 jim-p
				break;
593
			default:
594 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
595 e48cdc01 jim-p
		}
596 74b0f23e Stephen Beaver
597 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
598 74b0f23e Stephen Beaver
599 e48cdc01 jim-p
		switch ($service['name']) {
600
			case "openvpn":
601 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
602 e48cdc01 jim-p
				break;
603
			case "captiveportal":
604 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
605 e48cdc01 jim-p
				break;
606
			default:
607 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
608 e48cdc01 jim-p
		}
609 74b0f23e Stephen Beaver
610 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
611 74b0f23e Stephen Beaver
612 e48cdc01 jim-p
	} else {
613 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
614 1180e4f0 Sjon Hortensius
615 46bb8a0b Sjon Hortensius
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
616 1180e4f0 Sjon Hortensius
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
617 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
618 e48cdc01 jim-p
		}
619
	}
620 1180e4f0 Sjon Hortensius
621 e48cdc01 jim-p
	return $output;
622
}
623 ee6e6011 jim-p
624
function service_control_start($name, $extras) {
625
	global $g;
626 61e047a5 Phil Davis
	switch ($name) {
627 ee6e6011 jim-p
		case 'radvd':
628
			services_radvd_configure();
629
			break;
630
		case 'captiveportal':
631 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
632 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
633
			break;
634
		case 'ntpd':
635
		case 'openntpd':
636
			system_ntp_configure();
637
			break;
638 69eefb50 Renato Botelho
		case 'dpinger':
639 b05a8f35 jim-p
			setup_gateways_monitor();
640
			break;
641 ee6e6011 jim-p
		case 'bsnmpd':
642
			services_snmpd_configure();
643
			break;
644 9590e0de Phil Davis
		case 'dhcrelay':
645
			services_dhcrelay_configure();
646
			break;
647
		case 'dhcrelay6':
648
			services_dhcrelay6_configure();
649
			break;
650 ee6e6011 jim-p
		case 'dnsmasq':
651
			services_dnsmasq_configure();
652
			break;
653 a8604dc6 Phil Davis
		case 'unbound':
654
			services_unbound_configure();
655
			break;
656 ee6e6011 jim-p
		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 8b4abd59 Ermal
		case 'ipsec':
666 ee6e6011 jim-p
			vpn_ipsec_force_reload();
667
			break;
668 aaa78416 jim-p
		case 'sshd':
669
			send_event("service restart sshd");
670
			break;
671 ee6e6011 jim-p
		case 'openvpn':
672 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
673 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
674 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
675 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
676 61e047a5 Phil Davis
				if (file_exists($configfile)) {
677 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
678 61e047a5 Phil Davis
				}
679 ee6e6011 jim-p
			}
680
			break;
681
		case 'relayd':
682
			relayd_configure();
683
			break;
684
		default:
685
			start_service($name);
686
			break;
687
	}
688 086cf944 Phil Davis
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
689 ee6e6011 jim-p
}
690
function service_control_stop($name, $extras) {
691
	global $g;
692 61e047a5 Phil Davis
	switch ($name) {
693 ee6e6011 jim-p
		case 'radvd':
694
			killbypid("{$g['varrun_path']}/radvd.pid");
695
			break;
696
		case 'captiveportal':
697 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
698 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
699
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
700 ee6e6011 jim-p
			break;
701
		case 'ntpd':
702
			killbyname("ntpd");
703
			break;
704
		case 'openntpd':
705
			killbyname("openntpd");
706
			break;
707 69eefb50 Renato Botelho
		case 'dpinger':
708
			stop_dpinger();
709 b05a8f35 jim-p
			break;
710 ee6e6011 jim-p
		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 9590e0de Phil Davis
		case 'dhcrelay6':
723
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
724
			break;
725 ee6e6011 jim-p
		case 'dnsmasq':
726
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
727
			break;
728 33232486 Warren Baker
		case 'unbound':
729
			killbypid("{$g['varrun_path']}/unbound.pid");
730
			break;
731 ee6e6011 jim-p
		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 8b4abd59 Ermal
		case 'ipsec':
741
			exec("/usr/local/sbin/ipsec stop");
742 ee6e6011 jim-p
			break;
743
		case 'openvpn':
744 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
745 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
746 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
747 ee6e6011 jim-p
				$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 33232486 Warren Baker
761 ee6e6011 jim-p
function service_control_restart($name, $extras) {
762
	global $g;
763 61e047a5 Phil Davis
	switch ($name) {
764 ee6e6011 jim-p
		case 'radvd':
765
			services_radvd_configure();
766
			break;
767
		case 'captiveportal':
768 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
769 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
770
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
771 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
772
			break;
773
		case 'ntpd':
774
		case 'openntpd':
775
			system_ntp_configure();
776
			break;
777 69eefb50 Renato Botelho
		case 'dpinger':
778 b05a8f35 jim-p
			setup_gateways_monitor();
779
			break;
780 ee6e6011 jim-p
		case 'bsnmpd':
781
			services_snmpd_configure();
782
			break;
783 9590e0de Phil Davis
		case 'dhcrelay':
784
			services_dhcrelay_configure();
785
			break;
786
		case 'dhcrelay6':
787
			services_dhcrelay6_configure();
788
			break;
789 ee6e6011 jim-p
		case 'dnsmasq':
790
			services_dnsmasq_configure();
791
			break;
792 33232486 Warren Baker
		case 'unbound':
793
			services_unbound_configure();
794
			break;
795 ee6e6011 jim-p
		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 8b4abd59 Ermal
		case 'ipsec':
805 ee6e6011 jim-p
			vpn_ipsec_force_reload();
806
			break;
807 aaa78416 jim-p
		case 'sshd':
808
			send_event("service restart sshd");
809
			break;
810 ee6e6011 jim-p
		case 'openvpn':
811 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
812 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
813 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
814 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
815 61e047a5 Phil Davis
				if (file_exists($configfile)) {
816 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
817 61e047a5 Phil Davis
				}
818 ee6e6011 jim-p
			}
819
			break;
820
		case 'relayd':
821
			relayd_configure(true);
822
			break;
823
		default:
824
			restart_service($name);
825
			break;
826
	}
827 086cf944 Phil Davis
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
828 ee6e6011 jim-p
}
829
830 17098641 Ermal
?>