named_setup.in 12.7 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9 10
use English;
use Fcntl ':flock';
11
use Socket;
12 13 14 15

#
# Suck out virtual names and create CNAME map entries.
#
16 17
# This script always does the right thing, so it does not matter who calls it. 
#
18 19 20 21 22 23 24
# usage: named_setup
#

#
# Configure variables
#
my $TB		= "@prefix@";
25
my $TBOPS       = "@TBOPSEMAIL@";
26
my $USERS	= "@USERNODE@";
27
my $DISABLED    = "@DISABLE_NAMED_SETUP@";
28
my $OURDOMAIN   = "@OURDOMAIN@";
29

Leigh B. Stoller's avatar
Leigh B. Stoller committed
30
my $mapdir			= "/etc/namedb";
31 32 33 34 35 36 37 38 39
my $mapfile			= "$mapdir/${OURDOMAIN}.db";
my $mapfiletail			= "$mapfile.tail";
my $mapfile_internal		= "$mapdir/${OURDOMAIN}.internal.db";
my $mapfile_internal_head	= "$mapfile_internal.head";
my $vnodesfile			= "$mapdir/vnodes.${OURDOMAIN}.db";
my $vnodesback 			= "$mapdir/vnodes.${OURDOMAIN}.db.backup";
my $reversedir			= "$mapdir/reverse";
my $lockfile			= "/var/tmp/testbed_named_lockfile";
my $dbg	= 0;
40 41
my @row;

42 43
use strict;

44 45 46 47 48
# If we're disabled, just quietly exit
if ($DISABLED) {
    exit 0;
}

49 50
# We don't want to run this script unless its the real version.
if ($EUID != 0) {
51 52
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
53 54
}
# XXX Hacky!
55
if (0 && $TB ne "/usr/testbed") {
56 57
    die("*** $0:\n".
	"    Wrong version. Maybe its a development version?\n");
58 59
}

60 61 62 63
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

64 65
# Turn off line buffering on output
$| = 1;
66

67
# Testbed Support libraries
68 69 70
use lib "@prefix@/lib";
use libtestbed;
use libdb;
71 72 73 74 75 76

#
# We need to serialize this script to avoid a trashed map file. Use
# a dummy file in /var/tmp, opened for writing and flock'ed. 
#
open(LOCK, ">>$lockfile") || fatal("Couldn't open $lockfile\n");
77
my $count = 0;
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
if (flock(LOCK, LOCK_EX|LOCK_NB) == 0) {
    #
    # If we don't get it the first time, we wait for:
    # 1) The lock to become free, in which case we do our thing
    # 2) The time on the lock to change, in which case we wait for that process
    #    to finish
    #
    my $oldlocktime = (stat(LOCK))[9];
    my $gotlock = 0;
    while (1) {
	print "Another named map update in progress, waiting for it to finish\n";
	if (flock(LOCK, LOCK_EX|LOCK_NB) != 0) {
	    # OK, got the lock, we can do what we're supposed to
	    $gotlock = 1;
	    last;
	}
94
	my $locktime = (stat(LOCK))[9];
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	if ($locktime != $oldlocktime) {
	    $oldlocktime = $locktime;
	    last;
	}
	if ($count++ > 20)  {
	    fatal("Could not get the lock after a long time!\n");
	}
	sleep(1);
    }

    $count = 0;
    #
    # If we didn't get the lock, wait for the processes that did to finish
    #
    if (!$gotlock) {
110 111 112 113 114 115 116 117
	while (1) {
	    if ((stat(LOCK))[9] != $oldlocktime) {
		exit(0);
	    }
	    if (flock(LOCK, LOCK_EX|LOCK_NB) != 0) {
		close(LOCK);
		exit(0);
	    }
118
	    if ($count++ > 20)  {
119
		fatal("Process with the lock didn't finish after a long time!\n");
120 121
	    }
	    sleep(1); 
122 123
	}

124 125 126
    }
}

