]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/build-unbound-localzone-from-hosts.pl
Vendor import of Unbound 1.13.0.
[FreeBSD/FreeBSD.git] / contrib / build-unbound-localzone-from-hosts.pl
1 #!/usr/bin/perl -WT
2
3 use strict;
4 use warnings;
5
6 my $hostsfile = '/etc/hosts';
7 my $localzonefile = '/etc/unbound/localzone.conf.new';
8
9 my $localzone = 'example.com';
10
11 open( HOSTS,"<${hostsfile}" ) or die( "Could not open ${hostsfile}: $!" );
12 open( ZONE,">${localzonefile}" ) or die( "Could not open ${localzonefile}: $!" );
13
14 print ZONE "server:\n\n";
15 print ZONE "local-zone: \"${localzone}\" transparent\n\n";
16
17 my %ptrhash;
18
19 while ( my $hostline = <HOSTS> ) {
20
21         # Skip comments
22         if ( $hostline !~ "^#" and $hostline !~ '^\s+$' ) {
23
24                 my @entries = split( /\s+/, $hostline );
25
26                 my $ip;
27
28                 my $count = 0;
29                 foreach my $entry ( @entries ) {
30                         if ( $count == 0 ) {
31                                 $ip = $entry;
32                         } else {
33
34                                 if ( $count == 1) {
35
36                                         # Only return localhost for 127.0.0.1 and ::1
37                                         if ( ($ip ne '127.0.0.1' and $ip ne '::1') or $entry =~ 'localhost' ) {
38                                                 if ( ! defined $ptrhash{$ip} ) {
39                                                         $ptrhash{$ip} = $entry;
40                                                         print ZONE "local-data-ptr: \"$ip $entry\"\n";
41                                                 }
42                                         }
43
44                                 }
45
46                                 # Use AAAA for IPv6 addresses
47                                 my $a = 'A';
48                                 if ( $ip =~ ':' ) {
49                                         $a = 'AAAA';
50                                 }
51
52                                 print ZONE "local-data: \"$entry ${a} $ip\"\n";
53
54                         }
55                         $count++;
56                 }
57                 print ZONE "\n";
58
59
60         }
61 }
62
63
64
65
66 __END__
67