#!/usr/bin/perl

# A script to output data related to DNSSEC keys

use strict;

use Getopt::Long;
use Pod::Usage;

use Infoblox;

my $SERVER;
my $USER;
my $PASS;
my $DNS_VIEW = 'default';

### Get the passed parameters

my $options_okay = GetOptions (
    
    # GRID options
    's=s'   => \$SERVER,
    'u=s'   => \$USER,
    'p=s'   => \$PASS,
    'v=s'   => \$DNS_VIEW,

    # Standard meta-options
    'help|?'    => sub { pod2usage(1); },
    'man'       => sub { pod2usage(-exitstatus => 0, -verbose => 2); },
);

if ( ! ( $SERVER and $USER and $PASS ) ) {
    print "Supply correct arguments\n\n";
    pod2usage(1);
}


### Establish a session with the Grid Master

my %session_args = (
    master   => $SERVER,
    username => $USER,
    password => $PASS,
);

my $session_obj = Infoblox::Session->new( %session_args );

if ($session_obj->status_code()) {
    print "Failed to create session: "
          . $session_obj->status_code()
          . " : " . $session_obj->status_detail() . "\n";
    exit;
 }
print "Session created successfully to $SERVER\n";


### Retrieve zones

my @view_zone_objs = $session_obj->get(
	object => 'Infoblox::DNS::Zone',
	view   => $DNS_VIEW,
);


### Iterate through the zones in the view default and check if the zone is signed

for my $zone_obj (@view_zone_objs) {
	if ( $zone_obj->dnssec_signed() eq "true" ) {
        
        	# Print zone-name and ksk rollover date
		print "zone: ",$zone_obj->name(), "\n";
		my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime( $zone_obj->dnssec_ksk_rollover_date() );
		$year += 1900;	
		$mon += 1;
		print "ksk-rollover-date: $year-$mon-$mday\n";

		my $keys_ref = $zone_obj->dnssec_keys();

		# obtain the active KSK and print details
		for my $key_obj ( @$keys_ref ) {
			if ( $key_obj->type() eq "KSK" && $key_obj->status eq "ACTIVE" ) {
				print "algorithm: ",$key_obj->algorithm(),"\n";
				print "tag: ",$key_obj->tag(),"\n";
				print "\n";
			}
		}
	}
}


=head1 NAME

ksk-overview.pl - Print KSK rollover dates and key details

=head1 VERSION

This documentation refers to ksk-overview.pl version 0.0.1

=head1 USAGE


ksk-overview.pl --s=<IP> --u=<user> --p=<password> [ --v=<view name> ]

e.g.

  ksk-overview.pl --s=192.168.1.2 --u=user --p=password --v=external-view

=head1 REQUIRED ARGUMENTS

You can pass the arguments on the command line.

=over

=item -s GridMaster

The grid GM to connect to

=item -u username

The grid/PAPI username

=item -p password

The grid/PAPI password

=back

=head1 OPTIONAL ARGUMENTS

=over

=item -v DNSView

DNS View name, defaults to "default".

=item   --help|?

Print this summary

=item   --man

Displays the complete manpage then exits gracefully.

=back

=head1 DESCRIPTION

A full description of the application and its features.
May include numerous subsections (i.e. =head2, =head3, etc.)


=head1 DEPENDENCIES

Infoblox Perl API

=head1 INCOMPATIBILITIES

None known.

=head1 BUGS AND LIMITATIONS

None known.

=head1 AUTHOR

Paul de Haan ( pdehaan@infoblox.com )


=head1 LICENCE AND COPYRIGHT

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.
