fetchtar.proxy.in 5.21 KB
Newer Older
1
#!/usr/bin/perl -w
2
#
3
# Copyright (c) 2003-2014 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 25 26
#
use English;
use Getopt::Std;
use BSD::Resource;
27
use File::Basename;
28
use POSIX qw(:signal_h);
29
use strict;
30 31
    
#
32 33
# Fetch tarballs and RPMs on behalf of a user.
# This script runs on ops.
34 35 36
#
sub usage()
{
37
    print "Usage: $0 [-q] [-h] -u user [-g g1,g2,...] URL localfile\n";
38 39 40
 
    exit(-1);
}
41
my $optlist = "qu:g:h";
42
my $quiet   = 0;
43
my $hashit  = 0;
44
my $user;
45
my $grouplist;
46 47 48 49 50 51 52 53

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $FSDIR_PROJ = "@FSDIR_PROJ@";
my $FSDIR_GROUPS = "@FSDIR_GROUPS@";
54 55
my $FSDIR_SCRATCH = "@FSDIR_SCRATCH@";
my $ISFS = ("@USERNODE_IP@" eq "@FSNODE_IP@") ? 1 : 0;
56 57 58 59

# Locals
my $WGET = "/usr/local/bin/wget";
my $REALPATH = "/bin/realpath";
60
my $CHMOD = "/bin/chmod";
61
my $SHA1 = "/sbin/sha1";
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

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

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

#
# First option has to be the -u option, the user to run this script as.
#
if ($UID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!");
}

88 89
my %options = ();
if (! getopts($optlist, \%options)) {
90 91
    usage();
}
92 93 94 95 96 97
if (defined($options{"q"})) {
    $quiet = 1;
}
if (defined($options{"u"})) {
    $user = $options{"u"};
}
98 99 100 101 102 103
if (defined($options{"h"})) {
    $hashit = 1;
}
if (defined($options{"g"})) {
    $grouplist = $options{"g"};
}
104 105
if (@ARGV != 2 || !defined($user)) {
    usage();
106 107 108 109 110 111 112 113 114 115 116 117 118 119
}
my $URL = shift @ARGV;
my $localfile = shift @ARGV;

#
# Check arguments
#
if (!($user =~ /^([\w-]+)$/)) {
    die("*** $0:\n".
	"    Bad username given\n");
} else {
    $user = $1;
}

120
my (undef,undef,$unix_uid) = getpwnam($user) or
121 122 123
    die("*** $0:\n".
	"    No such user $user\n");

Leigh B Stoller's avatar
Leigh B Stoller committed
124
if (!($localfile =~ /^([\w\.\_\-+\/:]+)$/)) {
125 126 127
    die("*** $0:\n".
	"    Illegal local filename given: $localfile\n");
} else {
128 129 130 131
    $localfile   = $1;
    my $dirname  = dirname($localfile);
    my $basename = basename($localfile);
    my $realpath = `$REALPATH $dirname`;
132
    chomp $realpath;
133
    if (!TBValidUserDir($realpath, $ISFS)) {
134
	die("*** $0:\n".
135 136 137
	    "    Local file must be in one of " .
	    join(' or ', TBValidUserDirList()) . ".\n");
    }
138
    $localfile = "${realpath}/${basename}";
139 140 141 142 143 144 145
}

#
# 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.
#
146 147 148 149
my $glist;

if (defined($grouplist)) {
    $glist = join(" ", split(",", $grouplist));
150 151
}
else {
152 153 154 155 156 157 158 159 160
    $glist = `id -G $user`;

    if ($glist =~ /^([\d ]*)$/) {
	$glist = $1;
    }
    else {
	die("*** $0:\n".
	    "    Unexpected results from 'id -G $user': $glist\n");
    }
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
}

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

# Flip to user and never go back!
$GID            = $unix_gid;
$EGID           = $glist;
$EUID = $UID    = $unix_uid;
$ENV{'USER'}    = $user;
$ENV{'LOGNAME'} = $user;

#
# Fork a child process to run the wget
#
my $pid = fork();

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

#
# Child does the fetch, niced down, and exits
#
if (! $pid) {
    # Set the CPU limit for us.
    setrlimit(RLIMIT_CPU, 180, 180);
    
    # Give parent a chance to react.
    sleep(1);
194 195 196 197 198 199 200 201
    
    open(GET, "| nice -15 $WGET --no-check-certificate -q -O $localfile -i -")
	or die("Cannot start $WGET\n");
    print GET "$URL\n";
    if (!close(GET)) {
	exit(1);
    }
    exit(0);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
}

#
# Parent waits.
#
waitpid($pid, 0);
my $exit_status = $?;

#
# If the child was KILLed, then it overran its time limit.
# Send email. Otherwise, exit with result of child.
#
if (($exit_status & 0xff) == SIGKILL) {
    my $msg = "wget CPU Limit";

    SENDMAIL($TBOPS, "wget Exceeded CPU Limit", $msg);
    
219
    print STDERR "*** $msg\n";
220
    exit(15);
221
} elsif ($exit_status) {
222 223
    print STDERR "*** wget exited with status $exit_status\n"
	if (!$quiet);
224 225 226 227 228 229 230
} else {
    #
    # Change the permissions on the file so that other group members can
    # overwrite it
    #
    system("$CHMOD g+w $localfile") == 0
	or die ("*** ERROR - Unable to change permissions on $localfile!");
231

232 233 234 235 236 237 238 239
    if ($hashit) {
	my $hashfile = $localfile . ".sha1";
	system("$SHA1 $localfile > $hashfile");
    }
    if ($?) {
	die("Could not generate sha1 hash of $localfile\n");
    }
}
240 241
exit($exit_status >> 8);