Project

General

Profile

Download (20.5 KB) Statistics
| Branch: | Tag: | Revision:
1 c83068f6 Colin Smith
<?php
2 09221bc3 Renato Botelho
/*
3 8acd654a Renato Botelho
 * service-utils.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2005-2016 Rubicon Communications, LLC (Netgate)
7 aaec5634 Renato Botelho
 * Copyright (c) 2005-2006 Colin Smith (ethethlay@gmail.com)
8 8acd654a Renato Botelho
 * All rights reserved.
9
 *
10 f1f43826 Stephen Beaver
 * Licensed under the Apache License, Version 2.0 (the "License");
11
 * you may not use this file except in compliance with the License.
12
 * You may obtain a copy of the License at
13 8acd654a Renato Botelho
 *
14 f1f43826 Stephen Beaver
 * http://www.apache.org/licenses/LICENSE-2.0
15 8acd654a Renato Botelho
 *
16 f1f43826 Stephen Beaver
 * Unless required by applicable law or agreed to in writing, software
17
 * distributed under the License is distributed on an "AS IS" BASIS,
18
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
 * See the License for the specific language governing permissions and
20
 * limitations under the License.
21 8acd654a Renato Botelho
 */
22 c83068f6 Colin Smith
23 ab1cf455 jim-p
require_once("globals.inc");
24 ee6e6011 jim-p
require_once("captiveportal.inc");
25
require_once("openvpn.inc");
26
require_once("ipsec.inc");
27
require_once("vpn.inc");
28
require_once("vslb.inc");
29 b05a8f35 jim-p
require_once("gwlb.inc");
30 523855b0 Scott Ullrich
31 e9d66ed4 phildd
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
32 c83068f6 Colin Smith
function write_rcfile($params) {
33 c2151973 Ermal
	global $g;
34 af799b48 Renato Botelho
35 6f931ad2 Phil Davis
	safe_mkdir(RCFILEPREFIX);
36 e9d66ed4 phildd
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
37 61e047a5 Phil Davis
	if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) {
38 af799b48 Renato Botelho
		return false;
39 61e047a5 Phil Davis
	}
40 c2151973 Ermal
41 61e047a5 Phil Davis
	if (!is_writable($rcfile_fullname) || empty($params['start'])) {
42 ba8495f0 Ermal
		return false;
43 61e047a5 Phil Davis
	}
44 af799b48 Renato Botelho
45 ba8495f0 Ermal
	$towrite = "#!/bin/sh\n";
46 4dddf8c3 Warren Baker
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
47 ba8495f0 Ermal
48 c83068f6 Colin Smith
	/* write our rc functions */
49 ba8495f0 Ermal
	$towrite .= "rc_start() {\n";
50
	$towrite .= "\t{$params['start']}\n";
51
	$towrite .= "}\n\n";
52 61e047a5 Phil Davis
	if (!empty($params['stop'])) {
53 c83068f6 Colin Smith
		$tokill =& $params['stop'];
54 61e047a5 Phil Davis
	} else if (!empty($params['executable'])) {
55 a8dbad89 Colin Smith
		/* just nuke the executable */
56 873c1701 Renato Botelho
		$tokill = "/usr/bin/killall " . escapeshellarg($params['executable']);
57 c83068f6 Colin Smith
	} else {
58 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
59 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
60
	}
61 ba8495f0 Ermal
	$towrite .= "rc_stop() {\n";
62
	$towrite .= "\t{$tokill}\n";
63
	$towrite .= "}\n\n";
64 4685e464 Scott Ullrich
65 c83068f6 Colin Smith
	/* begin rcfile logic */
66 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";
67
68 44b19298 Ermal
	@file_put_contents($rcfile_fullname, $towrite);
69
	unset($towrite);
70 e9d66ed4 phildd
	@chmod("{$rcfile_fullname}", 0755);
71 ba8495f0 Ermal
72 c83068f6 Colin Smith
	return;
