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