service-utils.inc 16.1 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2 3

/*
Ad Schellevis's avatar
Ad Schellevis committed
4 5
 * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
 * All rights reserved.
6
 *
Ad Schellevis's avatar
Ad Schellevis committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

require_once("globals.inc");
require_once("captiveportal.inc");
require_once("openvpn.inc");
require_once("ipsec.inc");
require_once("vpn.inc");
require_once("vslb.inc");
require_once("gwlb.inc");

37
if (!function_exists('get_services')) {
38

39 40
	function get_services() {
		global $config;
41
		$services = array();
42

43 44 45 46 47 48 49 50 51
		/*    Add services that are in the base.
		 *
		 */
		if (is_radvd_enabled()) {
			$pconfig = array();
			$pconfig['name'] = "radvd";
			$pconfig['description'] = gettext("Router Advertisement Daemon");
			$services[] = $pconfig;
		}
52

53 54 55 56 57 58
		if (isset($config['dnsmasq']['enable'])) {
			$pconfig = array();
			$pconfig['name'] = "dnsmasq";
			$pconfig['description'] = gettext("DNS Forwarder");
			$services[] = $pconfig;
		}
59

60 61 62 63 64 65
		if (isset($config['unbound']['enable'])) {
			$pconfig = array();
			$pconfig['name'] = "unbound";
			$pconfig['description'] = gettext("Unbound DNS Forwarder");
			$services[] = $pconfig;
		}
66

Ad Schellevis's avatar
Ad Schellevis committed
67
		$pconfig = array();
68 69
		$pconfig['name'] = "ntpd";
		$pconfig['description'] = gettext("NTP clock sync");
Ad Schellevis's avatar
Ad Schellevis committed
70
		$services[] = $pconfig;
71

72 73 74 75 76 77 78 79 80 81 82
		if (is_array($config['captiveportal'])) {
			foreach ($config['captiveportal'] as $zone => $setting) {
				if (isset($setting['enable'])) {
					$pconfig = array();
					$pconfig['name'] = "captiveportal";
					$pconfig['zone'] = $zone;
					$pconfig['description'] = gettext("Captive Portal") . ": ".htmlspecialchars($setting['zone']);
					$services[] = $pconfig;
				}
			}
		}
83

84 85 86 87 88 89 90
		$iflist = array();
		$ifdescrs = get_configured_interface_list();
		foreach ($ifdescrs as $if) {
			$oc = $config['interfaces'][$if];
			if ($oc['if'] && (!link_interface_to_bridge($if)))
				$iflist[$if] = $if;
		}
91

92 93 94 95 96 97
		if (isset($config['dhcrelay']['enable'])) {
			$pconfig = array();
			$pconfig['name'] = "dhcrelay";
			$pconfig['description'] = gettext("DHCP Relay");
			$services[] = $pconfig;
		}
98

99 100 101 102 103 104
		if (isset($config['dhcrelay6']['enable'])) {
			$pconfig = array();
			$pconfig['name'] = "dhcrelay6";
			$pconfig['description'] = gettext("DHCPv6 Relay");
			$services[] = $pconfig;
		}
105

106 107 108 109 110 111
		if (is_dhcp_server_enabled()) {
			$pconfig = array();
			$pconfig['name'] = "dhcpd";
			$pconfig['description'] = gettext("DHCP Service");
			$services[] = $pconfig;
		}
112

113 114 115 116 117 118 119
		$gateways_arr = return_gateways_array();
		if (is_array($gateways_arr)) {
			$pconfig = array();
			$pconfig['name'] = "apinger";
			$pconfig['description'] = gettext("Gateway Monitoring Daemon");
			$services[] = $pconfig;
		}
120

121 122 123 124 125 126
		if (isset($config['snmpd']['enable'])) {
			$pconfig = array();
			$pconfig['name'] = "bsnmpd";
			$pconfig['description'] = gettext("SNMP Service");
			$services[] = $pconfig;
		}
127

128 129 130 131 132 133
		if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
			$pconfig = array();
			$pconfig['name'] = "igmpproxy";
			$pconfig['description'] = gettext("IGMP proxy");
			$services[] = $pconfig;
		}
