named_setup.in 19.4 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
#
3
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
#
24
use English;
25
use Socket;
26
use strict;
27 28 29 30

#
# Suck out virtual names and create CNAME map entries.
#
31 32
# This script always does the right thing, so it does not matter who calls it. 
#
33
# usage: named_setup [-norestart]
34 35
#

36 37 38 39 40 41 42
#
# Function phototypes
#

sub assemble_zonefile($);
sub make_forward_zonefile($$$);
sub isroutable($);
43
sub IsJailIP($);
44
sub process_nodes($);
45
sub make_ipv6_reverse_zonefile($$);
46

47 48 49 50
#
# Configure variables
#
my $TB		= "@prefix@";
51
my $TBOPS       = "@TBOPSEMAIL@";
52
my $USERS	= "@USERNODE@";
53
my $DISABLED    = "@DISABLE_NAMED_SETUP@";
54
my $OURDOMAIN   = "@OURDOMAIN@";
55 56
my $VIRTNODE_NETWORK   = "@VIRTNODE_NETWORK@";
my $VIRTNODE_NETMASK   = "@VIRTNODE_NETMASK@";
57 58 59
my $IPV6_ENABLED       = @IPV6_ENABLED@;
my $IPV6_SUBNET_PREFIX = "@IPV6_SUBNET_PREFIX@";
		  
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
my $mapdir			= "/etc/namedb";
61
my $mapfile			= "${mapdir}/${OURDOMAIN}.db";
62 63 64
my $mapfiletail			= "$mapfile.tail";
my $mapfile_internal		= "$mapdir/${OURDOMAIN}.internal.db";
my $mapfile_internal_head	= "$mapfile_internal.head";
65
my $mapfile_internal_tail	= "$mapfile_internal.tail";
66 67 68
my $vnodesfile			= "$mapdir/vnodes.${OURDOMAIN}.db";
my $vnodesback 			= "$mapdir/vnodes.${OURDOMAIN}.db.backup";
my $reversedir			= "$mapdir/reverse";
69 70 71 72 73
my $reverse_ipv6		= "$reversedir/${IPV6_SUBNET_PREFIX}.db";
# Switch colon to dot since colon causes gmake to act strange.
$reverse_ipv6 =~ s/\:/./g;
my $reverse_ipv6_head		= "${reverse_ipv6}.head";
my $reverse_ipv6_tail		= "${reverse_ipv6}.tail";
74
my $restart_named		= 1;
75
my $sortem			= 0; # set to 1 to generated IP-sorted file
76
my $dbg	= 0;
77
my $domx = 0;
78 79
my @row;

80
# If we are disabled, just quietly exit
81 82 83 84
if ($DISABLED) {
    exit 0;
}

85
# We do not want to run this script unless its the real version.
86
if ($EUID != 0) {
87 88
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
89 90
}
# XXX Hacky!
91
if (0 && $TB ne "/usr/testbed") {
92 93
    die("*** $0:\n".
	"    Wrong version. Maybe its a development version?\n");
94 95
}

96 97 98 99
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

100 101
# Turn off line buffering on output
$| = 1;
102

103
# Testbed Support libraries
104 105 106
use lib "@prefix@/lib";
use libtestbed;
use libdb;
107

108 109 110 111 112 113 114
#
# Check for norestart option.
#
if (@ARGV && $ARGV[0] eq "-norestart") {
    $restart_named = 0;
}

115
#
116
# We need to serialize this script to avoid a trashed map file.
117
#
118 119 120 121
if ((my $locked = TBScriptLock("named", 1)) != TBSCRIPTLOCK_OKAY()) {
    exit(0)
        if ($locked == TBSCRIPTLOCK_IGNORE);
    fatal("Could not get the lock after a long time!\n");
122

123 124 125
}

#
126
# Grab the list of all nodes with proper control network interfaces.
127
#
128
my $db_result =
129
    DBQueryFatal("select n.node_id, n.role, null, i.IP, i.role, ".
130
                 "    r.inner_elab_role, i.IPaliases, i.mac ".
131 132
		 "  from nodes as n join interfaces as i ".
		 "left join reserved as r on r.node_id=n.node_id ".
133
		 "where (n.node_id=i.node_id and ".
134
                 "       (n.role='powerctrl' or n.role='testnode' or n.role='virtnode')) ".
135
		 "    and i.IP is not null ".
136 137
		 "    and (i.role='" . TBDB_IFACEROLE_CONTROL() . "' or " .
		 "         i.role='" . TBDB_IFACEROLE_MANAGEMENT() . "')");
