Annotation of loncom/lontrans.pm, revision 1.38

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for User Files
                      3: #
1.38    ! raeburn     4: # $Id: lontrans.pm,v 1.37 2021/11/03 01:04:03 raeburn Exp $
1.1       www         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;
1.30      raeburn    32: use Apache::Constants qw(:common :remotehost);
1.18      raeburn    33: use Apache::lonnet;
1.30      raeburn    34: use Apache::loncommon;
1.1       www        35: use Apache::File();
1.15      raeburn    36: use LONCAPA qw(:DEFAULT :match);
1.2       www        37: 
1.1       www        38: sub handler {
                     39:     my $r = shift;
1.25      raeburn    40:     # FIXME line remove when mod_perl fixes BUG#4948
1.12      albertel   41:     $r->notes->set('error-notes' => '');
1.28      raeburn    42:     my $hdrhost = $r->headers_in->get('Host');
1.25      raeburn    43:     if ($r->uri=~m{^/raw/}) {
1.28      raeburn    44:         if ($hdrhost) {
                     45:             unless ($hdrhost =~ /^internal\-/) {
1.27      raeburn    46:                 my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
1.16      raeburn    47:                 my $lonhost = $r->dir_config('lonHostID');
                     48:                 if (&redirect_raw($remote_ip,$lonhost)) {
1.28      raeburn    49:                     my $location = 'https://internal-'.$hdrhost.$r->uri;
1.16      raeburn    50:                     $r->headers_out->set(Location => $location);
                     51:                     return REDIRECT;
1.15      raeburn    52:                 }
                     53:             }
                     54:         }
                     55:     }
1.27      raeburn    56:     my $alias = &Apache::lonnet::get_proxy_alias();
                     57:     if ($alias) {
                     58:         my $lonhost = $r->dir_config('lonHostID');
1.28      raeburn    59:         my $hostname = &Apache::lonnet::hostname($lonhost);
1.38    ! raeburn    60:         my $ssourl = '/adm/sso';
        !            61:         if ($r->dir_config('lonOtherAuthenUrl') ne '') {
        !            62:             $ssourl = $r->dir_config('lonOtherAuthenUrl');
        !            63:         }
1.28      raeburn    64:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
                     65:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
1.29      raeburn    66:             my ($vpnint,$vpnext);
1.28      raeburn    67:             if (ref($proxyinfo) eq 'HASH') {
1.29      raeburn    68:                 $vpnint = $proxyinfo->{'vpnint'};
                     69:                 $vpnext = $proxyinfo->{'vpnext'};
1.28      raeburn    70:             }
1.30      raeburn    71:             my ($redirect,$remote_ip);
1.28      raeburn    72:             if ($hdrhost eq $alias) {
1.30      raeburn    73:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.34      raeburn    74:                 if (($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) &&
                     75:                     ($r->uri !~ m{^/adm/(lti|launch)/})) {
1.28      raeburn    76:                     $redirect = $hostname;
1.36      raeburn    77:                 }
1.38    ! raeburn    78:                 if ($r->uri eq $ssourl) {
        !            79:                     if (&Apache::lonnet::alias_sso($lonhost)) {
1.28      raeburn    80:                         undef($redirect);
1.36      raeburn    81:                     } else {
                     82:                         $redirect = $hostname;
1.28      raeburn    83:                     }
                     84:                 }
1.36      raeburn    85:                 if ($redirect eq $hdrhost) {
                     86:                     undef($redirect);
                     87:                 }
1.28      raeburn    88:             } elsif ($hdrhost eq $hostname) {
1.30      raeburn    89:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
1.29      raeburn    90:                 unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
1.28      raeburn    91:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
1.34      raeburn    92:                         ($r->uri=~m{^/adm/(lti|launch)/}) ||
1.29      raeburn    93:                         ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
1.28      raeburn    94:                     $redirect = $alias;
1.34      raeburn    95:                     if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/})) {
1.27      raeburn    96:                         my %iphost = &Apache::lonnet::get_iphost();
                     97:                         if (exists($iphost{$remote_ip})) {
1.28      raeburn    98:                             undef($redirect);
1.27      raeburn    99:                         }
1.38    ! raeburn   100:                     } elsif ($r->uri eq $ssourl) {
        !           101:                         unless (&Apache::lonnet::alias_sso($lonhost)) {
1.36      raeburn   102:                             undef($redirect);
                    103:                         }
1.27      raeburn   104:                     }
                    105:                 }
                    106:             }