134

135 136 137 138 139 140
		if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
			$pconfig = array();
			$pconfig['name'] = "miniupnpd";
			$pconfig['description'] = gettext("UPnP Service");
			$services[] = $pconfig;
		}
141

142 143 144 145 146 147
		if (isset($config['ipsec']['enable'])) {
			$pconfig = array();
			$pconfig['name'] = "ipsec";
			$pconfig['description'] = gettext("IPsec VPN");
			$services[] = $pconfig;
		}
148

149
		if (isset($config['system']['ssh']['enabled'])) {
150
			$pconfig = array();
151 152
			$pconfig['name'] = 'sshd';
			$pconfig['description'] = gettext('Secure Shell Daemon');
153 154
			$services[] = $pconfig;
		}
155

156
		foreach (array('server', 'client') as $mode) {
157
			if (isset($config['openvpn']["openvpn-{$mode}"])) {
158 159 160 161 162 163 164 165 166 167 168
				foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) {
					if (!isset($setting['disable'])) {
						$pconfig = array();
						$pconfig['name'] = "openvpn";
						$pconfig['mode'] = $mode;
						$pconfig['id'] = $id;
						$pconfig['vpnid'] = $setting['vpnid'];
						$pconfig['description'] = gettext("OpenVPN") . " ".$mode.": ".htmlspecialchars($setting['description']);
						$services[] = $pconfig;
					}
				}
Ad Schellevis's avatar
Ad Schellevis committed
169 170
			}
		}
171

172 173 174 175 176 177
		if (count($config['load_balancer']['virtual_server']) && count($config['load_balancer']['lbpool'])) {
			$pconfig = array();
			$pconfig['name'] = "relayd";
			$pconfig['description'] = gettext("Server load balancing daemon");
			$services[] = $pconfig;
		}
178 179 180 181

		if (isset($config['OPNsense']['proxy']['general']['enabled']) && $config['OPNsense']['proxy']['general']['enabled'] == 1) {
			$services[] = array('name'=>'squid', 'description' => gettext("Proxy server"));
		}
182
		return $services;
Ad Schellevis's avatar
Ad Schellevis committed
183 184
	}

185
}
Ad Schellevis's avatar
Ad Schellevis committed
186

187
if (!function_exists('find_service_by_name')) {
188

189 190 191 192 193 194
	function find_service_by_name($name) {
		$services = get_services();
		foreach ($services as $service)
			if ($service["name"] == $name)
				return $service;
		return array();
Ad Schellevis's avatar
Ad Schellevis committed
195 196
	}

197
}
Ad Schellevis's avatar
Ad Schellevis committed
198

199
if (!function_exists('find_service_by_openvpn_vpnid')) {
200

201 202 203 204 205 206
	function find_service_by_openvpn_vpnid($vpnid) {
		$services = get_services();
		foreach ($services as $service)
			if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid))
				return $service;
		return array();
Ad Schellevis's avatar
Ad Schellevis committed
207 208 209
	}


210
}
Ad Schellevis's avatar
Ad Schellevis committed
211

212
if (!function_exists('find_service_by_cp_zone')) {
213 214


215 216 217 218 219 220
	function find_service_by_cp_zone($zone) {
		$services = get_services();
		foreach ($services as $service)
			if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone))
				return $service;
		return array();
Ad Schellevis's avatar
Ad Schellevis committed
221 222 223
	}


224
}
Ad Schellevis's avatar
Ad Schellevis committed
225

226
if (!function_exists('service_name_compare')) {
227 228


229 230 231 232
	function service_name_compare($a, $b) {
		if (strtolower($a['name']) == strtolower($b['name']))
			return 0;
		return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1;
Ad Schellevis's avatar
Ad Schellevis committed
233 234 235
	}


236 237
}

Ad Schellevis's avatar
Ad Schellevis committed
238

