- Timestamp:
- 09/15/22 16:33:50 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB/ExportBIND.pm
r854 r855 27 27 sub export { 28 28 # expected to be a DNSDB object 29 my $self = shift; 30 my $dbh = $self->{dbh}; 29 my $dnsdb = shift; 31 30 32 31 # to be a hash of views/locations, containing lists of zones … … 34 33 35 34 # allow for future exports of subgroups of records 36 my $viewlist = $ self->getLocList(curgroup => 1);37 38 my $soasth = $d bh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ".35 my $viewlist = $dnsdb->getLocList(curgroup => 1); 36 37 my $soasth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location ". 39 38 "FROM records WHERE rdns_id=? AND type=6"); 40 my $recsth = $d bh->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ".39 my $recsth = $dnsdb->{dbh}->prepare("SELECT host,type,val,distance,weight,port,ttl,record_id,location,extract(epoch from stamp),expires,stampactive ". 41 40 "FROM records WHERE rdns_id=? AND NOT type=6 ". 42 41 "ORDER BY masklen(inetlazy(val)) DESC, inetlazy(val)"); 43 42 44 43 # Fetch active zone list 45 my $revsth = $ self->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ".44 my $revsth = $dnsdb->{dbh}->prepare("SELECT rdns_id,revnet,status,changed,default_location FROM revzones WHERE status=1 ". 46 45 "ORDER BY masklen(revnet) DESC, rdns_id"); 47 46 # Unflag changed zones, so we can maybe cache the export and not redo everything every time 48 my $zonesth = $ self->{dbh}->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?");47 my $zonesth = $dnsdb->{dbh}->prepare("UPDATE revzones SET changed='n' WHERE rdns_id=?"); 49 48 $revsth->execute(); 50 49 while (my ($revid,$revzone,$revstat,$changed,$defloc) = $revsth->fetchrow_array) { 51 50 my $tmpzone = NetAddr::IP->new($revzone); 52 51 my $zfile = $tmpzone->network->addr."-".$tmpzone->masklen; 53 # my $cachefile = "$ self->{exportcache}/$zfile";54 # my $tmpcache = "$ self->{exportcache}/tmp.$zfile.$$";52 # my $cachefile = "$dnsdb->{exportcache}/$zfile"; 53 # my $tmpcache = "$dnsdb->{exportcache}/tmp.$zfile.$$"; 55 54 my $tmpcache = "tmp.$zfile.$$"; # safety net. don't overwrite a previous known-good file 56 55 … … 68 67 # - the cache file does not exist 69 68 # - the cache file is empty 70 if (!$ self->{usecache} || $self->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) {71 if ($ self->{usecache}) {69 if (!$dnsdb->{usecache} || $dnsdb->{force_refresh} || $changed || !-e $cachefile || -z $cachefile) { 70 if ($dnsdb->{usecache}) { 72 71 open ZONECACHE, ">$tmpcache" or die "Error creating temporary file $tmpcache: $!\n"; 73 72 $zonefilehandle = *ZONECACHE; … … 76 75 # fetch a list of views/locations present in the zone. we need to publish a file for each one. 77 76 # in the event that no locations are present (~~ $viewlist is empty), /%view collapses to nothing in the zone path 78 my (@loclist) = $ self->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid);77 my (@loclist) = $dnsdb->{dbh}->selectrow_array("SELECT DISTINCT location FROM records WHERE rdns_id = ?", undef, $revid); 79 78 push @loclist, $defloc unless grep /$defloc/, @loclist; 80 my $zonepath = $ self->{bind_export_zone_path};79 my $zonepath = $dnsdb->{bind_export_zone_path}; 81 80 my %zonefiles; # to be a list of file handles. 82 81 ##fixme: convert logical revzone into .arpa name … … 94 93 $soasth->execute($revid); 95 94 my (@zsoa) = $soasth->fetchrow_array(); 96 printrec_bind($zonefilehandle, $zsoa[7], 'y', \%recflags,$revzone,95 printrec_bind($zonefilehandle, $zsoa[7], 'y', \@loclist, $revzone, 97 96 $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],''); 98 97 … … 135 134 } # while ($recsth) 136 135 137 if ($ self->{usecache}) {136 if ($dnsdb->{usecache}) { 138 137 close ZONECACHE; # force the file to be written 139 138 # catch obvious write errors that leave an empty temp file … … 148 147 }; 149 148 if ($@) { 150 die "error writing ".($ self->{usecache} ? 'new data for ' : '')."$revzone: $@\n";149 die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$revzone: $@\n"; 151 150 # error! something borked, and we should be able to fall back on the old cache file 152 151 # report the error, somehow. … … 157 156 } 158 157 159 # if ($ self->{usecache}) {158 # if ($dnsdb->{usecache}) { 160 159 # # We've already made as sure as we can that a cached zone file is "good", 161 160 # # although possibly stale/obsolete due to errors creating a new one. … … 204 203 # Print individual records in BIND format 205 204 sub printrec_bind { 205 my $dnsdb = shift; 206 206 my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl, 207 $ recloc, $stamp, $expires, $stampactive) = @_;207 $loc, $stamp, $expires, $stampactive) = @_; 208 208 209 209 # Just In Case something is lingering in the DB 210 $ recloc = '' if !$loc;210 $loc = '' if !$loc; 211 211 212 212 ## And now to the records! … … 225 225 # anyone who says they need sub-nibble v6 delegations, at this time, needs their head examined. 226 226 ##fixme?: alternate SOA serial schemes? 227 ($serial) = $ self->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone);227 ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM revzones WHERE revnet=?", undef, $zone); 228 228 $zone = NetAddr::IP->new($zone); 229 229 # handle split-n-multiply SOA for off-octet (8 < mask < 16) or (16 < mask < 24) v4 zones 230 230 if (!$zone->{isv6} && ($zone->masklen < 24) && ($zone->masklen % 8 != 0)) { 231 231 foreach my $szone ($zone->split($zone->masklen + (8 - $zone->masklen % 8))) { 232 $szone = _ZONE($szone, 'ZONE.in-addr.arpa', 'r', '.'); 233 print $datafile "Z$szone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n" 232 $szone = DNSDB::_ZONE($szone, 'ZONE.in-addr.arpa', 'r', '.'); 233 # print $datafile "Z$szone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n" 234 print "$szone $ttl IN SOA $primary $email ( $serial $refresh $retry $expire $min_ttl )\n" 234 235 or die $!; 235 236 } 236 237 return; # skips "default" bits just below 237 238 } 238 $zone = _ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');239 $zone = DNSDB::_ZONE($zone, 'ZONE', 'r', '.').($zone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa'); 239 240 } else { 240 241 # just snarfing the right SOA serial for the zone type 241 242 ##fixme?: alternate SOA serial schemes? 242 ($serial) = $ self->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone);243 ($serial) = $dnsdb->{dbh}->selectrow_array("SELECT zserial FROM domains WHERE domain=?", undef, $zone); 243 244 } # revrec <> 'y' 244 245 # suppress a "uninitialized value" warning. should be impossible but... 245 246 # abuse hours as the last digit pair of the serial for simplicity 246 247 $serial = strftime("%Y%m%d%H", localtime()) if !$serial; 247 print $zonefiles{$recloc} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n" 248 or die $!; 248 # print *{$zonefiles->{$loc}} "Z$zone:$primary:$email:$serial:$refresh:$retry:$expire:$min_ttl:$ttl:$stamp:$loc\n" 249 # or die $!; 250 print *{$zonefiles->{$loc}} "$zone $ttl IN SOA $primary $email ( $serial $refresh $retry $expire $min_ttl )\n" 251 or die $!; 249 252 } # SOA 250 253
Note:
See TracChangeset
for help on using the changeset viewer.