73
}
74 31f346a8 Colin Smith
75 58826c73 Colin Smith
function start_service($name) {
76 669e1adb Bill Marquette
	global $config;
77 ba8495f0 Ermal
78 61e047a5 Phil Davis
	if (empty($name)) {
79 b27ade8e Ermal
		return;
80 61e047a5 Phil Davis
	}
81 b27ade8e Ermal
82 44b19298 Ermal
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
83 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
84
			if (strtolower($service['name']) == strtolower($name)) {
85
				if ($service['rcfile']) {
86 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
87 ba8495f0 Ermal
					if (!empty($service['prefix'])) {
88 31f346a8 Colin Smith
						$prefix =& $service['prefix'];
89
					}
90 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
91 ba8495f0 Ermal
						mwexec_bg("{$prefix}{$service['rcfile']} start");
92 a4ea3340 Scott Ullrich
					}
93 31f346a8 Colin Smith
				}
94 61e047a5 Phil Davis
				if (!empty($service['startcmd'])) {
95 31f346a8 Colin Smith
					eval($service['startcmd']);
96 61e047a5 Phil Davis
				}
97 31f346a8 Colin Smith
				break;
98
			}
99
		}
100
	}
101
}
102
103 58826c73 Colin Smith
function stop_service($name) {
104 cd72ded3 Timo Boettcher
	global $config;
105 246a887a Ermal
106 61e047a5 Phil Davis
	if (empty($name)) {
107 8bf2e9e5 Ermal
		return;
108 61e047a5 Phil Davis
	}
109 8bf2e9e5 Ermal
110 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
111 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
112
			if (strtolower($service['name']) == strtolower($name)) {
113
				if ($service['rcfile']) {
114 e9d66ed4 phildd
					$prefix = RCFILEPREFIX;
115 61e047a5 Phil Davis
					if (!empty($service['prefix'])) {
116 cd72ded3 Timo Boettcher
						$prefix =& $service['prefix'];
117
					}
118 61e047a5 Phil Davis
					if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
119 6ae78f08 Marcello Coutinho
						mwexec("{$prefix}{$service['rcfile']} stop");
120 098820e2 Ermal
					}
121
					return;
122 cd72ded3 Timo Boettcher
				}
123 61e047a5 Phil Davis
				if (!empty($service['stopcmd'])) {
124 cd72ded3 Timo Boettcher
					eval($service['stopcmd']);
125 b8877138 doktornotor
				} elseif (!empty($service['executable'])) {
126
					mwexec("/usr/bin/killall " . escapeshellarg($service['executable']));
127 61e047a5 Phil Davis
				}
128 ba8495f0 Ermal
129 cd72ded3 Timo Boettcher
				break;
130
			}
131
		}
132
	}
133 31f346a8 Colin Smith
}
134
135 58826c73 Colin Smith
function restart_service($name) {
136 cd72ded3 Timo Boettcher
	global $config;
137 ba8495f0 Ermal
138 61e047a5 Phil Davis
	if (empty($name)) {
139 8bf2e9e5 Ermal
		return;
140 61e047a5 Phil Davis
	}
141 8bf2e9e5 Ermal
142 6855f25d doktornotor
	if (is_service_running($name)) {
143
		stop_service($name);
144
	}
145 636ab238 Colin Smith
	start_service($name);
146 ba8495f0 Ermal
147 b4d772dc Ermal Luçi
	if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
148 61e047a5 Phil Davis
		foreach ($config['installedpackages']['service'] as $service) {
149
			if (strtolower($service['name']) == strtolower($name)) {
150
				if ($service['restartcmd']) {
151 cd72ded3 Timo Boettcher
					eval($service['restartcmd']);
152
				}
153
				break;
154
			}
155
		}
156
	}
157 31f346a8 Colin Smith
}
158 ec4e071a Colin Smith
159 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
160 61e047a5 Phil Davis
	if (!file_exists($pidfile)) {
161 ba8495f0 Ermal
		return false;
162 61e047a5 Phil Davis
	}
163 ba8495f0 Ermal
164 17098641 Ermal
	return (isvalidpid($pidfile));
165 cd72ded3 Timo Boettcher
}
166
167 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
168 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
169 61e047a5 Phil Davis
	if ($status != 0) {
170 ca88e48f Scott Ullrich
		return true;
171 61e047a5 Phil Davis
	}
172 ca88e48f Scott Ullrich
	return false;
173 8ff8520f Scott Ullrich
}
174
175 0661a033 Colin Smith
function restart_service_if_running($service) {
176
	global $config;
177 61e047a5 Phil Davis
	if (is_service_running($service)) {
178 0661a033 Colin Smith
		restart_service($service);
179 61e047a5 Phil Davis
	}
180 0661a033 Colin Smith
	return;
181
}
182
183 ccfc0269 phildd
function is_service_enabled($service_name) {
184
	global $config;
185 61e047a5 Phil Davis
	if ($service_name == "") {
186 ccfc0269 phildd
		return false;
187 61e047a5 Phil Davis
	}
188 762e8cf9 jim-p
	if (is_array($config['installedpackages'])) {
189
		if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) &&
190 61e047a5 Phil Davis
		    ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) ||
