dhcpd_makeconf.in 21.1 KB
Newer Older
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh 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 Stoller's avatar
Leigh Stoller committed
23
#
24
use English;
25
use Getopt::Std;
26
use Fcntl ':flock';
27
use Socket;
28

29 30 31 32
#
# dhcpd_makeconf - helper script to create dhcpd.conf files from the database.
# The template file should look like an ordinary dhcpd.conf file, but have
# the string %%nodetype=<type> where you want entries for a set of nodes
33
# filled out. See the template file in the dhcp directory for an example.
34
#
35 36 37 38 39
sub usage {
    print "Usage: $0 [-h] [-v] [-i] [-t <templatefile>]\n";
    print "-h	Show this message\n";
    print "-v	Use virtual names, when possible, for hostnames\n";
    print "-i 	Install new config file to standard location.\n";
40
    print "-r 	Restart DHCPD after config file regeneration (with -i only)\n";
41 42 43 44
    print "Without -i, spits out a dhcpd config file on stdout, as in:\n";
    print "  $0 > dhcpd.conf\n";
    exit(1);
}
45
my $optlist = "ihvt:r";
46 47
my $install = 0;
my $vnames  = 0;
48
my $restart = 0;
49

50
#
51
# Configure variables
52
#
53
my $TB          = "@prefix@";
54
my $TBOPS       = "@TBOPSEMAIL@";
55
my $ELABINELAB  = @ELABINELAB@;
56 57
my $BOSSNODE_IP = "@BOSSNODE_IP@";
my $OURDOMAIN   = "@OURDOMAIN@";
58 59 60
my $MANAGEMENT_NETWORK = "@MANAGEMENT_NETWORK@";
my $MANAGEMENT_NETMASK = "@MANAGEMENT_NETMASK@";
my $MANAGEMENT_GATEWAY = "@MANAGEMENT_ROUTER@";
61 62 63 64 65 66 67 68

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

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

69
use lib "@prefix@/lib";
70 71
use libdb;
use libtestbed;
72
use Node;
73 74 75
use Experiment;
use EmulabFeatures;
use EmulabConstants;
76

77
# Tell EmulabFeatures to take it down a notch.
78 79
$EmulabFeatures::verbose = 0;

80 81 82 83 84 85 86 87
#
# PXE boot constants
#
my $PXEBOOTDIR	    = "/tftpboot";
my $PXELINUXPREFIX  = "$PXEBOOTDIR/pxelinux";
my $PXELINUXBOOT    = "pxelinux.0";
my $PXELINUXCONFIG  = "pxelinux.cfg";

88 89 90 91
# XXX hack for now to support gpxelinux as well
my $GPXELINUXPREFIX = "$PXEBOOTDIR/gpxelinux";
my $GPXELINUXBOOT   = "gpxelinux.0";

92
my $CRTLTAG	    = TBDB_IFACEROLE_CONTROL();
93
my $MNGMNTTAG	    = TBDB_IFACEROLE_MANAGEMENT();
94 95
my $DHCPD_CONF	    = "/usr/local/etc/dhcpd.conf";
my $DHCPD_TEMPLATE  = "/usr/local/etc/dhcpd.conf.template";
96
my $DHCPD_TAIL      = "/usr/local/etc/dhcpd.conf.tail";
97
my %servernodes	    = ();
98 99 100
my %dhcp_subbosses  = ();
my %tftp_subbosses  = ();
my %bootinfo_subbosses  = ();
101
my %singlectlnet    = ();
102
my %extman_cache    = ();
103 104
my $template        = $DHCPD_TEMPLATE;
my $outfile	    = "/tmp/dhcpd_makeconf.$$";
105
my $OUT		    = *STDOUT;
106
my %subboss_dhcp_servers;
107 108 109
my %management_ifaces;
my $m_network = inet_ntoa(inet_aton($MANAGEMENT_NETWORK) &
			  inet_aton($MANAGEMENT_NETMASK));
110
my $mgmt_just_nodes;
111 112

