#!/usr/bin/perl
#
# Copyright 2011-2012 SPARTA, Inc.  All rights reserved.  See the COPYING
# file distributed with this software for details.
#
#
# makezones
#
#	This script creates a bunch of demo zones.
#

use strict;

use Getopt::Long qw(:config no_ignore_case_always);

#
# Version information.
#
my $NAME   = "makezones";
my $VERS   = "$NAME version: 1.9.1";
my $DTVERS = "DNSSEC-Tools Version: 1.9";

#######################################################################

my $DEFAULT_ZONECOUNT =	50;		# Default number of zones to create.
my $MINNUM = 5;				# Number of minutes to vary zones by.

#######################################################################
#
# Data required for command line options.
#

my %options = ();			# Filled option array.
my @opts =
(
	"count=i",			# Count of zones to create.
	"zs=s",				# Zonesigner arguments.
	"out=s",			# Output file.
	"Version",			# Display the version number.
	"help",				# Give a usage message and exit.
);

#
# Flag values for the various options.
#
my $zonecnt = $DEFAULT_ZONECOUNT;	# Zones to create.
my $zsargs;				# Zone name option value.

my $outfile;				# Output file option value.

#######################################################################

my $ret;				# Return code from main().

$ret = main();
exit($ret);

#-----------------------------------------------------------------------------
# Routine:	main()
#
# Purpose:	Main controller program.
#
sub main
{
	my $cntlen;				# Length of zone count.

	optsandargs();

	$cntlen = length("a" . $zonecnt) - 1;

	#
	# Create the rollrec file.
	#
	open(RRF,"> demo.rollrec");

	#
	# Create the new zone file and update the rollrec file.
	#
	print "\n";
	print "zonecnt - <$zonecnt>\n";
	for(my $ind=1; $ind <= $zonecnt; $ind++)
	{
		my $zone;			# New zone's name.
		my $tempus;			# Time for zone.
		my $min;			# Minutes adjustment value.
		my $lifespan;			# Key lifespan.

		#
		# Build the new zone's name.
		#
		$zone = sprintf "example-%0*d.com", $cntlen, $ind;

		#
		# Calculate a lifespan for the keys.
		#
		$lifespan = 30 + int(rand(30));

		#
		# Create and sign the new zone file.
		#
		system("cp save-example.com $zone");
		system("perl -pi -e 's/TTL	30/TTL	$lifespan/' $zone");
		system("zonesigner -ksklife $lifespan -zsklife $lifespan $zsargs $zone");

		#
		# Get the current time and drop it back a few minutes..
		#
		$tempus = time;
		$min = int(rand($MINNUM)) * 60;
		$tempus -= $min;

		#
		# Get the the text time.
		#
		$tempus = gmtime $tempus;
		chomp $tempus;

		#
		# Add an entry to the rollrec file for this zone.
		#
		print RRF "
roll \"$zone\"
	zonefile        \"$zone.signed\"
	zonename        \"$zone\"
	keyrec          \"$zone.krf\"
	maxttl          \"60\"
	display         \"1\"
	phasestart	\"$tempus\"

";
	}

	#
	# Close the rollrec file.
	#
	close(RRF);
}

#-----------------------------------------------------------------------------
# Routine:	optsandargs()
#
# Purpose:	This routine shakes and bakes our command line options.
#		A bunch of option variables are set according to the specified
#		options.  Then a little massaging is done to make sure that
#		the proper actions are taken.
#
sub optsandargs
{
	#
	# Parse the options.
	#
	GetOptions(\%options,@opts) || usage();

	#
	# Check some immediate commands.
	#
	version() if(defined($options{'Version'}));
	usage() if(defined($options{'help'}));

	#
	# Set our option variables based on the parsed options.
	#
	$zonecnt = $options{'count'}	  || $DEFAULT_ZONECOUNT;
	$zsargs	  = $options{'zs'}	  || "-genkeys";
	$outfile  = $options{'out'}       || "demo.rollrec";

	#
	# Set our option variables based on the parsed options.
	#
	if($zsargs =~ /\-ksklife/)
	{
		print STDERR "do not include -ksklife in -zsargs\n";
		exit(1);
	}
	if($zsargs =~ /\-zsklife/)
	{
		print STDERR "do not include -zsklife in -zsargs\n";
		exit(1);
	}
}


#----------------------------------------------------------------------
# Routine:	version()
#
# Purpose:	Print the version number(s) and exit.
#
sub version
{
	print STDERR "$VERS\n";
	print STDERR "$DTVERS\n";
	exit(0);
}

#-----------------------------------------------------------------------------
# Routine:	usage()
#
sub usage
{
	print STDERR "usage:  makezones [options]\n";
	print STDERR "\t-count		count of zones to generate\n";
	print STDERR "\t-out		output file\n";
	print STDERR "\t-zs		options for zonesigner\n";
	print STDERR "\t-Version	display version number\n";
	print STDERR "\t-help		help message \n";
	exit(0);
}

1;

##############################################################################
#

=pod

=head1 NAME

makezones - Creates and signs a large set of zones for a demo.

=head1 SYNOPSIS

  makezones [options]

=head1 DESCRIPTION

B<makezones> creates the files required for a DNSSEC-Tools zone.  A large
number of zones is created and signed (using B<zonesigner>.) A I<rollrec>
file is created that will allow B<rollerd> to manage the rollover process
for all these zones.

Each generated zone will be given a TTL of from 30 to 60 seconds.  This 
TTL value is also passed to B<zonesigner> as the value for the I<-ksklife>
and I<-zsklife> options.  Other arguments may be passed to B<zonesigner>
by using the I<-zs> argument to B<makezones>.

The number of zones may be specified using the I<-count> option.  The zones
will be created using a template named B<save-example.com>.  The zones are
essentially the same and the actual zone data is all for the zone
I<example.com>.  The DNSSEC-Tools commands don't manipulate the contents
of the zone files, so the similarity of zone data is not an issue.

The I<rollrec> file created to manage this large collection of zones
may be specified by the I<-out> option.  If this isn't given, then the
I<rollrec> entries will be written to B<demo.rollrec>.

=head1 OPTIONS

B<makezones> may be given the following options:

=over 4

=item B<-count zonecount>

This specifies the number of zones that will be created.  If this isn't
specified, then a default number of 50 zones will be created.

=item B<-out rollrec-file>

This specifies the name of the I<rollrec> file.  The file will be created if
it does not exist, and it will be truncated if it does exist.  If this isn't
specified, then B<demo.rollrec> will be used.

=item B<-zs zonesigner-opts>

This specifies options that will be passed to B<zonesigner>.  The I<-ksklife>
and I<-zsklife> options should not be included.  If this option is not
specified, then the I<-genkeys> option will be passed to B<zonesigner>.

=item B<-help>

Display a usage message.

=item B<-Version>

Display version information for B<makezones> and DNSSEC-Tools.

=back

=head1 COPYRIGHT

Copyright 2011-2012 SPARTA, Inc.  All rights reserved.
See the COPYING file included with the DNSSEC-Tools package for details.

=head1 AUTHOR

Wayne Morrison, tewok@tislabs.com

=head1 SEE ALSO

B<rollerd(8)>,
B<zonesigner(8)>

B<rollrec(5)>

=cut
