named_makeconf.in 9.09 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
4
# Copyright (c) 2000-2014 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#
use Getopt::Std;
use Socket;

#
# named_makeconf - helper script to create named.conf file. The template
# looks like standard file, only with %token% strings that need to be
# replaced. See below.
#
sub usage {
	print "Usage: $0 <templatefile>\n";
	print "Spits out a named.conf file on stdout, as in:\n";
	print "$0 named.conf.template > named.conf\n";
}

#
# Configure variables
#
42 43 44 45 46 47 48 49 50
my $TESTBED_NETWORK          = "@TESTBED_NETWORK@";
my $TESTBED_NETMASK          = "@TESTBED_NETMASK@";
my $EXTERNAL_TESTBED_NETWORK = "@EXTERNAL_TESTBED_NETWORK@";
my $EXTERNAL_TESTBED_NETMASK = "@EXTERNAL_TESTBED_NETMASK@";
my $BOSSNODE_IP              = "@BOSSNODE_IP@";
my $USERNODE_IP              = "@USERNODE_IP@";
my $EXTERNAL_BOSSNODE_IP     = "@EXTERNAL_BOSSNODE_IP@";
my $EXTERNAL_USERNODE_IP     = "@EXTERNAL_USERNODE_IP@";

51 52 53 54 55 56 57 58 59
my $CONTROL_ROUTER_IP  = "@CONTROL_ROUTER_IP@";
my $CONTROL_NETWORK    = "@CONTROL_NETWORK@";
my $CONTROL_NETMASK    = "@CONTROL_NETMASK@";
my $PRIVATE_NETWORK    = "@PRIVATE_NETWORK@";
my $PRIVATE_ROUTER     = "@PRIVATE_ROUTER@";
my $PRIVATE_NETMASK    = "@PRIVATE_NETMASK@";
my $PUBLIC_NETWORK     = "@PUBLIC_NETWORK@";
my $PUBLIC_ROUTER      = "@PUBLIC_ROUTER@";
my $PUBLIC_NETMASK     = "@PUBLIC_NETMASK@";
60
my $NAMED_FORWARDERS   = "@NAMED_FORWARDERS@";
61 62
my $VIRTNODE_NETWORK   = "@VIRTNODE_NETWORK@";
my $VIRTNODE_NETMASK   = "@VIRTNODE_NETMASK@";
63
my $NAMED_ALSONOTIFY   = "@NAMED_ALSONOTIFY@";
64 65 66 67 68 69 70 71 72 73 74
my $IPV6_ENABLED       = @IPV6_ENABLED@;
my $IPV6_SUBNET_PREFIX = "@IPV6_SUBNET_PREFIX@";
my $IPV6_MAPFILE       = "";
if ($IPV6_ENABLED) {
    $IPV6_MAPFILE = $IPV6_SUBNET_PREFIX . ".db";    
    # Switch : to dot since colon causes gmake to act strange.
    $IPV6_MAPFILE =~ s/\:/./g;
}

# Protos
sub ReverseDottedIPV6($);
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

# Testbed libraries.
use lib "@prefix@/lib";

# Simple is good. I stole this out of a google search.
my @NETMASKS =
    (0x10000000,                                             # 0
     0x80000000, 0xC0000000, 0xE0000000, 0xF0000000,         #  1 -  4
     0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000,         #  5 -  8
     0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000,         #  9 - 12
     0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000,         # 13 - 16
     0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000,         # 17 - 20
     0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00,         # 21 - 24
     0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0,         # 25 - 28
     0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF          # 29 - 32
);

