Annotation of loncom/lontrans.pm, revision 1.28

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for User Files
                      3: #
1.28    ! raeburn     4: # $Id: lontrans.pm,v 1.27 2020/12/18 15:23:04 raeburn Exp $
1.1       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: #
                     28: 
                     29: package Apache::lontrans;
                     30: 
                     31: use strict;
1.25      raeburn    32: use Apache::Constants qw(:common :remotehost REDIRECT);
1.18      raeburn    33: use Apache::lonnet;
1.1       www        34: use Apache::File();
1.15      raeburn    35: use LONCAPA qw(:DEFAULT :match);
1.2       www        36: 
1.1       www        37: sub handler {
                     38:     my $r = shift;
1.25      raeburn    39:     # FIXME line remove when mod_perl fixes BUG#4948
1.12      albertel   40:     $r->notes->set('error-notes' => '');
1.28    ! raeburn    41:     my $hdrhost = $r->headers_in->get('Host');
1.25      raeburn    42:     if ($r->uri=~m{^/raw/}) {
1.28    ! raeburn    43:         if ($hdrhost) {
        !            44:             unless ($hdrhost =~ /^internal\-/) {
1.27      raeburn    45:                 my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
1.16      raeburn    46:                 my $lonhost = $r->dir_config('lonHostID');
                     47:                 if (&redirect_raw($remote_ip,$lonhost)) {
1.28    ! raeburn    48:                     my $location = 'https://internal-'.$hdrhost.$r->uri;
1.16      raeburn    49:                     $r->headers_out->set(Location => $location);
                     50:                     return REDIRECT;
1.15      raeburn    51:                 }
                     52:             }
                     53:         }
                     54:     }
1.27      raeburn    55:     my $alias = &Apache::lonnet::get_proxy_alias();
                     56:     if ($alias) {
                     57:         my $lonhost = $r->dir_config('lonHostID');
1.28    ! raeburn    58:         my $hostname = &Apache::lonnet::hostname($lonhost);
        !            59:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
        !            60:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
        !            61:             my ($vpnpriv,$vpnnat);
        !            62:             if (ref($proxyinfo) eq 'HASH') {
        !            63:                 $vpnpriv = $proxyinfo->{'exempt'};
        !            64:                 $vpnnat = '35.12.16.96-35.12.16.111';
        !            65:             }
        !            66:             my $redirect;
        !            67:             if ($hdrhost eq $alias) {
        !            68:                 my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
        !            69:                 if ($vpnnat && &Apache::lonnet::ip_match($remote_ip,$vpnnat)) {
        !            70:                     $redirect = $hostname;
        !            71:                     if ($redirect eq $hdrhost) {
        !            72:                         undef($redirect);
        !            73:                     }
        !            74:                 }
        !            75:             } elsif ($hdrhost eq $hostname) {
        !            76:                 my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
        !            77:                 unless (($remote_ip eq '127.0.0.1') ||
        !            78:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
        !            79:                         ($vpnpriv && &Apache::lonnet::ip_match($remote_ip,$vpnpriv))) {
        !            80:                     $redirect = $alias;
1.27      raeburn    81:                     if ($r->uri=~m{^/raw/}){
                     82:                         my %iphost = &Apache::lonnet::get_iphost();
                     83:                         if (exists($iphost{$remote_ip})) {
1.28    ! raeburn    84:                             undef($redirect);
1.27      raeburn    85:                         }
                     86:                     }
                     87:                 }
                     88:             }
1.28    ! raeburn    89:             if ($redirect) {
        !            90:                 my $uri = $r->uri;
        !            91:                 my $protocol = 'http';
        !            92:                 my $port = $r->get_server_port();
        !            93:                 if ($port eq '443') {
        !            94:                     $protocol = 'https';
        !            95:                 }
        !            96: # FIXME should check if logged in, and if so use switchserver/migrateuser approach,
        !            97: # possibly moved to Access Handler?
        !            98:                 $r->header_out(Location => $protocol.'://'.$redirect.$uri);
        !            99:                 return REDIRECT;
        !           100:             }
1.27      raeburn   101:         }
                    102:     }
1.7       raeburn   103:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
                    104:         my $fn = $r->uri();
                    105:         $fn=~s/^\/raw//;
                    106:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
1.10      albertel  107: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
1.7       raeburn   108:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
1.9       albertel  109: 	my $allowed=0;
                    110: 	my @ids=&Apache::lonnet::current_machine_ids();
                    111: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
                    112: 	if ($allowed) {
1.13      albertel  113: 	    $r->filename(&propath($udom,$uname).
                    114: 			 '/userfiles/'.(join('/',@ufile)));
1.7       raeburn   115:         }
1.15      raeburn   116:         return OK;
1.26      raeburn   117:     } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
                    118:         my $uri = $r->uri;
                    119:         $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
                    120:         $r->uri($uri);
1.15      raeburn   121:     }
1.26      raeburn   122:     return DECLINED;
1.15      raeburn   123: }
                    124: 
                    125: sub redirect_raw {
                    126:     my ($remote_ip,$lonhost) = @_;
                    127:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
                    128:     my $redirect;
                    129:     while (@remhostids) {
                    130:         my $try_server = pop(@remhostids);
                    131:         my $remhostname = &Apache::lonnet::hostname($try_server);
                    132:         if ($remhostname) {
                    133:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
                    134:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
                    135:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
                    136:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
                    137:                 if (ref($internet_names) eq 'ARRAY') {
                    138:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                    139:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
                    140:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
                    141:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
                    142:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                    143:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                    144:                         my $replication = $domdefaults{'replication'};
                    145:                         if (ref($replication) eq 'HASH') {
                    146:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
                    147:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
                    148:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
                    149:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
                    150:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
                    151:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
1.24      raeburn   152:                                     $redirect = 0;
                    153:                                 } else {
1.15      raeburn   154:                                     $redirect = 1;
                    155:                                 }
                    156:                             }
                    157:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
                    158:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
1.24      raeburn   159:                                     $redirect = 1;
                    160:                                 } else {
1.15      raeburn   161:                                     $redirect = 0;
                    162:                                 }
                    163:                             }
                    164:                         }
                    165:                     }
                    166:                 }
                    167:             }
                    168:             last;
                    169:         }
                    170:     }
1.24      raeburn   171:     return $redirect;
1.1       www       172: }
                    173: 
                    174: 1;
                    175: __END__
                    176: 

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