191
		    ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) {
192 762e8cf9 jim-p
			return false;
193
		}
194
	}
195 ccfc0269 phildd
	return true;
196
}
197
198 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
199
	global $config;
200 ba8495f0 Ermal
201 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
202
		foreach ($config['installedpackages']['service'] as $aservice) {
203
			if (strtolower($service) == strtolower($aservice['name'])) {
204 ba8495f0 Ermal
				if ($aservice['custom_php_service_status_command'] <> "") {
205
					eval("\$rc={$aservice['custom_php_service_status_command']};");
206 c2d49311 Eirik Oeverby
					return $rc;
207
				}
208 61e047a5 Phil Davis
				if (empty($aservice['executable'])) {
209 ec4e071a Colin Smith
					return false;
210 61e047a5 Phil Davis
				}
211
				if (is_process_running($aservice['executable'])) {
212 ba8495f0 Ermal
					return true;
213 61e047a5 Phil Davis
				}
214 ba8495f0 Ermal
215
				return false;
216 cd72ded3 Timo Boettcher
			}
217
		}
218
	}
219 ba8495f0 Ermal
220 61e047a5 Phil Davis
	if (is_process_running($service)) {
221 ba8495f0 Ermal
		return true;
222 61e047a5 Phil Davis
	}
223 ba8495f0 Ermal
224
	return false;
225 ec4e071a Colin Smith
}
226 ff074bf9 Scott Ullrich
227 e48cdc01 jim-p
function get_services() {
228
	global $config;
229 61e047a5 Phil Davis
	if (is_array($config['installedpackages']['service'])) {
230 e48cdc01 jim-p
		$services = $config['installedpackages']['service'];
231 61e047a5 Phil Davis
	} else {
232 e48cdc01 jim-p
		$services = array();
233 61e047a5 Phil Davis
	}
234 e48cdc01 jim-p
235 61e047a5 Phil Davis
	/*
236
	 * Add services that are in the base.
237 e48cdc01 jim-p
	 */
238 33232486 Warren Baker
	if (is_radvd_enabled()) {
239 e48cdc01 jim-p
		$pconfig = array();
240
		$pconfig['name'] = "radvd";
241
		$pconfig['description'] = gettext("Router Advertisement Daemon");
242
		$services[] = $pconfig;
243
	}
244
245 33232486 Warren Baker
	if (isset($config['dnsmasq']['enable'])) {
246 e48cdc01 jim-p
		$pconfig = array();
247
		$pconfig['name'] = "dnsmasq";
248
		$pconfig['description'] = gettext("DNS Forwarder");
249
		$services[] = $pconfig;
250
	}
251
252 33232486 Warren Baker
	if (isset($config['unbound']['enable'])) {
253
		$pconfig = array();
254
		$pconfig['name'] = "unbound";
255 a3fb1412 Phil Davis
		$pconfig['description'] = gettext("DNS Resolver");
256 33232486 Warren Baker
		$services[] = $pconfig;
257
	}
258
259 e48cdc01 jim-p
	$pconfig = array();
260
	$pconfig['name'] = "ntpd";
261
	$pconfig['description'] = gettext("NTP clock sync");
262
	$services[] = $pconfig;
263
264
	if (is_array($config['captiveportal'])) {
265
		foreach ($config['captiveportal'] as $zone => $setting) {
266
			if (isset($setting['enable'])) {
267
				$pconfig = array();
268
				$pconfig['name'] = "captiveportal";
269
				$pconfig['zone'] = $zone;
270 e8c516a0 Phil Davis
				$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']);
271 e48cdc01 jim-p
				$services[] = $pconfig;
272
			}
273
		}
274
	}
275
276
	$iflist = array();
277
	$ifdescrs = get_configured_interface_list();
278
	foreach ($ifdescrs as $if) {
279
		$oc = $config['interfaces'][$if];
280 61e047a5 Phil Davis
		if ($oc['if'] && (!link_interface_to_bridge($if))) {
281 e48cdc01 jim-p
			$iflist[$if] = $if;
282 61e047a5 Phil Davis
		}
283 e48cdc01 jim-p
	}
284
285 33232486 Warren Baker
	if (isset($config['dhcrelay']['enable'])) {
286 e48cdc01 jim-p
		$pconfig = array();
287
		$pconfig['name'] = "dhcrelay";
288
		$pconfig['description'] = gettext("DHCP Relay");
289
		$services[] = $pconfig;
290
	}
291
292 33232486 Warren Baker
	if (isset($config['dhcrelay6']['enable'])) {
293 9590e0de Phil Davis
		$pconfig = array();
294
		$pconfig['name'] = "dhcrelay6";
295
		$pconfig['description'] = gettext("DHCPv6 Relay");
296
		$services[] = $pconfig;
297
	}
298
299 33232486 Warren Baker
	if (is_dhcp_server_enabled()) {
300 e48cdc01 jim-p
		$pconfig = array();
301
		$pconfig['name'] = "dhcpd";
302
		$pconfig['description'] = gettext("DHCP Service");
303
		$services[] = $pconfig;
304
	}
305
306 b05a8f35 jim-p
	$gateways_arr = return_gateways_array();
307
	if (is_array($gateways_arr)) {
308
		$pconfig = array();
309 69eefb50 Renato Botelho
		$pconfig['name'] = "dpinger";
310 b05a8f35 jim-p
		$pconfig['description'] = gettext("Gateway Monitoring Daemon");
311
		$services[] = $pconfig;
312
	}
313
314 33232486 Warren Baker
	if (isset($config['snmpd']['enable'])) {
315 e48cdc01 jim-p
		$pconfig = array();
316
		$pconfig['name'] = "bsnmpd";
317
		$pconfig['description'] = gettext("SNMP Service");
318
		$services[] = $pconfig;
319
	}
320
321
	if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
322
		$pconfig = array();
323
		$pconfig['name'] = "igmpproxy";
324
		$pconfig['description'] = gettext("IGMP proxy");
325
		$services[] = $pconfig;
326
	}