sub RestartSubbossDhcp($$);
113 114
sub SetupPXEBoot($$$$);
sub ClearPXEBoot($$);
115

116
#
117 118
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
119
#
120 121 122 123 124 125
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"i"})) {
    $install = 1;
126

127 128 129 130 131
    # We don't want to run this script unless its the real version.
    if ($EUID != 0) {
	die("*** $0:\n".
	    "    Must be root! Maybe its a development version?\n");
    }
132 133 134
    if (defined($options{"r"})) {
	$restart = 1;
    }
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
}
if (defined($options{"h"})) {
    usage();
}
if (defined($options{"v"})) {
    $vnames = 1;
}
if (defined($options{"t"})) {
    $template = $options{"t"};

    #
    # Untaint argument; Allow slash.
    #
    if ($template =~ /^([-\w\.\/]+)$/) {
	$template = $1;
    }
    else {
	die("Tainted template name: $template\n");
    }
}
155

156
#
157
# If we are going to actually install this file, must serialize to
158
# avoid a trashed config file.
159
#
160
if ($install) {
161 162 163 164
    if ((my $locked = TBScriptLock("dhcpd.conf", 1)) != TBSCRIPTLOCK_OKAY()) {
	exit(0)
	    if ($locked == TBSCRIPTLOCK_IGNORE);
	fatal("Could not get the lock after a long time!\n");
165
    }
166

167 168
    #
    # Open temporary output file.
169
    #
170 171 172 173
    open(OF, ">$outfile") or
	fatal("Could not open $outfile\n");
    $OUT = *OF;
}
174

175 176 177 178 179 180 181 182 183 184 185 186 187
#
# Locate all the "alternate server" nodes (inner elab boss nodes or
# inner plab PLC nodes).  We do this now rather than in the per-nodetype
# query below because the server may not be of the same type as the nodes
# they serve!
#
my $query_result =
    DBQueryWarn("select r.node_id,i.IP,r.pid,r.eid ".
		"from reserved as r ".
                "left join interfaces as i on ".
                "     r.node_id=i.node_id ".
                "where (r.inner_elab_role='boss' or ".
		"       r.inner_elab_role='boss+router' or ".
188
		"       r.inner_elab_role='boss+fs+router' or ".
189 190 191 192 193 194 195
		"       r.plab_role='plc') and ".
		"      i.role='$CRTLTAG' ");
if (! $query_result) {
    fatal("Could not dhcpd data from DB!");
}
while (my %row = $query_result->fetchhash()) {
    if (defined($row{"pid"}) && defined($row{"eid"})) {
196 197 198
	my $pid = $row{"pid"};
	my $eid = $row{"eid"};
	my $tag = "${pid}:${eid}";
199

200
	$servernodes{$tag} = $row{"IP"};
201 202 203 204 205 206 207 208 209 210 211 212 213

	#
	# Need to know if this is a single or dual control network model.
	#
	my $exp_result =
	    DBQueryFatal("select elabinelab_singlenet from experiments ".
			 "where pid='$pid' and eid='$eid'");
	if ($exp_result->num_rows) {
	    $singlectlnet{$tag} = ($exp_result->fetchrow_array())[0];
	}
	else {
	    $singlectlnet{$tag} = 0;
	}
214 215 216
    }
}

217 218 219 220 221 222
# Slurp in all of the subboss data to know A) if we should serve the DHCP
# lease to a given node, or if the next-server field should be something
# other than the IP for boss.
$query_result =
    DBQueryWarn("select s.node_id, s.service, s.subboss_id, i.IP ".
		"from subbosses as s left join interfaces as i on ".
223
		"s.subboss_id=i.node_id where i.role='$CRTLTAG' and s.disabled=0");
