xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/rpzrecurse/testgen.pl (revision 8aaca124c0ad52af9550477f296b63debc7b4c98)
1#!/usr/bin/env perl
2
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# This Source Code Form is subject to the terms of the Mozilla Public
8# License, v. 2.0.  If a copy of the MPL was not distributed with this
9# file, you can obtain one at https://mozilla.org/MPL/2.0/.
10#
11# See the COPYRIGHT file distributed with this work for additional
12# information regarding copyright ownership.
13
14use strict;
15use warnings;
16
17my $boilerplate_header = <<'EOB';
18# common configuration
19include "named.conf.header";
20
21view "recursive" {
22    zone "." {
23        type hint;
24        file "root.hint";
25    };
26
27    # policy configuration to be tested
28    response-policy {
29EOB
30
31my $no_option = <<'EOB';
32    } nsdname-enable yes nsip-enable yes;
33
34    # policy zones to be tested
35EOB
36
37my $qname_wait_recurse = <<'EOB';
38    } nsdname-enable yes nsip-enable yes qname-wait-recurse no;
39
40    # policy zones to be tested
41EOB
42
43my $boilerplate_end = <<'EOB';
44};
45EOB
46
47my $policy_option = $qname_wait_recurse;
48
49my $serialnum = "1";
50my $policy_zone_header = <<'EOH';
51$TTL 60
52@ IN SOA root.ns ns SERIAL 3600 1800 86400 60
53     NS ns
54ns A 127.0.0.1
55EOH
56
57sub policy_client_ip {
58    return "32.1.0.0.127.rpz-client-ip CNAME .\n";
59}
60
61sub policy_qname {
62    my $query_nbr = shift;
63    return sprintf "q%02d.l2.l1.l0 CNAME .\n", $query_nbr;
64}
65
66sub policy_ip {
67    return "32.255.255.255.255.rpz-ip CNAME .\n";
68}
69
70sub policy_nsdname {
71    return "ns.example.org.rpz-nsdname CNAME .\n";
72}
73
74sub policy_nsip {
75    return "32.255.255.255.255.rpz-ip CNAME .\n";
76}
77
78my %static_triggers = (
79    'client-ip' => \&policy_client_ip,
80    'ip'        => \&policy_ip,
81    'nsdname'   => \&policy_nsdname,
82    'nsip'      => \&policy_nsip,
83);
84
85sub mkconf {
86    my $case_id = shift;
87    my $n_queries = shift;
88
89    { # generate the query list
90        my $query_list_filename = "ns2/$case_id.queries";
91        my $query_list_fh;
92
93        open $query_list_fh, ">$query_list_filename" or die;
94
95        for( my $i = 1; $i <= $n_queries; $i++ ) {
96            print $query_list_fh sprintf "q%02d.l2.l1.l0\n", $i;
97        }
98    }
99
100    my @zones;
101
102    { # generate the conf file
103        my $conf_filename = "ns2/named.$case_id.conf";
104
105        my $conf_fh;
106
107        open $conf_fh, ">$conf_filename" or die;
108
109        print $conf_fh $boilerplate_header;
110
111        my $zone_seq = 0;
112
113        @zones = map {
114            [
115                sprintf( "$case_id.%02d.policy.local", $zone_seq++ ),
116                $_,
117            ];
118        } @_;
119
120        print $conf_fh map { qq{        zone "$_->[0]";\n} } @zones;
121
122        print $conf_fh $policy_option;
123
124        print $conf_fh map { qq{    zone "$_->[0]" { type primary; file "db.$_->[0]"; };\n} } @zones;
125
126        print $conf_fh $boilerplate_end;
127    }
128
129    # generate the policy zone contents
130    foreach my $policy_zone_info( @zones ) {
131        my $policy_zone_name = $policy_zone_info->[0];
132        my $policy_zone_contents = $policy_zone_info->[1];
133
134        my $policy_zone_filename = "ns2/db.$policy_zone_name";
135        my $policy_zone_fh;
136
137        open $policy_zone_fh, ">$policy_zone_filename" or die;
138
139        my $header = $policy_zone_header;
140        $header =~ s/SERIAL/$serialnum/;
141        print $policy_zone_fh $header;
142
143        foreach my $trigger( @$policy_zone_contents ) {
144            if( exists $static_triggers{$trigger} ) {
145                # matches a trigger type with a static value
146                print $policy_zone_fh $static_triggers{$trigger}->();
147            }
148            else {
149                # a qname trigger, where what was specified is the query number it should match
150                print $policy_zone_fh policy_qname( $trigger );
151            }
152        }
153    }
154}
155
156mkconf(
157    '1a',
158    1,
159    [ 'client-ip' ],
160);
161
162mkconf(
163    '1b',
164    2,
165    [ 1 ],
166);
167
168mkconf(
169    '1c',
170    1,
171    [ 'client-ip', 2 ],
172);
173
174mkconf(
175    '2a',
176    33,
177    map { [ $_ ]; }  1 .. 32
178);
179
180mkconf(
181    '3a',
182    1,
183    [ 'ip' ],
184);
185
186mkconf(
187    '3b',
188    1,
189    [ 'nsdname' ],
190);
191
192mkconf(
193    '3c',
194    1,
195    [ 'nsip' ],
196);
197
198mkconf(
199    '3d',
200    2,
201    [ 'ip', 1 ]
202);
203
204mkconf(
205    '3e',
206    2,
207    [ 'nsdname', 1 ]
208);
209
210mkconf(
211    '3f',
212    2,
213    [ 'nsip', 1 ]
214);
215
216{
217    my $seq_code = 'aa';
218    my $seq_nbr = 0;
219
220    while( $seq_nbr < 32 ) {
221
222        mkconf(
223            "4$seq_code",
224            33,
225            ( map { [ $_ ]; } 1 .. $seq_nbr ),
226            [ 'ip', $seq_nbr + 2 ],
227            ( map { [ $_ + 2 ]; } ($seq_nbr + 1) .. 31 ),
228        );
229
230        $seq_code++;
231        $seq_nbr++;
232    }
233}
234
235mkconf(
236    '5a',
237    6,
238    [ 1 ],
239    [ 2, 'ip' ],
240    [ 4 ],
241    [ 5, 'ip' ],
242    [ 6 ],
243);
244
245$policy_option = $no_option;
246
247mkconf(
248    '6a',
249    0,
250    [ ],
251);
252
253$serialnum = "2";
254mkconf(
255    '6b',
256    0,
257    [ 'nsdname' ],
258);
259
260$serialnum = "3";
261mkconf(
262    '6c',
263    0,
264    [ ],
265);
266
267__END__
268
2690x01 - has client-ip
270    32.1.0.0.127.rpz-client-ip CNAME .
2710x02 - has qname
272    qX.l2.l1.l0 CNAME .
2730x10 - has ip
274    32.255.255.255.255.rpz-ip CNAME .
2750x20 - has nsdname
276    ns.example.org.rpz-nsdname CNAME .
2770x40 - has nsip
278    32.255.255.255.255.rpz-nsip CNAME .
279
280$case.$seq.policy.local
281
282case 1a = 0x01
283    .q01 = (00,0x01)=-r
284case 1b = 0x02
285    .q01 = (00,0x02)=-r
286    .q02 = (--,----)=+r
287case 1c = 0x03
288    .q01 = (00,0x01)=-r
289
290case 2a = 0x03{32}
291    .q01 = (00,0x02)=-r
292    .q02 = (01,0x02)=-r
293     ...
294    .q31 = (30,0x02)=-r
295    .q32 = (31,0x02)=-r
296    .q33 = (--,----)=+r
297
298case 3a = 0x10
299    .q01 = (00,0x10)=+r
300case 3b = 0x20
301    .q01 = (00,0x20)=+r
302case 3c = 0x40
303    .q01 = (00,0x40)=+r
304case 3d = 0x12
305    .q01 = (00,0x10)=+r
306    .q02 = (00,0x02)=-r
307case 3e = 0x22
308    .q01 = (00,0x20)=+r
309    .q02 = (00,0x02)=-r
310case 3f = 0x42
311    .q01 = (00,0x40)=+r
312    .q02 = (00,0x02)=-r
313
314case 4aa = 0x12,0x02{31}
315    .q01 = (00,0x10)=+r
316    .q02 = (00,0x02)=-r
317    .q03 = (01,0x02)=+r
318     ...
319    .q32 = (30,0x02)=+r
320    .q33 = (31,0x02)=+r
321case 4__ = 0x02{n(1->30)},0x12,0x02{31-n}
322    .q01 = (00,0x02)=-r
323     ...
324    .q(n+1) = (n,0x10)=+r
325    .q(n+2) = (n,0x02)=-r
326     ...
327    .q33 = (31,0x02)=+r
328case 4bf = 0x02{31},0x12
329    .q01 = (00,0x02)=-r
330    .q02 = (01,0x02)=-r
331     ...
332    .q31 = (30,0x02)=-r
333    .q32 = (31,0x10)=+r
334    .q33 = (31,0x02)=-r
335
336case 5a = 0x02,0x12,0x02,0x12,0x02
337    .q01 = (00,0x02)=-r
338    .q02 = (01,0x02)=-r
339    .q03 = (01,0x10)=+r
340    .q04 = (02,0x02)=+r
341    .q05 = (03,0x02)=+r
342    .q06 = (04,0x02)=+r
343
344