spewrpmtar_verify.in 5.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#!/usr/bin/perl -wT
#
# Copyright (c) 2000-2012 University of Utah and the Flux Group.
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use POSIX qw(mktime);
28
use Cwd qw(realpath);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

#
# Spew a tar/rpm file to stdout. 
#
# The script is setuid and run from the webserver. 
#
sub usage()
{
    print STDERR "Usage: spewrpmtar_verify <key> <nodeid> <file>\n";
    exit(-1);
}
my $optlist   = "";
my $debug     = 1;

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TBLOGS      = "@TBLOGSEMAIL@";

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

# Protos
sub VerifyFile();
sub fatal($);

# 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.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 3) {
    usage();
}
my $key     = $ARGV[0];
my $nodeid  = $ARGV[1];
my $file    = $ARGV[2];

#
# Untaint the arguments.
#
if ($nodeid =~ /^([-\w]+)$/) {
    $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");
}

my $node = Node->Lookup($nodeid);
if (!defined($node)) {
    die("*** $0:\n".
	"    $nodeid does not exist!\n");
}
my $experiment = $node->Reservation();
if (!defined($experiment)) {
    die("*** $0:\n".
	"    $nodeid is not allocated to an experiment!\n");
}
my $pid     = $experiment->pid();
my $eid     = $experiment->eid();
my $gid     = $experiment->gid();
my $creator = $experiment->GetCreator()->uid();
my $unix_gid= $experiment->GetGroup()->unix_gid();

#
# We need the secret key to match
#
if (!$experiment->keyhash() || $experiment->keyhash() eq "") {
    fatal("No keyhash defined for $experiment!");
}
exit(1)
    if ($experiment->keyhash() ne $key);

exit(VerifyFile());

#
# Verify that we can return this file, return error if not allowed.
# Otherwise return 0 for okay.
#
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";
	}
	return 1;
    }

    #
    # 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.
    #
152
    my $translated = realpath($file);
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    if ($translated =~ /^([-\@\w\.\/]+)$/) {
	$translated = $1;
    }
    else {
	fatal("Bad data returned by realpath: $translated");
    }

    #
    # The file must reside in /proj/$pid/$eid, /groups/$pid/$gid
    # or /scratch/$pid.  Don't allow anything from /users!
    #
    if (! TBValidUserDir($translated, 0, undef, $pid, $gid)) {
	if ($debug) {
	    print STDERR "$translated is not in ",
			 join(' or ', TBValidUserDirList(undef, $pid, $gid)),
			 ".\n";
	}
	return 1;
    }

    #
    # Stat the file to confirm that its either owned by the experiment
    # creator, or in the gid of the experiment.
    #
    my (undef,undef,undef,undef,$stat_uid,$stat_gid,undef,$length,
	undef,undef) = stat($translated);

    my (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";
	}
	return 1;
    }
    return 0;
}

#
# 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) = @_;

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