Diff for /loncom/auth/lonauth.pm between versions 1.121.2.18 and 1.145

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

Removed from v.1.121.2.18  
changed lines
  Added in v.1.145


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