327
328
	if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
329
		$pconfig = array();
330
		$pconfig['name'] = "miniupnpd";
331
		$pconfig['description'] = gettext("UPnP Service");
332
		$services[] = $pconfig;
333
	}
334
335 dc0f709e Luiz Otavio O Souza
	if (ipsec_enabled()) {
336 e48cdc01 jim-p
		$pconfig = array();
337 8b4abd59 Ermal
		$pconfig['name'] = "ipsec";
338 e48cdc01 jim-p
		$pconfig['description'] = gettext("IPsec VPN");
339
		$services[] = $pconfig;
340
	}
341
342 aaa78416 jim-p
	if (isset($config['system']['enablesshd'])) {
343
		$pconfig = array();
344
		$pconfig['name'] = "sshd";
345
		$pconfig['description'] = gettext("Secure Shell Daemon");
346
		$services[] = $pconfig;
347
	}
348
349 e48cdc01 jim-p
	foreach (array('server', 'client') as $mode) {
350
		if (is_array($config['openvpn']["openvpn-{$mode}"])) {
351
			foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
352
				if (!isset($setting['disable'])) {
353
					$pconfig = array();
354
					$pconfig['name'] = "openvpn";
355
					$pconfig['mode'] = $mode;
356
					$pconfig['id'] = $id;
357
					$pconfig['vpnid'] = $setting['vpnid'];
358 e8c516a0 Phil Davis
					$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']);
359 e48cdc01 jim-p
					$services[] = $pconfig;
360
				}
361
			}
362
		}
363
	}
364
365
	if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
366
		$pconfig = array();
367
		$pconfig['name'] = "relayd";
368
		$pconfig['description'] = gettext("Server load balancing daemon");
369
		$services[] = $pconfig;
370
	}
371
	return $services;
372
}
373
374 76692ad2 jim-p
function find_service_by_name($name) {
375
	$services = get_services();
376 61e047a5 Phil Davis
	foreach ($services as $service) {
377
		if ($service["name"] == $name) {
378 76692ad2 jim-p
			return $service;
379 61e047a5 Phil Davis
		}
380
	}
381 76692ad2 jim-p
	return array();
382
}
383
384 6d9b1074 jim-p
function find_service_by_openvpn_vpnid($vpnid) {
385
	$services = get_services();
386 61e047a5 Phil Davis
	foreach ($services as $service) {
387
		if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) {
388 6d9b1074 jim-p
			return $service;
389 61e047a5 Phil Davis
		}
390
	}
