File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.171: download - view: text, annotated - select for diffs
Wed Nov 24 20:15:15 2021 UTC (2 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754
  - When log-in fails for a user with authentication set to lti: let user
    know they should launch LON-CAPA session via a different CMS.

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

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