127 128 129 130 131 132 133

#
# Perl-style touch(1)
#
my $now = time;
utime $now, $now, $lockfile;

134 135 136 137 138 139 140 141 142 143 144
#
# We stick the new map entries into the tail file. First zero it out.
#
open(MAP, ">$mapfiletail") || fatal("Couldn't open $mapfiletail\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";

#
145 146 147
# First create the IA records for those nodes that have a named_root
# defined. We glean the IP for the node from the interfaces table.
#
148
my $db_result =
149
    DBQueryFatal("select nt.type,nt.isremotenode,n.node_id,i.IP,i.role ".
150 151
		 "  from nodes as n ".
		 "left join node_types as nt on n.type=nt.type ".
152
		 "left join interfaces as i on n.node_id=i.node_id ". 
153
		 "where nt.isvirtnode=0 and ".
154 155
		 "      n.role='testnode' and i.IP is not null and ".
		 "      (i.card=nt.control_net or i.role='gw') ".
156 157
		 "order by nt.type,n.node_id");

158
my %reverse;
159
my %routable;
160 161 162 163 164 165 166 167 168 169 170
if ($db_result->numrows > 0) {
    #
    # Create an IN record for each node. 
    #
    my $oldtype = "";
    
    while (@row = $db_result->fetchrow_array) {
	my $type    = $row[0];
	my $isremote= $row[1];
	my $node_id = $row[2];
	my $IP      = $row[3];
171
	my $role    = $row[4];
172 173 174 175 176 177 178 179 180

	if ($oldtype ne $type) {
	    print MAP ";\n";
	    print MAP "; $type nodes.\n";
	    print MAP ";\n";
	    
	    $oldtype = $type;
	}

181 182 183 184
	next
	    if (! isroutable($IP));
	$routable{$node_id} = $IP;

185 186 187 188
	if ($role eq "gw") {
	    $node_id = "$node_id-gw";
	}

189 190 191
	print MAP "$node_id\tIN\tA\t$IP\n";
	print MAP "\tIN\tMX 10\t$USERS.\n";

192
	#
193 194 195
	# Put it into a map so we can generate the reverse zone file later
	#
	$IP =~ /(\d+\.\d+\.\d+)\.(\d+)/;
196 197 198 199 200 201 202
	if ($1 && $2) {
	    my $subnet = $1;
	    my $host = $2;
	    push @{$reverse{$subnet}}, [$host, $node_id];
	} else {
	    warn "Poorly formed IP address $IP\n";
	}
203 204 205
    }
}
print MAP "\n";
206
print MAP "\$TTL\t1\n\n";
207 208 209 210

#
# Figure out the set of experiment nodes we are working on. These are the 
# nodes that have a vname in the reserved table. 
211
#
212
$db_result =
213
    DBQueryFatal("select r.node_id,pid,eid,vname,n.phys_nodeid,n.jailip,i.IP ".
214
		 " from reserved as r ".
215 216
		 "left join nodes as n on n.node_id=r.node_id ".
		 "left join node_types as nt on nt.type=n.type ".
217 218
		 "left join interfaces as i on n.node_id=i.node_id and ".
		 "     nt.control_iface=i.iface ");
219 220 221 222 223

if ($db_result->numrows > 0) {
    #
    # Create a CNAME for each reserved node.
    #
224 225 226 227 228 229
    while (my %row = $db_result->fetchhash) {
	my $node_id = $row{"node_id"};
	my $pid     = $row{"pid"};
	my $eid     = $row{"eid"};
	my $physid  = $row{"phys_nodeid"};
	my $jailip  = $row{"jailip"};
230
	my $vname   = $node_id;
231
	my $cname;
232

233 234
	if (defined($row{"vname"})) {
	    $vname = $row{"vname"};
235
	}
236

237 238 239 240
	# If the node has its own unroutable IP skip; done later. 
	next
	    if (defined($row{"IP"}) and !isroutable($row{"IP"}));

241
        #
242 243 244 245
        # VIRTNODE HACK: Map cname to underlying physnode, but first
	# spit out a vname for the virtnode name (no point in polluting
	# the map with a zillion cnames for nodes that do not actually
	# exist until they are allocated).
246
        #
247 248 249
	if (defined($physid) && $physid ne $node_id) {
	    if (defined($jailip)) {
		#
250
		# If the jailip is unroutable, then skip this completely
251 252
		# since both the name and the cname will be entered into
		# the "private" vnodes map below. 
253 254 255
		#
		next
		    if (!isroutable($jailip));
256
	    }
257 258 259 260 261 262 263 264 265 266

	    #
	    # If the vnode has its own routable IP, we can create the CNAME
	    # to it directly below. But if the vnode does not have its own
	    # routable IP, then we need create an CNAME entry for it that
	    # points to the physnode first/.
	    #
	    if (!exists($routable{$node_id})) {
		$cname   = sprintf("%-40s", "$node_id");
		printf MAP "$cname IN\tCNAME\t$physid\n";
267
	    
268 269
		$node_id = $physid;
	    }
270
	}
271
	    
272
	$cname = sprintf("%-40s", "$vname.$eid.$pid");
273 274 275 276 277 278 279 280
	printf MAP "$cname IN\tCNAME\t$node_id\n";
    }
}

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

#
281
# Make this map!
282
#
283
make_zonefile($mapfile);
284

285 286 287 288 289 290 291 292
#
# If they have an 'internal' zone file (ie. with some internal IPs for boss and
# ops), make that too
#
if (-e $mapfile_internal_head) {
    make_zonefile($mapfile_internal,$mapfile_internal_head,$mapfiletail);
}

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
#
# Now we tack on the virtual node, but that has to go onto the end
# of a *copy* of the file we just created.
#
if (-e $vnodesfile) {
    system("mv -f $vnodesfile $vnodesback") == 0 or
	fatal("Could not back up $vnodesfile to $vnodesback\n");
}

system("cp $mapfile $vnodesfile") == 0 or
    fatal("Could not copy $mapfile to $vnodesfile\n");

#
# Open up the file and append the jail ips.
# 
open(MAP, ">>$vnodesfile") || fatal("Couldn't open $vnodesfile\n");
print MAP
    ";\n".
    "; Jail IPs (allocated nodes only).\n" .
312 313
    ";\n\n".
    "\$TTL\t60\n\n";
314 315 316 317 318
    
$db_result =
    DBQueryFatal("select r.*,n.jailip from reserved as r ".
		 "left join nodes as n on n.node_id=r.node_id ".
		 "left join node_types as nt on nt.type=n.type ".
319
		 "where nt.isvirtnode=1 and nt.isremotenode=0 and ".
320
		 "      nt.issubnode=0 and n.jailip is not null");
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

if ($db_result->numrows > 0) {
    #
    # Create a CNAME for each reserved node.
    #
    while (my %row = $db_result->fetchhash) {
	my $node_id = $row{"node_id"};
	my $pid     = $row{"pid"};
	my $eid     = $row{"eid"};
	my $IP      = $row{"jailip"};
	my $vname   = $node_id;

	if (defined($row{"vname"})) {
	    $vname = $row{"vname"};
	}

337 338 339
	next
	    if (isroutable($IP));

340 341 342 343 344 345 346 347
	# Spit an A record for the node.
	print MAP "$node_id\tIN\tA\t$IP\n";

	# Then a CNAME.
	my $cname = sprintf("%-40s", "$vname.$eid.$pid");
	printf MAP "$cname IN\tCNAME\t$node_id\n";
    }
}
348 349 350 351 352 353

#
# Other unroutable addresses.
# 
print MAP
    ";\n".
354
    "; Other unroutable IPs.\n" .
355 356 357 358 359 360 361 362
    ";\n";
    
$db_result =
    DBQueryFatal("select r.node_id,r.pid,r.eid,r.vname,i.IP ".
		 "  from reserved as r ".
		 "left join nodes as n on r.node_id=n.node_id ".
		 "left join node_types as nt on n.type=nt.type ".
		 "left join interfaces as i on n.node_id=i.node_id and ".
363
		 "     nt.control_iface=i.iface ".
364
		 "where n.role='testnode' and i.IP is not null ".
365 366 367 368 369 370 371 372 373 374 375 376 377
		 "order by nt.type,n.node_id");

if ($db_result->numrows > 0) {
    #
    # Create a CNAME for each reserved node.
    #
    while (my %row = $db_result->fetchhash) {
	my $node_id = $row{"node_id"};
	my $pid     = $row{"pid"};
	my $eid     = $row{"eid"};
	my $IP      = $row{"IP"};
	my $vname   = $node_id;

378 379 380
	next
	    if (isroutable($IP));

381 382 383 384 385 386 387 388 389 390 391 392
	if (defined($row{"vname"})) {
	    $vname = $row{"vname"};
	}

	# Spit an A record for the node.
	print MAP "$node_id\tIN\tA\t$IP\n";

	# Then a CNAME.
	my $cname = sprintf("%-40s", "$vname.$eid.$pid");
	printf MAP "$cname IN\tCNAME\t$node_id\n";
    }
}
393 394
close(MAP);

395 396 397 398 399 400 401 402 403 404 405 406 407
#
# 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)) {
    if ($dirent !~ /((\d+\.\d+\.\d+)\.db)\.head/) {
	next;
    }
    my $subnet = $2;
    my $basename = $1;

    my $filename = "$reversedir/$basename.tail";
    open MAP, ">$filename" || fatal("Couldn't open $filename: $!\n");
408 409 410 411 412
    if ($reverse{$subnet}) {
	foreach my $aref (sort {$$a[0] <=> $$b[0]} @{$reverse{$subnet}}) {
	    my ($host, $name) = @$aref;
	    printf MAP "$host\tIN\tPTR\t$name.$OURDOMAIN.\n";
	}
413 414 415 416 417 418 419 420
    }
    close MAP;

    make_zonefile("$reversedir/$basename");
    
}
closedir DIR;

