File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.30: download - view: text, annotated - select for diffs
Sun Apr 18 02:08:46 2021 UTC (3 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914
 - Update domain config UI to support settings documented in
   Domain_Configuration_WAF_Proxy.tex
 - Use /adm/migrateuser to handle redirects when user with a LON-CAPA session
   switches between access via VPN and not via VPN (and vice versa).

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.30 2021/04/18 02:08:46 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);
   33: use Apache::lonnet;
   34: use Apache::loncommon;
   35: use Apache::File();
   36: use LONCAPA qw(:DEFAULT :match);
   37: 
   38: sub handler {
   39:     my $r = shift;
   40:     # FIXME line remove when mod_perl fixes BUG#4948
   41:     $r->notes->set('error-notes' => '');
   42:     my $hdrhost = $r->headers_in->get('Host');
   43:     if ($r->uri=~m{^/raw/}) {
   44:         if ($hdrhost) {
   45:             unless ($hdrhost =~ /^internal\-/) {
   46:                 my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
   47:                 my $lonhost = $r->dir_config('lonHostID');
   48:                 if (&redirect_raw($remote_ip,$lonhost)) {
   49:                     my $location = 'https://internal-'.$hdrhost.$r->uri;
   50:                     $r->headers_out->set(Location => $location);
   51:                     return REDIRECT;
   52:                 }
   53:             }
   54:         }
   55:     }
   56:     my $alias = &Apache::lonnet::get_proxy_alias();
   57:     if ($alias) {
   58:         my $lonhost = $r->dir_config('lonHostID');
   59:         my $hostname = &Apache::lonnet::hostname($lonhost);
   60:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
   61:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
   62:             my ($vpnint,$vpnext);
   63:             if (ref($proxyinfo) eq 'HASH') {
   64:                 $vpnint = $proxyinfo->{'vpnint'};
   65:                 $vpnext = $proxyinfo->{'vpnext'};
   66:             }
   67:             my ($redirect,$remote_ip);
   68:             if ($hdrhost eq $alias) {
   69:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
   70:                 if ($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) {
   71:                     $redirect = $hostname;
   72:                     if ($redirect eq $hdrhost) {
   73:                         undef($redirect);
   74:                     }
   75:                 }
   76:             } elsif ($hdrhost eq $hostname) {
   77:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
   78:                 unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
   79:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
   80:                         ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
   81:                     $redirect = $alias;
   82:                     if ($r->uri=~m{^/raw/}){
   83:                         my %iphost = &Apache::lonnet::get_iphost();
   84:                         if (exists($iphost{$remote_ip})) {
   85:                             undef($redirect);
   86:                         }
   87:                     }
   88:                 }
   89:             }
   90:             if ($redirect) {
   91:                 my $uri = $r->uri;
   92:                 unless ($uri eq '/adm/migrateuser') {
   93:                     my %user;
   94:                     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
   95:                     if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
   96:                         my $token = &get_token($r,$remote_ip,\%user);
   97:                         unless ($token eq '') {
   98:                             $r->internal_redirect("/adm/migrateuser?token=$token");
   99:                             $r->set_handlers('PerlHandler'=> undef);
  100:                             return DECLINED;
  101:                         }
  102:                     }
  103:                 }
  104:                 my $protocol = 'http';
  105:                 my $port = $r->get_server_port();
  106:                 if ($port eq '443') {
  107:                     $protocol = 'https';
  108:                 }
  109:                 my $querystring = $r->args;
  110:                 my $location = $protocol.'://'.$redirect.$uri;
  111:                 if ($querystring) {
  112:                     $location .= "?$querystring";
  113:                 }
  114:                 $r->header_out(Location => $location);
  115:                 return REDIRECT;
  116:             }
  117:         }
  118:     }
  119:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
  120:         my $fn = $r->uri();
  121:         $fn=~s/^\/raw//;
  122:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
  123: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
  124:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
  125: 	my $allowed=0;
  126: 	my @ids=&Apache::lonnet::current_machine_ids();
  127: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
  128: 	if ($allowed) {
  129: 	    $r->filename(&propath($udom,$uname).
  130: 			 '/userfiles/'.(join('/',@ufile)));
  131:         }
  132:         return OK;
  133:     } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
  134:         my $uri = $r->uri;
  135:         $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
  136:         $r->uri($uri);
  137:     }
  138:     return DECLINED;
  139: }
  140: 
  141: sub redirect_raw {
  142:     my ($remote_ip,$lonhost) = @_;
  143:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
  144:     my $redirect;
  145:     while (@remhostids) {
  146:         my $try_server = pop(@remhostids);
  147:         my $remhostname = &Apache::lonnet::hostname($try_server);
  148:         if ($remhostname) {
  149:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
  150:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
  151:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
  152:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
  153:                 if (ref($internet_names) eq 'ARRAY') {
  154:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
  155:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
  156:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
  157:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
  158:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  159:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  160:                         my $replication = $domdefaults{'replication'};
  161:                         if (ref($replication) eq 'HASH') {
  162:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
  163:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
  164:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
  165:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
  166:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
  167:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
  168:                                     $redirect = 0;
  169:                                 } else {
  170:                                     $redirect = 1;
  171:                                 }
  172:                             }
  173:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
  174:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
  175:                                     $redirect = 1;
  176:                                 } else {
  177:                                     $redirect = 0;
  178:                                 }
  179:                             }
  180:                         }
  181:                     }
  182:                 }
  183:             }
  184:             last;
  185:         }
  186:     }
  187:     return $redirect;
  188: }
  189: 
  190: sub get_token {
  191:     my ($r,$remote_ip,$userref) = @_;
  192:     return unless (ref($userref) eq 'HASH');
  193:     my %user = %{$userref};
  194:     my %info = ('ip' => $remote_ip,
  195:                 'domain'    => $user{'domain'},
  196:                 'username'  => $user{'name'},
  197:                 'server'    => $r->dir_config('lonHostID'),
  198:                );
  199:     my $query = $r->args;
  200:     if ($query) {
  201:         foreach my $pair (split(/&/,$query)) {
  202:             my ($name,$value) = split(/=/,$pair);
  203:             $name = &LONCAPA::unescape($name);
  204:             next unless (($name eq 'role') || ($name eq 'symb'));
  205:             $value =~ tr/+/ /;
  206:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  207:             $info{$name} = $value;
  208:         }
  209:     }
  210:     unless ($info{'role'}) {
  211:         if ($user{'role'} ne '') {
  212:             $info{'role'} = $user{'role'};
  213:         }
  214:     }
  215:     unless ($info{'symb'}) {
  216:         unless ($r->uri eq '/adm/roles') {
  217:              $info{'origurl'} = $r->uri;
  218:         }
  219:     }
  220:     my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'));
  221:     unless (($token eq 'con_lost') || ($token eq 'refused') ||
  222:             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  223:         return $token;
  224:     }
  225:     return;
  226: }
  227: 
  228: 1;
  229: __END__
  230: 

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