138

139
my %routable;
140 141
my %unroutable;
my %reverse;
142

143
process_nodes($db_result);
144

145 146 147 148 149 150 151
#
# Now get the static virtnodes and older dynamic virtnodes that used
# jailip. The i2 join is to weed out the dynamic virtnodes we got in
# the above query (nodes with a dynamic control network interface).
#
$db_result =
    DBQueryFatal("select n.node_id, n.role, n.jailip, i.IP, i.role, ".
152
                 "    r.inner_elab_role, null ".
153 154 155 156
                 "  from nodes as n ".
                 "left join interfaces as i on i.node_id=n.phys_nodeid ".
                 "left join interfaces as i2 on i2.node_id=n.node_id ".
                 "left join reserved as r on r.node_id=n.node_id ".
157
                 "where n.role='virtnode' and i2.iface is null and ".
158 159 160 161 162
                 "      (i.IP is not null or n.jailip is not null) and ".
                 "      i.role='" . TBDB_IFACEROLE_CONTROL() . "'");

process_nodes($db_result);

163 164 165 166
#
# For IXPs we also need their gateway addresses as well
#
$db_result =
167
    DBQueryFatal("select n.node_id, n.role, n.jailip, i.IP, i.role, null,null ".
168
		 "  from nodes as n ".
169 170 171 172
		 "left join interfaces as i ".
		 "on n.phys_nodeid=i.node_id and n.node_id!=i.node_id ". 
		 "where n.role='testnode' ".
		 "    and (i.IP is not null or n.jailip is not null) ".
173
		 "    and (i.iface is null or ".
174 175
		 "         i.role='" . TBDB_IFACEROLE_GW() . "') ");
process_nodes($db_result);
176

177 178 179 180 181 182 183 184 185 186 187 188 189
# Get the v2pmap table since that holds the additional name mappings.
my %p2vmap = ();
$db_result =
    DBQueryFatal("select v.vname,v.node_id from reserved as r ".
                 "left join v2pmap as v on v.node_id=r.node_id and ".
                 "     v.exptidx=r.exptidx and v.vname!=r.vname ".
                 "where v.vname is not null");
while (my ($vname,$node_id) = $db_result->fetchrow_array()) {
    $p2vmap{$node_id} = []
        if (!exists($p2vmap{$node_id}));
    push(@{ $p2vmap{$node_id} }, $vname);
}

190
#
191
# Get the list of currently-reserved nodes so that we can make CNAMEs for them
192
#
193
$db_result =
194
    DBQueryFatal("select node_id,pid,eid,vname,inner_elab_role from reserved");
195

196
my %cnames;
197
while (my ($node_id,$pid,$eid,$vname,$erole) = $db_result->fetchrow_array()) {
198

199 200 201 202 203 204
    #
    # Handle some rare cases where a node can get reserved without a vname -
    # such as calling nalloc directly
    #
    if (!defined($vname)) {
	$vname = $node_id;
205
    }
206
    push @{$cnames{$node_id}}, "$vname.$eid.$pid";
207

208 209 210 211 212 213 214 215 216
    # Temp hack for Leigh
    if ($pid eq "testbed" and $eid eq "xen-leelab") {
        push @{$cnames{$node_id}}, "${vname}.xenlab.$pid";

        if (defined($erole) && $erole =~ /boss/) {
            push @{$cnames{$node_id}}, "www.xenlab.$pid";
        }
    }

217 218 219
    #
    # Special case for inner elab boss; add CNAME for www.
    #
220
    if (defined($erole) && $erole =~ /boss/) {
221
        push @{$cnames{$node_id}}, "www.$eid.$pid";
222 223 224
	if ($vname ne "boss") {
	    push @{$cnames{$node_id}}, "boss.$eid.$pid";
	}
225
    }
226 227 228 229 230 231 232
    #
    # Special case for inner elab ops; add CNAME for event-server.
    #
    if (defined($erole) &&
        ($erole eq "ops" || $erole eq "ops+fs")) {
        push @{$cnames{$node_id}}, "event-server.$eid.$pid";
    }
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    #
    # And add additional names in v2pmap, which can happen for delay and
    # bridge nodes.
    #
    if (exists($p2vmap{$node_id})) {
        my @extras = @{ $p2vmap{$node_id} };

	foreach my $extra (@extras) {
            next
		if ($extra eq $vname);

	    push @{$cnames{$node_id}}, "$extra.$eid.$pid";
	}
    }
}
249 250

