rungenilib.in 7.71 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT

#
Leigh Stoller's avatar
Leigh Stoller committed
4
# Copyright (c) 2000-2016 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 24
# 
# {{{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/>.
# 
# }}}
#
25
use strict;
26 27 28 29 30 31 32
use English;
use Getopt::Std;
use Socket;
use File::Basename;
use File::Temp qw(tempfile :POSIX );
use POSIX qw(:signal_h);
use POSIX ":sys_wait_h";
33
use File::stat;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#
# 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.
#
sub usage()
{
    print STDOUT "Usage: rungenilib [options] infile\n";
 
    exit(-1);
}
49
my $optlist    = "do:pP:b:W";
50 51 52
my $debug      = 0;
my $getparams  = 0;
my $paramfile;
53
my $ofile;
54
my $warningsfatal = 0;
55 56 57 58 59 60 61

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $CONTROL  = "@USERNODE@";
62
my $MAINSITE = @TBMAINSITE@;
63
my $TAR      = "/usr/bin/tar";
64 65 66 67 68 69 70 71 72 73 74 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 101 102 103 104 105 106

# Locals
my $SAVEUID  = $UID;
my $this_user;
my $file;

# Protos
sub fatal($);

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

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

if ($EUID != 0) {
    # We don't want to run this script unless its the real version.
    die("Must be root! Maybe its a development version?");
}

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
use User;

#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
107
my %options = ();
108 109 110 111 112 113
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
114 115 116
if (defined($options{"p"})) {
    $getparams = 1;
}
117 118 119 120 121 122 123 124 125 126 127
if (defined($options{"P"})) {
    $getparams = 1;
    $paramfile = $options{"P"};
    # Must taint check!
    if ($paramfile =~ /^([-\w\/\.]+)$/) {
	$paramfile = $1;
    }
    else {
	die("Bad data in argument: $paramfile.");
    }
}
128 129 130
if (defined($options{"W"})) {
    $warningsfatal = 1;
}
131 132 133 134 135 136 137 138 139 140
if (defined($options{"b"})) {
    $paramfile = $options{"b"};
    # Must taint check!
    if ($paramfile =~ /^([-\w\/\.]+)$/) {
	$paramfile = $1;
    }
    else {
	die("Bad data in argument: $paramfile.");
    }
}
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
if (defined($options{"o"})) {
    $ofile = $options{"o"};
}
if (@ARGV != 1) {
    usage();
}
$file = $ARGV[0];

#
# Must taint check!
#
if ($file =~ /^([-\w\/\.]+)$/) {
    $file = $1;
}
else {
    die("Bad data in argument: $file.");
}
if (defined($ofile)) {
    if ($ofile =~ /^([-\w\/\.]+)$/) {
	$ofile = $1;
    }
    else {
	die("Bad data in argument: $ofile.");
    }
}

#
# Get DB uid for sending over to ops.
#
$this_user = User->ThisUser();
if (! defined($this_user)) {
    tbdie("You ($UID) do not exist!");
}
# Run as the user for most of this script.
$EUID = $UID;

177 178 179 180 181
#
# If looking for the parameter definition block, then see if there
# are any define_parameter calls. If so, run the script to get the
# JSON block.
#
182 183
if ($getparams && !$paramfile &&
    system("egrep -q -s 'defineParameter' $file")) {
184 185 186 187 188 189 190 191 192
    # No parameters, return empty block.
    if (defined($ofile)) {
	system("echo '' > $ofile");
    }
    exit(0);
}

my $infile   = tmpnam();
my $outfile  = tmpnam();
193 194 195 196 197 198 199 200 201 202 203 204

#
# Touch the output file, to avoid a root owned, 644 file. 
#
system("touch $outfile") == 0 or
    fatal("Could not create $outfile");

#
# 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.
#
205 206 207
my $cmdargs = "$TB/libexec/rungenilib.proxy";

# lets try out the python jail
208 209 210
if ($MAINSITE) {
    $cmdargs .= " -J";
}
211

