Diff for /loncom/auth/lonauth.pm between versions 1.121.2.21 and 1.142

version 1.121.2.21, 2021/01/04 03:48:29 version 1.142, 2017/02/17 14:34:21
Line 1 Line 1
 # The LearningOnline Network  # The LearningOnline Network
 # User Authentication Module  # User Authentication Module
 #  #
 # $Id$  # $Id$
 #  #
 # Copyright Michigan State University Board of Trustees  # Copyright Michigan State University Board of Trustees
 #  #
 # This file is part of the LearningOnline Network with CAPA (LON-CAPA).  # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
 #  #
 # LON-CAPA is free software; you can redistribute it and/or modify  # LON-CAPA is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by  # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation; either version 2 of the License, or  # the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.  # (at your option) any later version.
 #  #
 # LON-CAPA is distributed in the hope that it will be useful,  # LON-CAPA is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of  # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.  # GNU General Public License for more details.
 #  #
 # You should have received a copy of the GNU General Public License  # You should have received a copy of the GNU General Public License
 # along with LON-CAPA; if not, write to the Free Software  # along with LON-CAPA; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #  #
 # /home/httpd/html/adm/gpl.txt  # /home/httpd/html/adm/gpl.txt
 #  #
 # http://www.lon-capa.org/  # http://www.lon-capa.org/
 #  #
   
 package Apache::lonauth;  package Apache::lonauth;
   
 use strict;  use strict;
 use LONCAPA;  use LONCAPA qw(:DEFAULT :match);
 use Apache::Constants qw(:common);  use Apache::Constants qw(:common);
 use CGI qw(:standard);  use CGI qw(:standard);
 use Apache::loncommon();  use DynaLoader; # for Crypt::DES version
 use Apache::lonnet;  use Crypt::DES;
 use Apache::lonmenu();  use Apache::loncommon();
 use Apache::createaccount;  use Apache::lonnet;
 use Fcntl qw(:flock);  use Apache::lonmenu();
 use Apache::lonlocal;  use Apache::createaccount;
 use Apache::File();  use Fcntl qw(:flock);
 use HTML::Entities;  use Apache::lonlocal;
 use Digest::MD5;  use Apache::File();
    use HTML::Entities;
 # ------------------------------------------------------------ Successful login   
 sub success {  # ------------------------------------------------------------ Successful login
     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,  sub success {
  $form,$cid) = @_;      my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
    $form) = @_;
 # ------------------------------------------------------------ Get cookie ready  
     my $cookie =  # ------------------------------------------------------------ Get cookie ready
  &Apache::loncommon::init_user_environment($r, $username, $domain,      my $cookie =
   $authhost, $form,   &Apache::loncommon::init_user_environment($r, $username, $domain,
   {'extra_env' => $extra_env,});    $authhost, $form,
     {'extra_env' => $extra_env,});
     my $public=($username eq 'public' && $domain eq 'public');  
       my $public=($username eq 'public' && $domain eq 'public');
     if ($public or $lowerurl eq 'noredirect') { return $cookie; }  
       if ($public or $lowerurl eq 'noredirect') { return $cookie; }
 # -------------------------------------------------------------------- Log this  
   # -------------------------------------------------------------------- Log this
     my $ip = &Apache::lonnet::get_requestor_ip();  
     &Apache::lonnet::log($domain,$username,$authhost,      &Apache::lonnet::log($domain,$username,$authhost,
                          "Login $ip");                           "Login $ENV{'REMOTE_ADDR'}");
   
 # ------------------------------------------------- Check for critical messages  # ------------------------------------------------- Check for critical messages
   
     my @what=&Apache::lonnet::dump('critical',$domain,$username);      my @what=&Apache::lonnet::dump('critical',$domain,$username);
     if ($what[0]) {      if ($what[0]) {
  if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {   if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
     $lowerurl='/adm/email?critical=display';      $lowerurl='/adm/email?critical=display';
         }          }
     }      }
   
 # ------------------------------------------------------------ Get cookies ready  # ------------------------------------------------------------ Get cookie ready
     my ($securecookie,$defaultcookie);      $cookie="lonID=$cookie; path=/; HttpOnly";
     my $ssl = $r->subprocess_env('https');  # -------------------------------------------------------- Menu script and info
     if ($ssl) {      my $destination = $lowerurl;
         $securecookie="lonSID=$cookie; path=/; HttpOnly; secure";  
         my $lonidsdir=$r->dir_config('lonIDsDir');      if (defined($form->{role})) {
         if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {          my $envkey = 'user.role.'.$form->{role};
             my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';          my $now=time;
             if (-e "$lonidsdir/$linkname.id") {          my $then=$env{'user.login.time'};
                 unlink("$lonidsdir/$linkname.id");          my $refresh=$env{'user.refresh.time'};
             }          my $update=$env{'user.update.time'};
             my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",          if (!$update) {
                                               "$lonidsdir/$linkname.id"); 1 };              $update = $then;
             if ($made_symlink) {          }
                 $defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";          if (exists($env{$envkey})) {
                 &Apache::lonnet::appenv({'user.linkedenv' => $linkname});              my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
             }              &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
         }                                           \$trolecode,\$tstatus,\$tstart,\$tend);
     } else {              if ($tstatus eq 'is') {
         $defaultcookie = "lonID=$cookie; path=/; HttpOnly;";                  $destination  .= ($destination =~ /\?/) ? '&' : '?';
     }                  my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
 # -------------------------------------------------------- Menu script and info                  $destination .= 'selectrole=1&'.$newrole.'=1';
     my $destination = $lowerurl;              }
           }
     if (defined($form->{role})) {      }
         my $envkey = 'user.role.'.$form->{role};      if (defined($form->{symb})) {
         my $now=time;          my $destsymb = $form->{symb};
         my $then=$env{'user.login.time'};          $destination  .= ($destination =~ /\?/) ? '&' : '?';
         my $refresh=$env{'user.refresh.time'};          if ($destsymb =~ /___/) {
         my $update=$env{'user.update.time'};              # FIXME Need to deal with encrypted symbs and urls as needed.
         if (!$update) {              my ($map,$resid,$desturl)=split(/___/,$destsymb);
             $update = $then;              $desturl = &Apache::lonnet::clutter($desturl);
         }              $desturl = &HTML::Entities::encode($desturl,'"<>&');
         if (exists($env{$envkey})) {              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);              $destination .= 'destinationurl='.$desturl.
             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,                              '&destsymb='.$destsymb;
                                          \$trolecode,\$tstatus,\$tstart,\$tend);          } else {
             if ($tstatus eq 'is') {              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
                 $destination  .= ($destination =~ /\?/) ? '&' : '?';              $destination .= 'destinationurl='.$destsymb;
                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');          }
                 $destination .= 'selectrole=1&'.$newrole.'=1';      }
             }      if ($destination =~ m{^/adm/roles}) {
         }          $destination  .= ($destination =~ /\?/) ? '&' : '?';
     }          $destination .= 'source=login';
     if (defined($form->{symb})) {      }
         my $destsymb = $form->{symb};  
         my $encrypted;      my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="loncapaclient";');
         if ($destsymb =~ m{^/enc/}) {      my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
             $encrypted = 1;      my $brcrum = [{'href' => '',
             if ($cid) {                     'text' => 'Successful Login'},];
                 $destsymb = &Apache::lonenc::unencrypted($destsymb,$cid);      my $args = {'bread_crumbs' => $brcrum,};
             }      unless ((defined($form->{role})) || (defined($form->{symb}))) {
         }          my $update=$env{'user.update.time'};
         $destination  .= ($destination =~ /\?/) ? '&' : '?';          if (!$update) {
         if ($destsymb =~ /___/) {              $update = $env{'user.login.time'};
             my ($map,$resid,$desturl)=split(/___/,$destsymb);          }
             $desturl = &Apache::lonnet::clutter($desturl);          my %roles_in_env;
             if ($encrypted) {          my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
                 $desturl = &Apache::lonenc::encrypted($desturl,1,$cid);          if ($showcount == 1) {
                 $destsymb = $form->{symb};              foreach my $rolecode (keys(%roles_in_env)) {
             }                  my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
             $desturl = &HTML::Entities::encode($desturl,'"<>&');                  if ($cid) {
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');                      my %coursedescription =
             $destination .= 'destinationurl='.$desturl.                          &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
                             '&destsymb='.$destsymb;                      if ($coursedescription{'type'} eq 'Placement') {
         } elsif (!$encrypted) {                          $args->{'crstype'} = 'Placement';
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');                      }
             $destination .= 'destinationurl='.$destsymb;                      last;
         }                  }
     }              }
     if ($destination =~ m{^/adm/roles}) {          }
         $destination  .= ($destination =~ /\?/) ? '&' : '?';      }
         $destination .= 'source=login';      my $start_page=&Apache::loncommon::start_page('Successful Login',
     }                                                    $header,$args);
       my $end_page  =&Apache::loncommon::end_page();
     my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});  
     my $startupremote=&Apache::lonmenu::startupremote($destination);   my $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
     my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);  # ------------------------------------------------- Output for successful login
     my $setflags=&Apache::lonmenu::setflags();  
     my $maincall=&Apache::lonmenu::maincall();      &Apache::loncommon::content_type($r,'text/html');
     my $brcrum = [{'href' => '',      $r->header_out('Set-cookie' => $cookie);
                    'text' => 'Successful Login'},];      $r->send_http_header;
     my $start_page=&Apache::loncommon::start_page('Successful Login',  
                                                   $startupremote,      my %lt=&Apache::lonlocal::texthash(
                                                   {'no_inline_link' => 1,         'wel' => 'Welcome',
                                                    'bread_crumbs' => $brcrum,});         'pro' => 'Login problems?',
     my $end_page  =&Apache::loncommon::end_page();         );
       my $loginhelp = &loginhelpdisplay($domain);
     my $continuelink;      if ($loginhelp) {
     if ($env{'environment.remote'} eq 'off') {          $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
  $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';      }
     }  
 # ------------------------------------------------- Output for successful login      my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
       $r->print(<<ENDSUCCESS);
     &Apache::loncommon::content_type($r,'text/html');  $start_page
     if ($securecookie) {  $windowinfo
         $r->headers_out->add('Set-cookie' => $securecookie);  <h1>$lt{'wel'}</h1>
     }  $welcome
     if ($defaultcookie) {  $loginhelp
         $r->headers_out->add('Set-cookie' => $defaultcookie);  $continuelink
     }  $end_page
     $r->send_http_header;  ENDSUCCESS
       return;
     my %lt=&Apache::lonlocal::texthash(  }
        'wel' => 'Welcome',  
        'pro' => 'Login problems?',  # --------------------------------------------------------------- Failed login!
        );  
     my $loginhelp = &loginhelpdisplay($domain);  sub failed {
     if ($loginhelp) {      my ($r,$message,$form) = @_;
         $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';      (undef,undef,undef,my $clientmathml,my $clientunicode) =
     }          &Apache::loncommon::decode_user_agent();
       my $args = {};
     my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');       if ($clientunicode && !$clientmathml) {
     $r->print(<<ENDSUCCESS);          $args = {'browser.unicode' => 1};
 $start_page      }
 $setflags  
 $windowinfo      my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
 <h1>$lt{'wel'}</h1>      my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
 $welcome      my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
 $loginhelp      if (&Apache::lonnet::domain($udom,'description') eq '') {
 $remoteinfo          undef($udom);
 $maincall      }
 $continuelink      my $retry = '/adm/login';
 $end_page      if ($uname eq $form->{'uname'}) {
 ENDSUCCESS          $retry .= '?username='.$uname;
     return;      }
 }      if ($udom) {
           $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
 # --------------------------------------------------------------- Failed login!      }
       if (exists($form->{role})) {
 sub failed {          my $role = &Apache::loncommon::cleanup_html($form->{role});
     my ($r,$message,$form) = @_;          if ($role ne '') {
     (undef,undef,undef,my $clientmathml,my $clientunicode) =              $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
         &Apache::loncommon::decode_user_agent();          }
     my $args = {};      }
     if ($clientunicode && !$clientmathml) {      if (exists($form->{symb})) {
         $args = {'browser.unicode' => 1};          my $symb = &Apache::loncommon::cleanup_html($form->{symb});
     }          if ($symb ne '') {
               $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);          }
     my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});      }
     my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});      my $end_page = &Apache::loncommon::end_page();
     if (&Apache::lonnet::domain($udom,'description') eq '') {      &Apache::loncommon::content_type($r,'text/html');
         undef($udom);      $r->send_http_header;
     }      my @actions =
     my $retry = '/adm/login';           (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
     if ($uname eq $form->{'uname'}) {      my $loginhelp = &loginhelpdisplay($udom);
         $retry .= '?username='.$uname;      if ($loginhelp) {
     }          push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
     if ($udom) {      }
         $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;      #FIXME: link to helpdesk might be added here
     }  
     if (exists($form->{role})) {      $r->print(
         my $role = &Apache::loncommon::cleanup_html($form->{role});         $start_page
         if ($role ne '') {        .'<h2>'.&mt('Sorry ...').'</h2>'
             $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;        .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
         }        .&Apache::lonhtmlcommon::actionbox(\@actions)
     }        .$end_page
     if (exists($form->{symb})) {      );
         my $symb = &Apache::loncommon::cleanup_html($form->{symb});   }
         if ($symb ne '') {  
             $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;  # ------------------------------------------------------------------ Rerouting!
         }  
     }  sub reroute {
     my $end_page = &Apache::loncommon::end_page();      my ($r) = @_;
     &Apache::loncommon::content_type($r,'text/html');      &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;      $r->send_http_header;
     my @actions =      my $msg='<b>'.&mt('Sorry ...').'</b><br />'
          (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));             .&mt('Please [_1]log in again[_2].');
     my $loginhelp = &loginhelpdisplay($udom);      &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
     if ($loginhelp) {  }
         push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');  
     }  # ---------------------------------------------------------------- Main handler
     #FIXME: link to helpdesk might be added here  
   sub handler {
     $r->print(      my $r = shift;
        $start_page      my $londocroot = $r->dir_config('lonDocRoot');
       .'<h2>'.&mt('Sorry ...').'</h2>'  # Are we re-routing?
       .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'      if (-e "$londocroot/lon-status/reroute.txt") {
       .&Apache::lonhtmlcommon::actionbox(\@actions)   &reroute($r);
       .$end_page   return OK;
     );      }
  }  
       &Apache::lonlocal::get_language_handle($r);
 # ------------------------------------------------------------------ Rerouting!  
   # -------------------------------- Prevent users from attempting to login twice
 sub reroute {      my $handle = &Apache::lonnet::check_for_valid_session($r);
     my ($r) = @_;      if ($handle ne '') {
     &Apache::loncommon::content_type($r,'text/html');          my $lonidsdir=$r->dir_config('lonIDsDir');
     $r->send_http_header;          if ($handle=~/^publicuser\_/) {
     my $msg='<b>'.&mt('Sorry ...').'</b><br />'  # For "public user" - remove it, we apparently really want to login
            .&mt('Please [_1]log in again[_2].');              unlink($r->dir_config('lonIDsDir')."/$handle.id");
     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});          } else {
 }  # Indeed, a valid token is found
               &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
 # ---------------------------------------------------------------- Main handler      &Apache::loncommon::content_type($r,'text/html');
       $r->send_http_header;
 sub handler {      my $start_page =
     my $r = shift;          &Apache::loncommon::start_page('Already logged in');
     my $londocroot = $r->dir_config('lonDocRoot');      my $end_page =
 # Are we re-routing?          &Apache::loncommon::end_page();
     if (-e "$londocroot/lon-status/reroute.txt") {              my $dest = '/adm/roles';
  &reroute($r);              if ($env{'form.firsturl'} ne '') {
  return OK;                  $dest = $env{'form.firsturl'};
     }              }
               $r->print(
     &Apache::lonlocal::get_language_handle($r);                 $start_page
                 .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
 # -------------------------------- Prevent users from attempting to login twice                .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
     my $handle = &Apache::lonnet::check_for_valid_session($r);                      ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
     if ($handle ne '') {                .'</p>'
         my $lonidsdir=$r->dir_config('lonIDsDir');                .$end_page
         if ($handle=~/^publicuser\_/) {              );
 # For "public user" - remove it, we apparently really want to login              return OK;
             unlink($r->dir_config('lonIDsDir')."/$handle.id");          }
         } else {      }
 # Indeed, a valid token is found  
             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);  # ---------------------------------------------------- No valid token, continue
     &Apache::loncommon::content_type($r,'text/html');  
     $r->send_http_header;  
     my $start_page =       my $buffer;
         &Apache::loncommon::start_page('Already logged in');      if ($r->header_in('Content-length') > 0) {
     my $end_page =    $r->read($buffer,$r->header_in('Content-length'),0);
         &Apache::loncommon::end_page();      }
             my $dest = '/adm/roles';      my %form;
             if ($env{'form.firsturl'} ne '') {      foreach my $pair (split(/&/,$buffer)) {
                 $dest = $env{'form.firsturl'};         my ($name,$value) = split(/=/,$pair);
             }         $value =~ tr/+/ /;
             $r->print(         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                $start_page         $form{$name}=$value;
               .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'      }
               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'  
                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')      if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
               .'</p>'   &failed($r,'Username, password and domain need to be specified.',
               .$end_page   \%form);
             );          return OK;
             return OK;      }
         }  
     }  # split user logging in and "su"-user
   
 # ---------------------------------------------------- No valid token, continue      ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
       $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
       $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
     my $buffer;      $form{'udom'}  = &LONCAPA::clean_domain(  $form{'udom'});
     if ($r->header_in('Content-length') > 0) {  
  $r->read($buffer,$r->header_in('Content-length'),0);      my $role   = $r->dir_config('lonRole');
     }      my $domain = $r->dir_config('lonDefDomain');
     my %form;      my $prodir = $r->dir_config('lonUsersDir');
     foreach my $pair (split(/&/,$buffer)) {      my $contact_name = &mt('LON-CAPA helpdesk');
        my ($name,$value) = split(/=/,$pair);  
        $value =~ tr/+/ /;  # ---------------------------------------- Get the information from login token
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;  
        $form{$name}=$value;      my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
     }                                        $form{'serverid'});
   
     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {      if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
  &failed($r,'Username, password and domain need to be specified.',          ($tmpinfo eq 'no_such_host')) {
  \%form);   &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
         return OK;          return OK;
     }      } else {
    my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
 # split user logging in and "su"-user     $form{'serverid'});
           if ( $reply ne 'ok' ) {
     ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});              &failed($r,'Session could not be opened.',\%form);
     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});      &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});      return OK;
     $form{'udom'}  = &LONCAPA::clean_domain($form{'udom'});   }
     $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});      }
   
     my $role   = $r->dir_config('lonRole');      if (!&Apache::lonnet::domain($form{'udom'})) {
     my $domain = $r->dir_config('lonDefDomain');          &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
     my $prodir = $r->dir_config('lonUsersDir');          return OK;
     my $contact_name = &mt('LON-CAPA helpdesk');      }
   
 # ---------------------------------------- Get the information from login token      my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo);
       if ($rolestr) {
     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},          $rolestr = &unescape($rolestr);
                                       $form{'serverid'});      }
       if ($symbstr) {
     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||           $symbstr= &unescape($symbstr);
         ($tmpinfo eq 'no_such_host')) {      }
  &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);      if ($iptokenstr) {
         return OK;          $iptokenstr = &unescape($iptokenstr);
     } else {      }
  my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},      if ($rolestr =~ /^role=/) {
    $form{'serverid'});          (undef,$form{'role'}) = split('=',$rolestr);
         if ( $reply ne 'ok' ) {      }
             &failed($r,'Session could not be opened.',\%form);      if ($symbstr =~ /^symb=/) {
     &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");          (undef,$form{'symb'}) = split('=',$symbstr);
     return OK;      }
  }      if ($iptokenstr =~ /^iptoken=/) {
     }          (undef,$form{'iptoken'}) = split('=',$iptokenstr);
       }
     if (!&Apache::lonnet::domain($form{'udom'})) {  
         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);      my $upass = $ENV{HTTPS} ? $form{'upass0'}
         return OK;          : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
     }  
   # ---------------------------------------------------------------- Authenticate
     my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo);  
     if ($rolestr) {      my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
         $rolestr = &unescape($rolestr);      my ($cancreate,$statustocreate) =
     }          &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
     if ($symbstr) {      my $defaultauth;
         $symbstr= &unescape($symbstr);      if (ref($cancreate) eq 'ARRAY') {
     }          if (grep(/^login$/,@{$cancreate})) {
     if ($iptokenstr) {              $defaultauth = 1;
         $iptokenstr = &unescape($iptokenstr);          }
     }      }
     if ($rolestr =~ /^role=/) {      my $clientcancheckhost = 1;
         (undef,$form{'role'}) = split('=',$rolestr);      my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
     }                                                $form{'udom'},$defaultauth,
     if ($symbstr =~ /^symb=/) {                                                 $clientcancheckhost);
         (undef,$form{'symb'}) = split('=',$symbstr);     
     }  # --------------------------------------------------------------------- Failed?
     if ($iptokenstr =~ /^iptoken=/) {  
         (undef,$form{'iptoken'}) = split('=',$iptokenstr);      if ($authhost eq 'no_host') {
     }   &failed($r,'Username and/or password could not be authenticated.',
    \%form);
     my $upass = &Apache::loncommon::des_decrypt($key,$form{'upass0'});          return OK;
       } elsif ($authhost eq 'no_account_on_host') {
 # ---------------------------------------------------------------- Authenticate          if ($defaultauth) {
               my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});              unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
     my ($cancreate,$statustocreate) =                  return OK;
         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});              }
     my $defaultauth;              my $start_page =
     if (ref($cancreate) eq 'ARRAY') {                  &Apache::loncommon::start_page('Create a user account in LON-CAPA');
         if (grep(/^login$/,@{$cancreate})) {              my $lonhost = $r->dir_config('lonHostID');
             $defaultauth = 1;              my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
         }              my $contacts =
     }                  &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
     my $clientcancheckhost = 1;                                                          $form{'udom'},$origmail);
     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,              my ($contact_email) = split(',',$contacts);
                                               $form{'udom'},$defaultauth,              my $output =
                                               $clientcancheckhost);                  &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
                                                              $domdesc,'',$lonhost,
 # --------------------------------------------------------------------- Failed?                                                         $contact_email,$contact_name,
                                                          undef,$statustocreate);
     if ($authhost eq 'no_host') {              &Apache::loncommon::content_type($r,'text/html');
  &failed($r,'Username and/or password could not be authenticated.',              $r->send_http_header;
  \%form);              &Apache::createaccount::print_header($r,$start_page);
         return OK;              $r->print('<h3>'.&mt('Account creation').'</h3>'.
     } elsif ($authhost eq 'no_account_on_host') {                        &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
         if ($defaultauth) {                        $output.&Apache::loncommon::end_page());
             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');              return OK;
             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {          } else {
                 return OK;              &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);
             }              return OK;
             my $start_page =           }
                 &Apache::loncommon::start_page('Create a user account in LON-CAPA',      }
                                                '',{'no_inline_link'   => 1,});  
             my $lonhost = $r->dir_config('lonHostID');      if (($firsturl eq '') ||
             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};   ($firsturl=~/^\/adm\/(logout|remote)/)) {
             my $contacts =    $firsturl='/adm/roles';
                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',      }
                                                         $form{'udom'},$origmail);  
             my ($contact_email) = split(',',$contacts);       my $hosthere;
             my $output =       if ($form{'iptoken'}) {
                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},          my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
                                                        $domdesc,'',$lonhost,          my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
                                                        $contact_email,$contact_name,          if (($sessiondata{'domain'} eq $form{'udom'}) &&
                                                        undef,$statustocreate);              ($sessiondata{'username'} eq $form{'uname'})) {
             &Apache::loncommon::content_type($r,'text/html');              $hosthere = 1;
             $r->send_http_header;          }
             &Apache::createaccount::print_header($r,$start_page);      }
             $r->print('<h3>'.&mt('Account creation').'</h3>'.  
                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.  # --------------------------------- Are we attempting to login as somebody else?
                       $output.&Apache::loncommon::end_page());      if ($form{'suname'}) {
             return OK;  # ------------ see if the original user has enough privileges to pull this stunt
         } else {   if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
             &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);  # ---------------------------------------------------- see if the su-user exists
             return OK;      unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
         }   eq 'no_host') {
     }   &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
   # ------------------------------ see if the su-user is not too highly privileged
     if (($firsturl eq '') ||    unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
  ($firsturl=~/^\/adm\/(logout|remote)/)) {  # -------------------------------------------------------- actually switch users
  $firsturl='/adm/roles';      &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
     }   ' logging in as '.$form{'suname'});
       $form{'uname'}=$form{'suname'};
     my $hosthere;   } else {
     if ($form{'iptoken'}) {      &Apache::lonnet::logthis('Attempted switch user to privileged user');
         my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});   }
         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});      }
         if (($sessiondata{'domain'} eq $form{'udom'}) &&   } else {
             ($sessiondata{'username'} eq $form{'uname'})) {      &Apache::lonnet::logthis('Non-privileged user attempting switch user');
             $hosthere = 1;   }
         }      }
     }  
       my ($is_balancer,$otherserver);
 # --------------------------------- Are we attempting to login as somebody else?  
     if ($form{'suname'}) {      unless ($hosthere) {
         my ($suname,$sudom,$sudomref);          ($is_balancer,$otherserver) =
         $suname = $form{'suname'};              &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
         $sudom = $form{'udom'};          if ($is_balancer) {
         if ($form{'sudom'}) {              if ($otherserver eq '') {
             unless ($sudom eq $form{'sudom'}) {                  my $lowest_load;
                 if (&Apache::lonnet::domain($form{'sudom'})) {                  ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
                     $sudomref = [$form{'sudom'}];                  if ($lowest_load > 100) {
                     $sudom = $form{'sudom'};                      $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});
                 }                  }
             }              }
         }              if ($otherserver ne '') {
 # ------------ see if the original user has enough privileges to pull this stunt                  my @hosts = &Apache::lonnet::current_machine_ids();
  if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {                  if (grep(/^\Q$otherserver\E$/,@hosts)) {
 # ---------------------------------------------------- see if the su-user exists                      $hosthere = $otherserver;
     unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {                  }
 # ------------------------------ see if the su-user is not too highly privileged              }
  if (&Apache::lonnet::privileged($suname,$sudom)) {          }
                     &Apache::lonnet::logthis('Attempted switch user to privileged user');      }
                 } else {  
                     my $noprivswitch;      if (($is_balancer) && (!$hosthere)) {
 #          if ($otherserver) {
 # su-user's home server and user's home server must have one of:              &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
 # (a) same domain                       \%form);
 # (b) same primary library server for the two domains              my $switchto = '/adm/switchserver?otherserver='.$otherserver;
 # (c) same "internet domain" for primary library server(s) for home servers' domains              if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
 #                  $switchto .= '&origurl='.$firsturl;
                     my $suprim = &Apache::lonnet::domain($sudom,'primary');              }
                     my $suintdom = &Apache::lonnet::internet_dom($suprim);              if ($form{'role'}) {
                     unless ($sudom eq $form{'udom'}) {                  $switchto .= '&role='.$form{'role'};
                         my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');              }
                         my $uintdom = &Apache::lonnet::internet_dom($uprim);              if ($form{'symb'}) {
                         unless ($suprim eq $uprim) {                  $switchto .= '&symb='.$form{'symb'};
                             unless ($suintdom eq $uintdom) {              }
                                 &Apache::lonnet::logthis('Attempted switch user '              $r->internal_redirect($switchto);
                                    .'to user with different "internet domain".');          } else {
                                 $noprivswitch = 1;              $r->print(&noswitch());
                             }          }
                         }          return OK;
                     }      } else {
           if (!&check_can_host($r,\%form,$authhost)) {
                     unless ($noprivswitch) {              my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
 #              if ($otherserver) {
 # server where log-in occurs must have same "internet domain" as su-user's home                  &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
 # server                           \%form);
 #                  my $switchto = '/adm/switchserver?otherserver='.$otherserver;
                         my $lonhost = $r->dir_config('lonHostID');                  if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
                         my $hostintdom = &Apache::lonnet::internet_dom($lonhost);                      $switchto .= '&origurl='.$firsturl;
                         if ($hostintdom ne $suintdom) {                  }
                             &Apache::lonnet::logthis('Attempted switch user on a '                  if ($form{'role'}) {
                                 .'server with a different "internet domain".');                      $switchto .= '&role='.$form{'role'};
                         } else {                  }
                   if ($form{'symb'}) {
 # -------------------------------------------------------- actually switch users                      $switchto .= '&symb='.$form{'symb'};
                   }
     &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.                  $r->internal_redirect($switchto);
  $form{'udom'}.' logging in as '.$suname.':'.$sudom);              } else {
     $form{'uname'}=$suname;                  $r->print(&noswitch());
                             if ($form{'udom'} ne $sudom) {              }
                                 $form{'udom'}=$sudom;              return OK;
                             }          }
                         }  
                     }  # ------------------------------------------------------- Do the load balancing
  }  
     }  # ---------------------------------------------------------- Determine own load
  } else {          my $loadlim = $r->dir_config('lonLoadLim');
     &Apache::lonnet::logthis('Non-privileged user attempting switch user');          my $loadavg;
  }          {
     }              my $loadfile=Apache::File->new('/proc/loadavg');
               $loadavg=<$loadfile>;
     my ($is_balancer,$otherserver);          }
           $loadavg =~ s/\s.*//g;
     unless ($hosthere) {          my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
         ($is_balancer,$otherserver) =          my $userloadpercent=&Apache::lonnet::userload();
             &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');  
         if ($is_balancer) {  # ---------------------------------------------------------- Are we overloaded?
             # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)          if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
             my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);              my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
             if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {              if (!$unloaded) {
                 $otherserver = $found_server;                  ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
             }              }
             if ($otherserver eq '') {              if ($unloaded) {
                 my $lowest_load;                  &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
                 ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});                           undef,\%form);
                 if ($lowest_load > 100) {                  $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
                     $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});                  return OK;
                 }              }
             }          }
             if ($otherserver ne '') {          if (($is_balancer) && ($hosthere)) {
                 my @hosts = &Apache::lonnet::current_machine_ids();              $form{'noloadbalance'} = $hosthere;
                 if (grep(/^\Q$otherserver\E$/,@hosts)) {          }
                     $hosthere = $otherserver;          &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
                 }                   \%form);
             }          return OK;
         }      }
     }  }
   
     if (($is_balancer) && (!$hosthere)) {  sub check_can_host {
         if ($otherserver) {      my ($r,$form,$authhost,$domdesc) = @_;
             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,      return unless (ref($form) eq 'HASH');
                      \%form);      my $canhost = 1;
             my $switchto = '/adm/switchserver?otherserver='.$otherserver;      my $lonhost = $r->dir_config('lonHostID');
             if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {      my $udom = $form->{'udom'};
                 $switchto .= '&origurl='.$firsturl;      my @intdoms;
             }      my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
             if ($form{'role'}) {      if (ref($internet_names) eq 'ARRAY') {
                 $switchto .= '&role='.$form{'role'};          @intdoms = @{$internet_names};
             }      }
             if ($form{'symb'}) {      my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                 $switchto .= '&symb='.$form{'symb'};      my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
             }      unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
             $r->internal_redirect($switchto);          my $machine_dom = &Apache::lonnet::host_domain($lonhost);
         } else {          my $hostname = &Apache::lonnet::hostname($lonhost);
             &Apache::loncommon::content_type($r,'text/html');          my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
             $r->send_http_header;          my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
             $r->print(&noswitch());          my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
         }          my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
         return OK;          my $loncaparev;
     } else {          if ($authhost eq 'no_account_on_host') {
         if (!&check_can_host($r,\%form,$authhost)) {              $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});          } else {
             if ($otherserver) {              $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,          }
                          \%form);          $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
                 my $switchto = '/adm/switchserver?otherserver='.$otherserver;                                                       $udomdefaults{'remotesessions'},
                 if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {                                                       $defdomdefaults{'hostedsessions'});
                     $switchto .= '&origurl='.$firsturl;      }
                 }      unless ($canhost) {
                 if ($form{'role'}) {          if ($authhost eq 'no_account_on_host') {
                     $switchto .= '&role='.$form{'role'};              my $checkloginvia = 1;
                 }              my ($login_host,$hostname) =
                 if ($form{'symb'}) {                  &Apache::lonnet::choose_server($udom,$checkloginvia);
                     $switchto .= '&symb='.$form{'symb'};              &Apache::loncommon::content_type($r,'text/html');
                 }              $r->send_http_header;
                 $r->internal_redirect($switchto);              if ($login_host ne '') {
             } else {                  my $protocol = $Apache::lonnet::protocol{$login_host};
                 &Apache::loncommon::content_type($r,'text/html');                  $protocol = 'http' if ($protocol ne 'https');
                 $r->send_http_header;                  my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
                 $r->print(&noswitch());                  $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
             }                            '<h3>'.&mt('Account creation').'</h3>'.
             return OK;                            &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
         }                            '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
                             '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
 # ------------------------------------------------------- Do the load balancing                            &Apache::loncommon::end_page());
               } else {
 # ---------------------------------------------------------- Determine own load                  $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
         my $loadlim = $r->dir_config('lonLoadLim');                            '<h3>'.&mt('Account creation unavailable').'</h3>'.
         my $loadavg;                            &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
         {                            '<p>'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'</p>'.
             my $loadfile=Apache::File->new('/proc/loadavg');                            &Apache::loncommon::end_page());
             $loadavg=<$loadfile>;              }
         }          } else {
         $loadavg =~ s/\s.*//g;              &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);                       $form);
         my $userloadpercent=&Apache::lonnet::userload();              my ($otherserver) = &Apache::lonnet::choose_server($udom);
               $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
 # ---------------------------------------------------------- Are we overloaded?          }
         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {      }
             my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});      return $canhost;
             if (!$unloaded) {  }
                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});  
             }  sub noswitch {
             if ($unloaded) {      my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',                   '<h3>'.&mt('Session unavailable').'</h3>'.
                          undef,\%form);                   &mt('This LON-CAPA server is unable to host your session.').'<br />'.
                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);                   '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
                 return OK;                   &Apache::loncommon::end_page();
             }      return $result;
         }  }
         if (($is_balancer) && ($hosthere)) {  
             $form{'noloadbalance'} = $hosthere;  sub loginhelpdisplay {
         }      my ($authdomain) = @_;
         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,      my $login_help = 1;
                  \%form);      my $lang = &Apache::lonlocal::current_language();
         return OK;      if ($login_help) {
     }          my $dom = $authdomain;
 }          if ($dom eq '') {
               $dom = &Apache::lonnet::default_login_domain();
 sub check_can_host {          }
     my ($r,$form,$authhost,$domdesc) = @_;          my %domconfhash = &Apache::loncommon::get_domainconf($dom);
     return unless (ref($form) eq 'HASH');          my $loginhelp_url;
     my $canhost = 1;          if ($lang) {
     my $lonhost = $r->dir_config('lonHostID');              $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
     my $udom = $form->{'udom'};              if ($loginhelp_url ne '') {
     my @intdoms;                  return $loginhelp_url;
     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);              }
     if (ref($internet_names) eq 'ARRAY') {          }
         @intdoms = @{$internet_names};          $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
     }          if ($loginhelp_url ne '') {
     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');              return $loginhelp_url;
     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);          } else {
     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {              return '/adm/loginproblems.html';
         my $machine_dom = &Apache::lonnet::host_domain($lonhost);          }
         my $hostname = &Apache::lonnet::hostname($lonhost);      }
         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);      return;
         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);  }
         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);  
         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);  1;
         my $loncaparev;  __END__
         if ($authhost eq 'no_account_on_host') {  
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);  
         } else {  
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);  
         }  
         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,  
                                                      $udomdefaults{'remotesessions'},  
                                                      $defdomdefaults{'hostedsessions'});  
     }  
     unless ($canhost) {  
         if ($authhost eq 'no_account_on_host') {  
             my $checkloginvia = 1;  
             my ($login_host,$hostname) =   
                 &Apache::lonnet::choose_server($udom,$checkloginvia);  
             &Apache::loncommon::content_type($r,'text/html');  
             $r->send_http_header;  
             if ($login_host ne '') {  
                 my $protocol = $Apache::lonnet::protocol{$login_host};  
                 $protocol = 'http' if ($protocol ne 'https');  
                 my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';  
                 $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').  
                           '<h3>'.&mt('Account creation').'</h3>'.  
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.  
                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.  
                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').  
                           &Apache::loncommon::end_page());  
             } else {  
                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').  
                           '<h3>'.&mt('Account creation unavailable').'</h3>'.  
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.  
                           '<p>'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'</p>'.  
                           &Apache::loncommon::end_page());  
             }  
         } else {  
             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,  
                      $form);  
             my ($otherserver) = &Apache::lonnet::choose_server($udom);  
             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);  
         }  
     }  
     return $canhost;  
 }  
   
 sub noswitch {  
     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').  
                  '<h3>'.&mt('Session unavailable').'</h3>'.  
                  &mt('This LON-CAPA server is unable to host your session.').'<br />'.  
                  '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.  
                  &Apache::loncommon::end_page();  
     return $result;  
 }  
   
 sub loginhelpdisplay {  
     my ($authdomain) = @_;  
     my $login_help = 1;  
     my $lang = &Apache::lonlocal::current_language();  
     if ($login_help) {  
         my $dom = $authdomain;  
         if ($dom eq '') {  
             $dom = &Apache::lonnet::default_login_domain();  
         }  
         my %domconfhash = &Apache::loncommon::get_domainconf($dom);  
         my $loginhelp_url;  
         if ($lang) {  
             $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};  
             if ($loginhelp_url ne '') {  
                 return $loginhelp_url;  
             }  
         }  
         $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};  
         if ($loginhelp_url ne '') {  
             return $loginhelp_url;  
         } else {  
             return '/adm/loginproblems.html';  
         }  
     }  
     return;  
 }  
   
 1;  
 __END__  
   
   

Removed from v.1.121.2.21  
changed lines
  Added in v.1.142


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