Project

General

Profile

Download (22.4 KB) Statistics
| Branch: | Tag: | Revision:
1 c83068f6 Colin Smith
<?php
2
/****h* pfSense/service-utils
3 61e047a5 Phil Davis
	NAME
4
		service-utils.inc - Service facility
5
	DESCRIPTION
6
		This file contains various functions used by the pfSense service facility.
7
	HISTORY
8
	$Id$
9
10
	Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
11
	All rights reserved.
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
33 c83068f6 Colin Smith
 */
34
35 ab1cf455 jim-p
require_once("globals.inc");
36 ee6e6011 jim-p
require_once("captiveportal.inc");
37
require_once("openvpn.inc");
38
require_once("ipsec.inc");
39
require_once("vpn.inc");
40
require_once("vslb.inc");
41 b05a8f35 jim-p
require_once("gwlb.inc");
42 523855b0 Scott Ullrich
43 e9d66ed4 phildd
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
44 c83068f6 Colin Smith
function write_rcfile($params) {
45 c2151973 Ermal
	global $g;
46 af799b48 Renato Botelho
47 6f931ad2 Phil Davis
	safe_mkdir(RCFILEPREFIX);
48 e9d66ed4 phildd
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
49 61e047a5 Phil Davis
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
50 af799b48 Renato Botelho
		return false;
51 61e047a5 Phil Davis
	}
52 c2151973 Ermal
53 61e047a5 Phil Davis
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
54 ba8495f0 Ermal
		return false;
55 61e047a5 Phil Davis
	}
56 af799b48 Renato Botelho
57 ba8495f0 Ermal
	$towrite = "#!/bin/sh\n";
58 4dddf8c3 Warren Baker
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
59 ba8495f0 Ermal
60 c83068f6 Colin Smith
	/* write our rc functions */
61 ba8495f0 Ermal
	$towrite .= "rc_start() {\n";
62
	$towrite .= "\t{$params['start']}\n";
63
	$towrite .= "}\n\n";
64 61e047a5 Phil Davis
	if (!empty($params['stop'])) {
65 c83068f6 Colin Smith
		$tokill =& $params['stop'];
66 61e047a5 Phil Davis
	} else if (!empty($params['executable'])) {
67 a8dbad89 Colin Smith
		/* just nuke the executable */
68 873c1701 Renato Botelho
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
69 c83068f6 Colin Smith
	} else {
70 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
71 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
72
	}
73 ba8495f0 Ermal
	$towrite .= "rc_stop() {\n";
74
	$towrite .= "\t{$tokill}\n";
75
	$towrite .= "}\n\n";
76 4685e464 Scott Ullrich
77 c83068f6 Colin Smith
	/* begin rcfile logic */
78 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";
79
80 44b19298 Ermal
	@file_put_contents($rcfile_fullname, $towrite);
81
	unset($towrite);
82 e9d66ed4 phildd
	@chmod("{$rcfile_fullname}", 0755);
83 ba8495f0 Ermal
84 c83068f6 Colin Smith
	return;
85
}
86 31f346a8 Colin Smith
87 58826c73 Colin Smith
function start_service($name) {
88 669e1adb Bill Marquette
	global $config;
89 ba8495f0 Ermal
90 61e047a5 Phil Davis
	if (empty($name)) {
91 b27ade8e Ermal
		return;
92 61e047a5 Phil Davis
	}
93 b27ade8e Ermal
94 44b19298 Ermal
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
95 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
96
			if (strtolower($service['name']) == strtolower($name)) {
97
				if ($service['rcfile']) {
98 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
99 ba8495f0 Ermal
					if (!empty($service['prefix'])) {
100 31f346a8 Colin Smith
						$prefix =& $service['prefix'];
101
					}
102 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
103 ba8495f0 Ermal
						mwexec_bg("{$prefix}{$service['rcfile']} start");
104 a4ea3340 Scott Ullrich
					}
105 31f346a8 Colin Smith
				}
106 61e047a5 Phil Davis
				if (!empty($service['startcmd'])) {
107 31f346a8 Colin Smith
					eval($service['startcmd']);
108 61e047a5 Phil Davis
				}
109 31f346a8 Colin Smith
				break;
110
			}
