--- loncom/auth/lonlogin.pm 2018/12/02 14:55:59 1.175 +++ loncom/auth/lonlogin.pm 2018/12/05 03:29:05 1.176 @@ -1,7 +1,7 @@ # The LearningOnline Network # Login Screen # -# $Id: lonlogin.pm,v 1.175 2018/12/02 14:55:59 raeburn Exp $ +# $Id: lonlogin.pm,v 1.176 2018/12/05 03:29:05 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -37,7 +37,7 @@ use Apache::lonauth(); use Apache::lonlocal; use Apache::migrateuser(); use lib '/home/httpd/lib/perl/'; -use LONCAPA; +use LONCAPA qw(:DEFAULT :match); use CGI::Cookie(); sub handler { @@ -47,7 +47,7 @@ sub handler { (join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'}, $ENV{'REDIRECT_QUERY_STRING'}), ['interface','username','domain','firsturl','localpath','localres', - 'token','role','symb','iptoken']); + 'token','role','symb','iptoken','btoken']); if (!defined($env{'form.firsturl'})) { &Apache::lonacc::get_posted_cgi($r,['firsturl']); } @@ -64,10 +64,10 @@ sub handler { # For "public user" - remove any exising "public" cookie, as user really wants to log-in my ($handle,$lonidsdir,$expirepub,$userdom); + $lonidsdir=$r->dir_config('lonIDsDir'); unless ($r->header_only) { $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom); if ($handle ne '') { - $lonidsdir=$r->dir_config('lonIDsDir'); if ($handle=~/^publicuser\_/) { unlink($r->dir_config('lonIDsDir')."/$handle.id"); undef($handle); @@ -106,6 +106,7 @@ sub handler { return OK; } + my $lonhost = $r->dir_config('lonHostID'); $env{'form.firsturl'} =~ s/(`)/'/g; # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer) @@ -120,6 +121,13 @@ sub handler { if ($env{'form.firsturl'} ne '') { $dest = $env{'form.firsturl'}; } + my %info = ( + balcookie => $lonhost.':'.$balancer_cookie, + ); + my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server); + if ($balancer_token) { + $dest .= (($dest=~/\?/)?'&;':'?') . 'btoken='.$balancer_token; + } my $url = $protocol.'://'.$hostname.$dest; my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef, @@ -131,6 +139,19 @@ sub handler { } # +# Check if a LON-CAPA load balancer sent user here because user's browser sent +# it a balancer cookie for an active session on this server. +# + + my $balcookie; + if ($env{'form.btoken'}) { + my %info = &Apache::lonnet::tmpget($env{'form.btoken'}); + $balcookie = $info{'balcookie'}; + &Apache::lonnet::tmpdel($env{'form.btoken'}); + delete($env{'form.btoken'}); + } + +# # If browser sent an old cookie for which the session file had been removed # check if configuration for user's domain has a portal URL set. If so # switch user's log-in to the portal. @@ -156,7 +177,7 @@ sub handler { &Apache::loncommon::end_page(); my $dest = '/adm/roles'; if ($env{'form.firsturl'} ne '') { - $dest = $env{'form.firsturl'}; + $dest = $env{'form.firsturl'}; } $r->print( $start_page @@ -194,12 +215,11 @@ sub handler { my $iconpath= &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL')); - my $lonhost = $r->dir_config('lonHostID'); my $domain = &Apache::lonnet::default_login_domain(); my $defdom = $domain; if ($lonhost ne '') { unless ($sessiondata{'sessionserver'}) { - my $redirect = &check_loginvia($domain,$lonhost); + my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie); if ($redirect) { $r->print($redirect); return OK; @@ -707,8 +727,8 @@ ENDDOCUMENT } sub check_loginvia { - my ($domain,$lonhost) = @_; - if ($domain eq '' || $lonhost eq '') { + my ($domain,$lonhost,$lonidsdir,$balcookie) = @_; + if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') { return; } my %domconfhash = &Apache::loncommon::get_domainconf($domain); @@ -737,6 +757,36 @@ sub check_loginvia { } if ($newhost ne $lonhost) { if (&Apache::lonnet::hostname($newhost) ne '') { + if ($balcookie) { + my ($balancer,$cookie) = split(/:/,$balcookie); + if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) { + my ($udom,$uname,$cookieid) = ($1,$2,$3); + unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') { + if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) { + while (my $filename=readdir($dh)) { + if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) { + my $handle = $1; + my %hash = + &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir, + ['request.balancercookie', + 'user.linkedenv']); + if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") { + if (unlink("$lonidsdir/$filename")) { + if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) && + (-l "$lonidsdir/$hash{'user.linkedenv'}.id") && + (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) { + unlink("$lonidsdir/$hash{'user.linkedenv'}.id"); + } + } + } + last; + } + } + closedir($dh); + } + } + } + } $output = &redirect_page($newhost,$path); } }