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