111
		}
112
	}
113
}
114
115 58826c73 Colin Smith
function stop_service($name) {
116 cd72ded3 Timo Boettcher
	global $config;
117 246a887a Ermal
118 61e047a5 Phil Davis
	if (empty($name)) {
119 8bf2e9e5 Ermal
		return;
120 61e047a5 Phil Davis
	}
121 8bf2e9e5 Ermal
122 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
123 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
124
			if (strtolower($service['name']) == strtolower($name)) {
125
				if ($service['rcfile']) {
126 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
127 61e047a5 Phil Davis
					if (!empty($service['prefix'])) {
128 cd72ded3 Timo Boettcher
						$prefix =& $service['prefix'];
129
					}
130 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
131 6ae78f08 Marcello Coutinho
						mwexec("{$prefix}{$service['rcfile']} stop");
132 098820e2 Ermal
					}
133
					return;
134 cd72ded3 Timo Boettcher
				}
135 61e047a5 Phil Davis
				if (!empty($service['stopcmd'])) {
136 cd72ded3 Timo Boettcher
					eval($service['stopcmd']);
137 b8877138 doktornotor
				} elseif (!empty($service['executable'])) {
138
					mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
139 61e047a5 Phil Davis
				}
140 ba8495f0 Ermal
141 cd72ded3 Timo Boettcher
				break;
142
			}
143
		}
144
	}
145 31f346a8 Colin Smith
}
146
147 58826c73 Colin Smith
function restart_service($name) {
148 cd72ded3 Timo Boettcher
	global $config;
149 ba8495f0 Ermal
150 61e047a5 Phil Davis
	if (empty($name)) {
151 8bf2e9e5 Ermal
		return;
152 61e047a5 Phil Davis
	}
153 8bf2e9e5 Ermal
154 6855f25d doktornotor
	if (is_service_running($name)) {
155
		stop_service($name);
156
	}
157 636ab238 Colin Smith
	start_service($name);
158 ba8495f0 Ermal
159 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
160 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
161
			if (strtolower($service['name']) == strtolower($name)) {
162
				if ($service['restartcmd']) {
163 cd72ded3 Timo Boettcher
					eval($service['restartcmd']);
164
				}
165
				break;
166
			}
167
		}
168
	}
169 31f346a8 Colin Smith
}
170 ec4e071a Colin Smith
171 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
172 61e047a5 Phil Davis
	if (!file_exists($pidfile)) {
173 ba8495f0 Ermal
		return false;
174 61e047a5 Phil Davis
	}
175 ba8495f0 Ermal
176 17098641 Ermal
	return (isvalidpid($pidfile));
177 cd72ded3 Timo Boettcher
}
178
179 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
180 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
181 61e047a5 Phil Davis
	if ($status != 0) {
182 ca88e48f Scott Ullrich
		return true;
183 61e047a5 Phil Davis
	}
184 ca88e48f Scott Ullrich
	return false;
185 8ff8520f Scott Ullrich
}
186
187 0661a033 Colin Smith
function restart_service_if_running($service) {
188
	global $config;
189 61e047a5 Phil Davis
	if (is_service_running($service)) {
190 0661a033 Colin Smith
		restart_service($service);
191 61e047a5 Phil Davis
	}
192 0661a033 Colin Smith
	return;
193
}
194
195 ccfc0269 phildd
function is_service_enabled($service_name) {
196
	global $config;
197 61e047a5 Phil Davis
	if ($service_name == "") {
198 ccfc0269 phildd
		return false;
199 61e047a5 Phil Davis
	}
200 762e8cf9 jim-p
	if (is_array($config['installedpackages'])) {
201
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
202 61e047a5 Phil Davis
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
203
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
204 762e8cf9 jim-p
			return false;
205
		}
206
	}
