Changeset 978 for branches


Ignore:
Timestamp:
01/09/26 15:09:27 (34 hours ago)
Author:
Kris Deugau
Message:

/branches/cname-collision

Fold the "not-a-CNAME" and "only-CNAME" lookups into a loop to avoid
mistake-prone copypasta in extending CNAME collision checks further
See #72

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/cname-collision/DNSDB.pm

    r974 r978  
    635635#  my $msg = 'OK';
    636636
    637   if (${$args{rectype}} == 5) {
     637  # The record type comparison is the only difference between two passes through this chunk of code.
     638  # CNAME records require both passes, where other records only need the second one.  Downside is
     639  # that returning error messages needs to check the loop variable on top of whatever else it references.
     640  foreach my $tcompare ('<>', '=') {
     641    next if $tcompare eq '<>' && ${$args{rectype}} != 5;
    638642    # First, check for non-CNAME collisions.
    639643    my $sql = "SELECT count(*) FROM "._rectable($args{defrec}, $args{revrec}).
    640         " WHERE "._recparent($args{defrec}, $args{revrec})." = ? AND type <> 5 AND $hfield = ?";
     644        " WHERE "._recparent($args{defrec}, $args{revrec})." = ? AND type $tcompare 5 AND $hfield = ?";
    641645    if ($args{defrec} eq 'n') {
    642646      # Expired records
     
    650654    my @t = $dbh->selectrow_array($sql, undef, @lookupargs);
    651655    if ($t[0] > 0) {
    652       $errstr = "One or more non-CNAME records already exist for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
    653         ".  CNAME records cannot use the same name as other records.";
     656      if ($tcompare eq '<>') {
     657        $errstr = "One or more non-CNAME records already exist for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
     658                ".  CNAME records cannot use the same name as other records.";
     659      } else {
     660        $errstr = "There is already a CNAME present for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
     661                ".  Only one CNAME may be present for a given name.";
     662      }
    654663      return;
    655664    }
    656   }
    657 
    658   # Second, check for multiple CNAMEs
    659   my $sql = "SELECT count(*) FROM "._rectable($args{defrec}, $args{revrec}).
    660         " WHERE "._recparent($args{defrec}, $args{revrec})." = ? AND type = 5 AND $hfield = ?";
    661   if ($args{defrec} eq 'n') {
    662     # Expired records
    663     $sql .= " AND (stampactive = 'f' OR (stampactive = 't' AND (expires = 't' AND stamp >= now())))";
    664   }
    665   my @lookupargs = ($args{id}, $hcheck);
    666   if ($args{update}) {
    667     $sql .= " AND record_id <> ?";
    668     push @lookupargs, $args{update};
    669   }
    670   my @t = $dbh->selectrow_array($sql, undef, @lookupargs);
    671   if ($t[0] > 0) {
    672     $errstr = "There is already a CNAME present for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
    673         ".  Only one CNAME may be present for a given name.";
    674     return;
    675665  }
    676666
Note: See TracChangeset for help on using the changeset viewer.