--- loncom/auth/lontokacc.pm 2002/08/02 14:45:04 1.5 +++ loncom/auth/lontokacc.pm 2007/03/02 23:43:29 1.18 @@ -1,7 +1,7 @@ # The LearningOnline Network # Access Handler for User File Transfers # -# $Id: lontokacc.pm,v 1.5 2002/08/02 14:45:04 www Exp $ +# $Id: lontokacc.pm,v 1.18 2007/03/02 23:43:29 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -32,13 +32,19 @@ use strict; use Apache::Constants qw(:common :remotehost); use Apache::lonnet(); use Apache::File(); +use IO::Socket; sub handler { my $r = shift; - my $reqhost; - unless ($reqhost=$r->get_remote_host(REMOTE_DOUBLE_REV)) { - $r->log_reason("Spoof request ".$reqhost); - return FORBIDDEN; + my $reqhost = $r->get_remote_host(REMOTE_NOLOOKUP); + my @hostids= &Apache::lonnet::get_hosts_from_ip($reqhost); + if (!@hostids && $reqhost ne '127.0.0.1' ) { + $r->log_reason("Unable to find a host for ". + $r->get_remote_host(REMOTE_NOLOOKUP)); + return FORBIDDEN; + } + if ($reqhost eq '127.0.0.1') { + return OK; } my $readline; my $lontabdir=$r->dir_config('lonTabDir'); @@ -49,26 +55,31 @@ sub handler { return FORBIDDEN; } while ($readline=<$fh>) { - my ($id,$domain,$role,$name,$ip)=split(/:/,$readline); - if ($name =~ /$reqhost/i) { - $r->register_cleanup(\&removefile); - return OK; - } + $readline=~s/\s*$//; + my ($id,$domain,$role,$name)=split(/:/,$readline); + foreach my $hostid (@hostids) { + my $hostname=&Apache::lonnet::hostname($hostid); + if ($name =~ /^\Q$hostname\E$/i) { + return OK; + } + } } } - $r->log_reason("Invalid request for user file transfer from $reqhost", + $r->log_reason("Invalid request for user file transfer from $reqhost (".join(",",@hostids).")", $r->filename); return FORBIDDEN; } sub removefile { my $r=shift; - if ($r->status==HTTP_OK) { - &Apache::lonnet::logthis('Unlinking '.$r->filename); + if ($r->status==200) { + unlink($r->filename); + #&Apache::lonnet::logthis('Unlinking '.$r->filename); } else { - &Apache::lonnet::logthis("Failed to transfer ".$r->filename); + &Apache::lonnet::logthis('Failed to transfer '.$r->filename); } + return OK; } 1; __END__