1: # The LearningOnline Network
2: # URL translation for User Files
3: #
4: # $Id: lontrans.pm,v 1.14.10.2.2.2 2022/01/05 00:51:20 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 $alias = &Apache::lonnet::get_proxy_alias();
43: if ($alias) {
44: my $hdrhost = $r->headers_in->get('Host');
45: my $lonhost = $r->dir_config('lonHostID');
46: my $hostname = &Apache::lonnet::hostname($lonhost);
47: my $ssourl = '/adm/sso';
48: if ($r->dir_config('lonOtherAuthenUrl') ne '') {
49: $ssourl = $r->dir_config('lonOtherAuthenUrl');
50: }
51: if (($hdrhost eq $alias) || ($hdrhost eq $hostname)) {
52: my $proxyinfo = &Apache::lonnet::get_proxy_settings($r->dir_config('lonDefDomain'));
53: my ($vpnint,$vpnext);
54: if (ref($proxyinfo) eq 'HASH') {
55: $vpnint = $proxyinfo->{'vpnint'};
56: $vpnext = $proxyinfo->{'vpnext'};
57: }
58: my ($redirect,$remote_ip);
59: if ($hdrhost eq $alias) {
60: $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
61: if (($vpnext && &Apache::lonnet::ip_match($remote_ip,$vpnext)) &&
62: ($r->uri !~ m{^/adm/(lti|launch)/})) {
63: $redirect = $hostname;
64: }
65: if ($r->uri eq $ssourl) {
66: if (&Apache::lonnet::alias_sso($lonhost)) {
67: undef($redirect);
68: } else {
69: $redirect = $hostname;
70: }
71: }
72: if ($redirect eq $hdrhost) {
73: undef($redirect);
74: }
75: } elsif ($hdrhost eq $hostname) {
76: $remote_ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP,1);
77: unless (($remote_ip eq '127.0.0.1') || ($remote_ip eq '::1') ||
78: ($remote_ip eq &Apache::lonnet::get_host_ip($lonhost)) ||
79: ($r->uri=~m{^/adm/(lti|launch)/}) ||
80: ($vpnint && &Apache::lonnet::ip_match($remote_ip,$vpnint))) {
81: $redirect = $alias;
82: if (($r->uri=~m{^/raw/}) || ($r->uri=~m{^/adm/dns/})) {
83: my %iphost = &Apache::lonnet::get_iphost();
84: if (exists($iphost{$remote_ip})) {
85: undef($redirect);
86: }
87: } elsif ($r->uri eq $ssourl) {
88: unless (&Apache::lonnet::alias_sso($lonhost)) {
89: undef($redirect);
90: }
91: }
92: }
93: }
94: if ($redirect) {
95: my $uri = $r->uri;
96: if (($uri eq '/adm/switchserver') || ($uri =~ m{^/Shibboleth.sso/})) {
97: return DECLINED;
98: }
99: unless (($uri eq '/adm/migrateuser') || ($uri eq $ssourl)) {
100: my %user;
101: my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
102: if (($handle) && ($user{'name'} ne '') && ($user{'domain'} ne '')) {
103: unless (($user{'name'} eq 'public') && ($user{'domain'} eq 'public')) {
104: my $dest = '/adm/migrateuser';
105: my $token = &set_token($r,$dest,$remote_ip,\%user);
106: unless ($token eq '') {
107: $r->internal_redirect("$dest?token=$token");
108: $r->set_handlers('PerlHandler'=> undef);
109: return DECLINED;
110: }
111: }
112: }
113: }
114: my $protocol = 'http';
115: my $port = $r->get_server_port();
116: if ($port eq '443') {
117: $protocol = 'https';
118: }
119: if ($uri =~ m{^(/adm/css/)(.+)(.css)$}) {
120: $uri = $1.&escape($2).$3;
121: }
122: my $location = $protocol.'://'.$redirect.$uri;
123: if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
124: my $token = &set_token($r,$r->uri);
125: unless ($token eq '') {
126: $location .= '?ttoken='.$token;
127: }
128: } elsif ($r->args) {
129: $location .= '?'.$r->args;
130: }
131: $r->header_out(Location => $location);
132: return REDIRECT;
133: }
134: }
135: }
136: if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
137: my $token = &set_token($r,$r->uri);
138: unless ($token eq '') {
139: $r->args('ttoken='.$token);
140: }
141: }
142: if ($r->uri=~m|^(/raw)?/uploaded/|) {
143: my $fn = $r->uri();
144: $fn=~s/^\/raw//;
145: my (undef,undef,$udom,$uname,@ufile)=split(/\//,$fn);
146: if (@ufile) { $ufile[-1]=~s/^[\~\.]+//; }
147: my $chome=&Apache::lonnet::homeserver($uname,$udom);
148: my $allowed=0;
149: my @ids=&Apache::lonnet::current_machine_ids();
150: foreach my $id (@ids) { if ($id eq $chome) { $allowed=1; } }
151: if ($allowed) {
152: $r->filename(&propath($udom,$uname).
153: '/userfiles/'.(join('/',@ufile)));
154: }
155: return OK;
156: } elsif ($r->uri =~ m{^\Q/adm/wrapper/ext/https:/\E[^/]}) {
157: my $uri = $r->uri;
158: $uri =~ s{^(\Q/adm/wrapper/ext/https:/\E)}{$1/};
159: $r->uri($uri);
160: }
161: return DECLINED;
162: }
163:
164: sub set_token {
165: my ($r,$dest,$remote_ip,$userref) = @_;
166: my (%info,%user);
167: if ($dest eq '/adm/migrateuser') {
168: return unless (ref($userref) eq 'HASH');
169: %user = %{$userref};
170: %info = ('ip' => $remote_ip,
171: 'domain' => $user{'domain'},
172: 'username' => $user{'name'},
173: 'server' => $r->dir_config('lonHostID'),
174: );
175: }
176: if ($r->args) {
177: foreach my $pair (split(/&/,$r->args)) {
178: my ($name,$value) = split(/=/,$pair);
179: $name = &LONCAPA::unescape($name);
180: next unless (($name eq 'role') || ($name eq 'symb') ||
181: ($name eq 'ltoken') || ($name eq 'linkkey') ||
182: ($name eq 'ttoken'));
183: $value =~ tr/+/ /;
184: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
185: $info{$name} = $value;
186: }
187: if ($info{'ttoken'}) {
188: my %current = &Apache::lonnet::tmpget($info{'ttoken'});
189: delete($info{'ttoken'});
190: if ($current{'origurl'} eq $r->uri) {
191: return if (keys(%current));
192: }
193: }
194: }
195: #
196: # To be able to support deep-linking for a shortened URL, i.e.,
197: # /tiny/$dom/$id with (a) linkkey included in POSTed data and
198: # data and (b) non-Shibboleth SSO in use, ltoken (or linkprot
199: # if sending to /adm/migrateuser), or linkkey are written to a
200: # .tmp file in /home/httpd/perl/tmp and the query string is
201: # replaced with ttoken=tokenid. This does mean other POSTed
202: # data will be discarded.
203: #
204: if ($r->uri =~ m{^/tiny/$match_domain/\w+$}) {
205: unless (($info{'ltoken'}) || ($info{'linkkey'} ne '')) {
206: &Apache::lonacc::get_posted_cgi($r,['linkkey']);
207: if ($env{'form.linkkey'} ne '') {
208: $info{'linkkey'} = $env{'form.linkkey'};
209: }
210: }
211: }
212: if ($dest eq '/adm/migrateuser') {
213: if ($info{'ltoken'}) {
214: my %link_info = &Apache::lonnet::tmpget($info{'ltoken'});
215: if ($link_info{'linkprot'}) {
216: $info{'linkprot'} = $link_info{'linkprot'};
217: }
218: &Apache::lonnet::tmpdel($info{'ltoken'});;
219: delete($info{'ltoken'});
220: }
221: unless ($info{'role'}) {
222: if ($user{'role'} ne '') {
223: $info{'role'} = $user{'role'};
224: }
225: }
226: unless ($info{'symb'}) {
227: unless ($r->uri eq '/adm/roles') {
228: $info{'origurl'} = $r->uri;
229: }
230: }
231: }
232: if (($dest eq '/adm/migrateuser') || (keys(%info) > 0)) {
233: unless ($dest eq '/adm/migrateuser') {
234: $info{'origurl'} = $r->uri;
235: }
236: my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
237: unless (($token eq 'con_lost') || ($token eq 'refused') ||
238: ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
239: return $token;
240: }
241: }
242: return;
243: }
244:
245: 1;
246: __END__
247:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>