#
251
# Make the zone file for routable IP addresses
252
#
253 254
make_forward_zonefile($mapfiletail,\%routable,\%cnames);
assemble_zonefile($mapfile);
255

256
#
257 258
# Make the zone file that includes both routable and unroutable IP addresses,
# if the site has a .head file for it
259 260
#
if (-e $mapfile_internal_head) {
261 262 263
    make_forward_zonefile($mapfile_internal_tail,
	{%routable, %unroutable},\%cnames);
    assemble_zonefile($mapfile_internal);
264
}
265

266 267 268 269 270
#
# Look for reverse zone files that we may need to make
#
opendir(DIR,$reversedir) or fatal("Unable to open directory $reversedir\n");
while (my $dirent = readdir(DIR)) {
271 272
    if (! (($dirent =~ /((\d+\.\d+\.\d+).*\.db)\.head/) ||
           ($dirent =~ /((\d+\.\d+).*\.db)\.head/))) {
273 274 275 276 277 278 279
	next;
    }
    my $subnet = $2;
    my $basename = $1;

    my $filename = "$reversedir/$basename.tail";
    open MAP, ">$filename" || fatal("Couldn't open $filename: $!\n");
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    if (exists($reverse{$subnet})) {
        if ($subnet =~ /^(\d+)\.(\d+)$/) {
            my $classb = $reverse{$subnet};
            foreach my $classc (keys(%{$reverse{$subnet}})) {
                print MAP "\$ORIGIN ${classc}.${2}.${1}.in-addr.arpa.\n";
                foreach my $aref (@{$reverse{$subnet}->{$classc}}) {
	            my ($host, $name) = @$aref;
	            printf MAP "$host\tIN\tPTR\t$name.$OURDOMAIN.\n";
                }
                print MAP "\n";
            }
        }
        else {
	    foreach my $aref (sort {$$a[0] <=> $$b[0]} @{$reverse{$subnet}}) {
	        my ($host, $name) = @$aref;
	        printf MAP "$host\tIN\tPTR\t$name.$OURDOMAIN.\n";
	    }
        }
298 299 300
    }
    close MAP;

301
    assemble_zonefile("$reversedir/$basename");
302 303 304
}

if ($IPV6_ENABLED) {
305 306
    require Net::IP;

307 308
    make_ipv6_reverse_zonefile($reverse_ipv6_tail,\%routable);
    assemble_zonefile($reverse_ipv6);
309 310 311
}
closedir DIR;

312
#
313
# Get the nameserver to reload the zone files.
314
# This is better than HUPing the nameserver directly. Note that we look
315 316
# for a local port of named first.
#
317 318 319 320
if ($restart_named) {
    if (-x "/usr/local/sbin/rndc") {
	system("/usr/local/sbin/rndc reload > /dev/null") == 0 or
	    fatal("/usr/local/sbin/rndc reload failed!\n");
321 322 323 324 325 326 327 328 329 330
    }
    # XXX named.reload went away circa FBSD 9
    elsif (-x "/usr/sbin/named.reload") {
	system("/usr/sbin/named.reload > /dev/null") == 0 or
	    fatal("/usr/sbin/named.reload failed!\n");
    }
    # try standard rndc
    else {
	system("/usr/sbin/rndc reload > /dev/null") == 0 or
	    fatal("/usr/sbin/rndc failed!\n");
331
    }
332
}
333 334
TBScriptUnlock();
exit(0);
335

