--- loncom/auth/migrateuser.pm 2005/10/28 21:51:43 1.3 +++ loncom/auth/migrateuser.pm 2023/01/23 00:59:22 1.25.2.5.2.8 @@ -1,7 +1,7 @@ # The LearningOnline Network # Starts a user off based of an existing token. # -# $Id: migrateuser.pm,v 1.3 2005/10/28 21:51:43 albertel Exp $ +# $Id: migrateuser.pm,v 1.25.2.5.2.8 2023/01/23 00:59:22 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -29,62 +29,592 @@ package Apache::migrateuser; use strict; -use Apache::Constants qw(:common :http :methods); +use LONCAPA qw(:DEFAULT :match); +use Apache::Constants qw(:common :http :methods :remotehost); use Apache::lonauth; use Apache::lonnet; +use Apache::lonlocal; +use Apache::lonlogin(); +use CGI::Cookie; sub goto_login { - my ($r) = @_; + my ($r,$domain,$data) = @_; + my $write_to_opener; + if (ref($data) eq 'HASH') { + $write_to_opener = $data->{'lcssowin'}; + } &Apache::loncommon::content_type($r,'text/html'); $r->send_http_header; - $r->print(< - - - Going to login - - -

One moment please...

-

- Transferring to login page. - Continue -