1.28      raeburn   107:             if ($redirect) {
                    108:                 my $uri = $r->uri;
1.36      raeburn   109:                 if (($uri eq '/adm/switchserver') || ($uri =~ m{^/Shibboleth.sso/})) {
1.32      raeburn   110:                     return DECLINED;
                    111:                 }
1.38    ! raeburn   112:                 unless (($uri eq '/adm/migrateuser') || ($uri eq $ssourl)) {
1.30      raeburn   113:                     my %user;
                    114:                     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
                    115:                     if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
1.35      raeburn   116:                         unless (($user{'name'} eq 'public') && ($user{'domain'} eq 'public')) {
1.37      raeburn   117:                             my $dest = '/adm/migrateuser';
                    118:                             my $token = &set_token($r,$dest,$remote_ip,\%user);
1.35      raeburn   119:                             unless ($token eq '') {
1.37      raeburn   120:                                 $r->internal_redirect("$dest?token=$token");
1.35      raeburn   121:                                 $r->set_handlers('PerlHandler'=> undef);
                    122:                                 return DECLINED;
                    123:                             }
1.30      raeburn   124:                         }
                    125:                     }
                    126:                 }
1.28      raeburn   127:                 my $protocol = 'http';
                    128:                 my $port = $r->get_server_port();
                    129:                 if ($port eq '443') {
                    130:                     $protocol = 'https';
                    131:                 }
1.33      raeburn   132:                 if ($uri =~ m{^(/adm/css/)(.+)(.css)$}) {
                    133:                     $uri = $1.&escape($2).$3;
                    134:                 }
1.30      raeburn   135:                 my $location = $protocol.'://'.$redirect.$uri;
1.37      raeburn   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;
1.30      raeburn   143:                 }
                    144:                 $r->header_out(Location => $location);
1.28      raeburn   145:                 return REDIRECT;
                    146:             }
1.27      raeburn   147:         }
                    148:     }
1.37      raeburn   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:     }
1.7       raeburn   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);
1.10      albertel  159: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
1.7       raeburn   160:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
1.9       albertel  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) {
1.13      albertel  165: 	    $r->filename(&propath($udom,$uname).
                    166: 			 '/userfiles/'.(join('/',@ufile)));
1.7       raeburn   167:         }
1.15      raeburn   168:         return OK;
1.26      raeburn   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);
1.15      raeburn   173:     }
1.26      raeburn   174:     return DECLINED;
1.15      raeburn   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'}})) {
1.24      raeburn   204:                                     $redirect = 0;
                    205:                                 } else {
1.15      raeburn   206:                                     $redirect = 1;
                    207:                                 }
                    208:                             }
                    209:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
                    210:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
1.24      raeburn   211:                                     $redirect = 1;
                    212:                                 } else {
1.15      raeburn   213:                                     $redirect = 0;
                    214:                                 }
                    215:                             }
                    216:                         }
                    217:                     }
                    218:                 }
                    219:             }
                    220:             last;
                    221:         }
                    222:     }
1.24      raeburn   223:     return $redirect;
1.1       www       224: }
                    225: 
1.37      raeburn   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)) {
1.30      raeburn   240:             my ($name,$value) = split(/=/,$pair);
                    241:             $name = &LONCAPA::unescape($name);
1.37      raeburn   242:             next unless (($name eq 'role') || ($name eq 'symb') ||
                    243:                          ($name eq 'ltoken') || ($name eq 'linkkey') ||
                    244:                          ($name eq 'ttoken'));
1.30      raeburn   245:             $value =~ tr/+/ /;
                    246:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    247:             $info{$name} = $value;
                    248:         }
1.37      raeburn   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:         }
1.30      raeburn   256:     }
1.37      raeburn   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:             }
1.30      raeburn   272:         }
                    273:     }
1.37      raeburn   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:             }
                    280:             &Apache::lonnet::tmpdel($info{'ltoken'});;
                    281:             delete($info{'ltoken'});
                    282:         }
                    283:         unless ($info{'role'}) {
                    284:             if ($user{'role'} ne '') {
                    285:                 $info{'role'} = $user{'role'};
                    286:             }
                    287:         }
                    288:         unless ($info{'symb'}) {
                    289:             unless ($r->uri eq '/adm/roles') {
                    290:                 $info{'origurl'} = $r->uri;
                    291:             }
1.30      raeburn   292:         }
                    293:     }
1.37      raeburn   294:     if (($dest eq '/adm/migrateuser') || (keys(%info) > 0)) {
                    295:         unless ($dest eq '/adm/migrateuser') {
                    296:             $info{'origurl'} = $r->uri;
                    297:         }
                    298:         my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
                    299:         unless (($token eq 'con_lost') || ($token eq 'refused') ||
                    300:                 ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
                    301:             return $token;
                    302:         }
1.30      raeburn   303:     }
                    304:     return;
                    305: }
                    306: 
1.1       www       307: 1;
                    308: __END__
                    309: 

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