#!/usr/bin/perl 

# dnsman 0.67 - editrecord.cgi
#
# Copyright (C) 2000-2002 Hampton Maxwell <maxwell@101freeway.com>
# See COPYRIGHT for details.
#
# 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; under version 2 of the License. If you
# wish to use a later version, you must obtain permission from the author.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use CGI::Carp qw(fatalsToBrowser); # log errors to browser window

require './config.cgi';
require './functions.cgi';

# set values before parsing so they can't be overridden
use vars qw ($basedir $chroot $dnsconf);
my ($textdomain,
    $selectdomain);

$basedir = $FORM{'BASEDIR'};
$chroot = $FORM{'CHROOT'};

if ($FORM{'CONFLOCATION'}) { $::dnsconf = $FORM{'CONFLOCATION'}; }
elsif ($FORM{'CONFIGFILE'}) { $::dnsconf = $FORM{'CONFIGFILE'}; }
else { $::dnsconf = "named.conf"; }
$::dnsconf = clean_filename($::dnsconf);

parse_form();

my $dnsfile;
$FORM{'DOMAIN'}=$FORM{'RECORDFILE'};
$dnsfile =  "$basedir/$FORM{'RECDIR'}$FORM{'PREFIX'}$FORM{'DOMAIN'}$FORM{'SUFFIX'}";
$dnsfile = &clean_filename($dnsfile);

html_header("Edit dns records");

print <<MODIFYBUTTONS;
<FORM Method=post Action="editrecord.cgi">
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="Edit">
&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="View">
&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="Add Hosts">
&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="Delete">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="Edit Config">
&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="View Config">
<BR>
MODIFYBUTTONS

if ($FORM{'textdomain'} && $FORM{'textdomain'} ne $FORM{'activedomain'}) {
    $searchdomain = $FORM{'textdomain'};
    $searchtype = "any";
}
elsif ($FORM{'selectdomain'} && $FORM{'selectdomain'} ne $FORM{'activedomain'}) {
    $searchdomain = $FORM{'selectdomain'};
    $searchtype = "exact";
}
elsif ($FORM{'activedomain'}) { # domain we have just performed an action on
    $searchdomain = $FORM{'activedomain'};
    $searchtype = $FORM{'activesearchtype'};
}
else { # edit/list all domains
    $searchdomain = "";
    $searchtype = "any";
}

if ($FORM{'NODOMAINLIST'} ne "Yes") {
    print_domainlist();
}

if ($searchdomain && $searchtype eq "any") { $textdomain = $searchdomain; }
print "<B>Search for:</B>
<INPUT TYPE=text SIZE=20 NAME=textdomain VALUE=\"$textdomain\">&nbsp;
<BR><BR>\n";

if ($FORM{'SUBMITTYPE'}) {
print "</TD></TR>
<TR BGCOLOR=#ffffff>
<TD VALIGN=center>
<BR>
<blockquote>\n";
}



if ($FORM{'SUBMITTYPE'} eq "Search")
{
	my ($matches, $dnsrecord, @results) = 
        get_dnsinfo($FORM{'BINDVERSION'}, "all", $FORM{'textdomain'});
    print_searchresults("Edit", @results);
}

elsif ($FORM{'SUBMITTYPE'} eq "View")
{
	if (!$basedir) {
		print "Missing the base directory of the named records<BR>\n";
		html_footer();
		exit;
	}
    my ($domain, $filename, @results, $matches);

        ($matches, $filename, @results) = 
            get_dnsinfo($FORM{'BINDVERSION'}, $searchtype, $searchdomain);

    if ($matches != -1 && $matches != 1) { print_searchresults("View", @results); }

    $domain = $results[-1];
    $filename = clean_filename($filename);

    open (VIEW, "<$filename") || open_error($filename, 'r', $!, "noheader");

	print "<BR>
<B>Contents of the $domain domain ($filename):</B>
<INPUT TYPE=hidden NAME=activedomain VALUE=\"$domain\">
<INPUT TYPE=hidden NAME=activesearchtype VALUE=\"exact\">
<PRE>\n";
    while (<VIEW>) { # save memory by never storing the contents
        print $_;
    }

    close VIEW;
	print "</PRE>\n";
	html_footer();
	exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "View Config")
{
    open (CONF, "<$dnsconf") || open_error($dnsconf, 'r', $!, "noheader");

	print "<BR>\n";
	print "<B>Contents of $dnsconf:</B>";
	print "<PRE>";
    while (<CONF>) { # print in place to save ram
        print $_;
    }
    close CONF;
	print "</PRE>\n";
	html_footer();
	exit;
}

