Project

General

Profile

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