Annotation of loncom/auth/switchserver.pm, revision 1.66
1.1 albertel 1: # The LearningOnline Network
2: # Switch Servers Handler
3: #
1.66 ! raeburn 4: # $Id: switchserver.pm,v 1.65 2023/06/02 01:20:26 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;
1.55 raeburn 32: use Apache::Constants qw(:common :remotehost);
1.1 albertel 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 {
1.62 raeburn 57: my ($r,$url,$only_body,$extra_text,$write_to_opener) = @_;
1.14 albertel 58: $r->send_http_header;
1.49 raeburn 59: my $delay = 0.5;
1.56 raeburn 60: if ($only_body && !$extra_text) {
1.49 raeburn 61: $delay = 0;
62: }
1.14 albertel 63: my $start_page =
1.16 raeburn 64: &Apache::loncommon::start_page('Switching Server ...',undef,
1.63 raeburn 65: {'redirect' => [$delay,$url,'',$write_to_opener,1],
1.66 ! raeburn 66: 'only_body' => $only_body,
! 67: 'switchserver' => 1,
! 68: 'links_disabled' => 1,});
1.14 albertel 69: my $end_page = &Apache::loncommon::end_page();
70: $r->print($start_page.$extra_text.$end_page);
1.30 raeburn 71: unless ($env{'user.name'} eq 'public' && ($env{'user.domain'} eq 'public')) {
72: $r->register_cleanup(\&flush_course_logs);
73: }
1.14 albertel 74: return OK;
1.30 raeburn 75: }
1.14 albertel 76:
1.45 raeburn 77: sub balancer_cookieid {
78: my ($r,$desthost,$uname,$udom) = @_;
79: my @hosts = &Apache::lonnet::current_machine_ids();
80: my $newcookieid;
81: unless (grep(/^\Q$desthost\E$/,@hosts)) {
82: my $balancedir=$r->dir_config('lonBalanceDir');
83: $newcookieid = &md5_hex(&md5_hex(time.{}.rand().$$));
84: my $cookie = $udom.'_'.$uname.'_'.$newcookieid;
85: my $balcookie = "balanceID=$cookie; path=/; HttpOnly;";
86: if (open(my $fh,'>',"$balancedir/$cookie.id")) {
87: print $fh $desthost;
88: close($fh);
89: $r->headers_out->add('Set-cookie' => $balcookie);
90: }
91: }
92: return $newcookieid;
93: }
94:
1.30 raeburn 95: sub flush_course_logs {
96: &Apache::lonnet::flushcourselogs();
97: return OK;
1.14 albertel 98: }
99:
1.1 albertel 100: sub handler {
1.5 albertel 101: my ($r) = @_;
1.60 raeburn 102:
1.1 albertel 103: my $handle=&init_env($r);
104: if (!defined($handle)) { return FORBIDDEN; }
105:
106: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.63 raeburn 107: ['otherserver','role','origurl','symb','lcssowin','edit']);
1.5 albertel 108:
1.13 albertel 109: my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.17 albertel 110: if (! $env{'form.otherserver'}) {
111: $env{'form.otherserver'} =
112: &Apache::lonnet::find_existing_session($env{'user.domain'},
113: $env{'user.name'});
114: if (! $env{'form.otherserver'}) {
115: $env{'form.otherserver'} =
1.55 raeburn 116: &Apache::lonnet::spareserver($r,30000,undef,1);
1.17 albertel 117: }
1.13 albertel 118: $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.4 albertel 119: }
1.5 albertel 120:
1.1 albertel 121: if (!defined($switch_to)) { return FORBIDDEN; }
1.4 albertel 122:
1.33 raeburn 123: my $protocol = 'http';
124: if ($env{'form.otherserver'}) {
125: if ($Apache::lonnet::protocol{$env{'form.otherserver'}} eq 'https') {
126: $protocol = $Apache::lonnet::protocol{$env{'form.otherserver'}};
127: }
1.54 raeburn 128: unless ($env{'form.otherserver'} eq $r->dir_config('lonHostID')) {
129: my $alias = &Apache::lonnet::use_proxy_alias($r,$env{'form.otherserver'});
130: $switch_to = $alias if ($alias ne '');
131: }
1.42 raeburn 132: }
1.33 raeburn 133:
1.14 albertel 134: if ($env{'user.name'} eq 'public'
135: && $env{'user.domain'} eq 'public') {
1.33 raeburn 136: my $url = $protocol.'://'.$switch_to.$r->uri;
1.36 raeburn 137: unlink($handle);
138: #expire the cookie
139: my $c = new CGI::Cookie(-name => 'lonPubID',
140: -value => '',
141: -expires => '-10y',);
142: $r->header_out('Set-cookie' => $c);
1.14 albertel 143: return &do_redirect($r,$url,1)
144: }
145:
1.28 raeburn 146: my $skip_canhost_check = '';
1.45 raeburn 147: my $now = time;
1.28 raeburn 148: if ($env{'form.role'}) {
149: if (!exists($env{'user.role.'.$env{'form.role'}})) {
1.35 raeburn 150: delete($env{'form.role'});
1.28 raeburn 151: } else {
152: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
153: if (($start && $start > $now) || ($end && $end < $now)) {
1.35 raeburn 154: delete($env{'form.role'});
1.28 raeburn 155: } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
156: if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
157: $skip_canhost_check = 1;
158: }
1.31 raeburn 159: } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
1.37 raeburn 160: my ($audom,$auname) = ($1,$2);
161: if (&Apache::lonnet::homeserver($auname,$audom) eq $env{'form.otherserver'}) {
1.46 raeburn 162: if ((&Apache::lonnet::will_trust('othcoau',$env{'user.domain'},$audom)) &&
163: (&Apache::lonnet::will_trust('coaurem',$audom,$env{'user.domain'}))) {
1.37 raeburn 164: $skip_canhost_check = 1;
165: }
1.28 raeburn 166: }
167: }
168: }
169: }
170:
171: unless ($skip_canhost_check) {
1.63 raeburn 172: my $canhost = &Apache::lonnet::can_switchserver($env{'user.domain'},$env{'form.otherserver'});
173: unless ($canhost) {
174: if (($env{'request.course.id'}) && ($env{'form.symb'} ne '') &&
175: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
176: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
177: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
178: if (($cdom ne '') && ($cnum ne '') && ($env{'form.role'} =~ m{^([^.]+)\Q./$cdom/$cnum\E$})) {
179: my $symb = &Apache::lonnet::symbclean($env{'form.symb'});
180: my ($map,$idx,$url) = &Apache::lonnet::decode_symb($symb);
181: if (&Apache::lonnet::symbverify($symb,$url)) {
182: my $fileloc = &Apache::lonnet::declutter(&Apache::lonnet::filelocation("",$url));
183: my $resurl = &Apache::lonnet::clutter($url);
184: if ($resurl =~ m{^/res/($match_domain)/($match_username)/}) {
185: my ($audom,$auname) = ($1,$2);
186: if (&Apache::lonnet::homeserver($auname,$audom) eq $env{'form.otherserver'}) {
187: my @possroles = ("user.role.au./$audom/","user.role.ca./$audom/$auname","user.role.aa./$audom/$auname");
188: my $hasrole;
189: foreach my $rolekey (@possroles) {
190: if (exists($env{$rolekey})) {
191: my ($start,$end) = split(/\./,$env{$rolekey});
192: unless (($start && $start > $now) || ($end && $end < $now)) {
193: if ($rolekey eq "user.role.au./$audom/") {
194: $hasrole = $rolekey;
195: } elsif ((&Apache::lonnet::will_trust('othcoau',$env{'user.domain'},$audom)) &&
196: (&Apache::lonnet::will_trust('coaurem',$audom,$env{'user.domain'}))) {
197: $hasrole = $rolekey;
198: }
199: }
200: if ($hasrole) {
201: $hasrole =~ s/^\Quser.role.\E//;
202: last;
203: }
204: }
205: }
206: if ($hasrole) {
207: $env{'form.role'} = $hasrole;
208: $env{'form.origurl'} = &Apache::lonnet::deversion($resurl);
209: $env{'form.origurl'} =~ s{^/res/}{/priv/};
210: delete($env{'form.symb'});
211: $canhost = 1;
212: if ($env{'form.edit'}) {
213: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
214: my %info=('ip' => $ip,
215: 'domain' => $env{'user.domain'},
216: 'username' => $env{'user.name'},
217: 'home' => $env{'user.home'},
218: 'role' => $env{'form.role'},
219: 'server' => $r->dir_config('lonHostID'),
220: 'origurl' => $env{'form.origurl'});
221: &Apache::loncommon::content_type($r,'text/html');
222: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
223: my $url = $protocol.'://'.$switch_to.'/adm/login?'.
224: 'domain='.$env{'user.domain'}.
225: '&username='.$env{'user.name'}.
226: '&token='.$token;
227: return &do_redirect($r,$url,0);
228: }
229: }
230: }
231: }
232: }
233: }
234: }
1.28 raeburn 235: }
236: unless ($canhost) { return FORBIDDEN; }
1.26 raeburn 237: }
238:
1.1 albertel 239: #remove session env, and log event
1.41 raeburn 240: if (unlink($handle)) {
241: if ($env{'user.linkedenv'} ne '') {
242: my $lonidsdir=$r->dir_config('lonIDsDir');
243: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
244: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
245: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq $handle)) {
246: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
247: }
1.36 raeburn 248: }
249: }
1.42 raeburn 250: my %temp=('switchserver' => $now.':'.$env{'form.otherserver'},
1.1 albertel 251: $env{'form.role'});
252: &Apache::lonnet::put('email_status',\%temp);
1.34 raeburn 253: my $logmsg = "Switch Server to $env{'form.otherserver'}";
254: if ($env{'form.role'}) {
255: $logmsg .= " with role: $env{'form.role'}";
1.39 raeburn 256: } elsif (($env{'form.lti.reqcrs'}) && ($env{'form.lti.reqrole'} eq 'cc')) {
257: $logmsg .= " to create new LTI course";
258: } elsif ($env{'form.lti.selfenrollrole'}) {
259: $logmsg .= " to selfenroll with role: $env{'form.lti.selfenrollrole'}";
1.34 raeburn 260: } else {
261: $logmsg .= " (no role)";
262: }
1.55 raeburn 263: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.52 raeburn 264: $logmsg .= ' '.$ip;
1.1 albertel 265: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
1.34 raeburn 266: $env{'user.home'},$logmsg);
1.1 albertel 267:
268: &Apache::loncommon::content_type($r,'text/html');
1.12 albertel 269:
1.41 raeburn 270: #expire the cookies
271: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
272: foreach my $name (keys(%cookies)) {
273: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
274: my $c = new CGI::Cookie(-name => $name,
275: -value => '',
276: -expires => '-10y',);
277: $r->headers_out->add('Set-cookie' => $c);
1.36 raeburn 278: }
1.20 albertel 279: if ($r->header_only) {
280: $r->send_http_header;
281: return OK;
282: }
1.1 albertel 283: # -------------------------------------------------------- Menu script and info
1.51 raeburn 284:
1.1 albertel 285: # ---------------------------------------------------------------- Get handover
286:
1.51 raeburn 287: my ($is_balancer,$setcookie,$newcookieid,$otherbalcookie,$offloadto,$dom_balancers);
1.49 raeburn 288: my $only_body = 0;
1.50 raeburn 289: ($is_balancer,undef,$setcookie,$offloadto,$dom_balancers) =
290: &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'},'switchserver');
1.42 raeburn 291: if ($is_balancer && $setcookie && $env{'form.otherserver'}) {
292:
293: # Set a balancer cookie unless browser already sent LON-CAPA load balancer
294: # cookie which points at the target server
295: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
296:
1.49 raeburn 297: if (($found_server eq $env{'form.otherserver'}) &&
298: ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
299: $only_body = 1;
300: } else {
1.45 raeburn 301: $newcookieid = &balancer_cookieid($r,$env{'form.otherserver'},
302: $env{'user.name'},$env{'user.domain'});
1.42 raeburn 303: }
304: }
1.51 raeburn 305: if ((!$is_balancer) && ($env{'request.balancercookie'})) {
306: $otherbalcookie = $env{'request.balancercookie'};
307: }
1.42 raeburn 308:
1.53 raeburn 309: my %info=('ip' => $ip,
1.43 raeburn 310: 'domain' => $env{'user.domain'},
311: 'username' => $env{'user.name'},
312: 'home' => $env{'user.home'},
313: 'role' => $env{'form.role'},
314: 'server' => $r->dir_config('lonHostID'),
315: 'balancer' => $is_balancer,
316: 'dom_balancers' => $dom_balancers,
317: 'offloadto' => '');
318: if (ref($offloadto) eq 'HASH') {
319: foreach my $key (keys(%{$offloadto})) {
320: if (ref($offloadto->{$key}) eq 'ARRAY') {
321: $info{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
322: }
323: }
324: $info{'offloadto'} =~ s/\&$//;
325: } elsif (ref($offloadto) eq 'ARRAY') {
326: $info{'offloadto'} = join(',',@{$offloadto});
327: }
1.42 raeburn 328: if ($newcookieid) {
329: $info{'balcookie'} = $newcookieid;
1.51 raeburn 330: } elsif ($otherbalcookie) {
331: $info{'otherbalcookie'} = $otherbalcookie;
1.42 raeburn 332: }
1.22 raeburn 333: if ($env{'form.origurl'}) {
334: $info{'origurl'} = $env{'form.origurl'};
1.64 raeburn 335: if ($env{'form.origurl'} eq '/adm/email') {
336: if ($env{'request.display'} && ($env{'request.mailrecip'} eq "$env{'user.name'}:$env{'user.domain'}")) {
337: $info{'display'} = &escape($env{'request.display'});
338: $info{'mailrecip'} = &escape($env{'request.mailrecip'});
339: }
340: }
1.22 raeburn 341: }
342: if ($env{'form.symb'}) {
343: $info{'symb'} = $env{'form.symb'};
344: }
1.62 raeburn 345: my $ssologoutscript = '';
346: my $write_to_opener;
1.8 albertel 347: if ($env{'request.sso.login'}) {
348: $info{'sso.login'} = $env{'request.sso.login'};
1.56 raeburn 349: if (defined($r->dir_config("lonSSOUserLogoutScriptFile_$info{domain}"))) {
350: if (open(my $fh,'<',$r->dir_config("lonSSOUserLogoutScriptFile_$info{domain}"))) {
351: $ssologoutscript .= join('',<$fh>);
352: close($fh);
353: }
354: }
1.58 raeburn 355: if (defined($r->dir_config('lonSSOUserLogoutScriptFile'))) {
1.56 raeburn 356: if (open(my $fh,'<',$r->dir_config('lonSSOUserLogoutScriptFile'))) {
357: $ssologoutscript .= join('',<$fh>);
358: close($fh);
359: }
360: }
1.8 albertel 361: }
1.11 raeburn 362: if ($env{'request.sso.reloginserver'}) {
363: $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
364: }
1.47 raeburn 365: if ($env{'request.linkprot'}) {
366: $info{'linkprot'} = $env{'request.linkprot'};
1.65 raeburn 367: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
1.61 raeburn 368: if ($env{'request.'.$item}) {
369: $info{$item} = $env{'request.'.$item};
370: }
1.60 raeburn 371: }
1.59 raeburn 372: } elsif ($env{'request.linkkey'} ne '') {
1.57 raeburn 373: $info{'linkkey'} = $env{'request.linkkey'};
1.47 raeburn 374: }
1.48 raeburn 375: if ($env{'request.deeplink.login'}) {
376: $info{'deeplink.login'} = $env{'request.deeplink.login'};
377: }
1.38 raeburn 378: if ($env{'request.lti.login'}) {
379: $info{'lti.login'} = $env{'request.lti.login'};
380: }
1.40 raeburn 381: if ($env{'request.lti.uri'}) {
382: $info{'lti.uri'} = $env{'request.lti.uri'};
383: }
1.39 raeburn 384: if ($env{'request.lti.reqcrs'}) {
385: $info{'lti.reqcrs'} = $env{'request.lti.reqcrs'};
386: }
387: if ($env{'request.lti.reqrole'}) {
388: $info{'lti.reqrole'} = $env{'request.lti.reqrole'};
389: }
390: if ($env{'request.lti.selfenrollrole'}) {
391: $info{'lti.selfenrollrole'} = $env{'request.lti.selfenrollrole'};
392: }
393: if ($env{'request.lti.sourcecrs'}) {
394: $info{'lti.sourcecrs'} = $env{'request.lti.sourcecrs'};
395: }
1.38 raeburn 396: if ($env{'request.lti.passbackid'}) {
397: $info{'lti.passbackid'} = $env{'request.lti.passbackid'};
398: }
399: if ($env{'request.lti.passbackurl'}) {
400: $info{'lti.passbackurl'} = $env{'request.lti.passbackurl'};
401: }
402: if ($env{'request.lti.rosterid'}) {
403: $info{'lti.rosterid'} = $env{'request.lti.rosterid'};
404: }
405: if ($env{'request.lti.rosterurl'}) {
406: $info{'lti.rosterurl'} = $env{'request.lti.rosterurl'};
407: }
1.40 raeburn 408: if ($env{'request.lti.target'}) {
409: $info{'lti.target'} = $env{'request.lti.target'};
410: }
1.3 albertel 411: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.62 raeburn 412: my @args = ("domain=$env{'user.domain'}",
413: "username=$env{'user.name'}",
414: "token=$token");
415: my $url = $protocol.'://'.$switch_to.'/adm/login?';
416: if ($env{'form.lcssowin'}) {
417: $url .= join('&',@args);
418: $only_body = 1;
419: } else {
420: $url .= join('&',@args);
421: }
1.1 albertel 422: # --------------------------------------------------------------- Screen Output
1.62 raeburn 423: return &do_redirect($r, $url, $only_body, $ssologoutscript, $env{'form.lcssowin'});
1.1 albertel 424: }
425:
426: 1;
427: __END__
428:
429:
430:
431:
432:
433:
434:
435:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>