Skip to content

Instantly share code, notes, and snippets.

@nuxwin
Created October 31, 2016 19:49
Show Gist options
  • Save nuxwin/93b293715fe8cfc02c0320cca2ab539d to your computer and use it in GitHub Desktop.
Save nuxwin/93b293715fe8cfc02c0320cca2ab539d to your computer and use it in GitHub Desktop.
10_named_global_ns.pl compatible with i-MSCP 1.3.7
# i-MSCP Listener::Named::Global::NS listener file
# Copyright (C) 2016 Laurent Declercq <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Listener file that allows to set identical NS entries in all zones
# Requires i-MSCP 1.3.8 or newer.
#
# Warning: Don't forget to declare your slave DNS servers to i-MSCP.
# Don't forget also to activate IPv6 support if needed. All this can
# be done by reconfiguring the named service as follow:
#
# perl /var/www/imscp/engine/setup/imscp-reconfigure -dr named
#
package Listener::Named::Global::NS;
use strict;
use warnings;
use iMSCP::EventManager;
use iMSCP::TemplateParser;
use iMSCP::Net;
#
## Configuration variables
#
# Zone defining nameservers
# Warning: For IDN, you must use the Punycode notation.
my $ZONE_NAME = 'zone.tld';
# Names servers
# Replace values with your own data and delete those which are not needed for
# your use case. The first two entries correspond to this server.
# Note that the order of declaration is signifiant.
my @NAMESERVERS = (
'<ipv4>', # MASTER DNS IP (IPv4 ; this server)
'<ipv6>', # MASTER DNS IP (IPv6 ; this server)
'<ipv4>', # SLAVE DNS 1 IP (IPv4)
'<ipv6>', # SLAVE DNS 1 IP (IPv6)
'<ipv4>', # SLAVE DNS 2 IP (IPv4)
'<ipv6>' # SLAVE DNS 2 IP (IPv6)
);
#
## Please, don't edit anything below this line
#
iMSCP::EventManager->getInstance()->register(
'beforeNamedAddDmnDb',
sub {
my ($tpl, $data) = @_;
# Override default SOA record (for all zones)
${$tpl} =~ s/\Qns1.{DOMAIN_NAME}.\E/ns1.$ZONE_NAME./gm;
${$tpl} =~ s/\Qhostmaster.{DOMAIN_NAME}.\E/hostmaster.$ZONE_NAME./gm;
# Set NS records and glue records
my $nsRecordB = getBloc( "; dmn NS entry BEGIN\n", "; dmn NS entry ENDING\n", ${$tpl} );
my $glueRecordB = getBloc( "; dmn NS A entry BEGIN\n", "; dmn NS A entry ENDING\n", ${$tpl} );
my ($nsRecords, $glueRecords) = ('', '');
my $net = iMSCP::Net->getInstance();
for my $ipAddrType(qw/ ipv4 ipv6 /) {
my $nsNumber = 1;
for my $ipAddr(@NAMESERVERS) {
next unless $net->getAddrVersion( $ipAddr ) eq $ipAddrType;
$nsRecords .= process(
{
NS_NUMBER => $nsNumber.'.'.$ZONE_NAME.'.'
},
$nsRecordB
);
# Glue records must be set only if $data->{'DOMAIN_NAME'] is equal to $ZONE_NAME
$glueRecords .= process(
{
NS_NUMBER => $nsNumber.'.'.$ZONE_NAME.'.',
NS_IP_TYPE => ($ipAddrType eq 'ipv4') ? 'A' : 'AAAA',
NS_IP => $ipAddr
},
$glueRecordB
) unless $ZONE_NAME ne $data->{'DOMAIN_NAME'};
$nsNumber++;
}
}
${$tpl} = replaceBloc("; dmn NS entry BEGIN\n", "; dmn NS entry ENDING\n", $nsRecords, ${$tpl});
${$tpl} = replaceBloc("; dmn NS A entry BEGIN\n", "; dmn NS A entry ENDING\n", $glueRecords, ${$tpl});
0;
}
);
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment