spewrpmtar.in 6.95 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6
# All rights reserved.
#
7
use strict;
8 9
use English;
use Getopt::Std;
10
use POSIX qw(mktime);
11 12 13 14 15 16 17 18

#
# Spew a tar/rpm file to stdout. 
#
# The script is setuid and run from the webserver. 
#
sub usage()
{
19
    print STDERR "Usage: spewtarfile [-v] [t timestamp] <nodeid> <file>\n".
20 21 22
	"Spew a tar/rpm for an experiment.\n";
    exit(-1);
}
23
my $optlist   = "vt:";
24 25
my $debug     = 1;
my $doverify  = 0;
26 27 28 29 30 31 32 33 34 35 36 37 38 39
my $timestamp;			# GM Time.

#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Proceeding.
# $status > 0 - Expected error. No such file, not modified, etc.
#               1. File could not be verified.
#               2. File has not changed since timestamp.
#               3. File could not be opened for reading.
40 41 42 43 44 45 46 47 48 49 50 51 52 53
  
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
54 55 56 57 58 59
use Node;

# Protos
sub SpewFile();
sub VerifyFile();
sub fatal($);
60 61 62 63 64 65 66 67 68 69 70 71

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
72
my %options = ();
73 74 75 76 77 78
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"v"})) {
    $doverify = 1;
}
79 80 81 82 83 84 85 86 87 88
if (defined($options{"t"})) {
    $timestamp = $options{"t"};

    if ($timestamp =~ /^([\d]+)$/) {
	$timestamp = $1;
    }
    else {
	die("*** Bad data in timestamp: $timestamp\n");
    }
}
89 90 91 92 93 94 95 96 97
if (@ARGV != 2) {
    usage();
}
my $nodeid  = $ARGV[0];
my $file    = $ARGV[1];

#
# Untaint the arguments.
#
98
if ($nodeid =~ /^([-\w]+)$/) {
99 100 101 102 103 104 105 106 107 108 109 110 111
    $nodeid = $1;
}
else {
    die("*** Bad data in nodeid: $nodeid\n");
}
# Note different taint check (allow /).
if ($file =~ /^([-\@\w\.\/]+)$/) {
    $file = $1;
}
else {
    die("*** Bad data in argument: $file");
}

112 113 114 115 116 117 118
my $node = Node->Lookup($nodeid);
if (!defined($node)) {
    die("*** $0:\n".
	"    $nodeid does not exist!\n");
}
my $experiment = $node->Reservation();
if (!defined($experiment)) {
119 120 121
    die("*** $0:\n".
	"    $nodeid is not allocated to an experiment!\n");
}
122 123 124
my $pid     = $experiment->pid();
my $eid     = $experiment->eid();
my $gid     = $experiment->gid();
125
my $creator = $experiment->GetCreator()->uid();
126 127
my $bytelen = 0;

128 129 130
if (my $retval = VerifyFile()) {
    exit($retval);
}
131 132
exit(0)
    if ($doverify);
133 134
exit(3)
    if (SpewFile() != 0);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
exit(0);

#
# Spew out a file. 
#
sub SpewFile()
{
    open(FD, "< $file")
	or fatal("Could not open $file!\n");

    #
    # Deal with NFS read failures
    #
    my $foffset = 0;
    my $retries = 5;
150
    my $buf;
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

    while ($bytelen) {
	my $rlen = sysread(FD, $buf, 8192);

	if (! defined($rlen)) {
	    #
	    # Retry a few times on error to avoid the
	    # changing-exports-file server problem.
	    #
	    if ($retries > 0 && sysseek(FD, $foffset, 0)) {
		$retries--;
		sleep(1);
		next;
	    }
	    fatal("Error reading $file: $!");
	}
        if ($rlen == 0) {
	    last;
	}
	if (! syswrite(STDOUT, $buf, $rlen)) {
	    fatal("Error writing file to stdout: $!");
	}
	$foffset += $rlen;
	$bytelen -= $rlen;
	$retries = 5;
    }
    if ($bytelen) {
	fatal("Did not get the entire file! $bytelen bytes left.");
    }
    close(FD);
181
    return 0;
182 183 184
}

#
185 186
# Verify that we can return this file, return error if not allowed.
# Otherwise return 0 for okay.
187 188 189 190 191 192 193 194 195 196 197
#
sub VerifyFile()
{
    #
    # First make sure the file is in the rpm or tar list for the node,
    # and that it exists and we can read it.
    #
    if (!VerifyTar() && !VerifyRPM()) {
	if ($debug) {
	    print STDERR "VerifyFile: Could not verify $file!\n";
	}
198
	return 1;
199 200 201 202 203 204 205 206 207
    }

    #
    # Now a few other checks.
    #
    # Use realpath to remove any symlinks to make sure we are not going
    # to hand out a file outside the appropriate files systems.
    #
    my $translated = `realpath $file`;
208
    if ($translated =~ /^([-\@\w\.\/]+)$/) {
209 210 211 212 213 214 215
	$translated = $1;
    }
    else {
	fatal("Bad data returned by realpath: $translated");
    }

    #
216 217
    # The file must reside in /proj/$pid/$eid, /groups/$pid/$gid
    # or /scratch/$pid.  Don't allow anything from /users!
218
    #
219
    if (! TBValidUserDir($translated, 0, undef, $pid, $gid)) {
220
	if ($debug) {
221 222 223
	    print STDERR "$translated is not in ",
			 join(' or ', TBValidUserDirList(undef, $pid, $gid)),
			 ".\n";
224
	}
225
	return 1;
226 227 228 229 230 231
    }

    #
    # Stat the file to confirm that its either owned by the experiment
    # creator, or in the gid of the experiment.
    #
232 233
    my (undef,undef,undef,undef,$stat_uid,$stat_gid,undef,$bytelen,
	undef,$mtime) = stat($translated);
234 235 236 237 238 239 240 241 242 243 244 245 246 247

    my ($unix_uid, $unix_gid, $unix_gname);

    TBGroupUnixInfo($pid, $gid, \$unix_gid, \$unix_gname) or
	fatal("No such group $pid/$gid\n");
    
    (undef,undef,$unix_uid) = getpwnam($creator) or
	fatal("No such user $creator\n");

    if ($stat_gid != $unix_gid &&
	$stat_uid != $unix_uid) {
	if ($debug) {
	    print STDERR "$translated has wrong uid/gid!\n";
	}
248
	return 1;
249
    }
250 251 252 253 254 255 256 257 258

    #
    # Check timestamp if supplied. Remember, we get GM timestamps, so
    # must convert the local stamp.
    #
    $mtime = mktime(gmtime($mtime));
    return 2
	if (defined($timestamp) && $timestamp >= $mtime);
    
259
    $file = $translated;
260
    return 0;
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
}

#
# Check the DB to make sure this is a valid TAR/RPM file for the node.
# Must pass a number of other checks too.
# 
sub VerifyTar()
{
    #
    # Get the tarball list from the DB. The requested path must be
    # on the list of tarballs for this node.
    #
    my $query_result =
	DBQueryFatal("select tarballs from nodes where node_id='$nodeid'");
    
    # No rpms/tarballs for the node in question.
    return 0
	if (!$query_result->numrows);

    #
    # The format is a colon separated list of "dir filename". We must find
    # the filename in the list.
    #
    my ($tarballs) = $query_result->fetchrow_array();

    foreach my $tarspec (split(":", $tarballs)) {
	my ($dir, $tar) = split(" ", $tarspec);
	return 1
	    if ($tar eq $file && -r $tar);
    }
    return 0;
}

sub VerifyRPM()
{
    my $query_result =
	DBQueryFatal("select rpms from nodes where node_id='$nodeid'");

    # No rpms/tarballs for the node in question.
    return 0
	if (!$query_result->numrows);

    #
    # The format is a colon separated list of filenames. We must find
    # the filename in the list.
    #
    my ($rpms) = $query_result->fetchrow_array();

    foreach my $rpm (split(":", $rpms)) {
	return 1
	    if ($rpm eq $file && -r $rpm);
    }
    return 0;
}

316 317 318
sub fatal($)
{
    my ($msg) = @_;
319 320 321 322 323

    SENDMAIL($TBOPS, "spewrpmtar:$file", $msg);
    die("*** $0:\n".
	"    $msg\n");
}