#! /usr/bin/perl -w

# vim:syntax=perl

use strict;
use lib '/usr/share/perl5';
use Lire::DlfSchema;
use Lire::Program qw( :msg :dlf );
use Lire::Syslog;

init_dlf_converter( "dnszone" );

my $schema  = eval { Lire::DlfSchema::load_schema( "dnszone" ) };
lr_err( "failed to load dnszone schema: $@" ) if $@;
my $dlf_maker   = 
  $schema->make_hashref2asciidlf_func( qw/server time axfr_host axfr_zone axfr_what loaded_zone loaded_serial loaded_role updatedenied_zone updatedenied_host/ );

my $lines       = 0;
my $dlflines    = 0;
my $errorlines  = 0;
my $syslog_parser = new Lire::Syslog;

sub print_dlf {
    my $dlf=$dlf_maker->($_[0]);
    print join(" ",@$dlf),"\n";
    $dlflines++;
}

my $line;
my @ltime=localtime;
while (<>) {
    lire_chomp();
    $lines++;

    my %dlf=();

    my $rec = eval { $syslog_parser->parse( $_ ) };
    if ($@) {
	lr_warn( "line $. is an invalid syslog message: $@" );
	$errorlines++;
    }
    next unless defined $rec->{process} && $rec->{process} eq 'named';

    my $line = $rec->{content};
    $dlf{time}	    = $rec->{timestamp};
    $dlf{server}    = $rec->{hostname};

    # approved AXFR from [10.10.10.1].2761 for "estc.net.au"
    # denied AXFR from [203.173.151.36].3430 for "blaat" (not master/slave)
    if ($line=~/(.+?) AXFR from \[([\d\.]+)\]\.\d+ for "(.+?)"/) {
	$dlf{axfr_what}=$1;
	$dlf{axfr_host}=$2;
	$dlf{axfr_zone}=$3;

	print_dlf(\%dlf);
	next;
    }

    # zone transfer (AXFR) of "sixteen.net.au" (IN) to [10.10.10.1].2713
    if ($line=~/zone transfer \(AXFR\) of "(.+?)" \(IN\) to \[([\d\.]+)\]\.\d+/) {
	$dlf{axfr_what}="axfr";
	$dlf{axfr_zone}=$1;
	$dlf{axfr_host}=$2;

	print_dlf(\%dlf);
	next;
    }

    # master zone "0.0.127.IN-ADDR.ARPA" (IN) loaded (serial 2002071300)
    # slave zone "92.185.218.in-addr.arpa" (IN) loaded (serial 2002071500)
    if ($line=~/: (.+?) zone "(.+?)" \(IN\) loaded \(serial (\d+)\)/) {
	$dlf{loaded_role}=$1;
	$dlf{loaded_zone}=$2;
	$dlf{loaded_serial}=$3;

	my @w=split(" ",$line);
	$dlf{server}=$w[3];
	$dlf{time}=syslog2cal($w[0],$w[1],$w[2],\@ltime);

	print_dlf(\%dlf);
	next;
    }

    # denied update from [10.193.1.129].2267 for "10.in-addr.arpa" IN
    if ($line=~/denied update from \[([\d\.]+)\].\d+ for "(.+?)"/) {
	$dlf{updatedenied_zone}=$1;
	$dlf{updatedenied_host}=$2;

	print_dlf(\%dlf);
	next;
    }
}

end_dlf_converter( $lines, $dlflines, $errorlines );

__END__

=pod

=head1 NAME

bind8_named2dlf - convert BIND8 named logs into dlf format

=head1 SYNOPSIS

B<bind8_named2dlf>

=head1 DESCRIPTION

This script converts each line in a BIND8 named log to a dnszone dlf record.
It is tested using Bind 8.2 logs.  We're not aware of any changes in the
logging format in any Bind 8.* version, we expect any bind 8.* log to get
processed fine by this script.

The following lines are recognized:

=over 4

=item *

approved/denied AXFR requests

=item *

completed zone transfers

=item *

loaded master and slave zones

=item *

denied dynamic DNS updates 

=back

=head1 DLF format

The DLF format of the dnszone superservice is

=over 4

=item server

Name of the DNS server

=item time

Timestamp for the request

=item axfr_host

IP address of the host requesting the AXFR

=item axfr_zone

Name of the zone being requested

=item axfr_what

approved, denied or axfr

=item loaded_zone

Name of the zone just loaded

=item loaded_serial

Serial of the zone just loaded

=item loaded_role

master or slave

=item updatedenied_host

IP address of the host requesting the update

=item updatedenied_zone

Name of the zone being updated

=back

Due to the nature of the different meanings of the lines in the
logfile (not every line has both a AXFR-request, loaded a zone and
gotten an update request), one or more fields can be replaced by a
0 or a -. These fields are automaticly filtered out for the different
kind of reports.

Example of DLF lines are:

=over 4

=item AXFR approved

tim 1026644876 10.10.10.1 algron.com approved - 0 - - -

=item update denied

tim 1026645567 - - - - 0 - 10.in-addr.arpa 10.199.16.218

=item master zone loaded

tim 1026525210 - - - nigelbowen.com.au 2002071301 master - -

=back

=head1 EXAMPLES

To process a log as produced by bind8:

 $ bind8_named2dlf < named-log

bind8_named2dlf will be rarely used on its own, but is more likely
called by lr_log2report:

 $ lr_log2report bind8_named < /var/log/named

=head1 SEE ALSO

bind8-query2dlf(1) bind9-query2dlf(1)

=head1 VERSION

$Id: bind8_named2dlf.in,v 1.7 2004/10/13 10:17:27 vanbaal Exp $

=head1 COPYRIGHT

Copyright (C) 2002 Edwin Groothuis <edwin@mavetju.org>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. The name of the copyright holder may not be used to endorse or promote
products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

=head1 AUTHOR

Edwin Groothuis <edwin@mavetju.org>

=cut

# Local Variables:
# mode: cperl
# End:

