spewrpmtar.in 7.59 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23
#
24
use strict;
25 26
use English;
use Getopt::Std;
27
use POSIX qw(mktime);
28
use Cwd qw(realpath);
29 30 31 32 33 34 35 36

#
# Spew a tar/rpm file to stdout. 
#
# The script is setuid and run from the webserver. 
#
sub usage()
{
37
    print STDERR "Usage: spewtarfile [-v] [t timestamp] <nodeid> <file>\n".
38 39 40
	"Spew a tar/rpm for an experiment.\n";
    exit(-1);
}
41
my $optlist   = "vt:";
42 43
my $debug     = 1;
my $doverify  = 0;
44 45 46 47 48 49 50 51 52 53 54 55 56 57
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.
58 59 60 61 62 63 64 65 66 67 68 69 70 71
  
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
72 73 74 75 76 77
use Node;

# Protos
sub SpewFile();
sub VerifyFile();
sub fatal($);
78 79 80 81 82 83 84 85 86 87 88 89

# 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.
#
90
my %options = ();
91 92 93 94 95 96
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"v"})) {
    $doverify = 1;
}
97 98 99 100 101 102 103 104 105 106
if (defined($options{"t"})) {
    $timestamp = $options{"t"};

    if ($timestamp =~ /^([\d]+)$/) {
	$timestamp = $1;
    }
    else {
	die("*** Bad data in timestamp: $timestamp\n");
    }
}
107 108 109 110 111 112 113 114 115
if (@ARGV != 2) {
    usage();
}
my $nodeid  = $ARGV[0];
my $file    = $ARGV[1];

#
# Untaint the arguments.
#
116
if ($nodeid =~ /^([-\w]+)$/) {
117 118 119 120 121 122 123 124 125 126 127 128 129
    $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");
}

130 131 132 133 134 135 136
my $node = Node->Lookup($nodeid);
if (!defined($node)) {
    die("*** $0:\n".
	"    $nodeid does not exist!\n");
}
my $experiment = $node->Reservation();
if (!defined($experiment)) {
137 138 139
    die("*** $0:\n".
	"    $nodeid is not allocated to an experiment!\n");
}
140 141 142
my $pid     = $experiment->pid();
my $eid     = $experiment->eid();
my $gid     = $experiment->gid();
143
my $creator = $experiment->GetCreator()->uid();
Leigh Stoller's avatar
Leigh Stoller committed
144
my $unix_gid= $experiment->GetGroup()->unix_gid();
145 146
my $bytelen = 0;

147 148 149
if (my $retval = VerifyFile()) {
    exit($retval);
}
150 151
exit(0)
    if ($doverify);
152 153
exit(3)
    if (SpewFile() != 0);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
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;
169
    my $buf;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

    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);
200
    return 0;
201 202 203
}

#
204 205
# Verify that we can return this file, return error if not allowed.
# Otherwise return 0 for okay.
206 207 208 209 210 211 212 213 214 215 216
#
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";
	}
217
	return 1;
218 219 220 221 222 223 224 225
    }

    #
    # 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.
    #
226
    my $translated = realpath($file);
227
    if ($translated =~ /^([-\@\w\.\/]+)$/) {
228 229 230 231 232 233 234
	$translated = $1;
    }
    else {
	fatal("Bad data returned by realpath: $translated");
    }

    #
235 236
    # The file must reside in /proj/$pid/$eid, /groups/$pid/$gid
    # or /scratch/$pid.  Don't allow anything from /users!
237
    #
238
    if (! TBValidUserDir($translated, 0, undef, $pid, $gid)) {
239
	if ($debug) {
240 241 242
	    print STDERR "$translated is not in ",
			 join(' or ', TBValidUserDirList(undef, $pid, $gid)),
			 ".\n";
243
	}
244
	return 1;
245 246 247 248 249 250
    }

    #
    # Stat the file to confirm that its either owned by the experiment
    # creator, or in the gid of the experiment.
    #
Leigh Stoller's avatar
Leigh Stoller committed
251
    my (undef,undef,undef,undef,$stat_uid,$stat_gid,undef,$length,
252
	undef,$mtime) = stat($translated);
253

Leigh Stoller's avatar
Leigh Stoller committed
254
    my (undef,undef,$unix_uid) = getpwnam($creator) or
255 256 257 258 259 260 261
	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";
	}
262
	return 1;
263
    }
264 265 266 267 268 269 270 271 272

    #
    # 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);
    
273
    $file = $translated;
Leigh Stoller's avatar
Leigh Stoller committed
274
    $bytelen = $length;
275
    return 0;
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
}

#
# 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;
}

331 332 333
sub fatal($)
{
    my ($msg) = @_;
334 335 336 337 338

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