Changeset 190


Ignore:
Timestamp:
12/08/11 18:12:01 (13 years ago)
Author:
Kris Deugau
Message:

/trunk

Start passing user data into DNSDB.pm subs for logging internal
to DNSDB.pm
addDomain() hacked up - may be useful to only pass UID and retrieve
that on log insert in _log() later instead of passing username
and fullname from UI

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/DNSDB.pm

    r186 r190  
    487487# Log an action
    488488# Internal sub
    489 # Takes a database handle, <foo>, <bar>
     489# Takes a database handle, domain_id, user_id, group_id, email, name and log entry
    490490sub _log {
     491  my $dbh = shift;
     492  my ($domain_id,$user_id,$group_id,$username,$name,$entry) = @_;
     493
     494##fixme:  farm out the actual logging to different subs for file, syslog, internal, etc based on config
     495  $dbh->do("INSERT INTO log (domain_id,user_id,group_id,email,name,entry) VALUES (?,?,?,?,?,?)", undef,
     496        ($domain_id,$user_id,$group_id,$username,$name,$entry));
     497#            123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
     498#                     1         2         3         4         5         6         7
    491499} # end _log
    492500
     
    498506## DNSDB::addDomain()
    499507# Add a domain
    500 # Takes a database handle, domain name, numeric group, and boolean(ish) state (active/inactive)
     508# Takes a database handle, domain name, numeric group, boolean(ish) state (active/inactive),
     509# and user info hash (for logging).
    501510# Returns a status code and message
    502511sub addDomain {
     
    511520  return ('FAIL',"Need domain status") if !defined($state);
    512521
     522  my %userinfo = @_;    # remaining bits.
     523# user ID, username, user full name
     524
    513525  $state = 1 if $state =~ /^active$/;
    514526  $state = 1 if $state =~ /^on$/;
     
    518530  return ('FAIL',"Invalid domain status") if $state !~ /^\d+$/;
    519531
     532  return ('FAIL', "Invalid characters in domain") if $domain !~ /^[a-zA-Z0-9_.-]+$/;
     533
    520534  my $sth = $dbh->prepare("SELECT domain_id FROM domains WHERE domain=?");
    521535  my $dom_id;
     
    532546  local $dbh->{RaiseError} = 1;
    533547
     548my $stage = 'insert';
    534549  # Wrap all the SQL in a transaction
    535550  eval {
    536551    # insert the domain...
    537     my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
    538     $sth->execute($domain,$group,$state);
     552    $dbh->do("INSERT INTO domains (domain,group_id,status) VALUES (?,?,?)", undef, ($domain, $group, $state));
     553$stage = 'domid';
    539554
    540555    # get the ID...
    541     $sth = $dbh->prepare("select domain_id from domains where domain='$domain'");
    542     $sth->execute;
    543     ($dom_id) = $sth->fetchrow_array();
    544 
     556    ($dom_id) = $dbh->selectrow_array("SELECT domain_id FROM domains WHERE domain=?", undef, ($domain));
     557
     558$stage = 'loginsert';
     559    _log($dbh, $dom_id, $userinfo{id}, $group, $userinfo{name}, $userinfo{fullname},
     560        "Added ".($state ? 'active' : 'inactive')." domain $domain");
     561
     562$stage = 'getdefrec';
    545563    # ... and now we construct the standard records from the default set.  NB:  group should be variable.
    546     $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group");
    547     my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)".
    548         " values ($dom_id,?,?,?,?,?,?,?)");
    549     $sth->execute;
     564    my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM default_records WHERE group_id=?");
     565    my $sth_in = $dbh->prepare("INSERT INTO records (domain_id,host,type,val,distance,weight,port,ttl)".
     566        " VALUES ($dom_id,?,?,?,?,?,?,?)");
     567    $sth->execute($group);
    550568    while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) {
    551569      $host =~ s/DOMAIN/$domain/g;
    552570      $val =~ s/DOMAIN/$domain/g;
    553571      $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl);
     572      if ($typemap{$type} eq 'SOA') {
     573        my @tmp1 = split /:/, $host;
     574        my @tmp2 = split /:/, $val;
     575        _log($dbh, $dom_id, $userinfo{id}, $group, $userinfo{name}, $userinfo{fullname},
     576                "[new $domain] Added SOA record [contact $tmp1[0]] [master $tmp1[1]] ".
     577                "[refresh $tmp2[0]] [retry $tmp2[1]] [expire $tmp2[2]] [minttl $tmp2[3]], TTL $ttl");
     578      } else {
     579        my $logentry = "[new $domain] Added record '$host $typemap{$type}";
     580        $logentry .= " [distance $dist]" if $typemap{$type} eq 'MX';
     581        $logentry .= " [priority $dist] [weight $weight] [port $port]" if $typemap{$type} eq 'SRV';
     582        _log($dbh, $dom_id, $userinfo{id}, $group, $userinfo{name}, $userinfo{fullname},
     583                $logentry." $val', TTL $ttl");
     584      }
    554585    }
    555586
     
    561592    my $msg = $@;
    562593    eval { $dbh->rollback; };
    563     return ('FAIL',$msg);
     594    return ('FAIL',"$msg : $stage");
    564595  } else {
    565596    return ('OK',$dom_id);
  • trunk/dns.cgi

    r188 r190  
    343343  }
    344344
    345   my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
     345  my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0),
     346        (name => $session->param("username"), id => $session->param("uid")));
    346347
    347348  if ($code eq 'OK') {
     
    352353    $session->param('add_failed', 1);
    353354##fixme:  domain a security risk for XSS?
     355##fixme:  keep active/inactive state, group selection
    354356    changepage(page => "newdomain", domain => $webvar{domain}, errmsg => $msg);
    355357  }
Note: See TracChangeset for help on using the changeset viewer.