Changeset 254 for trunk


Ignore:
Timestamp:
02/29/12 17:38:46 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Update parentID() (and all calls to it) for reverse records,
move to DNSDB.pm. See #26.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r252 r254  
    3636        &addRec &updateRec &delRec
    3737        &getTypelist
    38         &getParents
     38        &parentID
    3939        &isParent
    4040        &domStatus &importAXFR
     
    5858                &addRec &updateRec &delRec
    5959                &getTypelist
    60                 &getParents
     60                &parentID
    6161                &isParent
    6262                &domStatus &importAXFR
     
    22352235
    22362236
    2237 ## DNSDB::getParents()
    2238 # Find out which entities are parent to the requested id
    2239 # Returns arrayref containing hash pairs of id/type
    2240 sub getParents {
    2241   my $dbh = shift;
    2242   my $id = shift;
    2243   my $type = shift;
    2244   my $depth = shift || 'all';   # valid values:  'all', 'immed', <int> (stop at this group ID)
    2245 
    2246   my @parlist;
    2247 
    2248   while (1) {
    2249     my $result = $dbh->selectrow_hashref("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
    2250         undef, ($id) );
    2251     my %tmp = ($result->{$par_col{$type}} => $par_type{$type});
    2252     unshift @parlist, \%tmp;
    2253     last if $result->{$par_col{$type}} == 1;    # group 1 is its own parent
    2254     $id = $result->{$par_col{$type}};
    2255     $type = $par_type{$type};
    2256   }
    2257 
    2258   return \@parlist;
    2259 
    2260 } # end getParents()
     2237## DNSDB::parentID()
     2238# Get ID of entity that is nearest parent to requested id
     2239# Takes a database handle and a hash of entity ID, entity type, optional parent type flag
     2240# (domain/reverse zone or group), and optional default/live and forward/reverse flags
     2241# Returns the ID or undef on failure
     2242sub parentID {
     2243  my $dbh = shift;
     2244
     2245  my %args = @_;
     2246
     2247  # clean up the parent-type.  Set it to group if not set;  coerce revzone to domain for simpler logic
     2248  $args{partype} = 'group' if !$args{partype};
     2249  $args{partype} = 'domain' if $args{partype} eq 'revzone';
     2250
     2251  # clean up defrec and revrec.  default to live record, forward zone
     2252  $args{defrec} = 'n' if !$args{defrec};
     2253  $args{revrec} = 'n' if !$args{revrec};
     2254
     2255  if ($par_type{$args{partype}} eq 'domain') {
     2256    # only live records can have a domain/zone parent
     2257    return unless ($args{type} eq 'record' && $args{defrec} eq 'n');
     2258    my $result = $dbh->selectrow_hashref("SELECT ".($args{revrec} eq 'n' ? 'domain_id' : 'rdns_id').
     2259        " FROM records WHERE record_id = ?",
     2260        undef, ($args{id}) ) or return;
     2261    return $result;
     2262  } else {
     2263    # snag some arguments that will either fall through or be overwritten to save some code duplication
     2264    my $tmpid = $args{id};
     2265    my $type = $args{type};
     2266    if ($type eq 'record' && $args{defrec} eq 'n') {
     2267      # Live records go through the records table first.
     2268      ($tmpid) = $dbh->selectrow_array("SELECT ".($args{revrec} eq 'n' ? 'domain_id' : 'rdns_id').
     2269        " FROM records WHERE record_id = ?",
     2270        undef, ($args{id}) ) or return;
     2271      $type = ($args{revrec} eq 'n' ? 'domain' : 'revzone');
     2272    }
     2273    my ($result) = $dbh->selectrow_array("SELECT $par_col{$type} FROM $par_tbl{$type} WHERE $id_col{$type} = ?",
     2274        undef, ($tmpid) );
     2275    return $result;
     2276  }
     2277# should be impossible to get here with even remotely sane arguments
     2278  return;
     2279} # end parentID()
    22612280
    22622281
  • trunk/dns.cgi

    r253 r254  
    314314      my $stat = domStatus($dbh,$webvar{id},$webvar{domstatus});
    315315##fixme  switch to more consise "Enabled <domain"/"Disabled <domain>" as with users?
    316       logaction($webvar{id}, $session->param("username"), parentID($webvar{id}, 'dom', 'group'),
     316      logaction($webvar{id}, $session->param("username"),
     317        parentID($dbh, (id => $webvar{id}, type => 'domain', revrec => $webvar{revrec})),
    317318        "Changed ".domainName($dbh, $webvar{id})." state to ".($stat ? 'active' : 'inactive'));
    318319      $page->param(resultmsg => "Changed ".domainName($dbh, $webvar{id})." state to ".
     
    403404
    404405  } elsif ($webvar{del} eq 'ok') {
    405     my $pargroup = parentID($webvar{id}, 'dom', 'group');
     406    my $pargroup = parentID($dbh, (id => $webvar{id}, type => 'domain', revrec => $webvar{revrec}));
    406407    my $dom = domainName($dbh, $webvar{id});
    407408    my ($code,$msg) = delDomain($dbh, $webvar{id});
     
    587588                if $typemap{$webvar{type}} eq 'SRV';
    588589        $restr .= " $webvar{address}', TTL $webvar{ttl}";
    589         logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'), $restr);
     590        logaction($webvar{parentid}, $session->param("username"),
     591                parentID($dbh, (id => $webvar{parentid}, type => 'domain', revrec => $webvar{revrec})), $restr);
    590592      }
    591593      my %pageparams = (page => "reclist", id => $webvar{parentid},
     
    608610                "Failed adding default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl} ($msg)");
    609611        } else {
    610           logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
     612          logaction($webvar{parentid}, $session->param("username"),
     613                parentID($dbh, (id => $webvar{parentid}, type => 'domain', revrec => $webvar{revrec})),
    611614                "Failed adding record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl} ($msg)");
    612615        }
     
    658661        my $restr = "Updated record from '$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}', TTL $oldrec->{ttl}\n".
    659662                "to '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}";
    660         logaction($webvar{parentid}, $session->param("username"), parentID($webvar{id}, 'rec', 'group'), $restr);
     663        logaction($webvar{parentid}, $session->param("username"),
     664                parentID($dbh, (id => $webvar{id}, type => 'record', defrec => $webvar{defrec},
     665                        revrec => $webvar{revrec}, partype => 'group')),
     666                $restr);
    661667        changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
    662668      }
     
    675681                "Failed updating default record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl} ($msg)");
    676682        } else {
    677           logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
     683          logaction($webvar{parentid}, $session->param("username"),
     684                parentID($dbh, (id => $webvar{parentid}, type => 'domain', revrec => $webvar{revrec})),
    678685                "Failed updating record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl} ($msg)");
    679686        }
     
    735742        my $recclass = ($webvar{revrec} eq 'n' ? 'record' : 'reverse record');
    736743        my $restr = "Deleted $recclass '$rec->{host} $typemap{$rec->{type}} $rec->{val}', TTL $rec->{ttl}";
    737         logaction($rec->{parid}, $session->param("username"), parentID($rec->{parid}, 'dom', 'group'), $restr);
     744        logaction($rec->{parid}, $session->param("username"),
     745                parentID($dbh, (id => $rec->{parid}, type => 'domain', revrec => $webvar{revrec})),
     746                $restr);
    738747        changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec},
    739748                revrec => $webvar{revrec}, resultmsg => $restr);
     
    747756                " TTL $rec->{ttl} ($msg)");
    748757        } else {
    749           logaction($rec->{parid}, $session->param("username"), parentID($rec->{parid}, 'dom', 'group'),
     758          logaction($rec->{parid}, $session->param("username"),
     759                parentID($dbh, (id => $rec->{parid}, type => 'domain', revrec => $webvar{revrec})),
    750760                "Failed deleting record '$rec->{host} $typemap{$rec->{type}} $rec->{val}', TTL $rec->{ttl} ($msg)");
    751761        }
     
    828838      $logdomain = 0;
    829839    } else {
    830       $loggroup = parentID($logdomain, 'dom', 'group', $webvar{defrec});
     840      $loggroup = parentID($dbh, (id => $logdomain, type => 'domain', revrec => $webvar{revrec}));
    831841    }
    832842
     
    936946  } elsif ($webvar{del} eq 'ok') {
    937947    my $deleteme = groupName($dbh,$webvar{id}); # get this before we delete it...
    938     my $delparent = parentID($webvar{id}, 'group','group');
     948    my $delparent = parentID($dbh, (id => $webvar{id}, type => 'group'));
    939949    my ($code,$msg) = delGroup($dbh, $webvar{id});
    940950    if ($code eq 'OK') {
     
    10791089      my ($code, $msg) = changeGroup($dbh, 'domain', $webvar{$_}, $webvar{destgroup});
    10801090      if ($code eq 'OK') {
    1081         logaction($webvar{$_}, $session->param("username"), parentID($webvar{$_}, 'dom', 'group'),
     1091        logaction($webvar{$_}, $session->param("username"),
     1092                parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec})),
    10821093                "Moved domain ".domainName($dbh, $webvar{$_})." to group $newgname");
    10831094        $row{domok} = ($code eq 'OK');
    10841095      } else {
    1085         logaction($webvar{$_}, $session->param("username"), parentID($webvar{$_}, 'dom', 'group'),
     1096        logaction($webvar{$_}, $session->param("username"),
     1097                parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec})),
    10861098                "Failed to move domain ".domainName($dbh, $webvar{$_})." to group $newgname: $msg")
    10871099                if $config{log_failures};
     
    11101122##fixme:  error handling on status change
    11111123      my $stat = domStatus($dbh,$webvar{$_},($webvar{bulkaction} eq 'activate' ? 'domon' : 'domoff'));
    1112       logaction($webvar{$_}, $session->param("username"), parentID($webvar{$_}, 'dom', 'group'),
    1113                 "Changed domain ".domainName($dbh, $webvar{$_})." state to ".($stat ? 'active' : 'inactive'));
     1124      logaction($webvar{$_}, $session->param("username"),
     1125        parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec})),
     1126        "Changed domain ".domainName($dbh, $webvar{$_})." state to ".($stat ? 'active' : 'inactive'));
    11141127      $row{domok} = 1;
    11151128#      $row{domok} = ($code eq 'OK');
     
    11351148      }
    11361149      $row{domain} = domainName($dbh,$webvar{$_});
    1137       my $pargroup = parentID($webvar{$_}, 'dom', 'group');
     1150      my $pargroup = parentID($dbh, (id => $webvar{$_}, type => 'domain', revrec => $webvar{revrec}));
    11381151      my $dom = domainName($dbh, $webvar{$_});
    11391152      my ($code, $msg) = delDomain($dbh, $webvar{$_});
     
    11681181    if ($flag && ($permissions{admin} || $permissions{user_edit})) {
    11691182      my $stat = userStatus($dbh,$webvar{id},$webvar{userstatus});
    1170       logaction(0, $session->param("username"), parentID($webvar{id}, 'user', 'group'),
     1183      logaction(0, $session->param("username"), parentID($dbh, (id => $webvar{id}, type => 'user')),
    11711184        ($stat ? 'Enabled' : 'Disabled')." ".userFullName($dbh, $webvar{id}, '%u'));
    11721185      $page->param(resultmsg => ($stat ? 'Enabled' : 'Disabled')." ".userFullName($dbh, $webvar{id}, '%u'));
     
    22552268
    22562269
    2257 ##fixme:  generalize to return appropriate id on all cases (ie, use $partype)
    2258 sub parentID {
    2259   my $id = shift;
    2260   my $idtype = shift;
    2261   my $partype = shift;
    2262   my $defrec = shift || '';
    2263 
    2264   my $sql = '';
    2265 
    2266   if ($idtype eq 'dom') {
    2267     return $id if $defrec eq 'y';  # "domain" + default records, we're really looking at a group.
    2268     $sql = "SELECT group_id FROM domains WHERE domain_id=?";
    2269   } elsif ($idtype eq 'rec') {
    2270     if ($defrec eq 'y') {
    2271       $sql = "SELECT group_id FROM default_records WHERE record_id=?";
    2272     } else {
    2273       $sql = "SELECT d.group_id FROM domains d".
    2274         " INNER JOIN records r ON d.domain_id=r.domain_id".
    2275         " WHERE r.record_id=?";
    2276     }
    2277   } elsif ($idtype eq 'group') {
    2278     $sql = "SELECT parent_group_id FROM groups WHERE group_id=?";
    2279   } elsif ($idtype eq 'user') {
    2280     $sql = "SELECT group_id FROM users WHERE user_id=?";
    2281   } else {
    2282     return "FOO", "BAR";  # can't get here.... we think.
    2283   }
    2284   my $sth = $dbh->prepare($sql);
    2285   $sth->execute($id);
    2286   my ($retid) = $sth->fetchrow_array;
    2287   return $retid if $retid;
    2288   # ahh! fall of the edge of the world if things went sideways
    2289   ##fixme:  really need to do a little more error handling, I think
    2290 } # end parentID()
    2291 
    2292 
    22932270# we have to do this in a variety of places;  let's make it consistent
    22942271sub fill_permissions {
Note: See TracChangeset for help on using the changeset viewer.