336 337 338 339 340 341 342 343 344 345 346
#
# Sort out the routable and unroutable addresses from a DB query,
# and make a map for reversing them
#
sub process_nodes($) {
    while (my @row = $db_result->fetchrow_array()) {
	my $node_id = $row[0];
	my $nrole   = $row[1];
	my $jailIP  = $row[2];
	my $IP      = $row[3];
	my $irole   = $row[4];
347
	my $inner_elab_role = $row[5];
348
	my $IPaliases = $row[6];
349
	my $mac	    = $row[7];
350 351 352 353 354 355 356 357 358 359 360 361

	#
	# For most nodes, we get the IP address from the interfaces table;
	# but, for virtual nodes, we get it from the jailip column
	#
	if (defined($jailIP)) {
	    $IP = $jailIP;
	}
	if (!$IP) {
	    warn "named_setup: No IP for node $node_id!\n";
	    next;
	}
362 363
       	
	#
364 365 366 367
	# Special treatment for gateway interfaces - we give act as if they
	# are a separate node
	#
	if ($irole && $irole eq TBDB_IFACEROLE_GW()) {
368 369 370 371 372
	    $node_id = "${node_id}-gw";
	}
	# Ditto for management interface.
	if ($irole && $irole eq TBDB_IFACEROLE_MANAGEMENT()) {
	    $node_id = "${node_id}-mng";
373 374 375 376 377 378 379 380
	}

	#
	# Make a little structure so that we can make decisions later about
	# this node (ie. handling virt nodes differently)
	#
	my $node_rec = {
	    IP   => $IP,
381 382
	    role => $nrole,
	    inner_elab_role => $inner_elab_role
383 384 385 386 387 388 389 390 391 392 393
	};

	#
	# Sort it into the right pile based on whether or not it's routable
	#
	if (isroutable($IP)) {
	    $routable{$node_id} = $node_rec;
	} else {
	    $unroutable{$node_id} = $node_rec;
	}

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	#
	# Special treatment for virtnodes with private IP addresses;
	# If there is an IPaliases defined, add a entry for that too,
	# but only to the other map. In other words, if the IP is
	# unroutable and the ipalias is routable, then we can have
	# two entries, one of which is routable.
	#
	if (defined($IPaliases) && $IPaliases ne "") {
	    my @ipaliases = split(',', $IPaliases);
	    my $ipalias   = $ipaliases[0];

	    if (!isroutable($IP) && isroutable($ipalias)) {
		$routable{$node_id} = {
		    IP   => $ipalias,
		    role => $nrole,
		    inner_elab_role => $inner_elab_role
		};
	    }
	}
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
	if ($IPV6_ENABLED && $mac) {
	    # Calculate modified EUI-64 from MAC address
	    my $v = substr($mac, 1, 1);
	    $v = unpack("B4", pack("H1", $v));
	    if (substr($v, 2, 1) == '0') {
		substr($v, 2, 1, '1');
	    }
	    else {
		substr($v, 2, 1, '0');
	    }
	    $v = unpack("H1", pack("B4", $v));

	    my $IP6 = $IPV6_SUBNET_PREFIX . ':' . substr($mac,0,1) . $v .
		substr($mac,2,2) . ':' . substr($mac,4,2) . 'ff:fe' .
		substr($mac,6,2) . ':' . substr($mac,8,4);
	    
	    # Make new node_rec with IP6 address in IP field
	    my $node_rec6 = {
		IP   => $IP6,
		role => $nrole,
		inner_elab_role => $inner_elab_role,
	    };

	    # Push it to the routable & unroutable stack, add prefix to create
	    # a different node_id key
	    $routable{"_V6_" . $node_id} = $node_rec6;
	    $unroutable{"_V6_" . $node_id} = $node_rec6;
	}
441

442 443 444
	#
	# Put it into a map so we can generate the reverse zone file later
	#
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	# We use a Class C for the jail network, which complicates the
	# reverse zone generation.
	#
	if (IsJailIP($IP)) {
	    $IP =~ /(\d+\.\d+)\.(\d+)\.(\d+)/;
	    my $classb = $1;
	    my $subnet = $2;
	    my $host   = $3;

	    if (!exists($reverse{"$classb"})) {
		$reverse{"$classb"} = {};
	    }
	    if (!exists($reverse{"$classb"}->{$subnet})) {
		$reverse{"$classb"}->{$subnet} = [];
	    }
	    push(@{$reverse{"$classb"}->{$subnet}}, [$host, $node_id]);
	}
	elsif ($IP =~ /(\d+\.\d+\.\d+)\.(\d+)/) {
463 464 465
	    my $subnet = $1;
	    my $host = $2;
	    push @{$reverse{$subnet}}, [$host, $node_id];
466 467
	}
	else {
468 469 470 471 472
	    warn "Poorly formed IP address $IP\n";
	}
    }
}

