Changeset 265


Ignore:
Timestamp:
03/06/12 17:57:14 (12 years ago)
Author:
Kris Deugau
Message:

/trunk

Checkpoint, "add reverse zone" zone-info subsititution almost
complete
See #26

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r264 r265  
    606606  return ('OK','OK');
    607607} # done AAAA+PTR template record
     608
     609
     610##
     611## Record data substitution subs
     612##
     613
     614# Replace ZONE in hostname
     615sub _ZONE {
     616  my $zone = shift;
     617  my $string = shift;
     618  my $fr = shift || 'f';        # flag for forward/reverse order?  nb: ignored for IP
     619
     620  my $prefix = $zone->network->addr;    # Just In Case someone managed to slip in
     621                                        # a funky subnet that had host bits set.
     622
     623  $string =~ s/,/./ if !$zone->{isv6};
     624  $string =~ s/,/::/ if $zone->{isv6};
     625
     626#  if ($zone->{isv6} && ($zone->masklen % 4) != 0) {
     627#    # grumpyfail, non-nibble zone.  shouldn't happen
     628#    return;
     629#  }
     630
     631    # Subbing ZONE in the host.  We need to properly ID the netblock range
     632    # The subbed text should have "network IP with trailing zeros stripped" for
     633    # blocks lined up on octet (for v4) or 16-bit (for v6) boundaries
     634    # For blocks that do NOT line up on these boundaries, we tack on an extra "-0",
     635    # then take the most significant octet or 16-bit chunk of the "broadcast" IP and
     636    # append it after a double-dash
     637    # ie:
     638    # 8.0.0.0/6 -> 8.0.0.0 -> 11.255.255.255;  sub should be 8--11
     639    # 10.0.0.0/12 -> 10.0.0.0 -> 10.0.0.0 -> 10.15.255.255;  sub should be 10-0--15
     640    # 192.168.4.0/22 -> 192.168.4.0 -> 192.168.7.255;  sub should be 192-168-4--7
     641    # 192.168.0.8/29 -> 192.168.0.8 -> 192.168.0.15;  sub should be 192-168-0-8--15
     642    # Similar for v6
     643    if (!$zone->{isv6}) {
     644      my $bc = $zone->broadcast->addr;
     645      if ($zone->masklen > 24) {
     646        $bc =~ s/^\d+\.\d+\.\d+\.//;
     647      } elsif ($zone->masklen > 16) {
     648        $prefix =~ s/\.0$//;
     649        $bc =~ s/^\d+\.\d+\.//;
     650      } elsif ($zone->masklen > 8) {
     651        $bc =~ s/^\d+\.//;
     652        $prefix =~ s/\.0\.0$//;
     653      } else {
     654        $prefix =~ s/\.0\.0\.0$//;
     655      }
     656      if ($zone->masklen % 8) {
     657        $bc =~ s/(\.255)+$//;
     658        $prefix .= "--$bc";     #"--".zone->masklen;    # use range or mask length?
     659      }
     660    } else {
     661      if (($zone->masklen % 16) != 0) {
     662        # Strip trailing :0 off $prefix, and :ffff off the broadcast IP
     663        # Strip the leading 16-bit chunks off the front of the broadcast IP
     664        # Append the remaining 16-bit chunk to the prefix after "--"
     665        my $bc = $zone->broadcast->addr;
     666        for (my $i=0; $i<(7-int($zone->masklen / 16)); $i++) {
     667          $prefix =~ s/:0$//;
     668          $bc =~ s/:ffff$//;
     669        }
     670        $bc =~ s/^([a-f0-9]+:)+//;
     671        $prefix .= "--$bc";
     672      } else {
     673        # Strip off :0 from the end until we reach the netblock length.
     674        for (my $i=0; $i<(8-$zone->masklen / 16); $i++) {
     675          $prefix =~ s/:0$//;
     676        }
     677      }
     678    }
     679
     680  # Replace . and : with -
     681  # If flagged for reverse-order, split on . or :, reverse, and join with -
     682    if ($fr eq 'f') {
     683      $prefix =~ s/[:.]+/-/g;
     684    } else {
     685      $prefix = join('-', reverse(split(/[:.]/, $prefix)));
     686    }
     687    $string =~ s/ZONE/$prefix/;
     688#  }
     689  return $string;
     690} # done _ZONE
    608691
    609692
     
    12881371      $host =~ s/ADMINDOMAIN/$config{domain}/g;
    12891372
    1290 ## tmp
    1291 sub _ZONE {
    1292   my $zone = shift;
    1293   my $string = shift;
    1294   my $t1 = shift;       # flag for IP/network or hostname
    1295   my $t2 = shift || 'f';        # flag for forward/reverse order?  nb: ignored for IP
    1296 
    1297   my $prefix = $zone->network->addr;    # Just In Case someone managed to slip in
    1298                                         # a funky subnet that had host bits set.
    1299   $prefix =~ s/(:0)+$/::/;
    1300   $prefix =~ s/0$//;
    1301 
    1302   if ($t1 eq 'net') {
    1303     $string =~ s/ZONE[:.]+/$prefix/;
    1304   } else {
    1305     $prefix =~ s/[:.]+$//;
    1306     if ($t2 eq 'f') {
    1307       $prefix =~ s/[:.]+/-/g;
    1308     } else {
    1309       $prefix = join('-', reverse(split(/[:.]/, $prefix)));
    1310     }
    1311     $string =~ s/ZONE/$prefix/;
    1312   }
    1313   return $string;
    1314 }
    1315 ## tmp
    1316 
    1317 # Check the zone;  if it's v4 and smaller than a /24 (sub-octet), we need
    1318 # to check any template IPs to make sure they're in range.
    1319 ##fixme: need similar check on v6, except we'll just grump at non-nibble boundaries
    1320 # first-pass:  masklen must be divisible by 4
    1321 if (!$zone->{isv6} && $zone->masklen > 24) {
    1322       my $net = $zone->network->addr;
    1323       my $bc = $zone->broadcast;
    1324       $net =~ s/^\d+\.\d+\.\d+\.//;
    1325       $bc =~ s/^\d+\.\d+\.\d+\.//;
    1326       my $tmp = $host;
    1327       $tmp =~ s/^ZONE\.//;
    1328       if ($net <= $tmp && $tmp <= $bc) {
    1329       # replace ZONE with actual netblock in IP/value
    1330         unless (my $tmpval = _ZONE($zone, $val, 'net')) {
    1331           $warnstr .= "$val does not fit in $zone;  skipping\n";
     1373      # Check to make sure the IP stubs will fit in the zone.  Under most usage failures here should be rare.
     1374      # On failure, tack a note on to a warning string and continue without adding this record.
     1375      # While we're at it, we substitute $zone for ZONE in the value.
     1376      if ($val eq 'ZONE') {
     1377        $val = $zone->network;
     1378      } elsif ($val =~ /ZONE/) {
     1379        my $tmpval = $val;
     1380        $tmpval =~ s/ZONE//;
     1381        my $addr;
     1382        if (_ipparent($dbh, 'n', 'y', \$tmpval, $rdns_id, \$addr)) {
     1383          $val = $addr->addr;
     1384        } else {
     1385          $warnstr .= "Default record '$val $typemap{$type} $host' doesn't fit in $zone, skipping\n";
    13321386          next;
    13331387        }
    1334       } else {
    1335         $warnstr .= "Default record '$val $typemap{$type} $host' doesn't fit in $zone, skipping\n";
    13361388      }
    1337 }
     1389
     1390      # Substitute $zone for ZONE in the hostname.
     1391      $host = _ZONE($zone, $host);
    13381392
    13391393##work
    1340 # - replace ZONE in $val
    1341 #      $val =~ s/DOMAIN/$domain/g;
    1342 
    13431394      $sth_in->execute($host,$type,$val,$ttl);
    13441395      if ($typemap{$type} eq 'SOA') {
     
    13561407                username => $userinfo{name}, entry =>
    13571408                $logentry." $val', TTL $ttl"));
     1409#print $logentry." $val', TTL $ttl\n";
    13581410      }
    13591411    }
     
    13631415        username => $userinfo{name}, entry => $warnstr))
    13641416        if $warnstr;
    1365 
     1417#print $warnstr;
    13661418    # once we get here, we should have suceeded.
     1419#die "horribly\n";
    13671420    $dbh->commit;
    13681421  }; # end eval
Note: See TracChangeset for help on using the changeset viewer.