207 ccfc0269 phildd
	return true;
208
}
209
210 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
211
	global $config;
212 ba8495f0 Ermal
213 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
214
		foreach ($config['installedpackages']['service'] as $aservice) {
215
			if (strtolower($service) == strtolower($aservice['name'])) {
216 ba8495f0 Ermal
				if ($aservice['custom_php_service_status_command'] <> "") {
217
					eval("\$rc={$aservice['custom_php_service_status_command']};");
218 c2d49311 Eirik Oeverby
					return $rc;
219
				}
220 61e047a5 Phil Davis
				if (empty($aservice['executable'])) {
221 ec4e071a Colin Smith
					return false;
222 61e047a5 Phil Davis
				}
223
				if (is_process_running($aservice['executable'])) {
224 ba8495f0 Ermal
					return true;
225 61e047a5 Phil Davis
				}
226 ba8495f0 Ermal
227
				return false;
228 cd72ded3 Timo Boettcher
			}
229
		}
230
	}
231 ba8495f0 Ermal
232 61e047a5 Phil Davis
	if (is_process_running($service)) {
233 ba8495f0 Ermal
		return true;
234 61e047a5 Phil Davis
	}
235 ba8495f0 Ermal
236
	return false;
237 ec4e071a Colin Smith
}
238 ff074bf9 Scott Ullrich
239 e48cdc01 jim-p
function get_services() {
240
	global $config;
241 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
242 e48cdc01 jim-p
		$services = $config['installedpackages']['service'];
243 61e047a5 Phil Davis
	} else {
244 e48cdc01 jim-p
		$services = array();
245 61e047a5 Phil Davis
	}
246 e48cdc01 jim-p
247 61e047a5 Phil Davis
	/*
248
	 * Add services that are in the base.
249 e48cdc01 jim-p
	 */
250 33232486 Warren Baker
	if (is_radvd_enabled()) {
251 e48cdc01 jim-p
		$pconfig = array();
252
		$pconfig['name'] = "radvd";
253
		$pconfig['description'] = gettext("Router Advertisement Daemon");
254
		$services[] = $pconfig;
255
	}
256
257 33232486 Warren Baker
	if (isset($config['dnsmasq']['enable'])) {
258 e48cdc01 jim-p
		$pconfig = array();
259
		$pconfig['name'] = "dnsmasq";
260
		$pconfig['description'] = gettext("DNS Forwarder");
261
		$services[] = $pconfig;
262
	}
263
264 33232486 Warren Baker
	if (isset($config['unbound']['enable'])) {
265
		$pconfig = array();
266
		$pconfig['name'] = "unbound";
267 a3fb1412 Phil Davis
		$pconfig['description'] = gettext("DNS Resolver");
268 33232486 Warren Baker
		$services[] = $pconfig;
269
	}
270
271 e48cdc01 jim-p
	$pconfig = array();
272
	$pconfig['name'] = "ntpd";
273
	$pconfig['description'] = gettext("NTP clock sync");
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 61e047a5 Phil Davis
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
293 e48cdc01 jim-p
			$iflist[$if] = $if;
294 61e047a5 Phil Davis
		}
295 e48cdc01 jim-p
	}
296
297 33232486 Warren Baker
	if (isset($config['dhcrelay']['enable'])) {
298 e48cdc01 jim-p
		$pconfig = array();
299
		$pconfig['name'] = "dhcrelay";
300
		$pconfig['description'] = gettext("DHCP Relay");
301
		$services[] = $pconfig;
302
	}
303
304 33232486 Warren Baker
	if (isset($config['dhcrelay6']['enable'])) {
305 9590e0de Phil Davis
		$pconfig = array();
306
		$pconfig['name'] = "dhcrelay6";
307
		$pconfig['description'] = gettext("DHCPv6 Relay");
308
		$services[] = $pconfig;
309
	}