# XXX - make submit catch submissions that are triggered by pressing enter
#       might be better handling client-side with javascript
elsif ($FORM{'SUBMITTYPE'} eq "Edit" || (!$FORM{'SUBMITTYPE'} && $searchdomain) )
{
	if (!$basedir) {
		print "Missing the base directory of the named records\n";
		&html_footer;
		exit;
	}

	# here's how the priorities work -
	# List Domains click auto gets priority (doesn't pass SELDOMAIN and TEXTDOMAIN)
	# next, go off the boxes
	# finally, the hidden vars

    my ($matches, $filename, @results, $domain);
    ($matches, $filename, @results) =
        get_dnsinfo($FORM{'BINDVERSION'}, $searchtype, $searchdomain);

    if ($debug >= 1) {
        open (DEBUG, ">>$debugfile") || die "Can't open $debugfile: $!";
        print DEBUG "Search Results:\nmatches: $matches\nfilename: $filename\nresults:\n@results\n\n";
        close DEBUG;
    }

    if ($matches != -1 && $matches != 1) { print_searchresults("Edit", @results); }
    $domain = $results[-1];

    $filename = clean_filename($filename);
    open (EDITDOMAIN, "<$filename") || open_error($filename, 'r', $!, "noheader");

if (!$FORM{'EDITCOLS'}) { $FORM{'EDITCOLS'} = 80; }
if (!$FORM{'EDITROWS'}) { $FORM{'EDITROWS'} = 24; }
print "<BR>
<B>Contents of $domain ($filename)</B>&nbsp;&nbsp;&nbsp;&nbsp;<I>Update Serial!</I><BR>
<INPUT TYPE=hidden NAME=activedomain VALUE=\"$domain\">
<INPUT TYPE=hidden NAME=activesearchtype VALUE=\"exact\">
<INPUT TYPE=hidden NAME=EDITTYPE VALUE=fulledit>
<TEXTAREA NAME=DNSTEXT ROWS=\"$FORM{'EDITROWS'}\" COLS=\"$FORM{'EDITCOLS'}\" WRAP=VIRTUAL>\n";
while (<EDITDOMAIN>) { # print in place to save ram
    print $_;
}
close EDITDOMAIN;
print "</TEXTAREA>
<BR><BR>
<INPUT TYPE=submit name=SUBMITTYPE VALUE=\"Do The Math\">
&nbsp;&nbsp;&nbsp;&nbsp;Update Serial
<INPUT TYPE=checkbox NAME=UPDATESERIAL VALUE=checkbox";
if ($FORM{'AUTOSERIAL'} eq "yes") { print " checked\n"; }
print ">
</FORM></blockquote>\n";
    &html_footer;
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Do The Math")
{
    my ($matches, $filename, @results) = get_dnsinfo($FORM{'BINDVERSION'}, "exact", $FORM{'activedomain'});
    if ($matches != -1) {
        print "<H3>Filename of DNS record for $FORM{'activedomain'} could not be located in $dnsconf</H3><BR>\n";
        html_footer();
    }
    $FORM{'DNSTEXT'} =~s/\cM//g; # remove ^M's

    $filename = clean_filename($filename);
    open(DNS, ">$filename") || open_error($filename, 'w', $!, "noheader");
    print DNS $FORM{'DNSTEXT'};
    close DNS;

    if (!$FORM{'UPDATESERIAL'}) { print "<B>Please be sure you updated the serial.</B><BR><BR>\n"; }
    else { 
    	update_serial($filename);
    	print "<B>Serial was updated.</B><BR><BR>\n";
    }

    named_refresh();

    open (DNSCONF, "<$filename") || open_error($filename, 'r', $!, "noheader");
    my @contents = <DNSCONF>;
    close DNSCONF;

    print "<B>Updated $filename zone file:</B>
    <PRE>";
    print @contents;
    print "</PRE>\n";
    html_footer();
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Edit Config")
{
    open (DNSCONF, "<$dnsconf") || open_error($dnsconf, 'r', $!, "noheader");
    my @contents = <DNSCONF>;
    close DNSCONF;

    if (!$FORM{'EDITCOLS'}) { $FORM{'EDITCOLS'} = 80; }
    if (!$FORM{'EDITROWS'}) { $FORM{'EDITROWS'} = 24; }

    print "<BR>
<B>Contents of $dnsconf</B><BR>
<TEXTAREA NAME=DNSTEXT ROWS=\"$FORM{'EDITROWS'}\" COLS=\"$FORM{'EDITCOLS'}\" WRAP=VIRTUAL>
";
    print @contents;
    print "</TEXTAREA>
<BR><BR>
<INPUT TYPE=submit name=SUBMITTYPE VALUE=\"Make It So\">
</FORM>\n";
    html_footer();
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Delete")
{
    my ($matches, $filename, @domainqueue, $domain, $namedentry);
    ($matches, $filetodelete, @results, $namedentry) 
        = get_dnsinfo($FORM{'BINDVERSION'}, $searchtype, $searchdomain);

    if ($matches != -1 && $matches != 1) { &print_searchresults("Delete", @results); }
    $domain = $results[-1];

    $filetodelete = clean_filename($filetodelete);
    open (DELFILE, "<$filetodelete") || open_error($filetodelete, 'r', $!, "noheader");

    print "<BR>
<INPUT TYPE=hidden NAME=DOMAINTODELETE VALUE=\"$domain\">
<B>Are you SURE you want to delete the $domain domain?</B>
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE=\"Kill It\">&nbsp;&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE=\"Do Not Delete\">&nbsp;&nbsp;
<BR><BR>
<B>Contents of $domain record ($filetodelete):</B><BR>
<PRE>\n";
    while (<DELFILE>) { # print in place to save ram
        print $_;
    }
    close DELFILE;
    print "</PRE>
<B>Are you SURE you want to delete this domain?</B>
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE=\"Kill It\">&nbsp;&nbsp;
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE=\"Do Not Delete\">&nbsp;&nbsp;
<BR>";
    &html_footer;
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Kill It" || $FORM{'ACTION'} eq "slavedelete")
{
    if ($FORM{'ACTION'} eq "slavedelete") {
        version_check($FORM{'MSTRVERSION'});
    }
        
    my ($success, $deletedfile, $deleteerror) = delete_zone($FORM{'BINDVERSION'}, $FORM{'DOMAINTODELETE'});
    if ($success eq "yes") {
        print "<BR>The zone: $FORM{'DOMAINTODELETE'} was deleted successfully.<BR>\n";
        print "The corresponding file: $deletedfile was deleted.<BR><BR>\n";

        # don't print slavedelete on secondaries
        if ($FORM{'SLAVEUPDATE'} eq "yes" && $FORM{'ACTION'} ne "slavedelete") {
            print_slavedelete($FORM{'DOMAINTODELETE'});
        }
    }
    elsif ($success eq "nodelete") {
        print "<BR>The entry for the zone: $FORM{'DOMAINTODELETE'} was deleted successfully.<BR>\n";
        print "The file: $deletedfile could NOT be deleted.<BR>\nThe error was: $deleteerror.<BR>\n";
    }
    elsif ($success eq "domainnotfound") {
        print "<BR>Unable to find the entry for the zone: $domain.<BR>\n";
        print "You may need to delete the entry manually.<BR>\n";
    }
    elsif ($success eq "filenamenotfound") {
        print "<BR>Unable to find the record filename for the zone: $domain.<BR>\n";
        print "You may need to delete the zone manually.<BR>\n";
    }
    html_footer();
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Make It So")
{
    $FORM{'DNSTEXT'}=~s/\cM//g; # remove ^M's
    open(CONF, ">$dnsconf") || &open_error($dnsconf, 'w', $!, "noheader");
    print CONF $FORM{'DNSTEXT'};
    close CONF;
    print "<BR>\n";
    named_refresh();

    open (VIEWCONF, "<$dnsconf") || open_error($dnsconf, 'r', $!, "noheader");

    print "<BR>
    <B>Updated $dnsconf</B>
    <PRE>";
    while (<VIEWCONF>) {
        print $_;
    }
    close VIEWCONF;
    print "</PRE>\n";
    html_footer();
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Add Hosts")
{
	if (!$dnsconf) {
		print "Missing the base directory of the named records:\n";
		html_footer();
		exit;
	}
    my ($matches, $filename, @domainqueue, $domain);
    ($matches, $filename, @results) = get_dnsinfo($FORM{'BINDVERSION'}, "domain", $domain);

    if ($matches != -1) { print_searchresults("Add Hosts", @results); }
	print <<ADDHOSTS;
<B>Add Hosts to the $domain domain ($filename):</B>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE=hidden NAME=activedomain VALUE="$domain">
<INPUT TYPE=hidden NAME=activesearchtype VALUE="exact">
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="Add Some Hosts">
<BR><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<U>A Records (hostname -> ip mapping):</U><BR>
<TABLE BORDER=0>
<TR><TD WIDTH=60></TD>
<TD>Hostname 1: <INPUT TYPE=text SIZE=16 NAME=ARECORD1>
&nbsp;&nbsp;&nbsp;
Target IP Address: <INPUT TYPE=text SIZE=16 NAME=ARECORD1IP>
</TD></TR>
<TR><TD WIDTH=40></TD>
<TD>Hostname 2: <INPUT TYPE=text SIZE=16 NAME=ARECORD2>
&nbsp;&nbsp;&nbsp;
Target IP Address: <INPUT TYPE=text SIZE=16 NAME=ARECORD2IP>
</TD></TR>
<TR><TD WIDTH=40></TD>
<TD>Hostname 3: <INPUT TYPE=text SIZE=16 NAME=ARECORD3>
&nbsp;&nbsp;&nbsp;
Target IP Address: <INPUT TYPE=text SIZE=16 NAME=ARECORD3IP>
</TD></TR></TABLE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<U>CNAME Records (hostname -> hostname mapping):</U><BR>
<TABLE BORDER=0>
<TR><TD WIDTH=60></TD>
<TD>Hostname 1: <INPUT TYPE=text SIZE=16 NAME=CNAME1>
&nbsp;&nbsp;&nbsp;
Target Hostname: <INPUT TYPE=text NAME=CNAME1TARGET>
</TD></TR>
<TR><TD WIDTH=40></TD>
<TD>Hostname 2: <INPUT TYPE=text SIZE=16 NAME=CNAME2>
&nbsp;&nbsp;&nbsp;
Target Hostname: <INPUT TYPE=text NAME=CNAME2TARGET>
</TD></TR>
<TR><TD WIDTH=40></TD>
<TD>Hostname 3: <INPUT TYPE=text SIZE=16 NAME=CNAME3>
&nbsp;&nbsp;&nbsp;
Target Hostname: <INPUT TYPE=text NAME=CNAME3TARGET>
</TD></TR></TABLE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<U>MX Records (Mail eXchange):</U><BR>
<TABLE BORDER=0>
<TR><TD WIDTH=60></TD>
<TD>Hostname 1: <INPUT TYPE=text SIZE=16 NAME=MX1>
&nbsp;&nbsp;
Delivery hostname: <INPUT TYPE=text NAME=MX1TARGET>
&nbsp;&nbsp;
Priority <INPUT TYPE=text SIZE=4 NAME=MX1PRI VALUE="$FORM{'MX1PRI'}">
</TD></TR>
<TR><TD WIDTH=60></TD>
<TD>Hostname 2: <INPUT TYPE=text SIZE=16 NAME=MX2>
&nbsp;&nbsp;
Delivery hostname: <INPUT TYPE=text NAME=MX2TARGET>
&nbsp;&nbsp;
Priority <INPUT TYPE=text SIZE=4 NAME=MX2PRI VALUE="$FORM{'MX2PRI'}">
</TD></TR>
<TR><TD WIDTH=60></TD>
<TD>Hostname 3: <INPUT TYPE=text SIZE=16 NAME=MX3>
&nbsp;&nbsp;
Delivery hostname: <INPUT TYPE=text NAME=MX3TARGET>
&nbsp;&nbsp;
Priority <INPUT TYPE=text SIZE=4 NAME=MX3PRI VALUE="$FORM{'MX3PRI'}">
</TD></TR></TABLE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<U>NS Records (NameServer assignment):</U><BR>
<TABLE BORDER=0>
<TR><TD WIDTH=60></TD>
<TD>Hostname 1: <INPUT TYPE=text SIZE=16 NAME=NS1HOST>
&nbsp;&nbsp;
Nameserver IP or hostname: <INPUT TYPE=text NAME=NS1>
&nbsp;&nbsp;
</TD></TR>
<TR><TD WIDTH=60></TD>
<TD>Hostname 2: <INPUT TYPE=text SIZE=16 NAME=NS2HOST>
&nbsp;&nbsp;
Nameserver IP or hostname: <INPUT TYPE=text NAME=NS2>
&nbsp;&nbsp;
</TD></TR></TABLE>
<TABLE ALIGN=right><TR>
<TD>
<INPUT TYPE=submit NAME=SUBMITTYPE VALUE="Add Some Hosts"></TD>
<TD WIDTH=60></TD></TR></TABLE>
<BR>
</FORM><BR>\n
ADDHOSTS

    html_footer();
    exit;
}

elsif ($FORM{'SUBMITTYPE'} eq "Add Some Hosts")
{
	if (!$FORM{'activedomain'}) {
		print "Missing the following required values:<BR>\n";
		if (!$FORM{'activedomain'}) { print "name of the domain<BR>\n"; }
		html_footer();
		exit;
	}
    my ($matches, @results) = get_dnsinfo($FORM{'BINDVERSION'}, "filename", $FORM{'activedomain'});
    my $filename = shift(@results);
    if (!$filename) {
        print "<H3>DNS record for $FORM{'activedomain'} could not be located</H3><BR>\n";
        html_footer();
    }
	open (ADDHOST, ">>$filename") || &open_error($FORM{'filename'}, 'a', $!, "noheader");
	if ($FORM{'ARECORD1'}) {
		if ($FORM{'ARECORD1IP'} && &check_ip($FORM{'ARECORD1IP'})) {
			print ADDHOST "\n$FORM{'ARECORD1'}\tIN\tA\t$FORM{'ARECORD1IP'}";
			$hostsadded++;
		}
		else { push (@add_errors, "Invalid or non-existant IP for A entry #1\n"); }
	}
	if ($FORM{'ARECORD2'}) {
		if ($FORM{'ARECORD2IP'} && &check_ip($FORM{'ARECORD2IP'})) {
			print ADDHOST "\n$FORM{'ARECORD2'}\tIN\tA\t$FORM{'ARECORD2IP'}";
			$hostsadded++;
		}
		else { push (@add_errors, "Invalid or non-existant IP for A entry #2\n"); }
	}
	if ($FORM{'ARECORD3'}) {
		if ($FORM{'ARECORD3IP'} && &check_ip($FORM{'ARECORD3IP'})) {
			print ADDHOST "\n$FORM{'ARECORD3'}\tIN\tA\t$FORM{'ARECORD3IP'}";
			$hostsadded++;
		}
		else { push (@add_errors, "Invalid or non-existant IP for A entry #3\n"); }
	}
	if ($FORM{'CNAME1'}) {
		if ($FORM{'CNAME1TARGET'}) {
			$FORM{'CNAME1TARGET'} = &make_fqdn($FORM{'CNAME1TARGET'}); # make fully qualified
			print ADDHOST "\n$FORM{'CNAME1'}\tIN\tCNAME\t$FORM{'CNAME1TARGET'}";
			$hostsadded++;
		}
		else { push (@add_errors, "No target specified for CNAME entry #1\n"); }
	}
	if ($FORM{'CNAME2'}) {
		if ($FORM{'CNAME2TARGET'}) {
			$FORM{'CNAME2TARGET'} = &make_fqdn($FORM{'CNAME2TARGET'}); # make fully qualified
			print ADDHOST "\n$FORM{'CNAME2'}\tIN\tCNAME\t$FORM{'CNAME2TARGET'}";
			$hostsadded++;
		}
		else { push (@add_errors, "No target specified for CNAME entry #2\n"); }
	}
	if ($FORM{'CNAME3'}) {
		if ($FORM{'CNAME3TARGET'}) {
			$FORM{'CNAME3TARGET'} = &make_fqdn($FORM{'CNAME3TARGET'}); # make fully qualified
			print ADDHOST "\n$FORM{'CNAME3'}\tIN\tCNAME\t$FORM{'CNAME3TARGET'}";
			$hostsadded++;
		}
		else { push (@add_errors, "No target specified for CNAME entry #3\n"); }
	}
	if ($FORM{'MX1'}) {
		if ($FORM{'MX1TARGET'} && $FORM{'MX1PRI'}=~/^\d+$/) {
			$FORM{'MX1TARGET'} = &make_fqdn($FORM{'MX1TARGET'}); # make fully qualified
			print ADDHOST "\n$FORM{'MX1'}\tIN\tMX\t$FORM{'MX1PRI'}\t$FORM{'MX1TARGET'}";
			$hostsadded++;
		}
		else { push (@add_errors, "Invalid or missing values in MX entry #1\n"); }
	}
	if ($FORM{'MX2'}) {
		if ($FORM{'MX2TARGET'} && $FORM{'MX2PRI'}=~/^\d+$/) {
			$FORM{'MX2TARGET'} = &make_fqdn($FORM{'MX2TARGET'}); # make fully qualified
			print ADDHOST "\n$FORM{'MX2'}\tIN\tMX\t$FORM{'MX2PRI'}\t$FORM{'MX2TARGET'}";
			$hostsadded++;
		}
		else { push (@add_errors, "Invalid or missing values in MX entry #2\n"); }
	}
	if ($FORM{'MX3'}) {
		if ($FORM{'MX3TARGET'} && $FORM{'MX3PRI'}=~/^\d+$/) {
			$FORM{'MX3TARGET'} = &make_fqdn($FORM{'MX3TARGET'}); # make fully qualified
			print ADDHOST "\n$FORM{'MX3'}\tIN\tMX\t$FORM{'MX3PRI'}\t$FORM{'MX3TARGET'}";
			$hostsadded++;
		}
		else { push (@add_errors, "Invalid or missing values in MX entry #3\n"); }
	}
	close ADDHOST;
	if ($hostsadded) { $hostadded = "No"; }
	print "<B>$hostsadded entries were added successfully.</B><BR>\n";
	if (@add_errors) {
		print "<BR><B>The following errors occurred:</B><BR>\n";
		foreach $adderror (@add_errors) {
			print "&nbsp;&nbsp;&nbsp;$adderror<BR>\n";
		}
	}
	update_serial($filename);
	print "<BR><B>Serial automatically updated.</B><BR>\n";
	if ($FORM{'AUTOREFRESH'} eq "yes") { named_refresh(); }
    open (ADDHOSTS, "<$filename") || open_error($filename, 'r', $!, "noheader");
	print "<BR><B>Contents of the $FORM{'activedomain'} domain ($filename):</B>
<INPUT TYPE=hidden NAME=activedomain VALUE=\"$FORM{'activedomain'}\">
<INPUT TYPE=hidden NAME=activesearchtype VALUE=\"exact\">
<PRE>\n";
    while (<ADDHOSTS>) {
        print $_;
    }
    close ADDHOSTS;
    print @contents;
	print "</PRE><BR>\n";
	html_footer();
	exit;
}
	
html_footer();
exit;

sub print_searchresults
{
    my ($submittype, @domainlist) = @_;

    if (!@domainlist) { 
        print "<H3>Domain entry not found</H3><BR>\n";
        html_footer();
        exit;
    }

$submittype =~s/\ /%20/g;
print "<BR>
<B>List of domains that match your search:</B><BR><BR>                                                                    
<input type=hidden name=activedomain value=\"$searchdomain\">
<input type=hidden name=activesearchtype value=\"$searchtype\">
<blockquote>\n";                                                                                                          
@domainlist = sort (@domainlist);                                                                                         
if ($submittype eq "List%20Domains") { $submittype = "Edit"; }                                            
my $domainentry;
foreach $domainentry (@domainlist) {                                                                                      
    print "\n<A HREF=editrecord.cgi?SUBMITTYPE=$submittype&activedomain=$domainentry&activesearchtype=exact>$domainentry</A><BR><BR>";                     
}                                                                                                                         
print "\n</blockquote>\n";                                                                                                
&html_footer;                                                                                                             
exit;                                                                                                                     
}

sub print_domainlist {
    open (DOMAINLIST,"<$dnsconf") || open_error($dnsconf, 'r', $!, "noheader");
    print "<SELECT NAME=selectdomain>\n";

    my (@domainqueue, $reversequeue);
    my ($viewscope, $viewbrackets);
    while (<DOMAINLIST>) {
        if ($viewscope) {
            $viewbrackets += &bracketcheck($_);
            if (!$viewbrackets) { $viewscope = undef; }
        }

        if ($_=~/^\s*zone\s+\"(.{2,})\"/i) {  # find all zones of format:   zone  "name";
            my $mydomain = $1;
            # put reverse domains in separate queue, so they go to the bottom of the list
            if ($mydomain=~/arpa/i) { push (@reversequeue, lc $mydomain); }
            else {
                if ($viewscope) { $mydomain .= "($viewscope)"; }
                push (@domainqueue, lc $mydomain);
            }
        }
        elsif ($_=~/^\s*view\s+\"(.{2,})\"/i) {
            if ($debug >= 1) { print "view change: $1\n"; }
            $viewscope = $1;
            $viewbrackets += &bracketcheck($_);
        }
    }
    close DOMAINLIST;

    @domainqueue = sort(@domainqueue);
    @reversequeue = sort(@reversequeue);
    if ($searchdomain && $searchtype eq "exact") {
        print "<OPTION SELECTED>$searchdomain</OPTION>
<OPTION VALUE=\"\">Select Domain</OPTION>\n";
    }
    else {
        print "<OPTION VALUE=\"\" SELECTED>Select Domain\n";
    }

    foreach $poppeddomain (@domainqueue) {
            print "<OPTION>$poppeddomain</OPTION>\n";
    }
    foreach $poppedreverse (@reversequeue) {
            print "<OPTION>$poppedreverse</OPTION>\n";
    }

    print "</SELECT> - or - ";

}