224 225 226 227 228 229
if (! $query_result) {
    fatal("Could not dhcpd data from DB!");
}
while (my %row = $query_result->fetchhash()) {
    my $node_id = $row{"node_id"};

230 231 232 233 234 235 236
    # We need to try restarting dhcpd on *all* subbosses, just in case
    # we've moved all dhcp nodes from one subboss to another.
    # Otherwise the old server wouldn't get restarted and both the old
    # and new servers would attempt to respond to dhcp requests.

    $subboss_dhcp_servers{$row{"subboss_id"}} = 1;

237 238 239 240 241 242 243 244 245
    if ($row{"service"} eq 'tftp') {
	    $tftp_subbosses{$node_id} = $row{"IP"};
    } elsif ($row{"service"} eq 'dhcp') {
	    $dhcp_subbosses{$node_id} = $row{"IP"};
    } elsif ($row{"service"} eq 'bootinfo') {
	    $bootinfo_subbosses{$node_id} = $row{"IP"};
    }
}

246 247 248 249 250
#
# First process the template and insert fragments.
#
my @lines = ();

251 252
open(IF,"<$template") or
    fatal("Unable to open $template for reading");
253
while (<IF>) {
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
    if (/^(\s*)\%\%fragment=([-\w]+)/) {
	my $file = "$TB/etc/dhcpd.conf.frag.${2}";
	if (! -e $file) {
	    push(@lines, $1 . "#" . $_);
	    next;
	}
	open(FRAG, "$file") or
	    fatal("Unable to open $file for reading");
	while (<FRAG>) {
	    push(@lines, $1 . $_);
	}
	close(FRAG);
	next;
    }
    push(@lines, $_);
}
close(IF);

