File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.140: download - view: text, annotated - select for diffs
Sat Apr 2 04:31:33 2016 UTC (8 years, 3 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6808. New course container -- "Placement" for Placement Tests.
  Work in progress.

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: #
    4: # $Id: lonauth.pm,v 1.140 2016/04/02 04:31:33 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonauth;
   30: 
   31: use strict;
   32: use LONCAPA qw(:DEFAULT :match);
   33: use Apache::Constants qw(:common);
   34: use CGI qw(:standard);
   35: use DynaLoader; # for Crypt::DES version
   36: use Crypt::DES;
   37: use Apache::loncommon();
   38: use Apache::lonnet;
   39: use Apache::lonmenu();
   40: use Apache::createaccount;
   41: use Fcntl qw(:flock);
   42: use Apache::lonlocal;
   43: use Apache::File();
   44: use HTML::Entities;
   45:  
   46: # ------------------------------------------------------------ Successful login
   47: sub success {
   48:     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
   49: 	$form) = @_;
   50: 
   51: # ------------------------------------------------------------ Get cookie ready
   52:     my $cookie =
   53: 	&Apache::loncommon::init_user_environment($r, $username, $domain,
   54: 						  $authhost, $form,
   55: 						  {'extra_env' => $extra_env,});
   56: 
   57:     my $public=($username eq 'public' && $domain eq 'public');
   58: 
   59:     if ($public or $lowerurl eq 'noredirect') { return $cookie; }
   60: 
   61: # -------------------------------------------------------------------- Log this
   62: 
   63:     &Apache::lonnet::log($domain,$username,$authhost,
   64:                          "Login $ENV{'REMOTE_ADDR'}");
   65: 
   66: # ------------------------------------------------- Check for critical messages
   67: 
   68:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
   69:     if ($what[0]) {
   70: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
   71: 	    $lowerurl='/adm/email?critical=display';
   72:         }
   73:     }
   74: 
   75: # ------------------------------------------------------------ Get cookie ready
   76:     $cookie="lonID=$cookie; path=/";
   77: # -------------------------------------------------------- Menu script and info
   78:     my $destination = $lowerurl;
   79: 
   80:     if (defined($form->{role})) {
   81:         my $envkey = 'user.role.'.$form->{role};
   82:         my $now=time;
   83:         my $then=$env{'user.login.time'};
   84:         my $refresh=$env{'user.refresh.time'};
   85:         my $update=$env{'user.update.time'};
   86:         if (!$update) {
   87:             $update = $then;
   88:         }
   89:         if (exists($env{$envkey})) {
   90:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
   91:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
   92:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
   93:             if ($tstatus eq 'is') {
   94:                 $destination  .= ($destination =~ /\?/) ? '&' : '?';
   95:                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
   96:                 $destination .= 'selectrole=1&'.$newrole.'=1';
   97:             }
   98:         }
   99:     }
  100:     if (defined($form->{symb})) {
  101:         my $destsymb = $form->{symb};
  102:         $destination  .= ($destination =~ /\?/) ? '&' : '?';
  103:         if ($destsymb =~ /___/) {
  104:             # FIXME Need to deal with encrypted symbs and urls as needed.
  105:             my ($map,$resid,$desturl)=split(/___/,$destsymb);
  106:             $desturl = &Apache::lonnet::clutter($desturl);
  107:             $desturl = &HTML::Entities::encode($desturl,'"<>&');
  108:             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
  109:             $destination .= 'destinationurl='.$desturl.
  110:                             '&destsymb='.$destsymb;
  111:         } else {
  112:             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
  113:             $destination .= 'destinationurl='.$destsymb;
  114:         }
  115:     }
  116:     if ($destination =~ m{^/adm/roles}) {
  117:         $destination  .= ($destination =~ /\?/) ? '&' : '?';
  118:         $destination .= 'source=login';
  119:     }
  120: 
  121:     my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="loncapaclient";');
  122:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
  123:     my $brcrum = [{'href' => '',
  124:                    'text' => 'Successful Login'},];
  125:     my $args = {'bread_crumbs' => $brcrum,};
  126:     unless ((defined($form->{role})) || (defined($form->{symb}))) {
  127:         my $update=$env{'user.update.time'};
  128:         if (!$update) {
  129:             $update = $env{'user.login.time'};
  130:         }
  131:         my %roles_in_env;
  132:         my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
  133:         if ($showcount == 1) {
  134:             foreach my $rolecode (keys(%roles_in_env)) {
  135:                 my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
  136:                 if ($cid) {
  137:                     my %coursedescription =
  138:                         &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
  139:                     if ($coursedescription{'type'} eq 'Placement') {
  140:                         $args->{'crstype'} = 'Placement';
  141:                     }
  142:                     last;
  143:                 }
  144:             }
  145:         }
  146:     }
  147:     my $start_page=&Apache::loncommon::start_page('Successful Login',
  148:                                                   $header,$args);
  149:     my $end_page  =&Apache::loncommon::end_page();
  150: 
  151: 	my $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
  152: # ------------------------------------------------- Output for successful login
  153: 
  154:     &Apache::loncommon::content_type($r,'text/html');
  155:     $r->header_out('Set-cookie' => $cookie);
  156:     $r->send_http_header;
  157: 
  158:     my %lt=&Apache::lonlocal::texthash(
  159: 				       'wel' => 'Welcome',
  160: 				       'pro' => 'Login problems?',
  161: 				       );
  162:     my $loginhelp = &loginhelpdisplay($domain);
  163:     if ($loginhelp) {
  164:         $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
  165:     }
  166: 
  167:     my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>'); 
  168:     $r->print(<<ENDSUCCESS);
  169: $start_page
  170: $windowinfo
  171: <h1>$lt{'wel'}</h1>
  172: $welcome
  173: $loginhelp
  174: $continuelink
  175: $end_page
  176: ENDSUCCESS
  177:     return;
  178: }
  179: 
  180: # --------------------------------------------------------------- Failed login!
  181: 
  182: sub failed {
  183:     my ($r,$message,$form) = @_;
  184:     (undef,undef,undef,my $clientmathml,my $clientunicode) =
  185:         &Apache::loncommon::decode_user_agent();
  186:     my $args = {};
  187:     if ($clientunicode && !$clientmathml) {
  188:         $args = {'browser.unicode' => 1};
  189:     }
  190: 
  191:     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
  192:     my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
  193:     my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
  194:     if (&Apache::lonnet::domain($udom,'description') eq '') {
  195:         undef($udom);
  196:     }
  197:     my $retry = '/adm/login';
  198:     if ($uname eq $form->{'uname'}) {
  199:         $retry .= '?username='.$uname;
  200:     }
  201:     if ($udom) {
  202:         $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
  203:     }
  204:     if (exists($form->{role})) {
  205:         my $role = &Apache::loncommon::cleanup_html($form->{role});
  206:         if ($role ne '') {
  207:             $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
  208:         }
  209:     }
  210:     if (exists($form->{symb})) {
  211:         my $symb = &Apache::loncommon::cleanup_html($form->{symb});
  212:         if ($symb ne '') {
  213:             $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
  214:         }
  215:     }
  216:     my $end_page = &Apache::loncommon::end_page();
  217:     &Apache::loncommon::content_type($r,'text/html');
  218:     $r->send_http_header;
  219:     my @actions =
  220:          (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
  221:     my $loginhelp = &loginhelpdisplay($udom);
  222:     if ($loginhelp) {
  223:         push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
  224:     }
  225:     #FIXME: link to helpdesk might be added here
  226: 
  227:     $r->print(
  228:        $start_page
  229:       .'<h2>'.&mt('Sorry ...').'</h2>'
  230:       .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
  231:       .&Apache::lonhtmlcommon::actionbox(\@actions)
  232:       .$end_page
  233:     );
  234:  }
  235: 
  236: # ------------------------------------------------------------------ Rerouting!
  237: 
  238: sub reroute {
  239:     my ($r) = @_;
  240:     &Apache::loncommon::content_type($r,'text/html');
  241:     $r->send_http_header;
  242:     my $msg='<b>'.&mt('Sorry ...').'</b><br />'
  243:            .&mt('Please [_1]log in again[_2].');
  244:     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
  245: }
  246: 
  247: # ---------------------------------------------------------------- Main handler
  248: 
  249: sub handler {
  250:     my $r = shift;
  251:     my $londocroot = $r->dir_config('lonDocRoot');
  252:     my $form;
  253: # Are we re-routing?
  254:     if (-e "$londocroot/lon-status/reroute.txt") {
  255: 	&reroute($r);
  256: 	return OK;
  257:     }
  258: 
  259:     &Apache::lonlocal::get_language_handle($r);
  260: 
  261: # -------------------------------- Prevent users from attempting to login twice
  262:     my $handle = &Apache::lonnet::check_for_valid_session($r);
  263:     if ($handle ne '') {
  264:         my $lonidsdir=$r->dir_config('lonIDsDir');
  265:         if ($handle=~/^publicuser\_/) {
  266: # For "public user" - remove it, we apparently really want to login
  267:             unlink($r->dir_config('lonIDsDir')."/$handle.id");
  268:         } else {
  269: # Indeed, a valid token is found
  270:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  271: 	    &Apache::loncommon::content_type($r,'text/html');
  272: 	    $r->send_http_header;
  273: 	    my $start_page = 
  274: 	        &Apache::loncommon::start_page('Already logged in');
  275: 	    my $end_page = 
  276: 	        &Apache::loncommon::end_page();
  277:             my $dest = '/adm/roles';
  278:             if ($env{'form.firsturl'} ne '') {
  279:                 $dest = $env{'form.firsturl'};
  280:             }
  281:             $r->print(
  282:                $start_page
  283:               .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  284:               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
  285:                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
  286:               .'</p>'
  287:               .$end_page
  288:             );
  289:             return OK;
  290:         }
  291:     }
  292: 
  293: # ---------------------------------------------------- No valid token, continue
  294: 
  295: 
  296:     my $buffer;
  297:     if ($r->header_in('Content-length') > 0) {
  298: 	$r->read($buffer,$r->header_in('Content-length'),0);
  299:     }
  300:     my %form;
  301:     foreach my $pair (split(/&/,$buffer)) {
  302:        my ($name,$value) = split(/=/,$pair);
  303:        $value =~ tr/+/ /;
  304:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  305:        $form{$name}=$value;
  306:     }
  307: 
  308:     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
  309: 	&failed($r,'Username, password and domain need to be specified.',
  310: 		\%form);
  311:         return OK;
  312:     }
  313: 
  314: # split user logging in and "su"-user
  315: 
  316:     ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
  317:     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
  318:     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
  319:     $form{'udom'}  = &LONCAPA::clean_domain(  $form{'udom'});
  320: 
  321:     my $role   = $r->dir_config('lonRole');
  322:     my $domain = $r->dir_config('lonDefDomain');
  323:     my $prodir = $r->dir_config('lonUsersDir');
  324:     my $contact_name = &mt('LON-CAPA helpdesk');
  325: 
  326: # ---------------------------------------- Get the information from login token
  327: 
  328:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
  329:                                       $form{'serverid'});
  330: 
  331:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') || 
  332:         ($tmpinfo eq 'no_such_host')) {
  333: 	&failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
  334:         return OK;
  335:     } else {
  336: 	my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
  337: 					   $form{'serverid'});
  338:         if ( $reply ne 'ok' ) {
  339:             &failed($r,'Session could not be opened.',\%form);
  340: 	    &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
  341: 	    return OK;
  342: 	}
  343:     }
  344: 
  345:     if (!&Apache::lonnet::domain($form{'udom'})) {
  346:         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
  347:         return OK;
  348:     }
  349: 
  350:     my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo);
  351:     if ($rolestr) {
  352:         $rolestr = &unescape($rolestr);
  353:     }
  354:     if ($symbstr) {
  355:         $symbstr= &unescape($symbstr);
  356:     }
  357:     if ($iptokenstr) {
  358:         $iptokenstr = &unescape($iptokenstr);
  359:     }
  360:     if ($rolestr =~ /^role=/) {
  361:         (undef,$form{'role'}) = split('=',$rolestr);
  362:     }
  363:     if ($symbstr =~ /^symb=/) { 
  364:         (undef,$form{'symb'}) = split('=',$symbstr);
  365:     }
  366:     if ($iptokenstr =~ /^iptoken=/) {
  367:         (undef,$form{'iptoken'}) = split('=',$iptokenstr);
  368:     }
  369: 
  370:     my $upass = $ENV{HTTPS} ? $form{'upass0'} 
  371:         : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
  372: 
  373: # ---------------------------------------------------------------- Authenticate
  374: 
  375:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
  376:     my ($cancreate,$statustocreate) =
  377:         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
  378:     my $defaultauth;
  379:     if (ref($cancreate) eq 'ARRAY') {
  380:         if (grep(/^login$/,@{$cancreate})) {
  381:             $defaultauth = 1;
  382:         }
  383:     }
  384:     my $clientcancheckhost = 1;
  385:     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
  386:                                               $form{'udom'},$defaultauth,
  387:                                               $clientcancheckhost);
  388:     
  389: # --------------------------------------------------------------------- Failed?
  390: 
  391:     if ($authhost eq 'no_host') {
  392: 	&failed($r,'Username and/or password could not be authenticated.',
  393: 		\%form);
  394:         return OK;
  395:     } elsif ($authhost eq 'no_account_on_host') {
  396:         if ($defaultauth) {
  397:             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
  398:             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
  399:                 return OK;
  400:             }
  401:             my $start_page = 
  402:                 &Apache::loncommon::start_page('Create a user account in LON-CAPA');
  403:             my $lonhost = $r->dir_config('lonHostID');
  404:             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  405:             my $contacts = 
  406:                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
  407:                                                         $form{'udom'},$origmail);
  408:             my ($contact_email) = split(',',$contacts); 
  409:             my $output = 
  410:                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
  411:                                                        $domdesc,'',$lonhost,
  412:                                                        $contact_email,$contact_name,
  413:                                                        undef,$statustocreate);
  414:             &Apache::loncommon::content_type($r,'text/html');
  415:             $r->send_http_header;
  416:             &Apache::createaccount::print_header($r,$start_page);
  417:             $r->print('<h3>'.&mt('Account creation').'</h3>'.
  418:                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
  419:                       $output.&Apache::loncommon::end_page());
  420:             return OK;
  421:         } else {
  422:             &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);
  423:             return OK;
  424:         }
  425:     }
  426: 
  427:     if (($firsturl eq '') || 
  428: 	($firsturl=~/^\/adm\/(logout|remote)/)) {
  429: 	$firsturl='/adm/roles';
  430:     }
  431: 
  432:     my $hosthere;
  433:     if ($form{'iptoken'}) {
  434:         my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  435:         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  436:         if (($sessiondata{'domain'} eq $form{'udom'}) &&
  437:             ($sessiondata{'username'} eq $form{'uname'})) {
  438:             $hosthere = 1;
  439:         }
  440:     }
  441: 
  442: # --------------------------------- Are we attempting to login as somebody else?
  443:     if ($form{'suname'}) {
  444: # ------------ see if the original user has enough privileges to pull this stunt
  445: 	if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
  446: # ---------------------------------------------------- see if the su-user exists
  447: 	    unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
  448: 		eq 'no_host') {
  449: 		&Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
  450: # ------------------------------ see if the su-user is not too highly privileged
  451: 		unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
  452: # -------------------------------------------------------- actually switch users
  453: 		    &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
  454: 			' logging in as '.$form{'suname'});
  455: 		    $form{'uname'}=$form{'suname'};
  456: 		} else {
  457: 		    &Apache::lonnet::logthis('Attempted switch user to privileged user');
  458: 		}
  459: 	    }
  460: 	} else {
  461: 	    &Apache::lonnet::logthis('Non-privileged user attempting switch user');
  462: 	}
  463:     }
  464: 
  465:     my ($is_balancer,$otherserver);
  466: 
  467:     unless ($hosthere) {
  468:         ($is_balancer,$otherserver) =
  469:             &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'});
  470:     }
  471: 
  472:     if ($is_balancer) {
  473:         if (!$otherserver) { 
  474:             ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
  475:         }
  476:         if ($otherserver) {
  477:             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
  478:                      \%form);
  479:             my $switchto = '/adm/switchserver?otherserver='.$otherserver;
  480:             if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
  481:                 $switchto .= '&origurl='.$firsturl;
  482:             }
  483:             if ($form{'role'}) {
  484:                 $switchto .= '&role='.$form{'role'};
  485:             }
  486:             if ($form{'symb'}) {
  487:                 $switchto .= '&symb='.$form{'symb'};
  488:             }
  489:             $r->internal_redirect($switchto);
  490:         } else {
  491:             $r->print(&noswitch());
  492:         }
  493:         return OK;
  494:     } else {
  495:         if (!&check_can_host($r,\%form,$authhost)) {
  496:             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
  497:             if ($otherserver) {
  498:                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
  499:                          \%form);
  500:                 my $switchto = '/adm/switchserver?otherserver='.$otherserver;
  501:                 if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
  502:                     $switchto .= '&origurl='.$firsturl;
  503:                 }
  504:                 if ($form{'role'}) {
  505:                     $switchto .= '&role='.$form{'role'};
  506:                 }
  507:                 if ($form{'symb'}) {
  508:                     $switchto .= '&symb='.$form{'symb'};
  509:                 }
  510:                 $r->internal_redirect($switchto);
  511:             } else {
  512:                 $r->print(&noswitch());
  513:             }
  514:             return OK;
  515:         }
  516: 
  517: # ------------------------------------------------------- Do the load balancing
  518: 
  519: # ---------------------------------------------------------- Determine own load
  520:         my $loadlim = $r->dir_config('lonLoadLim');
  521:         my $loadavg;
  522:         {
  523:             my $loadfile=Apache::File->new('/proc/loadavg');
  524:             $loadavg=<$loadfile>;
  525:         }
  526:         $loadavg =~ s/\s.*//g;
  527:         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
  528:         my $userloadpercent=&Apache::lonnet::userload();
  529: 
  530: # ---------------------------------------------------------- Are we overloaded?
  531:         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
  532:             my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
  533:             if (!$unloaded) {
  534:                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
  535:             }
  536:             if ($unloaded) {
  537:                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
  538:                          undef,\%form);
  539:                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
  540:                 return OK;
  541:             }
  542:         }
  543:         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
  544:                  \%form);
  545:         return OK;
  546:     }
  547: }
  548: 
  549: sub check_can_host {
  550:     my ($r,$form,$authhost,$domdesc) = @_;
  551:     return unless (ref($form) eq 'HASH');
  552:     my $canhost = 1;
  553:     my $lonhost = $r->dir_config('lonHostID');
  554:     my $udom = $form->{'udom'};
  555:     my @intdoms;
  556:     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
  557:     if (ref($internet_names) eq 'ARRAY') {
  558:         @intdoms = @{$internet_names};
  559:     }
  560:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
  561:     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
  562:     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
  563:         my $machine_dom = &Apache::lonnet::host_domain($lonhost);
  564:         my $hostname = &Apache::lonnet::hostname($lonhost);
  565:         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
  566:         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
  567:         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
  568:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
  569:         my $loncaparev;
  570:         if ($authhost eq 'no_account_on_host') {
  571:             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
  572:         } else {
  573:             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
  574:         }
  575:         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
  576:                                                      $udomdefaults{'remotesessions'},
  577:                                                      $defdomdefaults{'hostedsessions'});
  578:     }
  579:     unless ($canhost) {
  580:         if ($authhost eq 'no_account_on_host') {
  581:             my $checkloginvia = 1;
  582:             my ($login_host,$hostname) = 
  583:                 &Apache::lonnet::choose_server($udom,$checkloginvia);
  584:             &Apache::loncommon::content_type($r,'text/html');
  585:             $r->send_http_header;
  586:             if ($login_host ne '') {
  587:                 my $protocol = $Apache::lonnet::protocol{$login_host};
  588:                 $protocol = 'http' if ($protocol ne 'https');
  589:                 my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
  590:                 $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
  591:                           '<h3>'.&mt('Account creation').'</h3>'.
  592:                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
  593:                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
  594:                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
  595:                           &Apache::loncommon::end_page());
  596:             } else {
  597:                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
  598:                           '<h3>'.&mt('Account creation unavailable').'</h3>'.
  599:                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
  600:                           '<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>'.
  601:                           &Apache::loncommon::end_page());
  602:             }
  603:         } else {
  604:             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
  605:                      $form);
  606:             my ($otherserver) = &Apache::lonnet::choose_server($udom);
  607:             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
  608:         }
  609:     }
  610:     return $canhost;
  611: }
  612: 
  613: sub noswitch {
  614:     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
  615:                  '<h3>'.&mt('Session unavailable').'</h3>'.
  616:                  &mt('This LON-CAPA server is unable to host your session.').'<br />'.
  617:                  '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
  618:                  &Apache::loncommon::end_page();
  619:     return $result;
  620: }
  621: 
  622: sub loginhelpdisplay {
  623:     my ($authdomain) = @_;
  624:     my $login_help = 1;
  625:     my $lang = &Apache::lonlocal::current_language();
  626:     if ($login_help) {
  627:         my $dom = $authdomain;
  628:         if ($dom eq '') {
  629:             $dom = &Apache::lonnet::default_login_domain();
  630:         }
  631:         my %domconfhash = &Apache::loncommon::get_domainconf($dom);
  632:         my $loginhelp_url;
  633:         if ($lang) {
  634:             $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
  635:             if ($loginhelp_url ne '') {
  636:                 return $loginhelp_url;
  637:             }
  638:         }
  639:         $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
  640:         if ($loginhelp_url ne '') {
  641:             return $loginhelp_url;
  642:         } else {
  643:             return '/adm/loginproblems.html';
  644:         }
  645:     }
  646:     return;
  647: }
  648: 
  649: 1;
  650: __END__
  651: 
  652: 

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