Index: /branches/cname-collision/DNSDB.pm
===================================================================
--- /branches/cname-collision/DNSDB.pm	(revision 980)
+++ /branches/cname-collision/DNSDB.pm	(revision 981)
@@ -655,11 +655,10 @@
     if ($t[0] > 0) {
       if ($tcompare eq '<>') {
-        $errstr = "One or more non-CNAME records already exist for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
-		".  CNAME records cannot use the same name as other records.";
+        return ('FAIL', "One or more non-CNAME records already exist for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
+		".  CNAME records cannot use the same name as other records.");
       } else {
-        $errstr = "There is already a CNAME present for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
-		".  Only one CNAME may be present for a given name.";
+        return ('FAIL', "There is already a CNAME present for ".($args{revrec} eq 'y' ? $arpaname : $hcheck).
+		".  Only one CNAME may be present for a given name.");
       }
-      return;
     }
   }
@@ -673,5 +672,5 @@
 #	if $t[0] > 1;
 #  }
-  return 1;
+  return ('OK', 'OK');
 
 } # _cname_collision()
@@ -752,6 +751,4 @@
   }
 
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
-
   return ('OK','OK');
 } # done A record
@@ -806,6 +803,4 @@
     }    
   }
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ('OK','OK');
@@ -888,6 +883,4 @@
     } # $zname !~ .rpz
   } # revzone eq 'n'
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ('OK','OK');
@@ -1017,6 +1010,4 @@
   }
 
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
-
   return ('WARN',$warnflag) if $warnflag;
 
@@ -1074,6 +1065,4 @@
 
   }
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ('OK','OK');
@@ -1107,6 +1096,4 @@
   }
 
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
-
   # Could arguably put a WARN return here on very long (>512) records
   return ('OK','OK');
@@ -1181,6 +1168,4 @@
     ${$args{val}} = $args{addr}->addr;
   }
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ('OK','OK');
@@ -1265,6 +1250,4 @@
 
   }
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ('OK','OK');
@@ -1371,6 +1354,4 @@
 #  } else {
 #  }
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ($code, $msg);
@@ -1656,7 +1637,4 @@
   }
 
-  # poor practice creating template records could result in a live CNAME collision
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
-
   return ('OK','OK');
 } # done A+PTR template record
@@ -1707,6 +1685,4 @@
   }
 
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
-
   return ('OK','OK');
 } # done delegation record
@@ -1760,6 +1736,4 @@
     push @{$args{vallist}}, $liveips;
   }
-
-  return ('FAIL', $errstr) unless $self->_cname_collision(%args);
 
   return ('WARN', join("\n", $errstr, $warnmsg) ) if $warnmsg;
@@ -4917,4 +4891,24 @@
   return ($retcode,$retmsg) if $retcode eq 'FAIL';
 
+  # Check for CNAME collisions.
+##fixme:  should trim the list of arguments, not all of these should be required for CNAME collision checking
+  my ($ccode,$cmsg) = _cname_collision($self, defrec => $defrec, revrec => $revrec, id => $id,
+	host => $host, rectype => $rectype, val => $val, addr => $addr,
+	dist => \$dist, port => \$port, weight => \$weight,
+	stamp => \$stamp, expires => \$expires,
+	fields => \$fields, vallist => \@vallist);
+
+  return ($ccode,$cmsg) if $ccode eq 'FAIL';
+
+  # If both the validator and CNAME collision check return warnings, glue them together
+  if ($ccode eq 'WARN') {
+    if ($retcode eq 'WARN') {
+      $retmsg .= "<br>\n$cmsg";
+    } else {
+      $retmsg = $cmsg;
+    }
+    $retcode = 'WARN';
+  }
+
   # Set up database fields and bind parameters
   $fields .= "host,type,val,ttl,"._recparent($defrec,$revrec);
@@ -5086,4 +5080,26 @@
 
   return ($retcode,$retmsg) if $retcode eq 'FAIL';
+
+  # Check for CNAME collisions.
+##fixme:  should trim the list of arguments, not all of these should be required for CNAME collision checking
+  my ($ccode,$cmsg) = _cname_collision($self, defrec => $defrec, revrec => $revrec,
+	id => ($defrec eq 'y' ? $oldrec->{group_id} : ($revrec eq 'n' ? $oldrec->{domain_id} : $oldrec->{rdns_id})),
+	host => $host, rectype => $rectype, val => $val, addr => $addr,
+	dist => \$dist, port => \$port, weight => \$weight,
+	stamp => \$stamp, expires => \$expires,
+	fields => \$fields, vallist => \@vallist,
+	update => $id);
+
+  return ($ccode,$cmsg) if $ccode eq 'FAIL';
+
+  # If both the validator and CNAME collision check return warnings, glue them together
+  if ($ccode eq 'WARN') {
+    if ($retcode eq 'WARN') {
+      $retmsg .= "<br>\n$cmsg";
+    } else {
+      $retmsg = $cmsg;
+    }
+    $retcode = 'WARN';
+  }
 
   # Set up database fields and bind parameters.  Note only the optional fields
