Changeset 353 for trunk/tiny-import.pl


Ignore:
Timestamp:
06/27/12 17:20:54 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint filling in tinydns data import stubs

  • Move a useful sub up the hierarchy
  • Include explicit count/limit in split() calls; lets us catch possible syntax oopses that would give strange timestamp or location entries
  • Convert existing record inserts to use prepared statement
  • Extend CNAME support to handle CNAMEs properly in reverse zones
  • Mostly finish tinydns "." type (SOA+NS+A) - note these seem to be mainly useful for very small installations, not large ones.
  • Add stub for location/view entries

See #26, partly

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tiny-import.pl

    r348 r353  
    3838my %cnt;
    3939my @deferred;
     40my $errstr = '';
    4041
    4142foreach my $file (@ARGV) {
     
    4849  if ($@) {
    4950    print "bleh: $@\n";
    50 die "die harder\n";
     51die "die harder: $errstr\n";
    5152  }
    5253}
     
    6566  our $recsth = $dbh->prepare("INSERT INTO records (domain_id,rdns_id,host,type,val,distance,weight,port,ttl) ".
    6667        " VALUES (?,?,?,?,?,?,?,?,?)");
     68
     69  my %deleg;
    6770
    6871  while (<FLAT>) {
     
    7982  }
    8083
     84print scalar(@deferred)." deferred records in $flatfile\n";
     85
    8186  # Sub for various nonstandard types with lots of pure bytes expressed in octal
    82   # Takes a tinydns rdata string and count, returns a lis of $count bytes as well
     87  # Takes a tinydns rdata string and count, returns a list of $count bytes as well
    8388  # as trimming those logical bytes off the front of the rdata string.
    8489  sub _byteparse {
     
    103108  }
    104109
     110  # Convert octal-coded bytes back to something resembling normal characters, general case
     111  sub _deoctal {
     112    my $targ = shift;
     113    while ($$targ =~ /\\(\d{3})/) {
     114      my $sub = chr(oct($1));
     115      $$targ =~ s/\\$1/$sub/g;
     116    }
     117  }
     118
    105119  sub recslurp {
    106120    my $rec = shift;
     
    114128#=sud-rr-iGi0-1_sud-gw1-iGi4-2.vianet.ca::10.10.10.13:900::in
    115129}
    116       my ($host,$ip,$ttl,$time,$loc) = split /:/, $rec;
     130      my ($host,$ip,$ttl,$stamp,$loc) = split /:/, $rec, 5;
    117131      $host =~ s/^=//;
    118132      $host =~ s/\.$//;
    119       $time = '' if !$time;
    120       $loc = '' if !$loc;
    121       print "bleh, bad A+PTR!  $rec\n" if $loc =~ /:/;
     133      $ttl = 0 if !$ttl;
     134      $stamp = '' if !$stamp;
     135      $loc = '' if !$loc;
     136      $loc = '' if $loc =~ /^:+$/;
    122137      my $fparent = DNSDB::_hostparent($dbh, $host);
    123138      my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($ip));
    124139      if ($fparent && $rparent) {
    125         $dbh->do("INSERT INTO records (domain_id,rdns_id,host,type,val,ttl) VALUES (?,?,?,?,?,?)", undef,
    126                 ($fparent, $rparent, $host, 65280, $ip, $ttl));
     140        $recsth->execute($fparent, $rparent, $host, 65280, $ip, 0, 0, 0, $ttl);
    127141      } else {
    128142        push @deferred, $rec unless $nodefer;
     
    132146    } elsif ($rec =~ /^C/) {
    133147      $cnt{CNAME}++;
    134       my ($host,$targ,$ttl,$time,$loc) = split /:/, $rec;
     148      my ($host,$targ,$ttl,$stamp,$loc) = split /:/, $rec, 5;
    135149      $host =~ s/^C//;
    136150      $host =~ s/\.$//;
    137       $time = '' if !$time;
    138       $loc = '' if !$loc;
    139       my $fparent = DNSDB::_hostparent($dbh, $host);
    140       if ($fparent) {
    141 
    142       } else {
    143         push @deferred, $rec unless $nodefer;
    144         #  print "$tmporig deferred;  can't find parent zone\n";
     151      $ttl = 0 if !$ttl;
     152      $stamp = '' if !$stamp;
     153      $loc = '' if !$loc;
     154      $loc = '' if $loc =~ /^:+$/;
     155      if ($host =~ /\.arpa$/) {
     156        ($code,$msg) = DNSDB::_zone2cidr($host);
     157        my ($rparent) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet >> ?", undef, ($msg));
     158        $recsth->execute(0, $rparent, $targ, 5, $msg->addr, 0, 0, 0, $ttl);
     159
     160##fixme:  automagically convert manually maintained sub-/24 delegations
     161#       my ($subip, $zone) = split /\./, $targ, 2;
     162#       ($code, $msg) = DNSDB::_zone2cidr($zone);
     163#       push @{$deleg{"$msg"}{iplist}}, $subip;
     164#print "$msg $subip\n";
     165
     166      } else {
     167        my $fparent = DNSDB::_hostparent($dbh, $host);
     168        if ($fparent) {
     169          $recsth->execute($fparent, 0, $host, 5, $targ, 0, 0, 0, $ttl);
     170        } else {
     171          push @deferred, $rec unless $nodefer;
     172          #  print "$tmporig deferred;  can't find parent zone\n";
     173        }
    145174      }
    146175
     
    154183      $cnt{SOA}++;
    155184#Z128.91.209.in-addr.arpa:ns1.vianet.ca.:dnsadmin.vianet.ca.::1209600:1209600:900:900:900:
    156       my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$time,$loc) = split /:/, $rec;
     185      my ($zone,$master,$contact,$serial,$refresh,$retry,$expire,$minttl,$ttl,$stamp,$loc) = split /:/, $rec, 11;
    157186      $zone =~ s/^Z//;
    158187      $zone =~ s/\.$//;
    159188      $master =~ s/\.$//;
    160189      $contact =~ s/\.$//;
    161       $time = '' if !$time;
    162       $loc = '' if !$loc;
     190      $ttl = 0 if !$ttl;
     191      $stamp = '' if !$stamp;
     192      $loc = '' if !$loc;
     193      $loc = '' if $loc =~ /^:+$/;
    163194      if ($zone =~ /\.arpa$/) {
    164195        ($code,$msg) = DNSDB::_zone2cidr($zone);
    165196        $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
    166197        my ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
    167         $dbh->do("INSERT INTO records (rdns_id,host,type,val,ttl) VALUES (?,?,6,?,?)", undef,
    168                 ($rdns, "$contact:$master", "$refresh:$retry:$expire:$minttl", $ttl));
     198        $recsth->execute(0, $rdns, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
    169199      } else {
    170200        $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($zone));
    171201        my ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
    172         $dbh->do("INSERT INTO records (rdns_id,host,type,val,ttl) VALUES (?,?,6,?,?)", undef,
    173                 ($domid, "$contact:$master", "$refresh:$retry:$expire:$minttl", $ttl));
    174       }
    175 
     202        $recsth->execute($domid, 0, "$contact:$master", 6, "$refresh:$retry:$expire:$minttl", 0, 0, 0, $ttl);
     203      }
     204      return;  # since we don't use the "standard" record-add call at the bottom
    176205    } elsif ($rec =~ /^\@/) {
    177206      $cnt{MX}++;
     
    179208      $cnt{TXT}++;
    180209
    181 sub _deoctal {
    182   my $targ = shift;
    183   while ($$targ =~ /\\(\d{3})/) {
    184     my $sub = chr(oct($1));
    185     $$targ =~ s/\\$1/$sub/g;
    186   }
    187 }
    188 
    189       my ($fqdn, $rdata, $ttl, $time, $loc) = split /:/, $rec;
     210      my ($fqdn, $rdata, $ttl, $stamp, $loc) = split /:/, $rec, 5;
    190211      $fqdn =~ s/^'//;
    191212      _deoctal(\$rdata);
    192 
    193 print "$fqdn TXT '$rdata'\n" if $fqdn =~ /^\*/;
     213      $ttl = 0 if !$ttl;
     214      $stamp = '' if !$stamp;
     215      $loc = '' if !$loc;
     216      $loc = '' if $loc =~ /^:+$/;
     217
    194218      my $domid = DNSDB::_hostparent($dbh, $fqdn);
    195219      if ($domid) {
     
    201225    } elsif ($rec =~ /^\./) {
    202226      $cnt{NSASOA}++;
     227      my ($fqdn, $ip, $ns, $ttl, $stamp, $loc) = split /:/, $rec, 6;
     228      $fqdn =~ s/^\.//;
     229      $fqdn =~ s/\.$//;
     230      $ns =~ s/\.$//;
     231      $ns = "$ns.ns.$fqdn" if $ns !~ /\./;
     232      $ttl = 0 if !$ttl;
     233      $stamp = '' if !$stamp;
     234      $loc = '' if !$loc;
     235      $loc = '' if $loc =~ /^:+$/;
     236
     237      if ($fqdn =~ /\.arpa$/) {
     238        ($code,$msg) = DNSDB::_zone2cidr($fqdn);
     239        my ($rdns) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revnet = ?", undef, ($msg));
     240        if (!$rdns) {
     241          $errstr = "adding revzone $msg";
     242          $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,1,1)", undef, ($msg));
     243          ($rdns) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')");
     244# this would probably make a lot more sense to do hostmaster.$config{admindomain}
     245          $recsth->execute(0, $rdns, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
     246        }
     247        $recsth->execute(0, $rdns, $ns, 2, "$msg", 0, 0, 0, $ttl);
     248##fixme:  (?)  implement full conversion of tinydns . records?
     249# -> problem:  A record for NS must be added to the appropriate *forward* zone, not the reverse
     250#$recsth->execute(0, $rdns, $ns, 1, $ip, 0, 0, 0, $ttl)
     251# ...  auto-A-record simply does not make sense in reverse zones.  Functionally
     252# I think it would work, sort of, but it's a nasty mess and anyone hosting reverse
     253# zones has names for their nameservers already.
     254# Even the auto-nameserver-fqdn comes out...  ugly.
     255
     256##work
     257      } else {
     258        my ($domid) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE lower(domain) = lower(?)",
     259                undef, ($fqdn));
     260        if (!$domid) {
     261          $errstr = "adding domain $fqdn";
     262          $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,1,1)", undef, ($fqdn));
     263          ($domid) = $dbh->selectrow_array("SELECT currval('domains_domain_id_seq')");
     264          $recsth->execute($domid, 0, "hostmaster.$fqdn:$ns", 6, "16384:2048:1048576:2560", 0, 0, 0, "2560");
     265        }
     266        $recsth->execute($domid, 0, $fqdn, 2, $ns, 0, 0, 0, $ttl);
     267        $recsth->execute($domid, 0, $ns, 1, $ip, 0, 0, 0, $ttl) if $ip;
     268      }
     269
     270
     271    } elsif ($rec =~ /^\%/) {
     272      $cnt{VIEWS}++;
    203273    } elsif ($rec =~ /^:/) {
    204274      $cnt{NCUST}++;
Note: See TracChangeset for help on using the changeset viewer.