Changeset 810 for trunk


Ignore:
Timestamp:
11/24/20 17:14:09 (4 years ago)
Author:
Kris Deugau
Message:

/trunk

Third sampled iteration of bind-import

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bind-import

    r809 r810  
    2626
    2727my $dnsdb = new DNSDB;
     28my $doimport = 0;
    2829
    2930#print Dumper(\%reverse_typemap);
     
    3233my $rev = 'n';
    3334my $zid;
     35
     36my %amap;
     37my %namemap;
    3438
    3539if ($zname =~ /\.arpa\.?$/ || $zname =~ m,^[\d./]+$,) {
     
    4751die "zone $zname not on file\n" if !$zid;
    4852
     53# still no sane way to expose a human-friendly view tag on the command line.
     54my $view = shift @ARGV;
     55$view = '' if !$view;
     56
    4957##fixme:  retrieve defttl from SOA record
    5058my $zonettl = 900;
     59my $defttl = $zonettl;
     60my $recbase = $zname;   # to append to unqualified names
    5161
     62# need to spin up a full state machine-ish thing, because BIND zone files are all about context
    5263while (<>) {
     64  chomp;
    5365  next if /^\s*$/;
    5466  next if /^\s*;/;
     67  if (my ($macro,$mdetail) = (/^\s*\$(TTL|ORIGIN|INCLUDE)\s+(.+)/) ) {
     68    # macro sort of thing;  $TTL and $ORIGIN most common.  $INCLUDE is a thing, expect it to be rare in live use tho
     69    if ($macro eq 'TTL') {
     70      if ($mdetail =~ /^\d+$/) {
     71        $defttl = $mdetail;
     72      } else {
     73        warn "invalid \$TTL: $_\n";
     74      }
     75    } elsif ($macro eq 'ORIGIN') {
     76##fixme:  going to skip the stupid case of "$ORIGIN com." and the like that lie
     77# between . and the root domain we were told we're importing;  anyone using such
     78# a mess outside the root servers is clearly insane
     79# handled cases:
     80#   $ORIGIN .
     81#   $ORIGIN [zonedomain].
     82#   $ORIGIN [subdomain.zonedomain].
     83      if ($mdetail eq '.' || $mdetail =~ /$zname\.$/ || $zname =~ /$mdetail\.$/) {
     84        $recbase = $mdetail;
     85      } else {
     86        # if we continue, we either use an $ORIGIN that's out of zone, or ignore it and potentially publish incorrect records.
     87        die "bad \$ORIGIN: $_\n";
     88      }
     89    }
     90    next;
     91  }
     92  # skip stale records that have no value
     93  next if /^ip-192-168-1(12|20)-\d+/;
     94  next if /ip.add.re.\d+\s*$/;
    5595  my ($name) = /([\w_.-]+)\s/;
    5696  # append zone name to record name if missing AND not dot-terminated;
     
    5999  # impossible with valid BIND zone file but...
    60100  $name .= ".$zname" if $name !~ /$zname$/ && $zname !~ /\.$/;
     101$name = $zname if /^\s*IN/;
    61102  s/([\w_.-]+)\s+//;
    62103  my ($class) = /(IN|CS|CH|HS)\s/;
     
    94135  }
    95136
     137if ($type eq 'A') {
     138#  if ($amap{$name}) {
     139#    print "urp:  dupe name $name $rdata\n";
     140#  } else {
     141    push @{$amap{$name}}, $rdata;
     142#  }
     143  push @{$namemap{$rdata}}, $name;
     144}
     145
    96146no warnings qw(uninitialized);
    97 print "parsed: '$name' '$class' '$ttl' '$type'->'$itype' '$rdata'\t";
     147#print "parsed: '$name' '$class' '$ttl' '$type'->'$itype' '$rdata'\n";
    98148#print;
    99149#;imap   IN      900     CNAME   deepnet.cx.
    100150##fixme:  not sure how to handle the case where someone leaves off the class.
    101   my ($code, $msg);
    102   if ($rev eq 'n') {
    103     ($code,$msg) = $dnsdb->addRec('n', $rev, $zid, \$name, \$itype, \$rdata, $ttl);
    104   } else {
    105     ($code,$msg) = $dnsdb->addRec('n', $rev, $zid, \$rdata, \$itype, \$name, $ttl);
     151  if ($doimport) {
     152    my ($code, $msg);
     153    if ($rev eq 'n') {
     154      ($code,$msg) = $dnsdb->addRec('n', $rev, $zid, \$name, \$itype, \$rdata, $ttl);
     155    } else {
     156      ($code,$msg) = $dnsdb->addRec('n', $rev, $zid, \$rdata, \$itype, \$name, $ttl);
     157    }
     158    print "$code: $msg\n";
    106159  }
    107   print "$code: $msg\n";
    108160}
     161
     162
     163#print Dumper \%amap;
     164foreach my $n (keys %amap) {
     165  foreach my $ip (@{$amap{$n}}) {
     166#print "$ip     $n\n";
     167    push @{$namemap{$ip}}, $n unless grep $n, @{$namemap{$ip}};
     168  }
     169}
     170
     171#print Dumper \%namemap;
     172foreach my $ip (sort keys %namemap) {
     173  print "$ip    ".join(' ', @{$namemap{$ip}})."\n";
     174}
Note: See TracChangeset for help on using the changeset viewer.