File:  [LON-CAPA] / loncom / auth / migrateuser.pm
Revision 1.25.2.2: download - view: text, annotated - select for diffs
Sat May 2 17:35:40 2020 UTC (4 years ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.25: preferred, unified
- For 2.11
  Backport 1.46

    1: # The LearningOnline Network
    2: # Starts a user off based of an existing token.
    3: #
    4: # $Id: migrateuser.pm,v 1.25.2.2 2020/05/02 17:35:40 raeburn Exp $
    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::migrateuser;
   30: 
   31: use strict;
   32: use LONCAPA qw(:DEFAULT :match);
   33: use Apache::Constants qw(:common :http :methods);
   34: use Apache::lonauth;
   35: use Apache::lonnet;
   36: use Apache::lonlocal;
   37: use Apache::lonlogin();
   38: 
   39: sub goto_login {
   40:     my ($r,$domain) = @_;
   41:     &Apache::loncommon::content_type($r,'text/html');
   42:     $r->send_http_header;
   43:     my $url = '/adm/login';
   44:     if ($domain) {
   45:         $url .= '?domain='.$domain;
   46:     }
   47:     $r->print(&Apache::loncommon::start_page('Going to login',undef,
   48: 					     {'redirect' => [0,$url],}).
   49: 	      '<h1>'.&mt('One moment please...').'</h1>'.
   50: 	      '<p>'.&mt('Transferring to login page.').'</p>'.
   51: 	      &Apache::loncommon::end_page());
   52:     return OK;
   53: }
   54: 
   55: 
   56: sub sso_check {
   57:     my ($data) = @_;
   58:     my %extra_env;
   59:     if (ref($data) eq 'HASH') {
   60:         if ($data->{'sso.login'}) {
   61:             $extra_env{'request.sso.login'} = $data->{'sso.login'};
   62:         }
   63:         if ($data->{'sso.reloginserver'}) {
   64:             $extra_env{'request.sso.reloginserver'} = 
   65:                 $data->{'sso.reloginserver'};
   66:         }
   67:     }
   68:     return \%extra_env;
   69: }
   70: 
   71: sub ip_changed {
   72:     my ($r,$udom,$camefrom,$dataref) = @_;
   73:     &Apache::loncommon::content_type($r,'text/html');
   74:     $r->send_http_header;
   75:     if (ref($dataref) eq 'HASH') {
   76:         my $title = 'LON-CAPA Session redirected';
   77:         my $message = &mt('Your internet address has changed since you logged in.');  
   78:         my $rule_in_effect;
   79:         if ($dataref->{'balancer'}) {
   80:             my $baldom = &Apache::lonnet::host_domain($camefrom);
   81:             my $balprimaryid = &Apache::lonnet::domain($baldom,'primary');
   82:             my $balintdom = &Apache::lonnet::internet_dom($balprimaryid);
   83:             my $uprimaryid = &Apache::lonnet::domain($udom,'primary'); 
   84:             my $uintdom = &Apache::lonnet::internet_dom($uprimaryid);
   85:             my $dom_in_use;
   86:             if (($uintdom ne '') && ($uintdom eq $balintdom)) {
   87:                 $dom_in_use = $udom;
   88:             } else {
   89:                 $dom_in_use = $baldom;
   90:             }
   91:             my ($result,$cached)=&Apache::lonnet::is_cached_new('loadbalancing',$dom_in_use);
   92:             unless (defined($cached)) {
   93:                 my $cachetime = 60*60*24; 
   94:                 my %domconfig =
   95:                     &Apache::lonnet::get_dom('configuration',['loadbalancing'],$dom_in_use);
   96:                 if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
   97:                     $result = &Apache::lonnet::do_cache_new('loadbalancing',$dom_in_use,
   98:                                                             $domconfig{'loadbalancing'},$cachetime);
   99:                 }
  100:             }
  101:             if (ref($result) eq 'HASH') {
  102:                 (undef,my $currtargets,my $currrules) =
  103:                     &Apache::lonnet::check_balancer_result($result,$dataref->{'server'});
  104:                 if (ref($currrules) eq 'HASH') {
  105:                     if ($dataref->{'sso.login'}) {
  106:                         if ($currrules->{'_LC_ipchangesso'} ne '') {
  107:                             $rule_in_effect = $currrules->{'_LC_ipchangesso'};
  108:                         }
  109:                     } else {
  110:                         if ($currrules->{'_LC_ipchange'} ne '') {
  111:                             $rule_in_effect = $currrules->{'_LC_ipchange'};
  112:                         }
  113:                     }
  114:                 }
  115:             }
  116:         }
  117:         my $url;
  118:         my $lonhost= $r->dir_config('lonHostID');
  119:         my $switchto = $lonhost;
  120:         if ($rule_in_effect ne 'offloadedto') {
  121:             my $hosthere;
  122:             my @ids=&Apache::lonnet::current_machine_ids();
  123:             unless ($rule_in_effect eq 'balancer') {
  124:                 if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
  125:                     $hosthere = 1;
  126:                 }
  127:             }
  128:             unless ($hosthere) {
  129:                 if ($dataref->{'role'}) {
  130:                     my ($adom,$aname);
  131:                     if ($dataref->{'role'} =~ m{^au\./($match_domain)/$}) {
  132:                         $adom = $1;
  133:                         $aname = $dataref->{'username'};
  134:                     } elsif ($dataref->{'role'} =~ m{^(?:ca|aa)\./($match_domain)/($match_username)$}) {
  135:                         $adom = $1;
  136:                         $aname = $2;
  137:                     }
  138:                     if ($adom ne '' && $aname ne '') {
  139:                         my $ahome = &Apache::lonnet::homeserver($aname,$adom);
  140:                         unless ($ahome eq 'no_host') {
  141:                             if ($ahome && grep(/^\Q$ahome\E$/,@ids)) {
  142:                                 $hosthere = 1;
  143:                             }
  144:                         }
  145:                     }
  146:                 }
  147:             }
  148:             unless ($hosthere) {
  149:                 my $hostname;
  150:                 if ($rule_in_effect eq 'balancer') {
  151:                     $hostname = &Apache::lonnet::hostname($dataref->{'server'});
  152:                     if ($hostname) {
  153:                         $switchto = $dataref->{'server'};
  154:                     }
  155:                 } else {
  156:                     $hostname = &Apache::lonnet::hostname($rule_in_effect);
  157:                     if ($hostname) {
  158:                         $switchto = $rule_in_effect;
  159:                     }
  160:                 }
  161:                 if ($hostname) {
  162:                     my $protocol = $Apache::lonnet::protocol{$switchto};
  163:                     $protocol = 'http' if ($protocol ne 'https');
  164:                     $url = $protocol.'://'.$hostname;
  165:                     if ($rule_in_effect eq 'balancer') {
  166:                         $message .= '<br />'.
  167:                                     &mt('As a result, your LON-CAPA session is being redirected to the server where you originally logged in.');
  168:                     } else {
  169:                         $message .= '<br />'.
  170:                                     &mt('As a result, your LON-CAPA session is being redirected.');
  171:                     }
  172:                 }
  173:             }
  174:             unless ($hosthere) {
  175:                 if (($dataref->{'balancer'}) && ($dataref->{'balcookie'})) {
  176:                     &Apache::lonnet::delbalcookie($dataref->{'balcookie'},$dataref->{'balancer'});
  177:                 }
  178:             }
  179:         }
  180:         if ($dataref->{'sso.login'}) {
  181:             $url .= '/adm/roles';
  182:         } else {
  183:             $url .= '/adm/login';
  184:             if ($udom) {
  185:                 $url .= '?domain='.$udom;
  186:             }
  187:             $message .= '<br />'.&mt('You will need to provide your password one more time.');
  188:         }
  189:         my %info= (
  190:                     'domain'          => $udom,
  191:                     'username'        => $dataref->{'username'},
  192:                     'role'            => $dataref->{'role'},
  193:                     'sessionserver'   => $lonhost,
  194:                   );
  195:         if ($dataref->{'origurl'}) {
  196:             $info{'origurl'} = $dataref->{'origurl'};
  197:         }
  198:         if ($dataref->{'symb'}) {
  199:             $info{'symb'} = $dataref->{'symb'};
  200:         }
  201:         my $iptoken = &Apache::lonnet::tmpput(\%info,$switchto);
  202:         unless ($iptoken eq 'conlost') {
  203:             $url .= ($url =~ /\?/) ? '&' : '?';
  204:             $url .= 'iptoken='.$iptoken;
  205:         }
  206:         $r->print(&Apache::loncommon::start_page($title,undef,
  207:                                                  {'redirect' =>
  208:                                                   [2,$url],}).
  209:                   '<h1>'.&mt('One moment please...').'</h1>'.
  210:                   '<p class="LC_warning">'.$message.'</p>'.
  211:                   &Apache::loncommon::end_page());
  212:     } else {
  213:         return &goto_login($r);
  214:     }
  215:     return OK;
  216: }
  217: 
  218: sub handler {
  219:     my ($r) = @_;
  220:     
  221:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['token']);
  222:     my %data =   &Apache::lonnet::tmpget($env{'form.token'});
  223:     if (keys(%data) == 0) {
  224:         return &goto_login($r);
  225:     }
  226:     my $delete = &Apache::lonnet::tmpdel($env{'form.token'});
  227: 
  228:     &Apache::lonlocal::get_language_handle($r);
  229: 
  230:     if ($delete ne 'ok') {
  231: 	return &goto_login($r);
  232:     }
  233: 
  234:     if (!defined($data{'username'}) || !defined($data{'domain'})) {
  235:         return &goto_login($r);
  236:     }
  237:     if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
  238:         &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
  239:                  $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
  240: 	return &ip_changed($r,$data{'domain'},$data{'server'},\%data);
  241:     }
  242: 
  243:     &Apache::lonnet::logthis("Allowing access for $data{'username'}:$data{'domain'} to $data{'role'}");
  244:     my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
  245:     my $udom;
  246:     if (&Apache::lonnet::domain($data{'domain'})) {
  247:         $udom=$data{'domain'};
  248:     }
  249:     if ($home =~ /(con_lost|no_such_host)/) { return &goto_login($r,$udom); }
  250: 
  251:     my $extra_env = &sso_check(\%data);
  252:     if (($data{'balancer'}) && ($data{'server'}) && ($data{'balcookie'})) {
  253:         if (ref($extra_env) eq 'HASH') {
  254:             $extra_env->{'request.balancercookie'} = $data{'server'}.':'.$data{'balcookie'};
  255:         } else {
  256:             $extra_env = { 'request.balancercookie' => $data{'server'}.':'.$data{'balcookie'} };
  257:         }
  258:     }
  259: 
  260:     my (%form,$cid);
  261:     if ($data{'symb'} ne '') {
  262:         $form{'symb'} = $data{'symb'};
  263:     }
  264:     if ($data{'iptoken'} ne '') {
  265:         $form{'iptoken'} = $data{'iptoken'};
  266:     }
  267:     if ($data{'noloadbalance'} ne '') {
  268:         $form{'noloadbalance'} = $data{'noloadbalance'};
  269:     }
  270:     if ($data{'role'}) {
  271:         if ($data{'role'} =~ m{\./($match_domain)/($match_courseid)(?:/\w+|$)}) {
  272:             unless (&Apache::lonnet::homeserver($2,$1) eq 'no_host') {
  273:                 $cid = $1.'_'.$2;
  274:             }
  275:         }
  276:     } else {
  277: 	my $handle = &Apache::lonnet::check_for_valid_session($r);
  278: 	if ($handle) {
  279: 	    &Apache::lonnet::transfer_profile_to_env($r->dir_config('lonIDsDir'),
  280: 						     $handle);
  281:             if ($data{'origurl'} ne '') {
  282:                 $r->internal_redirect($data{'origurl'});
  283:             } elsif ($env{'request.course.id'}) {
  284:                 $r->internal_redirect('/adm/navmaps');
  285: 	    } else {
  286: 		$r->internal_redirect('/adm/roles');
  287: 	    }
  288: 	} else {
  289:             my $desturl = '/adm/roles';
  290:             if ($data{'origurl'} ne '') {
  291:                 $desturl = $data{'origurl'};
  292:             }
  293: 	    &Apache::lonauth::success($r,$data{'username'},$data{'domain'},
  294: 				      $home,$desturl,$extra_env,\%form);
  295: 
  296: 	}
  297: 	return OK;
  298: 
  299:     }
  300: 
  301:     my $next_url='/adm/roles?selectrole=1&amp;'.&escape($data{'role'}).'=1';
  302:     if ($data{'origurl'} ne '') {
  303:         $next_url .= '&amp;orgurl='.&escape($data{'origurl'});
  304:     }
  305:     &Apache::lonauth::success($r,$data{'username'},$data{'domain'},$home,
  306: 			      $next_url,$extra_env,\%form,$cid);
  307:     return OK;
  308: }
  309: 
  310: 1;
  311: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>