my $TESTBED_SLASH_NETMASK = "24";
for (my $i = 0; $i < scalar(@NETMASKS); $i++) {
    my $foo = pack("N", $NETMASKS[$i]);

    if ($TESTBED_NETMASK eq inet_ntoa($foo)) {
	$TESTBED_SLASH_NETMASK = "$i";
	last;
    }
}
101 102 103 104 105 106 107 108 109
my $EXTERNAL_TESTBED_SLASH_NETMASK = "24";
for (my $i = 0; $i < scalar(@NETMASKS); $i++) {
    my $foo = pack("N", $NETMASKS[$i]);

    if ($EXTERNAL_TESTBED_NETMASK eq inet_ntoa($foo)) {
	$EXTERNAL_TESTBED_SLASH_NETMASK = "$i";
	last;
    }
}
110 111 112 113 114 115 116 117 118
my $VIRTNODE_SLASH_NETMASK = "24";
for (my $i = 0; $i < scalar(@NETMASKS); $i++) {
    my $foo = pack("N", $NETMASKS[$i]);

    if ($VIRTNODE_NETMASK eq inet_ntoa($foo)) {
	$VIRTNODE_SLASH_NETMASK = "$i";
	last;
    }
}
119 120 121

# This is for the dotted and reverse dotted notation needed for the
# reverse maps. This is probably all wrong ... 
122 123 124 125
my ($ia, $ib, $ic, $id) =
    ($TESTBED_NETWORK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
my ($ea, $eb, $ec, $ed) =
    ($EXTERNAL_TESTBED_NETWORK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
126 127
my ($ja, $jb, $jc, $jd) =
    ($VIRTNODE_NETWORK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
128

129 130 131 132
my ($ima, $imb, $imc, $imd) =
    ($TESTBED_NETMASK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
my ($ema, $emb, $emc, $emd) =
    ($EXTERNAL_TESTBED_NETMASK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
133 134
my ($jma, $jmb, $jmc, $kmd) =
    ($VIRTNODE_NETMASK =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/);
135

136 137 138 139 140 141 142 143 144 145
$infile = shift @ARGV || exit &usage;

open(IF,"<$infile") or
    die("Unable to open $infile for reading\n");

while (<IF>) {
	if (/^(.*)\%([-\w]*)\%(.*)$/) {
	    if ($2 eq "internal_network_withmask") {
		print $1 . "${TESTBED_NETWORK}/${TESTBED_SLASH_NETMASK}" ."$3\n";
	    }
146 147 148
	    elsif ($2 eq "virtnode_network_withmask") {
		print $1 . "${VIRTNODE_NETWORK}/${VIRTNODE_SLASH_NETMASK}" ."$3\n";
	    }
149
	    elsif ($2 eq "internal_network_dotted") {
150
		print $1 . "${ia}.${ib}.${ic}" . "$3\n";
151 152
	    }
	    elsif ($2 eq "internal_network_dotted_reverse") {
153 154 155 156 157 158 159
		print $1 . "${ic}.${ib}.${ia}" . "$3\n";
	    }
	    elsif ($2 eq "external_network_dotted") {
		print $1 . "${ea}.${eb}.${ec}" . "$3\n";
	    }
	    elsif ($2 eq "external_network_dotted_reverse") {
		print $1 . "${ec}.${eb}.${ea}" . "$3\n";
160
	    }
161 162 163 164 165 166
	    elsif ($2 eq "forwarders") {
		if ($NAMED_FORWARDERS eq "") {
		    print "";
		}
		else {
		    print $1 . "forwarders {\n";
167
		    foreach my $forwarder (split(/[\s+,]/, $NAMED_FORWARDERS)) {
168 169 170 171 172
			print $1 . $1 . "$forwarder;\n";
		    }
		    print $1 . "};\n";
		}
	    }
173 174 175 176 177 178 179 180 181 182 183 184
	    elsif ($2 eq "alsonotify") {
		if ($NAMED_ALSONOTIFY eq "") {
		    print "";
		}
		else {
		    print $1 . "also-notify {\n";
		    foreach my $notify (split(" ", $NAMED_ALSONOTIFY)) {
			print $1 . $1 . "$notify;\n";
		    }
		    print $1 . "};\n";
		}
	    }
185
	    elsif ($2 eq "internal_network_reverse_zones") {
186 187 188 189 190 191 192 193
		if ($IPV6_ENABLED) {
		    my $dotted_reverse = ReverseDottedIPV6($IPV6_SUBNET_PREFIX);

		    print $1 . "zone \"${dotted_reverse}.ip6.arpa\" in {\n";
		    print $1 . "\ttype master;\n";
		    print $1 . "\tfile \"reverse/${IPV6_MAPFILE}\";\n";
		    print $1 . "};\n\n";
		}
194 195 196 197 198 199 200 201 202
		for (my $i = $ic; $i <= $ic + (255 - $imc); $i++) {
		    my $dotted           = "${ia}.${ib}.${i}";
		    my $dotted_reverse   = "${i}.${ib}.${ia}";

		    print $1 . "zone \"${dotted_reverse}.in-addr.arpa\" in {\n";
		    print $1 . "\ttype master;\n";
		    print $1 . "\tfile \"reverse/${dotted}.db\";\n";
		    print $1 . "};\n";		    
		}
203 204 205 206 207 208 209 210 211
		for (my $i = $jb; $i <= $jb + (255 - $jmb); $i++) {
		    my $dotted           = "${ja}.${i}";
		    my $dotted_reverse   = "${i}.${ja}";

		    print $1 . "zone \"${dotted_reverse}.in-addr.arpa\" in {\n";
		    print $1 . "\ttype master;\n";
		    print $1 . "\tfile \"reverse/${dotted}.db\";\n";
		    print $1 . "};\n";
		}
212 213
	    }
	    elsif ($2 eq "external_network_reverse_zones") {
214 215 216 217 218 219 220 221
		if ($IPV6_ENABLED) {
		    my $dotted_reverse = ReverseDottedIPV6($IPV6_SUBNET_PREFIX);

		    print $1 . "zone \"${dotted_reverse}.ip6.arpa\" in {\n";
		    print $1 . "\ttype master;\n";
		    print $1 . "\tfile \"reverse/${IPV6_MAPFILE}\";\n";
		    print $1 . "};\n\n";
		}
222 223 224 225 226 227 228 229 230 231
		for (my $i = $ec; $i <= $ec + (255 - $emc); $i++) {
		    my $dotted           = "${ea}.${eb}.${i}";
		    my $dotted_reverse   = "${i}.${eb}.${ea}";

		    print $1 . "zone \"${dotted_reverse}.in-addr.arpa\" in {\n";
		    print $1 . "\ttype master;\n";
		    print $1 . "\tfile \"reverse/${dotted}.db\";\n";
		    print $1 . "};\n";		    
		}
	    }
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	    elsif ($2 eq "internal_network_extra_stuff") {
		if (-e "/etc/namedb/internal.named.conf") {
		    open(EXTRA, "/etc/namedb/internal.named.conf") or
			die("*** $0:\n".
			    "    Could not open internal.named.conf!\n");
		    print $1 . "#\n";
		    print $1 . "# Fragment included from internal.named.conf\n";
		    print $1 . "#\n";
		    while (<EXTRA>) {
			print $1 . $_;
		    }
		    close(EXTRA);
		    print $1 .
			"# End of fragment included from internal.named.conf\n";
		}
	    }
	    elsif ($2 eq "external_network_extra_stuff") {
		if (-e "/etc/namedb/external.named.conf") {
		    open(EXTRA, "/etc/namedb/external.named.conf") or
			die("*** $0:\n".
			    "    Could not open external.named.conf!\n");
		    print $1 . "#\n";
		    print $1 . "# Fragment included from external.named.conf\n";
		    print $1 . "#\n";
		    while (<EXTRA>) {
			print $1 . $_;
		    }
		    close(EXTRA);
		    print $1 .
			"# End of fragment included from external.named.conf\n";
		}
	    }
264 265 266 267 268 269 270 271 272 273 274 275
	    else {
		die("*** $0:\n".
		    "    Unknown special tag in named template file!\n");
	    }
	}
	else {
	    # It's a regular line
	    print;
	}
}
close(IF);
exit(0);
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298

#
# Helper to form a dotted reverse for IPv6
#
sub ReverseDottedIPV6($)
{
    my ($ipv6) = @_;
    my @digits = ();

    foreach my $nibblet (split(":", $ipv6)) {
	if ($nibblet =~ /^(\w)(\w)(\w)(\w)$/) {
	    push(@digits, $1, $2, $3, $4);
	}
	else {
	    die("*** $0:\n".
		"    Bad format for ipv6 address: $IPV6_SUBNET_PREFIX!\n");
	}
    }
    my $dotted = join(".", reverse(@digits));
    return $dotted;
}