#
# Now run through the updated template lines.
#
foreach (@lines) {
276 277 278 279 280
	if (/^(\s*)range\s*;/) {
		# Comment out a null DHCPD_DYNRANGE line.
		my $spaces = $1;
		print $OUT "${spaces}#range ... ...;\n";
	} elsif (/^(\s*)\%\%nodetype=(\w+)/) {
281 282
		my $spaces = $1;
		my $nodetype = $2;
283 284

		$query_result =
285 286
		    DBQueryWarn("select n.node_id,n.type, ".
				"       n.pxe_boot_path,n.next_pxe_boot_path,".
287 288
				"       i.IP,i.MAC,r.pid,r.eid,r.vname, ".
				"       r.inner_elab_role,r.inner_elab_boot, ".
289
				"       r.plab_role,r.plab_boot,i.role ".
290 291 292 293 294 295 296 297
				"from nodes as n ".
				"left join interfaces as i on ".
				"     n.node_id=i.node_id ".
				"left join node_types as t on n.type=t.type ".
				"left join reserved as r on ".
				"     n.node_id=r.node_id ".
				"where (n.type='$nodetype' or ".
				"       t.class='$nodetype') and ".
298 299 300
				"      (i.role='$CRTLTAG' or ".
				"       (i.role='$MNGMNTTAG' and ".
				"        t.isremotenode=0)) ".
301 302 303
				"order BY n.priority");
		if (! $query_result) {
		    fatal("Could not dhcpd data from DB!");
304
		}
305 306 307 308

		while (my %row = $query_result->fetchhash()) {
		    my $ip  = $row{"IP"};
		    my $mac = $row{"MAC"};
309
		    my $role = $row{"role"};
310 311
		    my $node_id;
		    my $next_server = "";
312
		    my $bootinfo_server = "";
313
		    my $hostname = "";
314
		    my $dns = "";
315
		    my $booting = "";
316
		    my $filename = "";
317
		    my $tag;
318
		    my $extman = 0;
319 320 321 322 323 324 325

		    if ($vnames && defined($row{"vname"})) {
			$node_id = $row{"vname"};
		    }
		    else {
			$node_id = $row{"node_id"};
		    }
326

327 328 329 330 331
		    #
		    # Grab some information for nodes that are
		    # currently reserved to an experiment.  In
		    # addition to setting a simple tag for further
		    # lookups below, check to see if a node's current
332
		    # experiment is marked for
333 334 335 336 337 338
		    # external management. This state is used below in
		    # determining whether or not to disable a node in
		    # the dhcp config file.  Keep a cache of this
		    # experiment-level info so that we don't have to
		    # hit the DB for each and every node.
		    #
339 340
		    if (defined($row{"pid"})) {
			$tag = $row{"pid"} . ":" . $row{"eid"};
341

Kirk Webb's avatar
Kirk Webb committed
342
			if (!exists($extman_cache{$tag})) {
343 344 345 346 347 348
			    my $experiment = Experiment->Lookup($row{"pid"},
								$row{"eid"});
			    $extman = $extman_cache{$tag} = 
				defined($experiment) &&
				EmulabFeatures->FeatureEnabled(
				    "ExternalNodeManagement", undef, undef,
349
				    $experiment);
350 351 352 353
			}
			else {
			    $extman = $extman_cache{$tag};
			}
354 355
		    }

356 357
		    # Management ports, nothing special to do.
		    if ($role eq TBDB_IFACEROLE_MANAGEMENT()) {
358 359 360 361 362 363 364 365 366 367 368 369
			my $network = inet_ntoa(inet_aton($ip) &
						inet_aton($MANAGEMENT_NETMASK));
			
			#
			# Ick, hack to get management interfaces onto
			# a different subnet in the dhcpd file. Needs to
			# be further generalized.
			#
			if ($network eq $m_network) {
			    push(@management_ifaces, [$node_id, $ip, $mac]);
			    next;
			}
370 371 372 373
			$hostname =
			    "${spaces}\toption host-name \"${node_id}-mng\";\n";
			goto spit;
		    }
374

375
		    #
376 377 378 379
		    # Neuter dhcp nodes reserved to experiments which
		    # are marked for external node management.  Some
		    # other entity will be responsible for
		    # bootstrapping them in this case.
380
		    #
381
		    if ($extman) {
382 383 384 385
			$hostname = "${spaces}\toption host-name ".
			    "\"$node_id\";\n";
			$booting  = "${spaces}\tignore booting;\n";
			goto spit;
386 387
		    }

388 389 390
		    #
		    # Do different stuff for elabinelab / plab nodes.
		    #
391
		    if (defined($tag) && defined($servernodes{$tag}) &&
392 393 394 395 396 397
			(($row{"inner_elab_boot"} == 1 &&
			  defined($row{"inner_elab_role"}) &&
			  $row{"inner_elab_role"} eq "node") ||
			 ($row{"plab_boot"} == 1 &&
			  defined($row{"plab_role"}) &&
			  $row{"plab_role"} eq "node"))) {
398

Mike Hibler's avatar
Mike Hibler committed
399 400 401 402 403 404 405 406 407 408
			#
			# XXX it is not yet completely clear what all we
			# need to do, or not do, for plab nodes.
			#
			if ($row{"plab_boot"} == 1) {
			    $hostname =
				"${spaces}\toption host-name \"$node_id\";\n";
			    $next_server = "${spaces}\tnext-server " .
				$servernodes{$tag} . ";\n";
			} else {
409 410 411 412 413 414 415 416 417 418 419
			    if ($singlectlnet{$tag}) {
				$hostname = "${spaces}\toption host-name ".
				    "\"$node_id\";\n";
				$booting  = "${spaces}\tignore booting;\n";
			    }
			    else {
				$next_server = "${spaces}\tnext-server " .
				    $servernodes{$tag} . ";\n";
				$dns = "${spaces}\toption ".
				    "domain-name-servers 1.1.1.1;\n";
			    }
420
			}
421
		    }
422 423 424
		    #
		    # Defer to subbosses for dhcp and/or tftp if delegated.
		    #
425 426 427 428
		    elsif (defined $dhcp_subbosses{$node_id}) {
			$hostname =
			    "${spaces}\toption host-name \"$node_id\";\n";
	                $booting = "${spaces}\tignore booting;\n";
429 430 431 432
		        if (defined $tftp_subbosses{$node_id}) {
			    $next_server = "${spaces}\tnext-server " .
			        $tftp_subbosses{$node_id} . ";\n";
		        }
433
		    }
434 435 436
		    #
		    # The default case: just a plain old TB node.
		    #
437 438 439
		    else {
			$hostname =
			    "${spaces}\toption host-name \"$node_id\";\n";
440

441 442 443 444
		        if (defined $tftp_subbosses{$node_id}) {
			    $next_server = "${spaces}\tnext-server " .
			        $tftp_subbosses{$node_id} . ";\n";
		        }
445

446 447 448 449
		        if (defined $bootinfo_subbosses{$node_id}) {
			    $bootinfo_server = "${spaces}\toption PXE.emulab-bootinfo " .
			        $bootinfo_subbosses{$node_id} . ";\n";
		        }
450

451
		    }
452

453 454
		    #
		    # Handle alternate boot program filename if it exists.
455 456 457 458 459 460 461
		    # Order is:
		    #
		    # 1. nodes.next_pxe_boot_path for node
		    # 2. nodes.pxe_boot_path for node
		    # 3. "pxe_boot_path" node_attribute for node
		    # 4. "pxe_boot_path" node_type_attribute for node type
		    # 5. don't set (i.e., use the dhcpd.conf global default).
462
		    #
463 464 465 466 467 468 469 470 471
		    # XXX Note that we cannot do this right now for
		    # boot_method == "pxelinux" nodes as they use the same
		    # file for their regular boot. We could do this in the
		    # future by taking advantage of the fact that pxelinux
		    # tries an order list of config files for each node.
		    #
		    my $nobj = Node->Lookup($node_id);
		    if (!$nobj) {
			fatal("Could not find Node info for $node_id");
472
		    }
473 474 475 476 477 478 479 480 481 482 483 484 485
		    if ($nobj->boot_method() ne "pxelinux") {
			my $pxeboot = $row{"next_pxe_boot_path"};
			if (!$pxeboot) {
			    $pxeboot = $row{"pxe_boot_path"};
			}
			$filename = SetupPXEBoot($node_id, $mac, 1, $pxeboot);
			if (!$filename) {
			    # Get the node or node type default
			    my $path = $nobj->default_pxeboot();
			    $filename = SetupPXEBoot($node_id, $mac, 0, $path);
			}
			if ($filename) {
			    $filename = "${spaces}\tfilename \"$filename\";\n";
486
			}
487
		    }
488
		  spit:
489 490 491 492
		    # Need to make MAC look right..
		    $mac =~ s/(..)\B/$1:/g;

		    print $OUT "${spaces}host $ip {\n";
493 494
		    print $OUT $filename
			if (defined($filename));
495
		    print $OUT $next_server;
496
		    print $OUT $bootinfo_server;
497
		    print $OUT $dns;
498
		    print $OUT $booting;
499 500 501 502
		    print $OUT "${spaces}\thardware ethernet $mac;\n";
		    print $OUT $hostname;
		    print $OUT "${spaces}\tfixed-address $ip;\n";
		    print $OUT "${spaces}}\n\n";
503
		}
504 505 506 507 508 509 510
	} elsif (/^(\s*)\%\%management(_nodes)?\s*$/) {
	    # UW added this change, to allow for a different template file
	    # definition of the management nodes.
	    die("ERROR: more than one of '\%\%management' and/or ".
		"'\%\%management_nodes' has been used in the ".
		"'$DHCPD_TEMPLATE' file!") if (defined($mgmt_just_nodes));
	    $mgmt_just_nodes = ($2) ? 1 : 0;
511 512 513 514 515 516
	    my $spaces = $1;
	    my $net  = $MANAGEMENT_NETWORK;
	    my $mask = $MANAGEMENT_NETMASK;
	    my $gate = $MANAGEMENT_GATEWAY;
	    
	    if (@management_ifaces) {
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
		#
		# UW wants more control in the template file. We still
		# spit out the nodes, but without the subnet/group wrapper,
		# which they put into the template file directly.
		#
		my @lines;
		if (!$mgmt_just_nodes) {
		    @lines =
			("subnet $net netmask $mask {",
			 "    option subnet-mask          $mask;",
			 "    option routers              $gate;",
			 "    option domain-name-servers  $BOSSNODE_IP;",
			 "    option domain-name          \"$OURDOMAIN\";",
			 "    next-server                 $BOSSNODE_IP;",
			 "",
			 "",
			 "    group {");
		}
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		foreach my $ref (@management_ifaces) {
		    my ($hostname, $ip, $mac) = @{$ref};
		    # naming convention.
		    $hostname .= "-mng";

		    # Need to make MAC look right..
		    $mac =~ s/(..)\B/$1:/g;

		    push(@lines,
			 "        host $ip {",
			 "            hardware ethernet $mac;",
			 "            option host-name \"$hostname\";",
			 "            fixed-address $ip;",
			 "        }\n");
		}
550 551 552
		if (!$mgmt_just_nodes) {
		    @lines = (@lines, "   }", "}");
		}
553 554 555 556
		foreach my $line (@lines) {
		    print $OUT $spaces . $line . "\n";
		}
	    }
557
	} else {
558 559
	    # It's a regular line
	    print $OUT $_;
560 561
	}
}
562 563 564 565 566

