File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.14.10.2.2.4: download - view: text, annotated - select for diffs
Wed Jul 5 19:22:09 2023 UTC (10 months ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.14.10.2: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.41

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.14.10.2.2.4 2023/07/05 19:22:09 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:     if (($r->uri eq '/adm/service/passback') ||
   43:         ($r->uri eq '/adm/service/roster')) {
   44:         return OK;
   45:     }
   46:     my $alias = &Apache::lonnet::get_proxy_alias();
   47:     if ($alias) {
   48:         my $hdrhost = $r->headers_in->get('Host');
   49:         my $lonhost = $r->dir_config('lonHostID');
   50:         my $hostname = &Apache::lonnet::hostname($lonhost);
   51:         my $ssourl = '/adm/sso';
   52:         if ($r->dir_config('lonOtherAuthenUrl') ne '') {
   53:             $ssourl = $r->dir_config('lonOtherAuthenUrl');
   54:         }
   55:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
   56:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
   57:             my ($vpnint,$vpnext);
   58:             if (ref($proxyinfo) eq 'HASH') {
   59:                 $vpnint = $proxyinfo->{'vpnint'};
   60:                 $vpnext = $proxyinfo->{'vpnext'};
   61:             }
   62:             my ($redirect,$remote_ip);
   63:             if ($hdrhost eq $alias) {
   64:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
   65:                 if (($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) &&
   66:                     ($r->uri !~ m{^/adm/(lti|launch)/})) {
   67:                     $redirect = $hostname;
   68:                 }
   69:                 if ($r->uri eq $ssourl) {
   70:                     if (&Apache::lonnet::alias_sso($lonhost)) {
   71:                         undef($redirect);
   72:                     } else {
   73:                         $redirect = $hostname;
   74:                     }
   75:                 }
   76:                 if ($redirect eq $hdrhost) {
   77:                     undef($redirect);
   78:                 }
   79:             } elsif ($hdrhost eq $hostname) {
   80:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
   81:                 unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
   82:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
   83:                         ($r->uri=~m{^/adm/(lti|launch)/}) ||
   84:                         ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
   85:                     $redirect = $alias;
   86:                     if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/})) {
   87:                         my %iphost = &Apache::lonnet::get_iphost();
   88:                         if (exists($iphost{$remote_ip})) {
   89:                             undef($redirect);
   90:                         }
   91:                     } elsif ($r->uri eq $ssourl) {
   92:                         unless (&Apache::lonnet::alias_sso($lonhost)) {
   93:                             undef($redirect);
   94:                         }
   95:                     }
   96:                 }
   97:             }
   98:             if ($redirect) {
   99:                 my $uri = $r->uri;
  100:                 if (($uri eq '/adm/switchserver') || ($uri =~ m{^/Shibboleth.sso/})) {
  101:                     return DECLINED;
  102:                 }
  103:                 unless (($uri eq '/adm/migrateuser') || ($uri eq $ssourl)) {
  104:                     my %user;
  105:                     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
  106:                     if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
  107:                         unless (($user{'name'} eq 'public') && ($user{'domain'} eq 'public')) {
  108:                             my $dest = '/adm/migrateuser';
  109:                             my $token = &set_token($r,$dest,$remote_ip,\%user);
  110:                             unless ($token eq '') {
  111:                                 $r->internal_redirect("$dest?token=$token");
  112:                                 $r->set_handlers('PerlHandler'=> undef);
  113:                                 return DECLINED;
  114:                             }
  115:                         }
  116:                     }
  117:                 }
  118:                 my $protocol = 'http';
  119:                 my $port = $r->get_server_port();
  120:                 if ($port eq '443') {
  121:                     $protocol = 'https';
  122:                 }
  123:                 if ($uri =~ m{^(/adm/css/)(.+)(.css)$}) {
  124:                     $uri = $1.&escape($2).$3;
  125:                 }
  126:                 my $location = $protocol.'://'.$redirect.$uri;
  127:                 if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
  128:                     my $token = &set_token($r,$r->uri);
  129:                     unless ($token eq '') {
  130:                         $location .= '?ttoken='.$token;
  131:                     }
  132:                 } elsif ($r->args) {
  133:                     $location .= '?'.$r->args;
  134:                 }
  135:                 $r->header_out(Location => $location);
  136:                 return REDIRECT;
  137:             }
  138:         }
  139:     }
  140:     if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
  141:         my $token = &set_token($r,$r->uri);
  142:         unless ($token eq '') {
  143:             $r->args('ttoken='.$token);
  144:         }
  145:     }
  146:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
  147:         my $fn = $r->uri();
  148:         $fn=~s/^\/raw//;
  149:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
  150: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
  151:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
  152: 	my $allowed=0;
  153: 	my @ids=&Apache::lonnet::current_machine_ids();
  154: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
  155: 	if ($allowed) {
  156: 	    $r->filename(&propath($udom,$uname).
  157: 			 '/userfiles/'.(join('/',@ufile)));
  158:         }
  159:         return OK;
  160:     } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
  161:         my $uri = $r->uri;
  162:         $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
  163:         $r->uri($uri);
  164:     }
  165:     return DECLINED;
  166: }
  167: 
  168: sub set_token {
  169:     my ($r,$dest,$remote_ip,$userref) = @_;
  170:     my (%info,%user);
  171:     if ($dest eq '/adm/migrateuser') {
  172:         return unless (ref($userref) eq 'HASH');
  173:         %user = %{$userref};
  174:         %info = ('ip' => $remote_ip,
  175:                  'domain'    => $user{'domain'},
  176:                  'username'  => $user{'name'},
  177:                  'server'    => $r->dir_config('lonHostID'),
  178:                 );
  179:     }
  180:     if ($r->args) {
  181:         foreach my $pair (split(/&/,$r->args)) {
  182:             my ($name,$value) = split(/=/,$pair);
  183:             $name = &LONCAPA::unescape($name);
  184:             next unless (($name eq 'role') || ($name eq 'symb') ||
  185:                          ($name eq 'ltoken') || ($name eq 'linkkey') ||
  186:                          ($name eq 'ttoken'));
  187:             $value =~ tr/+/ /;
  188:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  189:             $info{$name} = $value;
  190:         }
  191:         if ($info{'ttoken'}) {
  192:             my %current = &Apache::lonnet::tmpget($info{'ttoken'});
  193:             delete($info{'ttoken'});
  194:             if ($current{'origurl'} eq $r->uri) {
  195:                 return if (keys(%current));
  196:             }
  197:         }
  198:     }
  199: #
  200: # To be able to support deep-linking for a shortened URL, i.e.,
  201: # /tiny/$dom/$id with (a) linkkey included in POSTed data and
  202: # data and (b) non-Shibboleth SSO in use, ltoken (or linkprot
  203: # if sending to /adm/migrateuser), or linkkey are written to a
  204: # .tmp file in /home/httpd/perl/tmp and the query string is
  205: # replaced with ttoken=tokenid.  This does mean other POSTed
  206: # data will be discarded.
  207: #
  208:     if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
  209:         unless (($info{'ltoken'}) || ($info{'linkkey'} ne '')) {
  210:             &Apache::lonacc::get_posted_cgi($r,['linkkey']);
  211:             if ($env{'form.linkkey'} ne '') {
  212:                 $info{'linkkey'} = $env{'form.linkkey'};
  213:             }
  214:         }
  215:     }
  216:     if ($dest eq '/adm/migrateuser') {
  217:         if ($info{'ltoken'}) {
  218:             my %link_info = &Apache::lonnet::tmpget($info{'ltoken'});
  219:             if ($link_info{'linkprot'}) {
  220:                 $info{'linkprot'} = $link_info{'linkprot'};
  221:                 foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
  222:                     if ($link_info{$item} ne '') {
  223:                         $info{$item} = $link_info{$item};
  224:                     }
  225:                 }
  226:             }
  227:             &Apache::lonnet::tmpdel($info{'ltoken'});
  228:             delete($info{'ltoken'});
  229:         }
  230:         unless ($info{'role'}) {
  231:             if ($user{'role'} ne '') {
  232:                 $info{'role'} = $user{'role'};
  233:             }
  234:         }
  235:         unless ($info{'symb'}) {
  236:             unless ($r->uri eq '/adm/roles') {
  237:                 $info{'origurl'} = $r->uri;
  238:             }
  239:         }
  240:     }
  241:     if (($dest eq '/adm/migrateuser') || (keys(%info) > 0)) {
  242:         unless ($dest eq '/adm/migrateuser') {
  243:             $info{'origurl'} = $r->uri;
  244:         }
  245:         my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
  246:         unless (($token eq 'con_lost') || ($token eq 'refused') ||
  247:                 ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  248:             return $token;
  249:         }
  250:     }
  251:     return;
  252: }
  253: 
  254: 1;
  255: __END__
  256: 

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