- - -TOLOGIN + my $url = '/adm/login'; + if ($domain) { + $url .= '?domain='.$domain; + } + $r->print(&Apache::loncommon::start_page('Going to login',undef, + {'redirect' => [0,$url,'',$write_to_opener],}). + '

'.&mt('One moment please...').'

'. + '

'.&mt('Transferring to login page.').'

'. + &Apache::loncommon::end_page()); return OK; } +sub sso_check { + my ($data) = @_; + my %extra_env; + if (ref($data) eq 'HASH') { + if ($data->{'sso.login'}) { + $extra_env{'request.sso.login'} = $data->{'sso.login'}; + } + if ($data->{'sso.reloginserver'}) { + $extra_env{'request.sso.reloginserver'} = + $data->{'sso.reloginserver'}; + } + } + return \%extra_env; +} + +sub ip_changed { + my ($r,$udom,$camefrom,$dataref) = @_; + &Apache::loncommon::content_type($r,'text/html'); + $r->send_http_header; + if (ref($dataref) eq 'HASH') { + my $title = 'LON-CAPA Session redirected'; + my $message = &mt('Your internet address has changed since you logged in.'); + my $rule_in_effect; + if ($dataref->{'balancer'}) { + my $baldom = &Apache::lonnet::host_domain($camefrom); + my $balprimaryid = &Apache::lonnet::domain($baldom,'primary'); + my $balintdom = &Apache::lonnet::internet_dom($balprimaryid); + my $uprimaryid = &Apache::lonnet::domain($udom,'primary'); + my $uintdom = &Apache::lonnet::internet_dom($uprimaryid); + my $dom_in_use; + if (($uintdom ne '') && ($uintdom eq $balintdom)) { + $dom_in_use = $udom; + } else { + $dom_in_use = $baldom; + } + my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use); + unless (defined($cached)) { + my $cachetime = 60*60*24; + my %domconfig = + &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use); + if (ref($domconfig{'loadbalancing'}) eq 'HASH') { + $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use, + $domconfig{'loadbalancing'},$cachetime); + } + } + if (ref($result) eq 'HASH') { + (undef,my $currtargets,my $currrules) = + &Apache::lonnet::check_balancer_result($result,$dataref->{'server'}); + if (ref($currrules) eq 'HASH') { + if ($dataref->{'sso.login'}) { + if ($currrules->{'_LC_ipchangesso'} ne '') { + $rule_in_effect = $currrules->{'_LC_ipchangesso'}; + } + } else { + if ($currrules->{'_LC_ipchange'} ne '') { + $rule_in_effect = $currrules->{'_LC_ipchange'}; + } + } + } + } + } + my $url; + my $lonhost= $r->dir_config('lonHostID'); + my $switchto = $lonhost; + if ($rule_in_effect ne 'offloadedto') { + my $hosthere; + my @ids=&Apache::lonnet::current_machine_ids(); + unless ($rule_in_effect eq 'balancer') { + if (grep(/^\Q$rule_in_effect\E$/,@ids)) { + $hosthere = 1; + } + } + unless ($hosthere) { + if ($dataref->{'role'}) { + my ($adom,$aname); + if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) { + $adom = $1; + $aname = $dataref->{'username'}; + } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) { + $adom = $1; + $aname = $2; + } + if ($adom ne '' && $aname ne '') { + my $ahome = &Apache::lonnet::homeserver($aname,$adom); + unless ($ahome eq 'no_host') { + if ($ahome && grep(/^\Q$ahome\E$/,@ids)) { + $hosthere = 1; + } + } + } + } + } + unless ($hosthere) { + my $hostname; + if ($rule_in_effect eq 'balancer') { + $hostname = &Apache::lonnet::hostname($dataref->{'server'}); + if ($hostname) { + $switchto = $dataref->{'server'}; + } + } else { + $hostname = &Apache::lonnet::hostname($rule_in_effect); + if ($hostname) { + $switchto = $rule_in_effect; + } + } + if ($hostname) { + my $protocol = $Apache::lonnet::protocol{$switchto}; + $protocol = 'http' if ($protocol ne 'https'); + my $alias = &Apache::lonnet::use_proxy_alias($r,$switchto); + $hostname = $alias if ($alias ne ''); + $url = $protocol.'://'.$hostname; + if ($rule_in_effect eq 'balancer') { + $message .= '
'. + &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.'); + } else { + $message .= '
'. + &mt('As a result, your LON-CAPA session is being redirected.'); + } + } + } + unless ($hosthere) { + if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) { + &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'}); + } + } + } + if ($dataref->{'sso.login'}) { + $url .= '/adm/roles'; + } else { + $url .= '/adm/login'; + if ($udom) { + $url .= '?domain='.$udom; + } + $message .= '
'.&mt('You will need to provide your password one more time.'); + } + my %info= ( + 'domain' => $udom, + 'username' => $dataref->{'username'}, + 'sessionserver' => $lonhost, + ); + my @names = ('origurl','symb','role','linkprotuser','linkprotexit','linkprot','linkkey'); + foreach my $name (@names) { + if ($dataref->{$name} ne '') { + $info{$name} = $dataref->{$name}; + } + } + if ($info{'origurl'} eq '/adm/email') { + if (($dataref->{'display'}) && ($dataref->{'mailrecip'})) { + if (&unescape($dataref->{'mailrecip'}) eq $dataref->{'username'}.':'.$dataref->{'domain'}) { + $info{'display'} = $dataref->{'display'}; + $info{'mailrecip'} = $dataref->{'mailrecip'}; + } + } + } + my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto,'link'); + unless ($iptoken eq 'conlost') { + $url .= (($url =~ /\?/) ? '&' : '?') . 'iptoken='.$iptoken; + } + $r->print(&Apache::loncommon::start_page($title,undef, + {'redirect' => + [2,$url,'',$dataref->{'lcssowin'}]}). + '

'.&mt('One moment please...').'

'. + '

'.$message.'

'. + &Apache::loncommon::end_page()); + } else { + return &goto_login($r); + } + return OK; +} + +sub logout { + my ($r,$ip,$handle,$data,$lti_env,$linkprot_info) = @_; + my $lonidsdir=$r->dir_config('lonIDsDir'); + if (unlink("$lonidsdir/$handle.id")) { + if (($env{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) && + (-l "$lonidsdir/$env{'user.linkedenv'}.id") && + (readlink("$lonidsdir/$env{'user.linkedenv'}.id") eq "$lonidsdir/$handle.id")) { + unlink("$lonidsdir/$env{'user.linkedenv'}.id"); + } + } + my %temp=('logout' => time); + &Apache::lonnet::put('email_status',\%temp); + &Apache::lonnet::log($env{'user.domain'}, + $env{'user.name'}, + $env{'user.home'}, + "Logout $ip"); + + &Apache::loncommon::content_type($r,'text/html'); + + #expire the cookies + my %cookies=CGI::Cookie->parse($r->header_in('Cookie')); + foreach my $name (keys(%cookies)) { + next unless ($name =~ /^lon(|S|Link|Pub)ID$/); + my $c = new CGI::Cookie(-name => $name, + -value => '', + -expires => '-10y',); + $r->headers_out->add('Set-cookie' => $c); + } + my $lonhost = $r->dir_config('lonHostID'); + my ($url,$token,$delay,$write_to_opener,%info,%user_info); + if (ref($data) eq 'HASH') { + %user_info=('ip' => $ip, + 'domain' => $data->{'domain'}, + 'username' => $data->{'username'}, + 'home' => $data->{'home'}, + 'role' => $data->{'role'}, + 'origurl' => $data->{'origurl'}, + 'symb' => $data->{'symb'}, + 'server' => $lonhost); + $write_to_opener = $data->{'lcssowin'}; + } + $delay = 0.1; + if (ref($linkprot_info) eq 'HASH') { + if ($linkprot_info->{'linkprotuser'} ne '') { + $token = &Apache::lonnet::tmpput($linkprot_info,$lonhost); + $url = '/adm/login?ltoken='.$token; + } else { + foreach my $key (keys(%user_info)) { + if ($user_info{$key} eq '') { + delete($user_info{$key}); + } + } + %info = (%user_info,%{$linkprot_info}); + $token = &Apache::lonnet::tmpput(\%info,$lonhost); + $url = '/adm/migrateuser?token='.$token; + } + $delay = 0; + } else { + my %lti_info; + if (ref($lti_env) eq 'HASH') { + foreach my $key (sort(keys(%{$lti_env}))) { + if ($key =~ /^request\.(.+)$/) { + $lti_info{$1} = $lti_env->{$key}; + } + } + } + %info = (%user_info,%lti_info); + $token = &Apache::lonnet::tmpput(\%info,$lonhost); + $url = '/adm/migrateuser?token='.$token; + } + $r->send_http_header; + $r->print( + &Apache::loncommon::start_page('Updating Session ...',undef, + {'redirect' => [$delay,$url,'',$write_to_opener], + 'only_body' => 1,}). + &Apache::loncommon::end_page()); + if ($env{'request.balancercookie'}) { + my ($balancer,$cookie) = split(/:/,$env{'request.balancercookie'}); + if ((&Apache::lonnet::hostname($balancer)) && ($cookie =~ /^[a-f0-9]{32}$/)) { + $cookie = $env{'user.domain'}.'_'.$env{'user.name'}.'_'.$cookie; + &Apache::lonnet::delbalcookie($cookie,$balancer); + } + } + $r->register_cleanup(\&flush_course_logs); + return; +} + +sub flush_course_logs { + &Apache::lonnet::flushcourselogs(); + return OK; +} sub handler { my ($r) = @_; - + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']); - my %data = &Apache::lonnet::tmpget($env{'form.token'}); + my %data = &Apache::lonnet::tmpget($env{'form.token'}); + if (keys(%data) == 0) { + return &goto_login($r); + } + my $delete = &Apache::lonnet::tmpdel($env{'form.token'}); - if ($data{'ip'} ne $ENV{'REMOTE_ADDR'} || !defined($data{'username'}) || - !defined($data{'domain'}) ) { - return &goto_login($r); + &Apache::lonlocal::get_language_handle($r); + + if ($delete ne 'ok') { + return &goto_login($r,'',\%data); } - &Apache::lonnet::logthis("Allowing access for $data{'username'}\@$data{'domain'} to $data{'role'}"); + if (!defined($data{'username'}) || !defined($data{'domain'})) { + return &goto_login($r,'',\%data); + } + my $ip = &Apache::lonnet::get_requestor_ip($r,REMOTE_NOLOOKUP); + if ($data{'ip'} ne $ip) { + &Apache::lonnet::logthis('IP change when session migration requested -- was: '. + $data{'ip'}.'; now: '.$ip.' for '.$data{'username'}.':'.$data{'domain'}); + return &ip_changed($r,$data{'domain'},$data{'server'},\%data); + } + + &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}"); my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'}); - if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r); } + my $udom; + if (&Apache::lonnet::domain($data{'domain'})) { + $udom=$data{'domain'}; + } + if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom,\%data); } - if (!$data{'role'}) { - &Apache::lonauth::success($r,$data{'username'},$data{'domain'}, - $home,'/adm/roles'); + my $extra_env = &sso_check(\%data); + if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) { + if (ref($extra_env) eq 'HASH') { + $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'}; + } else { + $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} }; + } + } elsif (($data{'server'}) && ($data{'otherbalcookie'})) { + my ($balancer,$balcookie) = split(/:/,$data{'otherbalcookie'}); + if (defined(&Apache::lonnet::hostname($balancer)) && $balcookie =~ /^[a-f0-9]{32}$/) { + my $baldom = &Apache::lonnet::host_domain($balancer); + if (&Apache::lonnet::shared_institution($baldom)) { + my $cookieid = join('_',$udom,$data{'username'},$balcookie); + &Apache::lonnet::updatebalcookie($cookieid,$balancer,$data{'server'}); + if (ref($extra_env) eq 'HASH') { + $extra_env->{'request.balancercookie'} = $data{'otherbalcookie'}; + } else { + $extra_env = { 'request.balancercookie' => $data{'otherbalcookie'} }; + } + } + } + } + + my (%form,$cid); + if ($data{'symb'} ne '') { + $form{'symb'} = $data{'symb'}; + } + if ($data{'iptoken'} ne '') { + $form{'iptoken'} = $data{'iptoken'}; + } + if ($data{'noloadbalance'} ne '') { + $form{'noloadbalance'} = $data{'noloadbalance'}; + } + if ($data{'role'}) { + if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) { + unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') { + $cid = $1.'_'.$2; + } + } + } else { + my ($handle,$expirepub); + $handle = &Apache::lonnet::check_for_valid_session($r); + +# For "public user" - remove any existing "public" cookie so actual user is logged in. + if ($handle) { + if ($handle=~/^publicuser\_/) { + my $lonidsdir=$r->dir_config('lonIDsDir'); + if ($lonidsdir ne '') { + unlink("$lonidsdir/$handle.id"); + } + undef($handle); + $expirepub = 1; + } + } + if ($handle) { + &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'), + $handle); + my $checklaunch; + if ($data{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) { + unless ($env{'request.linkprot'} eq $data{'linkprot'}) { + $checklaunch = 1; + } + unless ($env{'request.linkprotexit'} eq $data{'linkprotexit'}) { + $checklaunch = 1; + } + unless ($env{'request.linkkey'} eq $data{'linkkey'}) { + $checklaunch = 1; + } + unless ($env{'request.deeplink.login'} eq $data{'deeplink.login'}) { + $checklaunch = 1; + } + } + if ($data{'linkprot'} ne '') { + if (($env{'user.name'} ne $data{'username'}) || + ($env{'user.domain'} ne $data{'domain'})) { + my %linkprot_env; + foreach my $item ('linkprot','linkprotexit','deeplink.login') { + if ($data{$item}) { + $linkprot_env{$item} = $data{$item}; + } + } + &logout($r,$ip,$handle,\%data,undef,\%linkprot_env); + return OK; + } + &Apache::lonnet::appenv({'request.linkprot' => $data{'linkprot'}}); + if ($data{'linkprotexit'}) { + &Apache::lonnet::appenv({'request.linkprotexit' => $data{'linkprotexit'}}); + } + if ($env{'request.linkkey'}) { + &Apache::lonnet::delenv('request.linkkey'); + } + my ($linkprotector,$deeplink) = split(/:/,$data{'linkprot'},2); + if ($env{'user.linkprotector'}) { + my @protectors = split(/,/,$env{'user.linkprotector'}); + unless (grep(/^\Q$linkprotector\E$/,@protectors)) { + push(@protectors,$linkprotector); + @protectors = sort { $a <=> $b } @protectors; + &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)}); + } + } else { + &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector }); + } + if ($env{'user.linkproturi'}) { + my @proturis = split(/,/,$env{'user.linkproturi'}); + unless(grep(/^\Q$deeplink\E$/,@proturis)) { + push(@proturis,$deeplink); + @proturis = sort @proturis; + &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)}); + } + } else { + &Apache::lonnet::appenv({'user.linkproturi' => $deeplink}); + } + } elsif ($data{'linkkey'} ne '') { + &Apache::lonnet::appenv({'request.linkkey' => $data{'linkkey'}}); + if ($env{'request.linkprot'}) { + &Apache::lonnet::delenv('request.linkprot'); + } + my $deeplink = $data{'deeplink.login'}; + my $linkkey = $data{'linkkey'}; + if ($env{'user.deeplinkkey'} ne '') { + my @linkkeys = split(/,/,$env{'user.deeplinkkey'}); + unless (grep(/^\Q$linkkey\E$/,@linkkeys)) { + push(@linkkeys,$linkkey); + &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))}); + } + } else { + &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey}); + } + if ($deeplink) { + if ($env{'user.keyedlinkuri'}) { + my @keyeduris = split(/,/,$env{'user.keyedlinkuri'}); + unless (grep(/^\Q$deeplink\E$/,@keyeduris)) { + push(@keyeduris,$deeplink); + &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))}); + } + } else { + &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink}); + } + } + } + if ($data{'deeplink.login'}) { + &Apache::lonnet::appenv({'request.deeplink.login' => $data{'deeplink.login'}}); + if ($env{'environment.remote'} eq 'on') { + &Apache::lonnet::appenv({'environment.remote' => 'off'}); + } + } + if ($data{'origurl'} ne '') { + my $dest = $data{'origurl'}; + if (($env{'request.deeplink.login'} eq $data{'origurl'}) && + (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) { + my %info; + if ($env{'request.linkprot'}) { + $info{'linkprot'} = $env{'request.linkprot'}; + foreach my $item ('linkprotuser','linkprotexit') { + if ($data{$item}) { + $info{$item} = $data{$item}; + } + } + } elsif ($env{'request.linkkey'} ne '') { + $info{'linkkey'} = $env{'request.linkkey'}; + } + $info{'origurl'} = $data{'origurl'}; + if ($checklaunch) { + $info{'checklaunch'} = 1; + } + my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link'); + unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) || + ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) { + $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token; + } + } + $r->internal_redirect($dest); + } elsif ($env{'request.course.id'}) { + $r->internal_redirect('/adm/navmaps'); + } else { + $r->internal_redirect('/adm/roles'); + } + } else { + my $desturl = '/adm/roles'; + if ($data{'origurl'} ne '') { + $desturl = $data{'origurl'}; + if ($data{'linkprot'}) { + my ($linkprotector,$linkuri) = split(/:/,$data{'linkprot'},2); + if ($linkprotector) { + if (ref($extra_env) eq 'HASH') { + $extra_env->{'user.linkprotector'} = $linkprotector; + $extra_env->{'user.linkproturi'} = $linkuri; + $extra_env->{'request.linkprot'} = $data{'linkprot'}; + } else { + $extra_env = {'user.linkprotector' => $linkprotector, + 'user.linkproturi' => $linkuri, + 'request.linkprot' => $data{'linkprot'}}; + } + if ($data{'linkprotexit'}) { + $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'}; + } + } + foreach my $item ('linkprotuser','linkprotexit') { + if ($data{$item} ne '') { + $form{$item} = $data{$item}; + } + } + } elsif ($data{'linkkey'}) { + if (ref($extra_env) eq 'HASH') { + $extra_env->{'user.deeplinkkey'} = $data{'linkkey'}; + $extra_env->{'user.keyedlinkuri'} = $data{'deeplink.login'}; + $extra_env->{'request.linkkey'} = $data{'linkkey'}; + } else { + $extra_env = {'user.deeplinkkey' => $data{'linkkey'}, + 'user.keyedlinkuri' => $data{'deeplink.login'}, + 'request.linkkey' => $data{'linkkey'}}; + } + } + if ($data{'deeplink.login'}) { + if (ref($extra_env) eq 'HASH') { + $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'}; + } else { + $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}}; + } + } + if ($desturl eq '/adm/email') { + if (($data{'display'}) && ($data{'mailrecip'})) { + if (&unescape($data{'mailrecip'}) eq "$data{'username'}:$data{'domain'}") { + $form{'display'} = &unescape($data{'display'}); + } + } + } + } + &Apache::lonauth::success($r,$data{'username'},$data{'domain'}, + $home,$desturl,$extra_env,\%form,'', + $expirepub,$data{'lcssowin'}); + } return OK; } - - my $cookie=&Apache::lonauth::success($r,$data{'username'},$data{'domain'}, - $home,'noredirect'); - $r->header_out('Set-cookie',"lonID=$cookie; path=/"); - &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'), - $cookie); - $env{'form.selectrole'}='1'; - $env{'form.'.$data{'role'}}='1'; - return &Apache::lonroles::handler($r); + + my $next_url='/adm/roles?selectrole=1&'.&escape($data{'role'}).'=1'; + if ($data{'origurl'} ne '') { + $next_url .= '&orgurl='.&escape($data{'origurl'}); + } + if ($data{'deeplink.login'}) { + if (ref($extra_env) eq 'HASH') { + $extra_env->{'request.deeplink.login'} = $data{'deeplink.login'}; + } else { + $extra_env = {'request.deeplink.login' => $data{'deeplink.login'}}; + } + if ($data{'linkprot'}) { + $extra_env->{'request.linkprot'} = $data{'linkprot'}; + if ($data{'linkprotexit'}) { + $extra_env->{'request.linkprotexit'} = $data{'linkprotexit'}; + } + foreach my $item ('linkprotuser','linkprotexit') { + if ($data{$item}) { + $form{'request.'.$item} = $data{$item}; + } + } + } elsif ($data{'linkkey'} ne '') { + $extra_env->{'request.linkkey'} = $data{'linkkey'}; + } + } + &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home, + $next_url,$extra_env,\%form,$cid,'',$data{'lcssowin'}); + return OK; } 1;