Annotation of loncom/lontrans.pm, revision 1.32

1.1       www         1: # The LearningOnline Network
                      2: # URL translation for User Files
                      3: #
1.32    ! raeburn     4: # $Id: lontrans.pm,v 1.31 2021/05/03 15:27:42 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);
                     60:         if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
                     61:             my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
1.29      raeburn    62:             my ($vpnint,$vpnext);
1.28      raeburn    63:             if (ref($proxyinfo) eq 'HASH') {
1.29      raeburn    64:                 $vpnint = $proxyinfo->{'vpnint'};
                     65:                 $vpnext = $proxyinfo->{'vpnext'};
1.28      raeburn    66:             }
1.30      raeburn    67:             my ($redirect,$remote_ip);
1.28      raeburn    68:             if ($hdrhost eq $alias) {
1.30      raeburn    69:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.29      raeburn    70:                 if ($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) {
1.28      raeburn    71:                     $redirect = $hostname;
                     72:                     if ($redirect eq $hdrhost) {
                     73:                         undef($redirect);
                     74:                     }
                     75:                 }
                     76:             } elsif ($hdrhost eq $hostname) {
1.30      raeburn    77:                 $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
1.29      raeburn    78:                 unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
1.28      raeburn    79:                         ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
1.29      raeburn    80:                         ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
1.28      raeburn    81:                     $redirect = $alias;
1.31      raeburn    82:                     if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/(hosts|domain)$})) {
1.27      raeburn    83:                         my %iphost = &Apache::lonnet::get_iphost();
                     84:                         if (exists($iphost{$remote_ip})) {
1.28      raeburn    85:                             undef($redirect);
1.27      raeburn    86:                         }
                     87:                     }
                     88:                 }
                     89:             }
1.28      raeburn    90:             if ($redirect) {
                     91:                 my $uri = $r->uri;
1.32    ! raeburn    92:                 if (($uri eq '/adm/sso') || ($uri eq '/adm/switchserver') ||
        !            93:                     ($uri =~ m{^/Shibboleth.sso/})) {
        !            94:                     return DECLINED;
        !            95:                 }
1.30      raeburn    96:                 unless ($uri eq '/adm/migrateuser') {
                     97:                     my %user;
                     98:                     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
                     99:                     if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
                    100:                         my $token = &get_token($r,$remote_ip,\%user);
                    101:                         unless ($token eq '') {
                    102:                             $r->internal_redirect("/adm/migrateuser?token=$token");
                    103:                             $r->set_handlers('PerlHandler'=> undef);
                    104:                             return DECLINED;
                    105:                         }
                    106:                     }
                    107:                 }
1.28      raeburn   108:                 my $protocol = 'http';
                    109:                 my $port = $r->get_server_port();
                    110:                 if ($port eq '443') {
                    111:                     $protocol = 'https';
                    112:                 }
1.30      raeburn   113:                 my $querystring = $r->args;
                    114:                 my $location = $protocol.'://'.$redirect.$uri;
                    115:                 if ($querystring) {
                    116:                     $location .= "?$querystring";
                    117:                 }
                    118:                 $r->header_out(Location => $location);
1.28      raeburn   119:                 return REDIRECT;
                    120:             }
1.27      raeburn   121:         }
                    122:     }
1.7       raeburn   123:     if ($r->uri=~m|^(/raw)?/uploaded/|) {
                    124:         my $fn = $r->uri();
                    125:         $fn=~s/^\/raw//;
                    126:         my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
1.10      albertel  127: 	if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
1.7       raeburn   128:         my $chome=&Apache::lonnet::homeserver($uname,$udom);
1.9       albertel  129: 	my $allowed=0;
                    130: 	my @ids=&Apache::lonnet::current_machine_ids();
                    131: 	foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
                    132: 	if ($allowed) {
1.13      albertel  133: 	    $r->filename(&propath($udom,$uname).
                    134: 			 '/userfiles/'.(join('/',@ufile)));
1.7       raeburn   135:         }
1.15      raeburn   136:         return OK;
1.26      raeburn   137:     } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
                    138:         my $uri = $r->uri;
                    139:         $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
                    140:         $r->uri($uri);
