summaryrefslogtreecommitdiffstats
path: root/contrib/dlz/modules/perl/testing/dlz_perl_example.pm
blob: 55bc388a7c48de4c51b04ab71c3c3d0c48da42dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#
# Copyright (C) 2009-2012  John Eaglesham
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND JOHN EAGLESHAM
# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# JOHN EAGLESHAM BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

package dlz_perl_example;

use warnings;
use strict;

use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

# Constructor. Everything after the class name can be folded into a hash of
# various options and settings. Right now only log_context and argv are
# available.
sub new {
    my ( $class, %config ) = @_;
    my $self = {};
    bless $self, $class;

    $self->{log} = sub {
        my ( $level, $msg ) = @_;
        DLZ_Perl::log( $config{log_context}, $level, $msg );
    };

    if ( $config{argv} ) { warn "Got argv: $config{argv}\n"; }

    $self->{zones} = {
        'example.com' => {
            '@' => [
                {
                    type => 'SOA',
                    ttl  => 86400,
                    data =>
                     'ns1.example.com. hostmaster.example.com. 12345 172800 900 1209600 3600',
                }
            ],
            perlrr => [
                {
                    type => 'A',
                    ttl  => 444,
                    data => '1.1.1.1',
                },
                {
                    type => 'A',
                    ttl  => 444,
                    data => '1.1.1.2',
                }
            ],
            perltime => [
                {
                    code => sub {
                        return ['TXT', '1', time()];
                    },
                },
            ],
            sourceip => [
                {
                    code => sub {
                        my ( $opaque ) = @_;
                        # Passing anything other than the proper opaque value,
                        # 0, or undef to this function will cause a crash (at
                        # best!).
                        my ( $addr, $port ) =
                         DLZ_Perl::clientinfo::sourceip( $opaque );
                        if ( !$addr ) { $addr = $port = 'unknown'; }
                        return ['TXT', '1', $addr], ['TXT', '1', $port];
                    },
                },
            ],
        },
    };

    $self->{log}->(
        DLZ_Perl::LOG_INFO(),
        'DLZ Perl Script: Called init. Loaded zone data: '
         . Dumper( $self->{zones} )
    );
    return $self;
}

# Do we have data for this zone? Expects a simple true or false return value.
sub findzone {
    my ( $self, $zone ) = @_;
    $self->{log}->(
        DLZ_Perl::LOG_INFO(),
        "DLZ Perl Script: Called findzone, looking for zone $zone"
    );

    return exists $self->{zones}->{$zone};
}

# Return the data for a given record in a given zone. The final parameter is
# an opaque value that can be passed to DLZ_Perl::clientinfo::sourceip to
# retrieve the client source IP and port. Expected return value is an array
# of array refs, with each array ref representing one record and containing
# the type, ttl, and data in that order. Data is as it appears in a zone file.
sub lookup {
    my ( $self, $name, $zone, $client_info ) = @_;
    $self->{log}->(
        DLZ_Perl::LOG_INFO(),
        "DLZ Perl Script: Called lookup, looking for record $name in zone $zone"
    );
    return unless $self->{zones}->{$zone}->{$name};

    my @results;
    foreach my $rr ( @{ $self->{zones}->{$zone}->{$name} } ) {
        if ( $rr->{'code'} ) {
            my @r = $rr->{'code'}->( $client_info );
            if ( @r ) {
                push @results, @r;
            }
        } else {
            push @results, [$rr->{'type'}, $rr->{'ttl'}, $rr->{'data'}];
        }
    }

    return @results;
}

# Will we allow zone transfer for this client? Expects a simple true or false
# return value.
sub allowzonexfr {
    my ( $self, $zone, $client ) = @_;
    $self->{log}->(
        DLZ_Perl::LOG_INFO(),
        "DLZ Perl Script: Called allowzonexfr, looking for zone $zone for " .
        "client $client"
    );
    if ( $client eq '127.0.0.1' ) { return 1; }
    return 0;
}

# Note the return AoA for this method differs from lookup in that it must
# return the name of the record as well as the other data.
sub allnodes {
    my ( $self, $zone ) = @_;
    my @results;
    $self->{log}->(
        DLZ_Perl::LOG_INFO(),
        "DLZ Perl Script: Called allnodes, looking for zone $zone"
    );

    foreach my $name ( keys %{ $self->{zones}->{$zone} } ) {
        foreach my $rr ( @{ $self->{zones}->{$zone}->{$name} } ) {
            if ( $rr->{'code'} ) {
                my @r = $rr->{'code'}->();
                # The code returns an array of array refs without the name.
                # This makes things easy for lookup but hard here. We must
                # iterate over each array ref and inject the name into it.
                foreach my $a ( @r ) {
                    unshift @{$a}, $name;
                }
                push @results, @r;
            } else {
                push @results,
                 [$name, $rr->{'type'}, $rr->{'ttl'}, $rr->{'data'}];
            }
        }
    }
    return @results;
}

1;