File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.14.10.2: download - view: text, annotated - select for diffs
Tue Dec 14 20:59:19 2021 UTC (2 years, 4 months ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4
Diff to branchpoint 1.14: preferred, unified
- For 2.11
  Backport 1.27, 1.28, 1.29, 1.30, 1.31, 1.32, 1.33, 1.34 (part),
           1.35, 1.36, 1.37 (part), 1.38

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.14.10.2 2021/12/14 20:59:19 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;
   37: 
   38: 
   39: sub handler {
   40:     my $r = shift;
   41:     # FIXME line remove when mod_perl fixes BUG#4948 
   42:     $r->notes->set('error-notes' => '');
   43:     my $alias = &Apache::lonnet::get_proxy_alias();
   44:     if ($alias) {
   45:         my $hdrhost = $r->headers_in->get('Host');
   46:         my $lonhost = $r->dir_config('lonHostID');
   47:         my $hostname = &Apache::lonnet::hostname($lonhost);
   48:         my $ssourl = '/adm/sso';
   49:         if ($r->dir_config('lonOtherAuthenUrl') ne '') {
   50:             $ssourl = $r->dir_config('lonOtherAuthenUrl');
   51:         }
   52:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
   53:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
   54:             my ($vpnint,$vpnext);
   55:             if (ref($proxyinfo) eq 'HASH') {
   56:                 $vpnint = $proxyinfo->{'vpnint'};
   57:                 $vpnext = $proxyinfo->{'vpnext'};
   58:             }
   59:             my ($redirect,$remote_ip);
   60:             if ($hdrhost eq $alias) {
   61:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
   62:                 if (($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext))) {
   63:                     $redirect = $hostname;
   64:                 }
   65:                 if ($r->uri eq $ssourl) {
   66:                     if (&Apache::lonnet::alias_sso($lonhost)) {
   67:                         undef($redirect);
   68:                     } else {
   69:                         $redirect = $hostname;
   70:                     }
   71:                 }
   72:                 if ($redirect eq $hdrhost) {
   73:                     undef($redirect);
   74:                 }
   75:             } elsif ($hdrhost eq $hostname) {
   76:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
   77:                 unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
   78:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
   79:                         ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
   80:                     $redirect = $alias;
   81:                     if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/})) {
   82:                         my %iphost = &Apache::lonnet::get_iphost();
   83:                         if (exists($iphost{$remote_ip})) {
   84:                             undef($redirect);
   85:                         }
   86:                     } elsif ($r->uri eq $ssourl) {
   87:                         unless (&Apache::lonnet::alias_sso($lonhost)) {
   88:                             undef($redirect);
   89:                         }
   90:                     }
   91:                 }
   92:             }
   93:             if ($redirect) {
   94:                 my $uri = $r->uri;
   95:                 if (($uri eq '/adm/switchserver') || ($uri =~ m{^/Shibboleth.sso/})) {
   96:                     return DECLINED;
   97:                 }
   98:                 unless (($uri eq '/adm/migrateuser') || ($uri eq $ssourl)) {
   99:                     my %user;
  100:                     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
  101:                     if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
  102:                         unless (($user{'name'} eq 'public') && ($user{'domain'} eq 'public')) {
  103:                             my $dest = '/adm/migrateuser';
  104:                             my $token = &set_token($r,$dest,$remote_ip,\%user);
  105:                             unless ($token eq '') {
  106:                                 $r->internal_redirect("$dest?token=$token");
  107:                                 $r->set_handlers('PerlHandler'=> undef);
  108:                                 return DECLINED;
  109:                             }
  110:                         }
  111:                     }
  112:                 }
  113:                 my $protocol = 'http';
  114:                 my $port = $r->get_server_port();
  115:                 if ($port eq '443') {
  116:                     $protocol = 'https';
  117:                 }
  118:                 if ($uri =~ m{^(/adm/css/)(.+)(.css)$}) {
  119:                     $uri = $1.&escape($2).$3;
  120:                 }
  121:                 my $location = $protocol.'://'.$redirect.$uri;
  122:                 if ($r->args) {
  123:                     $location .= '?'.$r->args;
  124:                 }
  125:                 $r->header_out(Location => $location);
  126:                 return REDIRECT;
  127:             }
  128:         }
  129:     }
  130:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
  131:         my $fn = $r->uri();
  132:         $fn=~s/^\/raw//;
  133:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
  134: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
  135:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
  136: 	my $allowed=0;
  137: 	my @ids=&Apache::lonnet::current_machine_ids();
  138: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
  139: 	if ($allowed) {
  140: 	    $r->filename(&propath($udom,$uname).
  141: 			 '/userfiles/'.(join('/',@ufile)));
  142:         }
  143:         return OK;
  144:     } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
  145:         my $uri = $r->uri;
  146:         $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
  147:         $r->uri($uri);
  148:     }
  149:     return DECLINED;
  150: }
  151: 
  152: sub set_token {
  153:     my ($r,$dest,$remote_ip,$userref) = @_;
  154:     my (%info,%user);
  155:     if ($dest eq '/adm/migrateuser') {
  156:         return unless (ref($userref) eq 'HASH');
  157:         %user = %{$userref};
  158:         %info = ('ip' => $remote_ip,
  159:                  'domain'    => $user{'domain'},
  160:                  'username'  => $user{'name'},
  161:                  'server'    => $r->dir_config('lonHostID'),
  162:                 );
  163:     }
  164:     if ($r->args) {
  165:         foreach my $pair (split(/&/,$r->args)) {
  166:             my ($name,$value) = split(/=/,$pair);
  167:             $name = &LONCAPA::unescape($name);
  168:             next unless (($name eq 'role') || ($name eq 'symb'));
  169:             $value =~ tr/+/ /;
  170:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  171:             $info{$name} = $value;
  172:         }
  173:     }
  174:     if ($dest eq '/adm/migrateuser') {
  175:         unless ($info{'role'}) {
  176:             if ($user{'role'} ne '') {
  177:                 $info{'role'} = $user{'role'};
  178:             }
  179:         }
  180:         unless ($info{'symb'}) {
  181:             unless ($r->uri eq '/adm/roles') {
  182:                 $info{'origurl'} = $r->uri;
  183:             }
  184:         }
  185:     }
  186:     if (($dest eq '/adm/migrateuser') || (keys(%info) > 0)) {
  187:         unless ($dest eq '/adm/migrateuser') {
  188:             $info{'origurl'} = $r->uri;
  189:         }
  190:         my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
  191:         unless (($token eq 'con_lost') || ($token eq 'refused') ||
  192:                 ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  193:             return $token;
  194:         }
  195:     }
  196:     return;
  197: }
  198: 
  199: 1;
  200: __END__
  201: 
  202: 
  203: 
  204: 
  205: 
  206: 
  207: 

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