#!/usr/bin/perl -w -T # Plaintext record list for DeepNet DNS Administrator ## # $Id: textrecs.cgi 422 2012-10-10 17:41:00Z kdeugau $ # Copyright 2012 Kris Deugau # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ## use strict; use warnings; use CGI::Carp qw (fatalsToBrowser); use CGI::Simple; use HTML::Template; use CGI::Session; use DBI; # don't remove! required for GNU/FHS-ish install from tarball use lib '.'; ##uselib## use DNSDB qw(:ALL); # Let's do these templates right... my $templatedir = "templates"; # 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; # shut up some warnings, in case we arrive somewhere we forgot to set this $webvar{defrec} = 'n' if !$webvar{defrec}; # non-default records #$webvar{revrec} = 'n' if !$webvar{revrec}; # non-reverse (domain) records # load some local system defaults (mainly DB connect info) # note this is not *absolutely* fatal, since there's a default dbname/user/pass in DNSDB.pm # we'll catch a bad DB connect string once we get to trying that ##fixme: pass params to loadConfig, and use them there, to allow one codebase to support multiple sites if (!loadConfig()) { warn "Using default configuration; unable to load custom settings: $DNSDB::errstr"; } # Check the session and if we have a zone ID to retrieve. Call a failure sub if not. my $sid = ($webvar{sid} ? $webvar{sid} : undef); my $session = new CGI::Session("driver:File", $sid, {Directory => $config{sessiondir}}) or die CGI::Session->errstr(); do_not_pass_go() if !$sid; do_not_pass_go() if !$webvar{id}; ##fixme: quit throwing the database handle around, and put all the SQL and direct DB fiddling into DNSDB.pm # dbname, user, pass, host (optional) my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost}); # Load config pieces from the database. Ideally all but the DB user/pass/etc should be loaded here. initGlobals($dbh); my $zone; $zone = domainName($dbh, $webvar{id}) if $webvar{defrec} eq 'n'; $zone = "group ".groupName($dbh, $webvar{id}) if $webvar{defrec} eq 'y'; ##fixme: do we support both HTML-plain and true plaintext? could be done, with another $webvar{} # Don't die on bad parameters. Saves munging the return from getDomRecs. #my $page = HTML::Template->new(filename => "$templatedir/textrecs.tmpl", # loop_context_vars => 1, global_vars => 1, die_on_bad_params => 0); #print "Content-type: text/html\n\n"; print "Content-type: text/plain\n\n"; print "Plaintext version of records for $zone.\n" if $webvar{defrec} eq 'n'; print "Plaintext version of default records for $zone.\n" if $webvar{defrec} eq 'y'; print qq(Press the "Back" button to return to the standard record list.\n\n); my $reclist = getDomRecs($dbh, $webvar{defrec}, $webvar{id}, 0, 'all', 'type,host', 'ASC'); foreach my $rec (@$reclist) { $rec->{type} = $typemap{$rec->{type}}; $rec->{val} .= '.' if $rec->{type} ne 'A' && $rec->{val} !~ /\.$/; $rec->{host} .= '.' if $rec->{val} !~ /\.$/; $rec->{val} = "$rec->{distance} $rec->{val}" if $rec->{type} eq 'MX'; $rec->{val} = "$rec->{distance} $rec->{weight} $rec->{port} $rec->{val}" if $rec->{type} eq 'SRV'; printf "%-45s\t%d\t%s\t%s\n", $rec->{host}, $rec->{ttl}, $rec->{type}, $rec->{val}; } #$page->param(defrec => ($webvar{defrec} eq 'y')); #$page->param(revrec => ($webvar{revrec} eq 'y')); #$page->param(zone => $zone); #$page->param(reclist => $reclist); #$page->param(fwdzone => ($webvar{revrec} eq 'n')); #print $page->output; exit; sub do_not_pass_go { my $webpath = $ENV{SCRIPT_NAME}; $webpath =~ s|/[^/]+$|/|; print "Status: 302\nLocation: http://$ENV{HTTP_HOST}$webpath\n\n"; exit; }