310
311 33232486 Warren Baker
	if (is_dhcp_server_enabled()) {
312 e48cdc01 jim-p
		$pconfig = array();
313
		$pconfig['name'] = "dhcpd";
314
		$pconfig['description'] = gettext("DHCP Service");
315
		$services[] = $pconfig;
316
	}
317
318 b05a8f35 jim-p
	$gateways_arr = return_gateways_array();
319
	if (is_array($gateways_arr)) {
320
		$pconfig = array();
321 69eefb50 Renato Botelho
		$pconfig['name'] = "dpinger";
322 b05a8f35 jim-p
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
323
		$services[] = $pconfig;
324
	}
325
326 33232486 Warren Baker
	if (isset($config['snmpd']['enable'])) {
327 e48cdc01 jim-p
		$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 dc0f709e Luiz Otavio O Souza
	if (ipsec_enabled()) {
348 e48cdc01 jim-p
		$pconfig = array();
349 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
350 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
351
		$services[] = $pconfig;
352
	}
353
354 aaa78416 jim-p
	if (isset($config['system']['enablesshd'])) {
355
		$pconfig = array();
356
		$pconfig['name'] = "sshd";
357
		$pconfig['description'] = gettext("Secure Shell Daemon");
358
		$services[] = $pconfig;
359
	}
360
361 e48cdc01 jim-p
	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 (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
378
		$pconfig = array();
379
		$pconfig['name'] = "relayd";
380
		$pconfig['description'] = gettext("Server load balancing daemon");
381
		$services[] = $pconfig;
382
	}
383
	return $services;
384
}
385
386 76692ad2 jim-p
function find_service_by_name($name) {
387
	$services = get_services();
388 61e047a5 Phil Davis
	foreach ($services as $service) {
389
		if ($service["name"] == $name) {
390 76692ad2 jim-p
			return $service;
391 61e047a5 Phil Davis
		}
392
	}
393 76692ad2 jim-p
	return array();
394
}
395
396 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
397
	$services = get_services();
398 61e047a5 Phil Davis
	foreach ($services as $service) {
399
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
400 6d9b1074 jim-p
			return $service;
401 61e047a5 Phil Davis
		}
402
	}
403 6d9b1074 jim-p
	return array();
404
}
405
406
function find_service_by_cp_zone($zone) {
407
	$services = get_services();
408 61e047a5 Phil Davis
	foreach ($services as $service) {
409
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
410 6d9b1074 jim-p
			return $service;
411 61e047a5 Phil Davis
		}
412
	}
413 6d9b1074 jim-p
	return array();
414
}
415
416 e48cdc01 jim-p
function service_name_compare($a, $b) {
417 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
418 e48cdc01 jim-p
		return 0;
419 61e047a5 Phil Davis
	}
420 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
421
}
422
423
function get_pkg_descr($package_name) {
424
	global $config;
425
	if (is_array($config['installedpackages']['package'])) {
426 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
427
			if ($pkg['name'] == $package_name) {
428 e48cdc01 jim-p
				return $pkg['descr'];
429 61e047a5 Phil Davis
			}
430 e48cdc01 jim-p
		}
431
	}
432
	return gettext("Not available.");
433
}
434
435
function get_service_status($service) {
436
	global $g;
437
	switch ($service['name']) {
438
		case "openvpn":
439
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
440
			break;
441
		case "captiveportal":
442 cd41643d Renato Botelho
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
443 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
444 cd41643d Renato Botelho
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
445 61e047a5 Phil Davis
			}
446 e48cdc01 jim-p
			break;
447 18725085 Ermal
		case "vhosts-http":
448
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
449
			break;
450 9590e0de Phil Davis
		case "dhcrelay6":
451
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
452
			break;
453 e381cc01 Ermal
		case 'ipsec':
454
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
455
			break;
456 e48cdc01 jim-p
		default:
457
			$running = is_service_running($service['name']);
458
	}
459
	return $running;
