File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.40: download - view: text, annotated - select for diffs
Thu Jun 30 21:04:15 2022 UTC (20 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  "Exit Tool" button available to logout a session launched via deep link
  and escape iframe and redirect (for LTI-protected link).

    1: # The LearningOnline Network
    2: # URL translation for User Files
    3: #
    4: # $Id: lontrans.pm,v 1.40 2022/06/30 21:04:15 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:         my $ssourl = '/adm/sso';
   61:         if ($r->dir_config('lonOtherAuthenUrl') ne '') {
   62:             $ssourl = $r->dir_config('lonOtherAuthenUrl');
   63:         }
   64:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
   65:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
   66:             my ($vpnint,$vpnext);
   67:             if (ref($proxyinfo) eq 'HASH') {
   68:                 $vpnint = $proxyinfo->{'vpnint'};
   69:                 $vpnext = $proxyinfo->{'vpnext'};
   70:             }
   71:             my ($redirect,$remote_ip);
   72:             if ($hdrhost eq $alias) {
   73:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
   74:                 if (($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) &&
   75:                     ($r->uri !~ m{^/adm/(lti|launch)/})) {
   76:                     $redirect = $hostname;
   77:                 }
   78:                 if ($r->uri eq $ssourl) {
   79:                     if (&Apache::lonnet::alias_sso($lonhost)) {
   80:                         undef($redirect);
   81:                     } else {
   82:                         $redirect = $hostname;
   83:                     }
   84:                 }
   85:                 if ($redirect eq $hdrhost) {
   86:                     undef($redirect);
   87:                 }
   88:             } elsif ($hdrhost eq $hostname) {
   89:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
   90:                 unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
   91:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
   92:                         ($r->uri=~m{^/adm/(lti|launch)/}) ||
   93:                         ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
   94:                     $redirect = $alias;
   95:                     if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/})) {
   96:                         my %iphost = &Apache::lonnet::get_iphost();
   97:                         if (exists($iphost{$remote_ip})) {
   98:                             undef($redirect);
   99:                         }
  100:                     } elsif ($r->uri eq $ssourl) {
  101:                         unless (&Apache::lonnet::alias_sso($lonhost)) {
  102:                             undef($redirect);
  103:                         }
  104:                     }
  105:                 }
  106:             }
  107:             if ($redirect) {
  108:                 my $uri = $r->uri;
  109:                 if (($uri eq '/adm/switchserver') || ($uri =~ m{^/Shibboleth.sso/})) {
  110:                     return DECLINED;
  111:                 }
  112:                 unless (($uri eq '/adm/migrateuser') || ($uri eq $ssourl)) {
  113:                     my %user;
  114:                     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
  115:                     if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
  116:                         unless (($user{'name'} eq 'public') && ($user{'domain'} eq 'public')) {
  117:                             my $dest = '/adm/migrateuser';
  118:                             my $token = &set_token($r,$dest,$remote_ip,\%user);
  119:                             unless ($token eq '') {
  120:                                 $r->internal_redirect("$dest?token=$token");
  121:                                 $r->set_handlers('PerlHandler'=> undef);
  122:                                 return DECLINED;
  123:                             }
  124:                         }
  125:                     }
  126:                 }
  127:                 my $protocol = 'http';
  128:                 my $port = $r->get_server_port();
  129:                 if ($port eq '443') {
  130:                     $protocol = 'https';
  131:                 }
  132:                 if ($uri =~ m{^(/adm/css/)(.+)(.css)$}) {
  133:                     $uri = $1.&escape($2).$3;
  134:                 }
  135:                 my $location = $protocol.'://'.$redirect.$uri;
  136:                 if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
  137:                     my $token = &set_token($r,$r->uri);
  138:                     unless ($token eq '') {
  139:                         $location .= '?ttoken='.$token;
  140:                     }
  141:                 } elsif ($r->args) {
  142:                     $location .= '?'.$r->args;
  143:                 }
  144:                 $r->header_out(Location => $location);
  145:                 return REDIRECT;
  146:             }
  147:         }
  148:     }
  149:     if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
  150:         my $token = &set_token($r,$r->uri);
  151:         unless ($token eq '') {
  152:             $r->args('ttoken='.$token);
  153:         }
  154:     }
  155:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
  156:         my $fn = $r->uri();
  157:         $fn=~s/^\/raw//;
  158:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
  159: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
  160:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
  161: 	my $allowed=0;
  162: 	my @ids=&Apache::lonnet::current_machine_ids();
  163: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
  164: 	if ($allowed) {
  165: 	    $r->filename(&propath($udom,$uname).
  166: 			 '/userfiles/'.(join('/',@ufile)));
  167:         }
  168:         return OK;
  169:     } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
  170:         my $uri = $r->uri;
  171:         $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
  172:         $r->uri($uri);
  173:     }
  174:     return DECLINED;
  175: }
  176: 
  177: sub redirect_raw {
  178:     my ($remote_ip,$lonhost) = @_;
  179:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
  180:     my $redirect;
  181:     while (@remhostids) {
  182:         my $try_server = pop(@remhostids);
  183:         my $remhostname = &Apache::lonnet::hostname($try_server);
  184:         if ($remhostname) {
  185:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
  186:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
  187:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
  188:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
  189:                 if (ref($internet_names) eq 'ARRAY') {
  190:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
  191:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
  192:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
  193:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
  194:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  195:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  196:                         my $replication = $domdefaults{'replication'};
  197:                         if (ref($replication) eq 'HASH') {
  198:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
  199:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
  200:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
  201:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
  202:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
  203:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
  204:                                     $redirect = 0;
  205:                                 } else {
  206:                                     $redirect = 1;
  207:                                 }
  208:                             }
  209:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
  210:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
  211:                                     $redirect = 1;
  212:                                 } else {
  213:                                     $redirect = 0;
  214:                                 }
  215:                             }
  216:                         }
  217:                     }
  218:                 }
  219:             }
  220:             last;
  221:         }
  222:     }
  223:     return $redirect;
  224: }
  225: 
  226: sub set_token {
  227:     my ($r,$dest,$remote_ip,$userref) = @_;
  228:     my (%info,%user);
  229:     if ($dest eq '/adm/migrateuser') {
  230:         return unless (ref($userref) eq 'HASH');
  231:         %user = %{$userref};
  232:         %info = ('ip' => $remote_ip,
  233:                  'domain'    => $user{'domain'},
  234:                  'username'  => $user{'name'},
  235:                  'server'    => $r->dir_config('lonHostID'),
  236:                 );
  237:     }
  238:     if ($r->args) {
  239:         foreach my $pair (split(/&/,$r->args)) {
  240:             my ($name,$value) = split(/=/,$pair);
  241:             $name = &LONCAPA::unescape($name);
  242:             next unless (($name eq 'role') || ($name eq 'symb') ||
  243:                          ($name eq 'ltoken') || ($name eq 'linkkey') ||
  244:                          ($name eq 'ttoken'));
  245:             $value =~ tr/+/ /;
  246:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  247:             $info{$name} = $value;
  248:         }
  249:         if ($info{'ttoken'}) {
  250:             my %current = &Apache::lonnet::tmpget($info{'ttoken'});
  251:             delete($info{'ttoken'});
  252:             if ($current{'origurl'} eq $r->uri) {
  253:                 return if (keys(%current));
  254:             }
  255:         }
  256:     }
  257: #
  258: # To be able to support deep-linking for a shortened URL, i.e.,
  259: # /tiny/$dom/$id with (a) linkkey included in POSTed data and
  260: # data and (b) non-Shibboleth SSO in use, ltoken (or linkprot
  261: # if sending to /adm/migrateuser), or linkkey are written to a
  262: # .tmp file in /home/httpd/perl/tmp and the query string is
  263: # replaced with ttoken=tokenid.  This does mean other POSTed
  264: # data will be discarded.
  265: #
  266:     if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
  267:         unless (($info{'ltoken'}) || ($info{'linkkey'} ne '')) {
  268:             &Apache::lonacc::get_posted_cgi($r,['linkkey']);
  269:             if ($env{'form.linkkey'} ne '') {
  270:                 $info{'linkkey'} = $env{'form.linkkey'};
  271:             }
  272:         }
  273:     }
  274:     if ($dest eq '/adm/migrateuser') {
  275:         if ($info{'ltoken'}) {
  276:             my %link_info = &Apache::lonnet::tmpget($info{'ltoken'});
  277:             if ($link_info{'linkprot'}) {
  278:                 $info{'linkprot'} = $link_info{'linkprot'};
  279:                 foreach my $item ('linkprotuser','linkprotexit') {
  280:                     if ($link_info{$item} ne '') {
  281:                         $info{$item} = $link_info{$item};
  282:                     }
  283:                 }
  284:             }
  285:             &Apache::lonnet::tmpdel($info{'ltoken'});
  286:             delete($info{'ltoken'});
  287:         }
  288:         unless ($info{'role'}) {
  289:             if ($user{'role'} ne '') {
  290:                 $info{'role'} = $user{'role'};
  291:             }
  292:         }
  293:         unless ($info{'symb'}) {
  294:             unless ($r->uri eq '/adm/roles') {
  295:                 $info{'origurl'} = $r->uri;
  296:             }
  297:         }
  298:     }
  299:     if (($dest eq '/adm/migrateuser') || (keys(%info) > 0)) {
  300:         unless ($dest eq '/adm/migrateuser') {
  301:             $info{'origurl'} = $r->uri;
  302:         }
  303:         my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
  304:         unless (($token eq 'con_lost') || ($token eq 'refused') ||
  305:                 ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  306:             return $token;
  307:         }
  308:     }
  309:     return;
  310: }
  311: 
  312: 1;
  313: __END__
  314: 

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