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

#
4
# Copyright (c) 2000-2017 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
# Convert (run) a geni-lib script to generate the rspec or param block.
37 38 39
#
sub usage()
{
40 41 42 43 44 45 46 47
    print STDERR "Usage: rungenilib [options] infile\n";
    print STDERR "Options:\n";
    print STDERR "  -d       - Turn on debugging\n";
    print STDERR "  -o file  - Output file for rspec instead of stdout\n";
    print STDERR "  -p       - Generate parameter block info instead\n";
    print STDERR "  -P file  - Generate and write parameter block to file\n";
    print STDERR "  -b file  - Run script using the parameter defs in file\n";
    print STDERR "  -W       - Python warnings are fatal.\n";
48 49 50
 
    exit(-1);
}
51
my $optlist    = "do:pP:b:W";
52 53 54
my $debug      = 0;
my $getparams  = 0;
my $paramfile;
55
my $ofile;
56
my $warningsfatal = 0;
57 58 59 60 61 62 63

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $CONTROL  = "@USERNODE@";
64
my $MAINSITE = @TBMAINSITE@;
65
my $TAR      = "/usr/bin/tar";
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 107 108

# 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.
#
109
my %options = ();
110 111 112 113 114 115
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
116 117 118
if (defined($options{"p"})) {
    $getparams = 1;
}
119 120 121 122 123 124 125 126 127 128 129
if (defined($options{"P"})) {
    $getparams = 1;
    $paramfile = $options{"P"};
    # Must taint check!
    if ($paramfile =~ /^([-\w\/\.]+)$/) {
	$paramfile = $1;
    }
    else {
	die("Bad data in argument: $paramfile.");
    }
}
130 131 132
if (defined($options{"W"})) {
    $warningsfatal = 1;
}
133 134 135 136 137 138 139 140 141 142
if (defined($options{"b"})) {
    $paramfile = $options{"b"};
    # Must taint check!
    if ($paramfile =~ /^([-\w\/\.]+)$/) {
	$paramfile = $1;
    }
    else {
	die("Bad data in argument: $paramfile.");
    }
}
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 177 178
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;

179 180 181 182 183
#
# 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.
#
184 185
if ($getparams && !$paramfile &&
    system("egrep -q -s 'defineParameter' $file")) {
186 187 188 189 190 191 192 193 194
    # No parameters, return empty block.
    if (defined($ofile)) {
	system("echo '' > $ofile");
    }
    exit(0);
}

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

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

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

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

#
# We want to send over both files via STDIN, so combine them, and pass
# the first file size with the -b option
#
222
if ($paramfile && !$getparams) {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
    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";
}
240 241 242 243 244

#
# 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.
245

246
$EUID = $UID = 0;
247
open ERR, "$cmdargs 2>&1 >> $outfile |";
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 292 293
$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);
294

295 296 297
    # We want to pass along the exit status, since it indicates an internal
    # vs script error.
    exit($exit_status >> 8);
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 332 333
#
# 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");
    }
}
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
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);
}