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

#
4
# Copyright (c) 2000-2017, 2019 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:WN";
52 53 54
my $debug      = 0;
my $getparams  = 0;
my $paramfile;
55
my $ofile;
56
my $newgenilib = 0;
57
my $warningsfatal = 0;
58 59 60 61 62 63 64

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

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

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

my $infile   = tmpnam();
my $outfile  = tmpnam();
199 200 201 202 203 204 205 206 207 208 209 210

#
# 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.
#
211 212 213
my $cmdargs = "$TB/libexec/rungenilib.proxy";

# lets try out the python jail
214 215 216
if ($MAINSITE) {
    $cmdargs .= " -J";
}
217

218 219
$cmdargs .= " -u " . $this_user->uid();
$cmdargs .= ($getparams ? " -p " : "");
220
$cmdargs .= ($warningsfatal ? " -W " : "");
221
$cmdargs .= ($newgenilib ? " -N " : "");
222 223 224 225 226

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

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

251
$EUID = $UID = 0;
252
open ERR, "$cmdargs 2>&1 >> $outfile |";
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 294 295 296 297 298
$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);
299

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