Changeset 273


Ignore:
Timestamp:
03/13/12 15:44:07 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

updateRec() should now handle all record update changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r272 r273  
    396396                " WHERE val = ?", undef, ($checkme)) };
    397397        return ('WARN', "PTR record for $checkme already exists;  adding another will probably not do what you want")
    398                 unless (grep /^$args{update}$/, @ptrs);
     398                if @ptrs && (!grep /^$args{update}$/, @ptrs);
    399399      } else {
    400400        # New record.  Always warn if a PTR exists
     
    436436  my $pname = ($args{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$args{id}));
    437437  ${$args{host}} =~ s/\.*$/\.$pname/ if ${$args{host}} !~ /$pname$/;
     438
     439# hmm..  this might work.  except possibly for something pointing to "deadbeef.ca".  <g>
     440#  if ($type == $reverse_typemap{NS} || $type == $reverse_typemap{MX} || $type == $reverse_typemap{SRV}) {
     441#    if ($val =~ /^\s*[\da-f:.]+\s*$/) {
     442#      return ('FAIL',"$val is not a valid IP address") if !$addr;
     443#    }
     444#  }
    438445
    439446  return ('OK','OK');
     
    563570        my @ptrs = @{ $dbh->selectcol_arrayref("SELECT record_id FROM "._rectable($args{defrec},$args{revrec}).
    564571                " WHERE val = ?", undef, (${$args{val}})) };
    565         unless (grep /^$args{update}$/, @ptrs) {
     572        if (@ptrs && (!grep /^$args{update}$/, @ptrs)) {
    566573          $msg = "PTR record for ${$args{val}} already exists;  adding another will probably not do what you want";
    567574          $code = 'WARN';
     
    23602367## DNSDB::updateRec()
    23612368# Update a record
     2369# Takes a database handle, default and reverse flags, record ID, immediate parent ID, and new record data.
     2370# Returns a status code and message
    23622371sub updateRec {
    23632372  $errstr = '';
     
    23692378  my $parid = shift;    # immediate parent entity that we're descending from to update the record
    23702379
    2371 # all records have these
     2380  # all records have these
    23722381  my $host = shift;
    23732382  my $hostbk = $$host;  # Keep a backup copy of the original, so we can WARN if the update mangles the domain
     
    23862395  my $retmsg = '';
    23872396
    2388 # do simple validation first
     2397  # do simple validation first
    23892398  return ('FAIL', "TTL must be numeric") unless $ttl =~ /^\d+$/;
    23902399
     
    23922401  # domain names technically are case-insensitive, and we use printf-like % codes for a couple
    23932402  # of types.  Other things may also be added to validate default records of several flavours.
    2394   return ('FAIL', "Hostnames may not contain anything other than (0-9 a-z . _)")
     2403  return ('FAIL', "Hostnames may not contain anything other than (0-9 a-z - . _)")
    23952404        if $defrec eq 'n' && $$host !~ /^[0-9a-z_%.-]+$/i;
    2396 ##work
    2397 
    2398 # only MX and SRV will use these
     2405
     2406  # only MX and SRV will use these
    23992407  my $dist = 0;
    24002408  my $weight = 0;
    24012409  my $port = 0;
    24022410
    2403 # standard validation
    24042411  my $fields;
    24052412  my @vallist;
    24062413
    2407 ##fixme
    2408 # get old record data so we can compare the old/new domain_id and rdns_id
    2409 my $oldrec = getRecLine($dbh, $defrec, $revrec, $id);
    2410 use Data::Dumper;
    2411 #print Dumper($oldrec);
     2414  # get old record data so we have the right parent ID
     2415  # and for logging (eventually)
     2416  my $oldrec = getRecLine($dbh, $defrec, $revrec, $id);
    24122417
    24132418  # Call the validation sub for the type requested.
     
    24222427  return ($retcode,$retmsg) if $retcode eq 'FAIL';
    24232428
    2424   # Set up database fields and bind parameters
    2425   $fields .= "host,type,val,ttl";
    2426   push @vallist, ($$host,$$rectype,$$val,$ttl);
    2427 #push @vallist,
    2428 #($defrec eq 'y' ? $oldrec->{group_id} : ($revrec eq 'n' ? $oldrec->{domain_id} : $oldrec->{rdns_id}));
    2429 
    2430   my $vallen = '?'.(',?'x$#vallist);
    2431 
    2432 #print "$fields\n";
    2433 #print join(',', @vallist)."\n";
    2434 
    2435 # hmm..  this might work.  except possibly for something pointing to "deadbeef.ca".  <g>
    2436 #  if ($type == $reverse_typemap{NS} || $type == $reverse_typemap{MX} || $type == $reverse_typemap{SRV}) {
    2437 #    if ($val =~ /^\s*[\da-f:.]+\s*$/) {
    2438 #      return ('FAIL',"$val is not a valid IP address") if !$addr;
    2439 #    }
    2440 #  }
     2429  # Set up database fields and bind parameters.  Note only the optional fields
     2430  # (distance, weight, port, secondary parent ID) are added in the validation call above
     2431  $fields .= "host,type,val,ttl,"._recparent($defrec,$revrec);
     2432  push @vallist, ($$host,$$rectype,$$val,$ttl,
     2433        ($defrec eq 'y' ? $oldrec->{group_id} : ($revrec eq 'n' ? $oldrec->{domain_id} : $oldrec->{rdns_id})) );
     2434
     2435  # hack hack PTHUI
     2436  # need to forcibly make sure we disassociate a record with a parent it's no longer related to.
     2437  # eg, PTR records may not have a domain parent, or A/AAAA records may not have a revzone parent.
     2438  # mainly needed for crossover types that got coerced down to "standard" types
     2439  if ($defrec eq 'n') {
     2440    if ($$rectype == $reverse_typemap{PTR}) {
     2441      $fields .= ",domain_id";
     2442      push @vallist, 0;
     2443    }
     2444    if ($$rectype == $reverse_typemap{A} || $$rectype == $reverse_typemap{AAAA}) {
     2445      $fields .= ",rdns_id";
     2446      push @vallist, 0;
     2447    }
     2448  }
     2449
     2450  # Fiddle the field list into something suitable for updates
     2451  $fields =~ s/,/=?,/g;
     2452  $fields .= "=?";
    24412453
    24422454  local $dbh->{AutoCommit} = 0;
     
    24442456
    24452457  eval {
    2446     $dbh->do("UPDATE ".($defrec eq 'y' ? 'default_' : '')."records ".
    2447         "SET host=?,val=?,type=?,ttl=?,distance=?,weight=?,port=? ".
    2448         "WHERE record_id=?", undef, ($$host, $$val, $$rectype, $ttl, $dist, $weight, $port, $id) );
    2449 #die "horribly";
     2458    $dbh->do("UPDATE "._rectable($defrec,$revrec)." SET $fields WHERE record_id=?", undef, (@vallist, $id) );
    24502459    $dbh->commit;
    24512460  };
     
    24572466
    24582467  return ($retcode, $retmsg);
    2459 #  return ('OK','OK');
    24602468} # end updateRec()
    24612469
Note: See TracChangeset for help on using the changeset viewer.