1.15      raeburn   141:     }
1.26      raeburn   142:     return DECLINED;
1.15      raeburn   143: }
                    144: 
                    145: sub redirect_raw {
                    146:     my ($remote_ip,$lonhost) = @_;
                    147:     my @remhostids = &Apache::lonnet::get_hosts_from_ip($remote_ip);
                    148:     my $redirect;
                    149:     while (@remhostids) {
                    150:         my $try_server = pop(@remhostids);
                    151:         my $remhostname = &Apache::lonnet::hostname($try_server);
                    152:         if ($remhostname) {
                    153:             my $remoterev = &Apache::lonnet::get_server_loncaparev(undef,$try_server);
                    154:             my ($remmajor,$remminor) = ($remoterev =~ /^(\d+)\.(\d+)/);
                    155:             if (($remmajor > 2) || (($remmajor == 2) && $remminor >= 12)) {
                    156:                 my $internet_names = &Apache::lonnet::get_internet_names($try_server);
                    157:                 if (ref($internet_names) eq 'ARRAY') {
                    158:                     my $intdom = &Apache::lonnet::internet_dom($lonhost);
                    159:                     unless (grep(/^\Q$intdom\E$/,@{$internet_names})) {
                    160:                         my $lonhostname = &Apache::lonnet::hostname($lonhost);
                    161:                         my $serverhomeID = &Apache::lonnet::get_server_homeID($lonhostname);
                    162:                         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                    163:                         my %domdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
                    164:                         my $replication = $domdefaults{'replication'};
                    165:                         if (ref($replication) eq 'HASH') {
                    166:                             my $remhomeID = &Apache::lonnet::get_server_homeID($remhostname);
                    167:                             my $remhomedom = &Apache::lonnet::host_domain($remhomeID);
                    168:                             my $remprimary = &Apache::lonnet::domain($remhomedom,'primary');
                    169:                             my $remintdom = &Apache::lonnet::internet_dom($remprimary);
                    170:                             if (ref($replication->{'certreq'}) eq 'ARRAY') {
                    171:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'certreq'}})) {
1.24      raeburn   172:                                     $redirect = 0;
                    173:                                 } else {
1.15      raeburn   174:                                     $redirect = 1;
                    175:                                 }
                    176:                             }
                    177:                             if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
                    178:                                 if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
1.24      raeburn   179:                                     $redirect = 1;
                    180:                                 } else {
1.15      raeburn   181:                                     $redirect = 0;
                    182:                                 }
                    183:                             }
                    184:                         }
                    185:                     }
                    186:                 }
                    187:             }
                    188:             last;
                    189:         }
                    190:     }
1.24      raeburn   191:     return $redirect;
1.1       www       192: }
                    193: 
1.30      raeburn   194: sub get_token {
                    195:     my ($r,$remote_ip,$userref) = @_;
                    196:     return unless (ref($userref) eq 'HASH');
                    197:     my %user = %{$userref};
                    198:     my %info = ('ip' => $remote_ip,
                    199:                 'domain'    => $user{'domain'},
                    200:                 'username'  => $user{'name'},
                    201:                 'server'    => $r->dir_config('lonHostID'),
                    202:                );
                    203:     my $query = $r->args;
                    204:     if ($query) {
                    205:         foreach my $pair (split(/&/,$query)) {
                    206:             my ($name,$value) = split(/=/,$pair);
                    207:             $name = &LONCAPA::unescape($name);
                    208:             next unless (($name eq 'role') || ($name eq 'symb'));
                    209:             $value =~ tr/+/ /;
                    210:             $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    211:             $info{$name} = $value;
                    212:         }
                    213:     }
                    214:     unless ($info{'role'}) {
                    215:         if ($user{'role'} ne '') {
                    216:             $info{'role'} = $user{'role'};
                    217:         }
                    218:     }
                    219:     unless ($info{'symb'}) {
                    220:         unless ($r->uri eq '/adm/roles') {
                    221:              $info{'origurl'} = $r->uri;
                    222:         }
                    223:     }
                    224:     my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'));
                    225:     unless (($token eq 'con_lost') || ($token eq 'refused') ||
                    226:             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
                    227:         return $token;
                    228:     }
                    229:     return;
                    230: }
                    231: 
1.1       www       232: 1;
                    233: __END__
                    234: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>
500 Internal Server Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at root@localhost to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.