initial commit of file from CVS for e-smith-tinydns on Wed 12 Jul 09:11:00 BST 2023

This commit is contained in:
Brian Read
2023-07-12 09:11:00 +01:00
parent 4d5bb6a2a3
commit 1cdaede0de
36 changed files with 1599 additions and 2 deletions

View File

@@ -0,0 +1,4 @@
{
my $datalimit = $tinydns{'DataLimit'} || "300000";
"$datalimit";
}

View File

@@ -0,0 +1,3 @@
{
$OUT = $tinydns{'ListenIP'} || "127.0.0.1";
}

View File

@@ -0,0 +1,115 @@
{
use esmith::HostsDB;
$hosts = esmith::HostsDB->open_ro;
use esmith::DomainsDB;
my $ddb = esmith::DomainsDB->open_ro;
use esmith::util;
#--------------------------------------------------------
# Returns a hash of hostnames with IP addresses as values
#--------------------------------------------------------
sub get_generic_hostentries
{
#--------------------------------------------------
# Compute local IP address, netmask and network values.
#--------------------------------------------------
my $ipaddrBits = esmith::util::IPquadToAddr ($LocalIP);
my $netmaskBits = esmith::util::IPquadToAddr ($LocalNetmask);
my $networkBits = $ipaddrBits & $netmaskBits;
#--------------------------------------------------
# Compute our hostid, and the highest hostid, limiting range
# to a class B at most (so we don't get a huge output file).
#--------------------------------------------------
my $myHostid = (~ $netmaskBits) & $ipaddrBits;
my $maxHostid = ((~ $netmaskBits) & 0xffffff) - 1;
$maxHostid = ($maxHostid <= 65534) ? $maxHostid : 65534;
my %name2ip;
#--------------------------------------------------
# Generate A records for the entire local network
# We can then override particular entries if we need to
# However, multiple A records are not an issue
# as long as there is a PTR record pointing to the correct
# hostname
#--------------------------------------------------
for ($i = 1; $i <= $maxHostid; $i++)
{
my $ip = esmith::util::IPaddrToQuad ($networkBits | $i);
my $hostname = sprintf ("pc-%.5d", $i);
$name2ip{$hostname} = $ip;
}
return %name2ip;
}
#--------------------------------------------------------
# Calculates an array of domains that require DNS
#--------------------------------------------------------
@domains = map { $_->key } $ddb->get_all_by_prop('type' => 'domain');
#--------------------------------------------------------
# Returns an array of domains that require DNS
#--------------------------------------------------------
sub get_domains { return @domains; }
sub get_local_domainname { return $DomainName; }
#--------------------------------------------------------
# Returns the IP Address of the host in question.
#--------------------------------------------------------
sub host2ip
{
my $host = shift;
my $ip = undef;
die "Host record must have HostType prop!"
unless my $hosttype = $host->prop('HostType');
if ($hosttype eq 'Self')
{
$ip = $LocalIP;
}
$ip ||= $host->prop('ExternalIP') || $host->prop('InternalIP');
return $ip;
}
#--------------------------------------------------------
# Returns a hash of IPs to hostnames, representing the
# chosen hostnames for reverse dns lookups for each IP.
#--------------------------------------------------------
sub get_reverse_lookup_choices
{
my %reverse_lookups = ();
foreach my $host ($hosts->hosts())
{
# A remote host must be a DNS alias.
next if $host->prop('HostType') eq 'Remote';
my $alias = $host->prop('ReverseDNS') || "no";
if ($alias eq "yes")
{
# This host is not a DNS alias, so we should make note of it
# for reverse DNS lookup purposes.
my $ip = host2ip($host);
$reverse_lookups{$ip} = $host->{key};
# Note: Here we clobber any existing key/value pair, so if
# there is more than one host with the same ip flagged as
# being the reversedns host, the last one entered in this hash
# will win. Don't do that. ;-)
}
}
return %reverse_lookups;
}
$OUT = '';
}

View File

@@ -0,0 +1,18 @@
{
$OUT .= "# NS Records\n";
foreach my $domain (get_domains())
{
$OUT .= ".$domain:\:$SystemName." . get_local_domainname(). "\n";
}
use esmith::util;
# Add name server record for local reverse zone
my $reverse =
esmith::util::computeLocalNetworkReversed ($LocalIP, $LocalNetmask);
$reverse =~ s/\.$//;
$OUT .= ".$reverse\:\:127.0.0.1\n";
$reverse =
esmith::util::computeLocalNetworkReversed ('127.0.0.1', '255.255.255.0');
$reverse =~ s/\.$//;
$OUT .= ".$reverse\:\:127.0.0.1\n";
}

View File

@@ -0,0 +1,8 @@
{
$OUT .= "# MX Records\n";
foreach my $domain (get_domains())
{
$OUT .= "\@$domain:\:$SystemName." . get_local_domainname(). "\n";
}
$OUT .= "\n";
}

View File

@@ -0,0 +1,8 @@
{
$OUT .= "# A Records for domains\n";
foreach my $domain (get_domains())
{
$OUT .= "+$domain:$LocalIP\n";
}
$OUT .= "\n";
}

View File

@@ -0,0 +1,46 @@
{
%allocated_ips = ();
foreach my $domain (get_domains())
{
$OUT .= "# A Records for Hosts in $domain\n";
foreach my $h ($hosts->get_hosts_by_domain($domain))
{
my $anIP = host2ip($h);
my $prefixchar = '+';
if ($anIP !~ /^\d+\.\d+\.\d+\.\d+$/)
{
$prefixchar = 'C';
}
else
{
my %reverse_lookups = get_reverse_lookup_choices();
# If this IP is spoken for, then we know which host to use for the
# reverse DNS lookup PTR.
if (exists $reverse_lookups{$anIP})
{
my $reverse_host = $reverse_lookups{$anIP};
if ($reverse_host eq $h->key)
{
$prefixchar = '=';
}
}
else
{
# Otherwise, we'll just use the first host that comes along.
# Have we picked one already?
unless (exists $allocated_ips{$anIP})
{
$prefixchar = '=';
}
}
# Note that this ip is taken.
$allocated_ips{$anIP} = 1;
}
$OUT .= $prefixchar . $h->key . ":$anIP\n";
}
$OUT .= "\n";
}
}

View File

@@ -0,0 +1,13 @@
{
my %name2ip = get_generic_hostentries();
my $domain = $DomainName;
$OUT .= "# Generic A Records for $domain\n";
foreach (sort keys %name2ip)
{
$prefixchar = '=';
$prefixchar = '+' if exists $allocated_ips{$name2ip{$_}};
$OUT .= $prefixchar . "$_.$domain" . ":" . $name2ip{$_} . "\n";
}
}