Annotation of loncom/auth/switchserver.pm, revision 1.44
1.1 albertel 1: # The LearningOnline Network
2: # Switch Servers Handler
3: #
1.44 ! raeburn 4: # $Id: switchserver.pm,v 1.43 2018/11/28 05:05:30 raeburn Exp $
1.1 albertel 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::switchserver;
30:
31: use strict;
32: use Apache::Constants qw(:common);
33: use Apache::lonnet;
1.42 raeburn 34: use Digest::MD5 qw(md5_hex);
1.1 albertel 35: use CGI::Cookie();
36: use Apache::lonlocal;
1.28 raeburn 37: use LONCAPA qw(:DEFAULT :match);
1.1 albertel 38:
39: sub init_env {
40: my ($r) = @_;
1.5 albertel 41:
42: if (-e $env{'user.environment'}) {
43: return $env{'user.environment'};
44: }
1.1 albertel 45: my $requrl=$r->uri;
1.18 albertel 46: my $handle= &Apache::lonnet::check_for_valid_session($r);
47: if ($handle ne '') {
1.1 albertel 48: return undef;
49: }
1.18 albertel 50: my $lonidsdir=$r->dir_config('lonIDsDir');
1.1 albertel 51: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.42 raeburn 52:
1.5 albertel 53: return $r->dir_config('lonIDsDir')."/$handle.id";
1.1 albertel 54: }
55:
1.14 albertel 56: sub do_redirect {
57: my ($r,$url,$only_body,$extra_text) = @_;
58: $r->send_http_header;
59: my $start_page =
1.16 raeburn 60: &Apache::loncommon::start_page('Switching Server ...',undef,
61: {'redirect' => [0.5,$url],
1.14 albertel 62: 'only_body' => $only_body,});
63: my $end_page = &Apache::loncommon::end_page();
64: $r->print($start_page.$extra_text.$end_page);
1.30 raeburn 65: unless ($env{'user.name'} eq 'public' && ($env{'user.domain'} eq 'public')) {
66: $r->register_cleanup(\&flush_course_logs);
67: }
1.14 albertel 68: return OK;
1.30 raeburn 69: }
1.14 albertel 70:
1.30 raeburn 71: sub flush_course_logs {
72: &Apache::lonnet::flushcourselogs();
73: return OK;
1.14 albertel 74: }
75:
1.1 albertel 76: sub handler {
1.5 albertel 77: my ($r) = @_;
1.1 albertel 78:
79: my $handle=&init_env($r);
80: if (!defined($handle)) { return FORBIDDEN; }
81:
82: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.22 raeburn 83: ['otherserver','role','origurl','symb']);
1.5 albertel 84:
1.13 albertel 85: my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.17 albertel 86: if (! $env{'form.otherserver'}) {
87: $env{'form.otherserver'} =
88: &Apache::lonnet::find_existing_session($env{'user.domain'},
89: $env{'user.name'});
90: if (! $env{'form.otherserver'}) {
91: $env{'form.otherserver'} =
92: &Apache::lonnet::spareserver(30000,undef,1);
93: }
1.13 albertel 94: $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.4 albertel 95: }
1.5 albertel 96:
1.1 albertel 97: if (!defined($switch_to)) { return FORBIDDEN; }
1.4 albertel 98:
1.33 raeburn 99: my $protocol = 'http';
100: if ($env{'form.otherserver'}) {
101: if ($Apache::lonnet::protocol{$env{'form.otherserver'}} eq 'https') {
102: $protocol = $Apache::lonnet::protocol{$env{'form.otherserver'}};
103: }
1.42 raeburn 104: }
1.33 raeburn 105:
1.14 albertel 106: if ($env{'user.name'} eq 'public'
107: && $env{'user.domain'} eq 'public') {
1.33 raeburn 108: my $url = $protocol.'://'.$switch_to.$r->uri;
1.36 raeburn 109: unlink($handle);
110: #expire the cookie
111: my $c = new CGI::Cookie(-name => 'lonPubID',
112: -value => '',
113: -expires => '-10y',);
114: $r->header_out('Set-cookie' => $c);
1.14 albertel 115: return &do_redirect($r,$url,1)
116: }
117:
1.28 raeburn 118: my $skip_canhost_check = '';
119: if ($env{'form.role'}) {
120: if (!exists($env{'user.role.'.$env{'form.role'}})) {
1.35 raeburn 121: delete($env{'form.role'});
1.28 raeburn 122: } else {
123: my $now = time;
124: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
125: if (($start && $start > $now) || ($end && $end < $now)) {
1.35 raeburn 126: delete($env{'form.role'});
1.28 raeburn 127: } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
128: if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
129: $skip_canhost_check = 1;
130: }
1.31 raeburn 131: } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
1.37 raeburn 132: my ($audom,$auname) = ($1,$2);
133: if (&Apache::lonnet::homeserver($auname,$audom) eq $env{'form.otherserver'}) {
134: if ((&Apache::lonnet::will_trust('othcoau',$audom,$env{'user.domain'})) &&
135: (&Apache::lonnet::will_trust('coremau',$env{'user.domain'},$audom))) {
136: $skip_canhost_check = 1;
137: }
1.28 raeburn 138: }
139: }
140: }
141: }
142:
143: unless ($skip_canhost_check) {
144: my $canhost = 1;
145: my $uprimary_id = &Apache::lonnet::domain($env{'user.domain'},'primary');
146: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
1.29 raeburn 147: my @intdoms;
148: my $internet_names = &Apache::lonnet::get_internet_names($env{'form.otherserver'});
149: if (ref($internet_names) eq 'ARRAY') {
150: @intdoms = @{$internet_names};
151: }
1.28 raeburn 152: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
153: my $serverhomeID = &Apache::lonnet::get_server_homeID($switch_to);
154: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
155: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
156: my %udomdefaults = &Apache::lonnet::get_domain_defaults($env{'user.domain'});
157: my $remoterev = &Apache::lonnet::get_server_loncaparev($env{'user.domain'},$env{'form.otherserver'});
158: $canhost =
159: &Apache::lonnet::can_host_session($env{'user.domain'},
160: $env{'form.otherserver'},
161: $remoterev,
162: $udomdefaults{'remotesessions'},
163: $defdomdefaults{'hostedsessions'});
164: }
165: unless ($canhost) { return FORBIDDEN; }
1.26 raeburn 166: }
167:
1.1 albertel 168: #remove session env, and log event
1.41 raeburn 169: if (unlink($handle)) {
170: if ($env{'user.linkedenv'} ne '') {
171: my $lonidsdir=$r->dir_config('lonIDsDir');
172: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
173: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
174: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq $handle)) {
175: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
176: }
1.36 raeburn 177: }
178: }
1.42 raeburn 179: my $now = time;
180: my %temp=('switchserver' => $now.':'.$env{'form.otherserver'},
1.1 albertel 181: $env{'form.role'});
182: &Apache::lonnet::put('email_status',\%temp);
1.34 raeburn 183: my $logmsg = "Switch Server to $env{'form.otherserver'}";
184: if ($env{'form.role'}) {
185: $logmsg .= " with role: $env{'form.role'}";
1.39 raeburn 186: } elsif (($env{'form.lti.reqcrs'}) && ($env{'form.lti.reqrole'} eq 'cc')) {
187: $logmsg .= " to create new LTI course";
188: } elsif ($env{'form.lti.selfenrollrole'}) {
189: $logmsg .= " to selfenroll with role: $env{'form.lti.selfenrollrole'}";
1.34 raeburn 190: } else {
191: $logmsg .= " (no role)";
192: }
193: $logmsg .= ' '.$ENV{'REMOTE_ADDR'};
1.1 albertel 194: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
1.34 raeburn 195: $env{'user.home'},$logmsg);
1.1 albertel 196:
197: &Apache::loncommon::content_type($r,'text/html');
1.12 albertel 198:
1.41 raeburn 199: #expire the cookies
200: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
201: foreach my $name (keys(%cookies)) {
202: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
203: my $c = new CGI::Cookie(-name => $name,
204: -value => '',
205: -expires => '-10y',);
206: $r->headers_out->add('Set-cookie' => $c);
1.36 raeburn 207: }
1.20 albertel 208: if ($r->header_only) {
209: $r->send_http_header;
210: return OK;
211: }
1.1 albertel 212: # -------------------------------------------------------- Menu script and info
213:
214: # ---------------------------------------------------------------- Get handover
215:
1.42 raeburn 216: my $newcookieid;
1.43 raeburn 217: my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
1.42 raeburn 218: &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'});
219: if ($is_balancer && $setcookie && $env{'form.otherserver'}) {
220:
221: # Set a balancer cookie unless browser already sent LON-CAPA load balancer
222: # cookie which points at the target server
223: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
224:
225: unless (($found_server eq $env{'form.otherserver'}) &&
226: ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
227: my @hosts = &Apache::lonnet::current_machine_ids();
228: my $desthost = $env{'form.otherserver'};
229: unless (grep(/^\Q$desthost\E$/,@hosts)) {
230: my $balancedir=$r->dir_config('lonBalanceDir');
231: $newcookieid = &md5_hex(&md5_hex($now.{}.rand().$$));
232: my $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$newcookieid;
233: my $balcookie = "balanceID=$cookie; path=/; HttpOnly;";
234: if (open(my $fh,'>',"$balancedir/$cookie.id")) {
235: print $fh $env{'form.otherserver'};
236: close($fh);
237: }
238: $r->headers_out->add('Set-cookie' => $balcookie);
239: }
240: }
241: }
242:
1.43 raeburn 243: my %info=('ip' => $ENV{'REMOTE_ADDR'},
244: 'domain' => $env{'user.domain'},
245: 'username' => $env{'user.name'},
246: 'home' => $env{'user.home'},
247: 'role' => $env{'form.role'},
248: 'server' => $r->dir_config('lonHostID'),
249: 'balancer' => $is_balancer,
250: 'dom_balancers' => $dom_balancers,
251: 'offloadto' => '');
252: if (ref($offloadto) eq 'HASH') {
253: foreach my $key (keys(%{$offloadto})) {
254: if (ref($offloadto->{$key}) eq 'ARRAY') {
255: $info{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
256: }
257: }
258: $info{'offloadto'} =~ s/\&$//;
259: } elsif (ref($offloadto) eq 'ARRAY') {
260: $info{'offloadto'} = join(',',@{$offloadto});
261: }
1.42 raeburn 262: if ($newcookieid) {
263: $info{'balcookie'} = $newcookieid;
264: }
1.22 raeburn 265: if ($env{'form.origurl'}) {
266: $info{'origurl'} = $env{'form.origurl'};
267: }
268: if ($env{'form.symb'}) {
269: $info{'symb'} = $env{'form.symb'};
270: }
1.8 albertel 271: if ($env{'request.sso.login'}) {
272: $info{'sso.login'} = $env{'request.sso.login'};
273: }
1.11 raeburn 274: if ($env{'request.sso.reloginserver'}) {
275: $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
276: }
1.38 raeburn 277: if ($env{'request.lti.login'}) {
278: $info{'lti.login'} = $env{'request.lti.login'};
279: }
1.40 raeburn 280: if ($env{'request.lti.uri'}) {
281: $info{'lti.uri'} = $env{'request.lti.uri'};
282: }
1.39 raeburn 283: if ($env{'request.lti.reqcrs'}) {
284: $info{'lti.reqcrs'} = $env{'request.lti.reqcrs'};
285: }
286: if ($env{'request.lti.reqrole'}) {
287: $info{'lti.reqrole'} = $env{'request.lti.reqrole'};
288: }
289: if ($env{'request.lti.selfenrollrole'}) {
290: $info{'lti.selfenrollrole'} = $env{'request.lti.selfenrollrole'};
291: }
292: if ($env{'request.lti.sourcecrs'}) {
293: $info{'lti.sourcecrs'} = $env{'request.lti.sourcecrs'};
294: }
1.38 raeburn 295: if ($env{'request.lti.passbackid'}) {
296: $info{'lti.passbackid'} = $env{'request.lti.passbackid'};
297: }
298: if ($env{'request.lti.passbackurl'}) {
299: $info{'lti.passbackurl'} = $env{'request.lti.passbackurl'};
300: }
301: if ($env{'request.lti.rosterid'}) {
302: $info{'lti.rosterid'} = $env{'request.lti.rosterid'};
303: }
304: if ($env{'request.lti.rosterurl'}) {
305: $info{'lti.rosterurl'} = $env{'request.lti.rosterurl'};
306: }
1.40 raeburn 307: if ($env{'request.lti.target'}) {
308: $info{'lti.target'} = $env{'request.lti.target'};
309: }
1.3 albertel 310: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.33 raeburn 311: my $url =$protocol.'://'.$switch_to.'/adm/login?'.
1.14 albertel 312: 'domain='.$env{'user.domain'}.
1.21 albertel 313: '&username='.$env{'user.name'}.
314: '&token='.$token;
1.1 albertel 315: # --------------------------------------------------------------- Screen Output
1.24 droeschl 316: return &do_redirect($r, $url, 0);
1.1 albertel 317: }
318:
319: 1;
320: __END__
321:
322:
323:
324:
325:
326:
327:
328:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>