rungenilib.proxy.in 8.84 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

#
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
use English;
27
use Getopt::Std;
28 29 30 31 32 33 34 35 36
use BSD::Resource;
use POSIX qw(:signal_h);
    
#
# Simply a wrapper for the geni-lib python environment
#
sub usage()
{
    print STDOUT
Leigh Stoller's avatar
Leigh Stoller committed
37
	"Usage: rungenilib.proxy [-J [-B iocagepath]] -u user [args ...]\n".
38 39
        "Where options and arguments are those required by geni-lib,\n".
	"except for -J which says to run script in a jail.\n";
40 41 42
 
    exit(-1);
}
Leigh Stoller's avatar
Leigh Stoller committed
43
my $optlist = "u:vpb:WJB:";
44 45 46
my $user;
my $getparams= 0;
my $paramsize;
47
my $warningsfatal = 0;
48
my $usejail = 0;
Leigh Stoller's avatar
Leigh Stoller committed
49
my $iocagepath;
50 51 52 53 54 55 56 57

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $TESTMODE = 0;
my $GENILIB  = "$TB/lib/geni-lib/";
58
my $JAILPROG = "$TB/libexec/genilib-jail";
59
my $TAR      = "/usr/bin/tar";
60 61 62 63 64
my $debug    = 0;

# Locals
my $tempdir = "/tmp/genilib-$$";
my $ifile   = "$$.py";
65 66
my $ofile   = "$$.out";
my $pfile   = "$$.json";
67

68 69
# Protos
sub fatal($);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

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

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

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

#
90
# Parse command arguments. Once we return from getopts, all that should
91 92
# left are the required arguments.
#
93 94
my %options = ();
if (! getopts($optlist, \%options)) {
95 96
    usage();
}
97 98 99 100 101 102 103 104 105 106 107 108
if (defined($options{"d"})) {
    $debug = 1;
}
if (defined($options{"p"})) {
    $getparams = 1;
}
if (defined($options{"b"})) {
    $paramsize = $options{"b"};
}
if (defined($options{"u"})) {
    $user = $options{"u"};
}
109 110 111
if (defined($options{"W"})) {
    $warningsfatal = 1;
}
112 113
if (defined($options{"J"})) {
    $usejail = 1;
Leigh Stoller's avatar
Leigh Stoller committed
114 115 116
    if (defined($options{"B"})) {
	$iocagepath = $options{"B"};
    }
117
}
118 119 120 121 122 123

#
# First option has to be the -u option, the user to run this script as.
# In testmode, we are not run as root, so run as the current user, and
# in the current directory (not invoked with ssh in testmode). 
#
124
my ($unix_uid,$unix_gid,$unix_glist);
125 126 127 128 129 130
if (! $TESTMODE) {
    if ($UID != 0) {
	die("*** $0:\n".
	    "    Must be root to run this script!");
    }

131
    (undef,undef,$unix_uid) = getpwnam($user) or
132 133 134 135 136 137 138 139
	die("*** $0:\n".
	    "    No such user $user\n");

    #
    # Need the entire group list for the user, cause of subgroups, and
    # cause thats the correct thing to do. Too bad perl does not have a
    # getgrouplist function like the C library.
    #
140 141 142
    $unix_glist = `id -G $user`;
    if ($unix_glist =~ /^([\d ]*)$/) {
	$unix_glist = $1;
143 144 145
    }
    else {
	die("*** $0:\n".
146
	    "    Unexpected results from 'id -G $user': $unix_glist\n");
147 148 149
    }

    # Need to split off the first group and create a proper list for $GUID.
150 151 152
    my @gglist   = split(" ", $unix_glist);
    $unix_gid    = $gglist[0];
    $unix_glist  = "$unix_gid $unix_glist";
153 154

    # Flip to user and never go back!
155 156 157 158 159 160 161
    if (!$usejail) {
	$GID            = $unix_gid;
	$EGID           = $unix_glist;
	$EUID = $UID    = $unix_uid;
	$ENV{'USER'}    = $user;
	$ENV{'LOGNAME'} = $user;
    }
162 163 164 165

    #
    # Create the tempdir and chmod it to keep people out.
    #
166 167 168 169 170 171
    # Note that in the $usejail case, the directory will be owned by root.
    # We leave it that way til the end so that a malicious user won't slip
    # in and insert symlinks for $ifile and $pfile. Since those files will
    # also be written as root, we need to make sure they aren't symlinks to
    # somewhere that the user could not normally access.
    #
172 173 174 175 176 177 178 179 180 181
    if (! mkdir($tempdir, 0750)) {
	die("Could not mkdir $tempdir: $!\n");
    }
    if (! chmod(0750, $tempdir)) {
	die("Could not chmod $tempdir to 0750: $!\n");
    }
    if (! chdir($tempdir)) {
	die("Could not chdir to $tempdir: $!\n");
    }
}
182 183 184 185
elsif ($usejail && $UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script with -J!");
}
186 187

