Changeset 453 for trunk/dns-rpc.cgi


Ignore:
Timestamp:
01/15/13 18:15:32 (11 years ago)
Author:
Kris Deugau
Message:

/trunk

Continue extending RPC support for real usage/needs. See #43.

Add addOrUpdateRec() to RPC interface to simplify calls and reduce
multiple-call time cost (0.3-0.5s for the XML-RPC client to decode
the response on each call?!? - must fix!)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dns-rpc.cgi

    r452 r453  
    6363        'dnsdb.addRec'          => \&addRec,
    6464        'dnsdb.updateRec'       => \&updateRec,
     65        'dnsdb.addOrUpdateRevRec'       => \&addOrUpdateRevRec,
    6566        'dnsdb.delRec'          => \&delRec,
    6667#sub getLogCount {}
     
    111112        unless DNSDB::initRPC($dbh, (username => $argref->{rpcuser}, rpcsys => $argref->{rpcsystem},
    112113                fullname => ($argref->{fullname} ? $argref->{fullname} : $argref->{rpcuser}) ) );
     114  }
     115}
     116
     117# set location to the zone's default location if none is specified
     118sub _loccheck {
     119  my $argref = shift;
     120  if (!$argref->{location} && $argref->{defrec} eq 'n') {
     121    $argref->{location} = DNSDB::getZoneLocation($dbh, $argref->{revrec}, $argref->{parent_id});
     122  }
     123}
     124
     125# set ttl to zone defailt minttl if none is specified
     126sub _ttlcheck {
     127  my $argref = shift;
     128  if (!$argref->{ttl}) {
     129    my $tmp = DNSDB::getSOA($dbh, $argref->{defrec}, $argref->{revrec}, $argref->{parent_id});
     130    $argref->{ttl} = $tmp->{minttl};
    113131  }
    114132}
     
    370388  _commoncheck(\%args, 'y');
    371389
    372   # add records in the zone's default location if none is specified
    373   if (!$args{location} && $args{defrec} eq 'n') {
    374     $args{location} = DNSDB::getZoneLocation($dbh, $args{revrec}, $args{parent_id});
    375   }
    376 
    377   # callers may often not care about TTLs
    378   if (!$args{ttl}) {
    379     my $tmp = DNSDB::getSOA($dbh, $args{defrec}, $args{revrec}, $args{parent_id});
    380     $args{ttl} = $tmp->{minttl};
    381   }
     390  _loccheck(\%args);
     391  _ttlcheck(\%args);
    382392
    383393  my @recargs = ($dbh, $args{defrec}, $args{revrec}, $args{parent_id},
     
    419429}
    420430
     431# Takes a passed CIDR block and DNS pattern;  adds a new record or updates the record(s) affected
     432sub addOrUpdateRevRec {
     433  my %args = @_;
     434
     435  _commoncheck(\%args, 'y');
     436  $args{cidr} = new NetAddr::IP $args{cidr};
     437
     438  my $zonelist = DNSDB::getZonesByCIDR($dbh, %args);
     439  if (scalar(@$zonelist) == 0) {
     440    # enhh....  WTF?
     441  } elsif (scalar(@$zonelist) == 1) {
     442    # check if the single zone returned is bigger than the CIDR.  if so, we can just add a record
     443    my $zone = new NetAddr::IP $zonelist->[0]->{revnet};
     444    if ($zone->contains($args{cidr})) {
     445      my $reclist = DNSDB::getDomRecs($dbh, defrec => 'n', revrec => 'y',
     446        id => $zonelist->[0]->{rdns_id}, filter => "$args{cidr}");
     447      if (scalar(@$reclist) == 0) {
     448        # Aren't Magic Numbers Fun?  See pseudotype list in dnsadmin.
     449        my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
     450        addRec(defrec =>'n', revrec => 'y', parent_id => $zonelist->[0]->{rdns_id}, type => $type,
     451          address => "$args{cidr}", %args);
     452      } else {
     453        foreach my $rec (@$reclist) {
     454          next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
     455          updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
     456            parent_id => $zonelist->[0]->{rdns_id}, %args);
     457          last; # only do one record.
     458        }
     459      }
     460    } else {
     461      # ebbeh?  CIDR is only partly represented in DNS.  This needs manual intervention.
     462    } # done single-zone-contains-$cidr
     463  } else {
     464    # Overlapping reverse zones shouldn't be possible, so if we're here we've got a CIDR
     465    # that spans multiple reverse zones (eg, /23 CIDR -> 2 /24 rzones)
     466    foreach my $zdata (@$zonelist) {
     467      my $reclist = DNSDB::getDomRecs($dbh, defrec => 'n', revrec => 'y',
     468        id => $zdata->{rdns_id}, filter => $zdata->{revnet});
     469      if (scalar(@$reclist) == 0) {
     470        my $type = ($args{cidr}->{isv6} ? 65282 : ($args{cidr}->masklen == 32 ? 65280 : 65283) );
     471        addRec(defrec =>'n', revrec => 'y', parent_id => $zdata->{rdns_id}, type => $type,
     472          address => "$args{cidr}", %args);
     473      } else {
     474        foreach my $rec (@$reclist) {
     475          next unless $rec->{type} == 65282 || $rec->{type} == 65283 || $rec->{type} == 65284;
     476          updateRec(defrec =>'n', revrec => 'y', id => $rec->{record_id},
     477            parent_id => $zdata->{rdns_id}, %args);
     478          last; # only do one record.
     479        }
     480      }
     481    } # iterate zones within $cidr
     482  } # done $cidr-contains-zones
     483}
     484
    421485sub delRec {
    422486  my %args = @_;
Note: See TracChangeset for help on using the changeset viewer.