if ($install) {
    close(OF) or
	fatal("Could not close $outfile");

567 568 569 570 571
    if (-e $DHCPD_TAIL) {
	system("cat $DHCPD_TAIL >> $outfile") == 0 or
	    fatal("Could not add tail to $outfile");
    }

572 573 574 575 576 577
    if (-e $DHCPD_CONF) {
	system("cp -fp $DHCPD_CONF ${DHCPD_CONF}.old") == 0 or
	    fatal("Could not backup copy of ${DHCPD_CONF}");
    }
    system("mv -f $outfile $DHCPD_CONF") == 0 or
	    fatal("Could not install new ${DHCPD_CONF}");
578 579 580 581 582 583 584 585 586 587 588 589

    if ($restart) {
	$dpid = `cat /var/run/dhcpd.pid`;
	chomp($dpid);
        # untaint
	if ($dpid =~ /^([\d]+)$/) {
	    $dpid = $1;
	}
	else {
	    fatal("Bad pid for DHCPD: $dpid");
	}
	if (kill('TERM', $dpid) == 0) {
590 591 592 593
	    fatal("Could not kill(TERM) process $dpid (dhcpd): $!.\n".
		  "    dhcpd will NOT be restarted.\n".
		  "    Restart manually with ".
		  "'/usr/local/etc/rc.d/2.dhcpd.sh start'\n");
594
	}
595

596 597
        my $old_uid = $UID;
        $UID = $EUID;
598 599 600
        for my $subboss (keys %subboss_dhcp_servers) {
            RestartSubbossDhcp($subboss, $restart);
        }
601
        $UID = $old_uid;
602 603
    }

604
    TBScriptUnlock();
605
}
606
exit(0);
607

