Changeset 682 for trunk/dns-rpc.cgi


Ignore:
Timestamp:
06/16/15 18:13:50 (10 years ago)
Author:
Kris Deugau
Message:

/trunk

Add templatesToRecords() to RPC handler. Most of this sub should
probably be moved into DNSDB.pm, but it has limited usefulness natively.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/dns-rpc.cgi

    r681 r682  
    9090        'dnsdb.splitTemplate'   => \&splitTemplate,
    9191        'dnsdb.resizeTemplate'  => \&resizeTemplate,
     92        'dnsdb.templatesToRecords'      => \&templatesToRecords,
    9293        'dnsdb.delRec'          => \&delRec,
    9394        'dnsdb.delByCIDR'       => \&delByCIDR,
     
    747748} # done resizeTemplate()
    748749
     750# Convert one or more template records to a set of individual IP records.  Expands the template.
     751# Handle the case of nested templates, although the primary caller (IPDB) should not be
     752# able to generate records that would trigger that case.
     753# Accounts for existing PTR or A+PTR records same as on-export template expansion.
     754# Takes a list of templates and a bounding CIDR?
     755sub templatesToRecords {
     756  my %args = @_;
     757
     758  _commoncheck(\%args, 'y');
     759
     760  my %iplist;
     761  my @retlist;
     762
     763  my $zsth = $dnsdb->{dbh}->prepare("SELECT rdns_id,group_id FROM revzones WHERE revnet >>= ?");
     764  # Going to assume template records with no expiry
     765  # Also note IPv6 template records don't expand sanely the way v4 records do
     766  my $recsth = $dnsdb->{dbh}->prepare(q(
     767      SELECT record_id, domain_id, host, type, val, ttl, location
     768      FROM records
     769      WHERE rdns_id = ?
     770          AND type IN (12, 65280, 65282, 65283)
     771          AND inetlazy(val) <<= ?
     772      ORDER BY masklen(inetlazy(val)) DESC
     773  ));
     774  my $insth = $dnsdb->{dbh}->prepare("INSERT INTO records (domain_id, rdns_id, host, type, val, ttl, location)".
     775        " VALUES (?,?,?,?,?,?,?)");
     776  my $delsth = $dnsdb->{dbh}->prepare("DELETE FROM records WHERE record_id = ?");
     777  my %typedown = (12 => 12, 65280 => 65280, 65281 => 65281, 65282 => 12, 65283 => 65280, 65284 => 65281);
     778
     779  my @checkrange;
     780
     781  local $dnsdb->{dbh}->{AutoCommit} = 0;
     782  local $dnsdb->{dbh}->{RaiseError} = 1;
     783
     784  eval {
     785    foreach my $template (@{$args{templates}}) {
     786      $zsth->execute($template);
     787      my ($zid,$zgrp) = $zsth->fetchrow_array;
     788      if (!$zid) {
     789        push @retlist, {$template, "Zone not found"};
     790        next;
     791      }
     792      $recsth->execute($zid, $template);
     793      while (my ($recid, $domid, $host, $type, $val, $ttl, $loc) = $recsth->fetchrow_array) {
     794        # Skip single IPs with PTR or A+PTR records
     795        if ($type == 12 || $type == 65280) {
     796          $iplist{"$val/32"}++;
     797          next;
     798        }
     799        my @newips = NetAddr::IP->new($template)->split(32);
     800        $type = $typedown{$type};
     801        foreach my $ip (@newips) {
     802          next if $iplist{$ip};
     803          my $newhost = $host;
     804          DNSDB::_template4_expand(\$newhost, $ip->addr);
     805          $insth->execute($domid, $zid, $newhost, $type, $ip->addr, $ttl, $loc);
     806          $iplist{$ip}++;
     807        }
     808        $delsth->execute($recid);
     809        $dnsdb->_log(group_id => $zgrp, domain_id => $domid, rdns_id => $zid,
     810            entry => "$template converted to individual $typemap{$type} records");
     811        push @retlist, "$template converted to individual records";
     812      } # record fetch
     813    } # foreach passed template CIDR
     814
     815    $dnsdb->{dbh}->commit;
     816  };
     817  if ($@) {
     818    die "Error converting a template record to individual records: $@";
     819  }
     820
     821  return \@retlist;
     822
     823} # done templatesToRecords()
     824
    749825sub delRec {
    750826  my %args = @_;
Note: See TracChangeset for help on using the changeset viewer.