239
if (!function_exists('get_service_status')) {
240 241


242 243 244 245
	function get_service_status($service) {
		global $g;
		switch ($service['name']) {
			case "openvpn":
246
				$running = isvalidpid("/var/run/openvpn_{$service['mode']}{$service['vpnid']}.pid");
247 248
				break;
			case "captiveportal":
249
				$running = isvalidpid("/var/run/lighty-{$service['zone']}-CaptivePortal.pid");
250
				if (isset($config['captiveportal'][$service['zone']]['httpslogin']))
251
					$running = $running && isvalidpid("/var/run/lighty-{$service['zone']}-CaptivePortal-SSL.pid");
252 253
				break;
			case "vhosts-http":
254
				$running = isvalidpid("/var/run/vhosts-http.pid");
255 256
				break;
			case "dhcrelay6":
257
				$running = isvalidpid("/var/run/dhcrelay6.pid");
258 259
				break;
			case 'ipsec':
260
				$running = isvalidpid("/var/run/charon.pid");
261 262
				break;
			default:
263
				$running = is_process_running($service['name']);
264 265 266
		}
		return $running;
	}
Ad Schellevis's avatar
Ad Schellevis committed
267 268 269 270


}

271
if (!function_exists('get_service_status_icon')) {
272 273


274 275 276 277 278
	function get_service_status_icon($service, $withtext = true, $smallicon = false) {
		global $g;
		$output = "";
		if(get_service_status($service)) {
			$statustext = gettext("Running");
279
			$output .= '<span class="btn btn-success"><span class="glyphicon glyphicon-play" title="'.sprintf(gettext('%s Service is'),$service['name']).' '.$statustext.'" data-toggle="tooltip" data-placement="bottom" ></span></span>&nbsp;';
280 281
		} else {
			$statustext = gettext("Stopped") ;
282
			$output .= '<span class="btn btn-danger"><span class="glyphicon glyphicon-stop" title="'.sprintf(gettext('%s Service is'),$service['name']).' '.$statustext.'" data-toggle="tooltip" data-placement="bottom" ></span></span>&nbsp;';
283

Ad Schellevis's avatar
Ad Schellevis committed
284
		}
285
		return $output;
Ad Schellevis's avatar
Ad Schellevis committed
286 287
	}

288

Ad Schellevis's avatar
Ad Schellevis committed
289 290
}

291
if (!function_exists('get_service_control_links')) {
292 293


294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	function get_service_control_links($service, $addname = false) {
		global $g;
		$output = "";
		$stitle = ($addname) ? $service['name'] . " " : "";
		if(get_service_status($service)) {
			switch ($service['name']) {
				case "openvpn":
					$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}' class=\"btn btn-default\">";
					break;
				case "captiveportal":
					$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}&amp;zone={$service['zone']}' class=\"btn btn-default\">";
					break;
				default:
					$output .= "<a href='status_services.php?mode=restartservice&amp;service={$service['name']}' class=\"btn btn-default\">";
			}
309
			$output .= "<span data-toggle=\"tooltip\" data-placement=\"bottom\" title='" . sprintf(gettext("Restart %sService"),$stitle) . "' class=\"glyphicon glyphicon-refresh\"/></a>\n";
310 311 312 313 314 315 316 317 318
			switch ($service['name']) {
				case "openvpn":
					$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}' class=\"btn btn-default\">";
					break;
				case "captiveportal":
					$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}&amp;zone={$service['zone']}' class=\"btn btn-default\">";
					break;
				default:
					$output .= "<a href='status_services.php?mode=stopservice&amp;service={$service['name']}' class=\"btn btn-default\">";
319
					break;
320
			}
321
			$output .= "<span data-toggle=\"tooltip\" data-placement=\"bottom\" title='" . sprintf(gettext("Stop %sService"),$stitle) . "' class=\"glyphicon glyphicon-stop\" />";
