fetchtar.proxy.in 5.78 KB
Newer Older
1
#!/usr/bin/perl -w
2
#
Leigh Stoller's avatar
Leigh Stoller committed
3
# Copyright (c) 2003-2019 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:hdc";
Leigh Stoller's avatar
Leigh Stoller committed
42
my $debug   = 0;
43
my $quiet   = 0;
44
my $hashit  = 0;
45
my $continue= 0;
46
my $user;
47
my $grouplist;
48 49 50 51 52 53 54 55

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

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

#
# 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!");
}

90 91
my %options = ();
if (! getopts($optlist, \%options)) {
92 93
    usage();
}
Leigh Stoller's avatar
Leigh Stoller committed
94 95 96
if (defined($options{"d"})) {
    $debug = 1;
}
97 98 99
if (defined($options{"c"})) {
    $continue = 1;
}
100 101 102 103 104 105
if (defined($options{"q"})) {
    $quiet = 1;
}
if (defined($options{"u"})) {
    $user = $options{"u"};
}
106 107 108 109 110 111
if (defined($options{"h"})) {
    $hashit = 1;
}
if (defined($options{"g"})) {
    $grouplist = $options{"g"};
}
112 113
if (@ARGV != 2 || !defined($user)) {
    usage();
114 115 116 117 118 119 120 121 122 123 124 125 126 127
}
my $URL = shift @ARGV;
my $localfile = shift @ARGV;

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

128
my (undef,undef,$unix_uid) = getpwnam($user) or
129 130 131
    die("*** $0:\n".
	"    No such user $user\n");

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

#
# 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.
#
154 155 156 157
my $glist;

if (defined($grouplist)) {
    $glist = join(" ", split(",", $grouplist));
158 159
}
else {
160 161 162 163 164 165 166 167 168
    $glist = `id -G $user`;

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

# 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);
Leigh Stoller's avatar
Leigh Stoller committed
202

203 204 205 206 207 208 209 210 211
    my $retry_options = "--retry-connrefused --waitretry=5 ";
    if (0) {
	$retry_options .=
	    "--tries 3 --timeout=5 --limit-rate=3000k";
    }
    else {
	$retry_options .=
	    "--dns-timeout=30 --connect-timeout=60 --read-timeout=600 ";
    }
Leigh Stoller's avatar
Leigh Stoller committed
212
    my $cmd = "nice -15 $WGET --no-check-certificate ".
213 214
	($continue ? "-c " : "") .
	"$retry_options --show-progress --progress=dot:giga ".
Leigh Stoller's avatar
Leigh Stoller committed
215 216 217 218 219 220
	"-q -O $localfile -i -";

    if ($debug) {
	print "running '$cmd'\n";
	print "url is '$URL'\n";
    }
221
    
Leigh Stoller's avatar
Leigh Stoller committed
222
    open(GET, "| $cmd")
223 224 225 226 227 228
	or die("Cannot start $WGET\n");
    print GET "$URL\n";
    if (!close(GET)) {
	exit(1);
    }
    exit(0);
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
}

#
# 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);
    
246
    print STDERR "*** $msg\n";
247
    exit(15);
248
} elsif ($exit_status) {
249 250
    print STDERR "*** wget exited with status $exit_status\n"
	if (!$quiet);
251 252 253 254 255 256 257
} 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!");
258

259 260 261 262 263 264 265 266
    if ($hashit) {
	my $hashfile = $localfile . ".sha1";
	system("$SHA1 $localfile > $hashfile");
    }
    if ($?) {
	die("Could not generate sha1 hash of $localfile\n");
    }
}
267 268
exit($exit_status >> 8);