391 6d9b1074 jim-p
	return array();
392
}
393
394
function find_service_by_cp_zone($zone) {
395
	$services = get_services();
396 61e047a5 Phil Davis
	foreach ($services as $service) {
397
		if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) {
398 6d9b1074 jim-p
			return $service;
399 61e047a5 Phil Davis
		}
400
	}
401 6d9b1074 jim-p
	return array();
402
}
403
404 e48cdc01 jim-p
function service_name_compare($a, $b) {
405 61e047a5 Phil Davis
	if (strtolower($a['name']) == strtolower($b['name'])) {
406 e48cdc01 jim-p
		return 0;
407 61e047a5 Phil Davis
	}
408 e48cdc01 jim-p
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
409
}
410
411 095edfeb Stephen Beaver
function service_dispname_compare($a, $b) {
412
	if (strtolower($a['dispname']) == strtolower($b['dispname'])) {
413
		return 0;
414
	}
415
	return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
416
}
417
418 e48cdc01 jim-p
function get_pkg_descr($package_name) {
419
	global $config;
420
	if (is_array($config['installedpackages']['package'])) {
421 61e047a5 Phil Davis
		foreach ($config['installedpackages']['package'] as $pkg) {
422
			if ($pkg['name'] == $package_name) {
423 e48cdc01 jim-p
				return $pkg['descr'];
424 61e047a5 Phil Davis
			}
425 e48cdc01 jim-p
		}
426
	}
427
	return gettext("Not available.");
428
}
429
430
function get_service_status($service) {
431
	global $g;
432
	switch ($service['name']) {
433
		case "openvpn":
434
			$running = is_pid_running("{$g['varrun_path']}/openvpn_{$service['mode']}{$service['vpnid']}.pid");
435
			break;
436
		case "captiveportal":
437 cd41643d Renato Botelho
			$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid");
438 61e047a5 Phil Davis
			if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
439 cd41643d Renato Botelho
				$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid");
440 61e047a5 Phil Davis
			}
441 e48cdc01 jim-p
			break;
442 18725085 Ermal
		case "vhosts-http":
443
			$running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid");
444
			break;
445 9590e0de Phil Davis
		case "dhcrelay6":
446
			$running = is_pid_running("{$g['varrun_path']}/dhcrelay6.pid");
447
			break;
448 e381cc01 Ermal
		case 'ipsec':
449
			$running = is_pid_running("{$g['varrun_path']}/charon.pid");
450
			break;
451 e48cdc01 jim-p
		default:
452
			$running = is_service_running($service['name']);
453
	}
454
	return $running;
455
}
456
457 f1f43826 Stephen Beaver
function get_service_status_icon($service, $withtext = true, $smallicon = false, $withthumbs = false, $title = "service_state") {
458 e48cdc01 jim-p
	$output = "";
459 f1f43826 Stephen Beaver
460 61e047a5 Phil Davis
	if (get_service_status($service)) {
461 92421710 jim-p
		$statustext = gettext("Running");
462 f1f43826 Stephen Beaver
		$text_class = "text-success";
463
		$fa_class = "fa fa-check-circle";
464
		$fa_class_thumbs = "fa fa-thumbs-o-up";
465
		$Thumbs_UpDown = "Thumbs up";
466 e48cdc01 jim-p
	} else {
467 f1f43826 Stephen Beaver
		if (is_service_enabled($service['name'])) {
468
			$statustext = gettext("Stopped");
469
			$text_class = "text-danger";
470
			$fa_class = "fa fa-times-circle";
471
		} else {
472
			$statustext = gettext("Disabled");
473
			$text_class = "text-warning";
474
			$fa_class = "fa fa-ban";
475 61e047a5 Phil Davis
		}
476 f1f43826 Stephen Beaver
		$fa_class_thumbs = "fa fa-thumbs-o-down";
477
		$Thumbs_UpDown = "Thumbs down";
478
	}
479
	$fa_size = ($smallicon) ? "fa-1x" : "fa-lg";
480
481
	if ($title == "state") {
482 230547af NOYB
		$title = $statustext;
483 f1f43826 Stephen Beaver
	} elseif ($title == "service_state") {
484 230547af NOYB
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["name"], $statustext);
485 f1f43826 Stephen Beaver
	} elseif ($title == "description_state") {
486 230547af NOYB
		$title = sprintf(gettext('%1$s Service is %2$s'), $service["description"], $statustext);
487 f1f43826 Stephen Beaver
	} elseif ($title == "description_service_state") {
488 230547af NOYB
		$title = sprintf(gettext('%1$s, %2$s Service is %3$s'), $service["description"], $service["name"], $statustext);
489 f1f43826 Stephen Beaver
	}
