[925] | 1 | #!/usr/bin/perl
|
---|
| 2 | # Import script for autoaxfr data
|
---|
| 3 | ##
|
---|
| 4 | # $Id: autoaxfr-import.pl 925 2025-08-20 21:32:47Z kdeugau $
|
---|
| 5 | # Copyright 2025 Kris Deugau <kdeugau@deepnet.cx>
|
---|
| 6 | #
|
---|
| 7 | # This program is free software: you can redistribute it and/or modify
|
---|
| 8 | # it under the terms of the GNU General Public License as published by
|
---|
| 9 | # the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | # (at your option) any later version.
|
---|
| 11 | #
|
---|
| 12 | # This program is distributed in the hope that it will be useful,
|
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | # GNU General Public License for more details.
|
---|
| 16 | #
|
---|
| 17 | # You should have received a copy of the GNU General Public License
|
---|
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | ##
|
---|
| 20 |
|
---|
| 21 | use strict;
|
---|
| 22 | use warnings;
|
---|
| 23 |
|
---|
| 24 | use DBI;
|
---|
| 25 | use Data::Dumper;
|
---|
| 26 |
|
---|
| 27 | # Taint-safe (ish) voodoo to push "the directory the script is in" into @INC.
|
---|
| 28 | use File::Spec ();
|
---|
| 29 | use File::Basename ();
|
---|
| 30 | my $path;
|
---|
| 31 | BEGIN {
|
---|
| 32 | $path = File::Basename::dirname(File::Spec->rel2abs($0));
|
---|
| 33 | if ($path =~ /(.*)/) {
|
---|
| 34 | $path = $1;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 | use lib $path;
|
---|
| 38 |
|
---|
| 39 | use DNSDB;
|
---|
| 40 |
|
---|
| 41 | my $dnsdb = new DNSDB;
|
---|
| 42 |
|
---|
| 43 | usage() if !@ARGV;
|
---|
| 44 |
|
---|
| 45 | sub usage {
|
---|
| 46 | die q(usage: autoaxfr-import.pl /path/to/autoaxfr-root
|
---|
| 47 | /path/to/autoaxfr-root is the directory containing the slaves/
|
---|
| 48 | directory that tells autoaxfr which zones to AXFR from which
|
---|
| 49 | hosts, and the zones/ directory where it leaves the tinydns-formatted
|
---|
| 50 | zone data.
|
---|
| 51 |
|
---|
| 52 | All zones are imported as there is no sane way to share manually-
|
---|
| 53 | managed zones and dnsadmin-managed zones in one autoaxfr instance.
|
---|
| 54 | );
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | ##fixme: option flags for these
|
---|
| 58 | my $cverbose = 0;
|
---|
| 59 | my $group = 1;
|
---|
| 60 | my $status = 1;
|
---|
| 61 |
|
---|
| 62 | my $dbh = $dnsdb->{dbh};
|
---|
| 63 |
|
---|
| 64 | # collect some things for logging
|
---|
| 65 | ($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
|
---|
| 66 | $dnsdb->{logfullname} =~ s/,//g;
|
---|
| 67 | $dnsdb->{loguserid} = 0; # not worth setting up a pseudouser the way the RPC system does
|
---|
| 68 | $dnsdb->{logusername} = $dnsdb->{logusername}."/autoaxfr-import.pl";
|
---|
| 69 | $dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
|
---|
| 70 | $dnsdb->{logfullname} = $dnsdb->{logfullname}."/autoaxfr-import.pl";
|
---|
| 71 |
|
---|
| 72 | $dbh->{AutoCommit} = 0;
|
---|
| 73 | $dbh->{RaiseError} = 1;
|
---|
| 74 |
|
---|
| 75 | eval {
|
---|
| 76 |
|
---|
| 77 | my $logparent = $dnsdb->_log(group_id => $group,
|
---|
| 78 | entry => "[ Importing secondary zones from autoaxfr root path $ARGV[0] ]");
|
---|
| 79 | my $autoroot = "$ARGV[0]/slaves/";
|
---|
| 80 |
|
---|
| 81 | my $zsth = $dbh->prepare("INSERT INTO secondaryzones (zone, primaryserver, group_id, status) VALUES (?,?,?,?) RETURNING secondary_id");
|
---|
| 82 |
|
---|
| 83 | if (opendir AUTOROOT, $autoroot) {
|
---|
| 84 | while (my $zone = readdir AUTOROOT) {
|
---|
| 85 | next if $zone eq '.' || $zone eq '..';
|
---|
| 86 | open PRIMARY, "$autoroot$zone";
|
---|
| 87 | my @primary;
|
---|
| 88 | foreach (<PRIMARY>) {
|
---|
| 89 | chomp;
|
---|
| 90 | push @primary, $_;
|
---|
| 91 | }
|
---|
| 92 | print "$zone => ".join(', ',@primary)."\n" if $verbose;
|
---|
| 93 | $zsth->execute($zone, join(',',@primary), $group, $status);
|
---|
| 94 | my ($zid) = $zsth->fetchrow_array;
|
---|
| 95 | $dnsdb->_log(group_id => $group, secondary_id => $zid, logparent => $logparent,
|
---|
| 96 | entry => "[autoaxfr import] Added secondary zone $zone with primary server".(scalar(@primary) > 1 ? 's' : '').
|
---|
| 97 | " ".join(', ',@primary));
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | $dbh->commit;
|
---|
| 102 | };
|
---|
| 103 | if ($@) {
|
---|
| 104 | $dbh->rollback;
|
---|
| 105 | die "error importing autoaxfr secondary zones: $@";
|
---|
| 106 | }
|
---|