460
}
461
462 36d1c798 jim-p
function get_service_status_icon($service, $withtext = true, $smallicon = false) {
463 e48cdc01 jim-p
	global $g;
464
	$output = "";
465 61e047a5 Phil Davis
	if (get_service_status($service)) {
466 92421710 jim-p
		$statustext = gettext("Running");
467 9f605c1c Hari
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
468 7ea65674 Jared Dillard
		$output .= ($smallicon) ? "fa fa-play" : "fa fa-lg fa-play";
469 9f605c1c Hari
		$output .= "\" ></i></a>";
470 61e047a5 Phil Davis
		if ($withtext) {
471 17b8c60a Colin Fleming
			$output .= "&nbsp;" . $statustext;
472 61e047a5 Phil Davis
		}
473 e48cdc01 jim-p
	} else {
474 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
475
		$statustext = ($service_enabled) ? gettext("Stopped") : gettext("Disabled");
476 9f605c1c Hari
		$output .= "<a title=\"" . sprintf(gettext("%s Service is"), $service["name"]) . " {$statustext}\" ><i class=\"";
477 7ea65674 Jared Dillard
		$output .= ($smallicon) ? "fa fa-times" : "fa fa-lg fa-times";
478 9f605c1c Hari
		$output .= "\" ></i></a>";
479 61e047a5 Phil Davis
		if ($withtext) {
480 9f605c1c Hari
			$output .= "&nbsp;" . $statustext;
481 61e047a5 Phil Davis
		}
482 e48cdc01 jim-p
	}
483
	return $output;
484
}
485
486 d8ed9328 Stephen Beaver
487
// This version proved GET formatted links
488
function get_service_control_GET_links($service, $addname = false) {
489
	global $g;
490
	$output = "";
491
	$stitle = ($addname) ? $service['name'] . " " : "";
492
493
	switch ($service['name']) {
494
		case "openvpn":
495
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service='.$service['name'] . '&amp;vpnmode=' . $service['mode'] . '&amp;id=' . $service['vpnid'] . '">';
496
		break;
497
		case "captiveportal":
498
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service=' . $service['name'] . '&amp;zone=' . $service['zone'] . '">';
499
		break;
500
		default:
501
			$link = '<a title="%s" href="status_services.php?mode=%s&amp;service=' . $service['name'] . '">';
502
	}
503
504
	if (get_service_status($service)) {
505
		switch ($service['name']) {
506
			case "openvpn":
507 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}\">";
508 d8ed9328 Stephen Beaver
				break;
509
			case "captiveportal":
510 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}&amp;zone={$service['zone']}\">";
511 d8ed9328 Stephen Beaver
				break;
512
			default:
513 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=restartservice&amp;service={$service['name']}\">";
514 d8ed9328 Stephen Beaver
		}
515 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
516 d8ed9328 Stephen Beaver
		switch ($service['name']) {
517
			case "openvpn":
518 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}\">";
519 d8ed9328 Stephen Beaver
				break;
520
			case "captiveportal":
521 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}&amp;zone={$service['zone']}\">";
522 d8ed9328 Stephen Beaver
				break;
523
			default:
524 ab7e04c7 Colin Fleming
				$output .= "<a href=\"status_services.php?mode=stopservice&amp;service={$service['name']}\">";
525 d8ed9328 Stephen Beaver
		}
526 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
527 d8ed9328 Stephen Beaver
	} else {
528
		$service_enabled = is_service_enabled($service['name']);
529
530
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
531
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
532 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
533 d8ed9328 Stephen Beaver
		}
534
	}
535
536
	return $output;
537
}
538
539 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
540 49840447 PiBa-NL
	global $g;
541 e48cdc01 jim-p
	$output = "";
542 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
543 1180e4f0 Sjon Hortensius
544
	switch ($service['name']) {
545
		case "openvpn":
546 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
547 1180e4f0 Sjon Hortensius
		break;
548
		case "captiveportal":
549 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
550 1180e4f0 Sjon Hortensius
		break;
551
		default:
552 ab7e04c7 Colin Fleming
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
553 1180e4f0 Sjon Hortensius
	}