#
188 189
# Take our input and write it to the temp file. If we are also getting
# a parameter file in the stream, write that to another file.
190
#
191 192 193 194 195 196 197 198 199 200 201 202 203 204
if (defined($paramsize) && $paramsize) {
    my $paramstr;
    
    open(PTMP, ">$pfile") ||
	fatal("Couldn't open $pfile\n");

    my $cc = read(STDIN, $paramstr, $paramsize);
    if (!defined($cc) || $cc != $paramsize) {
	fatal("Could not read paramter block from stdin");
    }
    print PTMP $paramstr;
    close(PTMP);
    chmod(0644, $pfile);
}
205 206 207 208 209 210 211 212
open(TMP, ">$ifile") ||
    fatal("Couldn't open $ifile\n");
while (<STDIN>) {
    print TMP $_;
}
close(TMP);
chmod(0644, $ifile);

213 214 215 216 217 218
#
# Need to add things to the environment for the portal module.
#
$ENV{'GENILIB_PORTAL_MODE'} = "Yep";
$ENV{'GENILIB_PORTAL_REQUEST_PATH'} = $ofile;
if ($getparams) {
219
    $ENV{'GENILIB_PORTAL_DUMPPARAMS_PATH'} = $pfile;
220 221 222 223
}
elsif (defined($paramsize) && $paramsize) {
    $ENV{'GENILIB_PORTAL_PARAMS_PATH'} = $pfile;
}
224 225 226
if ($warningsfatal) {
    $ENV{'GENILIB_PORTAL_WARNINGS_ARE_FATAL'} = "1";
}
227

228
my $exit_status;
229
if ($usejail) {
Leigh Stoller's avatar
Leigh Stoller committed
230 231
    my $bopt = (defined($iocagepath) ? "-B $iocagepath" : "");

232 233 234 235 236 237 238 239 240 241 242 243
    #
    # We are executing the command in a jail, fire off the jail script.
    # Note that the directory with all the files is owned by root and
    # not writable by the user. We have to do this since the jail script
    # runs as root and trusts us to validate all the files. The only way
    # we can do that is to prevent the user from messing with them til
    # the jail is done.
    #
    # Note that the jail script will copy the files again into the jail
    # name space. Those copies will be owned by the user so they can be
    # read/written.
    #
Leigh Stoller's avatar
Leigh Stoller committed
244
    $exit_status = system("$JAILPROG $bopt -u $user $ifile");
245

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    #
    # Now that we are done with the files, chown them to the user and
    # drop our permissions for any remaining activities (e.g., reading
    # the results file).
    #
    if ($unix_uid) {
	chown($unix_uid, $unix_gid, $tempdir, $ifile, $pfile, $ofile);
	$GID  = $unix_gid;
	$EGID = $unix_glist;
	$EUID = $UID = $unix_uid;
    }
} else {
    #
    # Fork a child process to run the parser in.
    #
    my $pid = fork();

    if (!defined($pid)) {
	die("*** $0:\n".
	    "    Could not fork a new process!");
    }
267 268

    #
269
    # Child runs the parser, niced down, and then exits.
270
    #
271 272 273 274 275 276
    if (! $pid) {
	# Set the CPU limit for us.
	setrlimit(RLIMIT_CPU, 600, 600);
	
	# Give parent a chance to react.
	sleep(1);
277

278 279 280 281 282 283
	#
	# Dup stdout to stderr; all output is considered error output.
	# which allows us to use stdout to send the rspec back to boss.
	#
	close(STDOUT);
	POSIX::dup2(fileno(STDERR), 1);
284

285 286 287 288 289 290 291 292
	exec("nice -15 /usr/local/bin/python $ifile");
	die("Could not exec the parser!\n");
    }

    #
    # Parent waits.
    #
    waitpid($pid, 0);
293
    $exit_status = $?;
294
}
295 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

#
# If the child was KILLed, then it overran its time limit.
# Send email. Otherwise, exit with result of child.
#
if (($exit_status & 0xff) == SIGKILL) {
    print STDERR "geni-lib Exceeded CPU Limit\n";
    $exit_status = 15;
}
elsif ($exit_status & 0xff) {
    # 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 = $exit_status & 0x7f;
    my $signame = $sig_name[$signal];
    
    print STDERR "geni-lib Died with SIG$signame.\n";
    $exit_status = 128 + $signal;
}
elsif ($exit_status) {
    $exit_status = $exit_status >> 8;
}
elsif (! -s $ofile)  {
    print STDERR "Cannot find the rspec, did you call printRspec(rspec)?\n";
    $exit_status = 1;
}
else {
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    if ($getparams) {
	if (! -e $pfile) {
	    if (system("egrep -q -s 'defineParameter' $ifile")) {
		# No parameters
		system("touch $pfile");
	    }
	    else {
		print STDERR
		    "Cannot find the params, did you call bindParameters?\n";
		$exit_status = 1;
	    }
	}
	if (!$exit_status) {
	    # Send back both params and rspec as a tar file.
	    system("$TAR cf - $pfile $ofile");
	}
    }
    else {
	# Send the rspec output to stdout (back to boss).
	system("cat $ofile");
    }
347 348 349 350 351 352 353 354
}
unless($debug) {
    if (-d $tempdir) {
	system("/bin/rm -r $tempdir");
    }
}
exit($exit_status);

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

358 359 360 361
    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}