source: trunk/textrecs.cgi@ 797

Last change on this file since 797 was 797, checked in by Kris Deugau, 4 years ago

/trunk

Clean up a lingering nuisance with Perl's default include path; all scripts
should now run correctly no matter what the caller's current directory.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 4.0 KB
Line 
1#!/usr/bin/perl -w -T
2# Plaintext record list for DeepNet DNS Administrator
3##
4# $Id: textrecs.cgi 797 2020-11-03 20:38:37Z kdeugau $
5# Copyright 2012-2014,2020 Kris Deugau <kdeugau@deepnet.cx>
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
20
21use strict;
22use warnings;
23
24use CGI::Carp qw (fatalsToBrowser);
25use CGI::Simple;
26use HTML::Template;
27use CGI::Session;
28use DBI;
29
30# push "the directory the script is in" into @INC
31use FindBin;
32use lib "$FindBin::RealBin/";
33
34use DNSDB;
35
36# Let's do these templates right...
37my $templatedir = "templates";
38
39# Set up the CGI object...
40my $q = new CGI::Simple;
41# ... and get query-string params as well as POST params if necessary
42$q->parse_query_string;
43
44# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
45my %webvar = $q->Vars;
46
47# shut up some warnings, in case we arrive somewhere we forgot to set this
48$webvar{defrec} = 'n' if !$webvar{defrec}; # non-default records
49$webvar{revrec} = 'n' if !$webvar{revrec}; # non-reverse (domain) records
50
51my $dnsdb = new DNSDB;
52
53# Check the session and if we have a zone ID to retrieve. Call a failure sub if not.
54my $sid = $q->cookie('dnsadmin_session');
55my $session = new CGI::Session("driver:File", $sid, {Directory => $dnsdb->{sessiondir}})
56 or die CGI::Session->errstr();
57do_not_pass_go() if !$sid;
58do_not_pass_go() if !$webvar{id};
59
60my $zone;
61$zone = ($webvar{revrec} eq 'n' ? $dnsdb->domainName($webvar{id}) : $dnsdb->revName($webvar{id}))
62 if $webvar{defrec} eq 'n';
63$zone = "group ".$dnsdb->groupName($webvar{id}) if $webvar{defrec} eq 'y';
64
65##fixme: do we support both HTML-plain and true plaintext? could be done, with another $webvar{}
66# Don't die on bad parameters. Saves munging the return from getRecList.
67#my $page = HTML::Template->new(filename => "$templatedir/textrecs.tmpl",
68# loop_context_vars => 1, global_vars => 1, die_on_bad_params => 0);
69#print "Content-type: text/html\n\n";
70
71print "Content-type: text/plain\n\n";
72print "Plaintext version of records for $zone.\n" if $webvar{defrec} eq 'n';
73print "Plaintext version of default ".($webvar{revrec} eq 'y' ? 'reverse ' : '')."records for $zone.\n"
74 if $webvar{defrec} eq 'y';
75print qq(Press the "Back" button to return to the standard record list.\n\n);
76
77my $reclist = $dnsdb->getRecList(defrec => $webvar{defrec}, revrec => $webvar{revrec}, id => $webvar{id},
78 sortby => ($webvar{revrec} eq 'n' ? 'type,host' : 'type,val'), sortorder => 'ASC', offset => 'all');
79foreach my $rec (@$reclist) {
80 $rec->{type} = $typemap{$rec->{type}};
81 $rec->{val} .= '.' if $rec->{type} ne 'A' && $rec->{type} ne 'TXT' && $webvar{revrec} eq 'n' && $rec->{val} !~ /\.$/;
82 $rec->{host} .= '.' if $webvar{revrec} eq 'y' && $rec->{val} !~ /\.$/;
83 $rec->{val} = "$rec->{distance} $rec->{val}" if $rec->{type} eq 'MX';
84 $rec->{val} = "$rec->{distance} $rec->{weight} $rec->{port} $rec->{val}" if $rec->{type} eq 'SRV';
85 if ($webvar{revrec} eq 'y') {
86 printf "%-16s\t%d\t%s\t%s\n", $rec->{val}, $rec->{ttl}, $rec->{type}, $rec->{host};
87 } else {
88 printf "%-45s\t%d\t%s\t%s\n", $rec->{host}, $rec->{ttl}, $rec->{type}, $rec->{val};
89 }
90}
91#$page->param(defrec => ($webvar{defrec} eq 'y'));
92#$page->param(revrec => ($webvar{revrec} eq 'y'));
93#$page->param(zone => $zone);
94#$page->param(reclist => $reclist);
95#$page->param(fwdzone => ($webvar{revrec} eq 'n'));
96#print $page->output;
97
98exit;
99
100sub do_not_pass_go {
101 my $webpath = $ENV{SCRIPT_NAME};
102 $webpath =~ s|/[^/]+$|/|;
103 print "Status: 302\nLocation: http://$ENV{HTTP_HOST}$webpath\n\n";
104 exit;
105}
Note: See TracBrowser for help on using the repository browser.