554
555 61e047a5 Phil Davis
	if (get_service_status($service)) {
556 e48cdc01 jim-p
		switch ($service['name']) {
557
			case "openvpn":
558 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
559 e48cdc01 jim-p
				break;
560
			case "captiveportal":
561 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
562 e48cdc01 jim-p
				break;
563
			default:
564 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
565 e48cdc01 jim-p
		}
566 74b0f23e Stephen Beaver
567 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
568 74b0f23e Stephen Beaver
569 e48cdc01 jim-p
		switch ($service['name']) {
570
			case "openvpn":
571 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
572 e48cdc01 jim-p
				break;
573
			case "captiveportal":
574 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
575 e48cdc01 jim-p
				break;
576
			default:
577 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
578 e48cdc01 jim-p
		}
579 74b0f23e Stephen Beaver
580 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
581 74b0f23e Stephen Beaver
582 e48cdc01 jim-p
	} else {
583 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
584 1180e4f0 Sjon Hortensius
585 46bb8a0b Sjon Hortensius
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
586 1180e4f0 Sjon Hortensius
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
587 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
588 e48cdc01 jim-p
		}
589
	}
590 1180e4f0 Sjon Hortensius
591 e48cdc01 jim-p
	return $output;
592
}
593 ee6e6011 jim-p
594
function service_control_start($name, $extras) {
595
	global $g;
596 61e047a5 Phil Davis
	switch ($name) {
597 ee6e6011 jim-p
		case 'radvd':
598
			services_radvd_configure();
599
			break;
600
		case 'captiveportal':
601 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
602 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
603
			break;
604
		case 'ntpd':
605
		case 'openntpd':
606
			system_ntp_configure();
607
			break;
608 69eefb50 Renato Botelho
		case 'dpinger':
609 b05a8f35 jim-p
			setup_gateways_monitor();
610
			break;
611 ee6e6011 jim-p
		case 'bsnmpd':
612
			services_snmpd_configure();
613
			break;
614 9590e0de Phil Davis
		case 'dhcrelay':
615
			services_dhcrelay_configure();
616
			break;
617
		case 'dhcrelay6':
618
			services_dhcrelay6_configure();
619
			break;
620 ee6e6011 jim-p
		case 'dnsmasq':
621
			services_dnsmasq_configure();
622
			break;
623 a8604dc6 Phil Davis
		case 'unbound':
624
			services_unbound_configure();
625
			break;
626 ee6e6011 jim-p
		case 'dhcpd':
627
			services_dhcpd_configure();
628
			break;
629
		case 'igmpproxy':
630
			services_igmpproxy_configure();
631
			break;
632
		case 'miniupnpd':
633
			upnp_action('start');
634
			break;
635 8b4abd59 Ermal
		case 'ipsec':
636 ee6e6011 jim-p
			vpn_ipsec_force_reload();
637
			break;
638 aaa78416 jim-p
		case 'sshd':
639
			send_event("service restart sshd");
640
			break;
641 ee6e6011 jim-p
		case 'openvpn':
642 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
643 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
644 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
645 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
646 61e047a5 Phil Davis
				if (file_exists($configfile)) {
647 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
648 61e047a5 Phil Davis
				}
649 ee6e6011 jim-p
			}
650
			break;
651
		case 'relayd':
652
			relayd_configure();
653
			break;
654
		default:
655
			start_service($name);
656
			break;
657
	}