212 213
$cmdargs .= " -u " . $this_user->uid();
$cmdargs .= ($getparams ? " -p " : "");
214
$cmdargs .= ($warningsfatal ? " -W " : "");
215 216 217 218 219

#
# We want to send over both files via STDIN, so combine them, and pass
# the first file size with the -b option
#
220
if ($paramfile && !$getparams) {
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    system("cat $paramfile > $infile") == 0
	or fatal("Could not copy $paramfile to $infile");

    $cmdargs .= " -b " . stat($infile)->size;
    
    system("cat $file >> $infile") == 0
	or fatal("Could not concat $file to $infile");
}
else {
    system("cat $file > $infile") == 0
	or fatal("Could not copy $file to $infile");
}
$cmdargs = "sshtb -host $CONTROL $cmdargs < $infile";

if ($debug) {
    print $cmdargs . "\n";
}
238 239 240 241 242

#
# Run parser, redirecting stdout to a file to capture the parser results.
# Stderr is redirected to the ERR filehandle
# Must flip to real root to run ssh.
243

244
$EUID = $UID = 0;
245
open ERR, "$cmdargs 2>&1 >> $outfile |";
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
$EUID = $UID = $SAVEUID;

#
# Now read in the results from stderr.
#
my $errs = "";
while (<ERR>) {
    $errs .= $_;
}
close(ERR);
my $exit_status = $?;

if ($exit_status) {
    if (WIFSIGNALED($exit_status)) {
	# The POSIX module doesn't create constants for valid signals
	# (including SIGBUS), thus we have to do it the hard way.

	# Get the mapping from signal num. to name
	use Config;
	my (%sig_num, @sig_name);
	my @names = split ' ', $Config{sig_name};
	@sig_num{@names} = split ' ', $Config{sig_num};
	foreach (@names) {$sig_name[$sig_num{$_}] ||= $_}

	my $signal  = WTERMSIG($exit_status);
	my $signame = $sig_name[$signal];
	
	if (grep {$_ eq $signame} qw(ILL TRAP EMT FPE BUS SEGV SYS)) {
	    SENDMAIL($TBOPS, "geni-lib converter Crashed",
		     "$errs\n",
		     undef, undef, 
		     $file);
	}
	fatal("Failed to convert genilib script!");
    }
    if (defined($ofile)) {
	if (open(OFILE, "> $ofile")) {
	    print OFILE $errs;
	    close(OFILE);
	}
    }
    else {
	print STDERR $errs;
    }
    unlink($outfile);
    unlink($infile);
292

293 294 295
    # We want to pass along the exit status, since it indicates an internal
    # vs script error.
    exit($exit_status >> 8);
296
}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
#
# When getting the params, we get back a tar file. First file has the
# paramdefs and the other is the rspec. Break them apart.
#
if ($getparams) {
    my $files  = `$TAR tf $outfile`;
    my ($p,$r) = split(/\n/, $files);
    # Taint check.
    if ($p =~ /^([-\w\/\.]+)$/) {
	$p = $1;
    }
    else {
	fatal("Bad data in argument: $p.");
    }
    if ($r =~ /^([-\w\/\.]+)$/) {
	$r = $1;
    }
    else {
	fatal("Bad data in argument: $r.");
    }
    my $tfile = tmpnam();

    # Suck out params.
    system("$TAR -xOf $outfile $p > $tfile");
    if ($paramfile) {
	system("cat $tfile > $paramfile");
	system("$TAR -xOf $outfile $r > $tfile");
	system("/bin/mv $tfile $outfile");
	# We also write out the rspec below.
    }
    else {
	# Caller wants only the paramfile.
	system("/bin/mv $tfile $outfile");
    }
}
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
if (defined($ofile)) {
    system("cat $outfile > $ofile");
}
else {
    system("cat $outfile");
}
unlink($outfile);
unlink($infile);
exit(0);

sub fatal($) {
    my ($mesg) = $_[0];

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    unlink($outfile)
	if (defined($outfile));
    unlink($infile)
	if (defined($infile));
    exit(-1);
}