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

#
# Spew a tar/rpm file to stdout. 
#
# The script is setuid and run from the webserver. 
#
sub usage()
{
18
    print STDERR "Usage: spewtarfile [-v] [t timestamp] <nodeid> <file>\n".
19 20 21
	"Spew a tar/rpm for an experiment.\n";
    exit(-1);
}
22 23 24 25

sub SpewFile();
sub VerifyFile();

26
my $optlist   = "vt:";
27 28
my $debug     = 1;
my $doverify  = 0;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
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.
#
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

#
# Load the Testbed support stuff. 
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

# 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.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"v"})) {
    $doverify = 1;
}
77 78 79 80 81 82 83 84 85 86
if (defined($options{"t"})) {
    $timestamp = $options{"t"};

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

#
# Untaint the arguments.
#
98
if ($nodeid =~ /^([-\w]+)$/) {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    $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");
}

#
# We need the pid,eid,gid,creator.
#
if (!NodeidToExp($nodeid, \$pid, \$eid)) {
    die("*** $0:\n".
	"    $nodeid is not allocated to an experiment!\n");
}
my $gid     = ExpGroup($pid, $eid);
my $creator = ExpLeader($pid, $eid);
my $bytelen = 0;

123 124 125
if (my $retval = VerifyFile()) {
    exit($retval);
}
126 127
exit(0)
    if ($doverify);
128 129
exit(3)
    if (SpewFile() != 0);
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 164 165 166 167 168 169 170 171 172 173 174
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;

    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);
175
    return 0;
176 177 178
}

#
179 180
# Verify that we can return this file, return error if not allowed.
# Otherwise return 0 for okay.
181 182 183 184 185 186 187 188 189 190 191
#
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";
	}
192
	return 1;
193 194 195 196 197 198 199 200 201
    }

    #
    # 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`;
202
    if ($translated =~ /^([-\@\w\.\/]+)$/) {
203 204 205 206 207 208 209
	$translated = $1;
    }
    else {
	fatal("Bad data returned by realpath: $translated");
    }

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

    #
    # Stat the file to confirm that its either owned by the experiment
    # creator, or in the gid of the experiment.
    #
226 227
    (undef,undef,undef,undef,$stat_uid,$stat_gid,undef,$bytelen,
     undef,$mtime) = stat($translated);
228 229 230 231 232 233 234 235 236 237 238 239 240 241

    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";
	}
242
	return 1;
243
    }
244 245 246 247 248 249 250 251 252

    #
    # 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);
    
253
    $file = $translated;
254
    return 0;
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
}

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

sub fatal {
    my $msg = $_[0];

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