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