File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.121.2.17: download - view: text, annotated - select for diffs
Thu Aug 1 00:42:34 2019 UTC (4 years, 10 months ago) by raeburn
Branches: version_2_11_X
- For 2.11
  Backport 1.144, 1.145, 1.146, 1.147, 1.148

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

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