rungenilib.in 6.56 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:pb: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 62 63 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

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $CONTROL  = "@USERNODE@";

# 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.
#
105
my %options = ();
106 107 108 109 110 111
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}
112 113 114
if (defined($options{"p"})) {
    $getparams = 1;
}
115 116 117
if (defined($options{"W"})) {
    $warningsfatal = 1;
}
118 119 120 121 122 123 124 125 126 127
if (defined($options{"b"})) {
    $paramfile = $options{"b"};
    # Must taint check!
    if ($paramfile =~ /^([-\w\/\.]+)$/) {
	$paramfile = $1;
    }
    else {
	die("Bad data in argument: $paramfile.");
    }
}
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 163
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;

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#
# 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.
#
if ($getparams && system("egrep -q -s 'defineParameter' $file")) {
    # No parameters, return empty block.
    if (defined($ofile)) {
	system("echo '' > $ofile");
    }
    exit(0);
}

my $infile   = tmpnam();
my $outfile  = tmpnam();
179 180 181 182 183 184 185 186 187 188 189 190

#
# 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.
#
191 192 193
my $cmdargs = "$TB/libexec/rungenilib.proxy";

# lets try out the python jail
Leigh Stoller's avatar
Leigh Stoller committed
194
$cmdargs .= " -J";
195

196 197
$cmdargs .= " -u " . $this_user->uid();
$cmdargs .= ($getparams ? " -p " : "");
198
$cmdargs .= ($warningsfatal ? " -W " : "");
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 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
#
if ($paramfile) {
    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";
}
222 223 224 225 226

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

228
$EUID = $UID = 0;
229
open ERR, "$cmdargs 2>&1 >> $outfile |";
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 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
$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);
276 277 278
    # We want to pass along the exit status, since it indicates an internal
    # vs script error.
    exit($exit_status >> 8);
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
}
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);
}