322 323 324 325 326 327 328 329 330 331
			$output .= "</a>";
		} else {
			switch ($service['name']) {
				case "openvpn":
					$output .= "<a href='status_services.php?mode=startservice&amp;service={$service['name']}&amp;vpnmode={$service['mode']}&amp;id={$service['vpnid']}' class=\"btn btn-default\">";
					break;
				case "captiveportal":
					$output .= "<a href='status_services.php?mode=startservice&amp;service={$service['name']}&amp;zone={$service['zone']}' class=\"btn btn-default\">";
					break;
				default:
332 333
					$output .= "<a href='status_services.php?mode=startservice&amp;service={$service['name']}' class=\"btn btn-default\">";
					break;
334
			}
335
			$output .= "<span data-toggle=\"tooltip\" data-placement=\"bottom\" title='" . sprintf(gettext("Start %sService"),$stitle) . "' alt='start' class=\"glyphicon glyphicon-play\"/></a>\n";
336 337
		}
		return $output;
Ad Schellevis's avatar
Ad Schellevis committed
338
	}
339 340


Ad Schellevis's avatar
Ad Schellevis committed
341 342
}

343
if (!function_exists('service_control_start')) {
344 345


346 347 348 349 350
	function service_control_start($name, $extras) {
		global $g;
		switch($name) {
			case 'radvd':
				services_radvd_configure();
Ad Schellevis's avatar
Ad Schellevis committed
351
				break;
352
			case 'captiveportal':
353
				captiveportal_configure();
Ad Schellevis's avatar
Ad Schellevis committed
354
				break;
355 356
			case 'ntpd':
				system_ntp_configure();
Ad Schellevis's avatar
Ad Schellevis committed
357
				break;
358 359
			case 'apinger':
				setup_gateways_monitor();
Ad Schellevis's avatar
Ad Schellevis committed
360
				break;
361 362
			case 'bsnmpd':
				services_snmpd_configure();
Ad Schellevis's avatar
Ad Schellevis committed
363
				break;
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
			case 'dhcrelay':
				services_dhcrelay_configure();
				break;
			case 'dhcrelay6':
				services_dhcrelay6_configure();
				break;
			case 'dnsmasq':
				services_dnsmasq_configure();
				break;
			case 'dhcpd':
				services_dhcpd_configure();
				break;
			case 'igmpproxy':
				services_igmpproxy_configure();
				break;
			case 'miniupnpd':
				upnp_action('start');
				break;
			case 'ipsec':
				vpn_ipsec_force_reload();
				break;
			case 'sshd':
386
				configd_run("sshd restart");
387 388 389 390 391
				break;
			case 'openvpn':
				$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
				if (($vpnmode == "server") || ($vpnmode == "client")) {
					$id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']);
392
					$configfile = "/var/etc/openvpn/{$vpnmode}{$id}.conf";
393 394 395 396 397 398
					if (file_exists($configfile))
						openvpn_restart_by_vpnid($vpnmode, $id);
				}
				break;
			case 'relayd':
				relayd_configure();
Ad Schellevis's avatar
Ad Schellevis committed
399
				break;
400 401
			case 'squid':
				configd_run("proxy start");
Ad Schellevis's avatar
Ad Schellevis committed
402
				break;
Ad Schellevis's avatar
Ad Schellevis committed
403
			default:
404
				log_error(gettext("Could not start unknown service `%s'", $name));
405
				break;
Ad Schellevis's avatar
Ad Schellevis committed
406
		}
407
		return sprintf(gettext("%s has been started."),htmlspecialchars($name));
Ad Schellevis's avatar
Ad Schellevis committed
408 409 410
	}

}
411 412

