File:  [LON-CAPA] / loncom / lontrans.pm
Revision 1.41: download - view: text, annotated - select for diffs
Fri Jun 2 01:20:28 2023 UTC (10 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bugs 6754 and 6907
  - Return of grades to launcher CMS supported for resources or folders
    accessed via LTI-mediated deep link.
  - Support option: "Encrypt stored consumer secrets defined in domain"
  - Signing of LTI payloads for roster retrieval, passback of grades,
    and callback to logput launcher CMS session all now occur on
    primary library server for course's domain.

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

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