xref: /freebsd/contrib/unbound/contrib/build-unbound-localzone-from-hosts.pl (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1#!/usr/bin/perl -WT
2
3use strict;
4use warnings;
5
6my $hostsfile = '/etc/hosts';
7my $localzonefile = '/etc/unbound/localzone.conf.new';
8
9my $localzone = 'example.com';
10
11open( HOSTS,"<${hostsfile}" ) or die( "Could not open ${hostsfile}: $!" );
12open( ZONE,">${localzonefile}" ) or die( "Could not open ${localzonefile}: $!" );
13
14print ZONE "server:\n\n";
15print ZONE "local-zone: \"${localzone}\" transparent\n\n";
16
17my %ptrhash;
18
19while ( 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
68