Changeset 24 for trunk/DNSDB.pm


Ignore:
Timestamp:
10/30/09 17:52:55 (15 years ago)
Author:
Kris Deugau
Message:

/trunk

checkpoint

  • group management more or less functional
  • user management partially functional
  • misc minor tweaks and normalizations
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r23 r24  
    2626        &addDomain &delDomain &domainName
    2727        &addGroup &delGroup &getChildren &groupName
     28        &addUser &delUser
    2829        &getSOA &getRecLine &getDomRecs
    2930        &addRec &updateRec &delRec
     
    3738                &addDomain &delDomain &domainName
    3839                &addGroup &delGroup &getChildren &groupName
     40                &addUser &delUser
    3941                &getSOA &getRecLine &getDomRecs
    4042                &addRec &updateRec &delRec
     
    379381## DNSDB::getChildren()
    380382# Get a list of all groups whose parent^n is group <n>
    381 # Takes a database handle, group ID, and reference to an array to put the group IDs in
     383# Takes a database handle, group ID, reference to an array to put the group IDs in,
     384# and an optional flag to return only immediate children or all children-of-children
     385# default to returning all children
    382386# Calls itself
    383387sub getChildren {
     
    386390  my $rootgroup = shift;
    387391  my $groupdest = shift;
     392  my $immed = shift || 'all';
    388393
    389394  # special break for default group;  otherwise we get stuck.
    390395  if ($rootgroup == 1) {
    391396    # by definition, group 1 is the Root Of All Groups
    392     my $sth = $dbh->prepare("SELECT group_id FROM groups");
     397    my $sth = $dbh->prepare("SELECT group_id FROM groups WHERE NOT (group_id=1)".
     398        ($immed ne 'all' ? " AND parent_group_id=1" : ''));
    393399    $sth->execute;
    394     my @grouplist;
    395400    while (my @this = $sth->fetchrow_array) {
    396401      push @$groupdest, @this;
     
    403408    while (my ($group) = $sth->fetchrow_array) {
    404409      push @$groupdest, $group;
    405       getChildren($dbh,$group,$groupdest);
     410      getChildren($dbh,$group,$groupdest) if $immed eq 'all';
    406411    }
    407412  }
     
    423428  return $groupname if $groupname;
    424429} # end groupName
     430
     431
     432## DNSDB::addUser()
     433#
     434sub addUser {
     435  $errstr = '';
     436  my $dbh = shift;
     437  return ('FAIL',"Need database handle") if !$dbh;
     438  my $username = shift;
     439  return ('FAIL',"Missing username") if !defined($username);
     440  my $group = shift;
     441  return ('FAIL',"Missing group") if !defined($group);
     442  my $pass = shift;
     443  return ('FAIL',"Missing password") if !defined($pass);
     444
     445  my $state = shift;
     446  return ('FAIL',"Need account status") if !defined($state);
     447  my $fname = shift || '';
     448  my $lname = shift || '';
     449
     450  my $user_id;
     451
     452  # Allow transactions, and raise an exception on errors so we can catch it later.
     453  # Use local to make sure these get "reset" properly on exiting this block
     454  local $dbh->{AutoCommit} = 0;
     455  local $dbh->{RaiseError} = 1;
     456
     457  # Wrap all the SQL in a transaction
     458  eval {
     459    # insert the user...
     460    my $sth = $dbh->prepare("INSERT INTO users (email,group_id,password,status) VALUES (?,?,?,?)");
     461    $sth->execute($username,$group,$pass,$state);
     462die "user fail\n";
     463
     464    # get the ID...
     465    $sth = $dbh->prepare("select user_id from users where username=?");
     466    $sth->execute($username);
     467    ($user_id) = $sth->fetchrow_array();
     468
     469    # once we get here, we should have suceeded.
     470    $dbh->commit;
     471  }; # end eval
     472
     473  if ($@) {
     474    my $msg = $@;
     475    eval { $dbh->rollback; };
     476    return ('FAIL',$msg);
     477  } else {
     478    return ('OK',$user_id);
     479  }
     480} # end addUser
     481
     482
     483## DNSDB::delUser()
     484#
     485sub delUser {
     486} # end delUser
    425487
    426488
     
    574636
    575637  my $fields = ($defrec eq 'y' ? 'group_id' : 'domain_id').",host,type,val,ttl";
    576   my $vallist = "$id,'$host',$rectype,'$val',$ttl";
     638  my $vallen = "?,?,?,?,?";
     639  my @vallist = ($id,$host,$rectype,$val,$ttl);
    577640
    578641  my $dist;
     
    581644    return ('FAIL',"Need distance for $typemap{$rectype} record") if !defined($dist);
    582645    $fields .= ",distance";
    583     $vallist .= ",$dist";
     646    $vallen .= ",?";
     647    push @vallist, $dist;
    584648  }
    585649  my $weight;
    586650  my $port;
    587651  if ($rectype == $reverse_typemap{SRV}) {
     652    # check for _service._protocol.  NB:  RFC2782 does not say "MUST"...  nor "SHOULD"...
     653    # it just says (paraphrased) "... is prepended with _ to prevent DNS collisions"
     654    return ('FAIL',"SRV records must begin with _service._protocol")
     655        if $host !~ /^_[A-Za-z]+\._[A-Za-z]+\.[a-z0-9-]+/;
    588656    $weight = shift;
    589657    $port = shift;
    590658    return ('FAIL',"Need weight and port for SRV record") if !defined($weight) or !defined($port);
    591659    $fields .= ",weight,port";
    592     $vallist .= ",$weight,$port";
    593   }
    594 
    595   my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)";
     660    $vallen .= ",?,?";
     661    push @vallist, ($weight,$port);
     662  }
     663
     664  my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallen)";
     665##fixme: use array for values, replace "vallist" with series of ?,?,? etc
    596666# something is bugging me about this...
    597667#warn "DEBUG: $sql";
    598668  my $sth = $dbh->prepare($sql);
    599   $sth->execute;
     669  $sth->execute(@vallist);
    600670
    601671  return ('FAIL',$sth->errstr) if $sth->err;
Note: See TracChangeset for help on using the changeset viewer.