#!/usr/bin/perl -w -T # dns/cgi-bin/dns.cgi ### # SVN revision info # $Date: 2009-09-15 21:52:13 +0000 (Tue, 15 Sep 2009) $ # SVN revision $Rev: 16 $ # Last update by $Author: kdeugau $ ### # Copyright (C) 2008,2009 - Kris Deugau use strict; use warnings; use CGI::Carp qw (fatalsToBrowser); use CGI::Simple; use HTML::Template; use CGI::Session; use DBI; use lib '.'; # custom modules use DNSDB qw(:ALL); my @debugbits; # temp, to be spit out near the end of processing # Let's do these templates right... my $templatedir = "templates"; my $sessiondir = "session"; # Set up the CGI object... my $q = new CGI::Simple; # ... and get query-string params as well as POST params if necessary $q->parse_query_string; # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about... my %webvar = $q->Vars; # persistent stuff needed on most/all pages my $sid = ($webvar{sid} ? $webvar{sid} : undef); my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir}); #$sid = $session->id() if !$sid; if (!$sid) { # init stuff. can probably axe this down to just above if'n'when user manipulation happens $sid = $session->id(); # need to know the "upper" group the user can deal with; may as well # stick this in the session rather than calling out to the DB every time. $session->param('logingroupid',1); $session->param('workinggroupid',1); # yes, we *do* need to track this too. er, probably. } my $group = ($webvar{grp} ? $webvar{grp} : 1); # handle login redirect if ($webvar{action} && $webvar{action} eq 'login') { ##fixme: need to actually do a user/pass check changepage(page => "domlist"); } my $header = HTML::Template->new(filename => "$templatedir/header.tmpl"); my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl"); # default #my $perpage = 15; my $perpage = 3; my $offset = ($webvar{offset} ? $webvar{offset} : 0); # NB: these must match the field name and SQL ascend/descend syntax respectively my $sortfield = "domains"; my $sortorder = "asc"; my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret"); #my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret", # { AutoCommit => 0 }) or die $DBI::errstr; ##fixme. PLEASE! print $msg if !$dbh; # fiddle hardcoded "defaults" as per system/user (?) prefs initGlobals($dbh); ## Default page is a login page #my $page; # to be initialized as an HTML::Template entity sooner or later # decide which page to spit out... $webvar{page} = 'login' if !$webvar{page}; #if (!$webvar{page}) { # $page = HTML::Template->new(filename => "$templatedir/login.tmpl"); #} else { #} my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl"); $page->param(sid => $sid); if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') { # hmm. seeing problems in some possibly-not-so-corner cases. # this currently only handles "domain on", "domain off" if (defined($webvar{action})) { domStatus($dbh,$webvar{id},$webvar{action}); } listdomains(); } elsif ($webvar{page} eq 'reclist') { # Handle record list for both default records (per-group) and live domain records $page->param(defrec => $webvar{defrec}); $page->param(id => $webvar{id}); $page->param(curpage => 'reclist'); # select count(*) from (default_)?records where (group|domain)_id=? my $sth = $dbh->prepare("SELECT count(*) FROM ". ($webvar{defrec} eq 'y' ? 'default_' : '')."records ". "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ". "AND NOT type=$reverse_typemap{SOA}"); $sth->execute($webvar{id}); my ($count) = ($sth->fetchrow_array); # fill the page-count and first-previous-next-last-all details fill_pgcount($count,"records",domainName($dbh,$webvar{id})); fill_fpnla($count); # should put some params on this sub... $page->param(defrec => $webvar{defrec}); if ($webvar{defrec} eq 'y') { ##fixme: hardcoded group showdomain('y',1); } else { showdomain('n',$webvar{id}); } } elsif ($webvar{page} eq 'newdomain') { # weesa gonna discard parent_group_id for now my $sth = $dbh->prepare("select group_id,parent_group_id,name from groups order by group_id"); $sth->execute; my @grplist; while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) { my %row; $row{grpname} = $grpname; $row{grpval} = $grpid; ##fixme: need magic # $row{defgrp} = ''; push @grplist, \%row; } $page->param(grplist => \@grplist); } elsif ($webvar{page} eq 'deldom') { $page->param(id => $webvar{id}); # first pass = confirm y/n (sorta) if (!defined($webvar{del})) { $page->param(del_getconf => 1); $page->param(domain => domainName($dbh,$webvar{id})); # print some neato things? # } else { # #whether actually deleting or cancelling we redirect to the domain list, default format } elsif ($webvar{del} eq 'ok') { my ($code,$msg) = delDomain($dbh, $webvar{id}); if ($code ne 'OK') { # need to find failure mode $page->param(del_failed => 1); $page->param(errmsg => $msg); } else { # success. go back to the domain list, do not pass "GO" changepage(page => "domlist"); } } else { # cancelled. whee! changepage(page => "domlist"); } } elsif ($webvar{page} eq 'record') { if ($webvar{recact} eq 'new') { $page->param(todo => "Add record to"); $page->param(recact => "add"); fill_rectypes(); } elsif ($webvar{recact} eq 'add') { my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl}); if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) { push @recargs, $webvar{distance}; if ($webvar{type} == $reverse_typemap{SRV}) { push @recargs, $webvar{weight}; push @recargs, $webvar{port}; } } my ($code,$msg) = addRec(@recargs); if ($code eq 'OK') { changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}); } else { $page->param(failed => 1); $page->param(errmsg => $msg); fill_recdata(); # populate the form... er, mostly. } } elsif ($webvar{recact} eq 'edit') { $page->param(todo => "Update record"); $page->param(recact => "update"); $page->param(parentid => $webvar{parentid}); $page->param(defrec => $webvar{defrec}); my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ". ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?"); $sth->execute($webvar{id}); my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array; $page->param(name => $host); $page->param(address => $val); $page->param(distance => $distance); $page->param(weight => $weight); $page->param(port => $port); $page->param(ttl => $ttl); fill_rectypes($type); } elsif ($webvar{recact} eq 'update') { my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id}, $webvar{name},$webvar{type},$webvar{address},$webvar{ttl}, $webvar{distance},$webvar{weight},$webvar{port}); if ($code eq 'OK') { $page->param(failed => 1); $page->param(errmsg => "testing"); $page->param(wastrying => "updating"); $page->param(todo => "Update record"); $page->param(recact => "update"); $page->param(parentid => $webvar{parentid}); $page->param(defrec => $webvar{defrec}); fill_recdata(); # changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}); } else { $page->param(failed => 1); $page->param(errmsg => $msg); $page->param(wastrying => "updating"); $page->param(todo => "Update record"); $page->param(recact => "update"); $page->param(parentid => $webvar{parentid}); $page->param(defrec => $webvar{defrec}); fill_recdata(); } } if ($webvar{defrec} eq 'y') { $page->param(dohere => "group ".grpName($dbh,$webvar{parentid})); } else { $page->param(dohere => domainName($dbh,$webvar{parentid})); } } elsif ($webvar{page} eq 'newrec') { push @debugbits, "whee!\n"; # populate most fields as needed. (eg, type list.) stdrecs(); } elsif ($webvar{page} eq 'addrec') { my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl}); if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) { push @recargs, $webvar{distance}; if ($webvar{type} == $reverse_typemap{SRV}) { push @recargs, $webvar{weight}; push @recargs, $webvar{port}; } } # wtf? # push @recargs, my ($code,$msg) = addRec(@recargs); if ($code eq 'OK') { showdomain($webvar{defrec},$webvar{parentid}); # NB: should **really** redirect here, in case of reload. >_< eyowch. } else { $page->param(add_failed => 1); $page->param(errmsg => $msg); stdrecs($webvar{type}); # populate the form... er, mostly. $page->param(name => $webvar{name}); $page->param(address => $webvar{address}); $page->param(distance => $webvar{distance}) if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}); $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV}; $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV}; } $page->param(defrec => $webvar{defrec}); } elsif ($webvar{page} eq 'conf_del') { $page->param(id => $webvar{id}); $page->param(defrec => $webvar{defrec}); my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec}); } elsif ($webvar{page} eq 'delrec') { $page->param(id => $webvar{id}); $page->param(defrec => $webvar{defrec}); # first pass = confirm y/n (sorta) if (!defined($webvar{del})) { $page->param(del_getconf => 1); my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id}); $page->param(host => $rec{host}); $page->param(ftype => $typemap{$rec{type}}); $page->param(recval => $rec{val}); } else { my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id}); if ($code ne 'OK') { ## need to find failure mode $page->param(del_failed => 1); $page->param(errmsg => $msg); } ##fixme: group/parent instead of hardcoded 1 showdomain('y',1); } } elsif ($webvar{page} eq 'editsoa') { fillsoa($webvar{defrec},$webvar{recid}); } elsif ($webvar{page} eq 'updatesoa') { print "ooooo!\n"; my $sth; my $sql = ''; # no domain ID, so we're editing the default SOA for a group (we don't care which one here) # plus a bit of magic to update the appropriate table $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records"). " set host='$webvar{prins}:$webvar{contact}',". " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',". " ttl=$webvar{ttl} where record_id=$webvar{recid}"; $sth = $dbh->prepare($sql); $sth->execute; if ($sth->err) { $page->param(update_failed => 1); $page->param(msg => $DBI::errstr); fillsoa($webvar{defrec},1); } else { $page->param(update_failed => 0); ##fixme! need to set group ID properly here showdomain('y',1); } } elsif ($webvar{page} eq 'adddomain') { # Need some magic here. ##fixme: Group should be variable my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0)); # hokay, a bit of magic to decide which page we hit. if ($code eq 'OK') { # redirect to dns.cgi?etc&page=reclist changepage(page => "reclist", id => $msg); $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl"); showdomain(0,$msg); ##work } else { # oooh, yeah, this is supposed to be a redirect. er, maybe. whee. $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl"); $page->param(add_failed => 1); $page->param(domain => $webvar{domain}); $page->param(errmsg => $msg); } } ## hmm. may want to move this so we can redirect after adding/deleting/etc print "Content-type: text/html\n\n", $header->output; foreach (@debugbits) { print; } $page->param(grp => $group) if $webvar{page} ne 'login'; # spit it out print $page->output; print "
webvar keys:
\n";
foreach my $key (keys %webvar) {
  print "key: $key\tval: $webvar{$key}\n";
}
print "
\nENV:\n
\n";
foreach my $key (keys %ENV) {
  print "key: $key\tval: $ENV{$key}\n";
}
print "
\n"; print $footer->output; exit 0; sub changepage { my %params = @_; # think this works the way I want... # handle user check my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid"; foreach (keys %params) { $newurl .= "&$_=$params{$_}"; } print "Status: 302\nLocation: $newurl\n\n"; exit; } # end changepage sub fillsoa { my $def = shift; my $id = shift; my $domname; if ($webvar{domain} == 0) { $domname = "DOMAIN"; } else { my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}"); $sth->execute(); ($domname) = $sth->fetchrow_array(); } $page->param(domain => $domname); $page->param(defrec => !$webvar{domain}); $page->param(group => $DNSDB::group); # defaults $page->param(defcontact => $DNSDB::def{contact}); $page->param(defns => $DNSDB::def{prins}); $page->param(defsoattl => $DNSDB::def{soattl}); $page->param(defrefresh => $DNSDB::def{refresh}); $page->param(defretry => $DNSDB::def{retry}); $page->param(defexpire => $DNSDB::def{expire}); $page->param(defminttl => $DNSDB::def{minttl}); # there are probably better ways to do this. TMTOWTDI. my %soa = getSOA($dbh,$def,$id); $page->param(domainid => $webvar{domain}); $page->param(recid => $soa{recid}); $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins})); $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact})); $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh})); $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry})); $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire})); $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl})); $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl})); } sub showdomain { my $def = shift; my $id = shift; # get the SOA first my %soa = getSOA($dbh,$def,$id); $page->param(recid => $soa{recid}); $page->param(contact => $soa{contact}); $page->param(prins => $soa{prins}); $page->param(refresh => $soa{refresh}); $page->param(retry => $soa{retry}); $page->param(expire => $soa{expire}); $page->param(minttl => $soa{minttl}); $page->param(ttl => $soa{ttl}); # my @foo2 = getDomRecs($dbh,'def',1); my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset}); my $row = 0; foreach my $rec (@$foo2) { $rec->{type} = $typemap{$rec->{type}}; $rec->{row} = $row % 2; $rec->{defrec} = $webvar{defrec}; $rec->{sid} = $webvar{sid}; $rec->{id} = $id; $row++; } $page->param(reclist => $foo2); } # fill in record type list on add/update/edit record template sub fill_rectypes { my $type = shift || $reverse_typemap{A}; my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder"); $sth->execute; push @debugbits, "type $type"; my @typelist; while (my ($rval,$rname) = $sth->fetchrow_array()) { my %row = ( recval => $rval, recname => $rname ); $row{tselect} = 1 if $rval == $type; push @typelist, \%row; } $page->param(typelist => \@typelist); } sub fill_recdata { fill_rectypes($webvar{type}); $page->param(name => $webvar{name}); $page->param(address => $webvar{address}); $page->param(distance => $webvar{distance}) if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}); $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV}; $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV}; $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl})); } sub fill_fpnla { my $count = shift; ##fixme if ($offset eq 'all') { #print "foo! wanna see'em all\n"; } else { # all these bits only have sensible behaviour if offset is numeric. err, probably. if ($count > $perpage) { # if there are more results than the default, always show the "all" link $page->param(navall => 1); if ($offset > 0) { $page->param(navfirst => 1); $page->param(navprev => 1); $page->param(prevoffs => $offset-1); } # show "next" and "last" links if we're not on the last page of results if ( (($offset+1) * $perpage - $count) < 0 ) { $page->param(navnext => 1); $page->param(nextoffs => $offset+1); $page->param(navlast => 1); $page->param(lastoffs => int (($count-1)/$perpage)); } } } } # end fill_fpnla() sub fill_pgcount { my $pgcount = shift; my $pgtype = shift; my $parent = shift; $page->param(ntot => $pgcount); $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1)); $page->param(npglast => ($offset eq 'all' ? $pgcount : ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) ) )); $page->param(pgtype => $pgtype); $page->param(parent => $parent); } # end fill_pgcount() sub listdomains { my $sth = $dbh->prepare("select count(*) from domains"); $sth->execute; my ($count) = ($sth->fetchrow_array); # fill page count and first-previous-next-last-all bits ##fixme - hardcoded group bit fill_pgcount($count,"domains","default group"); fill_fpnla($count); ##fixme - group $page->param(grp => 1); my @domlist; $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains". " inner join groups on domains.group_id=groups.group_id". " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage)); $sth->execute; my $rownum = 0; while (my @data = $sth->fetchrow_array) { my %row; $row{domainid} = $data[0]; $row{domain} = $data[1]; $row{status} = ($data[2] ? 'Active' : 'Inactive'); $row{group} = $data[3]; $row{bg} = ($rownum++)%2; # $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0); $row{mkactive} = !$data[2]; $row{sid} = $sid; $row{offset} = $offset; ##fixme: need to clean up status indicator/usage/inversion push @domlist, \%row; } $page->param(domtable => \@domlist); } # end listdomains()