2014-06-06 17:45:24 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga Web 2.
|
|
|
|
*
|
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Protocol;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discover dns records using regular or reverse lookup
|
|
|
|
*/
|
2014-06-16 14:16:18 +02:00
|
|
|
class Dns
|
|
|
|
{
|
2014-06-06 17:45:24 +02:00
|
|
|
|
|
|
|
/**
|
2014-06-16 14:16:18 +02:00
|
|
|
* Discover all service records on a given domain
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-16 14:16:18 +02:00
|
|
|
* @param string $domain The domain to search
|
|
|
|
* @param string $service The type of the service, like for example 'ldaps' or 'ldap'
|
|
|
|
* @param string $protocol The transport protocol used by the service, defaults to 'tcp'
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-25 11:06:34 +02:00
|
|
|
* @return array|null An array of all service domains
|
2014-06-06 17:45:24 +02:00
|
|
|
*/
|
2014-06-16 14:16:18 +02:00
|
|
|
public static function getSrvRecords($domain, $service, $protocol = 'tcp')
|
2014-06-06 17:45:24 +02:00
|
|
|
{
|
2014-06-16 14:16:18 +02:00
|
|
|
$records = dns_get_record('_' . $service . '._' . $protocol . '.' . $domain, DNS_SRV);
|
|
|
|
if ($records === false) {
|
2014-06-25 11:06:34 +02:00
|
|
|
return null;
|
2014-06-16 14:16:18 +02:00
|
|
|
}
|
|
|
|
$targets = array();
|
|
|
|
foreach ($records as $record) {
|
|
|
|
if (array_key_exists('target', $record)) {
|
|
|
|
$targets[] = $record['target'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $targets;
|
2014-06-06 17:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all ldap records for the given domain
|
|
|
|
*
|
2014-06-16 14:16:18 +02:00
|
|
|
* @param string $query The domain to query
|
|
|
|
* @param int $type The type of DNS-entry to fetch, see
|
|
|
|
* http://www.php.net/manual/de/function.dns-get-record.php for available types
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-25 11:06:34 +02:00
|
|
|
* @return array|null An array of record entries
|
2014-06-06 17:45:24 +02:00
|
|
|
*/
|
|
|
|
public static function records($query, $type = DNS_ANY)
|
|
|
|
{
|
|
|
|
return dns_get_record($query, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-16 14:16:18 +02:00
|
|
|
* Reverse lookup all host names available on the given ip address
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-16 14:16:18 +02:00
|
|
|
* @param string $ipAddress
|
|
|
|
* @param int $type
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-25 11:06:34 +02:00
|
|
|
* @return array|null
|
2014-06-06 17:45:24 +02:00
|
|
|
*/
|
|
|
|
public static function ptr($ipAddress, $type = DNS_ANY)
|
|
|
|
{
|
|
|
|
$host = gethostbyaddr($ipAddress);
|
|
|
|
if ($host === false || $host === $ipAddress) {
|
|
|
|
// malformed input or no host found
|
2014-06-25 11:06:34 +02:00
|
|
|
return null;
|
2014-06-06 17:45:24 +02:00
|
|
|
}
|
|
|
|
return self::records($host, $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the IPv4 address of the given hostname.
|
|
|
|
*
|
2014-06-16 14:16:18 +02:00
|
|
|
* @param $hostname The hostname to resolve
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-25 11:06:34 +02:00
|
|
|
* @return string|null The IPv4 address of the given hostname or null, when no entry exists.
|
2014-06-06 17:45:24 +02:00
|
|
|
*/
|
|
|
|
public static function ipv4($hostname)
|
|
|
|
{
|
|
|
|
$records = dns_get_record($hostname, DNS_A);
|
2014-06-23 14:17:56 +02:00
|
|
|
if ($records !== false && count($records) > 0) {
|
2014-06-06 17:45:24 +02:00
|
|
|
return $records[0]['ip'];
|
|
|
|
}
|
2014-06-25 11:06:34 +02:00
|
|
|
return null;
|
2014-06-06 17:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the IPv6 address of the given hostname.
|
|
|
|
*
|
2014-06-16 14:16:18 +02:00
|
|
|
* @param $hostname The hostname to resolve
|
2014-06-06 17:45:24 +02:00
|
|
|
*
|
2014-06-25 11:06:34 +02:00
|
|
|
* @return string|null The IPv6 address of the given hostname or null, when no entry exists.
|
2014-06-06 17:45:24 +02:00
|
|
|
*/
|
|
|
|
public static function ipv6($hostname)
|
|
|
|
{
|
|
|
|
$records = dns_get_record($hostname, DNS_AAAA);
|
2014-06-23 14:17:56 +02:00
|
|
|
if ($records !== false && count($records) > 0) {
|
2014-06-06 17:45:24 +02:00
|
|
|
return $records[0]['ip'];
|
|
|
|
}
|
2014-06-25 11:06:34 +02:00
|
|
|
return null;
|
2014-06-06 17:45:24 +02:00
|
|
|
}
|
2014-06-16 14:16:18 +02:00
|
|
|
}
|