490
491 230547af NOYB
	$spacer = ($withthumbs || $withtext) ? " " : "";
492
493
	$output = "<i class=\"{$text_class} {$fa_class} {$fa_size}\" title=\"{$title}\"><span style=\"display: none\">{$statustext}</span></i>{$spacer}";
494
495 f1f43826 Stephen Beaver
	$spacer = ($withtext) ? " " : "";
496
	if ($withthumbs) {
497
		$output .= "<i class=\"{$text_class} {$fa_class_thumbs} {$fa_size}\" title=\"{$Thumbs_UpDown}\"></i>{$spacer}";
498
	}
499
500
	if ($withtext) {
501
		$output .= "<span class=\"" . $text_class . "\">" . $statustext . "</span>";
502 e48cdc01 jim-p
	}
503 f1f43826 Stephen Beaver
504 e48cdc01 jim-p
	return $output;
505
}
506
507 f4daf025 jim-p
function get_service_control_links($service, $addname = false) {
508 49840447 PiBa-NL
	global $g;
509 e48cdc01 jim-p
	$output = "";
510 f4daf025 jim-p
	$stitle = ($addname) ? $service['name'] . " " : "";
511 1180e4f0 Sjon Hortensius
512
	switch ($service['name']) {
513
		case "openvpn":
514 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="openvpn-%s-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
515 1180e4f0 Sjon Hortensius
		break;
516
		case "captiveportal":
517 74b0f23e Stephen Beaver
			$link = '<a title="%s" href="#" id="captiveportal-%s-' . $service['zone'] . '">';
518 1180e4f0 Sjon Hortensius
		break;
519
		default:
520 ab7e04c7 Colin Fleming
			$link = '<a title="%s" href="#" id="%s-' . $service['name'] . '">';
521 1180e4f0 Sjon Hortensius
	}
522
523 61e047a5 Phil Davis
	if (get_service_status($service)) {
524 e48cdc01 jim-p
		switch ($service['name']) {
525
			case "openvpn":
526 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-restartservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
527 e48cdc01 jim-p
				break;
528
			case "captiveportal":
529 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-restartservice-' . $service['zone'] . '">';
530 e48cdc01 jim-p
				break;
531
			default:
532 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="restartservice-' . $service['name'] . '" >';
533 e48cdc01 jim-p
		}
534 74b0f23e Stephen Beaver
535 ab7e04c7 Colin Fleming
		$output .= "<i class=\"fa fa-repeat\" title=\"" . sprintf(gettext("Restart %sService"), $stitle) . "\"></i></a>\n";
536 74b0f23e Stephen Beaver
537 e48cdc01 jim-p
		switch ($service['name']) {
538
			case "openvpn":
539 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="openvpn-stopservice-' . $service['mode'] . '-' . $service['vpnid'] . '" >';
540 e48cdc01 jim-p
				break;
541
			case "captiveportal":
542 ab7e04c7 Colin Fleming
				$output .= '<a href="#" id="captiveportal-stopservice-' . $service['zone'] . '">';
543 e48cdc01 jim-p
				break;
544
			default:
545 74b0f23e Stephen Beaver
				$output .= '<a href="#" id="stopservice-' . $service['name'] . '">';
546 e48cdc01 jim-p
		}
547 74b0f23e Stephen Beaver
548 335ac1e8 Stephen Beaver
		$output .= "<i class=\"fa fa-stop-circle-o\" title=\"" . sprintf(gettext("Stop %sService"), $stitle) . "\"></i></a>";
549 74b0f23e Stephen Beaver
550 e48cdc01 jim-p
	} else {
551 ccfc0269 phildd
		$service_enabled = is_service_enabled($service['name']);
552 1180e4f0 Sjon Hortensius
553 46bb8a0b Sjon Hortensius
		if ($service['name'] == 'openvpn' || $service['name'] == 'captiveportal' || $service_enabled) {
554 1180e4f0 Sjon Hortensius
			$output .= sprintf($link, sprintf(gettext("Start %sService"), $stitle), 'startservice');
555 7ea65674 Jared Dillard
			$output .= '<i class="fa fa-play-circle"></i></a> ';
556 e48cdc01 jim-p
		}
557
	}
558 1180e4f0 Sjon Hortensius
559 e48cdc01 jim-p
	return $output;
560
}
561 ee6e6011 jim-p
562
function service_control_start($name, $extras) {
563
	global $g;
564 61e047a5 Phil Davis
	switch ($name) {
565 ee6e6011 jim-p
		case 'radvd':
566
			services_radvd_configure();
567
			break;
568
		case 'captiveportal':
569 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
570 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
571
			break;
572
		case 'ntpd':
573
		case 'openntpd':
574
			system_ntp_configure();
575
			break;
576 69eefb50 Renato Botelho
		case 'dpinger':
577 b05a8f35 jim-p
			setup_gateways_monitor();
578
			break;
579 ee6e6011 jim-p
		case 'bsnmpd':
580
			services_snmpd_configure();
581
			break;
582 9590e0de Phil Davis
		case 'dhcrelay':
583
			services_dhcrelay_configure();
584
			break;
585
		case 'dhcrelay6':
586
			services_dhcrelay6_configure();
587
			break;
588 ee6e6011 jim-p
		case 'dnsmasq':
589
			services_dnsmasq_configure();
590
			break;
591 a8604dc6 Phil Davis
		case 'unbound':
592
			services_unbound_configure();
593
			break;
594 ee6e6011 jim-p
		case 'dhcpd':
595
			services_dhcpd_configure();
596
			break;
597
		case 'igmpproxy':
598
			services_igmpproxy_configure();
599
			break;
600
		case 'miniupnpd':
601
			upnp_action('start');
602
			break;
603 8b4abd59 Ermal
		case 'ipsec':
604 ee6e6011 jim-p
			vpn_ipsec_force_reload();
605
			break;
606 aaa78416 jim-p
		case 'sshd':
607
			send_event("service restart sshd");
608
			break;
609 ee6e6011 jim-p
		case 'openvpn':
610 2f9951fe Renato Botelho
			$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
611 ee6e6011 jim-p
			if (($vpnmode == "server") || ($vpnmode == "client")) {
612 2f9951fe Renato Botelho
				$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
613 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
614 61e047a5 Phil Davis
				if (file_exists($configfile)) {
615 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
616 61e047a5 Phil Davis
				}
617 ee6e6011 jim-p
			}
618
			break;
619
		case 'relayd':
620
			relayd_configure();
621 a8014f46 jim-p
			filter_configure();
622 ee6e6011 jim-p
			break;
623
		default:
624
			start_service($name);
625
			break;
626
	}
627 086cf944 Phil Davis
	return sprintf(gettext("%s has been started."), htmlspecialchars($name));
628 ee6e6011 jim-p
}
629
function service_control_stop($name, $extras) {
630
	global $g;
631 61e047a5 Phil Davis
	switch ($name) {
632 ee6e6011 jim-p
		case 'radvd':
633
			killbypid("{$g['varrun_path']}/radvd.pid");
634
			break;
635
		case 'captiveportal':
636 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
637 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
638
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
639 ee6e6011 jim-p
			break;
640
		case 'ntpd':
641
			killbyname("ntpd");
642
			break;
643
		case 'openntpd':
644
			killbyname("openntpd");
645
			break;
646 69eefb50 Renato Botelho
		case 'dpinger':
647
			stop_dpinger();
648 b05a8f35 jim-p
			break;
649 ee6e6011 jim-p
		case 'bsnmpd':
650
			killbypid("{$g['varrun_path']}/snmpd.pid");
651
			break;
652
		case 'choparp':
653
			killbyname("choparp");
654
			break;
655
		case 'dhcpd':
656
			killbyname("dhcpd");
657
			break;
658
		case 'dhcrelay':
659
			killbypid("{$g['varrun_path']}/dhcrelay.pid");
660
			break;
661 9590e0de Phil Davis
		case 'dhcrelay6':
662
			killbypid("{$g['varrun_path']}/dhcrelay6.pid");
663
			break;
664 ee6e6011 jim-p
		case 'dnsmasq':
665
			killbypid("{$g['varrun_path']}/dnsmasq.pid");
666
			break;
667 33232486 Warren Baker
		case 'unbound':
668
			killbypid("{$g['varrun_path']}/unbound.pid");
669
			break;
670 ee6e6011 jim-p
		case 'igmpproxy':
671
			killbyname("igmpproxy");
672
			break;
673
		case 'miniupnpd':
674
			upnp_action('stop');
675
			break;
676
		case 'sshd':
677
			killbyname("sshd");
678
			break;
679 8b4abd59 Ermal
		case 'ipsec':
680
			exec("/usr/local/sbin/ipsec stop");
681 ee6e6011 jim-p
			break;
682
		case 'openvpn':
683 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
684 ee6e6011 jim-p
			if (($vpnmode == "server") or ($vpnmode == "client")) {
685 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
686 ee6e6011 jim-p
				$pidfile = "{$g['varrun_path']}/openvpn_{$vpnmode}{$id}.pid";
687
				killbypid($pidfile);
688
			}
689
			break;
690
		case 'relayd':
691
			mwexec('pkill relayd');
692
			break;
693
		default:
694
			stop_service($name);
695
			break;
696
	}
697
	return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
698
}
699 33232486 Warren Baker
700 ee6e6011 jim-p
function service_control_restart($name, $extras) {
701
	global $g;
702 61e047a5 Phil Davis
	switch ($name) {
703 ee6e6011 jim-p
		case 'radvd':
704
			services_radvd_configure();
705
			break;
706
		case 'captiveportal':
707 2f9951fe Renato Botelho
			$zone = htmlspecialchars($extras['zone']);
708 cd41643d Renato Botelho
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal.pid");
709
			killbypid("{$g['varrun_path']}/nginx-{$zone}-CaptivePortal-SSL.pid");
710 ee6e6011 jim-p
			captiveportal_init_webgui_zonename($zone);
711
			break;
712
		case 'ntpd':
713
		case 'openntpd':
714
			system_ntp_configure();
715
			break;
716 69eefb50 Renato Botelho
		case 'dpinger':
717 b05a8f35 jim-p
			setup_gateways_monitor();
718
			break;
719 ee6e6011 jim-p
		case 'bsnmpd':
720
			services_snmpd_configure();
721
			break;
722 9590e0de Phil Davis
		case 'dhcrelay':
723
			services_dhcrelay_configure();
724
			break;
725
		case 'dhcrelay6':
726
			services_dhcrelay6_configure();
727
			break;
728 ee6e6011 jim-p
		case 'dnsmasq':
729
			services_dnsmasq_configure();
730
			break;
731 33232486 Warren Baker
		case 'unbound':
732
			services_unbound_configure();
733
			break;
734 ee6e6011 jim-p
		case 'dhcpd':
735
			services_dhcpd_configure();
736
			break;
737
		case 'igmpproxy':
738
			services_igmpproxy_configure();
739
			break;
740
		case 'miniupnpd':
741
			upnp_action('restart');
742
			break;
743 8b4abd59 Ermal
		case 'ipsec':
744 ee6e6011 jim-p
			vpn_ipsec_force_reload();
745
			break;
746 aaa78416 jim-p
		case 'sshd':
747
			send_event("service restart sshd");
748
			break;
749 ee6e6011 jim-p
		case 'openvpn':
750 2f9951fe Renato Botelho
			$vpnmode = htmlspecialchars($extras['vpnmode']);
751 ee6e6011 jim-p
			if ($vpnmode == "server" || $vpnmode == "client") {
752 2f9951fe Renato Botelho
				$id = htmlspecialchars($extras['id']);
753 ee6e6011 jim-p
				$configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf";
754 61e047a5 Phil Davis
				if (file_exists($configfile)) {
755 ee6e6011 jim-p
					openvpn_restart_by_vpnid($vpnmode, $id);
756 61e047a5 Phil Davis
				}
757 ee6e6011 jim-p
			}
758
			break;
759
		case 'relayd':
760
			relayd_configure(true);
761 a8014f46 jim-p
			filter_configure();
762 ee6e6011 jim-p
			break;
763
		default:
764
			restart_service($name);
765
			break;
766
	}
767 086cf944 Phil Davis
	return sprintf(gettext("%s has been restarted."), htmlspecialchars($name));
768 ee6e6011 jim-p
}
769
770 17098641 Ermal
?>