Diff for /loncom/auth/migrateuser.pm between versions 1.36 and 1.37

version 1.36, 2018/11/28 05:05:30 version 1.37, 2018/12/02 14:41:19
Line 33  use LONCAPA qw(:DEFAULT :match); Line 33  use LONCAPA qw(:DEFAULT :match);
 use Apache::Constants qw(:common :http :methods);  use Apache::Constants qw(:common :http :methods);
 use Apache::lonauth;  use Apache::lonauth;
 use Apache::lonnet;  use Apache::lonnet;
   use Apache::loncommon;
 use Apache::lonlocal;  use Apache::lonlocal;
 use Apache::lonlogin();  use Apache::lonlogin();
 use Apache::ltiauth;  use Apache::ltiauth;
   use Apache::switchserver;
 use CGI::Cookie;  use CGI::Cookie;
   
 sub goto_login {  sub goto_login {
Line 118  sub lti_check { Line 120  sub lti_check {
 }  }
   
 sub ip_changed {  sub ip_changed {
     my ($r,$udom,$camefrom,$dataref) = @_;      my ($r,$udom,$camefrom,$idsref,$dataref) = @_;
     &Apache::loncommon::content_type($r,'text/html');      &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;      $r->send_http_header;
     if (ref($dataref) eq 'HASH') {      if (ref($dataref) eq 'HASH') {
Line 167  sub ip_changed { Line 169  sub ip_changed {
         my $lonhost= $r->dir_config('lonHostID');          my $lonhost= $r->dir_config('lonHostID');
         my $switchto = $lonhost;          my $switchto = $lonhost;
         if ($rule_in_effect ne 'offloadedto') {          if ($rule_in_effect ne 'offloadedto') {
             my $hosthere;              my ($hosthere,@ids);
             my @ids=&Apache::lonnet::current_machine_ids();              if (ref($idsref) eq 'ARRAY') {
                   @ids=@{$idsref};
               }
             unless ($rule_in_effect eq 'balancer') {              unless ($rule_in_effect eq 'balancer') {
                 if (grep(/^\Q$rule_in_effect\E$/,@ids)) {                  if (grep(/^\Q$rule_in_effect\E$/,@ids)) {
                     $hosthere = 1;                      $hosthere = 1;
Line 325  sub conlost_userhome { Line 329  sub conlost_userhome {
     return unless ((ref($idsref) eq 'ARRAY') && (ref($dataref) eq 'HASH'));      return unless ((ref($idsref) eq 'ARRAY') && (ref($dataref) eq 'HASH'));
     my @ids = @{$idsref};      my @ids = @{$idsref};
     my %data = %{$dataref};      my %data = %{$dataref};
     my (%conlost,%posstargets);      my ($lonhost,$lowest_load,$otherserver,$is_balancer,%conlost,%posstargets);
     my $lonhost = $r->dir_config('lonHostID');      $lonhost = $r->dir_config('lonHostID');
     $conlost{$lonhost} = 1;      if (grep(/^\Q$lonhost\E$/,split(/,/,$data{'dom_balancers'}))) {
           $is_balancer = 1;
       }
       $lowest_load = 30000;
     if ($data{'conlost'} ne '') {      if ($data{'conlost'} ne '') {
         map { $conlost{$_} = 1; } split(/,/,$data{'conlost'});          map { $conlost{$_} = 1; } split(/,/,$data{'conlost'});
     }      }
     my ($lowest_load,$otherserver);      if ($data{'loncfail'}) {
     $lowest_load = 30000;          if ($is_balancer) {
               if ($lonhost ne $data{'from_balancer'}) {
                   my ($is_balancer,$posshost,$setcookie,$offloadto,$dom_balancers) =
                       &Apache::lonnet::check_loadbalancing($data{'username'},$data{'domain'});
                   if ($is_balancer) {
                       if (ref($offloadto) eq 'HASH') {
                           $data{'offloadto'} = '';
                           foreach my $key (keys(%{$offloadto})) {
                               if (ref($offloadto->{$key}) eq 'ARRAY') {
                                   $data{'offloadto'} .= $key.'='.join(',',@{$offloadto->{$key}}).'&';
                               }
                           }
                           $data{'offloadto'} =~ s/\&$//;
                       } elsif (ref($offloadto) eq 'ARRAY') {
                           $data{'offloadto'} = join(',',@{$offloadto});
                       }
                   }
               }
           }
       } else {
           $conlost{$lonhost} = 1;
           $data{'conlost'} = join(',',sort(keys(%conlost)));
       }
     if ($data{'offloadto'} =~ /\&/) {      if ($data{'offloadto'} =~ /\&/) {
         my @items = split(/\&/,$data{'offloadto'});          foreach my $item (split(/\&/,$data{'offloadto'})) {
         foreach my $item (@items) {  
             my ($type,$targets) = split(/\=/,$item);              my ($type,$targets) = split(/\=/,$item);
             @{$posstargets{$type}} = split(/,/,$targets);              @{$posstargets{$type}} = split(/,/,$targets);
         }          }
Line 347  sub conlost_userhome { Line 375  sub conlost_userhome {
     }      }
     if (ref($posstargets{'primary'}) eq 'ARRAY') {      if (ref($posstargets{'primary'}) eq 'ARRAY') {
         foreach my $try_server (@{$posstargets{'primary'}}) {          foreach my $try_server (@{$posstargets{'primary'}}) {
             next if (grep(/^\Q$try_server\E$/,@ids));  
             next if ($conlost{$try_server});              next if ($conlost{$try_server});
               next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
             ($otherserver,$lowest_load) =              ($otherserver,$lowest_load) =
                 &Apache::lonnet::compare_server_load($try_server,                  &Apache::lonnet::compare_server_load($try_server,
                                                      $otherserver,                                                       $otherserver,
Line 359  sub conlost_userhome { Line 387  sub conlost_userhome {
     if (!$found_server) {      if (!$found_server) {
         if (ref($posstargets{'default'}) eq 'ARRAY') {          if (ref($posstargets{'default'}) eq 'ARRAY') {
             foreach my $try_server (@{$posstargets{'default'}}) {              foreach my $try_server (@{$posstargets{'default'}}) {
                   next if ($conlost{$try_server});
                   next if ((grep(/^\Q$try_server\E$/,@ids)) && !$data{'loncfail'});
                 ($otherserver,$lowest_load) =                  ($otherserver,$lowest_load) =
                 &Apache::lonnet::compare_server_load($try_server,                      &Apache::lonnet::compare_server_load($try_server,
                                                      $otherserver,                                                           $otherserver,
                                                      $lowest_load);                                                           $lowest_load);
             }              }
         }          }
     }      }
     if ($otherserver ne '') {      if ($otherserver ne '') {
         my $switchto = &Apache::lonnet::hostname($otherserver);          my $switchto = &Apache::lonnet::hostname($otherserver);
         if ($switchto ne '') {          if ($switchto ne '') {
             my $protocol = $Apache::lonnet::protocol{$switchto};  
             $protocol = 'http' if ($protocol ne 'https');  
             my $url = $protocol.'://'.$switchto.'/adm/login?'.  
                                     'domain='.$env{'user.domain'}.  
                                     '&username='.$env{'user.name'};  
             $data{'conlost'} = join(',',sort(keys(%conlost)));  
             $data{'server'} = $lonhost;              $data{'server'} = $lonhost;
               if ($data{'loncfail'}) {
                   delete($data{'loncfail'});
               }
             if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {              if (grep(/^\Q$otherserver\E$/,split(/,/,$data{'dom_balancers'}))) {
                 $data{'noloadbalance'} = $otherserver;                   $data{'noloadbalance'} = $otherserver;
             }              }
             my $token = &Apache::lonnet::tmpput(\%data,$otherserver);              &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
             $url .= '&token='.$token;  
             $r->send_http_header;  
             $r->print(  
                 &Apache::loncommon::start_page('Switching Server ...',undef,  
                                                {'redirect'       => [0.1,$url]}).  
                                                 &Apache::loncommon::end_page());  
             return $otherserver;              return $otherserver;
           } else {
               #FIXME Contents of $data{offloadto} contains invalid hostID.
           }
       } else {
           my (%poss_balancers,%tried_balancers);
           map { $poss_balancers{$_} = 1; } split(/,/,$data{'dom_balancers'});
           map { $tried_balancers{$_} = 1; } split(/,/,$data{'tried_balancers'});
           if (keys(%poss_balancers)) {
               foreach my $try_server (sort(keys(%poss_balancers))) {
                   next if (grep(/^\Q$try_server\E$/,@ids));
                   next if (($data{'loncfail'}) && ($tried_balancers{$try_server}));
                   my $alreadytried;
                   foreach my $key (keys(%posstargets)) {
                       if ((ref($posstargets{$key}) eq 'ARRAY') &&
                           (grep(/^\Q$try_server\E$/,@{$posstargets{$key}}))) {
                           $alreadytried = 1;
                           last;
                       }
                   }
                   unless ($alreadytried) {
                       if (&Apache::lonnet::reply('ping',$try_server) eq $try_server) {
                           $otherserver = $try_server;
                           last;
                       }
                   }
               }
           }
           if ($otherserver ne '') {
               my $switchto = &Apache::lonnet::hostname($otherserver);
               if ($switchto ne '') {
                   $data{'server'} = $lonhost;
                   $data{'loncfail'} = $lonhost;
                   $tried_balancers{$otherserver} = 1;
                   $data{'tried_balancers'} = join(',',sort(keys(%tried_balancers)));
                   &do_server_switch($r,$otherserver,$switchto,$is_balancer,\%data);
                   return $otherserver;
               } else {
                   #FIXME Contents of $data{'dom_balancers'} contains invalid hostID.
               }  
           } else {
               if ($data{'loncfail'}) {
                   #FIXME Nowhere to go. 
               } else {
                   # FIXME Send back to a balancer (no token); different balancer if this is a balancer.
                   # Send command to balancer to exclude this host using http instead on lonc/lond
               }
         }          }
     }      }
       return;
   }
   
   sub do_server_switch {
       my ($r,$otherserver,$switchto,$is_balancer,$dataref) = @_;
       if (ref($dataref) eq 'HASH') {
           my $domain = $dataref->{'domain'};
           my $username = $dataref->{'username'};
           # If this is a balancer set a balancer cookie unless browser already sent
           # LON-CAPA load balancer cookie which points at the target server.
           if ($is_balancer) {
               my $newcookieid;
               my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
               unless (($found_server eq $otherserver) &&
                       ($balancer_cookie =~ /^\Q$domain\E_\Q$username\E_/)) {
                   $newcookieid =
                       &Apache::switchserver::balancer_cookieid($r,$otherserver,$username,$domain);
                   if ($newcookieid) {
                       $dataref->{'balcookie'} = $newcookieid;
                   }
               }
           }
           my $token = &Apache::lonnet::tmpput($dataref,$otherserver);
           my $protocol = $Apache::lonnet::protocol{$otherserver};
           $protocol = 'http' if ($protocol ne 'https');
           my $url = $protocol.'://'.$switchto.'/adm/login?domain='.$domain.
                                    '&username='.$username.'&token='.$token;
           $r->send_http_header;
           $r->print(
           &Apache::loncommon::start_page('Switching Server ...',undef,
                                          {'redirect'       => [0.1,$url]}).
           &Apache::loncommon::end_page());
       }
       return;
 }  }
   
 sub log_switch {  sub log_switch {
Line 436  sub handler { Line 537  sub handler {
     if (!defined($data{'username'}) || !defined($data{'domain'})) {      if (!defined($data{'username'}) || !defined($data{'domain'})) {
         return &goto_login($r,undef,\%data);          return &goto_login($r,undef,\%data);
     }      }
       my ($home,@ids);
       @ids=&Apache::lonnet::current_machine_ids();
     if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {      if ($data{'ip'} ne $ENV{'REMOTE_ADDR'}) {
         &Apache::lonnet::logthis('IP change when session migration requested -- was: '.          &Apache::lonnet::logthis('IP change when session migration requested -- was: '.
                  $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});                   $data{'ip'}.'; now: '.$ENV{'REMOTE_ADDR'}.' for '.$data{'username'}.':'.$data{'domain'});
  return &ip_changed($r,$data{'domain'},$data{'server'},\%data);   return &ip_changed($r,$data{'domain'},$data{'server'},\@ids,\%data);
     }      }
       if ($data{'loncfail'}) {
     my $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});          &Apache::lonnet::logthis('Returned from '.$data{'server'}.' -- no connection to library node or other access node(s)');
     if ($home eq 'no_host') {          my $otherserver = &conlost_userhome($r,\@ids,\%data);
         if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {          if ($otherserver ne '') {
             &Apache::lonnet::reconlonc($data{'home'});              &Apache::lonnet::logthis("Switching to $otherserver");
             $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});          }
             if ($home eq 'no_host') {          return OK;
                 my @ids=&Apache::lonnet::current_machine_ids();      } else {
                 unless (grep(/^\Q$data{'home'}\E$/,@ids)) {          $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
                     if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {          if ($home eq 'no_host') {
                         my $otherserver = &conlost_userhome($r,\@ids,\%data);              if (($data{'home'} ne '') && (&Apache::lonnet::hostname($data{'home'}))) {
                         if ($otherserver ne '') {                  &Apache::lonnet::reconlonc($data{'home'});
                             &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");                  $home=&Apache::lonnet::homeserver($data{'username'},$data{'domain'});
                   if ($home eq 'no_host') {
                       unless (grep(/^\Q$data{'home'}\E$/,@ids)) {
                           if (&Apache::lonnet::reply('ping',$data{'home'}) eq 'con_lost') {
                               my $otherserver = &conlost_userhome($r,\@ids,\%data);
                               if ($otherserver ne '') {
                                   &Apache::lonnet::logthis("No connection to home server ($data{'home'}) for $data{'username'}:$data{'domain'}. Switching to $otherserver");
                               }
                             return OK;                              return OK;
                         }                          }
                     }                      }
Line 488  sub handler { Line 598  sub handler {
         $extra_env = $lti_env;          $extra_env = $lti_env;
     }      }
   
       if (($data{'conlost'}) && ($data{'server'})) {
           my @conlosts = split(/,/,$data{'conlost'});
           my $switchfrom = $data{'server'};
           if (@conlosts) {
               if (grep(/^\Q$switchfrom\E$/,@conlosts)) {
                   &log_switch($r,\%data,$extra_env);
               }
           }
       }
   
     my %form;      my %form;
     if ($data{'symb'} ne '') {      if ($data{'symb'} ne '') {
         $form{'symb'} = $data{'symb'};          $form{'symb'} = $data{'symb'};
Line 499  sub handler { Line 619  sub handler {
         $form{'noloadbalance'} = $data{'noloadbalance'};          $form{'noloadbalance'} = $data{'noloadbalance'};
     }      }
   
     if (($data{'conlost'}) && ($data{'server'})) {  
         my @conlosts = split(/,/,$data{'conlost'});  
         my $switchfrom = $data{'server'};  
         if (@conlosts) {  
             if (grep(/^\Q$switchfrom\E$/,@conlosts)) {  
                 &log_switch($r,\%data,$extra_env);  
             }  
         }  
     }  
   
     if (!$data{'role'}) {      if (!$data{'role'}) {
  my $handle = &Apache::lonnet::check_for_valid_session($r);   my $handle = &Apache::lonnet::check_for_valid_session($r);
  if ($handle) {   if ($handle) {

Removed from v.1.36  
changed lines
  Added in v.1.37


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