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