421
#
422 423 424 425 426 427 428 429 430 431 432
# This is better than HUPing the nameserver directly. Notet that we look
# for a local port of named first.
#
if (-x "/usr/local/sbin/rndc") {
    system("/usr/local/sbin/rndc reload > /dev/null") == 0 or
	fatal("/usr/local/sbin/rndc reload failed!\n");
}
else {
    system("named.reload > /dev/null") == 0 or
	fatal("named.reload failed!\n");
}
433 434

sub fatal {
435
    my $msg = $_[0];
436

437
    SENDMAIL($TBOPS, "Named Setup Failed", $msg);
438 439
    die($msg);
}
440 441 442 443

#
# Make a zone file from the 
#
444 445
sub make_zonefile($;$$) {
    my ($mapfile,$mapfilehead,$mapfiletail) = @_;
446 447

    my $mapfileback = "$mapfile.backup";
448 449 450 451 452 453
    if (!defined $mapfilehead) {
	$mapfilehead = "$mapfile.head";
    }
    if (!defined $mapfiletail) {
	$mapfiletail = "$mapfile.tail";
    }
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

    #
    # 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".
    "; Edit the \"head\" file, then run ${TB}bin/named_setup.\n".
    "; ******************************************************************\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 ) {
	    my $serial = `date +%s`;
	    chop $serial;

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

    #
    # Now the tail of the map.
    # 
    system("cat $mapfiletail >> $mapfile") == 0 or
    fatal("Failed to concat $mapfiletail to $mapfile\n");
}
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

#
# 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;
}