Annotation of loncom/lontrans.pm, revision 1.41
1.1 www 1: # The LearningOnline Network
2: # URL translation for User Files
3: #
1.41 ! raeburn 4: # $Id: lontrans.pm,v 1.40 2022/06/30 21:04:15 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.41 ! raeburn 43: if (($r->uri eq '/adm/service/passback') ||
! 44: ($r->uri eq '/adm/service/roster')) {
! 45: return OK;
! 46: }
1.25 raeburn 47: if ($r->uri=~m{^/raw/}) {
1.28 raeburn 48: if ($hdrhost) {
49: unless ($hdrhost =~ /^internal\-/) {
1.27 raeburn 50: my $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
1.16 raeburn 51: my $lonhost = $r->dir_config('lonHostID');
52: if (&redirect_raw($remote_ip,$lonhost)) {
1.28 raeburn 53: my $location = 'https://internal-'.$hdrhost.$r->uri;
1.16 raeburn 54: $r->headers_out->set(Location => $location);
55: return REDIRECT;
1.15 raeburn 56: }
57: }
58: }
59: }
1.27 raeburn 60: my $alias = &Apache::lonnet::get_proxy_alias();
61: if ($alias) {
62: my $lonhost = $r->dir_config('lonHostID');
1.28 raeburn 63: my $hostname = &Apache::lonnet::hostname($lonhost);
1.38 raeburn 64: my $ssourl = '/adm/sso';
65: if ($r->dir_config('lonOtherAuthenUrl') ne '') {
66: $ssourl = $r->dir_config('lonOtherAuthenUrl');
67: }
1.28 raeburn 68: if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
69: my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
1.29 raeburn 70: my ($vpnint,$vpnext);
1.28 raeburn 71: if (ref($proxyinfo) eq 'HASH') {
1.29 raeburn 72: $vpnint = $proxyinfo->{'vpnint'};
73: $vpnext = $proxyinfo->{'vpnext'};
1.28 raeburn 74: }
1.30 raeburn 75: my ($redirect,$remote_ip);
1.28 raeburn 76: if ($hdrhost eq $alias) {
1.30 raeburn 77: $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.34 raeburn 78: if (($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) &&
79: ($r->uri !~ m{^/adm/(lti|launch)/})) {
1.28 raeburn 80: $redirect = $hostname;
1.36 raeburn 81: }
1.38 raeburn 82: if ($r->uri eq $ssourl) {
83: if (&Apache::lonnet::alias_sso($lonhost)) {
1.28 raeburn 84: undef($redirect);
1.36 raeburn 85: } else {
86: $redirect = $hostname;
1.28 raeburn 87: }
88: }
1.36 raeburn 89: if ($redirect eq $hdrhost) {
90: undef($redirect);
91: }
1.28 raeburn 92: } elsif ($hdrhost eq $hostname) {
1.30 raeburn 93: $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
1.29 raeburn 94: unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
1.28 raeburn 95: ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
1.34 raeburn 96: ($r->uri=~m{^/adm/(lti|launch)/}) ||
1.29 raeburn 97: ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
1.28 raeburn 98: $redirect = $alias;
1.34 raeburn 99: if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/})) {
1.27 raeburn 100: my %iphost = &Apache::lonnet::get_iphost();
101: if (exists($iphost{$remote_ip})) {
1.28 raeburn 102: undef($redirect);
1.27 raeburn 103: }
1.38 raeburn 104: } elsif ($r->uri eq $ssourl) {
105: unless (&Apache::lonnet::alias_sso($lonhost)) {
1.36 raeburn 106: undef($redirect);
107: }
1.27 raeburn 108: }
109: }
110: }
1.28 raeburn 111: if ($redirect) {
112: my $uri = $r->uri;
1.36 raeburn 113: if (($uri eq '/adm/switchserver') || ($uri =~ m{^/Shibboleth.sso/})) {
1.32 raeburn 114: return DECLINED;
115: }
1.38 raeburn 116: unless (($uri eq '/adm/migrateuser') || ($uri eq $ssourl)) {
1.30 raeburn 117: my %user;
118: my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
119: if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
1.35 raeburn 120: unless (($user{'name'} eq 'public') && ($user{'domain'} eq 'public')) {
1.37 raeburn 121: my $dest = '/adm/migrateuser';
122: my $token = &set_token($r,$dest,$remote_ip,\%user);
1.35 raeburn 123: unless ($token eq '') {
1.37 raeburn 124: $r->internal_redirect("$dest?token=$token");
1.35 raeburn 125: $r->set_handlers('PerlHandler'=> undef);
126: return DECLINED;
127: }
1.30 raeburn 128: }
129: }
130: }
1.28 raeburn 131: my $protocol = 'http';
132: my $port = $r->get_server_port();
133: if ($port eq '443') {
134: $protocol = 'https';
135: }
1.33 raeburn 136: if ($uri =~ m{^(/adm/css/)(.+)(.css)$}) {
137: $uri = $1.&escape($2).$3;
138: }
1.30 raeburn 139: my $location = $protocol.'://'.$redirect.$uri;
1.37 raeburn 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;
1.30 raeburn 147: }
148: $r->header_out(Location => $location);
1.28 raeburn 149: return REDIRECT;
150: }
1.27 raeburn 151: }
152: }
1.37 raeburn 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: }
1.7 raeburn 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);
1.10 albertel 163: if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
1.7 raeburn 164: my $chome=&Apache::lonnet::homeserver($uname,$udom);
1.9 albertel 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) {
1.13 albertel 169: $r->filename(&propath($udom,$uname).
170: '/userfiles/'.(join('/',@ufile)));
1.7 raeburn 171: }
1.15 raeburn 172: return OK;
1.26 raeburn 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);
1.15 raeburn 177: }
1.26 raeburn 178: return DECLINED;
1.15 raeburn 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'}})) {
1.24 raeburn 208: $redirect = 0;
209: } else {
1.15 raeburn 210: $redirect = 1;
211: }
212: }
213: if (ref($replication->{'nocertreq'}) eq 'ARRAY') {
214: if (grep(/^\Q$remintdom\E$/,@{$replication->{'nocertreq'}})) {
1.24 raeburn 215: $redirect = 1;
216: } else {
1.15 raeburn 217: $redirect = 0;
218: }
219: }
220: }
221: }
222: }
223: }
224: last;
225: }
226: }
1.24 raeburn 227: return $redirect;
1.1 www 228: }
229:
1.37 raeburn 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)) {
1.30 raeburn 244: my ($name,$value) = split(/=/,$pair);
245: $name = &LONCAPA::unescape($name);
1.37 raeburn 246: next unless (($name eq 'role') || ($name eq 'symb') ||
247: ($name eq 'ltoken') || ($name eq 'linkkey') ||
248: ($name eq 'ttoken'));
1.30 raeburn 249: $value =~ tr/+/ /;
250: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
251: $info{$name} = $value;
252: }
1.37 raeburn 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: }
1.30 raeburn 260: }
1.37 raeburn 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: }
1.30 raeburn 276: }
277: }
1.37 raeburn 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'};
1.41 ! raeburn 283: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
1.40 raeburn 284: if ($link_info{$item} ne '') {
285: $info{$item} = $link_info{$item};
286: }
1.39 raeburn 287: }
1.37 raeburn 288: }
1.39 raeburn 289: &Apache::lonnet::tmpdel($info{'ltoken'});
1.37 raeburn 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: }
1.30 raeburn 301: }
302: }
1.37 raeburn 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: }
1.30 raeburn 312: }
313: return;
314: }
315:
1.1 www 316: 1;
317: __END__
318:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>