File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.121.2.24.2.6: download - view: text, annotated - select for diffs
Mon Jan 23 00:38:50 2023 UTC (15 months, 3 weeks ago) by raeburn
Branches: version_2_11_4_msu
- For 2.11.4 (modified)
  Include changes in 1.178

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: #
    4: # $Id: lonauth.pm,v 1.121.2.24.2.6 2023/01/23 00:38:50 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 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: use Digest::MD5;
   44: use CGI::Cookie();
   45:  
   46: # ------------------------------------------------------------ Successful login
   47: sub success {
   48:     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
   49: 	$form,$cid,$expirepub,$write_to_opener) = @_;
   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:     my $ip = &Apache::lonnet::get_requestor_ip();
   64:     &Apache::lonnet::log($domain,$username,$authhost,
   65:                          "Login $ip");
   66: 
   67: # ------------------------------------------------- Check for critical messages
   68: 
   69:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
   70:     if ($what[0]) {
   71: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
   72: 	    $lowerurl='/adm/email?critical=display';
   73:         }
   74:     }
   75: 
   76: # ------------------------------------------------------------ Get cookies ready
   77:     my ($securecookie,$defaultcookie);
   78:     my $ssl = $r->subprocess_env('https');
   79:     if ($ssl) {
   80:         $securecookie="lonSID=$cookie; path=/; HttpOnly; secure";
   81:         my $lonidsdir=$r->dir_config('lonIDsDir');
   82:         if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {
   83:             my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';
   84:             if (-e "$lonidsdir/$linkname.id") {
   85:                 unlink("$lonidsdir/$linkname.id");
   86:             }
   87:             my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",
   88:                                               "$lonidsdir/$linkname.id"); 1 };
   89:             if ($made_symlink) {
   90:                 $defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";
   91:                 &Apache::lonnet::appenv({'user.linkedenv' => $linkname});
   92:             }
   93:         }
   94:     } else {
   95:         $defaultcookie = "lonID=$cookie; path=/; HttpOnly;";
   96:     }
   97: # -------------------------------------------------------- Menu script and info
   98:     my $destination = $lowerurl;
   99: 
  100:     if (defined($form->{role})) {
  101:         my $envkey = 'user.role.'.$form->{role};
  102:         my $now=time;
  103:         my $then=$env{'user.login.time'};
  104:         my $refresh=$env{'user.refresh.time'};
  105:         my $update=$env{'user.update.time'};
  106:         if (!$update) {
  107:             $update = $then;
  108:         }
  109:         if (exists($env{$envkey})) {
  110:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
  111:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
  112:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
  113:             if ($tstatus eq 'is') {
  114:                 $destination  .= ($destination =~ /\?/) ? '&' : '?';
  115:                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
  116:                 $destination .= 'selectrole=1&'.$newrole.'=1';
  117:             }
  118:         }
  119:     } elsif (defined($form->{display})) {
  120:         if ($destination =~ m{^/adm/email($|\?)}) {
  121:             $destination  .= ($destination =~ /\?/) ? '&' : '?' .'display='.&escape($form->{display});
  122:         }
  123:     }
  124:     if (defined($form->{symb})) {
  125:         my $destsymb = $form->{symb};
  126:         my $encrypted;
  127:         if ($destsymb =~ m{^/enc/}) {
  128:             $encrypted = 1;
  129:             if ($cid) {
  130:                 $destsymb = &Apache::lonenc::unencrypted($destsymb,$cid);
  131:             }
  132:         }
  133:         $destination  .= ($destination =~ /\?/) ? '&' : '?';
  134:         if ($destsymb =~ /___/) {
  135:             my ($map,$resid,$desturl)=split(/___/,$destsymb);
  136:             $desturl = &Apache::lonnet::clutter($desturl);
  137:             if ($encrypted) {
  138:                 $desturl = &Apache::lonenc::encrypted($desturl,1,$cid);
  139:                 $destsymb = $form->{symb};
  140:             }
  141:             $desturl = &HTML::Entities::encode($desturl,'"<>&');
  142:             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
  143:             $destination .= 'destinationurl='.$desturl.
  144:                             '&destsymb='.$destsymb;
  145:         } elsif (!$encrypted) {
  146:             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
  147:             $destination .= 'destinationurl='.$destsymb;
  148:         }
  149:     }
  150:     if ($destination =~ m{^/adm/roles}) {
  151:         $destination  .= ($destination =~ /\?/) ? '&' : '?';
  152:         $destination .= 'source=login';
  153:     }
  154: 
  155:     my $brcrum = [{'href' => '',
  156:                    'text' => 'Successful Login'},];
  157:     my $args = {'no_inline_link' => 1,
  158:                 'bread_crumbs' => $brcrum,};
  159:     if (($env{'request.deeplink.login'} eq $lowerurl) &&
  160:         (($env{'request.linkprot'}) || ($env{'request.linkkey'} ne ''))) {
  161:         my %info;
  162:         if ($env{'request.linkprot'}) {
  163:             $info{'linkprot'} = $env{'request.linkprot'};
  164:             foreach my $item ('linkprotuser','linkprotexit') {
  165:                 if ($form->{$item}) {
  166:                     $info{$item} = $form->{$item};
  167:                 }
  168:             }
  169:             $args = {'only_body' => 1,};
  170:         } elsif ($env{'request.linkkey'} ne '') {
  171:             $info{'linkkey'} = $env{'request.linkkey'};
  172:         }
  173:         $info{'origurl'} = $lowerurl;
  174:         my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
  175:         unless (($token eq 'con_lost') || ($token eq 'refused') ||
  176:                 ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  177:             $destination .= (($destination =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
  178:         }
  179:     }
  180:     if ($env{'request.deeplink.login'}) {
  181:         if ($env{'environment.remote'} eq 'on') {
  182:             &Apache::lonnet::appenv({'environment.remote' => 'off'});
  183:         }
  184:     }
  185:     my $startupremote;
  186:     if ($write_to_opener) {
  187:         if ($env{'environment.remote'} eq 'on') {
  188:             &Apache::lonnet::appenv({'environment.remote' => 'off'});
  189:         }
  190:         $args->{'redirect'} = [0,$destination,'',$write_to_opener];
  191:     } else {
  192:         $startupremote=&Apache::lonmenu::startupremote($destination);
  193:     }
  194: 
  195:     my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});
  196:     my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
  197:     my $setflags=&Apache::lonmenu::setflags();
  198:     my $maincall=&Apache::lonmenu::maincall();
  199:     my $start_page=&Apache::loncommon::start_page('Successful Login',
  200:                                                   $startupremote,$args);
  201:     my $end_page  =&Apache::loncommon::end_page();
  202: 
  203:     my $continuelink;
  204:     if ($env{'environment.remote'} eq 'off') {
  205:         unless ($write_to_opener) {
  206: 	    $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
  207:         }
  208:     }
  209: # ------------------------------------------------- Output for successful login
  210: 
  211:     &Apache::loncommon::content_type($r,'text/html');
  212:     if ($securecookie) {
  213:         $r->headers_out->add('Set-cookie' => $securecookie);
  214:     }
  215:     if ($defaultcookie) {
  216:         $r->headers_out->add('Set-cookie' => $defaultcookie);
  217:     }
  218:     if ($expirepub) {
  219:         my $c = new CGI::Cookie(-name    => 'lonPubID',
  220:                                 -value   => '',
  221:                                 -expires => '-10y',);
  222:         $r->headers_out->add('Set-cookie' => $c);
  223:     }
  224:     $r->send_http_header;
  225: 
  226:     if ($env{'request.linkprot'}) {
  227:         $r->print(<<END);
  228: $start_page
  229: <br />$continuelink
  230: $end_page
  231: END
  232:     } else {
  233:         my %lt=&Apache::lonlocal::texthash(
  234: 				           'wel' => 'Welcome',
  235: 				           'pro' => 'Login problems?',
  236: 				          );
  237:         my $loginhelp = &loginhelpdisplay($domain);
  238:         if ($loginhelp) {
  239:             $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
  240:         }
  241: 
  242:         my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>'); 
  243:         $r->print(<<ENDSUCCESS);
  244: $start_page
  245: $setflags
  246: $windowinfo
  247: <h1>$lt{'wel'}</h1>
  248: $welcome
  249: $loginhelp
  250: $remoteinfo
  251: $maincall
  252: $continuelink
  253: $end_page
  254: ENDSUCCESS
  255:     }
  256:     return;
  257: }
  258: 
  259: # --------------------------------------------------------------- Failed login!
  260: 
  261: sub failed {
  262:     my ($r,$message,$form,$authhost) = @_;
  263:     (undef,undef,undef,my $clientmathml,my $clientunicode) =
  264:         &Apache::loncommon::decode_user_agent();
  265:     my $args = {};
  266:     if ($clientunicode && !$clientmathml) {
  267:         $args = {'browser.unicode' => 1};
  268:     }
  269:     if ($form->{firsturl} =~ m{^/tiny/$match_domain/\w+$}) {
  270:         if ($form->{linkprot}) {
  271:             $args->{only_body} = 1;
  272:         }
  273:     }
  274: 
  275:     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
  276:     my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
  277:     my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
  278:     if (&Apache::lonnet::domain($udom,'description') eq '') {
  279:         undef($udom);
  280:     }
  281:     my $retry = '/adm/login';
  282:     if ($uname eq $form->{'uname'}) {
  283:         $retry .= '?username='.$uname;
  284:     }
  285:     if ($udom) {
  286:         $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
  287:     }
  288:     my $lonhost = $r->dir_config('lonHostID');
  289:     my $querystr;
  290:     my $result = &set_retry_token($form,$lonhost,\$querystr);
  291:     if ($result eq 'fail') {
  292:         if (exists($form->{role})) {
  293:             my $role = &Apache::loncommon::cleanup_html($form->{role});
  294:             if ($role ne '') {
  295:                 $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
  296:             }
  297:         }
  298:         if (exists($form->{symb})) {
  299:             my $symb = &Apache::loncommon::cleanup_html($form->{symb});
  300:             if ($symb ne '') {
  301:                 $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
  302:             }
  303:         }
  304:         if (exists($form->{firsturl})) {
  305:             my $firsturl = &Apache::loncommon::cleanup_html($form->{firsturl});
  306:             if ($firsturl ne '') {
  307:                 $retry .= (($retry=~/\?/)?'&amp;':'?').'firsturl='.$firsturl;
  308:                 if ($form->{firsturl} =~ m{^/tiny/$match_domain/\w+$}) {
  309:                     unless (exists($form->{linkprot})) {
  310:                         if (exists($form->{linkkey})) {
  311:                             $retry .= 'linkkey='.$form->{linkkey};
  312:                         }
  313:                     }
  314:                 }
  315:             }
  316:         }
  317:         if (exists($form->{linkprot})) {
  318:             my %info = (
  319:                          'linkprot' => $form->{'linkprot'},
  320:                        );
  321:             foreach my $item ('linkprotuser','linkprotexit') {
  322:                 if ($form->{$item} ne '') {
  323:                     $info{$item} = $form->{$item};
  324:                 }
  325:             }
  326:             my $ltoken = &Apache::lonnet::tmpput(\%info,
  327:                                                  $r->dir_config('lonHostID'),'retry');
  328:             if ($ltoken) {
  329:                 $retry .= (($retry =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;
  330:             }
  331:         }
  332:     } elsif ($querystr ne '') {
  333:         $retry .= (($retry=~/\?/)?'&amp;':'?').$querystr;
  334:     }
  335:     my $end_page = &Apache::loncommon::end_page();
  336:     &Apache::loncommon::content_type($r,'text/html');
  337:     $r->send_http_header;
  338:     my @actions =
  339:          (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
  340:     my $loginhelp = &loginhelpdisplay($udom);
  341:     if ($loginhelp) {
  342:         push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
  343:     }
  344:     #FIXME: link to helpdesk might be added here
  345: 
  346:     $r->print(
  347:        $start_page
  348:       .'<h2>'.&mt('Sorry ...').'</h2>'
  349:       .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
  350:       .&Apache::lonhtmlcommon::actionbox(\@actions)
  351:       .$end_page
  352:     );
  353:  }
  354: 
  355: # ------------------------------------------------------------------ Rerouting!
  356: 
  357: sub reroute {
  358:     my ($r) = @_;
  359:     &Apache::loncommon::content_type($r,'text/html');
  360:     $r->send_http_header;
  361:     my $msg='<b>'.&mt('Sorry ...').'</b><br />'
  362:            .&mt('Please [_1]log in again[_2].');
  363:     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
  364: }
  365: 
  366: # ---------------------------------------------------------------- Main handler
  367: 
  368: sub handler {
  369:     my $r = shift;
  370:     my $londocroot = $r->dir_config('lonDocRoot');
  371: # Are we re-routing?
  372:     if (-e "$londocroot/lon-status/reroute.txt") {
  373: 	&reroute($r);
  374: 	return OK;
  375:     }
  376: 
  377:     &Apache::lonlocal::get_language_handle($r);
  378: 
  379: # -------------------------------- Prevent users from attempting to login twice
  380:     my $handle = &Apache::lonnet::check_for_valid_session($r);
  381:     if ($handle ne '') {
  382:         my $lonidsdir=$r->dir_config('lonIDsDir');
  383:         if ($handle=~/^publicuser\_/) {
  384: # For "public user" - remove it, we apparently really want to login
  385:             unlink($r->dir_config('lonIDsDir')."/$handle.id");
  386:         } else {
  387: # Indeed, a valid token is found
  388:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  389: 	    &Apache::loncommon::content_type($r,'text/html');
  390: 	    $r->send_http_header;
  391: 	    my $start_page = 
  392: 	        &Apache::loncommon::start_page('Already logged in');
  393: 	    my $end_page = 
  394: 	        &Apache::loncommon::end_page();
  395:             my $dest = '/adm/roles';
  396:             my %form = &get_form_items($r);
  397:             if ($form{'logtoken'}) {
  398:                 my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
  399:                                                      $form{'serverid'});
  400:                 unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
  401:                         ($tmpinfo eq 'no_such_host')) {
  402:                     my ($des_key,$firsturl,@rest)=split(/&/,$tmpinfo);
  403:                     $firsturl = &unescape($firsturl);
  404:                     my %info;
  405:                     foreach my $item (@rest) {
  406:                         my ($key,$value) = split(/=/,$item);
  407:                         $info{$key} = &unescape($value);
  408:                     }
  409:                     if ($firsturl ne '') {
  410:                         $info{'firsturl'} = $firsturl;
  411:                         $dest = $firsturl;
  412:                         my $relogin;
  413:                         if ($dest =~ m{^/tiny/$match_domain/\w+$}) {
  414:                             if ($env{'request.course.id'}) {
  415:                                 my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  416:                                 my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  417:                                 my $symb = &Apache::loncommon::symb_from_tinyurl($dest,$cnum,$cdom);
  418:                                 if ($symb) {
  419:                                     unless (&set_deeplink_login(%info) eq 'ok') {
  420:                                         $relogin = 1;
  421:                                     }
  422:                                 }
  423:                             }
  424:                             if ($relogin) {
  425:                                 $r->print(
  426:                                       $start_page
  427:                                      .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  428:                                      .'<p>'.&mt('Please [_1]log out[_2] first, and then try your access again',
  429:                                                 '<a href="/adm/logout">','</a>')
  430:                                      .'</p>'
  431:                                      .$end_page);
  432:                             } else {
  433:                                 if (($info{'linkprot'}) || ($info{'linkkey'} ne '')) {
  434:                                     if (($info{'linkprot'}) && ($info{'linkprotuser'} ne '')) {
  435:                                         unless ($info{'linkprotuser'} eq $env{'user.name'}.':'.$env{'user.domain'}) {
  436:                                             $r->print(
  437:                                                       $start_page
  438:                                                       .'<p class="LC_warning">'.&mt('You are already logged in, but as a different user from the one expected for the link you followed from another system').'</p>'
  439:                                                       .'<p>'.&mt('Please [_1]log out[_2] first, and then try following the link again from the other system',
  440:                                                                  '<a href="/adm/logout">','</a>')
  441: 
  442:                                                       .'</p>'
  443:                                                       .$end_page);
  444:                                             return OK;
  445:                                         }
  446:                                     }
  447:                                     my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'link');
  448:                                     unless (($token eq 'con_lost') || ($token eq 'refused') ||
  449:                                             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  450:                                         $dest .= (($dest =~ /\?/) ? '&' : '?') . 'ttoken='.$token;
  451:                                     }
  452:                                 }
  453:                                 $r->print(
  454:                                       $start_page
  455:                                      .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  456:                                      .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4] first, and then try your access again',
  457:                                                 '<a href="'.$dest.'">','</a>',
  458:                                                 '<a href="/adm/logout">','</a>')
  459:                                      .'</p>'
  460:                                      .$end_page);
  461:                             }
  462:                             return OK;
  463:                         }
  464:                     }
  465:                 }
  466:             }
  467:             $r->print(
  468:                $start_page
  469:               .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  470:               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
  471:                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
  472:               .'</p>'
  473:               .$end_page
  474:             );
  475:             return OK;
  476:         }
  477:     }
  478: 
  479: # ---------------------------------------------------- No valid token, continue
  480: 
  481:     my %form = &get_form_items($r);
  482:     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
  483: 	&failed($r,'Username, password and domain need to be specified.',
  484: 		\%form);
  485:         return OK;
  486:     }
  487: 
  488: # split user logging in and "su"-user
  489: 
  490:     ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
  491:     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
  492:     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
  493:     $form{'udom'}  = &LONCAPA::clean_domain($form{'udom'});
  494:     $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
  495: 
  496:     my $role   = $r->dir_config('lonRole');
  497:     my $domain = $r->dir_config('lonDefDomain');
  498:     my $prodir = $r->dir_config('lonUsersDir');
  499:     my $contact_name = &mt('LON-CAPA helpdesk');
  500: 
  501: # ---------------------------------------- Get the information from login token
  502: 
  503:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
  504:                                       $form{'serverid'});
  505: 
  506:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') || 
  507:         ($tmpinfo eq 'no_such_host')) {
  508: 	&failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
  509:         return OK;
  510:     } else {
  511: 	my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
  512: 					   $form{'serverid'});
  513:         if ( $reply ne 'ok' ) {
  514:             &failed($r,'Session could not be opened.',\%form);
  515: 	    &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
  516: 	    return OK;
  517: 	}
  518:     }
  519: 
  520:     if (!&Apache::lonnet::domain($form{'udom'})) {
  521:         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
  522:         return OK;
  523:     }
  524: 
  525:     my ($des_key,$firsturl,@rest)=split(/&/,$tmpinfo);
  526:     $firsturl = &unescape($firsturl);
  527:     foreach my $item (@rest) {
  528:         my ($key,$value) = split(/=/,$item);
  529:         $form{$key} = &unescape($value);
  530:     }
  531:     if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
  532:         $form{'firsturl'} = $firsturl;
  533:     }
  534:     my $upass = &Apache::loncommon::des_decrypt($des_key,$form{'upass0'});
  535: 
  536: # ---------------------------------------------------------------- Authenticate
  537: 
  538:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
  539:     my ($cancreate,$statustocreate) =
  540:         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
  541:     my $defaultauth;
  542:     if (ref($cancreate) eq 'ARRAY') {
  543:         if (grep(/^login$/,@{$cancreate})) {
  544:             $defaultauth = 1;
  545:         }
  546:     }
  547:     my $clientcancheckhost = 1;
  548:     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
  549:                                               $form{'udom'},$defaultauth,
  550:                                               $clientcancheckhost);
  551:     
  552: # --------------------------------------------------------------------- Failed?
  553: 
  554:     if ($authhost eq 'no_host') {
  555:         my $pwdverify;
  556:         if (&Apache::lonnet::homeserver($form{'uname'},$form{'udom'}) eq 'no_host') {
  557:             my %possunames = &alternate_unames_check($form{'uname'},$form{'udom'});
  558:             if (keys(%possunames) > 0) {
  559:                 foreach my $rulematch (keys(%possunames)) {
  560:                     my $possuname = $possunames{$rulematch};
  561:                     if (($possuname ne '') && ($possuname =~ /^$match_username$/)) {
  562:                         $authhost=Apache::lonnet::authenticate($possuname,$upass,
  563:                                                                $form{'udom'},undef,
  564:                                                                $clientcancheckhost);
  565:                         if (($authhost eq 'no_host') || ($authhost eq 'no_account_on_host')) {
  566:                             next;
  567:                         } elsif (($authhost ne '') && (&Apache::lonnet::hostname($authhost) ne '')) {
  568:                             $pwdverify = 1;
  569:                             &Apache::lonnet::logthis("Authenticated user: $possuname was submitted as: $form{'uname'}");
  570:                             $form{'uname'} = $possuname;
  571:                             last;
  572:                         }
  573:                     }
  574:                 }
  575:             }
  576:         }
  577:         unless ($pwdverify) {
  578:             &failed($r,'Username and/or password could not be authenticated.',
  579:                     \%form);
  580:             return OK;
  581:         }
  582:     } elsif ($authhost eq 'no_account_on_host') {
  583:         if ($defaultauth) {
  584:             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
  585:             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
  586:                 return OK;
  587:             }
  588:             my $start_page = 
  589:                 &Apache::loncommon::start_page('Create a user account in LON-CAPA',
  590:                                                '',{'no_inline_link'   => 1,});
  591:             my $lonhost = $r->dir_config('lonHostID');
  592:             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  593:             my $contacts = 
  594:                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
  595:                                                         $form{'udom'},$origmail);
  596:             my ($contact_email) = split(',',$contacts); 
  597:             my $output = 
  598:                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
  599:                                                        $domdesc,'',$lonhost,
  600:                                                        $contact_email,$contact_name,
  601:                                                        undef,$statustocreate);
  602:             &Apache::loncommon::content_type($r,'text/html');
  603:             $r->send_http_header;
  604:             &Apache::createaccount::print_header($r,$start_page);
  605:             $r->print('<h3>'.&mt('Account creation').'</h3>'.
  606:                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
  607:                       $output.&Apache::loncommon::end_page());
  608:             return OK;
  609:         } else {
  610:             &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);
  611:             return OK;
  612:         }
  613:     }
  614: 
  615:     if (($firsturl eq '') || 
  616: 	($firsturl=~/^\/adm\/(logout|remote)/)) {
  617: 	$firsturl='/adm/roles';
  618:     }
  619: 
  620:     my ($hosthere,%sessiondata);
  621:     if ($form{'iptoken'}) {
  622:         %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  623:         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  624:         if (($sessiondata{'domain'} eq $form{'udom'}) &&
  625:             ($sessiondata{'username'} eq $form{'uname'})) {
  626:             $hosthere = 1;
  627:         }
  628:     }
  629: 
  630: # --------------------------------- Are we attempting to login as somebody else?
  631:     if ($form{'suname'}) {
  632:         my ($suname,$sudom,$sudomref);
  633:         $suname = $form{'suname'};
  634:         $sudom = $form{'udom'};
  635:         if ($form{'sudom'}) {
  636:             unless ($sudom eq $form{'sudom'}) {
  637:                 if (&Apache::lonnet::domain($form{'sudom'})) {
  638:                     $sudomref = [$form{'sudom'}];
  639:                     $sudom = $form{'sudom'};
  640:                 }
  641:             }
  642:         }
  643: # ------------ see if the original user has enough privileges to pull this stunt
  644: 	if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
  645: # ---------------------------------------------------- see if the su-user exists
  646: 	    unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
  647: # ------------------------------ see if the su-user is not too highly privileged
  648: 		if (&Apache::lonnet::privileged($suname,$sudom)) {
  649:                     &Apache::lonnet::logthis('Attempted switch user to privileged user');
  650:                 } else {
  651:                     my $noprivswitch;
  652: #
  653: # su-user's home server and user's home server must have one of:
  654: # (a) same domain
  655: # (b) same primary library server for the two domains
  656: # (c) same "internet domain" for primary library server(s) for home servers' domains
  657: #
  658:                     my $suprim = &Apache::lonnet::domain($sudom,'primary');
  659:                     my $suintdom = &Apache::lonnet::internet_dom($suprim);
  660:                     unless ($sudom eq $form{'udom'}) {
  661:                         my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
  662:                         my $uintdom = &Apache::lonnet::internet_dom($uprim);
  663:                         unless ($suprim eq $uprim) {
  664:                             unless ($suintdom eq $uintdom) {
  665:                                 &Apache::lonnet::logthis('Attempted switch user '
  666:                                    .'to user with different "internet domain".');
  667:                                 $noprivswitch = 1;
  668:                             }
  669:                         }
  670:                     }
  671: 
  672:                     unless ($noprivswitch) {
  673: #
  674: # server where log-in occurs must have same "internet domain" as su-user's home
  675: # server
  676: #
  677:                         my $lonhost = $r->dir_config('lonHostID');
  678:                         my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
  679:                         if ($hostintdom ne $suintdom) {
  680:                             &Apache::lonnet::logthis('Attempted switch user on a '
  681:                                 .'server with a different "internet domain".');
  682:                         } else {
  683: 
  684: # -------------------------------------------------------- actually switch users
  685: 
  686: 			    &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
  687: 				$form{'udom'}.' logging in as '.$suname.':'.$sudom);
  688: 			    $form{'uname'}=$suname;
  689:                             if ($form{'udom'} ne $sudom) {
  690:                                 $form{'udom'}=$sudom;
  691:                             }
  692:                         }
  693:                     }
  694: 		}
  695: 	    }
  696: 	} else {
  697: 	    &Apache::lonnet::logthis('Non-privileged user attempting switch user');
  698: 	}
  699:     }
  700: 
  701:     if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  702:         if (($form{'linkprot'}) && ($form{'linkprotuser'} ne '')) {
  703:             unless($form{'linkprotuser'} eq $form{'uname'}.':'.$form{'udom'}) {
  704:                 delete($form{'udom'});
  705:                 delete($form{'uname'});
  706:                 &failed($r,'Username and/or domain are different to that expected for the link you followed from another system',
  707:                         \%form,$authhost);
  708:                 return OK;
  709:             }
  710:         }
  711:     }
  712: 
  713:     my ($is_balancer,$otherserver);
  714: 
  715:     unless ($hosthere) {
  716:         ($is_balancer,$otherserver) =
  717:             &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
  718:         if ($is_balancer) {
  719:             # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  720:             my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  721:             if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
  722:                 $otherserver = $found_server;
  723:             }
  724:             if ($otherserver eq '') {
  725:                 my $lowest_load;
  726:                 ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
  727:                 if ($lowest_load > 100) {
  728:                     $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});
  729:                 }
  730:             }
  731:             if ($otherserver ne '') {
  732:                 my @hosts = &Apache::lonnet::current_machine_ids();
  733:                 if (grep(/^\Q$otherserver\E$/,@hosts)) {
  734:                     $hosthere = $otherserver;
  735:                 }
  736:             }
  737:         }
  738:     }
  739: 
  740:     if (($is_balancer) && (!$hosthere)) {
  741:         if ($otherserver) {
  742:             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
  743:                      \%form);
  744:             my $switchto = '/adm/switchserver?otherserver='.$otherserver;
  745:             if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
  746:                 $switchto .= '&origurl='.$firsturl;
  747:             }
  748:             if ($form{'role'}) {
  749:                 $switchto .= '&role='.$form{'role'};
  750:             }
  751:             if ($form{'symb'}) {
  752:                 $switchto .= '&symb='.$form{'symb'};
  753:             }
  754:             if ($form{'linkprot'}) {
  755:                 $env{'request.linkprot'} = $form{'linkprot'};
  756:                 foreach my $item ('linkprotuser','linkprotexit') {
  757:                     if ($form{$item}) {
  758:                         $env{'request.'.$item} = $form{$item};
  759:                     }
  760:                 }
  761:             } elsif ($form{'linkkey'} ne '') {
  762:                 $env{'request.linkkey'} = $form{'linkkey'};
  763:             }
  764:             if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  765:                 &set_deeplink_login(%form);
  766:             } elsif ($firsturl eq '/adm/email') {
  767:                 if ($form{'display'} && ($form{'mailrecip'} eq "$form{'uname'}:$form{'udom'}")) {
  768:                     $env{'request.display'} = $form{'display'};
  769:                     $env{'request.mailrecip'} = $form{'mailrecip'};
  770:                 }
  771:             }
  772:             $r->internal_redirect($switchto);
  773:         } else {
  774:             &Apache::loncommon::content_type($r,'text/html');
  775:             $r->send_http_header;
  776:             $r->print(&noswitch());
  777:         }
  778:         return OK;
  779:     } else {
  780:         if (!&check_can_host($r,\%form,$authhost)) {
  781:             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
  782:             if ($otherserver) {
  783:                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
  784:                          \%form);
  785:                 my $switchto = '/adm/switchserver?otherserver='.$otherserver;
  786:                 if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
  787:                     $switchto .= '&origurl='.$firsturl;
  788:                 }
  789:                 if ($form{'role'}) {
  790:                     $switchto .= '&role='.$form{'role'};
  791:                 }
  792:                 if ($form{'symb'}) {
  793:                     $switchto .= '&symb='.$form{'symb'};
  794:                 }
  795:                 if ($form{'linkprot'}) {
  796:                     $env{'request.linkprot'} = $form{'linkprot'};
  797:                     foreach my $item ('linkprotuser','linkprotexit') {
  798:                         if ($form{$item}) {
  799:                             $env{'request.'.$item} = $form{$item};
  800:                         }
  801:                     }
  802:                 } elsif ($form{'linkkey'} ne '') {
  803:                     $env{'request.linkkey'} = $form{'linkkey'};
  804:                 }
  805:                 if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  806:                     &set_deeplink_login(%form);
  807:                 } elsif ($firsturl eq '/adm/email') {
  808:                     if ($form{'display'} && ($form{'mailrecip'} eq "$form{'uname'}:$form{'udom'}")) {
  809:                         $env{'request.display'} = $form{'display'};
  810:                         $env{'request.mailrecip'} = $form{'mailrecip'};
  811:                     }
  812:                 }
  813:                 $r->internal_redirect($switchto);
  814:             } else {
  815:                 &Apache::loncommon::content_type($r,'text/html');
  816:                 $r->send_http_header;
  817:                 $r->print(&noswitch());
  818:             }
  819:             return OK;
  820:         }
  821: 
  822: # ------------------------------------------------------- Do the load balancing
  823: 
  824: # ---------------------------------------------------------- Determine own load
  825:         my $loadlim = $r->dir_config('lonLoadLim');
  826:         my $loadavg;
  827:         {
  828:             my $loadfile=Apache::File->new('/proc/loadavg');
  829:             $loadavg=<$loadfile>;
  830:         }
  831:         $loadavg =~ s/\s.*//g;
  832:         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
  833:         my $userloadpercent=&Apache::lonnet::userload();
  834: 
  835: # ---------------------------------------------------------- Are we overloaded?
  836:         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
  837:             my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});
  838:             if (!$unloaded) {
  839:                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
  840:             }
  841:             if ($unloaded) {
  842:                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
  843:                          undef,\%form);
  844:                 if ($form{'linkprot'}) {
  845:                     $env{'request.linkprot'} = $form{'linkprot'};
  846:                 } elsif ($form{'linkkey'} ne '') {
  847:                     $env{'request.linkkey'} = $form{'linkkey'};
  848:                 }
  849:                 if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  850:                     &set_deeplink_login(%form);
  851:                 } elsif ($firsturl eq '/adm/email') {
  852:                     if ($form{'display'} && ($form{'mailrecip'} eq "$form{'uname'}:$form{'udom'}")) {
  853:                         $env{'request.display'} = $form{'display'};
  854:                         $env{'request.mailrecip'} = $form{'mailrecip'};
  855:                     }
  856:                 }
  857:                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
  858:                 return OK;
  859:             }
  860:         }
  861:         if (($is_balancer) && ($hosthere)) {
  862:             $form{'noloadbalance'} = $hosthere;
  863:         }
  864:         my $extra_env;
  865:         if (($hosthere) && ($sessiondata{'sessionserver'} ne '')) {
  866:             if ($sessiondata{'origurl'} ne '') {
  867:                 $firsturl = $sessiondata{'origurl'};
  868:                 $form{'firsturl'} = $sessiondata{'origurl'};
  869:                 my @names = ('role','symb','linkprot','linkkey');
  870:                 foreach my $item (@names) {
  871:                     if ($sessiondata{$item} ne '') {
  872:                         $form{$item} = $sessiondata{$item};
  873:                     }
  874:                 }
  875:                 if ($sessiondata{'origurl'} eq '/adm/email') {
  876:                     if (($sessiondata{'display'}) && ($sessiondata{'mailrecip'})) {
  877:                         if (&unescape($sessiondata{'mailrecip'}) eq "$form{'uname'}:$form{'udom'}") {
  878:                             $form{'display'} = &unescape($sessiondata{'display'});
  879:                             $form{'mailrecip'} = &unescape($sessiondata{'mailrecip'});
  880:                         }
  881:                     }
  882:                 }
  883:             }
  884:         }
  885:         if ($form{'linkprot'}) {
  886:             my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);
  887:             if ($linkprotector) {
  888:                 $extra_env = {'user.linkprotector' => $linkprotector,
  889:                               'user.linkproturi'   => $uri};
  890:             }
  891:         } elsif ($form{'linkkey'} ne '') {
  892:             $extra_env = {'user.deeplinkkey'  => $form{'linkkey'},
  893:                           'user.keyedlinkuri' => $form{'firsturl'}};
  894:         }
  895:         if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  896:             &set_deeplink_login(%form);
  897:             if ($form{'linkprot'}) {
  898:                 if (ref($extra_env) eq 'HASH') {
  899:                     %{$extra_env} = ( %{$extra_env}, 'request.linkprot' => $form{'linkprot'} );
  900:                 } else {
  901:                     $extra_env = {'request.linkprot' => $form{'linkprot'}};
  902:                 }
  903:                 if ($form{'linkprotexit'}) {
  904:                     $extra_env->{'request.linkprotexit'} = $form{'linkprotexit'};
  905:                 }
  906:             } elsif ($form{'linkkey'} ne '') {
  907:                 if (ref($extra_env) eq 'HASH') {
  908:                     %{$extra_env} = ( %{$extra_env}, 'request.linkkey' => $form{'linkkey'} );
  909:                 } else {
  910:                     $extra_env = {'request.linkkey' => $form{'linkkey'}};
  911:                 }
  912:             }
  913:             if ($env{'request.deeplink.login'}) {
  914:                 if (ref($extra_env) eq 'HASH') {
  915:                     %{$extra_env} = ( %{$extra_env}, 'request.deeplink.login' => $form{'firsturl'} );
  916:                 } else {
  917:                     $extra_env = {'request.deeplink.login' => $form{'firsturl'}};
  918:                 }
  919:             }
  920:         }
  921:         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,
  922:                  \%form);
  923:         return OK;
  924:     }
  925: }
  926: 
  927: sub get_form_items {
  928:     my ($r) = @_;
  929:     my $buffer;
  930:     if ($r->header_in('Content-length') > 0) {
  931:         $r->read($buffer,$r->header_in('Content-length'),0);
  932:     }
  933:     my %form;
  934:     foreach my $pair (split(/&/,$buffer)) {
  935:        my ($name,$value) = split(/=/,$pair);
  936:        $value =~ tr/+/ /;
  937:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  938:        $form{$name}=$value;
  939:     }
  940:     return %form;
  941: }
  942: 
  943: sub set_deeplink_login {
  944:     my (%form) = @_;
  945:     my $disallow;
  946:     if ($form{'firsturl'} =~ m{^/tiny/($match_domain)/\w+$}) {
  947:         my $cdom = $1;
  948:         my ($cnum,$symb) = &Apache::loncommon::symb_from_tinyurl($form{'firsturl'},'',$cdom);
  949:         if ($symb) {
  950:             if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
  951:                 my $deeplink;
  952:                 if ($symb =~ /\.(page|sequence)$/) {
  953:                     my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($symb))[2]);
  954:                     my $navmap = Apache::lonnavmaps::navmap->new();
  955:                     if (ref($navmap)) {
  956:                         $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');
  957:                     }
  958:                 } else {
  959:                     $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$symb);
  960:                 }
  961:                 if ($deeplink ne '') {
  962:                     my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
  963:                     if (($protect ne 'none') && ($protect ne '')) {
  964:                         my ($acctype,$item) = split(/:/,$protect);
  965:                         if ($acctype =~ /lti(c|d)$/) {
  966:                             unless ($form{'linkprot'} eq $item.$1.':'.$env{'request.deeplink.login'}) {
  967:                                 $disallow = 1;
  968:                             }
  969:                         } elsif ($acctype eq 'key') {
  970:                             unless ($form{'linkkey'} eq $item) {
  971:                                 $disallow = 1;
  972:                             }
  973:                         }
  974:                     }
  975:                 }
  976:                 unless ($disallow) {
  977:                     $env{'request.deeplink.login'} = $form{'firsturl'};
  978:                 }
  979:             } else {
  980:                 $env{'request.deeplink.login'} = $form{'firsturl'};
  981:             }
  982:         }
  983:     }
  984:     if ($disallow) {
  985:         return;
  986:     }
  987:     return 'ok';
  988: }
  989: 
  990: sub set_retry_token {
  991:     my ($form,$lonhost,$querystr) = @_;
  992:     if (ref($form) eq 'HASH') {
  993:         my ($firsturl,$token,$extras,@names);
  994:         @names = ('role','symb','linkprotuser','linkprotexit','linkprot','linkkey','iptoken');
  995:         foreach my $name (@names) {
  996:             if ($form->{$name} ne '') {
  997:                 $extras .= '&'.$name.'='.&escape($form->{$name});
  998:                 last if ($name eq 'linkprot');
  999:             }
 1000:         }
 1001:         my $firsturl = $form->{'firsturl'};
 1002:         if (($firsturl ne '') || ($extras ne '')) {
 1003:             $extras .= ':retry';
 1004:             $token = &Apache::lonnet::reply('tmpput:'.&escape($firsturl).
 1005:                                             $extras,$lonhost);
 1006:             if (($token eq 'con_lost') || ($token eq 'no_such_host')) {
 1007:                 return 'fail';
 1008:             } else {
 1009:                 if (ref($querystr)) {
 1010:                     $$querystr = 'retry='.$token;
 1011:                 }
 1012:                 return 'ok';
 1013:             }
 1014:         }
 1015:     }
 1016:     return;
 1017: }
 1018: 
 1019: sub check_can_host {
 1020:     my ($r,$form,$authhost,$domdesc) = @_;
 1021:     return unless (ref($form) eq 'HASH');
 1022:     my $canhost = 1;
 1023:     my $lonhost = $r->dir_config('lonHostID');
 1024:     my $udom = $form->{'udom'};
 1025:     my @intdoms;
 1026:     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
 1027:     if (ref($internet_names) eq 'ARRAY') {
 1028:         @intdoms = @{$internet_names};
 1029:     }
 1030:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
 1031:     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 1032:     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
 1033:         my $machine_dom = &Apache::lonnet::host_domain($lonhost);
 1034:         my $hostname = &Apache::lonnet::hostname($lonhost);
 1035:         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
 1036:         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
 1037:         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
 1038:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
 1039:         my $loncaparev;
 1040:         if ($authhost eq 'no_account_on_host') {
 1041:             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
 1042:         } else {
 1043:             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
 1044:         }
 1045:         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
 1046:                                                      $udomdefaults{'remotesessions'},
 1047:                                                      $defdomdefaults{'hostedsessions'});
 1048:     }
 1049:     unless ($canhost) {
 1050:         if ($authhost eq 'no_account_on_host') {
 1051:             my $checkloginvia = 1;
 1052:             my ($login_host,$hostname) = 
 1053:                 &Apache::lonnet::choose_server($udom,$checkloginvia);
 1054:             &Apache::loncommon::content_type($r,'text/html');
 1055:             $r->send_http_header;
 1056:             if ($login_host ne '') {
 1057:                 my $protocol = $Apache::lonnet::protocol{$login_host};
 1058:                 $protocol = 'http' if ($protocol ne 'https');
 1059:                 my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);
 1060:                 $hostname = $alias if ($alias ne '');
 1061:                 my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
 1062: #FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host
 1063:                 $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
 1064:                           '<h3>'.&mt('Account creation').'</h3>'.
 1065:                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
 1066:                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
 1067:                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
 1068:                           &Apache::loncommon::end_page());
 1069:             } else {
 1070:                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
 1071:                           '<h3>'.&mt('Account creation unavailable').'</h3>'.
 1072:                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
 1073:                           '<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>'.
 1074:                           &Apache::loncommon::end_page());
 1075:             }
 1076:         } else {
 1077:             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
 1078:                      $form);
 1079:             if ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
 1080:                 $env{'request.deeplink.login'} = $form->{'firsturl'};
 1081:             } elsif ($form->{'firsturl'} eq '/adm/email') {
 1082:                 if ($form->{'display'} && ($form->{'mailrecip'} eq $form->{'uname'}.':'.$form->{'udom'})) {
 1083:                     $env{'request.display'} = $form->{'mailrecip'};
 1084:                     $env{'request.mailrecip'} = $form->{'mailrecip'};
 1085:                 }
 1086:             }
 1087:             if ($form->{'linkprot'}) {
 1088:                 $env{'request.linkprot'} = $form->{'linkprot'};
 1089:             } elsif ($form->{'linkkey'} ne '') {
 1090:                 $env{'request.linkkey'} = $form->{'linkkey'};
 1091:             }
 1092:             my ($otherserver) = &Apache::lonnet::choose_server($udom);
 1093:             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
 1094:         }
 1095:     }
 1096:     return $canhost;
 1097: }
 1098: 
 1099: sub noswitch {
 1100:     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
 1101:                  '<h3>'.&mt('Session unavailable').'</h3>'.
 1102:                  &mt('This LON-CAPA server is unable to host your session.').'<br />'.
 1103:                  '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
 1104:                  &Apache::loncommon::end_page();
 1105:     return $result;
 1106: }
 1107: 
 1108: sub loginhelpdisplay {
 1109:     my ($authdomain) = @_;
 1110:     my $login_help = 1;
 1111:     my $lang = &Apache::lonlocal::current_language();
 1112:     if ($login_help) {
 1113:         my $dom = $authdomain;
 1114:         if ($dom eq '') {
 1115:             $dom = &Apache::lonnet::default_login_domain();
 1116:         }
 1117:         my %domconfhash = &Apache::loncommon::get_domainconf($dom);
 1118:         my $loginhelp_url;
 1119:         if ($lang) {
 1120:             $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
 1121:             if ($loginhelp_url ne '') {
 1122:                 return $loginhelp_url;
 1123:             }
 1124:         }
 1125:         $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
 1126:         if ($loginhelp_url ne '') {
 1127:             return $loginhelp_url;
 1128:         } else {
 1129:             return '/adm/loginproblems.html';
 1130:         }
 1131:     }
 1132:     return;
 1133: }
 1134: 
 1135: sub alternate_unames_check {
 1136:     my ($uname,$udom) = @_;
 1137:     my %possunames;
 1138:     my %domdefs = &Apache::lonnet::get_domain_defaults($udom);
 1139:     if (ref($domdefs{'unamemap_rule'}) eq 'ARRAY') {
 1140:         if (@{$domdefs{'unamemap_rule'}} > 0) {
 1141:             %possunames =
 1142:                 &Apache::lonnet::inst_rulecheck($udom,$uname,undef,
 1143:                                                 'unamemap',$domdefs{'unamemap_rule'});
 1144:         }
 1145:     }
 1146:     return %possunames;
 1147: }
 1148: 
 1149: 1;
 1150: __END__
 1151: 
 1152: 

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