Annotation of loncom/auth/lonracc.pm, revision 1.20

1.1       albertel    1: # The LearningOnline Network
                      2: # Access Handler for File Transfers
1.3       www         3: #
1.20    ! albertel    4: # $Id: lonracc.pm,v 1.19 2007/02/01 06:31:33 albertel Exp $
1.3       www         5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.1       albertel   28: 
                     29: package Apache::lonracc;
                     30: 
                     31: use strict;
                     32: use Apache::Constants qw(:common :remotehost);
1.17      albertel   33: use Apache::lonnet;
1.1       albertel   34: use Apache::File();
1.14      albertel   35: use IO::Socket;
1.1       albertel   36: 
1.5       albertel   37: sub subscribed {
                     38:     my ($filename,$id) = @_;
1.18      albertel   39: 
1.20    ! albertel   40:     return 0 if (!-e "$filename.subscription");
1.18      albertel   41: 
1.14      albertel   42:     my $hostname=$Apache::lonnet::hostname{$id};
                     43:     my (undef,undef,undef,undef,$ip) = gethostbyname($hostname);
1.18      albertel   44:     
                     45:     return 0 if (length($ip) != 4);
                     46: 
1.14      albertel   47:     $ip=inet_ntoa($ip);
1.18      albertel   48: 
                     49:     my $expr='^'.quotemeta($id).':'.quotemeta($ip).':';
                     50: 
                     51:     my $found=0;
                     52:     if (my $sh=Apache::File->new("$filename.subscription")) {
1.5       albertel   53: 	while (my $subline=<$sh>) { if ($subline =~ /$expr/) { $found=1; } }
                     54: 	$sh->close();
                     55:     }
                     56:     return $found;
                     57: }
                     58: 
1.1       albertel   59: sub handler {
                     60:     my $r = shift;
1.19      albertel   61: 
                     62:     my $filename=$r->filename;
                     63:     if (!-e $filename) {
                     64: 	return NOT_FOUND;
                     65:     }
                     66: 
1.15      albertel   67:     my $reqhost = $r->get_remote_host(REMOTE_NOLOOKUP);
1.16      albertel   68:     my %iphost=&Apache::lonnet::get_iphost();
                     69:     my $hostids=$iphost{$reqhost};
                     70:     if (!$hostids && $reqhost ne '127.0.0.1' ) {
                     71: 	$r->log_reason("Unable to find a host for ".
                     72: 		       $r->get_remote_host(REMOTE_NOLOOKUP));
1.15      albertel   73: 	return FORBIDDEN;
1.12      www        74:     }
1.16      albertel   75:     if ($reqhost eq '127.0.0.1') {
1.14      albertel   76: 	return OK;
1.1       albertel   77:     }
1.13      www        78:     my $return;
1.18      albertel   79:     my @ids;
1.16      albertel   80: 
1.18      albertel   81:     foreach my $id (@{$hostids}) {
1.16      albertel   82: 	my $uri =$r->uri;
1.18      albertel   83: 	if (($filename=~/\.meta$/) ||
                     84: 	    ($uri=~m|^/raw/uploaded|) ||
                     85: 	    (-e "$filename.$id") ||
                     86: 	    &subscribed($filename,$id) ) {
1.16      albertel   87: 	    return OK;
                     88: 	} else {
                     89: 	    $return=FORBIDDEN;
                     90: 	    push(@ids,$id);
1.13      www        91: 	}
                     92:     }
                     93:     if ($return == FORBIDDEN) {
                     94: 	$r->log_reason(join(':',@ids)." not subscribed", $r->filename);
                     95: 	return FORBIDDEN;
1.1       albertel   96:     }
                     97:     $r->log_reason("Invalid request for file transfer from $reqhost", 
                     98:                    $r->filename); 
                     99:     return FORBIDDEN;
                    100: }
                    101: 
                    102: 1;
                    103: __END__
                    104: 
1.4       harris41  105: =head1 NAME
                    106: 
                    107: Apache::lonracc - Access Handler for File Transfers
                    108: 
                    109: =head1 SYNOPSIS
                    110: 
                    111: Invoked by /etc/httpd/conf/loncapa.conf:
                    112: 
                    113:  <LocationMatch "^/raw.*">
                    114:  PerlAccessHandler Apache::lonracc
                    115:  </LocationMatch>
                    116: 
                    117: =head1 INTRODUCTION
                    118: 
                    119: This module enables authentication for file transfers and works
                    120: against the /res tree.
                    121: 
                    122: Only lond invokes the /raw namespace through its subscribe function.
                    123: 
                    124: This is part of the LearningOnline Network with CAPA project
                    125: described at http://www.lon-capa.org.
                    126: 
                    127: =head1 HANDLER SUBROUTINE
                    128: 
                    129: This routine is called by Apache and mod_perl.
                    130: 
                    131: =over 4
                    132: 
                    133: =item *
                    134: 
                    135: Determine requesting host
                    136: 
                    137: =item *
                    138: 
                    139: See whether or not the requesting host is subscribed.
                    140: 
                    141: =item *
                    142: 
                    143: Respond with status of request and make log entry in case of unallowed
                    144: access.
                    145: 
                    146: =back
                    147: 
                    148: =cut
1.1       albertel  149: 
                    150: 
                    151: 
                    152: 
                    153: 
                    154: 
                    155: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>