if (!function_exists('service_control_stop')) {
413

414 415 416 417 418

	function service_control_stop($name, $extras) {
		global $g;
		switch($name) {
			case 'radvd':
419
				killbypid("/var/run/radvd.pid");
420 421 422
				break;
			case 'captiveportal':
				$zone = htmlspecialchars($extras['zone']);
423 424
				killbypid("/var/run/lighty-{$zone}-CaptivePortal.pid");
				killbypid("/var/run/lighty-{$zone}-CaptivePortal-SSL.pid");
425 426 427 428 429
				break;
			case 'ntpd':
				killbyname("ntpd");
				break;
			case 'apinger':
430
				killbypid("/var/run/apinger.pid");
431 432
				break;
			case 'bsnmpd':
433
				killbypid("/var/run/snmpd.pid");
434 435 436 437 438 439 440 441
				break;
			case 'choparp':
				killbyname("choparp");
				break;
			case 'dhcpd':
				killbyname("dhcpd");
				break;
			case 'dhcrelay':
442
				killbypid("/var/run/dhcrelay.pid");
443 444
				break;
			case 'dhcrelay6':
445
				killbypid("/var/run/dhcrelay6.pid");
446 447
				break;
			case 'dnsmasq':
448
				killbypid("/var/run/dnsmasq.pid");
449 450
				break;
			case 'unbound':
451
				killbypid("/var/run/unbound.pid");
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
				break;
			case 'igmpproxy':
				killbyname("igmpproxy");
				break;
			case 'miniupnpd':
				upnp_action('stop');
				break;
			case 'sshd':
				killbyname("sshd");
				break;
			case 'ipsec':
				exec("/usr/local/sbin/ipsec stop");
				break;
			case 'openvpn':
				$vpnmode = htmlspecialchars($extras['vpnmode']);
				if (($vpnmode == "server") or ($vpnmode == "client")) {
					$id = htmlspecialchars($extras['id']);
469
					$pidfile = "/var/run/openvpn_{$vpnmode}{$id}.pid";
470 471 472 473 474 475
					killbypid($pidfile);
				}
				break;
			case 'relayd':
				mwexec('pkill relayd');
				break;
476 477
			case 'squid':
				configd_run("proxy stop");
Ad Schellevis's avatar
Ad Schellevis committed
478
				break;
479
			default:
480
				log_error(gettext("Could not stop unknown service `%s'", $name));
481 482 483
				break;
		}
		return sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
Ad Schellevis's avatar
Ad Schellevis committed
484
	}
485 486


Ad Schellevis's avatar
Ad Schellevis committed
487 488
}

489
if (!function_exists('service_control_restart')) {
490 491


492 493 494 495 496 497 498
	function service_control_restart($name, $extras) {
		global $g;
		switch($name) {
			case 'radvd':
				services_radvd_configure();
				break;
			case 'captiveportal':
499
				captiveportal_configure();
500 501 502 503 504
				break;
			case 'ntpd':
				system_ntp_configure();
				break;
			case 'apinger':
505
				killbypid("/var/run/apinger.pid");
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
				setup_gateways_monitor();
				break;
			case 'bsnmpd':
				services_snmpd_configure();
				break;
			case 'dhcrelay':
				services_dhcrelay_configure();
				break;
			case 'dhcrelay6':
				services_dhcrelay6_configure();
				break;
			case 'dnsmasq':
				services_dnsmasq_configure();
				break;
			case 'unbound':
				services_unbound_configure();
				break;
			case 'dhcpd':
				services_dhcpd_configure();
				break;
			case 'igmpproxy':
				services_igmpproxy_configure();
				break;
			case 'miniupnpd':
				upnp_action('restart');
				break;
			case 'ipsec':
				vpn_ipsec_force_reload();
				break;
			case 'sshd':
536
				configd_run("sshd restart");
537 538 539 540 541
				break;
			case 'openvpn':
				$vpnmode = htmlspecialchars($extras['vpnmode']);
				if ($vpnmode == "server" || $vpnmode == "client") {
					$id = htmlspecialchars($extras['id']);
542
					$configfile = "/var/etc/openvpn/{$vpnmode}{$id}.conf";
543 544 545 546 547 548 549
					if (file_exists($configfile))
						openvpn_restart_by_vpnid($vpnmode, $id);
				}
				break;
			case 'relayd':
				relayd_configure(true);
				break;
550 551
			case 'squid':
				configd_run("proxy restart");
Ad Schellevis's avatar
Ad Schellevis committed
552
				break;
553
			default:
554
				log_error(gettext("Could not restart unknown service `%s'", $name));
555 556 557
				break;
		}
		return sprintf(gettext("%s has been restarted."),htmlspecialchars($name));
Ad Schellevis's avatar
Ad Schellevis committed
558 559
	}
}