1 | #!/usr/bin/perl
|
---|
2 | # Import a BIND zone file
|
---|
3 | ##
|
---|
4 | # Copyright 2020 Kris Deugau <kdeugau@deepnet.cx>
|
---|
5 | #
|
---|
6 | # This program is free software: you can redistribute it and/or modify
|
---|
7 | # it under the terms of the GNU General Public License as published by
|
---|
8 | # the Free Software Foundation, either version 3 of the License, or
|
---|
9 | # (at your option) any later version.
|
---|
10 | #
|
---|
11 | # This program is distributed in the hope that it will be useful,
|
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | # GNU General Public License for more details.
|
---|
15 | #
|
---|
16 | # You should have received a copy of the GNU General Public License
|
---|
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | ##
|
---|
19 |
|
---|
20 | use strict;
|
---|
21 | use warnings;
|
---|
22 | use Data::Dumper;
|
---|
23 |
|
---|
24 | use lib '.';
|
---|
25 | use DNSDB;
|
---|
26 |
|
---|
27 | my $dnsdb = new DNSDB;
|
---|
28 |
|
---|
29 | #print Dumper(\%reverse_typemap);
|
---|
30 |
|
---|
31 | my $zname = shift @ARGV;
|
---|
32 | my $rev = 'n';
|
---|
33 | my $zid;
|
---|
34 |
|
---|
35 | if ($zname =~ /\.arpa$/ || $zname =~ m,^[\d./]+$,) {
|
---|
36 | $rev = 'y';
|
---|
37 | $zname = _zone2cidr($zname) if $zname =~ /\.arpa$/;
|
---|
38 | $zid = $dnsdb->revID($zname,':ANY:');
|
---|
39 | } else {
|
---|
40 | $zid = $dnsdb->domainID($zname,':ANY:');
|
---|
41 | }
|
---|
42 |
|
---|
43 | die "zone $zname not on file\n" if !$zid;
|
---|
44 |
|
---|
45 | ##fixme: retrieve defttl from SOA record
|
---|
46 | my $zonettl = 900;
|
---|
47 |
|
---|
48 | while (<>) {
|
---|
49 | next if /^\s*$/;
|
---|
50 | next if /^\s*;/;
|
---|
51 | my ($name) = /([\w_.-]+)\s/;
|
---|
52 | s/([\w_.-]+)\s+//;
|
---|
53 | my ($class) = /(IN|CS|CH|HS)\s/;
|
---|
54 | if ($class) {
|
---|
55 | if ($class ne 'IN') {
|
---|
56 | print "Non-Internet class records not supported, you weirdo\n";
|
---|
57 | next;
|
---|
58 | }
|
---|
59 | s/(IN|CS|CH|HS)\s+//;
|
---|
60 | } else {
|
---|
61 | $class = 'IN' if !$class;
|
---|
62 | }
|
---|
63 | my ($ttl) = /(\d+)?\s/;
|
---|
64 | if (defined $ttl) {
|
---|
65 | # TTL may be zero
|
---|
66 | s/(\d+)?\s+//;
|
---|
67 | } else {
|
---|
68 | # Fall back to zone default TTL
|
---|
69 | $ttl = $zonettl;
|
---|
70 | }
|
---|
71 | my ($type) = /([A-Z-]+)\s/;
|
---|
72 | if (!$reverse_typemap{$type}) {
|
---|
73 | print "Unknown type $type, skipping\n";
|
---|
74 | next;
|
---|
75 | }
|
---|
76 | my $itype = $reverse_typemap{$type};
|
---|
77 | s/([A-Z-]+)\s+//;
|
---|
78 | chomp;
|
---|
79 | my $rdata = $_;
|
---|
80 |
|
---|
81 | # Quotes may arguably be syntactically required, but they're not actually part of the record data
|
---|
82 | if ($itype == 16) {
|
---|
83 | $rdata =~ s/^"//;
|
---|
84 | $rdata =~ s/"$//;
|
---|
85 | }
|
---|
86 |
|
---|
87 | no warnings qw(uninitialized);
|
---|
88 | print "parsed: '$name' '$class' '$ttl' '$type'->'$itype' '$rdata'\t";
|
---|
89 | #print;
|
---|
90 | #;imap IN 900 CNAME deepnet.cx.
|
---|
91 | ##fixme: not sure how to handle the case where someone leaves off the class.
|
---|
92 | my ($code,$msg) = $dnsdb->addRec('n', $rev, $zid, \$name, \$itype, \$rdata, $ttl);
|
---|
93 | print "$code: $msg\n";
|
---|
94 | }
|
---|