Annotation of loncom/auth/switchserver.pm, revision 1.35.2.7
1.1 albertel 1: # The LearningOnline Network
2: # Switch Servers Handler
3: #
1.35.2.7! raeburn 4: # $Id: switchserver.pm,v 1.35.2.6 2021/12/12 21:07:21 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 {
57: my ($r,$url,$only_body,$extra_text) = @_;
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.7! raeburn 65: {'redirect' => [$delay,$url,'',1],
1.14 albertel 66: 'only_body' => $only_body,});
67: my $end_page = &Apache::loncommon::end_page();
68: $r->print($start_page.$extra_text.$end_page);
1.30 raeburn 69: unless ($env{'user.name'} eq 'public' && ($env{'user.domain'} eq 'public')) {
70: $r->register_cleanup(\&flush_course_logs);
71: }
1.14 albertel 72: return OK;
1.30 raeburn 73: }
1.14 albertel 74:
1.35.2.1 raeburn 75: sub balancer_cookieid {
76: my ($r,$desthost,$uname,$udom) = @_;
77: my @hosts = &Apache::lonnet::current_machine_ids();
78: my $newcookieid;
79: unless (grep(/^\Q$desthost\E$/,@hosts)) {
80: my $balancedir=$r->dir_config('lonBalanceDir');
81: $newcookieid = &md5_hex(&md5_hex(time.{}.rand().$$));
82: my $cookie = $udom.'_'.$uname.'_'.$newcookieid;
83: my $balcookie = "balanceID=$cookie; path=/; HttpOnly;";
84: if (open(my $fh,'>',"$balancedir/$cookie.id")) {
85: print $fh $desthost;
86: close($fh);
87: $r->headers_out->add('Set-cookie' => $balcookie);
88: }
89: }
90: return $newcookieid;
91: }
92:
1.30 raeburn 93: sub flush_course_logs {
94: &Apache::lonnet::flushcourselogs();
95: return OK;
1.14 albertel 96: }
97:
1.1 albertel 98: sub handler {
1.5 albertel 99: my ($r) = @_;
1.1 albertel 100:
101: my $handle=&init_env($r);
102: if (!defined($handle)) { return FORBIDDEN; }
103:
104: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.35.2.7! raeburn 105: ['otherserver','role','origurl','symb','edit']);
1.5 albertel 106:
1.13 albertel 107: my $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.17 albertel 108: if (! $env{'form.otherserver'}) {
109: $env{'form.otherserver'} =
110: &Apache::lonnet::find_existing_session($env{'user.domain'},
111: $env{'user.name'});
112: if (! $env{'form.otherserver'}) {
113: $env{'form.otherserver'} =
1.35.2.6 raeburn 114: &Apache::lonnet::spareserver($r,30000,undef,1);
1.17 albertel 115: }
1.13 albertel 116: $switch_to=&Apache::lonnet::hostname($env{'form.otherserver'});
1.4 albertel 117: }
1.5 albertel 118:
1.1 albertel 119: if (!defined($switch_to)) { return FORBIDDEN; }
1.4 albertel 120:
1.33 raeburn 121: my $protocol = 'http';
122: if ($env{'form.otherserver'}) {
123: if ($Apache::lonnet::protocol{$env{'form.otherserver'}} eq 'https') {
124: $protocol = $Apache::lonnet::protocol{$env{'form.otherserver'}};
125: }
1.35.2.6 raeburn 126: unless ($env{'form.otherserver'} eq $r->dir_config('lonHostID')) {
127: my $alias = &Apache::lonnet::use_proxy_alias($r,$env{'form.otherserver'});
128: $switch_to = $alias if ($alias ne '');
129: }
1.35.2.1 raeburn 130: }
1.33 raeburn 131:
1.14 albertel 132: if ($env{'user.name'} eq 'public'
133: && $env{'user.domain'} eq 'public') {
1.33 raeburn 134: my $url = $protocol.'://'.$switch_to.$r->uri;
1.35.2.2 raeburn 135: unlink($handle);
136: #expire the cookie
137: my $c = new CGI::Cookie(-name => 'lonPubID',
138: -value => '',
139: -expires => '-10y',);
140: $r->header_out('Set-cookie' => $c);
1.14 albertel 141: return &do_redirect($r,$url,1)
142: }
143:
1.28 raeburn 144: my $skip_canhost_check = '';
1.35.2.1 raeburn 145: my $now = time;
1.28 raeburn 146: if ($env{'form.role'}) {
147: if (!exists($env{'user.role.'.$env{'form.role'}})) {
1.35 raeburn 148: delete($env{'form.role'});
1.28 raeburn 149: } else {
150: my ($start,$end) = split(/\./,$env{'user.role.'.$env{'form.role'}});
151: if (($start && $start > $now) || ($end && $end < $now)) {
1.35 raeburn 152: delete($env{'form.role'});
1.28 raeburn 153: } elsif ($env{'form.role'} eq 'au./'.$env{'user.domain'}.'/') {
154: if (&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'}) eq $env{'form.otherserver'}) {
155: $skip_canhost_check = 1;
156: }
1.31 raeburn 157: } elsif ($env{'form.role'} =~ m{^[ac]a\./($match_domain)/($match_username)$}) {
1.28 raeburn 158: if (&Apache::lonnet::homeserver($2,$1) eq $env{'form.otherserver'}) {
159: $skip_canhost_check = 1;
160: }
161: }
162: }
163: }
164:
165: unless ($skip_canhost_check) {
1.35.2.7! raeburn 166: my $canhost = &Apache::lonnet::can_switchserver($env{'user.domain'},$env{'form.otherserver'});
! 167: unless ($canhost) {
! 168: if (($env{'request.course.id'}) && ($env{'form.symb'} ne '') &&
! 169: (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
! 170: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
! 171: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
! 172: if (($cdom ne '') && ($cnum ne '') && ($env{'form.role'} =~ m{^([^.]+)\Q./$cdom/$cnum\E$})) {
! 173: my $symb = &Apache::lonnet::symbclean($env{'form.symb'});
! 174: my ($map,$idx,$url) = &Apache::lonnet::decode_symb($symb);
! 175: if (&Apache::lonnet::symbverify($symb,$url)) {
! 176: my $fileloc = &Apache::lonnet::declutter(&Apache::lonnet::filelocation("",$url));
! 177: my $resurl = &Apache::lonnet::clutter($url);
! 178: if ($resurl =~ m{^/res/($match_domain)/($match_username)/}) {
! 179: my ($audom,$auname) = ($1,$2);
! 180: if (&Apache::lonnet::homeserver($auname,$audom) eq $env{'form.otherserver'}) {
! 181: my @possroles = ("user.role.au./$audom/","user.role.ca./$audom/$auname","user.role.aa./$audom/$auname");
! 182: my $hasrole;
! 183: foreach my $rolekey (@possroles) {
! 184: if (exists($env{$rolekey})) {
! 185: my ($start,$end) = split(/\./,$env{$rolekey});
! 186: unless (($start && $start > $now) || ($end && $end < $now)) {
! 187: $hasrole = $rolekey;
! 188: }
! 189: if ($hasrole) {
! 190: $hasrole =~ s/^\Quser.role.\E//;
! 191: last;
! 192: }
! 193: }
! 194: }
! 195: if ($hasrole) {
! 196: $env{'form.role'} = $hasrole;
! 197: $env{'form.origurl'} = &Apache::lonnet::deversion($resurl);
! 198: $env{'form.origurl'} =~ s{^/res/}{/priv/};
! 199: delete($env{'form.symb'});
! 200: $canhost = 1;
! 201: if ($env{'form.edit'}) {
! 202: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
! 203: my %info=('ip' => $ip,
! 204: 'domain' => $env{'user.domain'},
! 205: 'username' => $env{'user.name'},
! 206: 'home' => $env{'user.home'},
! 207: 'role' => $env{'form.role'},
! 208: 'server' => $r->dir_config('lonHostID'),
! 209: 'origurl' => $env{'form.origurl'});
! 210: &Apache::loncommon::content_type($r,'text/html');
! 211: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
! 212: my $url = $protocol.'://'.$switch_to.'/adm/login?'.
! 213: 'domain='.$env{'user.domain'}.
! 214: '&username='.$env{'user.name'}.
! 215: '&token='.$token;
! 216: return &do_redirect($r,$url,0);
! 217: }
! 218: }
! 219: }
! 220: }
! 221: }
! 222: }
! 223: }
1.28 raeburn 224: }
225: unless ($canhost) { return FORBIDDEN; }
1.26 raeburn 226: }
227:
1.1 albertel 228: #remove session env, and log event
1.35.2.2 raeburn 229: if (unlink($handle)) {
230: if ($env{'user.linkedenv'} ne '') {
231: my $lonidsdir=$r->dir_config('lonIDsDir');
232: if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
233: (-l "$lonidsdir/$env{'user.linkedenv'}.id") &&
234: (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq $handle)) {
235: unlink("$lonidsdir/$env{'user.linkedenv'}.id");
236: }
237: }
238: }
1.35.2.1 raeburn 239: my %temp=('switchserver' => $now.':'.$env{'form.otherserver'},
1.1 albertel 240: $env{'form.role'});
241: &Apache::lonnet::put('email_status',\%temp);
1.34 raeburn 242: my $logmsg = "Switch Server to $env{'form.otherserver'}";
243: if ($env{'form.role'}) {
244: $logmsg .= " with role: $env{'form.role'}";
245: } else {
246: $logmsg .= " (no role)";
247: }
1.35.2.6 raeburn 248: my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP);
1.35.2.5 raeburn 249: $logmsg .= ' '.$ip;
1.1 albertel 250: &Apache::lonnet::log($env{'user.domain'},$env{'user.name'},
1.34 raeburn 251: $env{'user.home'},$logmsg);
1.1 albertel 252:
253: &Apache::loncommon::content_type($r,'text/html');
1.12 albertel 254:
1.35.2.2 raeburn 255: #expire the cookies
256: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
257: foreach my $name (keys(%cookies)) {
258: next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
259: my $c = new CGI::Cookie(-name => $name,
260: -value => '',
261: -expires => '-10y',);
262: $r->headers_out->add('Set-cookie' => $c);
263: }
1.20 albertel 264: if ($r->header_only) {
265: $r->send_http_header;
266: return OK;
267: }
1.1 albertel 268: # -------------------------------------------------------- Menu script and info
1.35.2.3 raeburn 269:
1.1 albertel 270: # ---------------------------------------------------------------- Get handover
271:
1.35.2.4 raeburn 272: my ($is_balancer,$setcookie,$newcookieid,$otherbalcookie);
1.35.2.1 raeburn 273: my $only_body = 0;
1.35.2.3 raeburn 274: ($is_balancer,undef,$setcookie) =
275: &Apache::lonnet::check_loadbalancing($env{'user.name'},$env{'user.domain'},'switchserver');
1.35.2.1 raeburn 276: if ($is_balancer && $setcookie && $env{'form.otherserver'}) {
1.35.2.3 raeburn 277:
1.35.2.1 raeburn 278: # Set a balancer cookie unless browser already sent LON-CAPA load balancer
279: # cookie which points at the target server
280: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
281:
282: if (($found_server eq $env{'form.otherserver'}) &&
283: ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
284: $only_body = 1;
285: } else {
286: $newcookieid = &balancer_cookieid($r,$env{'form.otherserver'},
287: $env{'user.name'},$env{'user.domain'});
288: }
289: }
1.35.2.3 raeburn 290: if ((!$is_balancer) && ($env{'request.balancercookie'})) {
291: $otherbalcookie = $env{'request.balancercookie'};
292: }
1.35.2.1 raeburn 293:
1.35.2.5 raeburn 294: my %info=('ip' => $ip,
1.3 albertel 295: 'domain' => $env{'user.domain'},
296: 'username' => $env{'user.name'},
297: 'role' => $env{'form.role'},
1.32 raeburn 298: 'server' => $r->dir_config('lonHostID'),
299: 'balancer' => $is_balancer);
1.35.2.1 raeburn 300: if ($newcookieid) {
301: $info{'balcookie'} = $newcookieid;
1.35.2.3 raeburn 302: } elsif ($otherbalcookie) {
303: $info{'otherbalcookie'} = $otherbalcookie;
1.35.2.1 raeburn 304: }
1.22 raeburn 305: if ($env{'form.origurl'}) {
306: $info{'origurl'} = $env{'form.origurl'};
307: }
308: if ($env{'form.symb'}) {
309: $info{'symb'} = $env{'form.symb'};
310: }
1.35.2.6 raeburn 311: my $ssologoutscript = '';
1.8 albertel 312: if ($env{'request.sso.login'}) {
313: $info{'sso.login'} = $env{'request.sso.login'};
1.35.2.6 raeburn 314: if (defined($r->dir_config("lonSSOUserLogoutScriptFile_$info{domain}"))) {
315: if (open(my $fh,'<',$r->dir_config("lonSSOUserLogoutScriptFile_$info{domain}"))) {
316: $ssologoutscript .= join('',<$fh>);
317: close($fh);
318: }
319: }
320: if (defined($r->dir_config('lonSSOUserLogoutScriptFile'))) {
321: if (open(my $fh,'<',$r->dir_config('lonSSOUserLogoutScriptFile'))) {
322: $ssologoutscript .= join('',<$fh>);
323: close($fh);
324: }
325: }
1.8 albertel 326: }
1.11 raeburn 327: if ($env{'request.sso.reloginserver'}) {
328: $info{'sso.reloginserver'} = $env{'request.sso.reloginserver'};
329: }
1.3 albertel 330: my $token = &Apache::lonnet::tmpput(\%info,$env{'form.otherserver'});
1.33 raeburn 331: my $url =$protocol.'://'.$switch_to.'/adm/login?'.
1.14 albertel 332: 'domain='.$env{'user.domain'}.
1.21 albertel 333: '&username='.$env{'user.name'}.
334: '&token='.$token;
1.1 albertel 335: # --------------------------------------------------------------- Screen Output
1.35.2.6 raeburn 336: return &do_redirect($r, $url, $only_body, $ssologoutscript);
1.1 albertel 337: }
338:
339: 1;
340: __END__
341:
342:
343:
344:
345:
346:
347:
348:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>