Changeset 311


Ignore:
Timestamp:
04/19/12 17:21:11 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Move SQL for SOA update to DNSDB.pm (see #1)
Clean up support for reverse zones (see #26)
Move action logging for SOA updates to DNSDB.pm (see #35)

Includes log/info message consistency cleanups, dropping stale
commented code, dropping if(0)'ed code, tweaks to fillsoa() to
retain user changes on errors, updates to getSOA() for consistency,
removal of a stale form input

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r309 r311  
    24942494  my $rev = shift;
    24952495  my $id = shift;
    2496   my %ret;
    24972496
    24982497  # (ab)use distance and weight columns to store SOA data?  can't for default_rev_records...
     
    25012500  my $sql = "SELECT record_id,host,val,ttl from "._rectable($def,$rev).
    25022501        " WHERE "._recparent($def,$rev)." = ? AND type=$reverse_typemap{SOA}";
    2503 
    2504   my $sth = $dbh->prepare($sql);
    2505   $sth->execute($id);
     2502  my $ret = $dbh->selectrow_hashref($sql, undef, ($id) );
     2503  return if !$ret;
    25062504##fixme:  stick a flag somewhere if the record doesn't exist.  by the API, this is an impossible case, but...
    25072505
    2508   my ($recid,$host,$val,$ttl) = $sth->fetchrow_array() or return;
    2509   my ($contact,$prins) = split /:/, $host;
    2510   my ($refresh,$retry,$expire,$minttl) = split /:/, $val;
    2511 
    2512   $ret{recid}   = $recid;
    2513   $ret{ttl}     = $ttl;
    2514 #  $ret{serial} = $serial;      # ca't use distance for serial with default_rev_records
    2515   $ret{prins}   = $prins;
    2516   $ret{contact} = $contact;
    2517   $ret{refresh} = $refresh;
    2518   $ret{retry}   = $retry;
    2519   $ret{expire}  = $expire;
    2520   $ret{minttl}  = $minttl;
    2521 
    2522   return %ret;
     2506  ($ret->{contact},$ret->{prins}) = split /:/, $ret->{host};
     2507  delete $ret->{host};
     2508  ($ret->{refresh},$ret->{retry},$ret->{expire},$ret->{minttl}) = split /:/, $ret->{val};
     2509  delete $ret->{val};
     2510
     2511  return $ret;
    25232512} # end getSOA()
    25242513
     
    25352524  my %soa = @_;
    25362525
    2537   my %oldsoa = getSOA($dbh, $defrec, $revrec, $soa{recid});
     2526  my $oldsoa = getSOA($dbh, $defrec, $revrec, $soa{id});
     2527
     2528  my $msg;
     2529  my %logdata;
     2530  if ($defrec eq 'n') {
     2531    $logdata{domain_id} = $soa{id} if $revrec eq 'n';
     2532    $logdata{rdns_id} = $soa{id} if $revrec eq 'y';
     2533    $logdata{group_id} = parentID($dbh, (id => $soa{id}, revrec => $revrec,
     2534        type => ($revrec eq 'n' ? 'domain' : 'revzone') ) );
     2535  } else {
     2536    $logdata{group_id} = $soa{id};
     2537  }
     2538  my $parname = ($defrec eq 'y' ? groupName($dbh, $soa{id}) :
     2539                ($revrec eq 'n' ? domainName($dbh, $soa{id}) : revName($dbh, $soa{id})) );
    25382540
    25392541  # Allow transactions, and raise an exception on errors so we can catch it later.
     
    25422544  local $dbh->{RaiseError} = 1;
    25432545
    2544   my $msg;
    2545 
    25462546  eval {
    2547 ##fixme: data validation: make sure {recid} is really the SOA for {parent}
    25482547    my $sql = "UPDATE "._rectable($defrec, $revrec)." SET host=?, val=?, ttl=? WHERE record_id=? AND type=6";
    25492548    $dbh->do($sql, undef, ("$soa{contact}:$soa{prins}", "$soa{refresh}:$soa{retry}:$soa{expire}:$soa{minttl}",
    2550         $soa{ttl}, $soa{recid}) );
    2551 
    2552     $msg = "Updated ".($defrec eq 'y' ? 'default ' : '')."SOA for ".
    2553         ($defrec eq 'y' ? groupName($dbh, $soa{recid}) :
    2554                 ($revrec eq 'n' ? domainName($dbh, $soa{recid}) : revName($dbh, $soa{recid}) ) ).
    2555         ": (ns $oldsoa{prins}, contact $oldsoa{contact}, refresh $oldsoa{refresh},".
    2556         " retry $oldsoa{retry}, expire $oldsoa{expire}, minTTL $oldsoa{minttl}, TTL $oldsoa{ttl}) to ".
     2549        $soa{ttl}, $oldsoa->{record_id}) );
     2550    $msg = "Updated ".($defrec eq 'y' ? ($revrec eq 'y' ? 'default reverse ' : 'default ') : '').
     2551        "SOA for $parname: ".
     2552        "(ns $oldsoa->{prins}, contact $oldsoa->{contact}, refresh $oldsoa->{refresh},".
     2553        " retry $oldsoa->{retry}, expire $oldsoa->{expire}, minTTL $oldsoa->{minttl}, TTL $oldsoa->{ttl}) to ".
    25572554        "(ns $soa{prins}, contact $soa{contact}, refresh $soa{refresh},".
    25582555        " retry $soa{retry}, expire $soa{expire}, minTTL $soa{minttl}, TTL $soa{ttl})";
    25592556
    2560 #    _log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group,
    2561 #       username => $userinfo{name}, entry => $msg) );
     2557    $logdata{entry} = $msg;
     2558    _log($dbh, %logdata);
    25622559
    25632560    $dbh->commit;
     
    25662563    $msg = $@;
    25672564    eval { $dbh->rollback; };
    2568     return ('FAIL',$msg);
     2565    $logdata{entry} = "Error updating ".($defrec eq 'y' ? ($revrec eq 'y' ? 'default reverse zone ' : 'default ') : '').
     2566        "SOA record for $parname: $msg";
     2567    if ($config{log_failures}) {
     2568      _log($dbh, %logdata);
     2569      $dbh->commit;
     2570    }
     2571    return ('FAIL', $logdata{entry});
    25692572  } else {
    25702573    return ('OK', $msg);
  • trunk/dns.cgi

    r310 r311  
    804804  if (!check_scope(id => $webvar{recid}, type =>
    805805        ($webvar{defrec} eq 'y' ? ($webvar{revrec} eq 'y' ? 'defrevrec' : 'defrec') : 'record'))) {
     806##fixme:  should we redirect to the requested record list page instead of the domain list?
    806807    changepage(page => 'domlist', errmsg => "You do not have permission to edit the requested SOA record");
    807808  }
     
    809810  if (!check_scope(id => $webvar{id}, type =>
    810811        ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'revzone' : 'domain')))) {
    811     changepage(page => 'domlist', errmsg => "You do not have permission to edit the ".
     812    changepage(page => ($webvar{revrec} eq 'y' ? 'revzones' : 'domlist'),
     813        errmsg => "You do not have permission to edit the ".
    812814        ($webvar{defrec} eq 'y' ? 'default ' : '')."SOA record for the requested ".
    813         ($webvar{defrec} eq 'y' ? 'group' : 'domain'));
     815        ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'reverse zone' : 'domain')) );
    814816  }
    815817
     
    817819        unless ($permissions{admin} || $permissions{domain_edit});
    818820
    819   # get old SOA for log
    820   my %soa = getSOA($dbh,$webvar{defrec},$webvar{revrec},$webvar{id});
    821 
    822 #  my $sth;
    823 ###fixme:  push SQL into DNSDB.pm
    824 ###fixme: data validation: make sure {recid} is really the SOA for {id}
    825 #  # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
    826 #  # plus a bit of magic to update the appropriate table
    827 #  my $sql = "UPDATE ".($webvar{defrec} eq 'y' ? "default_records" : "records").
    828 #       " SET host=?, val=?, ttl=? WHERE record_id=?";
    829 #  $sth = $dbh->prepare($sql);
    830 #  $sth->execute("$webvar{contact}:$webvar{prins}",
    831 #       "$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}",
    832 #       $webvar{ttl},
    833 #       $webvar{recid});
    834 
    835821  my ($code, $msg) = updateSOA($dbh, $webvar{defrec}, $webvar{revrec},
    836822        (contact => $webvar{contact}, prins => $webvar{prins}, refresh => $webvar{refresh},
    837         retry => $webvar{retry}, expire => $webvar{expire}, minttl => $soa{minttl},
    838         ttl => $webvar{ttl}, recid => $webvar{recid}) );
     823        retry => $webvar{retry}, expire => $webvar{expire}, minttl => $webvar{minttl},
     824        ttl => $webvar{ttl}, id => $webvar{id}) );
    839825  if ($code eq 'OK') {
    840826    changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec}, revrec => $webvar{revrec},
     
    842828  } else {
    843829    $page->param(update_failed => 1);
    844     $page->param(msg => $DNSDB::errstr);
    845     fillsoa($webvar{defrec},$webvar{revrec},$webvar{id});
    846   }
    847 
    848 ##fixme: faillog
    849 #  } else {
    850 if (0) {
    851     # do this in the order of "default to most common case"
    852     my $loggroup;
    853     my $logdomain = $webvar{id};
    854     if ($webvar{defrec} eq 'y') {
    855       $loggroup = $webvar{id};
    856       $logdomain = 0;
    857     } else {
    858       $loggroup = parentID($dbh, (id => $logdomain, type => 'domain', revrec => $webvar{revrec}));
    859     }
    860 
    861     logaction($logdomain, $session->param("username"), $loggroup,
    862         "Updated ".($webvar{defrec} eq 'y' ? 'default ' : '')."SOA for ".
    863         ($webvar{defrec} eq 'y' ? groupName($dbh, $webvar{id}) : domainName($dbh, $webvar{id}) ).
    864         ": (ns $soa{prins}, contact $soa{contact}, refresh $soa{refresh},".
    865         " retry $soa{retry}, expire $soa{expire}, minTTL $soa{minttl}, TTL $soa{ttl}) to ".
    866         "(ns $webvar{prins}, contact $webvar{contact}, refresh $webvar{refresh},".
    867         " retry $webvar{retry}, expire $webvar{expire}, minTTL $webvar{minttl}, TTL $webvar{ttl})");
    868   } # if (0)
     830    $page->param(msg => $msg);
     831    fillsoa($webvar{defrec}, $webvar{revrec}, $webvar{id}, 'w');
     832  }
    869833
    870834} elsif ($webvar{page} eq 'grpman') {
     
    17231687  my $revrec = shift;
    17241688  my $id = shift;
     1689  my $preserve = shift || 'd';  # Flag to use webvar fields or retrieve from database
     1690
    17251691  my $domname = ($defrec eq 'y' ? '' : "DOMAIN");
    17261692
     
    17441710  $page->param(defminttl        => $DNSDB::def{minttl});
    17451711
    1746   # there are probably better ways to do this.  TMTOWTDI.
    1747   my %soa = getSOA($dbh,$defrec,$revrec,$id);
    1748 
    17491712  $page->param(id       => $id);
    1750   $page->param(recid    => $soa{recid});
    1751   $page->param(prins    => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
    1752   $page->param(contact  => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
    1753   $page->param(refresh  => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
    1754   $page->param(retry    => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
    1755   $page->param(expire   => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
    1756   $page->param(minttl   => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
    1757   $page->param(ttl      => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
     1713
     1714  if ($preserve eq 'd') {
     1715    # there are probably better ways to do this.  TMTOWTDI.
     1716    my $soa = getSOA($dbh,$defrec,$revrec,$id);
     1717
     1718    $page->param(prins  => ($soa->{prins} ? $soa->{prins} : $DNSDB::def{prins}));
     1719    $page->param(contact        => ($soa->{contact} ? $soa->{contact} : $DNSDB::def{contact}));
     1720    $page->param(refresh        => ($soa->{refresh} ? $soa->{refresh} : $DNSDB::def{refresh}));
     1721    $page->param(retry  => ($soa->{retry} ? $soa->{retry} : $DNSDB::def{retry}));
     1722    $page->param(expire => ($soa->{expire} ? $soa->{expire} : $DNSDB::def{expire}));
     1723    $page->param(minttl => ($soa->{minttl} ? $soa->{minttl} : $DNSDB::def{minttl}));
     1724    $page->param(ttl    => ($soa->{ttl} ? $soa->{ttl} : $DNSDB::def{soattl}));
     1725  } else {
     1726    $page->param(prins  => ($webvar{prins} ? $webvar{prins} : $DNSDB::def{prins}));
     1727    $page->param(contact        => ($webvar{contact} ? $webvar{contact} : $DNSDB::def{contact}));
     1728    $page->param(refresh        => ($webvar{refresh} ? $webvar{refresh} : $DNSDB::def{refresh}));
     1729    $page->param(retry  => ($webvar{retry} ? $webvar{retry} : $DNSDB::def{retry}));
     1730    $page->param(expire => ($webvar{expire} ? $webvar{expire} : $DNSDB::def{expire}));
     1731    $page->param(minttl => ($webvar{minttl} ? $webvar{minttl} : $DNSDB::def{minttl}));
     1732    $page->param(ttl    => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{soattl}));
     1733  }
    17581734}
    17591735
     
    17641740
    17651741  # get the SOA first
    1766   my %soa = getSOA($dbh,$def,$rev,$id);
    1767 
    1768   $page->param(contact  => $soa{contact});
    1769   $page->param(prins    => $soa{prins});
    1770   $page->param(refresh  => $soa{refresh});
    1771   $page->param(retry    => $soa{retry});
    1772   $page->param(expire   => $soa{expire});
    1773   $page->param(minttl   => $soa{minttl});
    1774   $page->param(ttl      => $soa{ttl});
     1742  my $soa = getSOA($dbh,$def,$rev,$id);
     1743
     1744  $page->param(contact  => $soa->{contact});
     1745  $page->param(prins    => $soa->{prins});
     1746  $page->param(refresh  => $soa->{refresh});
     1747  $page->param(retry    => $soa->{retry});
     1748  $page->param(expire   => $soa->{expire});
     1749  $page->param(minttl   => $soa->{minttl});
     1750  $page->param(ttl      => $soa->{ttl});
    17751751
    17761752  my $foo2 = getDomRecs($dbh,$def,$rev,$id,$perpage,$webvar{offset},$sortby,$sortorder,$filter);
     
    18201796  }
    18211797# retrieve the right ttl instead of falling (way) back to the hardcoded system default
    1822   my %soa = getSOA($dbh,$webvar{defrec},$webvar{revrec},$webvar{parentid});
    1823   $page->param(ttl      => ($webvar{ttl} ? $webvar{ttl} : $soa{minttl}));
     1798  my $soa = getSOA($dbh,$webvar{defrec},$webvar{revrec},$webvar{parentid});
     1799  $page->param(ttl      => ($webvar{ttl} ? $webvar{ttl} : $soa->{minttl}));
    18241800}
    18251801
  • trunk/templates/editsoa.tmpl

    r277 r311  
    1616<input type="hidden" name="page" value="updatesoa" />
    1717<input type="hidden" name="id" value="<TMPL_VAR NAME=id>" />
    18 <input type="hidden" name="recid" value="<TMPL_VAR NAME=recid>" />
    1918<input type="hidden" name="defrec" value="<TMPL_VAR NAME=defrec>" />
    2019<input type="hidden" name="revrec" value="<TMPL_VAR NAME=revrec>" />
Note: See TracChangeset for help on using the changeset viewer.