parse-ns.in 11.3 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-2004 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 Getopt::Std;
11
use Socket;
12 13
    
#
14 15 16 17 18 19
# Parse an ns file. Since the parser runs arbitrary NS file for the user,
# this cannot be safely done on boss without jumping through huge hoops
# to secure tcl and the DB. Yuck! So, instead of running the parser on boss,
# we run it over on ops. This first version operates like this:
#
# NB: This script is setuid.
20 21 22 23
#
sub usage()
{
    print STDOUT
24
	"Usage: parse-ns [options] pid gid eid nsfile\n".
25 26 27 28
        "Where options and arguments are those required by parse.tcl\n";
 
    exit(-1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
my $optlist  = "nqap";
30
my $anonmode = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
my $passmode = 0;
32 33
my $impotent = 0;
my $debug    = 0;
34 35 36 37 38 39

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
40 41 42 43 44 45 46 47 48 49 50 51
my $CONTROL  = "@USERNODE@";
my $TESTMODE = @TESTMODE@;

# Locals
my $infile   = "parse.input";
my $outfile  = "parse.output";
my $SAVEUID  = $UID;
my $dbuid;
my $pid;
my $gid;
my $eid;
my $nsfile;
52 53 54 55 56 57 58 59 60

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

#
# Untaint the path
# 
61
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/sbin:/usr/sbin";
62 63
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
if ($TESTMODE) {
    # In testmode, drop privs (my devel tree at home is TESTMODE=1)
    $EUID = $UID;
}
elsif ($EUID != 0) {
    # We don't want to run this script unless its the real version.
    die("*** $0:\n".
	"    Must be root! Maybe its a development version?\n");
}

# This script is setuid, so please do not run it as root. Hard to track
# what has happened.
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root! Its already setuid!\n");
}

81 82 83 84
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
85
use libdb;
86 87 88
use libtestbed;

#
89 90
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
91
#
92 93 94 95 96 97 98 99 100 101
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"a"})) {
    $anonmode = 1;
}
if (defined($options{"n"})) {
    $impotent = 1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
102 103 104
if (defined($options{"p"})) {
    $passmode = 1;
}
105 106 107
if (@ARGV != 4 && @ARGV != 1) {
    usage();
}
108

109 110 111 112 113 114 115 116
if (@ARGV == 1) {
    $nsfile = $ARGV[0];
}
else {
    $pid   = $ARGV[0];
    $gid   = $ARGV[1];
    $eid   = $ARGV[2];
    $nsfile= $ARGV[3];
117 118 119
}

#
120
# Must taint check!
121
#
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
if ($nsfile =~ /^([-\w\/\.]+)$/) {
    $nsfile = $1;
}
else {
    die("Bad data in argument: $nsfile.");
}
   
if (defined($pid)) {
    if ($pid =~ /^([-\w]+)$/) {
	$pid = $1;
    }
    else {
	die("Bad data in argument: $pid.");
    }
    if ($eid =~ /^([-\w]+)$/) {
	$eid = $1;
    }
    else {
	die("Bad data in argument: $eid.");
    }
    if ($gid =~ /^([-\w]+)$/) {
	$gid = $1;
    }
    else {
	die("Bad data in argument: $gid.");
    }
}

# Remove temps cause of swapin by alternates.
unlink($infile);
unlink($outfile);

if (!$TESTMODE) {
    #
    # Get DB uid for sending over to ops. In testmode, it runs locally
    # as the user running the testsuite, obviously.
    #
    if (! UNIX2DBUID($UID, \$dbuid)) {
	die("*** $0:\n".
	    "    You do not exist in the Emulab Database!\n");
    }
163

164 165
    # Run as the user for most of this script.
    $EUID = $UID;
166 167
}

168 169 170 171 172 173 174
# Create the defs files.
GenDefsFile($infile);

# Now append the NS file to. This part is hokey. Fix later.
system("cat $nsfile >> $infile") == 0
    or die("Could not combine TCL defs file and the NS file!\n");

175 176 177 178 179 180 181 182 183 184 185 186
#
# Touch the output file, to avoid a root owned, 644 file. 
#
if (-e $outfile) {
    unlink($outfile) or
	die("*** $0:\n".
	    "    Could not unlink $outfile\n");
}
system("touch $outfile") == 0 or
    die("*** $0:\n".
	"    Could not create $outfile\n");

187
#
188 189 190
# Build up a new command line to run the parser on ops, writing the
# result back to a file if not in anonmode. Remember to tack on the
# user ID to flip to, when not in testmode.
191
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
192
my $cmdargs = "$TB/libexec/ns2ir/parse.proxy ";
193 194 195
$cmdargs .= " -u $dbuid"
    if (defined($dbuid));
$cmdargs .= " -a"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
196
    if ($anonmode);
197 198 199 200
$cmdargs .= " -n"
    if (defined($options{"n"}));
$cmdargs .= " -q"
    if (defined($options{"q"}));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201 202
$cmdargs .= " -p"
    if ($passmode);
203 204
$cmdargs .= " $pid $gid $eid"
    if (!$anonmode);
205 206

#
207 208
# Run parser, redirecting stdout to a file to capture the parser results.
# Must flip to real root to run ssh.
209
#
210
if ($TESTMODE) {
211
    #
212 213
    # In testmode, no ssh. Just run locally.
    #
214
    system("$cmdargs < $infile >> $outfile");
215 216
}
else {
217
    #
218 219 220 221
    # In non-testmode, must flip to real root for the ssh, and then flip
    # back afterwards.
    # 
    $EUID = $UID = 0;
222
    system("sshtb -host $CONTROL $cmdargs < $infile >> $outfile");
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    $EUID = $UID = $SAVEUID;
}

# 
# Error output went to stderr, so the caller captures that. 
# 
if ($?) {
    my $exit_status = $? >> 8;
    
    die("Parser exited with error code $exit_status!\n");
}
exit(0)
    if ($impotent);

#
# Run the XML converter on the output. 
#
240
system("$TB/libexec/xmlconvert -p -x $outfile $pid $eid");
241 242 243 244 245 246 247 248 249 250

if ($?) {
    my $exit_status = $? >> 8;
    
    die("XML converter exited with error code $exit_status!\n");
}

#
# Now we have to fix up one minor thing; widearea tunnel IPs. These have
# to be unique, but without the DB to ask, there is no easy way to arrange
251 252
# that.

253
my %subnetmap = ();
254
my $WANETMASK = "255.255.255.248";
255 256 257 258 259 260 261 262 263 264 265 266

my $query_result =
    DBQueryFatal("select vname,ips from virt_nodes ".
		 "where pid='$pid' and eid='$eid'");
while (my ($vname,$ips) = $query_result->fetchrow_array()) {
    my @newiplist = ();
    my $newips;

    foreach my $ipinfo (split(" ", $ips)) {
	my ($port,$ip) = split(":", $ipinfo);
	my ($a,$b,$c,$d) = ($ip =~ /(\d+).(\d+).(\d+).(\d+)/);

267
	if ($a eq "69" && $b eq "69") {
268 269 270
	    my $net = inet_ntoa(inet_aton($WANETMASK) & inet_aton($ip));
		
	    if (! defined($subnetmap{$net})) {
271 272 273 274 275 276
		DBQueryFatal("insert into ipsubnets ".
			     "values ('$pid','$eid', NULL)");
		my ($id) =
		    DBQueryFatal("select LAST_INSERT_ID() ".
				 "from ipsubnets")->fetchrow_array();

277 278 279 280 281 282 283 284 285 286
		# We are going to shift the bits up so they do not conflict
		# with the lower part.
		if ($id >= 8192) {
		    die("No more widearea subnets left!\n");
		}
		$id = $id << 3;

		my $cc = ($id & 0xff00) >> 8;
		my $dd = ($id & 0xf8);
		$subnetmap{$net} = inet_aton("192.168.$cc.$dd");
287
	    }
288 289
	    my $newsubnet = inet_ntoa($subnetmap{$net} | inet_aton("$d"));
	    push(@newiplist, "$port:${newsubnet}");
290 291 292 293
	}
	else {
	    push(@newiplist, $ipinfo);
	}
294
    }
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
    $newips = join(" ", @newiplist);
    
    if ($newips ne $ips) {
	DBQueryFatal("update virt_nodes set ips='$newips' ".
		     "where vname='$vname' and pid='$pid' and eid='$eid'");
    }
}
#
# So, if we ended up changing any, we have look for corresponding entries
# in virt_trafgens, and fix them too.
#
if (keys(%subnetmap)) {
    $query_result =
	DBQueryFatal("select vnode,vname,ip,target_ip from virt_trafgens ".
		     "where pid='$pid' and eid='$eid'");
    while (my ($vnode,$vname,$ip,$dstip) = $query_result->fetchrow_array()) {
	my ($a,$b,$c,$d) = ($ip =~ /(\d+).(\d+).(\d+).(\d+)/);
	my $newip        = $ip;
	my $newdstip     = $dstip;
314
	my $net		 = inet_ntoa(inet_aton($WANETMASK) & inet_aton($ip));
315
	
316 317
	if (defined($subnetmap{$net})) {
	    $newip = inet_ntoa($subnetmap{$net} | inet_aton("$d"));
318 319
	}
	($a,$b,$c,$d) = ($dstip =~ /(\d+).(\d+).(\d+).(\d+)/);
320 321 322
	$net          = inet_ntoa(inet_aton($WANETMASK) & inet_aton($dstip));
	if (defined($subnetmap{$net})) {
	    $newdstip = inet_ntoa($subnetmap{$net} | inet_aton("$d"));
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
	}
	if ($ip ne $newip || $dstip ne $newdstip) {
	    DBQueryFatal("update virt_trafgens set ".
			 "       ip='$newip',target_ip='$newdstip' ".
			 "where pid='$pid' and eid='$eid' and ".
			 "      vnode='$vnode' and vname='$vname'");
	}
    }
}
exit(0);


#
# Open up a TCL file and write a bunch of TCL to it!
#
sub GenDefsFile($)
{
    my($fname) = $_[0];

    open(TCL, "> $fname")
	or die("Could not open TCL defs file for writing: $!\n");

    print TCL "namespace eval TBCOMPAT {\n";

    print TCL "# Event Object Types\n";
    my $query_result =
	DBQueryFatal("select idx,type from event_objecttypes");
    while (my ($idx,$type) = $query_result->fetchrow_array()) {
	print TCL "set objtypes($type) $idx\n";
    }
    print TCL "\n";

    print TCL "# Event Event Types\n";
    $query_result =
	DBQueryFatal("select idx,type from event_eventtypes");
    while (my ($idx,$type) = $query_result->fetchrow_array()) {
	print TCL "set eventtypes($type) $idx\n";
    }
    print TCL "\n";
    
    print TCL "# Node Types\n";
    $query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
365
	DBQueryFatal("select type,isremotenode,isvirtnode,osname,issubnode ".
366 367
		     " from node_types as nt ".
		     "left join os_info as o on nt.osid=o.osid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
368 369
    while (my ($type,$isrem,$isvirt,$osid,$issubnode) =
	   $query_result->fetchrow_array()) {
370 371 372
	print TCL "set hwtypes($type) 1\n";
	print TCL "set isremote($type) $isrem\n";
	print TCL "set isvirt($type) $isvirt\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
373
	print TCL "set issubnode($type) $issubnode\n";
374 375 376 377 378 379 380
	print TCL "set default_osids($type) \"$osid\"\n"
	    if (defined($osid) && $osid ne "");
    }
    print TCL "\n";
    
    print TCL "# Node Classes\n";
    $query_result =
Robert Ricci's avatar
Robert Ricci committed
381 382 383 384
	DBQueryFatal("select class,isremotenode,isvirtnode,issubnode ".
		     "from node_types");
    while (my ($class,$isrem,$isvirt,$issubnode) =
	   $query_result->fetchrow_array()) {
385 386 387
	print TCL "set hwtypes($class) 1\n";
	print TCL "set isremote($class) $isrem\n";
	print TCL "set isvirt($class) $isvirt\n";
Robert Ricci's avatar
Robert Ricci committed
388
	print TCL "set issubnode($class) $issubnode\n";
389 390 391
    }
    print TCL "\n";

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    print TCL "# Node Aux Types\n";
    $query_result =
	DBQueryFatal("select at.auxtype,".
		     "       isremotenode,isvirtnode,osname,issubnode ".
		     " from node_types_auxtypes as at ".
		     "left join node_types as nt on nt.type=at.type ".
		     "left join os_info as o on nt.osid=o.osid");
    while (my ($type,$isrem,$isvirt,$osid,$issubnode) =
	   $query_result->fetchrow_array()) {
	print TCL "set hwtypes($type) 1\n";
	print TCL "set isremote($type) $isrem\n";
	print TCL "set isvirt($type) $isvirt\n";
	print TCL "set issubnode($type) $issubnode\n";
	print TCL "set default_osids($type) \"$osid\"\n"
	    if (defined($osid) && $osid ne "");
    }
    print TCL "\n";
    
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    print TCL "# Node Permissions\n";
    my %nodeperms = ();
    $query_result =
	DBQueryFatal("select type,pid from nodetypeXpid_permissions");
    while (my ($type,$pid) = $query_result->fetchrow_array()) {
	if (! defined($nodeperms{$type})) {
	    $nodeperms{$type} = [];
	}
	push(@{$nodeperms{$type}}, $pid);
    }
    foreach my $type (keys(%nodeperms)) {
	my @pids = @{$nodeperms{$type}};

	print TCL "set nodetypeXpid_permissions($type) [list @pids]\n";
    }
    print TCL "\n";

    if (defined($pid)) {
	print TCL "# OSIDs\n";
	$query_result =
	    DBQueryFatal("select osname from os_info ".
			 "where shared=1 or pid='$pid'");
		     
	while (my ($osname) = $query_result->fetchrow_array()) {
	    print TCL "set osids($osname) 1\n";
	}
	print TCL "\n";
437 438
    }

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    if (defined($pid)) {
	# Load reserved nodes, for swapmodify.
	$query_result = 
	    DBQueryFatal("select r.vname,r.node_id,n.type from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "where r.pid='$pid' and r.eid='$eid'");

	print TCL "# Reserved Nodes\n";
	while (($vname,$reserved,$type) = $query_result->fetchrow_array()) {
	    print TCL "lappend reserved_list \"$vname\"\n";
	    print TCL "set reserved_type($vname) \"$type\"\n";
	    print TCL "set reserved_node($vname) \"$reserved\"\n";
	}
    }

454 455
    print TCL "}\n";
    close(TCL);
456
}