File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.28: download - view: text, annotated - select for diffs
Wed Dec 23 22:03:42 2020 UTC (3 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914. Support selective use of WAF/Reverse Proxy, e.g., VPN users
  connect directly (no WAF), whereas non-VPN users must connect via WAF.

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.28 2020/12/23 22:03:42 raeburn Exp $
    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;
   32: use Apache::Constants qw(:common :remotehost REDIRECT);
   33: use Apache::lonnet;
   34: use Apache::File();
   35: use LONCAPA qw(:DEFAULT :match);
   36: 
   37: sub handler {
   38:     my $r = shift;
   39:     # FIXME line remove when mod_perl fixes BUG#4948
   40:     $r->notes->set('error-notes' => '');
   41:     my $hdrhost = $r->headers_in->get('Host');
   42:     if ($r->uri=~m{^/raw/}) {
   43:         if ($hdrhost) {
   44:             unless ($hdrhost =~ /^internal\-/) {
   45:                 my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
   46:                 my $lonhost = $r->dir_config('lonHostID');
   47:                 if (&redirect_raw($remote_ip,$lonhost)) {
   48:                     my $location = 'https://internal-'.$hdrhost.$r->uri;
   49:                     $r->headers_out->set(Location => $location);
   50:                     return REDIRECT;
   51:                 }
   52:             }
   53:         }
   54:     }
   55:     my $alias = &Apache::lonnet::get_proxy_alias();
   56:     if ($alias) {
   57:         my $lonhost = $r->dir_config('lonHostID');
   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;
   81:                     if ($r->uri=~m{^/raw/}){
   82:                         my %iphost = &Apache::lonnet::get_iphost();
   83:                         if (exists($iphost{$remote_ip})) {
   84:                             undef($redirect);
   85:                         }
   86:                     }
   87:                 }
   88:             }
   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:             }
  101:         }
  102:     }
  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);
  107: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
  108:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
  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) {
  113: 	    $r->filename(&propath($udom,$uname).
  114: 			 '/userfiles/'.(join('/',@ufile)));
  115:         }
  116:         return OK;
  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);
  121:     }
  122:     return DECLINED;
  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'}})) {
  152:                                     $redirect = 0;
  153:                                 } else {
  154:                                     $redirect = 1;
  155:                                 }
  156:                             }
  157:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
  158:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
  159:                                     $redirect = 1;
  160:                                 } else {
  161:                                     $redirect = 0;
  162:                                 }
  163:                             }
  164:                         }
  165:                     }
  166:                 }
  167:             }
  168:             last;
  169:         }
  170:     }
  171:     return $redirect;
  172: }
  173: 
  174: 1;
  175: __END__
  176: 

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