Diff for /loncom/auth/lonauth.pm between versions 1.121 and 1.151

version 1.121, 2012/04/11 11:40:07 version 1.151, 2018/04/21 21:29:31
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 DynaLoader; # for Crypt::DES version  use Apache::loncommon();
 use Crypt::DES;  use Apache::lonnet;
 use Apache::loncommon();  use Apache::lonmenu();
 use Apache::lonnet;  use Apache::createaccount;
 use Apache::lonmenu();  use Apache::ltiauth;
 use Apache::createaccount;  use Fcntl qw(:flock);
 use Fcntl qw(:flock);  use Apache::lonlocal;
 use Apache::lonlocal;  use Apache::File();
 use Apache::File();  use HTML::Entities;
 use HTML::Entities;  use Digest::MD5;
     
 # ------------------------------------------------------------ Successful login  # ------------------------------------------------------------ Successful login
 sub success {  sub success {
     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,      my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
  $form) = @_;   $form,$skipcritical) = @_;
   
 # ------------------------------------------------------------ Get cookie ready  # ------------------------------------------------------------ Get cookie ready
     my $cookie =      my $cookie =
  &Apache::loncommon::init_user_environment($r, $username, $domain,   &Apache::loncommon::init_user_environment($r, $username, $domain,
   $authhost, $form,    $authhost, $form,
   {'extra_env' => $extra_env,});    {'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
   
     &Apache::lonnet::log($domain,$username,$authhost,      &Apache::lonnet::log($domain,$username,$authhost,
                          "Login $ENV{'REMOTE_ADDR'}");                           "Login $ENV{'REMOTE_ADDR'}");
   
 # ------------------------------------------------- Check for critical messages  # ------------------------------------------------- Check for critical messages
   
     my @what=&Apache::lonnet::dump('critical',$domain,$username);      unless ($skipcritical) {
     if ($what[0]) {          my @what=&Apache::lonnet::dump('critical',$domain,$username);
  if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {          if ($what[0]) {
     $lowerurl='/adm/email?critical=display';      if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
         }          $lowerurl='/adm/email?critical=display';
     }              }
           }
 # ------------------------------------------------------------ Get cookie ready      }
     $cookie="lonID=$cookie; path=/";  
 # -------------------------------------------------------- Menu script and info  # ----------------------------------------------------------- Get cookies ready
     my $destination = $lowerurl;      my ($securecookie,$defaultcookie);
       if ($ENV{'SERVER_PORT'} == 443) {
     if (defined($form->{role})) {          $securecookie="lonID=$cookie; path=/; HttpOnly; secure";
         my $envkey = 'user.role.'.$form->{role};          my $lonidsdir=$r->dir_config('lonIDsDir');
         my $now=time;          if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {
         my $then=$env{'user.login.time'};              my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';
         my $refresh=$env{'user.refresh.time'};              if (-e "$lonidsdir/$linkname.id") {
         my $update=$env{'user.update.time'};                  unlink("$lonidsdir/$linkname.id");
         if (!$update) {              }
             $update = $then;              my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",
         }                                                "$lonidsdir/$linkname.id"); 1 };
         if (exists($env{$envkey})) {              if ($made_symlink) {
             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);                  $defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";
             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,                  &Apache::lonnet::appenv({'user.linkedenv' => "$lonidsdir/$linkname.id"});
                                          \$trolecode,\$tstatus,\$tstart,\$tend);              }
             if ($tstatus eq 'is') {          }
                 $destination  .= ($destination =~ /\?/) ? '&' : '?';      } else {
                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');          $defaultcookie = "lonID=$cookie; path=/; HttpOnly;";
                 $destination .= 'selectrole=1&'.$newrole.'=1';      }
             }  # -------------------------------------------------------- Menu script and info
         }      my $destination = $lowerurl;
     }      if ($env{'request.lti.login'}) {
     if (defined($form->{symb})) {          if (($env{'request.lti.reqcrs'}) && ($env{'request.lti.reqrole'} eq 'cc')) {
         my $destsymb = $form->{symb};              &Apache::loncommon::content_type($r,'text/html');
         $destination  .= ($destination =~ /\?/) ? '&' : '?';              if ($securecookie) {
         if ($destsymb =~ /___/) {                  $r->headers_out->add('Set-cookie' => $securecookie);
             # FIXME Need to deal with encrypted symbs and urls as needed.              }
             my ($map,$resid,$desturl)=split(/___/,$destsymb);              if ($defaultcookie) {
             unless ($desturl=~/^(adm|uploaded|editupload|public)/) {                  $r->headers_out->add('Set-cookie' => $defaultcookie);
                 $desturl = &Apache::lonnet::clutter($desturl);              }
             }              $r->send_http_header;
             $desturl = &HTML::Entities::encode($desturl,'"<>&');              &Apache::ltiauth::lti_reqcrs($r,$domain,$form,$username,$domain);
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');              return;
             $destination .= '&destinationurl='.$desturl.          }
                             '&destsymb='.$destsymb;          if ($env{'request.lti.selfenrollrole'}) {
         } else {              if (&Apache::ltiauth::lti_enroll($username,$domain,
             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');                                               $env{'request.lti.selfenrollrole'}) eq 'ok') {
             $destination .= '&destinationurl='.$destsymb;                  $form->{'role'} = $env{'request.lti.selfenrollrole'};
         }                  &Apache::lonnet::delenv('request.lti.selfenrollrole');
     }              } else {
     if ($destination =~ m{^/adm/roles}) {                  &Apache::ltiauth::invalid_request($r,24);
         $destination  .= ($destination =~ /\?/) ? '&' : '?';              }
         $destination .= 'source=login';          }
     }      }
       if (defined($form->{role})) {
     my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="loncapaclient";');          my $envkey = 'user.role.'.$form->{role};
     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';          my $now=time;
     my $brcrum = [{'href' => '',          my $then=$env{'user.login.time'};
                    'text' => 'Successful Login'},];          my $refresh=$env{'user.refresh.time'};
     my $start_page=&Apache::loncommon::start_page('Successful Login',          my $update=$env{'user.update.time'};
                                                   $header,          if (!$update) {
                                                   {'bread_crumbs' => $brcrum,});              $update = $then;
     my $end_page  =&Apache::loncommon::end_page();          }
           if (exists($env{$envkey})) {
  my $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';              my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
 # ------------------------------------------------- Output for successful login              &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
                                            \$trolecode,\$tstatus,\$tstart,\$tend);
     &Apache::loncommon::content_type($r,'text/html');              if ($tstatus eq 'is') {
     $r->header_out('Set-cookie' => $cookie);                  $destination  .= ($destination =~ /\?/) ? '&' : '?';
     $r->send_http_header;                  my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
                   $destination .= 'selectrole=1&'.$newrole.'=1';
     my %lt=&Apache::lonlocal::texthash(              }
        'wel' => 'Welcome',          }
        'pro' => 'Login problems?',      }
        'log' => 'loginproblems.html',      if (defined($form->{symb})) {
        );          my $destsymb = $form->{symb};
     my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');           $destination  .= ($destination =~ /\?/) ? '&' : '?';
     $r->print(<<ENDSUCCESS);          if ($destsymb =~ /___/) {
 $start_page              # FIXME Need to deal with encrypted symbs and urls as needed.
 $windowinfo              my ($map,$resid,$desturl)=split(/___/,$destsymb);
 <h1>$lt{'wel'}</h1>              $desturl = &Apache::lonnet::clutter($desturl);
 $welcome<p>              $desturl = &HTML::Entities::encode($desturl,'"<>&');
 <a href="/adm/$lt{'log'}">$lt{'pro'}</a></p>              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
 $continuelink              $destination .= 'destinationurl='.$desturl.
 $end_page                              '&destsymb='.$destsymb;
 ENDSUCCESS          } else {
 }              $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
               $destination .= 'destinationurl='.$destsymb;
 # --------------------------------------------------------------- Failed login!          }
       }
 sub failed {      if ($destination =~ m{^/adm/roles}) {
     my ($r,$message,$form) = @_;          $destination  .= ($destination =~ /\?/) ? '&' : '?';
     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef);          $destination .= 'source=login';
     my $retry = '/adm/login?username='.$form->{'uname'}.      }
                 '&domain='.$form->{'udom'};  
     if (exists($form->{role})) {      my $windowname = 'loncapaclient';
         $retry .= '&role='.$form->{role};      if ($env{'request.lti.login'}) {
     }          $windowname .= 'lti';
     if (exists($form->{symb})) {      }
         $retry .= '&symb='.$form->{symb};      my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";');
     }      my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
     my $end_page   = &Apache::loncommon::end_page();      my $brcrum = [{'href' => '',
     &Apache::loncommon::content_type($r,'text/html');                     'text' => 'Successful Login'},];
     $r->send_http_header;      my $args = {'bread_crumbs' => $brcrum,};
     $r->print(      unless ((defined($form->{role})) || (defined($form->{symb}))) {
        $start_page          my $update=$env{'user.update.time'};
       .'<h1>'.&mt('Sorry ...').'</h1>'          if (!$update) {
       .'<p class="LC_warning">'.&mt($message).'</p>'              $update = $env{'user.login.time'};
       .'<p>'.&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>')          }
       .'</p>'          my %roles_in_env;
       .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'          my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
       .$end_page          if ($showcount == 1) {
     );              foreach my $rolecode (keys(%roles_in_env)) {
  }                  my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
                   if ($cid) {
 # ------------------------------------------------------------------ Rerouting!                      my %coursedescription =
                           &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
 sub reroute {                      if ($coursedescription{'type'} eq 'Placement') {
     my ($r) = @_;                          $args->{'crstype'} = 'Placement';
     &Apache::loncommon::content_type($r,'text/html');                      }
     $r->send_http_header;                      last;
     my $msg='<h1>'.&mt('Sorry ...').'</h1>'                  }
            .&mt('Please [_1]log in again[_2].');              }
     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg);          }
 }      }
   
 # ---------------------------------------------------------------- Main handler  # ------------------------------------------------- Output for successful login
   
 sub handler {      &Apache::loncommon::content_type($r,'text/html');
     my $r = shift;      if ($securecookie) {
     my $londocroot = $r->dir_config('lonDocRoot');          $r->headers_out->add('Set-cookie' => $securecookie);
     my $form;      }
 # Are we re-routing?      if ($defaultcookie) {
     if (-e "$londocroot/lon-status/reroute.txt") {          $r->headers_out->add('Set-cookie' => $defaultcookie);
  &reroute($r);      }
  return OK;      $r->send_http_header;
     }  
       my $start_page;
     &Apache::lonlocal::get_language_handle($r);      if ($env{'request.lti.login'}) {
           $args = {'only_body' => 1,};
 # -------------------------------- Prevent users from attempting to login twice          $start_page=&Apache::loncommon::start_page('',$header,$args);
     my $handle = &Apache::lonnet::check_for_valid_session($r);      } else {
     if ($handle ne '') {          $start_page=&Apache::loncommon::start_page('Successful Login',
         my $lonidsdir=$r->dir_config('lonIDsDir');                                                     $header,$args);
         if ($handle=~/^publicuser\_/) {      }
 # For "public user" - remove it, we apparently really want to login      my $end_page  =&Apache::loncommon::end_page();
             unlink($r->dir_config('lonIDsDir')."/$handle.id");  
         } else {      my $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
 # Indeed, a valid token is found  
             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);      my $pagebody;
     &Apache::loncommon::content_type($r,'text/html');      unless ($env{'request.lti.login'}) {
     $r->send_http_header;          my %lt=&Apache::lonlocal::texthash(
     my $start_page =              'wel' => 'Welcome',
         &Apache::loncommon::start_page('Already logged in');             'pro' => 'Login problems?',
     my $end_page =             );
         &Apache::loncommon::end_page();          $pagebody = "<h1>$lt{'wel'}</h1>\n".
             my $dest = '/adm/roles';                      &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
             if ($env{'form.firsturl'} ne '') {          my $loginhelp = &loginhelpdisplay($domain);
                 $dest = $env{'form.firsturl'};          if ($loginhelp) {
             }              $pagebody .= '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
             $r->print(          }
                $start_page      }
               .'<p class="LC_warning>"'.&mt('You are already logged in!').'</p>'      $r->print(<<ENDSUCCESS);
               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'  $start_page
                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')  $windowinfo
               .'</p>'  $pagebody
               .$end_page  $continuelink
             );  $end_page
             return OK;  ENDSUCCESS
         }      return;
     }  }
   
 # ---------------------------------------------------- No valid token, continue  # --------------------------------------------------------------- Failed login!
   
   sub failed {
     my $buffer;      my ($r,$message,$form) = @_;
     if ($r->header_in('Content-length') > 0) {      (undef,undef,undef,my $clientmathml,my $clientunicode) =
  $r->read($buffer,$r->header_in('Content-length'),0);          &Apache::loncommon::decode_user_agent();
     }      my $args = {};
     my %form;      if ($clientunicode && !$clientmathml) {
     foreach my $pair (split(/&/,$buffer)) {          $args = {'browser.unicode' => 1};
        my ($name,$value) = split(/=/,$pair);      }
        $value =~ tr/+/ /;  
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;      my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
        $form{$name}=$value;      my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
     }       my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
       if (&Apache::lonnet::domain($udom,'description') eq '') {
     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {          undef($udom);
  &failed($r,'Username, password and domain need to be specified.',      }
  \%form);      my $retry = '/adm/login';
         return OK;      if ($uname eq $form->{'uname'}) {
     }          $retry .= '?username='.$uname;
       }
 # split user logging in and "su"-user      if ($udom) {
           $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
     ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});      }
     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});      if (exists($form->{role})) {
     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});          my $role = &Apache::loncommon::cleanup_html($form->{role});
     $form{'udom'}  = &LONCAPA::clean_domain(  $form{'udom'});          if ($role ne '') {
               $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
     my $role   = $r->dir_config('lonRole');          }
     my $domain = $r->dir_config('lonDefDomain');      }
     my $prodir = $r->dir_config('lonUsersDir');      if (exists($form->{symb})) {
     my $contact_name = &mt('LON-CAPA helpdesk');          my $symb = &Apache::loncommon::cleanup_html($form->{symb});
           if ($symb ne '') {
 # ---------------------------------------- Get the information from login token              $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
           }
     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},      }
                                       $form{'serverid'});      my $end_page = &Apache::loncommon::end_page();
       &Apache::loncommon::content_type($r,'text/html');
     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||       $r->send_http_header;
         ($tmpinfo eq 'no_such_host')) {      my @actions =
  &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);           (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
         return OK;      my $loginhelp = &loginhelpdisplay($udom);
     } else {      if ($loginhelp) {
  my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},          push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
    $form{'serverid'});      }
         if ( $reply ne 'ok' ) {      #FIXME: link to helpdesk might be added here
             &failed($r,'Session could not be opened.',\%form);  
     &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");      $r->print(
     return OK;         $start_page
  }        .'<h2>'.&mt('Sorry ...').'</h2>'
     }        .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
         .&Apache::lonhtmlcommon::actionbox(\@actions)
     if (!&Apache::lonnet::domain($form{'udom'})) {        .$end_page
         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);      );
         return OK;   }
     }  
   # ------------------------------------------------------------------ Rerouting!
     my ($key,$firsturl,$rolestr,$symbstr)=split(/&/,$tmpinfo);  
     if ($rolestr) {  sub reroute {
         $rolestr = &unescape($rolestr);      my ($r) = @_;
     }      &Apache::loncommon::content_type($r,'text/html');
     if ($symbstr) {      $r->send_http_header;
         $symbstr= &unescape($symbstr);      my $msg='<b>'.&mt('Sorry ...').'</b><br />'
     }             .&mt('Please [_1]log in again[_2].');
     if ($rolestr =~ /^role=/) {      &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
         (undef,$form{'role'}) = split('=',$rolestr);  }
     }  
     if ($symbstr =~ /^symb=/) {   # ---------------------------------------------------------------- Main handler
         (undef,$form{'symb'}) = split('=',$symbstr);  
     }  sub handler {
       my $r = shift;
     my $keybin=pack("H16",$key);      my $londocroot = $r->dir_config('lonDocRoot');
   # Are we re-routing?
     my $cipher;      if (-e "$londocroot/lon-status/reroute.txt") {
     if ($Crypt::DES::VERSION>=2.03) {   &reroute($r);
  $cipher=new Crypt::DES $keybin;   return OK;
     }      }
     else {  
  $cipher=new DES $keybin;      &Apache::lonlocal::get_language_handle($r);
     }  
     my $upass='';  # -------------------------------- Prevent users from attempting to login twice
     for (my $i=0;$i<=2;$i++) {      my $handle = &Apache::lonnet::check_for_valid_session($r);
  my $chunk=      if ($handle ne '') {
     $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));          my $lonidsdir=$r->dir_config('lonIDsDir');
           if ($handle=~/^publicuser\_/) {
  $chunk.=  # For "public user" - remove it, we apparently really want to login
     $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));              unlink($r->dir_config('lonIDsDir')."/$handle.id");
           } else {
  $chunk=substr($chunk,1,ord(substr($chunk,0,1)));  # Indeed, a valid token is found
  $upass.=$chunk;              &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
     }      &Apache::loncommon::content_type($r,'text/html');
       $r->send_http_header;
 # ---------------------------------------------------------------- Authenticate      my $start_page =
           &Apache::loncommon::start_page('Already logged in');
     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});      my $end_page =
     my ($cancreate,$statustocreate) =          &Apache::loncommon::end_page();
         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});              my $dest = '/adm/roles';
     my $defaultauth;              if ($env{'form.firsturl'} ne '') {
     if (ref($cancreate) eq 'ARRAY') {                  $dest = $env{'form.firsturl'};
         if (grep(/^login$/,@{$cancreate})) {              }
             $defaultauth = 1;              $r->print(
         }                 $start_page
     }                .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
     my $clientcancheckhost = 1;                .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,                      ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
                                               $form{'udom'},$defaultauth,                .'</p>'
                                               $clientcancheckhost);                .$end_page
                   );
 # --------------------------------------------------------------------- Failed?              return OK;
           }
     if ($authhost eq 'no_host') {      }
  &failed($r,'Username and/or password could not be authenticated.',  
  \%form);  # ---------------------------------------------------- No valid token, continue
         return OK;  
     } elsif ($authhost eq 'no_account_on_host') {  
         if ($defaultauth) {      my $buffer;
             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');      if ($r->header_in('Content-length') > 0) {
             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {   $r->read($buffer,$r->header_in('Content-length'),0);
                 return OK;      }
             }      my %form;
             my $start_page =       foreach my $pair (split(/&/,$buffer)) {
                 &Apache::loncommon::start_page('Create a user account in LON-CAPA');         my ($name,$value) = split(/=/,$pair);
             my $lonhost = $r->dir_config('lonHostID');         $value =~ tr/+/ /;
             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
             my $contacts =          $form{$name}=$value;
                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',      }
                                                         $form{'udom'},$origmail);  
             my ($contact_email) = split(',',$contacts);       if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
             my $output =    &failed($r,'Username, password and domain need to be specified.',
                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},   \%form);
                                                        $domdesc,'',$lonhost,          return OK;
                                                        $contact_email,$contact_name,      }
                                                        undef,$statustocreate);  
             &Apache::loncommon::content_type($r,'text/html');  # split user logging in and "su"-user
             $r->send_http_header;  
             &Apache::createaccount::print_header($r,$start_page);      ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
             $r->print('<h3>'.&mt('Account creation').'</h3>'.      $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.      $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
                       $output.&Apache::loncommon::end_page());      $form{'udom'}  = &LONCAPA::clean_domain($form{'udom'});
             return OK;      $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
         } else {  
             &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);      my $role   = $r->dir_config('lonRole');
             return OK;      my $domain = $r->dir_config('lonDefDomain');
         }      my $prodir = $r->dir_config('lonUsersDir');
     }      my $contact_name = &mt('LON-CAPA helpdesk');
   
     if (($firsturl eq '') ||   # ---------------------------------------- Get the information from login token
  ($firsturl=~/^\/adm\/(logout|remote)/)) {  
  $firsturl='/adm/roles';      my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
     }                                        $form{'serverid'});
 # --------------------------------- Are we attempting to login as somebody else?  
     if ($form{'suname'}) {      if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
 # ------------ see if the original user has enough privileges to pull this stunt          ($tmpinfo eq 'no_such_host')) {
  if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {   &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
 # ---------------------------------------------------- see if the su-user exists          return OK;
     unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})      } else {
  eq 'no_host') {   my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
  &Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));     $form{'serverid'});
 # ------------------------------ see if the su-user is not too highly privileged          if ( $reply ne 'ok' ) {
  unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {              &failed($r,'Session could not be opened.',\%form);
 # -------------------------------------------------------- actually switch users      &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
     &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.      return OK;
  ' logging in as '.$form{'suname'});   }
     $form{'uname'}=$form{'suname'};      }
  } else {  
     &Apache::lonnet::logthis('Attempted switch user to privileged user');      if (!&Apache::lonnet::domain($form{'udom'})) {
  }          &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
     }          return OK;
  } else {      }
     &Apache::lonnet::logthis('Non-privileged user attempting switch user');  
  }      my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo);
     }      if ($rolestr) {
           $rolestr = &unescape($rolestr);
     my ($is_balancer,$otherserver) =       }
         &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'});      if ($symbstr) {
           $symbstr= &unescape($symbstr);
     if ($is_balancer) {      }
         if (!$otherserver) {       if ($iptokenstr) {
             ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});          $iptokenstr = &unescape($iptokenstr);
         }      }
         if ($otherserver) {      if ($rolestr =~ /^role=/) {
             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,          (undef,$form{'role'}) = split('=',$rolestr);
                      \%form);      }
     $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver.'&origurl='.$firsturl);      if ($symbstr =~ /^symb=/) {
         } else {          (undef,$form{'symb'}) = split('=',$symbstr);
             $r->print(&noswitch());      }
         }      if ($iptokenstr =~ /^iptoken=/) {
         return OK;          (undef,$form{'iptoken'}) = split('=',$iptokenstr);
     } else {      }
         if (!&check_can_host($r,\%form,$authhost)) {  
             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});      my $upass = $ENV{HTTPS} ? $form{'upass0'}
             if ($otherserver) {          : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,  
                          \%form);  # ---------------------------------------------------------------- Authenticate
                 $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver.'&origurl='.$firsturl);  
             } else {      my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
                 $r->print(&noswitch());      my ($cancreate,$statustocreate) =
             }          &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
             return OK;      my $defaultauth;
         }      if (ref($cancreate) eq 'ARRAY') {
           if (grep(/^login$/,@{$cancreate})) {
 # ------------------------------------------------------- Do the load balancing              $defaultauth = 1;
           }
 # ---------------------------------------------------------- Determine own load      }
         my $loadlim = $r->dir_config('lonLoadLim');      my $clientcancheckhost = 1;
         my $loadavg;      my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
         {                                                $form{'udom'},$defaultauth,
             my $loadfile=Apache::File->new('/proc/loadavg');                                                $clientcancheckhost);
             $loadavg=<$loadfile>;  
         }  # --------------------------------------------------------------------- Failed?
         $loadavg =~ s/\s.*//g;  
         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);      if ($authhost eq 'no_host') {
         my $userloadpercent=&Apache::lonnet::userload();   &failed($r,'Username and/or password could not be authenticated.',
    \%form);
 # ---------------------------------------------------------- Are we overloaded?          return OK;
         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {      } elsif ($authhost eq 'no_account_on_host') {
             my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});          if ($defaultauth) {
             if (!$unloaded) {              my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});              unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
             }                  return OK;
             if ($unloaded) {              }
                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',              my $start_page =
                          undef,\%form);                  &Apache::loncommon::start_page('Create a user account in LON-CAPA');
                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);              my $lonhost = $r->dir_config('lonHostID');
                 return OK;              my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
             }              my $contacts =
         }                  &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,                                                          $form{'udom'},$origmail);
                  \%form);              my ($contact_email) = split(',',$contacts);
         return OK;              my $output =
     }                  &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
 }                                                         $domdesc,'',$lonhost,
                                                          $contact_email,$contact_name,
 sub check_can_host {                                                         undef,$statustocreate);
     my ($r,$form,$authhost,$domdesc) = @_;              &Apache::loncommon::content_type($r,'text/html');
     return unless (ref($form) eq 'HASH');              $r->send_http_header;
     my $canhost = 1;              &Apache::createaccount::print_header($r,$start_page);
     my $lonhost = $r->dir_config('lonHostID');              $r->print('<h3>'.&mt('Account creation').'</h3>'.
     my $udom = $form->{'udom'};                        &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
     my @intdoms;                        $output.&Apache::loncommon::end_page());
     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);              return OK;
     if (ref($internet_names) eq 'ARRAY') {          } else {
         @intdoms = @{$internet_names};              &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 $uprimary_id = &Apache::lonnet::domain($udom,'primary');          }
     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);      }
     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {  
         my $machine_dom = &Apache::lonnet::host_domain($lonhost);      if (($firsturl eq '') ||
         my $hostname = &Apache::lonnet::hostname($lonhost);   ($firsturl=~/^\/adm\/(logout|remote)/)) {
         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);   $firsturl='/adm/roles';
         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);      }
         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);  
         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);      my $hosthere;
         my $loncaparev;      if ($form{'iptoken'}) {
         if ($authhost eq 'no_account_on_host') {          my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);          my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
         } else {          if (($sessiondata{'domain'} eq $form{'udom'}) &&
             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);              ($sessiondata{'username'} eq $form{'uname'})) {
         }              $hosthere = 1;
         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,          }
                                                      $udomdefaults{'remotesessions'},      }
                                                      $defdomdefaults{'hostedsessions'});  
     }  # --------------------------------- Are we attempting to login as somebody else?
     unless ($canhost) {      if ($form{'suname'}) {
         if ($authhost eq 'no_account_on_host') {          my ($suname,$sudom,$sudomref);
             my $checkloginvia = 1;          $suname = $form{'suname'};
             my ($login_host,$hostname) =           $sudom = $form{'udom'};
                 &Apache::lonnet::choose_server($udom,$checkloginvia);          if ($form{'sudom'}) {
             &Apache::loncommon::content_type($r,'text/html');              unless ($sudom eq $form{'sudom'}) {
             $r->send_http_header;                  if (&Apache::lonnet::domain($form{'sudom'})) {
             if ($login_host ne '') {                      $sudomref = [$form{'sudom'}];
                 my $protocol = $Apache::lonnet::protocol{$login_host};                      $sudom = $form{'sudom'};
                 $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>'.  # ------------ see if the original user has enough privileges to pull this stunt
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.   if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.  # ---------------------------------------------------- see if the su-user exists
                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').      unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
                           &Apache::loncommon::end_page());  # ------------------------------ see if the su-user is not too highly privileged
             } else {   if (&Apache::lonnet::privileged($suname,$sudom)) {
                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').                      &Apache::lonnet::logthis('Attempted switch user to privileged user');
                           '<h3>'.&mt('Account creation unavailable').'</h3>'.                  } else {
                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.                      my $noprivswitch;
                           '<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());  # su-user's home server and user's home server must have one of:
             }  # (a) same domain
         } else {  # (b) same primary library server for the two domains
             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,  # (c) same "internet domain" for primary library server(s) for home servers' domains
                      $form);  #
             my ($otherserver) = &Apache::lonnet::choose_server($udom);                      my $suprim = &Apache::lonnet::domain($sudom,'primary');
             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);                      my $suintdom = &Apache::lonnet::internet_dom($suprim);
         }                      unless ($sudom eq $form{'udom'}) {
     }                          my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
     return $canhost;                          my $uintdom = &Apache::lonnet::internet_dom($uprim);
 }                          unless ($suprim eq $uprim) {
                               unless ($suintdom eq $uintdom) {
 sub noswitch {                                  &Apache::lonnet::logthis('Attempted switch user '
     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').                                     .'to user with different "internet domain".');                        
                  '<h3>'.&mt('Session unavailable').'</h3>'.                                  $noprivswitch = 1;
                  &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;  
 }                      unless ($noprivswitch) {
   #
 1;  # server where log-in occurs must have same "internet domain" as su-user's home
 __END__  # server
   #
                           my $lonhost = $r->dir_config('lonHostID');
                           my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
                           if ($hostintdom ne $suintdom) {
                               &Apache::lonnet::logthis('Attempted switch user on a '
                                   .'server with a different "internet domain".');
                           } else {
   
   # -------------------------------------------------------- actually switch users
   
               &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
                                 $form{'udom'}.' logging in as '.$suname.':'.$sudom);
               $form{'uname'}=$suname;
                               if ($form{'udom'} ne $sudom) {
                                   $form{'udom'}=$sudom;
                               }
                           }
                       }
    }
       }
    } else {
       &Apache::lonnet::logthis('Non-privileged user attempting switch user');
    }
       }
   
       my ($is_balancer,$otherserver);
   
       unless ($hosthere) {
           ($is_balancer,$otherserver) =
               &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
           if ($is_balancer) {
               if ($otherserver eq '') {
                   my $lowest_load;
                   ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
                   if ($lowest_load > 100) {
                       $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});
                   }
               }
               if ($otherserver ne '') {
                   my @hosts = &Apache::lonnet::current_machine_ids();
                   if (grep(/^\Q$otherserver\E$/,@hosts)) {
                       $hosthere = $otherserver;
                   }
               }
           }
       }
   
       if (($is_balancer) && (!$hosthere)) {
           if ($otherserver) {
               &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                        \%form);
               my $switchto = '/adm/switchserver?otherserver='.$otherserver;
               if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
                   $switchto .= '&origurl='.$firsturl;
               }
               if ($form{'role'}) {
                   $switchto .= '&role='.$form{'role'};
               }
               if ($form{'symb'}) {
                   $switchto .= '&symb='.$form{'symb'};
               }
               $r->internal_redirect($switchto);
           } else {
               $r->print(&noswitch());
           }
           return OK;
       } else {
           if (!&check_can_host($r,\%form,$authhost)) {
               my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
               if ($otherserver) {
                   &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                            \%form);
                   my $switchto = '/adm/switchserver?otherserver='.$otherserver;
                   if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
                       $switchto .= '&origurl='.$firsturl;
                   }
                   if ($form{'role'}) {
                       $switchto .= '&role='.$form{'role'};
                   }
                   if ($form{'symb'}) {
                       $switchto .= '&symb='.$form{'symb'};
                   }
                   $r->internal_redirect($switchto);
               } else {
                   $r->print(&noswitch());
               }
               return OK;
           }
   
   # ------------------------------------------------------- Do the load balancing
   
   # ---------------------------------------------------------- Determine own load
           my $loadlim = $r->dir_config('lonLoadLim');
           my $loadavg;
           {
               my $loadfile=Apache::File->new('/proc/loadavg');
               $loadavg=<$loadfile>;
           }
           $loadavg =~ s/\s.*//g;
           my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
           my $userloadpercent=&Apache::lonnet::userload();
   
   # ---------------------------------------------------------- Are we overloaded?
           if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
               my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
               if (!$unloaded) {
                   ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
               }
               if ($unloaded) {
                   &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
                            undef,\%form);
                   $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
                   return OK;
               }
           }
           if (($is_balancer) && ($hosthere)) {
               $form{'noloadbalance'} = $hosthere;
           }
           &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
                    \%form);
           return OK;
       }
   }
   
   sub check_can_host {
       my ($r,$form,$authhost,$domdesc) = @_;
       return unless (ref($form) eq 'HASH');
       my $canhost = 1;
       my $lonhost = $r->dir_config('lonHostID');
       my $udom = $form->{'udom'};
       my @intdoms;
       my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
       if (ref($internet_names) eq 'ARRAY') {
           @intdoms = @{$internet_names};
       }
       my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
       my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
       unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
           my $machine_dom = &Apache::lonnet::host_domain($lonhost);
           my $hostname = &Apache::lonnet::hostname($lonhost);
           my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
           my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
           my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
           my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
           my $loncaparev;
           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  
changed lines
  Added in v.1.151


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