File:  [LON-CAPA] / loncom / auth / migrateuser.pm
Revision 1.23: download - view: text, annotated - select for diffs
Mon Oct 6 00:48:44 2014 UTC (9 years, 7 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Bug 6675 (IP change detected in session migration).
- Additional option for both SSO user and non-SSO users
  - Session hosted (after re-authentication) on a specified server selected
    from servers belonging to domain's "internet domain".

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

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