608 609
#
# Special handling of pxe_boot_path argument.
610
#
611
# Any specified pxeboot path must start with PXEBOOTDIR (/tftpboot).
612 613 614 615 616 617
# If path starts with $PXELINUXPREFIX (/tftpboot/pxelinux/) or
# $GPXELINUXPREFIX (/tftpboot/gpxelinux), then the remainder of the
# path identifies the pxelinux configuration file from $PXELINUXCONFIG
# (/tftpboot/pxelinux.cfg) to use. This config file for the node
# (MAC address) will be symlinked to this config file. The PXE boot
# program will be set to either $PXELINUXBOOT or $GPXELINUXBOOT.
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
#
# Returns the string to be used for the "filename" option in dhcpd.conf.
#
sub SetupPXEBoot($$$$)
{
    my ($node, $mac, $clear, $str) = @_;

    # get rid of any old pxelinux magic
    if ($clear) {
	ClearPXEBoot($node, $mac);
    }

    return undef
	if (!defined($str));

    # don't allow if outside TFTP directory
    if ($str !~ /^$PXEBOOTDIR\// || $str =~ /\.\./) {
	print STDERR "$node: ignoring bogus pxe_boot_path '$str'\n";
	return undef;
    }

    # if it starts with the magic pxelinux prefix, setup a config file
640 641 642
    if ($str =~ /^($PXELINUXPREFIX|$GPXELINUXPREFIX)\/(.*)$/) {
	my $path = $1;
	my $cfile = $2;
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	my $cpath = "$PXEBOOTDIR/$PXELINUXCONFIG";

	if (! -r "$cpath/$cfile") {
	    print STDERR "$node: ignoring invalid pxelinux config '$cfile'\n";
	    return undef;
	}

	#
	# XXX ugh. In an inner elab with an inner control network,
	# pxelinux will use the MAC address of the real control net interface
	# to construct its config file name. So we need to look that up.
	#
	if ($ELABINELAB) {
	    my $qr = DBQueryWarn("select MAC from interfaces where ".
				 " node_id='$node' and role='outer_ctrl'");
	    if ($qr && $qr->numrows) {
		my %row = $qr->fetchhash();
		if (defined($row{"MAC"})) {
		    $mac = $row{"MAC"};
		}
	    }
	}

	if ($mac =~ /^(..)(..)(..)(..)(..)(..)$/) {
	    $macname = "01-$1-$2-$3-$4-$5-$6";
	} else {
	    print STDERR "$node: ignoring unparsable MAC address '$mac'\n";
	    return undef;
	}

	if ($install) {
	    if (-e "$cpath/$macname") {
		if (!unlink("$cpath/$macname")) {
		    print STDERR "$node: could not remove old pxelinux config\n";
		    return undef;
		}
	    }
	    if (!symlink($cfile, "$cpath/$macname")) {
		print STDERR "$node: could not symlink to pxelinux config '$cfile'\n";
		return undef;
	    }
	} else {
	    print STDERR "WARNING: not changing pxelinux config for $node\n";
	}

	# return the pxelinux binary
689 690 691 692 693
	if ($path eq $PXELINUXPREFIX) {
	    $str = "$PXEBOOTDIR/$PXELINUXBOOT";
	} else {
	    $str = "$PXEBOOTDIR/$GPXELINUXBOOT";
	}
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 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    }

    return $str;
}

