Skip to content
Snippets Groups Projects
Commit 621e8239 authored by LaoDC's avatar LaoDC
Browse files

Added basic Master zone class

- Eventually manage master zone servers
Changed logic for when empty or @ is entered for a record to default to fqdn
Added exporting of zone list for slave and master format through exportZones()
parent bb7de197
1 merge request!2Added basic Master zone class
......@@ -5,6 +5,7 @@
use Laodc\Database\Database;
use Laodc\Errors\Errors;
use Laodc\Functions\Functions;
class Domain extends Base
{
private Database $db;
......@@ -403,7 +404,7 @@ DNS;
$output .= self::exportRecord( $record, $max );
}
$file = sprintf( '%s/%s.zone', $outputFolder, $zone );
$file = sprintf( '%s/dns.%s', $outputFolder, $zone );
file_put_contents( $file, $output );
return true;
......
......@@ -7,13 +7,16 @@
class Domains
{
// Main table name
const TABLE_NAME = 'domains';
// Auto settings
const AUTO_UPDATE_SERIALS = TRUE;
const AUTO_UPDATE_PTRS = FALSE;
const AUTO_DEFAULTS = TRUE;
const MIN_TTL = 300;
// TTL Settings
const MIN_TTL = 60;
const MIN_REFRESH = 300;
const MIN_RETRY = 300;
const MIN_EXPIRE = 86400;
......@@ -24,6 +27,13 @@
const DEFAULT_EXPIRE = 86400;
const DEFAULT_MIN_TTL = 1800;
// Master zones
const MASTER_TABLE_NAME = 'masters';
const EXPORT_MASTER = 'master';
const EXPORT_SLAVE = 'slave';
const EXPORT_ALL = -1;
// URI Redirct donaub
const URL_REDIRECT_CNAME = 'redirect.laodc.com.';
private int $group_size = 0;
......@@ -172,6 +182,31 @@
return $domains;
}
public function getMasters( $all = false ) : array
{
$sql = sprintf( '
SELECT
id, fqdn, ip, active
FROM
%s
%s
',
$this->db->escape( self::MASTER_TABLE_NAME ),
( false === $all ) ? 'WHERE active = true' : ''
);
$result = $this->db->fetchAll( $sql );
$masters = [];
foreach( $result as $master )
{
$masters[] = new Master( $this->db, $master );
}
return $masters;
}
public function new() : Domain
{
return new Domain( $this->db, [] );
......@@ -206,4 +241,108 @@
return true;
}
public function exportZones( string $outputFolder, $type = self::EXPORT_ALL ) : bool
{
// if ALL is passed, simply run both master/slave
if( self::EXPORT_ALL === $type )
{
if( false === self::exportZones( $outputFolder, self::EXPORT_MASTER ) )
return false;
if( false === self::exportZones( $outputFolder, self::EXPORT_SLAVE ) )
return false;
return true;
}
// check permissions
if( !is_writable( $outputFolder ) )
{
Errors::set( 'Unable to access output folder' );
return false;
}
// get list of all domains
if( [] === ( $domains = self::getAll() ) )
{
Errors::set( 'No domains to export' );
return false;
}
$zones = [];
if( self::EXPORT_SLAVE === $type )
{
// grab list of master nameservers
$masters = [];
foreach( self::getMasters() as $master )
$masters[] = $master->get( 'ip' );
// bail if no masters are available
if( empty( $masters ) )
return false;
$masters = implode( ";\n ", $masters );
// declare master nodes
$zones[] = <<< MASTER
masters master_nodes {
\t{$masters};
};
MASTER;
}
foreach( $domains as $domain )
{
// skip inactive domains
if( false === $domain->get( 'active' ) )
continue;
// export domain
if( false === $domain->export( $outputFolder ) )
return false;
$zone = substr( $domain->get( 'fqdn' ), 0, -1 );
switch( $type )
{
case self::EXPORT_MASTER:
$slaves = implode( ";\n\t\t", explode( ',', $domain->get( 'xfer' ) ) );
$zones[] = <<< DNS
zone "{$zone}" in {
\ttype master;
\tfile "{$zone}.zone";
\tnotify yes;
\talso-notify {
\t\t{$slaves};
\t};
};
DNS;
break;
case self::EXPORT_SLAVE:
$zones[] = <<< DNS
zone "{$zone}" in {
\ttype slave;
\tfile "{$zone}.zone";
\tnotify no;
\tmasters { master_nodes; };
};
DNS;
break;
}
}
// generate slave and master zones
$file = sprintf( '%s/zones.%s.list', $outputFolder, $type );
file_put_contents( $file, implode( "\n", $zones ) );
return true;
}
}
<?php
namespace Laodc\Dns;
use Laodc\Database\Database;
use Laodc\Errors\Errors;
use Laodc\Functions\Functions;
class Master extends Base
{
private Database $db;
function __construct( Database $database, array $master )
{
$this->db = $database;
$this->data_fields = $master;
}
}
......@@ -98,6 +98,10 @@
return false;
}
// Set name to FQDN is blank or @ was submitted
if( empty( $this->get( 'name' ) ) || '@' === $this->get( 'name' ) )
$this->set( 'name', $this->domain->get( 'fqdn' ) );
// make sure name is valid
if( null === ( $name = Validate::Name( $this->get( 'name' ), $this->domain->get( 'fqdn' ), true ) ) )
return false;
......@@ -117,6 +121,13 @@
return false;
}
// make sure pref is a number in range
if( $this->get( 'pref' ) && false === Validate::inRange( $this->get( 'pref' ) ) )
{
Errors::set( 'pref must be a number between 0 and 65535' );
return false;
}
// Validate against record type
switch( $this->get( 'type' ) )
{
......@@ -128,12 +139,17 @@
if( false === Validate::IPv6( $this->get( 'data' ) ) )
return false;
break;
case static::TYPE_CAA:
if( null === ( $data = Validate::CAA( $this->get( 'data' ) ) ) )
return false;
$this->set( 'data', $data );
break;
case static::TYPE_ALIAS:
case static::TYPE_CH:
case static::TYPE_CNAME:
case static::TYPE_MX:
case static::TYPE_NS:
case static::TYPE_CNAME:
if( null === ( $data = Validate::Name( $this->get( 'data' ), $this->domain->get( 'fqdn' ) ) ) )
return false;
......@@ -155,12 +171,6 @@
if( null === ( $data = Validate::NAPTR( $this->get( 'data' ) ) ) )
return false;
$this->set( 'data', $data );
break;
case static::TYPE_CAA:
if( null === ( $data = Validate::CAA( $this->get( 'data' ) ) ) )
return false;
$this->set( 'data', $data );
break;
case static::TYPE_RP:
......
......@@ -9,7 +9,7 @@
const TABLE_NAME = 'records';
const MAX_TXT_LENGTH = 2048;
const MAX_TXT_ELEM_LENGTH = 255;
const MIN_TTL = 300;
const MIN_TTL = 60;
const DEFAULT_TTL = 1800;
private int $group_size = 0;
......
......@@ -219,8 +219,10 @@
// if an empty name is provided and an origin is provided
// set the name to be the origin
if( empty( $name ) && 0 === strlen( $name ) && !empty( $origin ) )
$name = $origin;
//if( ( empty( $name ) || 0 === strlen( $name ) ) && !empty( $origin ) )
// $name = $origin;
if( empty( $name ) )
return $name;
// Name too long
if( strlen( $name ) > 255 )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment