zone-ls.pl
changeset 47 31f29baca131
parent 44 8b487ebf8d99
child 60 2c45d68844bf
equal deleted inserted replaced
46:de6761ce0d5b 47:31f29baca131
     1 #! /usr/bin/perl
     1 #! /usr/bin/perl
     2 
     2 
       
     3 use v5.10;
     3 use strict;
     4 use strict;
     4 use warnings;
     5 use warnings;
       
     6 use Pod::Usage;
     5 use File::Basename;
     7 use File::Basename;
     6 use FindBin;
     8 use FindBin;
       
     9 use Time::Local;
       
    10 use Getopt::Long;
     7 
    11 
     8 # liest die Konfiguration ein
       
     9 my @configs = ("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
       
    10 my %config;
    12 my %config;
       
    13 my $opt_expiry = undef;
    11 
    14 
    12 for (grep { -f } @configs) {
    15 MAIN: {
    13     open(CONFIG, $_) or die "Can't open $_: $!\n";
    16     my %info;    # will hold the information we collected
    14 }
       
    15 
    17 
    16 unless (seek(CONFIG, 0, 0)) {
    18     GetOptions(
    17     die "Can't open config (searched: @configs)\n";
    19         "e|expiry" => \$opt_expiry,
    18 }
    20         "h|help"   => sub { pod2usage(-exit => 0, -verbose => 1) },
       
    21         "m|man"    => sub {
       
    22             pod2usage(
       
    23                 -exit      => 0,
       
    24                 -verbose   => 2,
       
    25                 -noperldoc => system("perldoc -V &>/dev/null")
       
    26             );
       
    27         },
       
    28     ) or pod2usage;
    19 
    29 
    20 while (<CONFIG>) {
    30     {    # find and read/parse the config (could use some common config parser)
    21     chomp;
    31         my @configs = ("$FindBin::Bin/dnstools.conf", "/etc/dnstools.conf");
    22     s/#.*//;
    32         ($_) = grep { -f } @configs;
    23     s/\t//g;
    33         open(my $config, $_) or die "Can't open $_: $!\n";
    24     s/\s//g;
       
    25     next unless length;
       
    26     my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
       
    27     $config{$cname} = $ccont;
       
    28 }
       
    29 close(CONFIG);
       
    30 
    34 
    31 my $master_dir = $config{master_dir};
    35         while (<$config>) {
    32 my $zone;
    36             chomp;
    33 my ($info_zsk, $info_ksk, $info_kc, $info_end, $info_status);
    37             s/#.*//;
    34 
    38             s/\s//g;
    35 unless (-d $master_dir and -r $master_dir) {
    39             my ($k, $v) = split(/\s*=\s*/, $_, 2) or next;
    36     die "$master_dir: $!\n";
    40             $config{$k} = $v;
    37 }
       
    38 
       
    39 printf "%-35s %-8s %1s/%1s %3s %7s\n", "Domain", "Status", "ZSK", "KSK",
       
    40   "Used", "Sig-end";
       
    41 
       
    42 for my $dir (glob "$master_dir/*") {
       
    43 
       
    44     $zone = basename($dir);
       
    45 
       
    46     # prueft mit dig nach der zone
       
    47     $info_status = "OK";
       
    48     for (`dig \@localhost $zone`) {
       
    49         if (/root-servers/) {
       
    50             $info_status = "FAILED";
       
    51             last;
       
    52         }
    41         }
    53     }
    42     }
    54 
    43 
    55     if (not -f "$dir/.index.zsk") {
    44     die "$config{master_dir}: $!\n" if not -d $config{master_dir};
    56         $info_zsk = $info_ksk = $info_kc = 0;
    45 
    57         $info_end = "-";
    46     foreach my $dir (grep { -d } glob "$config{master_dir}/*") {
    58         next;
    47 
       
    48         my $zone = basename($dir);
       
    49         $info{$zone} = { status => "OK" };
       
    50 
       
    51         if (not -f "$dir/.index.zsk") {
       
    52             $info{$zone}{zsk}    = 0;
       
    53             $info{$zone}{ksk}    = 0;
       
    54             $info{$zone}{kc}     = 0;
       
    55             $info{$zone}{end}    = "-";
       
    56             $info{$zone}{expiry} = undef;
       
    57             next;
       
    58         }
       
    59 
       
    60         # prueft wie viele zsks genutzt werden
       
    61         {
       
    62             open(my ($fh), $_ = "<$dir/.index.zsk")
       
    63               or die "Can't open $_: $!\n";
       
    64             () = <$fh>;
       
    65             $info{$zone}{zsk} = $.
       
    66         }
       
    67 
       
    68         # prueft wie viele ksks genutzt werden
       
    69         {
       
    70             open(my ($fh), $_ = "<$dir/.index.ksk")
       
    71               or die "Can't open $_: $!\n";
       
    72             () = <$fh>;
       
    73             $info{$zone}{ksk} = $.
       
    74         }
       
    75 
       
    76         # prueft wie oft die schluessel zum signieren genutzt wurden
       
    77         {
       
    78             open(my ($fh), $_ = "<$dir/.keycounter")
       
    79               or die "Can't open $_: $!\n";
       
    80             chomp($info{$zone}{kc} = <$fh>);
       
    81         }
       
    82 
       
    83         # prueft das ablaufdatum
       
    84         if (!-f "$dir/$zone.signed") {
       
    85             $info{$zone}{end} = "-";
       
    86             next;
       
    87         }
       
    88 
       
    89         open(my ($fh), $_ = "<$dir/$zone.signed") or die "Can't open $_: $!\n";
       
    90         while (<$fh>) {
       
    91             next if not /RSIG.*SOA.*\s
       
    92 				(?<year>\d\d\d\d)
       
    93 				(?<mon>\d\d)
       
    94 				(?<day>\d\d)
       
    95 				(?<hour>\d\d)
       
    96 				(?<min>\d\d)\d+\s\(/ix;
       
    97             $info{$zone}{end} = "$+{day}.$+{mon}.$+{year} $+{hour}:$+{min}";
       
    98             $info{$zone}{expiry} =
       
    99               timelocal(0, $+{min}, $+{hour}, $+{day}, $+{mon} - 1, $+{year});
       
   100         }
    59     }
   101     }
    60 
   102 
    61     # prueft wie viele zsks genutzt werden
   103     {    # output
    62     close(FILE);
       
    63     open(FILE, $_ = "<$dir/.index.zsk") or die "Can't open $_: $!\n";
       
    64     () = <FILE>;
       
    65     $info_zsk = $.;
       
    66 
   104 
    67     # prueft wie viele ksks genutzt werden
   105         my $sort_by =
    68     close(FILE);
   106           $opt_expiry
    69     open(FILE, $_ = "<$dir/.index.ksk") or die "Can't open $_: $!\n";
   107           ? sub { ($info{$a}{expiry} // 2**64) <=> ($info{$b}{expiry} // 2**64) }
    70     () = <FILE>;
   108           : sub { $a cmp $b };
    71     $info_ksk = $.;
       
    72 
   109 
    73     # prueft wie oft die schluessel zum signieren genutzt wurden
   110         my $format_h = "%-35s %-8s %1s/%1s %3s %7s\n";
    74     open(FILE, $_ = "<$dir/.keycounter") or die "Can't open $_: $!\n";
   111         my $format_l = "%-35s %-8s %1d/%1d %5d %19s\n";
    75     chomp($info_kc = <FILE>);
       
    76 
   112 
    77     # prueft das ablaufdatum
   113         printf $format_h => qw(Domain Status ZSK KSK Used Sig-end);
    78     if (!-f "$dir/$zone.signed") {
   114 
    79         $info_end = "-";
   115         foreach my $zone (sort $sort_by keys %info) {
    80         next;
   116             printf $format_l => $zone,
       
   117               @{ $info{$zone} }{qw(status zsk ksk kc end)};
       
   118         }
    81     }
   119     }
    82 
       
    83     open(FILE, $_ = "<$dir/$zone.signed") or die "Can't open $_: $!\n";
       
    84     while (<FILE>) {
       
    85         $info_end = "$+{day}.$+{mon}.$+{year} $+{hour}:$+{min}"
       
    86           if /RSIG.*SOA.*\s
       
    87 			(?<year>\d\d\d\d)
       
    88 			(?<mon>\d\d)
       
    89 			(?<day>\d\d)
       
    90 			(?<hour>\d\d)
       
    91 			(?<min>\d\d)\d+\s\(/ix;
       
    92     }
       
    93 
       
    94 }
       
    95 continue {
       
    96     printf "%-35s %-8s %1d/%1d %5d %19s\n", $zone, $info_status, $info_zsk,
       
    97       $info_ksk, $info_kc,
       
    98       $info_end;
       
    99 }
   120 }
   100 
   121 
       
   122 __END__
       
   123 
       
   124 =head1 NAME
       
   125 
       
   126  zone-ls -- lists all zones
       
   127 
       
   128 =head1 SYNOPSIS
       
   129 
       
   130  zone-ls [-e|--expiry]
       
   131 
       
   132 =head1 DESCRIPTION
       
   133 
       
   134 This B<zone-ls> lists all zones under control of our dnstools suite. The output is ordered by domain name.
       
   135 
       
   136 =head1 OPTIONS
       
   137 
       
   138 =over
       
   139 
       
   140 =item B<-e>|B<--expiry>
       
   141 
       
   142 Order the output by expiry date. The sooner the key expires, the more top the
       
   143 domain is listed.
       
   144 
       
   145 =back
       
   146 
       
   147 Additionally the common B<-h>|B<--help>|B<-m>|B<--man> options, which should be
       
   148 self explanatory.
       
   149 
       
   150 =head1 AUTHORS
       
   151 
       
   152 L<andre.suess@pipkin.cc>
       
   153 
       
   154 =cut
       
   155 
       
   156 # vim:ts=4 sw=4 ai si aw: