File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.204: download - view: text, annotated - select for diffs
Sat Sep 17 23:38:50 2022 UTC (19 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support access to specific LON-CAPA message after login in cases where a
  LON-CAPA loadbalancer node is used as the portal for a domain.

    1: # The LearningOnline Network
    2: # Login Screen
    3: #
    4: # $Id: lonlogin.pm,v 1.204 2022/09/17 23: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::lonlogin;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::File ();
   34: use Apache::lonnet;
   35: use Apache::loncommon();
   36: use Apache::lonauth();
   37: use Apache::lonlocal;
   38: use Apache::migrateuser();
   39: use lib '/home/httpd/lib/perl/';
   40: use LONCAPA qw(:DEFAULT :match);
   41: use URI::Escape;
   42: use HTML::Entities();
   43: use CGI::Cookie();
   44:  
   45: sub handler {
   46:     my $r = shift;
   47: 
   48:     &Apache::loncommon::get_unprocessed_cgi
   49: 	(join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'},
   50: 	      $ENV{'REDIRECT_QUERY_STRING'}),
   51: 	 ['interface','username','domain','firsturl','localpath','localres',
   52: 	  'token','role','symb','iptoken','btoken','ltoken','ttoken','linkkey',
   53:           'saml','sso','retry','display']); 
   54: 
   55: # -- check if they are a migrating user
   56:     if (defined($env{'form.token'})) {
   57:         return &Apache::migrateuser::handler($r);
   58:     }
   59: 
   60:     my $lonhost = $r->dir_config('lonHostID');
   61:     if ($env{'form.ttoken'}) {
   62:         my %info = &Apache::lonnet::tmpget($env{'form.ttoken'});
   63:         &Apache::lonnet::tmpdel($env{'form.ttoken'});
   64:         if ($info{'origurl'}) {
   65:             $env{'form.firsturl'} = $info{'origurl'};
   66:         }
   67:         if ($info{'ltoken'}) {
   68:             $env{'form.ltoken'} = $info{'ltoken'};
   69:         } elsif ($info{'linkprot'}) {
   70:             $env{'form.linkprot'} = $info{'linkprot'};
   71:             foreach my $item ('linkprotuser','linkprotexit') {
   72:                 if ($info{$item} ne '') {
   73:                     $env{'form.'.$item} = $info{$item};
   74:                 }
   75:             }
   76:         } elsif ($info{'linkkey'} ne '') {
   77:             $env{'form.linkkey'} = $info{'linkkey'};
   78:         }
   79:     } elsif (($env{'form.sso'}) || ($env{'form.retry'})) {
   80:         my $infotoken;
   81:         if ($env{'form.sso'}) {
   82:             $infotoken = $env{'form.sso'};
   83:         } else {
   84:             $infotoken = $env{'form.retry'};
   85:         }
   86:         my $data = &Apache::lonnet::reply('tmpget:'.$infotoken,$lonhost);
   87:         unless (($data=~/^error/) || ($data eq 'con_lost') ||
   88:                 ($data eq 'no_such_host')) {
   89:             my %info = &decode_token($data);
   90:             foreach my $item (keys(%info)) {
   91:                 $env{'form.'.$item} = $info{$item};
   92:             }
   93:             &Apache::lonnet::tmpdel($infotoken);
   94:         }
   95:     } else {
   96:         if (!defined($env{'form.firsturl'})) {
   97:             &Apache::lonacc::get_posted_cgi($r,['firsturl']);
   98:         }
   99:         if (!defined($env{'form.firsturl'})) {
  100:             if ($ENV{'REDIRECT_URL'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) {
  101:                 $env{'form.firsturl'} = $ENV{'REDIRECT_URL'};
  102:             }
  103:         }
  104:         if (($env{'form.firsturl'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) &&
  105:             (!$env{'form.ltoken'}) && (!$env{'form.linkprot'}) && (!$env{'form.linkkey'})) {
  106:             &Apache::lonacc::get_posted_cgi($r,['linkkey']);
  107:         }
  108:         if ($env{'form.firsturl'} eq '/adm/logout') {
  109:             delete($env{'form.firsturl'});
  110:         }
  111:     }
  112: 
  113: # For "public user" - remove any exising "public" cookie, as user really wants to log-in
  114:     my ($handle,$lonidsdir,$expirepub,$userdom);
  115:     $lonidsdir=$r->dir_config('lonIDsDir');
  116:     unless ($r->header_only) {
  117:         $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom);
  118:         if ($handle ne '') {
  119:             if ($handle=~/^publicuser\_/) {
  120:                 unlink($r->dir_config('lonIDsDir')."/$handle.id");
  121:                 undef($handle);
  122:                 undef($userdom);
  123:                 $expirepub = 1;
  124:             }
  125:         }
  126:     }
  127: 
  128:     &Apache::loncommon::no_cache($r);
  129:     &Apache::lonlocal::get_language_handle($r);
  130:     &Apache::loncommon::content_type($r,'text/html');
  131:     if ($expirepub) {
  132:         my $c = new CGI::Cookie(-name    => 'lonPubID',
  133:                                 -value   => '',
  134:                                 -expires => '-10y',);
  135:         $r->header_out('Set-cookie' => $c);
  136:     } elsif (($handle eq '') && ($userdom ne '')) {
  137:         my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  138:         foreach my $name (keys(%cookies)) {
  139:             next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
  140:             my $c = new CGI::Cookie(-name    => $name,
  141:                                     -value   => '',
  142:                                     -expires => '-10y',);
  143:             $r->headers_out->add('Set-cookie' => $c);
  144:         }
  145:     }
  146:     $r->send_http_header;
  147:     return OK if $r->header_only;
  148: 
  149: 
  150: # Are we re-routing?
  151:     my $londocroot = $r->dir_config('lonDocRoot'); 
  152:     if (-e "$londocroot/lon-status/reroute.txt") {
  153: 	&Apache::lonauth::reroute($r);
  154: 	return OK;
  155:     }
  156: 
  157: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  158: 
  159:     my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
  160:     if ($found_server) {
  161:         my $hostname = &Apache::lonnet::hostname($found_server);
  162:         if ($hostname ne '') {
  163:             my $protocol = $Apache::lonnet::protocol{$found_server};
  164:             $protocol = 'http' if ($protocol ne 'https');
  165:             my $dest = '/adm/roles';
  166:             if ($env{'form.firsturl'} ne '') {
  167:                 $dest = &HTML::Entities::encode($env{'form.firsturl'},'\'"<>&');
  168:             }
  169:             my %info = (
  170:                          balcookie => $lonhost.':'.$balancer_cookie,
  171:                        );
  172:             if ($env{'form.role'}) {
  173:                 $info{'role'} = $env{'form.role'};
  174:             }
  175:             if ($env{'form.symb'}) {
  176:                 $info{'symb'} = $env{'form.symb'};
  177:             }
  178:             if (($env{'form.firsturl'} eq '/adm/email') && ($env{'form.display'} ne '')) {
  179:                 if ($env{'form.sso'}) {
  180:                     if ($env{'form.mailrecip'}) {
  181:                         $info{'display'} = &escape($env{'form.display'});
  182:                         $info{'mailrecip'} = &escape($env{'form.mailrecip'});
  183:                     }
  184:                 } else {
  185:                     if (($env{'form.username'}) && ($env{'form.domain'})) {
  186:                         $info{'display'} = &escape($env{'form.display'});
  187:                         $info{'mailrecipient'} = &escape($env{'form.username'}.':'.$env{'form.domain'});
  188:                     }
  189:                 }
  190:             }
  191:             my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server);
  192:             unless (($balancer_token eq 'con_lost') || ($balancer_token eq 'refused') ||
  193:                     ($balancer_token eq 'unknown_cmd') || ($balancer_token eq 'no_such_host')) {
  194:                 $dest .=  (($dest=~/\?/)?'&amp;':'?') . 'btoken='.$balancer_token;
  195:             }
  196:             if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  197:                 my %link_info;
  198:                 if ($env{'form.ltoken'}) {
  199:                     $link_info{'ltoken'} = $env{'form.ltoken'};
  200:                 } elsif ($env{'form.linkprot'}) {
  201:                     $link_info{'linkprot'} = $env{'form.linkprot'};
  202:                     foreach my $item ('linkprotuser','linkprotexit') {
  203:                         if ($env{'form.'.$item} ne '') {
  204:                             $link_info{$item} = $env{'form.'.$item};
  205:                         }
  206:                     }
  207:                 } elsif ($env{'form.linkkey'} ne '') {
  208:                     $link_info{'linkkey'} = $env{'form.linkkey'};
  209:                 }
  210:                 if (keys(%link_info)) {
  211:                     $link_info{'origurl'} = $env{'form.firsturl'};
  212:                     my $token = &Apache::lonnet::tmpput(\%link_info,$found_server,'link');
  213:                     unless (($token eq 'con_lost') || ($token eq 'refused') ||
  214:                             ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  215:                         $dest .=  (($dest=~/\?/)?'&amp;':'?') . 'ttoken='.$token;
  216:                     }
  217:                 }
  218:             }
  219:             unless ($found_server eq $lonhost) {
  220:                 my $alias = &Apache::lonnet::use_proxy_alias($r,$found_server);
  221:                 $hostname = $alias if ($alias ne '');
  222:             }
  223:             my $url = $protocol.'://'.$hostname.$dest;
  224:             my $start_page =
  225:                 &Apache::loncommon::start_page('Switching Server ...',undef,
  226:                                                {'redirect'       => [0,$url],});
  227:             my $end_page   = &Apache::loncommon::end_page();
  228:             $r->print($start_page.$end_page);
  229:             return OK;
  230:         }
  231:     }
  232: 
  233: #
  234: # Check if a LON-CAPA load balancer sent user here because user's browser sent
  235: # it a balancer cookie for an active session on this server.
  236: #
  237: 
  238:     my $balcookie;
  239:     if ($env{'form.btoken'}) {
  240:         my %info = &Apache::lonnet::tmpget($env{'form.btoken'});
  241:         $balcookie = $info{'balcookie'};
  242:         &Apache::lonnet::tmpdel($env{'form.btoken'});
  243:         delete($env{'form.btoken'});
  244:         if (($env{'form.firsturl'} eq '/adm/email') &&
  245:             (exists($info{'display'})) && (exists($info{'mailrecip'}))) {
  246:             $env{'form.display'} = &unescape($info{'display'});
  247:             $env{'form.mailrecip'} = &unescape($info{'mailrecip'});
  248:         }
  249:     }
  250: 
  251: #
  252: # If browser sent an old cookie for which the session file had been removed
  253: # check if configuration for user's domain has a portal URL set.  If so
  254: # switch user's log-in to the portal.
  255: #
  256: 
  257:     if (($handle eq '') && ($userdom ne '')) {
  258:         my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom);
  259:         if ($domdefaults{'portal_def'} =~ /^https?\:/) {
  260:             my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
  261:                                           {'redirect' => [0,$domdefaults{'portal_def'}],});
  262:             my $end_page   = &Apache::loncommon::end_page();
  263:             $r->print($start_page.$end_page);
  264:             return OK;
  265:         }
  266:     }
  267: 
  268: # -------------------------------- Prevent users from attempting to login twice
  269:     if ($handle ne '') {
  270:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  271: 	my $start_page = 
  272: 	    &Apache::loncommon::start_page('Already logged in');
  273: 	my $end_page = 
  274: 	    &Apache::loncommon::end_page();
  275:         my $dest = '/adm/roles';
  276:         if ($env{'form.firsturl'} ne '') {
  277:             $dest = &HTML::Entities::encode($env{'form.firsturl'},'\'"<>&');
  278:         }
  279:         if (($env{'form.ltoken'}) || ($env{'form.linkprot'})) {
  280:             my ($linkprot,$linkprotuser,$linkprotexit);
  281:             if ($env{'form.ltoken'}) {
  282:                 my %info = &Apache::lonnet::tmpget($env{'form.ltoken'});
  283:                 $linkprot = $info{'linkprot'};
  284:                 if ($info{'linkprotuser'} ne '') {
  285:                     $linkprotuser = $info{'linkprotuser'};
  286:                 }
  287:                 if ($info{'linkprotexit'} ne '') {
  288:                     $linkprotexit = $info{'linkprotexit'};
  289:                 }
  290:             } else {
  291:                 $linkprot = $env{'form.linkprot'};
  292:                 $linkprotuser = $env{'form.linkprotuser'};
  293:                 $linkprotexit = $env{'form.linkprotexit'};
  294:             }
  295:             if ($linkprot) {
  296:                 my ($linkprotector,$deeplink) = split(/:/,$linkprot,2);
  297:                 if (($deeplink =~ m{^/tiny/$match_domain/\w+$}) &&
  298:                     ($linkprotuser ne '') && ($linkprotuser ne $env{'user.name'}.':'.$env{'user.domain'})) {
  299:                     my $ip = &Apache::lonnet::get_requestor_ip();
  300:                     my %linkprotinfo = (
  301:                                           origurl => $deeplink,
  302:                                           linkprot => $linkprot,
  303:                                           linkprotuser => $linkprotuser,
  304:                                           linkprotexit => $linkprotexit,
  305:                                        );    
  306:                     if ($env{'form.ltoken'}) {
  307:                         my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
  308:                     }
  309:                     &Apache::migrateuser::logout($r,$ip,$handle,undef,undef,\%linkprotinfo);
  310:                     return OK;
  311:                 }
  312:                 if ($env{'user.linkprotector'}) {
  313:                     my @protectors = split(/,/,$env{'user.linkprotector'});
  314:                     unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
  315:                         push(@protectors,$linkprotector);
  316:                         @protectors = sort { $a <=> $b } @protectors;
  317:                         &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
  318:                     }
  319:                 } else {
  320:                     &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
  321:                 }
  322:                 if ($env{'user.linkproturi'}) {
  323:                     my @proturis = split(/,/,$env{'user.linkproturi'});
  324:                     unless (grep(/^\Q$deeplink\E$/,@proturis)) {
  325:                         push(@proturis,$deeplink);
  326:                         @proturis = sort @proturis;
  327:                         &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
  328:                     }
  329:                 } else {
  330:                     &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
  331:                 }
  332:             }
  333:         } elsif ($env{'form.linkkey'} ne '') {
  334:             if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  335:                 my $linkkey = $env{'form.linkkey'};
  336:                 if ($env{'user.deeplinkkey'}) {
  337:                     my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
  338:                     unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
  339:                         push(@linkkeys,$linkkey);
  340:                         &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});
  341:                     }
  342:                 } else {
  343:                     &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
  344:                 }
  345:                 my $deeplink = $env{'form.firsturl'}; 
  346:                 if ($env{'user.keyedlinkuri'}) {
  347:                     my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
  348:                     unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
  349:                         push(@keyeduris,$deeplink);
  350:                         &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
  351:                     }
  352:                 } else {
  353:                     &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
  354:                 }
  355:             }
  356:         }
  357:         if ($env{'form.ltoken'}) {
  358:             my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
  359:         }
  360:         if (($env{'form.firsturl'} eq '/adm/email') && ($env{'form.display'})) {
  361:             if ($env{'form.mailrecip'}) {
  362:                 if ($env{'form.mailrecip'} eq "$env{'user.name'}:$env{'user.domain'}") {
  363:                     $dest .= (($dest=~/\?/)?'&amp;':'?') . 'display='.&escape($env{'form.display'}).
  364:                                                            '&amp;mailrecip='.&escape($env{'form.mailrecip'});
  365:                 }
  366:             } elsif (($env{'form.username'} eq $env{'user.name'}) && ($env{'form.domain'} eq $env{'user.domain'})) {
  367:                 $dest .= (($dest=~/\?/)?'&amp;':'?') . 'display='.&escape($env{'form.display'}).
  368:                                                        '&amp;mailrecip='.&escape("$env{'user.name'}:$env{'form.domain'}");
  369:             }
  370:         }
  371: 	$r->print(
  372:                   $start_page
  373:                  .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  374:                  .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
  375:                   '<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>').'</p>'
  376:                  .$end_page
  377:                  );
  378:         return OK;
  379:     }
  380: 
  381: # ---------------------------------------------------- No valid token, continue
  382: 
  383: # ---------------------------- Not possible to really login to domain "public"
  384:     if ($env{'form.domain'} eq 'public') {
  385: 	$env{'form.domain'}='';
  386: 	$env{'form.username'}='';
  387:     }
  388: 
  389: # ------ Is this page requested because /adm/migrateuser detected an IP change?
  390:     my %sessiondata;
  391:     if ($env{'form.iptoken'}) {
  392:         %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'});
  393:         unless ($sessiondata{'sessionserver'}) {
  394:             my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'});
  395:             delete($env{'form.iptoken'});
  396:         }
  397:     }
  398: # ----------------------------------------------------------- Process Interface
  399:     $env{'form.interface'}=~s/\W//g;
  400: 
  401:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
  402:         &Apache::loncommon::decode_user_agent($r);
  403: 
  404:     my $iconpath= 
  405: 	&Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL'));
  406: 
  407:     my $domain = &Apache::lonnet::default_login_domain();
  408:     my $defdom = $domain;
  409:     if ($lonhost ne '') {
  410:         unless ($sessiondata{'sessionserver'}) {
  411:             my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie);
  412:             if ($redirect) {
  413:                 $r->print($redirect);
  414:                 return OK;
  415:             }
  416:         }
  417:     }
  418: 
  419:     if (($sessiondata{'domain'}) &&
  420:         (&Apache::lonnet::domain($sessiondata{'domain'},'description'))) {
  421:         $domain=$sessiondata{'domain'};
  422:     } elsif (($env{'form.domain'}) && 
  423: 	(&Apache::lonnet::domain($env{'form.domain'},'description'))) {
  424: 	$domain=$env{'form.domain'};
  425:     }
  426: 
  427:     my $role    = $r->dir_config('lonRole');
  428:     my $loadlim = $r->dir_config('lonLoadLim');
  429:     my $uloadlim= $r->dir_config('lonUserLoadLim');
  430:     my $servadm = $r->dir_config('lonAdmEMail');
  431:     my $tabdir  = $r->dir_config('lonTabDir');
  432:     my $include = $r->dir_config('lonIncludes');
  433:     my $expire  = $r->dir_config('lonExpire');
  434:     my $version = $r->dir_config('lonVersion');
  435:     my $host_name = &Apache::lonnet::hostname($lonhost);
  436: 
  437: # --------------------------------------------- Default values for login fields
  438:     
  439:     my ($authusername,$authdomain);
  440:     if ($sessiondata{'username'}) {
  441:         $authusername=$sessiondata{'username'};
  442:     } else {
  443:         $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'});
  444:         $authusername=($env{'form.username'}?$env{'form.username'}:'');
  445:     }
  446:     if ($sessiondata{'domain'}) {
  447:         $authdomain=$sessiondata{'domain'};
  448:     } else {
  449:         $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'});
  450:         $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain);
  451:     }
  452: 
  453: # ---------------------------------------------------------- Determine own load
  454:     my $loadavg;
  455:     {
  456: 	my $loadfile=Apache::File->new('/proc/loadavg');
  457: 	$loadavg=<$loadfile>;
  458:     }
  459:     $loadavg =~ s/\s.*//g;
  460: 
  461:     my ($loadpercent,$userloadpercent);
  462:     if ($loadlim) {
  463:         $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
  464:     }
  465:     if ($uloadlim) {
  466:         $userloadpercent=&Apache::lonnet::userload();
  467:     }
  468: 
  469:     my $firsturl=
  470:     ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'});
  471: 
  472: # ----------------------------------------------------------- Get announcements
  473:     my $announcements=&Apache::lonnet::getannounce();
  474: # -------------------------------------------------------- Set login parameters
  475: 
  476:     my @hexstr=('0','1','2','3','4','5','6','7',
  477:                 '8','9','a','b','c','d','e','f');
  478:     my $lkey='';
  479:     for (0..7) {
  480:         $lkey.=$hexstr[rand(15)];
  481:     }
  482: 
  483:     my $ukey='';
  484:     for (0..7) {
  485:         $ukey.=$hexstr[rand(15)];
  486:     }
  487: 
  488:     my $lextkey=hex($lkey);
  489:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
  490: 
  491:     my $uextkey=hex($ukey);
  492:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
  493: 
  494: # -------------------------------------------------------- Store away log token
  495:     my ($tokenextras,$tokentype,$linkprot_for_login);
  496:     my @names = ('role','symb','iptoken','ltoken','linkprotuser','linkprotexit','linkprot','linkkey','display');
  497:     foreach my $name (@names) {
  498:         if ($env{'form.'.$name} ne '') {
  499:             if ($name eq 'ltoken') {
  500:                 my %info = &Apache::lonnet::tmpget($env{'form.'.$name});
  501:                 if ($info{'linkprot'}) {
  502:                     $linkprot_for_login = $info{'linkprot'};
  503:                     $tokenextras .= '&linkprot='.&escape($info{'linkprot'});
  504:                     foreach my $item ('linkprotuser','linkprotexit') {
  505:                         if ($info{$item}) {
  506:                             $tokenextras .= '&'.$item.'='.&escape($info{$item});
  507:                         }
  508:                     }
  509:                     $tokentype = 'link';
  510:                     last;
  511:                 }
  512:             } elsif ($env{'form.display'} && ($env{'form.firsturl'} eq '/adm/email')) {
  513:                 if (($env{'form.mailrecip'}) ||
  514:                     ($env{'form.username'} =~ /^$match_username$/) && ($env{'form.domain'} =~ /^$match_domain$/)) {
  515:                     $tokenextras .= '&'.$name.'='.&escape($env{'form.display'});
  516:                     if ($env{'form.mailrecip'}) {
  517:                         $tokenextras .= '&mailrecip='.&escape($env{'form.mailrecip'});
  518:                     } else {
  519:                         $tokenextras .= '&mailrecip='.&escape($env{'form.username'}.':'.$env{'form.domain'});
  520:                     }
  521:                 }
  522:             } else {
  523:                 $tokenextras .= '&'.$name.'='.&escape($env{'form.'.$name});
  524:                 if (($name eq 'linkkey') || ($name eq 'linkprot')) {
  525:                     if ((($env{'form.retry'}) || ($env{'form.sso'})) && 
  526:                         (!$env{'form.ltoken'}) && ($name eq 'linkprot')) {
  527:                         $linkprot_for_login = $env{'form.linkprot'};
  528:                     }
  529:                     $tokentype = 'link';
  530:                 }
  531:             }
  532:         }
  533:     }
  534:     if ($tokentype) {
  535:         $tokenextras .= ":$tokentype";
  536:     }
  537:     my $logtoken=Apache::lonnet::reply(
  538:        'tmpput:'.$ukey.$lkey.'&'.&escape($firsturl).$tokenextras,
  539:        $lonhost);
  540: 
  541: # -- If we cannot talk to ourselves, or hostID does not map to a hostname
  542: #    we are in serious trouble
  543: 
  544:     if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  545:         if ($logtoken eq 'no_such_host') {
  546:             &Apache::lonnet::logthis('No valid logtoken for log-in page -- unable to determine hostname for hostID: '.$lonhost.'. Check entry in hosts.tab');
  547:         }
  548:         if ($env{'form.ltoken'}) {
  549:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
  550:             delete($env{'form.ltoken'});
  551:         }
  552:         my $spares='';
  553:         my (@sparehosts,%spareservers);
  554:         my $sparesref = &Apache::lonnet::this_host_spares($defdom);
  555:         if (ref($sparesref) eq 'HASH') {
  556:             foreach my $key (keys(%{$sparesref})) {
  557:                 if (ref($sparesref->{$key}) eq 'ARRAY') {
  558:                     my @sorted = sort { &Apache::lonnet::hostname($a) cmp
  559:                                         &Apache::lonnet::hostname($b);
  560:                                       } @{$sparesref->{$key}};
  561:                     if (@sorted) {
  562:                         if ($key eq 'primary') {
  563:                             unshift(@sparehosts,@sorted);
  564:                         } elsif ($key eq 'default') {
  565:                             push(@sparehosts,@sorted);
  566:                         }
  567:                     }
  568:                 }
  569:             }
  570:         }
  571:         foreach my $hostid (@sparehosts) {
  572:             next if ($hostid eq $lonhost);
  573: 	    my $hostname = &Apache::lonnet::hostname($hostid);
  574: 	    next if (($hostname eq '') || ($spareservers{$hostname}));
  575:             $spareservers{$hostname} = 1;
  576:             my $protocol = $Apache::lonnet::protocol{$hostid};
  577:             $protocol = 'http' if ($protocol ne 'https');
  578:             $spares.='<br /><span style="font-size: larger;"><a href="'.$protocol.'://'.
  579:                 $hostname.
  580:                 '/adm/login?domain='.$authdomain.'">'.
  581:                 $hostname.'</a>'.
  582:                 ' '.&mt('(preferred)').'</span>'.$/;
  583:         }
  584:         if ($spares) {
  585:             $spares.= '<br />';
  586:         }
  587:         my %all_hostnames = &Apache::lonnet::all_hostnames();
  588:         foreach my $hostid (sort
  589: 		    {
  590: 			&Apache::lonnet::hostname($a) cmp
  591: 			    &Apache::lonnet::hostname($b);
  592: 		    }
  593: 		    keys(%all_hostnames)) {
  594:             next if ($hostid eq $lonhost);
  595:             my $hostname = &Apache::lonnet::hostname($hostid);
  596:             next if (($hostname eq '') || ($spareservers{$hostname}));
  597:             $spareservers{$hostname} = 1;
  598:             my $protocol = $Apache::lonnet::protocol{$hostid};
  599:             $protocol = 'http' if ($protocol ne 'https');
  600:             $spares.='<br /><a href="'.$protocol.'://'.
  601: 	             $hostname.
  602: 	             '/adm/login?domain='.$authdomain.'">'.
  603: 	             $hostname.'</a>';
  604:          }
  605:          $r->print(
  606:    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
  607:   .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
  608:   .'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'
  609:   .&mt('The LearningOnline Network with CAPA')
  610:   .'</title></head>'
  611:   .'<body bgcolor="#FFFFFF">'
  612:   .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'
  613:   .'<img src="/adm/lonKaputt/lonlogo_broken.gif" alt="broken icon" align="right" />'
  614:   .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');
  615:         if ($spares) {
  616:             $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')
  617:                      .'</p>'
  618:                      .$spares);
  619:         }
  620:         $r->print('</body>'
  621:                  .'</html>'
  622:         );
  623:         return OK;
  624:     }
  625: 
  626: # ----------------------------------------------- Apparently we are in business
  627:     $servadm=~s/\,/\<br \/\>/g;
  628: 
  629: # ----------------------------------------------------------- Front page design
  630:     my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);
  631:     my $font=&Apache::loncommon::designparm('login.font',$domain);
  632:     my $link=&Apache::loncommon::designparm('login.link',$domain);
  633:     my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);
  634:     my $alink=&Apache::loncommon::designparm('login.alink',$domain);
  635:     my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);
  636:     my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);
  637:     my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);
  638:     my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);
  639:     my $logo=&Apache::loncommon::designparm('login.logo',$domain);
  640:     my $img=&Apache::loncommon::designparm('login.img',$domain);
  641:     my $domainlogo=&Apache::loncommon::domainlogo($domain);
  642:     my $showbanner = 1;
  643:     my $showmainlogo = 1;
  644:     if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {
  645:         $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);
  646:     }
  647:     if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {
  648:         $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);
  649:     }
  650:     my $showadminmail;
  651:     my @possdoms = &Apache::lonnet::current_machine_domains();
  652:     if (grep(/^\Q$domain\E$/,@possdoms)) {
  653:         $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);
  654:     }
  655:     my $showcoursecat =
  656:         &Apache::loncommon::designparm('login.coursecatalog',$domain);
  657:     my $shownewuserlink = 
  658:         &Apache::loncommon::designparm('login.newuser',$domain);
  659:     my $showhelpdesk =
  660:         &Apache::loncommon::designparm('login.helpdesk',$domain);
  661:     my $now=time;
  662:     my $js = (<<ENDSCRIPT);
  663: 
  664: <script type="text/javascript" language="JavaScript">
  665: // <![CDATA[
  666: function send()
  667: {
  668: this.document.server.elements.uname.value
  669: =this.document.client.elements.uname.value;
  670: 
  671: this.document.server.elements.udom.value
  672: =this.document.client.elements.udom.value;
  673: 
  674: uextkey=this.document.client.elements.uextkey.value;
  675: lextkey=this.document.client.elements.lextkey.value;
  676: initkeys();
  677: 
  678: if(this.document.server.action.substr(0,5) === 'http:'){
  679:     this.document.server.elements.upass0.value
  680:         =getCrypted(this.document.client.elements.upass$now.value);
  681: } else {
  682:     this.document.server.elements.upass0.value
  683:         =this.document.client.elements.upass$now.value;
  684: }
  685: 
  686: this.document.client.elements.uname.value='';
  687: this.document.client.elements.upass$now.value='';
  688: 
  689: this.document.server.submit();
  690: return false;
  691: }
  692: 
  693: function enableInput() {
  694:     this.document.client.elements.upass$now.removeAttribute("readOnly");
  695:     this.document.client.elements.uname.removeAttribute("readOnly");
  696:     this.document.client.elements.udom.removeAttribute("readOnly");
  697:     return;
  698: }
  699: 
  700: // ]]>
  701: </script>
  702: 
  703: ENDSCRIPT
  704: 
  705:     my ($lonhost_in_use,@hosts,%defaultdomconf,$saml_prefix,$saml_landing,
  706:         $samlssotext,$samlnonsso,$samlssoimg,$samlssoalt,$samlssourl,$samltooltip,
  707:         $samlwindow);
  708:     %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
  709:     @hosts = &Apache::lonnet::current_machine_ids();
  710:     $lonhost_in_use = $lonhost;
  711:     if (@hosts > 1) {
  712:         foreach my $hostid (@hosts) {
  713:             if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
  714:                 $lonhost_in_use = $hostid;
  715:                 last;
  716:             }
  717:         }
  718:     }
  719:     $saml_prefix = $defdom.'.login.saml_';
  720:     if ($defaultdomconf{$saml_prefix.$lonhost_in_use}) {
  721:         $saml_landing = 1;
  722:         $samlssotext = $defaultdomconf{$saml_prefix.'text_'.$lonhost_in_use};
  723:         $samlnonsso = $defaultdomconf{$saml_prefix.'notsso_'.$lonhost_in_use};
  724:         $samlssoimg = $defaultdomconf{$saml_prefix.'img_'.$lonhost_in_use};
  725:         $samlssoalt = $defaultdomconf{$saml_prefix.'alt_'.$lonhost_in_use};
  726:         $samlssourl = $defaultdomconf{$saml_prefix.'url_'.$lonhost_in_use};
  727:         $samltooltip = $defaultdomconf{$saml_prefix.'title_'.$lonhost_in_use};
  728:         $samlwindow = $defaultdomconf{$saml_prefix.'window_'.$lonhost_in_use};
  729:     }
  730:     if ($saml_landing) {
  731:        if ($samlssotext eq '') {
  732:            $samlssotext = 'SSO Login';
  733:        }
  734:        if ($samlnonsso eq '') {
  735:            $samlnonsso = 'Non-SSO Login';
  736:        }
  737:        $js .= <<"ENDSAMLJS";
  738: 
  739: <script type="text/javascript">
  740: // <![CDATA[
  741: function toggleLClogin() {
  742:     if (document.getElementById('LC_standard_login')) {
  743:         if (document.getElementById('LC_standard_login').style.display == 'none') {
  744:             document.getElementById('LC_standard_login').style.display = 'inline-block';
  745:             if (document.getElementById('LC_login_text')) {
  746:                 document.getElementById('LC_login_text').innerHTML = '$samlnonsso';
  747:             }
  748:             if ( document.client.uname ) { document.client.uname.focus(); }
  749:             if (document.getElementById('LC_SSO_login')) {
  750:                 document.getElementById('LC_SSO_login').style.display = 'none';
  751:             }
  752:         } else {
  753:             document.getElementById('LC_standard_login').style.display = 'none';
  754:             if (document.getElementById('LC_login_text')) {
  755:                 document.getElementById('LC_login_text').innerHTML = '$samlssotext';
  756:             }
  757:             if (document.getElementById('LC_SSO_login')) {
  758:                 document.getElementById('LC_SSO_login').style.display = 'inline-block';
  759:             }
  760:         }
  761:     }
  762:     return;
  763: }
  764: 
  765: // ]]>
  766: </script>
  767: 
  768: ENDSAMLJS
  769:     }
  770: 
  771: # --------------------------------------------------- Print login screen header
  772: 
  773:     my %add_entries = (
  774: 	       bgcolor      => "$mainbg",
  775: 	       text         => "$font",
  776: 	       link         => "$link",
  777: 	       vlink        => "$vlink",
  778: 	       alink        => "$alink",
  779:                onload       => 'javascript:enableInput();',);
  780: 
  781:     my ($headextra,$headextra_exempt);
  782:     $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
  783:     $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
  784:     if ($headextra) {
  785:         my $omitextra;
  786:         if ($headextra_exempt ne '') {
  787:             my @exempt = split(',',$headextra_exempt);
  788:             my $ip = &Apache::lonnet::get_requestor_ip();
  789:             if (grep(/^\Q$ip\E$/,@exempt)) {
  790:                 $omitextra = 1;
  791:             }
  792:         }
  793:         unless ($omitextra) {
  794:             my $confname = $defdom.'-domainconfig';
  795:             if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
  796:                 my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
  797:                 unless ($extra eq '-1') {
  798:                     $js .= "\n".$extra."\n";
  799:                 }
  800:             }
  801:         }
  802:     }
  803: 
  804:     $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
  805: 			       { 'redirect'       => [$expire,'/adm/roles'], 
  806: 				 'add_entries' => \%add_entries,
  807: 				 'only_body'   => 1,}));
  808: 
  809: # ----------------------------------------------------------------------- Texts
  810: 
  811:     my %lt=&Apache::lonlocal::texthash(
  812:           'un'       => 'Username',
  813:           'pw'       => 'Password',
  814:           'dom'      => 'Domain',
  815:           'perc'     => 'percent',
  816:           'load'     => 'Server Load',
  817:           'userload' => 'User Load',
  818:           'catalog'  => 'Course/Community Catalog',
  819:           'log'      => 'Log in',
  820:           'help'     => 'Log-in Help',
  821:           'serv'     => 'Server',
  822:           'servadm'  => 'Server Administration',
  823:           'helpdesk' => 'Contact Helpdesk',
  824:           'forgotpw' => 'Forgot password?',
  825:           'newuser'  => 'New User?',
  826:           'change'   => 'Change?',
  827:        );
  828: # -------------------------------------------------- Change password field name
  829: 
  830:     my $forgotpw = &forgotpwdisplay(%lt);
  831:     $forgotpw .= '<br />' if $forgotpw;
  832:     my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
  833:     if ($loginhelp) {
  834:         $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
  835:     }
  836: 
  837: # ---------------------------------------------------- Serve out DES JavaScript
  838:     {
  839:     my $jsh=Apache::File->new($include."/londes.js");
  840:     $r->print(<$jsh>);
  841:     }
  842: # ---------------------------------------------------------- Serve rest of page
  843: 
  844:     $r->print(
  845:     '<div class="LC_Box"'
  846:    .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
  847: );
  848: 
  849:     $r->print(<<ENDSERVERFORM);
  850: <form name="server" action="/adm/authenticate" method="post" target="_top">
  851:    <input type="hidden" name="logtoken" value="$logtoken" />
  852:    <input type="hidden" name="serverid" value="$lonhost" />
  853:    <input type="hidden" name="uname" value="" />
  854:    <input type="hidden" name="upass0" value="" />
  855:    <input type="hidden" name="udom" value="" />
  856:    <input type="hidden" name="localpath" value="$env{'form.localpath'}" />
  857:    <input type="hidden" name="localres" value="$env{'form.localres'}" />
  858:   </form>
  859: ENDSERVERFORM
  860:     my $coursecatalog;
  861:     if (($showcoursecat eq '') || ($showcoursecat)) {
  862:         $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';
  863:     }
  864:     my $newuserlink;
  865:     if ($shownewuserlink) {
  866:         $newuserlink = &newuser_link($lt{'newuser'}).'<br />';
  867:     }
  868:     my $logintitle =
  869:         '<h2 class="LC_hcell"'
  870:        .' style="background:'.$loginbox_header_bgcol.';'
  871:        .' color:'.$loginbox_header_textcol.'">'
  872:        .$lt{'log'}
  873:        .'</h2>';
  874: 
  875:     my $noscript_warning='<noscript><span class="LC_warning"><b>'
  876:                         .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
  877:                         .'</b></span></noscript>';
  878:     my $helpdeskscript;
  879:     my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,
  880:                                        $authdomain,\$helpdeskscript,
  881:                                        $showhelpdesk,\@possdoms);
  882: 
  883:     my $mobileargs;
  884:     if ($clientmobile) {
  885:         $mobileargs = 'autocapitalize="off" autocorrect="off"'; 
  886:     }
  887:     my $loginform=(<<LFORM);
  888: <form name="client" action="" onsubmit="return(send())" id="lclogin">
  889:   <input type="hidden" name="lextkey" value="$lextkey" />
  890:   <input type="hidden" name="uextkey" value="$uextkey" />
  891:   <b><label for="uname">$lt{'un'}</label>:</b><br />
  892:   <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />
  893:   <b><label for="upass$now">$lt{'pw'}</label>:</b><br />
  894:   <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />
  895:   <b><label for="udom">$lt{'dom'}</label>:</b><br />
  896:   <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />
  897:   <input type="submit" value="$lt{'log'}" />
  898: </form>
  899: LFORM
  900: 
  901:     if ($showbanner) {
  902:         my $alttext = &Apache::loncommon::designparm('login.alttext_img',$domain);
  903:         if ($alttext eq '') {
  904:             $alttext = 'The Learning Online Network with CAPA';
  905:         }
  906:         $r->print(<<HEADER);
  907: <!-- The LON-CAPA Header -->
  908: <div style="background:$pgbg;margin:0;width:100%;">
  909:   <img src="$img" border="0" alt="$alttext" class="LC_maxwidth" id="lcloginbanner" />
  910: </div>
  911: HEADER
  912:     }
  913: 
  914:     my $stdauthformstyle = 'inline-block';
  915:     my $ssoauthstyle = 'none';
  916:     my $sso_onclick;
  917:     my $logintype;
  918:     $r->print('<div style="float:left;margin-top:0;">');
  919:     if ($saml_landing) {
  920:         $ssoauthstyle = 'inline-block';
  921:         $stdauthformstyle = 'none';
  922:         $logintype = $samlssotext;
  923:         my $ssologin = '/adm/sso';
  924:         if ($samlssourl  ne '') {
  925:             $ssologin = $samlssourl;
  926:         }
  927:         my $ssologin_for_js = &js_escape($ssologin);
  928:         my $querystr_for_js;
  929:         if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  930:             my $querystring;
  931:             if ($env{'form.firsturl'} ne '') {
  932:                 $querystring = 'origurl=';
  933:                 if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
  934:                     $querystring .= &uri_escape_utf8($env{'form.firsturl'});
  935:                 } else {
  936:                     $querystring .= &uri_escape($env{'form.firsturl'});
  937:                 }
  938:                 $querystring = &HTML::Entities::encode($querystring,"'");
  939:             }
  940:             if ($env{'form.ltoken'} ne '') {
  941:                 $querystring .= (($querystring eq '')?'':'&amp;') . 'ltoken='.
  942:                                   &HTML::Entities::encode(&uri_escape($env{'form.ltoken'}));
  943:             } elsif ($env{'form.linkkey'}) {
  944:                 $querystring .= (($querystring eq '')?'':'&amp;') . 'linkkey='.
  945:                                   &HTML::Entities::encode(&uri_escape($env{'form.linkkey'}));
  946:             }
  947:             if ($querystring ne '') {
  948:                 $ssologin .= (($ssologin=~/\?/)?'&amp;':'?') . $querystring;
  949:                 $querystr_for_js = &js_escape($querystring);
  950:             }
  951:         } elsif ($logtoken ne '') {
  952:             $ssologin .= (($ssologin=~/\?/)?'&amp;':'?') . 'logtoken='.$logtoken;
  953:             $querystr_for_js = &js_escape('logtoken='.$logtoken);
  954:         }
  955:         my $ssohref;
  956:         if ($samlwindow) {
  957:             $sso_onclick = <<"ENDJS";
  958: if (document.getElementById('LC_sso_login_link')) {
  959:     var ssoelem = document.getElementById('LC_sso_login_link')
  960:     ssoelem.addEventListener('click',samlWinFunction,false);
  961:     var windows = {};
  962:     function samlWinFunction(evt) {
  963:         evt.preventDefault();
  964:         var url = '$ssologin_for_js';
  965:         var name = 'lcssowin';
  966:         var querystr = '$querystr_for_js';
  967:         if (querystr) {
  968:             url += '?'+querystr+'&lcssowin=1';
  969:         } else {
  970:             url += '?lcssowin=1';
  971:         }
  972:         if ((typeof windows[name] !== 'undefined') && (!windows[name].closed)) {
  973:             windows[name].close();
  974:         }
  975:         windows[name]=window.open(url,name,'width=350,height=600');
  976:         windows[name].focus();
  977:         return false;
  978:     }
  979: }
  980: ENDJS
  981:         }
  982:         if ($samlssoimg ne '') {
  983:             $ssohref = '<a href="'.$ssologin.'" title="'.$samltooltip.'" id="LC_sso_login_link">'.
  984:                        '<img src="'.$samlssoimg.'" alt="'.$samlssoalt.'" id="lcssobutton" /></a>';
  985:         } else {
  986:             $ssohref = '<a href="'.$ssologin.'" id="LC_sso_login_link">'.$samlssotext.'</a>';
  987:         }
  988:         if (($env{'form.saml'} eq 'no') ||
  989:             (($env{'form.username'} ne '') && ($env{'form.domain'} ne ''))) {
  990:             $ssoauthstyle = 'none';
  991:             $stdauthformstyle = 'inline-block';
  992:             $logintype = $samlnonsso;
  993:         }
  994:         $r->print(<<ENDSAML);
  995: <p>
  996: Log-in type:
  997: <span style="font-weight:bold" id="LC_login_text">$logintype</span><br />
  998: <span><a href="javascript:toggleLClogin();" style="color:#000000">$lt{'change'}</a></span>
  999: </p>
 1000: <div style="display:$ssoauthstyle" id="LC_SSO_login">
 1001: <div class="LC_Box" style="padding-top: 10px;">
 1002: $ssohref
 1003: $noscript_warning
 1004: </div>
 1005: <div class="LC_Box" style="padding-top: 10px;">
 1006: $loginhelp
 1007: $contactblock
 1008: $coursecatalog
 1009: </div>
 1010: </div>
 1011: ENDSAML
 1012:     } else {
 1013:         if ($env{'form.ltoken'}) {
 1014:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1015:             delete($env{'form.ltoken'});
 1016:         }
 1017:     }
 1018:     my $in_frame_js;
 1019:     if ($linkprot_for_login) {
 1020:         my ($linkprotector,$linkproturi) = split(/:/,$linkprot_for_login,2);
 1021:         if (($linkprotector =~ /^\d+(c|d)$/) && ($linkproturi =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$})) {
 1022:             my $set_target;
 1023:             if (($env{'form.retry'}) || ($env{'form.sso'})) {
 1024:                 if ($linkproturi eq $env{'form.firsturl'}) {
 1025:                     $set_target = "    document.server.target = '_self';";
 1026:                 }
 1027:             } else {
 1028:                 $set_target = <<ENDTARG;
 1029:     var linkproturi = '$linkproturi';
 1030:     var path = document.location.pathname.replace( new RegExp('^/adm/launch'),'');
 1031:     if (linkproturi == path) {
 1032:         document.server.target = '_self';
 1033:     }
 1034: ENDTARG
 1035:             }
 1036:             $in_frame_js = <<ENDJS;
 1037: <script type="text/javascript">
 1038: // <![CDATA[
 1039: if ((window.self !== window.top) && (document.server.target != '_self')) {
 1040:     $set_target
 1041:     $sso_onclick
 1042: }
 1043: // ]]>
 1044: </script>
 1045: ENDJS
 1046:         }
 1047:     } elsif ($samlwindow) {
 1048:         $in_frame_js = <<ENDJS;
 1049: <script type="text/javascript">
 1050: // <![CDATA[
 1051: if ((window.self !== window.top) && (document.server.target != '_self')) {
 1052:     $sso_onclick
 1053: }
 1054: // ]]>
 1055: </script>
 1056: ENDJS
 1057:     }
 1058: 
 1059:     $r->print(<<ENDLOGIN);
 1060: <div style="display:$stdauthformstyle;" id="LC_standard_login">
 1061: <div class="LC_Box" style="background:$loginbox_bg;">
 1062:   $logintitle
 1063:   $loginform
 1064:   $noscript_warning
 1065: </div>
 1066:   
 1067: <div class="LC_Box" style="padding-top: 10px;">
 1068:   $loginhelp
 1069:   $forgotpw
 1070:   $contactblock
 1071:   $newuserlink
 1072:   $coursecatalog
 1073: </div>
 1074: </div>
 1075: 
 1076: ENDLOGIN
 1077:     $r->print('</div><div>'."\n");
 1078:     if ($showmainlogo) {
 1079:         my $alttext = &Apache::loncommon::designparm('login.alttext_logo',$domain); 
 1080:         $r->print(' <img src="'.$logo.'" alt="'.$alttext.'" class="LC_maxwidth" id="lcloginmainlogo" />'."\n");
 1081:     }
 1082: $r->print(<<ENDTOP);
 1083: $announcements
 1084: </div>
 1085: <hr style="clear:both;" />
 1086: ENDTOP
 1087:     my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);
 1088:     $domainrow = <<"END";
 1089:       <tr>
 1090:        <td  align="left" valign="top">
 1091:         <small><b>$lt{'dom'}:&nbsp;</b></small>
 1092:        </td>
 1093:        <td  align="left" valign="top">
 1094:         <small><tt>&nbsp;$domain</tt></small>
 1095:        </td>
 1096:       </tr>
 1097: END
 1098:     $serverrow = <<"END";
 1099:       <tr>
 1100:        <td  align="left" valign="top">
 1101:         <small><b>$lt{'serv'}:&nbsp;</b></small>
 1102:        </td>
 1103:        <td align="left" valign="top">
 1104:         <small><tt>&nbsp;$lonhost ($role)</tt></small>
 1105:        </td>
 1106:       </tr>
 1107: END
 1108:     if ($loadlim) {
 1109:         $loadrow = <<"END";
 1110:       <tr>
 1111:        <td align="left" valign="top">
 1112:         <small><b>$lt{'load'}:&nbsp;</b></small>
 1113:        </td>
 1114:        <td align="left" valign="top">
 1115:         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>
 1116:        </td>
 1117:       </tr>
 1118: END
 1119:     }
 1120:     if ($uloadlim) {
 1121:         $userloadrow = <<"END";
 1122:       <tr>
 1123:        <td align="left" valign="top">
 1124:         <small><b>$lt{'userload'}:&nbsp;</b></small>
 1125:        </td>
 1126:        <td align="left" valign="top">
 1127:         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>
 1128:        </td>
 1129:       </tr>
 1130: END
 1131:     }
 1132:     if (($version ne '') && ($version ne '<!-- VERSION -->')) {
 1133:         $versionrow = <<"END";
 1134:       <tr>
 1135:        <td colspan="2" align="left">
 1136:         <small>$version</small>
 1137:        </td>
 1138:       </tr>
 1139: END
 1140:     }
 1141: 
 1142:     $r->print(<<ENDDOCUMENT);
 1143:     <div style="float: left;">
 1144:      <table border="0" cellspacing="0" cellpadding="0">
 1145: $domainrow
 1146: $serverrow
 1147: $loadrow    
 1148: $userloadrow
 1149: $versionrow
 1150:      </table>
 1151:     </div>
 1152:     <div style="float: right;">
 1153:     $domainlogo
 1154:     </div>
 1155:     <br style="clear:both;" />
 1156:  </div>
 1157: 
 1158: $in_frame_js
 1159: <script type="text/javascript">
 1160: // <![CDATA[
 1161: // the if prevents the script error if the browser can not handle this
 1162: if ( document.client.uname ) { document.client.uname.focus(); }
 1163: // ]]>
 1164: </script>
 1165: $helpdeskscript
 1166: 
 1167: ENDDOCUMENT
 1168:     my %endargs = ( 'noredirectlink' => 1, );
 1169:     $r->print(&Apache::loncommon::end_page(\%endargs));
 1170:     return OK;
 1171: }
 1172: 
 1173: sub check_loginvia {
 1174:     my ($domain,$lonhost,$lonidsdir,$balcookie) = @_;
 1175:     if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') {
 1176:         return;
 1177:     }
 1178:     my %domconfhash = &Apache::loncommon::get_domainconf($domain);
 1179:     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};
 1180:     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};
 1181:     my $output;
 1182:     if ($loginvia ne '') {
 1183:         my $noredirect;
 1184:         my $ip = &Apache::lonnet::get_requestor_ip();   
 1185:         if ($ip eq '127.0.0.1') {
 1186:             $noredirect = 1;
 1187:         } else {
 1188:             if ($loginvia_exempt ne '') {
 1189:                 my @exempt = split(',',$loginvia_exempt);
 1190:                 if (grep(/^\Q$ip\E$/,@exempt)) {
 1191:                     $noredirect = 1;
 1192:                 }
 1193:             }
 1194:         }
 1195:         unless ($noredirect) {
 1196:             my ($newhost,$path);
 1197:             if ($loginvia =~ /:/) {
 1198:                 ($newhost,$path) = split(':',$loginvia);
 1199:             } else {
 1200:                 $newhost = $loginvia;
 1201:             }
 1202:             if ($newhost ne $lonhost) {
 1203:                 if (&Apache::lonnet::hostname($newhost) ne '') {
 1204:                     if ($balcookie) {
 1205:                         my ($balancer,$cookie) = split(/:/,$balcookie);
 1206:                         if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) {
 1207:                             my ($udom,$uname,$cookieid) = ($1,$2,$3);
 1208:                             unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') {
 1209:                                 if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) {
 1210:                                     while (my $filename=readdir($dh)) {
 1211:                                         if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) {
 1212:                                             my $handle = $1;
 1213:                                             my %hash =
 1214:                                                 &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir,
 1215:                                                                                      ['request.balancercookie',
 1216:                                                                                       'user.linkedenv']);
 1217:                                             if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") {
 1218:                                                 if (unlink("$lonidsdir/$filename")) {
 1219:                                                     if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
 1220:                                                         (-l "$lonidsdir/$hash{'user.linkedenv'}.id") &&
 1221:                                                         (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) {
 1222:                                                         unlink("$lonidsdir/$hash{'user.linkedenv'}.id");
 1223:                                                     }
 1224:                                                 }
 1225:                                             }
 1226:                                             last;
 1227:                                         }
 1228:                                     }
 1229:                                     closedir($dh);
 1230:                                 }
 1231:                             }
 1232:                         }
 1233:                     }
 1234:                     $output = &redirect_page($newhost,$path);
 1235:                 }
 1236:             }
 1237:         }
 1238:     }
 1239:     return $output;
 1240: }
 1241: 
 1242: sub redirect_page {
 1243:     my ($desthost,$path) = @_;
 1244:     my $hostname = &Apache::lonnet::hostname($desthost);
 1245:     my $protocol = $Apache::lonnet::protocol{$desthost};
 1246:     $protocol = 'http' if ($protocol ne 'https');
 1247:     unless ($path =~ m{^/}) {
 1248:         $path = '/'.$path;
 1249:     }
 1250:     my $url = $protocol.'://'.$hostname.$path;
 1251:     my $args = {};
 1252:     if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
 1253:         $url = $protocol.'://'.$hostname.$env{'form.firsturl'};
 1254:         if (($env{'form.ltoken'}) || ($env{'form.linkprot'} ne '') ||
 1255:             ($env{'form.linkkey'} ne '')) {
 1256:             my %link_info;
 1257:             if ($env{'form.ltoken'}) {
 1258:                 %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
 1259:                 &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1260:                 $args->{'only_body'} = 1;
 1261:             } elsif ($env{'form.linkprot'}) {
 1262:                 $link_info{'linkprot'} = $env{'form.linkprot'};
 1263:                 foreach my $item ('linkprotuser','linkprotexit') {
 1264:                     if ($env{'form.'.$item}) {
 1265:                         $link_info{$item} = $env{'form.'.$item};
 1266:                     }
 1267:                 }
 1268:                 $args->{'only_body'} = 1;
 1269:             } elsif ($env{'form.linkkey'} ne '') {
 1270:                 $link_info{'linkkey'} = $env{'form.linkkey'};
 1271:             }
 1272:             my $token = &Apache::lonnet::tmpput(\%link_info,$desthost,'link');
 1273:             unless (($token eq 'con_lost') || ($token eq 'refused') ||
 1274:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
 1275:                 $url .= '?ltoken='.$token;
 1276:             }
 1277:         }
 1278:     } else {
 1279:         my $querystring;
 1280:         if ($env{'form.firsturl'} ne '') {
 1281:             if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
 1282:                 $querystring = &uri_escape_utf8($env{'form.firsturl'});
 1283:             } else {
 1284:                 $querystring = &uri_escape($env{'form.firsturl'});
 1285:             }
 1286:             $querystring = &HTML::Entities::encode($querystring,"'");
 1287:             $querystring = '?firsturl='.$querystring;
 1288:         }
 1289:         if ($env{'form.ltoken'}) {
 1290:             my %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
 1291:             &Apache::lonnet::tmpdel($env{'form.ltoken'});
 1292:             my $token = &Apache::lonnet::tmpput(\%link_info,$desthost,'link');
 1293:             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
 1294:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
 1295:                 unless (($path eq '/adm/roles') || ($path eq '/adm/login')) {
 1296:                     $url = $protocol.'://'.$hostname.'/adm/roles';
 1297:                 }
 1298:                 $querystring .= (($querystring =~/^\?/)?'&amp;':'?') . 'ttoken='.$token;
 1299:             }
 1300:         }
 1301:         $url .= $querystring;
 1302:     }
 1303:     $args->{'redirect'} = [0,$url];
 1304:     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,$args);
 1305:     my $end_page   = &Apache::loncommon::end_page();
 1306:     return $start_page.$end_page;
 1307: }
 1308: 
 1309: sub contactdisplay {
 1310:     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,
 1311:         $possdoms) = @_;
 1312:     my $contactblock;
 1313:     my $origmail;
 1314:     if (ref($possdoms) eq 'ARRAY') {
 1315:         if (grep(/^\Q$authdomain\E$/,@{$possdoms})) { 
 1316:             $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
 1317:         }
 1318:     }
 1319:     my $requestmail = 
 1320:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
 1321:                                                  $authdomain,$origmail);
 1322:     unless ($showhelpdesk eq '0') {
 1323:         if ($requestmail =~ m/[^\@]+\@[^\@]+/) {
 1324:             $showhelpdesk = 1;
 1325:         } else {
 1326:             $showhelpdesk = 0;
 1327:         }
 1328:     }
 1329:     if ($servadm && $showadminmail) {
 1330:         $contactblock .= $$lt{'servadm'}.':<br />'.
 1331:                          '<tt>'.$servadm.'</tt><br />';
 1332:     }
 1333:     if ($showhelpdesk) {
 1334:         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';
 1335:         my $thisurl = &escape('/adm/login');
 1336:         $$helpdeskscript = <<"ENDSCRIPT";
 1337: <script type="text/javascript">
 1338: // <![CDATA[
 1339: function helpdesk() {
 1340:     var possdom = document.client.udom.value;
 1341:     var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');
 1342:     if (codedom == '') {
 1343:         codedom = "$authdomain";
 1344:     }
 1345:     var querystr = "origurl=$thisurl&codedom="+codedom;
 1346:     document.location.href = "/adm/helpdesk?"+querystr;
 1347:     return;
 1348: }
 1349: // ]]>
 1350: </script>
 1351: ENDSCRIPT
 1352:     }
 1353:     return $contactblock;
 1354: }
 1355: 
 1356: sub forgotpwdisplay {
 1357:     my (%lt) = @_;
 1358:     my $prompt_for_resetpw = 1; 
 1359:     if ($prompt_for_resetpw) {
 1360:         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';
 1361:     }
 1362:     return;
 1363: }
 1364: 
 1365: sub coursecatalog_link {
 1366:     my ($linkname) = @_;
 1367:     return <<"END";
 1368:       <a href="/adm/coursecatalog">$linkname</a>
 1369: END
 1370: }
 1371: 
 1372: sub newuser_link {
 1373:     my ($linkname) = @_;
 1374:     return '<a href="/adm/createaccount">'.$linkname.'</a>';
 1375: }
 1376: 
 1377: sub decode_token {
 1378:     my ($info) = @_;
 1379:     my ($firsturl,@rest)=split(/\&/,$info);
 1380:     my %form;
 1381:     if ($firsturl ne '') {
 1382:         $form{'firsturl'} = &unescape($firsturl);
 1383:     }
 1384:     foreach my $item (@rest) {
 1385:         my ($key,$value) = split(/=/,$item);
 1386:         $form{$key} = &unescape($value);
 1387:     }
 1388:     return %form;
 1389: }
 1390: 
 1391: 1;
 1392: __END__

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