Annotation of loncom/auth/switchserver.pm, revision 1.35.2.6.2.7
1.1 albertel 1: # The LearningOnline Network
2: # Switch Servers Handler
3: #
1.35.2.6.2.7! raeburn 4: # $Id: switchserver.pm,v 1.35.2.6.2.6 2023/07/05 21:13:07 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.35.2.6 raeburn 32: use Apache::Constants qw(:common :remotehost);
1.1 albertel 33: use Apache::lonnet;
1.35.2.1 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.35.2.1 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.35.2.6.2.3 raeburn 57: my ($r,$url,$only_body,$extra_text,$write_to_opener) = @_;
1.14 albertel 58: $r->send_http_header;
1.35.2.1 raeburn 59: my $delay = 0.5;
1.35.2.6 raeburn 60: if ($only_body && !$extra_text) {
1.35.2.1 raeburn 61: $delay = 0;
62: }
1.14 albertel 63: my $start_page =
1.16 raeburn 64: &Apache::loncommon::start_page('Switching Server ...',undef,
1.35.2.6.2.4 raeburn 65: {'redirect' => [$delay,$url,'',$write_to_opener,1],
1.35.2.6.2.7! raeburn 66: 'only_body' => $only_body,
! 67: 'switchserver' => 1,
! 68: 'links_disabled' => 1,
! 69: 'no_inline_link' => 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.35.2.1 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.35.2.6.2.2 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.35.2.6.2.4 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.35.2.6 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.35.2.6 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.35.2.1 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.35.2.2 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.35.2.1 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.28 raeburn 161: if (&Apache::lonnet::homeserver($2,$1) eq $env{'form.otherserver'}) {
162: $skip_canhost_check = 1;
163: }
164: }
165: }
166: }
167:
168: unless ($skip_canhost_check) {
1.35.2.6.2.4 raeburn 169: my $canhost = &Apache::lonnet::can_switchserver($env{'user.domain'},$env{'form.otherserver'});
170: unless ($canhost) {
171: if (($env{'request.course.id'}) && ($env{'form.symb'} ne '') &&
172: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
173: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
174: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
175: if (($cdom ne '') && ($cnum ne '') && ($env{'form.role'} =~ m{^([^.]+)\Q./$cdom/$cnum\E$})) {
176: my $symb = &Apache::lonnet::symbclean($env{'form.symb'});
177: my ($map,$idx,$url) = &Apache::lonnet::decode_symb($symb);
178: if (&Apache::lonnet::symbverify($symb,$url)) {
179: my $fileloc = &Apache::lonnet::declutter(&Apache::lonnet::filelocation("",$url));
180: my $resurl = &Apache::lonnet::clutter($url);
181: if ($resurl =~ m{^/res/($match_domain)/($match_username)/}) {
182: my ($audom,$auname) = ($1,$2);
183: if (&Apache::lonnet::homeserver($auname,$audom) eq $env{'form.otherserver'}) {
184: my @possroles = ("user.role.au./$audom/","user.role.ca./$audom/$auname","user.role.aa./$audom/$auname");
185: my $hasrole;
186: foreach my $rolekey (@possroles) {
187: if (exists($env{$rolekey})) {
188: my ($start,$end) = split(/\./,$env{$rolekey});
189: unless (($start && $start > $now) || ($end && $end < $now)) {
190: $hasrole = $rolekey;
191: }
192: if ($hasrole) {
193: $hasrole =~ s/^\Quser.role.\E//;
194: last;
195: }
196: }
197: }
198: if ($hasrole) {
199: $env{'form.role'} = $hasrole;
200: $env{'form.origurl'} = &Apache::lonnet::deversion($resurl);
201: $env{'form.origurl'} =~ s{^/res/}{/priv/};
202: delete($env{'form.symb'});
203: $canhost = 1;
204: if ($env{'form.edit'}) {
205: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
206: my %info=('ip' => $ip,
207: 'domain' => $env{'user.domain'},
208: 'username' => $env{'user.name'},
209: 'home' => $env{'user.home'},
210: 'role' => $env{'form.role'},
211: 'server' => $r->dir_config('lonHostID'),
212: 'origurl' => $env{'form.origurl'});
213: &Apache::loncommon::content_type($r,'text/html');
214: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
215: my $url = $protocol.'://'.$switch_to.'/adm/login?'.
216: 'domain='.$env{'user.domain'}.
217: '&username='.$env{'user.name'}.
218: '&token='.$token;
219: return &do_redirect($r,$url,0);
220: }
221: }
222: }
223: }
224: }
225: }
226: }
1.28 raeburn 227: }
228: unless ($canhost) { return FORBIDDEN; }
1.26 raeburn 229: }
230:
1.1 albertel 231: #remove session env, and log event
1.35.2.2 raeburn 232: if (unlink($handle)) {
233: if ($env{'user.linkedenv'} ne '') {
234: my $lonidsdir=$r->dir_config('lonIDsDir');
235: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
236: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
237: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq $handle)) {
238: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
239: }
240: }
241: }
1.35.2.1 raeburn 242: my %temp=('switchserver' => $now.':'.$env{'form.otherserver'},
1.1 albertel 243: $env{'form.role'});
244: &Apache::lonnet::put('email_status',\%temp);
1.34 raeburn 245: my $logmsg = "Switch Server to $env{'form.otherserver'}";
246: if ($env{'form.role'}) {
247: $logmsg .= " with role: $env{'form.role'}";
1.35.2.6.2.6 raeburn 248: } elsif (($env{'form.lti.reqcrs'}) && ($env{'form.lti.reqrole'} eq 'cc')) {
249: $logmsg .= " to create new LTI course";
250: } elsif ($env{'form.lti.selfenrollrole'}) {
251: $logmsg .= " to selfenroll with role: $env{'form.lti.selfenrollrole'}";
1.34 raeburn 252: } else {
253: $logmsg .= " (no role)";
254: }
1.35.2.6 raeburn 255: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.35.2.5 raeburn 256: $logmsg .= ' '.$ip;
1.1 albertel 257: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
1.34 raeburn 258: $env{'user.home'},$logmsg);
1.1 albertel 259:
260: &Apache::loncommon::content_type($r,'text/html');
1.12 albertel 261:
1.35.2.2 raeburn 262: #expire the cookies
263: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
264: foreach my $name (keys(%cookies)) {
265: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
266: my $c = new CGI::Cookie(-name => $name,
267: -value => '',
268: -expires => '-10y',);
269: $r->headers_out->add('Set-cookie' => $c);
270: }
1.20 albertel 271: if ($r->header_only) {
272: $r->send_http_header;
273: return OK;
274: }
1.1 albertel 275: # -------------------------------------------------------- Menu script and info
1.35.2.3 raeburn 276:
1.1 albertel 277: # ---------------------------------------------------------------- Get handover
278:
1.35.2.4 raeburn 279: my ($is_balancer,$setcookie,$newcookieid,$otherbalcookie);
1.35.2.1 raeburn 280: my $only_body = 0;
1.35.2.3 raeburn 281: ($is_balancer,undef,$setcookie) =
282: &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'},'switchserver');
1.35.2.1 raeburn 283: if ($is_balancer && $setcookie && $env{'form.otherserver'}) {
1.35.2.3 raeburn 284:
1.35.2.1 raeburn 285: # Set a balancer cookie unless browser already sent LON-CAPA load balancer
286: # cookie which points at the target server
287: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
288:
289: if (($found_server eq $env{'form.otherserver'}) &&
290: ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
291: $only_body = 1;
292: } else {
293: $newcookieid = &balancer_cookieid($r,$env{'form.otherserver'},
294: $env{'user.name'},$env{'user.domain'});
295: }
296: }
1.35.2.3 raeburn 297: if ((!$is_balancer) && ($env{'request.balancercookie'})) {
298: $otherbalcookie = $env{'request.balancercookie'};
299: }
1.35.2.1 raeburn 300:
1.35.2.5 raeburn 301: my %info=('ip' => $ip,
1.3 albertel 302: 'domain' => $env{'user.domain'},
303: 'username' => $env{'user.name'},
304: 'role' => $env{'form.role'},
1.32 raeburn 305: 'server' => $r->dir_config('lonHostID'),
306: 'balancer' => $is_balancer);
1.35.2.1 raeburn 307: if ($newcookieid) {
308: $info{'balcookie'} = $newcookieid;
1.35.2.3 raeburn 309: } elsif ($otherbalcookie) {
310: $info{'otherbalcookie'} = $otherbalcookie;
1.35.2.1 raeburn 311: }
1.22 raeburn 312: if ($env{'form.origurl'}) {
313: $info{'origurl'} = $env{'form.origurl'};
1.35.2.6.2.5 raeburn 314: if ($env{'form.origurl'} eq '/adm/email') {
315: if ($env{'request.display'} && ($env{'request.mailrecip'} eq "$env{'user.name'}:$env{'user.domain'}")) {
316: $info{'display'} = &escape($env{'request.display'});
317: $info{'mailrecip'} = &escape($env{'request.mailrecip'});
318: }
319: }
1.22 raeburn 320: }
321: if ($env{'form.symb'}) {
322: $info{'symb'} = $env{'form.symb'};
323: }
1.35.2.6.2.3 raeburn 324: my $ssologoutscript = '';
325: my $write_to_opener;
1.8 albertel 326: if ($env{'request.sso.login'}) {
327: $info{'sso.login'} = $env{'request.sso.login'};
1.35.2.6 raeburn 328: if (defined($r->dir_config("lonSSOUserLogoutScriptFile_$info{domain}"))) {
329: if (open(my $fh,'<',$r->dir_config("lonSSOUserLogoutScriptFile_$info{domain}"))) {
330: $ssologoutscript .= join('',<$fh>);
331: close($fh);
332: }
333: }
334: if (defined($r->dir_config('lonSSOUserLogoutScriptFile'))) {
335: if (open(my $fh,'<',$r->dir_config('lonSSOUserLogoutScriptFile'))) {
336: $ssologoutscript .= join('',<$fh>);
337: close($fh);
338: }
339: }
1.8 albertel 340: }
1.11 raeburn 341: if ($env{'request.sso.reloginserver'}) {
342: $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
343: }
1.35.2.6.2.1 raeburn 344: if ($env{'request.linkprot'}) {
345: $info{'linkprot'} = $env{'request.linkprot'};
1.35.2.6.2.6 raeburn 346: foreach my $item ('linkprotuser','linkprotexit','linkprotpbid','linkprotpburl') {
1.35.2.6.2.2 raeburn 347: if ($env{'request.'.$item}) {
348: $info{$item} = $env{'request.'.$item};
349: }
350: }
1.35.2.6.2.1 raeburn 351: } elsif ($env{'request.linkkey'} ne '') {
352: $info{'linkkey'} = $env{'request.linkkey'};
353: }
354: if ($env{'request.deeplink.login'}) {
355: $info{'deeplink.login'} = $env{'request.deeplink.login'};
356: }
1.35.2.6.2.6 raeburn 357: if ($env{'request.lti.login'}) {
358: $info{'lti.login'} = $env{'request.lti.login'};
359: }
360: if ($env{'request.lti.uri'}) {
361: $info{'lti.uri'} = $env{'request.lti.uri'};
362: }
363: if ($env{'request.lti.reqcrs'}) {
364: $info{'lti.reqcrs'} = $env{'request.lti.reqcrs'};
365: }
366: if ($env{'request.lti.reqrole'}) {
367: $info{'lti.reqrole'} = $env{'request.lti.reqrole'};
368: }
369: if ($env{'request.lti.selfenrollrole'}) {
370: $info{'lti.selfenrollrole'} = $env{'request.lti.selfenrollrole'};
371: }
372: if ($env{'request.lti.sourcecrs'}) {
373: $info{'lti.sourcecrs'} = $env{'request.lti.sourcecrs'};
374: }
375: if ($env{'request.lti.passbackid'}) {
376: $info{'lti.passbackid'} = $env{'request.lti.passbackid'};
377: }
378: if ($env{'request.lti.passbackurl'}) {
379: $info{'lti.passbackurl'} = $env{'request.lti.passbackurl'};
380: }
381: if ($env{'request.lti.rosterid'}) {
382: $info{'lti.rosterid'} = $env{'request.lti.rosterid'};
383: }
384: if ($env{'request.lti.rosterurl'}) {
385: $info{'lti.rosterurl'} = $env{'request.lti.rosterurl'};
386: }
387: if ($env{'request.lti.target'}) {
388: $info{'lti.target'} = $env{'request.lti.target'};
389: }
1.3 albertel 390: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.35.2.6.2.3 raeburn 391: my @args = ("domain=$env{'user.domain'}",
392: "username=$env{'user.name'}",
393: "token=$token");
394: my $url = $protocol.'://'.$switch_to.'/adm/login?';
395: if ($env{'form.lcssowin'}) {
396: $url .= join('&',@args);
397: $only_body = 1;
398: } else {
399: $url .= join('&',@args);
400: }
1.1 albertel 401: # --------------------------------------------------------------- Screen Output
1.35.2.6.2.3 raeburn 402: return &do_redirect($r, $url, $only_body, $ssologoutscript, $env{'form.lcssowin'});
1.1 albertel 403: }
404:
405: 1;
406: __END__
407:
408:
409:
410:
411:
412:
413:
414:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>