#
# Make sure there is no pxelinux config for the given node
#
sub ClearPXEBoot($$)
{
    my ($node, $mac) = @_;
    my $cpath = "$PXEBOOTDIR/$PXELINUXCONFIG";

    #
    # XXX ugh. In an inner elab with an inner control network,
    # pxelinux will use the MAC address of the real control net interface
    # to construct its config file name. So we need to look that up.
    #
    if ($ELABINELAB) {
	my $qr = DBQueryWarn("select MAC from interfaces where ".
			     " node_id='$node' and role='outer_ctrl'");
	if ($qr && $qr->numrows) {
	    my %row = $qr->fetchhash();
	    if (defined($row{"MAC"})) {
		$mac = $row{"MAC"};
	    }
	}
    }

    if ($mac =~ /^(..)(..)(..)(..)(..)(..)$/) {
	$macname = "01-$1-$2-$3-$4-$5-$6";
	if ($install) {
	    if (-e "$cpath/$macname" && !unlink("$cpath/$macname")) {
		print STDERR "$node: could not remove old pxelinux config\n";
	    }
	} else {
	    print STDERR "WARNING: not changing pxelinux config for $node\n";
	}
    }
}

735 736 737 738 739
#
# Die.
#
sub fatal {
    my $msg = $_[0];
740

741 742
    TBScriptUnlock()
	if ($install);
743

744 745
    die("*** $0:\n".
	"    $msg\n");
746
}
747

748 749 750
sub RestartSubbossDhcp($$)
{
        my ($subboss, $restart) = @_;
751
        
752
        my $cmd = "ssh -o ConnectTimeout=5 $subboss @prefix@/sbin/subboss_dhcpd_makeconf";
753 754 755 756 757 758 759 760

        $cmd .= " -r" if ($restart);

        print "Restarting dhcpd on subboss $subboss\n";
        if (system($cmd)) {
	    print STDERR "Failed to restart dhcpd on $subboss\n";
	}
}