658 086cf944 Phil Davis
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
659 ee6e6011 jim-p
}
660
function service_control_stop($name, $extras) {
661
	global $g;
662 61e047a5 Phil Davis
	switch ($name) {
663 ee6e6011 jim-p
		case 'radvd':
664
			killbypid("{$g['varrun_path']}/radvd.pid");
665
			break;
666
		case 'captiveportal':
667 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
668 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
669
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
670 ee6e6011 jim-p
			break;
671
		case 'ntpd':
672
			killbyname("ntpd");
673
			break;
674
		case 'openntpd':
675
			killbyname("openntpd");
676
			break;
677 69eefb50 Renato Botelho
		case 'dpinger':
678
			stop_dpinger();
679 b05a8f35 jim-p
			break;
680 ee6e6011 jim-p
		case 'bsnmpd':
681
			killbypid("{$g['varrun_path']}/snmpd.pid");
682
			break;
683
		case 'choparp':
684
			killbyname("choparp");
685
			break;
686
		case 'dhcpd':
687
			killbyname("dhcpd");
688
			break;
689
		case 'dhcrelay':
690
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
691
			break;
692 9590e0de Phil Davis
		case 'dhcrelay6':
693
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
694
			break;
695 ee6e6011 jim-p
		case 'dnsmasq':
696
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
697
			break;
698 33232486 Warren Baker
		case 'unbound':
699
			killbypid("{$g['varrun_path']}/unbound.pid");
700
			break;
701 ee6e6011 jim-p
		case 'igmpproxy':
702
			killbyname("igmpproxy");
703
			break;
704
		case 'miniupnpd':
705
			upnp_action('stop');
706
			break;
707
		case 'sshd':
708
			killbyname("sshd");
709
			break;
710 8b4abd59 Ermal
		case 'ipsec':
711
			exec("/usr/local/sbin/ipsec stop");
712 ee6e6011 jim-p
			break;
713
		case 'openvpn':
714 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
715 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
716 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
717 ee6e6011 jim-p
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
718
				killbypid($pidfile);
719
			}
720
			break;
721
		case 'relayd':
722
			mwexec('pkill relayd');
723
			break;
724
		default:
725
			stop_service($name);
726
			break;
727
	}
728
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
729
}
730 33232486 Warren Baker
731 ee6e6011 jim-p
function service_control_restart($name, $extras) {
732
	global $g;
733 61e047a5 Phil Davis
	switch ($name) {
734 ee6e6011 jim-p
		case 'radvd':
735
			services_radvd_configure();
736
			break;
737
		case 'captiveportal':
738 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
739 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
740
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
741 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
742
			break;
743
		case 'ntpd':
744
		case 'openntpd':
745
			system_ntp_configure();
746
			break;
747 69eefb50 Renato Botelho
		case 'dpinger':
748 b05a8f35 jim-p
			setup_gateways_monitor();
749
			break;
750 ee6e6011 jim-p
		case 'bsnmpd':
751
			services_snmpd_configure();
752
			break;
753 9590e0de Phil Davis
		case 'dhcrelay':
754
			services_dhcrelay_configure();
755
			break;
756
		case 'dhcrelay6':
757
			services_dhcrelay6_configure();
758
			break;
759 ee6e6011 jim-p
		case 'dnsmasq':
760
			services_dnsmasq_configure();
761
			break;
762 33232486 Warren Baker
		case 'unbound':
763
			services_unbound_configure();
764
			break;
765 ee6e6011 jim-p
		case 'dhcpd':
766
			services_dhcpd_configure();
767
			break;
768
		case 'igmpproxy':
769
			services_igmpproxy_configure();
770
			break;
771
		case 'miniupnpd':
772
			upnp_action('restart');
773
			break;
774 8b4abd59 Ermal
		case 'ipsec':
775 ee6e6011 jim-p
			vpn_ipsec_force_reload();
776
			break;
777 aaa78416 jim-p
		case 'sshd':
778
			send_event("service restart sshd");
779
			break;
780 ee6e6011 jim-p
		case 'openvpn':
781 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
782 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
783 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
784 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
785 61e047a5 Phil Davis
				if (file_exists($configfile)) {
786 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
787 61e047a5 Phil Davis
				}
788 ee6e6011 jim-p
			}
789
			break;
790
		case 'relayd':
791
			relayd_configure(true);
792
			break;
793
		default:
794
			restart_service($name);
795
			break;
796
	}
797 086cf944 Phil Davis
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
798 ee6e6011 jim-p
}
799
800 17098641 Ermal
?>