473 474 475
#
# Die and tell someone about it
#
476
sub fatal {
477
    my $msg = $_[0];
478

479
    TBScriptUnlock();
480
    SENDMAIL($TBOPS, "Named Setup Failed", $msg);
481 482
    die($msg);
}
483 484

#
485
# Put together a zone file from its consituent head and tail pieces
486
#
487 488
sub assemble_zonefile($) {
    my ($mapfile) = @_;
489 490

    my $mapfileback = "$mapfile.backup";
491 492
    my $mapfilehead = "$mapfile.head";
    my $mapfiletail = "$mapfile.tail";
493
    my $mapfilefrag = "$mapfile.local";
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511

    #
    # Concat the head and tail files to create the new map.
    #
    if (-e $mapfile) {
	system("mv -f $mapfile $mapfileback") == 0 or
	fatal("Could not back up $mapfile to $mapfileback\n");
    }

    #
    # Generate a warning so that no one tries to edit the file by hand
    #
    open(MAP, ">$mapfile") || fatal("Couldn't open $mapfile\n");
    print MAP
    ";\n".
    "; ******************************************************************\n".
    "; DO NOT EDIT THIS FILE. IT IS A CREATION, A FIGMENT, A CONTRIVANCE!\n".
    ";\n".
512
    "; Edit the \"head\" file, then run ${TB}/bin/named_setup.\n".
513 514 515 516 517 518 519 520 521 522
    "; ******************************************************************\n".
    ";\n";

    #
    # Now copy in the head part of the map, looking for the serial
    # number so it can be bumped up.
    #
    open(MAPHEAD, "<$mapfilehead") || fatal("Couldn't open $mapfilehead\n");
    while (<MAPHEAD>) {
	if ( /;\s*Serial\s+/i ) {
523 524 525
	    my $serial = `date +%s`;
	    chomp($serial);
	    $serial = $serial | 0x70000000;
526 527 528 529 530 531 532 533 534 535

	    print MAP "\t\t\t$serial\t; Serial Number -- DO NOT EDIT\n";
	}
	else {
	    print MAP "$_";
	}
    }
    close(MAPHEAD);
    close(MAP);

536 537 538 539 540 541 542 543 544 545 546 547
    # Give local admin a place to add static stuff to the head.
    if (-e $mapfilefrag) {
	system("echo '' >> $mapfile");
	system("echo ';' >> $mapfile");
	system("echo '; This is a local fragment; $mapfilefrag' >> $mapfile");
	system("echo ';' >> $mapfile");
	system("cat $mapfilefrag >> $mapfile") == 0 or
	    fatal("Failed to concat $mapfilefrag to $mapfile\n");
	system("echo '; End of local fragment; $mapfilefrag' >> $mapfile");
	system("echo '' >> $mapfile");
    }

548 549 550 551
    #
    # Now the tail of the map.
    # 
    system("cat $mapfiletail >> $mapfile") == 0 or
552
	fatal("Failed to concat $mapfiletail to $mapfile\n");
553
}
554

