- Timestamp:
- 01/17/23 18:22:00 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB/ExportBIND.pm
r877 r878 4 4 ## 5 5 # $Id$ 6 # Copyright 2022 Kris Deugau <kdeugau@deepnet.cx>6 # Copyright 2022,2023 Kris Deugau <kdeugau@deepnet.cx> 7 7 # 8 8 # This program is free software: you can redistribute it and/or modify … … 25 25 use warnings; 26 26 27 use DNSDB; 28 27 29 sub export { 28 30 # expected to be a DNSDB object … … 86 88 ##fixme: need to open separate zone files for aggregated metazones eg /22 or /14 87 89 foreach my $loc (@loclist) { 88 my $zfilepath = $dnsdb-> bind_export_reverse_zone_path};90 my $zfilepath = $dnsdb->{bind_export_reverse_zone_path}; 89 91 $zfilepath =~ s/\%view/$loc/; 90 92 $zfilepath =~ s/\%zone/$revzone/; … … 111 113 112 114 printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $arpazone, $loc, scalar(localtime) 113 or die "Error writing header [$ zone, '$loc']: $!\n";;115 or die "Error writing header [$cidr, '$loc']: $!\n"; 114 116 115 117 # need to fetch this separately since the rest of the records all (should) have real IPs in val … … 117 119 my (@zsoa) = $soasth->fetchrow_array(); 118 120 ##fixme: do we even need @loclist passed in? 119 publishrec_bind( \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr,121 publishrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'y', \%recflags, $cidr, 120 122 $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, ''); 121 123 } # if force_refresh etc … … 127 129 # now the meat of the records 128 130 $recsth->execute($revid); 129 my $fullzone = _ZONE($tmpzone, 'ZONE', 'r', '.').($tmpzone->{isv6} ? '.ip6.arpa' : '.in-addr.arpa');130 131 131 132 while (my ($host, $type, $val, $dist, $weight, $port, $ttl, $recid, $loc, $stamp, $expires, $stampactive) … … 136 137 if ($val =~ /\.arpa$/) { 137 138 # val is non-IP 138 if ($val !~ /$ fullzone$/) {139 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $ tmpzone)\n";139 if ($val !~ /$arpazone$/) { 140 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $cidr)\n"; 140 141 next; 141 142 } 142 143 } else { 143 144 my $ipval = new NetAddr::IP $val; 144 if (!$ tmpzone->contains($ipval)) {145 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $ tmpzone)\n";145 if (!$cidr->contains($ipval)) { 146 warn "Not exporting out-of-zone record $val $typemap{$type} $host, $ttl (zone $cidr)\n"; 146 147 next; 147 148 } … … 157 158 } 158 159 159 publishrec_bind( \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone,160 publishrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'y', \%recflags, $revzone, 160 161 $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive); 161 162 … … 230 231 # %recflags = (); 231 232 232 while (my ($domid,$dom ,$domstat,$changed) = $domsth->fetchrow_array) {233 while (my ($domid,$domain,$domstat,$changed) = $domsth->fetchrow_array) { 233 234 234 235 # fetch a list of views/locations present in the zone. we need to publish a file for each one. … … 244 245 245 246 ##fixme: use tmpfile module for more secure temp files? want the zone name at least in it anyway, not sure that works... 246 my $zfile = $dom ; # can probably drop this intermediate247 my $zfile = $domain; # can probably drop this intermediate 247 248 my $tmpcache = "tmp.$zfile.$$"; # safety net. don't overwrite a previous known-good file 248 249 foreach my $loc (@loclist) { … … 289 290 290 291 # drop in a header line so we know when things went KABOOM 291 printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $ arpazone, $loc, scalar(localtime)292 or die "Error writing header [$ cidr, '$loc']: $!\n";293 294 printrec_bind( \%zonefiles, \@loclist, $zsoa[7], 'n', \%recflags, $dom,292 printf {$zonefiles{$loc}} "; %s in view %s exported %s\n", $domain, $loc, scalar(localtime) 293 or die "Error writing header [$domain, '$loc']: $!\n"; 294 295 printrec_bind($dnsdb, \%zonefiles, \@loclist, $zsoa[7], 'n', \%recflags, $domain, 295 296 $zsoa[0], $zsoa[1], $zsoa[2], $zsoa[3], $zsoa[4], $zsoa[5], $zsoa[6], $loc, ''); 296 297 297 # $self->_printrec_tiny($zonefilehandle, $zsoa[7], 'n',\%recflags, $dom,298 # $self->_printrec_tiny($zonefilehandle, $zsoa[7], 'n',\%recflags, $domain, 298 299 # $zsoa[0],$zsoa[1],$zsoa[2],$zsoa[3],$zsoa[4],$zsoa[5],$zsoa[6],$zsoa[8],''); 299 300 … … 301 302 302 303 # tag the zonefile for publication in the view 303 push @{$viewzones{$loc}}, $ arpazone;304 push @{$viewzones{$loc}}, $domain; 304 305 } # foreach @loclist 305 306 … … 320 321 321 322 # Check for out-of-zone data 322 $host = $dom if $host eq '@';323 if ($host !~ /$dom $/i) {324 warn "Not exporting out-of-zone record $host $type $val, $ttl (zone $dom )\n";323 $host = $domain if $host eq '@'; 324 if ($host !~ /$domain$/i) { 325 warn "Not exporting out-of-zone record $host $type $val, $ttl (zone $domain)\n"; 325 326 next; 326 327 } … … 328 329 $recflags{$recid} = 1; 329 330 330 printrec_bind( \%zonefiles, \@loclist, $recid, 'n', \%recflags, $dom,331 printrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'n', \%recflags, $domain, 331 332 $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive); 332 333 … … 339 340 "JOIN domains d ON r.domain_id=d.domain_id ". 340 341 "WHERE r.type=2 AND d.domain LIKE ?"); 341 $subnssth->execute('%.'.$dom );342 $subnssth->execute('%.'.$domain); 342 343 while (my ($host,$val,$ttl,$recid,$loc,$stamp,$expires,$stampactive) = $subnssth->fetchrow_array) { 343 printrec_bind( \%zonefiles, \@loclist, $recid, 'n', \%recflags, $dom,344 printrec_bind($dnsdb, \%zonefiles, \@loclist, $recid, 'n', \%recflags, $domain, 344 345 $host, 2, $val, '', '', '', $ttl, $loc, $stamp, $expires, $stampactive); 345 346 } # subdomain-ns-recsth … … 355 356 # } 356 357 357 } # if $changed or cache filesize is 0358 # } # if $changed or cache filesize is 0 358 359 359 360 }; 360 361 if ($@) { 361 die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$ revzone: $@\n";362 die "error writing ".($dnsdb->{usecache} ? 'new data for ' : '')."$domain: $@\n"; 362 363 # error! something borked, and we should be able to fall back on the old cache file 363 364 # report the error, somehow. … … 365 366 # mark zone as unmodified. Only do this if no errors, that way 366 367 # export failures should recover a little more automatically. 367 $zonesth->execute($ revid);368 $zonesth->execute($domid); 368 369 } 369 370 … … 389 390 open BINDCONF, ">", $tmpconf; 390 391 391 foreach my $view (@{$viewlist}, 'common' ) {392 foreach my $view (@{$viewlist}, 'common', { location => 'common', iplist => '' }) { 392 393 #print Dumper($view); 393 394 print BINDCONF "view $view->{location} {\n"; … … 406 407 foreach my $zone (@{$viewzones{$view->{location}}}) { 407 408 ##fixme: notify settings, maybe per-zone? 408 print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n); 409 print BINDCONF qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n); 410 # print qq( zone "$zone" IN {\n\ttype master;\n\tnotify no;\n\tfile "db.$zone";\n };\n); 409 411 } 410 412 print BINDCONF "};\n\n"; … … 431 433 if ($loc eq '') { 432 434 foreach my $subloc (@{$loclist}) { 433 publishrec_bind($ zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, $distance, $weight, $port, $ttl,434 $ subloc, $stamp, $expires, $stampactive);435 publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, $type, $val, 436 $distance, $weight, $port, $ttl, $subloc, $stamp, $expires, $stampactive); 435 437 } 436 438 } … … 489 491 490 492 } else { 491 my $recdata = "$host. $ttl IN NS $val.\n" 493 my $recdata = "$host. $ttl IN NS $val.\n"; 492 494 recprint($zonefiles, $loclist, $loc, $recdata); 493 495 } … … 587 589 # $$recflags{$val}++; 588 590 if ($revrec eq 'y') { 589 publishrec_bind($ zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val, $distance, $weight, $port, $ttl,590 $ loc, $stamp, $expires, $stampactive);591 publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 12, $val, 592 $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive); 591 593 # ... but we need to tweak it for this case? so the A record gets published... 592 594 #$$recflags{$val} = 'a+ptr'; 593 595 #print {$zonefiles->{$loc}} "=$host:$val:$ttl:$stamp:$loc\n" or die $!; 594 # publishrec_bind( \%zonefiles, $recid, 'y', \@loclist, $revzone,596 # publishrec_bind($dnsdb, \%zonefiles, $recid, 'y', \@loclist, $revzone, 595 597 # $host, $type, $val, $dist, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive); 596 598 # my ($zonefiles, $recid, $revrec, $loclist, $zone, $host, $type, $val, $distance, $weight, $port, $ttl, 597 599 # $loc, $stamp, $expires, $stampactive) = @_; 598 600 } else { 599 publishrec_bind($ zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val, $distance, $weight, $port, $ttl,600 $ loc, $stamp, $expires, $stampactive);601 publishrec_bind($dnsdb, $zonefiles, $loclist, $recid, $revrec, $recflags, $zone, $host, 1, $val, 602 $distance, $weight, $port, $ttl, $loc, $stamp, $expires, $stampactive); 601 603 # set a unique flag to skip template expansion for this IP in forward zones 602 604 $$recflags{$val} = 'a'; … … 612 614 if ($val->masklen <= 16) { 613 615 foreach my $sub ($val->split(16)) { 614 __publish_template_bind($ sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);616 __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec); 615 617 } 616 618 } else { 617 __publish_template_bind($ sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);619 __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec); 618 620 } 619 621 } # PTR template … … 626 628 if ($val->masklen < 16) { 627 629 foreach my $sub ($val->split(16)) { 628 __publish_template_bind($ sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);630 __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec); 629 631 } 630 632 } else { 631 __publish_template_bind($ sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec);633 __publish_template_bind($dnsdb, $sub, $recflags, $host, $zonefiles, $loclist, $ttl, $stamp, $loc, $zone, $revrec); 632 634 } 633 635 } # A+PTR template … … 643 645 644 646 sub __publish_template_bind { 647 my $dnsdb = shift; 645 648 my $sub = shift; 646 649 my $recflags = shift;
Note:
See TracChangeset
for help on using the changeset viewer.