555 556 557 558 559 560 561 562 563 564
my $laddrs;
sub byip {
    my @aa = split '\.', $$laddrs{$a}->{IP};
    my @bb = split '\.', $$laddrs{$b}->{IP};
    return $aa[0] <=> $bb[0] ||
	$aa[1] <=> $bb[1] ||
	    $aa[2] <=> $bb[2] ||
		$aa[3] <=> $bb[3];
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
#
# Make Reverse IPv6 zonefile
#

sub make_ipv6_reverse_zonefile($$) {
    my ($filename, $addresses) = @_;
    open(MAP, ">$filename") || fatal("Couldn't open $filename\n");
    print MAP "\n";
    print MAP ";\n";
    print MAP "; DO NOT EDIT below this point. Auto generated map entries!\n";
    print MAP ";\n";
    print MAP "\n";
    print MAP "\n";
    print MAP ";\n";
    print MAP "; Nodes\n";
    print MAP ";\n";
    print MAP "\n";
    my @list = keys(%$addresses);
    if ($sortem) {
        $laddrs = $addresses;
        @list = sort byip @list;
    }
    for my $node_id (@list) {
        my $node_rec = $$addresses{$node_id};

	if ($node_id =~ /^_V6_(.*)$/) {
	    $node_id = $1;
	    my $ipv6 = $node_rec->{IP};
	    $ipv6 =~ s/[\n|\r]//g;
	    my $convert_ip = new Net::IP ($ipv6)
		or die (Net::IP::Error());
	    print MAP $convert_ip->reverse_ip() . "\tIN\tPTR\t" .
		$node_id . "." . $OURDOMAIN . ".\n";
	}
    }

    print MAP "\n";
    close(MAP);
}


606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
#
# Make a forward zone file, from the given map of addresses and CNAMEs
#
sub make_forward_zonefile($$$) {
    my ($filename, $addresses, $cnames) = @_;
    open(MAP, ">$filename") || fatal("Couldn't open $filename\n");
    print MAP "\n";
    print MAP ";\n";
    print MAP "; DO NOT EDIT below this point. Auto generated map entries!\n";
    print MAP ";\n";
    print MAP "\n";

    #
    # Start out with the A records for the nodes
    #
    print MAP "\n";
    print MAP ";\n";
    print MAP "; Nodes\n";
    print MAP ";\n";
    print MAP "\n";
626 627 628 629 630 631 632
    my @list = keys(%$addresses);
    if ($sortem) {
	$laddrs = $addresses;
	@list = sort byip @list;
    }
    for my $node_id (@list) {
	my $node_rec = $$addresses{$node_id};
633 634 635 636 637 638 639 640

	#
	# Special treatment for virtual nodes - we only bother to print them
	# out if some has reserved them (ie. if they have a CNAME)
	#
	if (($node_rec->{role} eq "virtnode") && (!$cnames->{$node_id})) {
	    next;
	}
641 642 643 644 645 646 647
	if ($node_id =~ /^_V6_(.*)$/) {
	    $node_id = $1;
	    print MAP $node_id . "\tIN\tAAAA\t$node_rec->{IP}\n";
	}
	else {
	    print MAP "$node_id\tIN\tA\t$node_rec->{IP}\n";
	}
648 649 650 651 652 653 654 655 656
	if ($domx) {
	    if (defined($node_rec->{inner_elab_role}) &&
		($node_rec->{inner_elab_role} eq "ops" ||
		 $node_rec->{inner_elab_role} eq "ops+fs")) {
		print MAP "\tIN\tMX 10\t$node_id\n";
	    }
	    else {
		print MAP "\tIN\tMX 10\t$USERS.\n";
	    }
657
	}
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
    }

    #
    # Switch the TTL to 1 second for CNAMEs so that people will changes quickly
    # as experiments swap in and out
    #
    print MAP "\n";
    print MAP "\$TTL\t1\n\n";

    print MAP "\n";
    print MAP ";\n";
    print MAP "; CNAMEs for reserved nodes\n";
    print MAP ";\n";
    print MAP "\n";

    while (my ($pname, $vnames) = each %$cnames) {
	#
	# Only print out CNAMEs for nodes that are actually going into this map
	#
677
	next unless ($addresses->{$pname} || $addresses->{"_V6_" . $pname});
678 679 680 681 682 683 684 685 686 687 688 689 690 691

	#
	# Write out every CNAME for this pnode
	#
	foreach my $vname (@$vnames) {
	    my $formatted_vname = sprintf "%-50s", $vname;
	    print MAP "$formatted_vname\tIN\tCNAME\t$pname\n";
	}
    }

    print MAP "\n";
    close(MAP);
}

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
#
# Is an IP routable?
#
sub isroutable($)
{
    my ($IP)  = @_;
    my ($a,$b,$c,$d) = ($IP =~ /^(\d*)\.(\d*)\.(\d*)\.(\d*)/);

    #
    # These are unroutable:
    # 10.0.0.0        -   10.255.255.255  (10/8 prefix)
    # 172.16.0.0      -   172.31.255.255  (172.16/12 prefix)
    # 192.168.0.0     -   192.168.255.255 (192.168/16 prefix)
    #

    # Easy tests.
    return 0
	if (($a eq "10") ||
	    ($a eq "192" && $b eq "168"));

    # Lastly
    return 0
	if (inet_ntoa((inet_aton($IP) & inet_aton("255.240.0.0"))) eq
	    "172.16.0.0");

    return 1;
}
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733

#
# IsJailIP()
#
sub IsJailIP($)
{
    my ($IP)  = @_;

    return 1
	if (inet_ntoa((inet_aton($IP) & inet_aton($VIRTNODE_NETMASK))) eq
	    $VIRTNODE_NETWORK);

    return 0;
}