File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.183: download - view: text, annotated - select for diffs
Mon May 3 15:27:44 2021 UTC (3 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6914
 - Check whether WAF proxy alias should be used when using switchserver.
 - Requests for /adm/dns/hosts and /adm/dns/domain from other nodes in the
   network do not require redirecting to an alias (if in use).

    1: # The LearningOnline Network
    2: # Login Screen
    3: #
    4: # $Id: lonlogin.pm,v 1.183 2021/05/03 15:27:44 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 CGI::Cookie();
   42:  
   43: sub handler {
   44:     my $r = shift;
   45: 
   46:     &Apache::loncommon::get_unprocessed_cgi
   47: 	(join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'},
   48: 	      $ENV{'REDIRECT_QUERY_STRING'}),
   49: 	 ['interface','username','domain','firsturl','localpath','localres',
   50: 	  'token','role','symb','iptoken','btoken','ltoken','linkkey']);
   51:     if (!defined($env{'form.firsturl'})) {
   52:         &Apache::lonacc::get_posted_cgi($r,['firsturl']);
   53:     }
   54:     if (!defined($env{'form.firsturl'})) {
   55:         if ($ENV{'REDIRECT_URL'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) {
   56:             $env{'form.firsturl'} = $ENV{'REDIRECT_URL'};
   57:         }
   58:     }
   59:     if (($env{'form.firsturl'} =~ m{^/+tiny/+$LONCAPA::match_domain/+\w+$}) &&
   60:         (!$env{'form.ltoken'}) && (!$env{'form.linkkey'})) {
   61:         &Apache::lonacc::get_posted_cgi($r,['linkkey']);
   62:     }
   63: 
   64: # -- check if they are a migrating user
   65:     if (defined($env{'form.token'})) {
   66: 	return &Apache::migrateuser::handler($r);
   67:     }
   68: 
   69: # For "public user" - remove any exising "public" cookie, as user really wants to log-in
   70:     my ($handle,$lonidsdir,$expirepub,$userdom);
   71:     $lonidsdir=$r->dir_config('lonIDsDir');
   72:     unless ($r->header_only) {
   73:         $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom);
   74:         if ($handle ne '') {
   75:             if ($handle=~/^publicuser\_/) {
   76:                 unlink($r->dir_config('lonIDsDir')."/$handle.id");
   77:                 undef($handle);
   78:                 undef($userdom);
   79:                 $expirepub = 1;
   80:             }
   81:         }
   82:     }
   83: 
   84:     &Apache::loncommon::no_cache($r);
   85:     &Apache::lonlocal::get_language_handle($r);
   86:     &Apache::loncommon::content_type($r,'text/html');
   87:     if ($expirepub) {
   88:         my $c = new CGI::Cookie(-name    => 'lonPubID',
   89:                                 -value   => '',
   90:                                 -expires => '-10y',);
   91:         $r->header_out('Set-cookie' => $c);
   92:     } elsif (($handle eq '') && ($userdom ne '')) {
   93:         my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   94:         foreach my $name (keys(%cookies)) {
   95:             next unless ($name =~ /^lon(|S|Link|Pub)ID$/);
   96:             my $c = new CGI::Cookie(-name    => $name,
   97:                                     -value   => '',
   98:                                     -expires => '-10y',);
   99:             $r->headers_out->add('Set-cookie' => $c);
  100:         }
  101:     }
  102:     $r->send_http_header;
  103:     return OK if $r->header_only;
  104: 
  105: 
  106: # Are we re-routing?
  107:     my $londocroot = $r->dir_config('lonDocRoot'); 
  108:     if (-e "$londocroot/lon-status/reroute.txt") {
  109: 	&Apache::lonauth::reroute($r);
  110: 	return OK;
  111:     }
  112: 
  113:     my $lonhost = $r->dir_config('lonHostID');
  114:     $env{'form.firsturl'} =~ s/(`)/'/g;
  115: 
  116: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  117: 
  118:     my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1);
  119:     if ($found_server) {
  120:         my $hostname = &Apache::lonnet::hostname($found_server);
  121:         if ($hostname ne '') {
  122:             my $protocol = $Apache::lonnet::protocol{$found_server};
  123:             $protocol = 'http' if ($protocol ne 'https');
  124:             my $dest = '/adm/roles';
  125:             if ($env{'form.firsturl'} ne '') {
  126:                 $dest = $env{'form.firsturl'};
  127:             }
  128:             my %info = (
  129:                          balcookie => $lonhost.':'.$balancer_cookie,
  130:                        );
  131:             if ($env{'form.ltoken'}) {
  132:                 my %link_info = &Apache::lonnet::tmpget($env{'form.ltoken'});
  133:                 if ($link_info{'linkprot'}) {
  134:                     $info{'linkprot'} = $link_info{'linkprot'};
  135:                 }
  136:                 &Apache::lonnet::tmpdel($env{'form.ltoken'});
  137:                 delete($env{'form.ltoken'});
  138:             } elsif ($env{'form.linkkey'}) {
  139:                 $info{'linkkey'} = $env{'form.linkkey'};
  140:                 delete($env{'form.linkkey'});
  141:             }
  142:             my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server);
  143:             if ($balancer_token) {
  144:                 $dest .=  (($dest=~/\?/)?'&;':'?') . 'btoken='.$balancer_token;
  145:             }
  146:             unless ($found_server eq $lonhost) {
  147:                 my $alias = &Apache::lonnet::use_proxy_alias($r,$found_server);
  148:                 $hostname = $alias if ($alias ne '');
  149:             }
  150:             my $url = $protocol.'://'.$hostname.$dest;
  151:             my $start_page =
  152:                 &Apache::loncommon::start_page('Switching Server ...',undef,
  153:                                                {'redirect'       => [0,$url],});
  154:             my $end_page   = &Apache::loncommon::end_page();
  155:             $r->print($start_page.$end_page);
  156:             return OK;
  157:         }
  158:     }
  159: 
  160: #
  161: # Check if a LON-CAPA load balancer sent user here because user's browser sent
  162: # it a balancer cookie for an active session on this server.
  163: #
  164: 
  165:     my ($balcookie,$linkprot,$linkkey);
  166:     if ($env{'form.btoken'}) {
  167:         my %info = &Apache::lonnet::tmpget($env{'form.btoken'});
  168:         $balcookie = $info{'balcookie'};
  169:         if ($balcookie) {
  170:             if ($info{'linkprot'}) {
  171:                 $linkprot = $info{'linkprot'};
  172:             } elsif ($info{'linkkey'}) {
  173:                 $linkkey = $info{'linkkey'};
  174:             }
  175:         }    
  176:         &Apache::lonnet::tmpdel($env{'form.btoken'});
  177:         delete($env{'form.btoken'});
  178:     }
  179: 
  180: #
  181: # If browser sent an old cookie for which the session file had been removed
  182: # check if configuration for user's domain has a portal URL set.  If so
  183: # switch user's log-in to the portal.
  184: #
  185: 
  186:     if (($handle eq '') && ($userdom ne '')) {
  187:         my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom);
  188:         if ($domdefaults{'portal_def'} =~ /^https?\:/) {
  189:             my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
  190:                                           {'redirect' => [0,$domdefaults{'portal_def'}],});
  191:             my $end_page   = &Apache::loncommon::end_page();
  192:             $r->print($start_page.$end_page);
  193:             return OK;
  194:         }
  195:     }
  196: 
  197: # -------------------------------- Prevent users from attempting to login twice
  198:     if ($handle ne '') {
  199:         &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  200: 	my $start_page = 
  201: 	    &Apache::loncommon::start_page('Already logged in');
  202: 	my $end_page = 
  203: 	    &Apache::loncommon::end_page();
  204:         my $dest = '/adm/roles';
  205:         if ($env{'form.firsturl'} ne '') {
  206:             $dest = $env{'form.firsturl'};
  207:         }
  208:         if (($env{'form.ltoken'}) || ($linkprot)) {
  209:             unless ($linkprot) {
  210:                 my %info = &Apache::lonnet::tmpget($env{'form.ltoken'});
  211:                 $linkprot = $info{'linkprot'};
  212:                 my $delete = &Apache::lonnet::tmpdel($env{'form.ltoken'});
  213:                 delete($env{'form.ltoken'});
  214:             }
  215:             if ($linkprot) {
  216:                 my ($linkprotector,$deeplink) = split(/:/,$linkprot,2);
  217:                 if ($env{'user.linkprotector'}) {
  218:                     my @protectors = split(/,/,$env{'user.linkprotector'});
  219:                     unless (grep(/^\Q$linkprotector\E$/,@protectors)) {
  220:                         push(@protectors,$linkprotector);
  221:                         @protectors = sort { $a <=> $b } @protectors;
  222:                         &Apache::lonnet::appenv({'user.linkprotector' => join(',',@protectors)});
  223:                     }
  224:                 } else {
  225:                     &Apache::lonnet::appenv({'user.linkprotector' => $linkprotector });
  226:                 }
  227:                 if ($env{'user.linkproturi'}) {
  228:                     my @proturis = split(/,/,$env{'user.linkproturi'});
  229:                     unless (grep(/^\Q$deeplink\E$/,@proturis)) {
  230:                         push(@proturis,$deeplink);
  231:                         @proturis = sort @proturis;
  232:                         &Apache::lonnet::appenv({'user.linkproturi' => join(',',@proturis)});
  233:                     }
  234:                 } else {
  235:                     &Apache::lonnet::appenv({'user.linkproturi' => $deeplink});
  236:                 }
  237:             }
  238:         } elsif (($env{'form.linkkey'}) || ($linkkey)) {
  239:             if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
  240:                 if ($linkkey eq '') {
  241:                     $linkkey = $env{'form.linkkey'};
  242:                 }
  243:                 if ($env{'user.deeplinkkey'}) {
  244:                     my @linkkeys = split(/,/,$env{'user.deeplinkkey'});
  245:                     unless (grep(/^\Q$linkkey\E$/,@linkkeys)) {
  246:                         push(@linkkeys,$linkkey);
  247:                         &Apache::lonnet::appenv({'user.deeplinkkey' => join(',',sort(@linkkeys))});  
  248:                     }
  249:                 } else {
  250:                     &Apache::lonnet::appenv({'user.deeplinkkey' => $linkkey});
  251:                 }
  252:                 my $deeplink = $env{'form.firsturl'}; 
  253:                 if ($env{'user.keyedlinkuri'}) {
  254:                     my @keyeduris = split(/,/,$env{'user.keyedlinkuri'});
  255:                     unless (grep(/^\Q$deeplink\E$/,@keyeduris)) {
  256:                         push(@keyeduris,$deeplink);
  257:                         &Apache::lonnet::appenv({'user.keyedlinkuri' => join(',',sort(@keyeduris))});
  258:                     }
  259:                 } else {
  260:                     &Apache::lonnet::appenv({'user.keyedlinkuri' => $deeplink});
  261:                 }
  262:             }
  263:         }
  264: 	$r->print(
  265:                   $start_page
  266:                  .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
  267:                  .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].',
  268:                   '<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>').'</p>'
  269:                  .$end_page
  270:                  );
  271:         return OK;
  272:     }
  273: 
  274: # ---------------------------------------------------- No valid token, continue
  275: 
  276: # ---------------------------- Not possible to really login to domain "public"
  277:     if ($env{'form.domain'} eq 'public') {
  278: 	$env{'form.domain'}='';
  279: 	$env{'form.username'}='';
  280:     }
  281: 
  282: # ------ Is this page requested because /adm/migrateuser detected an IP change?
  283:     my %sessiondata;
  284:     if ($env{'form.iptoken'}) {
  285:         %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'});
  286:         unless ($sessiondata{'sessionserver'}) {
  287:             my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'});
  288:             delete($env{'form.iptoken'});
  289:         }
  290:     }
  291: # ----------------------------------------------------------- Process Interface
  292:     $env{'form.interface'}=~s/\W//g;
  293: 
  294:     (undef,undef,undef,undef,undef,undef,my $clientmobile) =
  295:         &Apache::loncommon::decode_user_agent();
  296: 
  297:     my $iconpath= 
  298: 	&Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL'));
  299: 
  300:     my $domain = &Apache::lonnet::default_login_domain();
  301:     my $defdom = $domain;
  302:     if ($lonhost ne '') {
  303:         unless ($sessiondata{'sessionserver'}) {
  304:             my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie,$linkprot);
  305:             if ($redirect) {
  306:                 $r->print($redirect);
  307:                 return OK;
  308:             }
  309:         }
  310:     }
  311: 
  312:     if (($sessiondata{'domain'}) &&
  313:         (&Apache::lonnet::domain($sessiondata{'domain'},'description'))) {
  314:         $domain=$sessiondata{'domain'};
  315:     } elsif (($env{'form.domain'}) && 
  316: 	(&Apache::lonnet::domain($env{'form.domain'},'description'))) {
  317: 	$domain=$env{'form.domain'};
  318:     }
  319: 
  320:     my $role    = $r->dir_config('lonRole');
  321:     my $loadlim = $r->dir_config('lonLoadLim');
  322:     my $uloadlim= $r->dir_config('lonUserLoadLim');
  323:     my $servadm = $r->dir_config('lonAdmEMail');
  324:     my $tabdir  = $r->dir_config('lonTabDir');
  325:     my $include = $r->dir_config('lonIncludes');
  326:     my $expire  = $r->dir_config('lonExpire');
  327:     my $version = $r->dir_config('lonVersion');
  328:     my $host_name = &Apache::lonnet::hostname($lonhost);
  329: 
  330: # --------------------------------------------- Default values for login fields
  331:     
  332:     my ($authusername,$authdomain);
  333:     if ($sessiondata{'username'}) {
  334:         $authusername=$sessiondata{'username'};
  335:     } else {
  336:         $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'});
  337:         $authusername=($env{'form.username'}?$env{'form.username'}:'');
  338:     }
  339:     if ($sessiondata{'domain'}) {
  340:         $authdomain=$sessiondata{'domain'};
  341:     } else {
  342:         $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'});
  343:         $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain);
  344:     }
  345: 
  346: # ---------------------------------------------------------- Determine own load
  347:     my $loadavg;
  348:     {
  349: 	my $loadfile=Apache::File->new('/proc/loadavg');
  350: 	$loadavg=<$loadfile>;
  351:     }
  352:     $loadavg =~ s/\s.*//g;
  353: 
  354:     my ($loadpercent,$userloadpercent);
  355:     if ($loadlim) {
  356:         $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
  357:     }
  358:     if ($uloadlim) {
  359:         $userloadpercent=&Apache::lonnet::userload();
  360:     }
  361: 
  362:     my $firsturl=
  363:     ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'});
  364: 
  365: # ----------------------------------------------------------- Get announcements
  366:     my $announcements=&Apache::lonnet::getannounce();
  367: # -------------------------------------------------------- Set login parameters
  368: 
  369:     my @hexstr=('0','1','2','3','4','5','6','7',
  370:                 '8','9','a','b','c','d','e','f');
  371:     my $lkey='';
  372:     for (0..7) {
  373:         $lkey.=$hexstr[rand(15)];
  374:     }
  375: 
  376:     my $ukey='';
  377:     for (0..7) {
  378:         $ukey.=$hexstr[rand(15)];
  379:     }
  380: 
  381:     my $lextkey=hex($lkey);
  382:     if ($lextkey>2147483647) { $lextkey-=4294967296; }
  383: 
  384:     my $uextkey=hex($ukey);
  385:     if ($uextkey>2147483647) { $uextkey-=4294967296; }
  386: 
  387: # -------------------------------------------------------- Store away log token
  388:     my $tokenextras;
  389:     if ($env{'form.role'}) {
  390:         $tokenextras = '&role='.&escape($env{'form.role'});
  391:     }
  392:     if ($env{'form.symb'}) {
  393:         if (!$tokenextras) {
  394:             $tokenextras = '&';
  395:         }
  396:         $tokenextras .= '&symb='.&escape($env{'form.symb'});
  397:     }
  398:     if ($env{'form.iptoken'}) {
  399:         if (!$tokenextras) {
  400:             $tokenextras = '&&';
  401:         }
  402:         $tokenextras .= '&iptoken='.&escape($env{'form.iptoken'});
  403:     }
  404:     if ($env{'form.ltoken'}) {
  405:         my %info = &Apache::lonnet::tmpget($env{'form.ltoken'});
  406:         &Apache::lonnet::tmpdel($env{'form.ltoken'});
  407:         delete($env{'form.ltoken'});
  408:         if ($info{'linkprot'}) {
  409:             if (!$tokenextras) {
  410:                 $tokenextras = '&&&';
  411:             }
  412:             $tokenextras .= '&linkprot='.&escape($info{'linkprot'});
  413:         }
  414:     } elsif ($env{'form.linkkey'}) {
  415:         if (!$tokenextras) {
  416:             $tokenextras = '&&&';
  417:         }
  418:         $tokenextras .= '&linkkey='.&escape($env{'form.linkkey'});
  419:     }
  420:     my $logtoken=Apache::lonnet::reply(
  421:        'tmpput:'.$ukey.$lkey.'&'.$firsturl.$tokenextras,
  422:        $lonhost);
  423: 
  424: # -- If we cannot talk to ourselves, or hostID does not map to a hostname
  425: #    we are in serious trouble
  426: 
  427:     if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
  428:         if ($logtoken eq 'no_such_host') {
  429:             &Apache::lonnet::logthis('No valid logtoken for log-in page -- unable to determine hostname for hostID: '.$lonhost.'. Check entry in hosts.tab');
  430:         }
  431:         my $spares='';
  432:         my (@sparehosts,%spareservers);
  433:         my $sparesref = &Apache::lonnet::this_host_spares($defdom);
  434:         if (ref($sparesref) eq 'HASH') {
  435:             foreach my $key (keys(%{$sparesref})) {
  436:                 if (ref($sparesref->{$key}) eq 'ARRAY') {
  437:                     my @sorted = sort { &Apache::lonnet::hostname($a) cmp
  438:                                         &Apache::lonnet::hostname($b);
  439:                                       } @{$sparesref->{$key}};
  440:                     if (@sorted) {
  441:                         if ($key eq 'primary') {
  442:                             unshift(@sparehosts,@sorted);
  443:                         } elsif ($key eq 'default') {
  444:                             push(@sparehosts,@sorted);
  445:                         }
  446:                     }
  447:                 }
  448:             }
  449:         }
  450:         foreach my $hostid (@sparehosts) {
  451:             next if ($hostid eq $lonhost);
  452: 	    my $hostname = &Apache::lonnet::hostname($hostid);
  453: 	    next if (($hostname eq '') || ($spareservers{$hostname}));
  454:             $spareservers{$hostname} = 1;
  455:             my $protocol = $Apache::lonnet::protocol{$hostid};
  456:             $protocol = 'http' if ($protocol ne 'https');
  457:             $spares.='<br /><span style="font-size: larger;"><a href="'.$protocol.'://'.
  458:                 $hostname.
  459:                 '/adm/login?domain='.$authdomain.'">'.
  460:                 $hostname.'</a>'.
  461:                 ' '.&mt('(preferred)').'</span>'.$/;
  462:         }
  463:         if ($spares) {
  464:             $spares.= '<br />';
  465:         }
  466:         my %all_hostnames = &Apache::lonnet::all_hostnames();
  467:         foreach my $hostid (sort
  468: 		    {
  469: 			&Apache::lonnet::hostname($a) cmp
  470: 			    &Apache::lonnet::hostname($b);
  471: 		    }
  472: 		    keys(%all_hostnames)) {
  473:             next if ($hostid eq $lonhost);
  474:             my $hostname = &Apache::lonnet::hostname($hostid);
  475:             next if (($hostname eq '') || ($spareservers{$hostname}));
  476:             $spareservers{$hostname} = 1;
  477:             my $protocol = $Apache::lonnet::protocol{$hostid};
  478:             $protocol = 'http' if ($protocol ne 'https');
  479:             $spares.='<br /><a href="'.$protocol.'://'.
  480: 	             $hostname.
  481: 	             '/adm/login?domain='.$authdomain.'">'.
  482: 	             $hostname.'</a>';
  483:          }
  484:          $r->print(
  485:    '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
  486:   .'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
  487:   .'<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>'
  488:   .&mt('The LearningOnline Network with CAPA')
  489:   .'</title></head>'
  490:   .'<body bgcolor="#FFFFFF">'
  491:   .'<h1>'.&mt('The LearningOnline Network with CAPA').'</h1>'
  492:   .'<img src="/adm/lonKaputt/lonlogo_broken.gif" alt="broken icon" align="right" />'
  493:   .'<h3>'.&mt('This LON-CAPA server is temporarily not available for login.').'</h3>');
  494:         if ($spares) {
  495:             $r->print('<p>'.&mt('Please attempt to login to one of the following servers:')
  496:                      .'</p>'
  497:                      .$spares);
  498:         }
  499:         $r->print('</body>'
  500:                  .'</html>'
  501:         );
  502:         return OK;
  503:     }
  504: 
  505: # ----------------------------------------------- Apparently we are in business
  506:     $servadm=~s/\,/\<br \/\>/g;
  507: 
  508: # ----------------------------------------------------------- Front page design
  509:     my $pgbg=&Apache::loncommon::designparm('login.pgbg',$domain);
  510:     my $font=&Apache::loncommon::designparm('login.font',$domain);
  511:     my $link=&Apache::loncommon::designparm('login.link',$domain);
  512:     my $vlink=&Apache::loncommon::designparm('login.vlink',$domain);
  513:     my $alink=&Apache::loncommon::designparm('login.alink',$domain);
  514:     my $mainbg=&Apache::loncommon::designparm('login.mainbg',$domain);
  515:     my $loginbox_bg=&Apache::loncommon::designparm('login.sidebg',$domain);
  516:     my $loginbox_header_bgcol=&Apache::loncommon::designparm('login.bgcol',$domain);
  517:     my $loginbox_header_textcol=&Apache::loncommon::designparm('login.textcol',$domain);
  518:     my $logo=&Apache::loncommon::designparm('login.logo',$domain);
  519:     my $img=&Apache::loncommon::designparm('login.img',$domain);
  520:     my $domainlogo=&Apache::loncommon::domainlogo($domain);
  521:     my $showbanner = 1;
  522:     my $showmainlogo = 1;
  523:     if (defined(&Apache::loncommon::designparm('login.showlogo_img',$domain))) {
  524:         $showbanner = &Apache::loncommon::designparm('login.showlogo_img',$domain);
  525:     }
  526:     if (defined(&Apache::loncommon::designparm('login.showlogo_logo',$domain))) {
  527:         $showmainlogo = &Apache::loncommon::designparm('login.showlogo_logo',$domain);
  528:     }
  529:     my $showadminmail;
  530:     my @possdoms = &Apache::lonnet::current_machine_domains();
  531:     if (grep(/^\Q$domain\E$/,@possdoms)) {
  532:         $showadminmail=&Apache::loncommon::designparm('login.adminmail',$domain);
  533:     }
  534:     my $showcoursecat =
  535:         &Apache::loncommon::designparm('login.coursecatalog',$domain);
  536:     my $shownewuserlink = 
  537:         &Apache::loncommon::designparm('login.newuser',$domain);
  538:     my $showhelpdesk =
  539:         &Apache::loncommon::designparm('login.helpdesk',$domain);
  540:     my $now=time;
  541:     my $js = (<<ENDSCRIPT);
  542: 
  543: <script type="text/javascript" language="JavaScript">
  544: // <![CDATA[
  545: function send()
  546: {
  547: this.document.server.elements.uname.value
  548: =this.document.client.elements.uname.value;
  549: 
  550: this.document.server.elements.udom.value
  551: =this.document.client.elements.udom.value;
  552: 
  553: uextkey=this.document.client.elements.uextkey.value;
  554: lextkey=this.document.client.elements.lextkey.value;
  555: initkeys();
  556: 
  557: if(this.document.server.action.substr(0,5) === 'http:'){
  558:     this.document.server.elements.upass0.value
  559:         =getCrypted(this.document.client.elements.upass$now.value);
  560: } else {
  561:     this.document.server.elements.upass0.value
  562:         =this.document.client.elements.upass$now.value;
  563: }
  564: 
  565: this.document.client.elements.uname.value='';
  566: this.document.client.elements.upass$now.value='';
  567: 
  568: this.document.server.submit();
  569: return false;
  570: }
  571: 
  572: function enableInput() {
  573:     this.document.client.elements.upass$now.removeAttribute("readOnly");
  574:     this.document.client.elements.uname.removeAttribute("readOnly");
  575:     this.document.client.elements.udom.removeAttribute("readOnly");
  576:     return;
  577: }
  578: 
  579: // ]]>
  580: </script>
  581: 
  582: ENDSCRIPT
  583: 
  584: # --------------------------------------------------- Print login screen header
  585: 
  586:     my %add_entries = (
  587: 	       bgcolor      => "$mainbg",
  588: 	       text         => "$font",
  589: 	       link         => "$link",
  590: 	       vlink        => "$vlink",
  591: 	       alink        => "$alink",
  592:                onload       => 'javascript:enableInput();',);
  593: 
  594:     my ($lonhost_in_use,$headextra,$headextra_exempt,@hosts,%defaultdomconf);
  595:     @hosts = &Apache::lonnet::current_machine_ids();
  596:     $lonhost_in_use = $lonhost;
  597:     if (@hosts > 1) {
  598:         foreach my $hostid (@hosts) {
  599:             if (&Apache::lonnet::host_domain($hostid) eq $defdom) {
  600:                 $lonhost_in_use = $hostid;
  601:                 last;
  602:             }
  603:         }
  604:     }
  605:     %defaultdomconf = &Apache::loncommon::get_domainconf($defdom);
  606:     $headextra = $defaultdomconf{$defdom.'.login.headtag_'.$lonhost_in_use};
  607:     $headextra_exempt = $defaultdomconf{$domain.'.login.headtag_exempt_'.$lonhost_in_use};
  608:     if ($headextra) {
  609:         my $omitextra;
  610:         if ($headextra_exempt ne '') {
  611:             my @exempt = split(',',$headextra_exempt);
  612:             my $ip = &Apache::lonnet::get_requestor_ip();
  613:             if (grep(/^\Q$ip\E$/,@exempt)) {
  614:                 $omitextra = 1;
  615:             }
  616:         }
  617:         unless ($omitextra) {
  618:             my $confname = $defdom.'-domainconfig';
  619:             if ($headextra =~ m{^\Q/res/$defdom/$confname/login/headtag/$lonhost_in_use/\E}) {
  620:                 my $extra = &Apache::lonnet::getfile(&Apache::lonnet::filelocation("",$headextra));
  621:                 unless ($extra eq '-1') {
  622:                     $js .= "\n".$extra."\n";
  623:                 }
  624:             }
  625:         }
  626:     }
  627: 
  628:     $r->print(&Apache::loncommon::start_page('The LearningOnline Network with CAPA Login',$js,
  629: 			       { 'redirect'       => [$expire,'/adm/roles'], 
  630: 				 'add_entries' => \%add_entries,
  631: 				 'only_body'   => 1,}));
  632: 
  633: # ----------------------------------------------------------------------- Texts
  634: 
  635:     my %lt=&Apache::lonlocal::texthash(
  636:           'un'       => 'Username',
  637:           'pw'       => 'Password',
  638:           'dom'      => 'Domain',
  639:           'perc'     => 'percent',
  640:           'load'     => 'Server Load',
  641:           'userload' => 'User Load',
  642:           'catalog'  => 'Course/Community Catalog',
  643:           'log'      => 'Log in',
  644:           'help'     => 'Log-in Help',
  645:           'serv'     => 'Server',
  646:           'servadm'  => 'Server Administration',
  647:           'helpdesk' => 'Contact Helpdesk',
  648:           'forgotpw' => 'Forgot password?',
  649:           'newuser'  => 'New User?',
  650:        );
  651: # -------------------------------------------------- Change password field name
  652: 
  653:     my $forgotpw = &forgotpwdisplay(%lt);
  654:     $forgotpw .= '<br />' if $forgotpw;
  655:     my $loginhelp = &Apache::lonauth::loginhelpdisplay($authdomain);
  656:     if ($loginhelp) {
  657:         $loginhelp = '<a href="'.$loginhelp.'">'.$lt{'help'}.'</a><br />';
  658:     }
  659: 
  660: # ---------------------------------------------------- Serve out DES JavaScript
  661:     {
  662:     my $jsh=Apache::File->new($include."/londes.js");
  663:     $r->print(<$jsh>);
  664:     }
  665: # ---------------------------------------------------------- Serve rest of page
  666: 
  667:     $r->print(
  668:     '<div class="LC_Box"'
  669:    .' style="margin:0 auto; padding:10px; width:90%; height: auto; background-color:#FFFFFF;">'
  670: );
  671: 
  672:     $r->print(<<ENDSERVERFORM);
  673: <form name="server" action="/adm/authenticate" method="post" target="_top">
  674:    <input type="hidden" name="logtoken" value="$logtoken" />
  675:    <input type="hidden" name="serverid" value="$lonhost" />
  676:    <input type="hidden" name="uname" value="" />
  677:    <input type="hidden" name="upass0" value="" />
  678:    <input type="hidden" name="udom" value="" />
  679:    <input type="hidden" name="localpath" value="$env{'form.localpath'}" />
  680:    <input type="hidden" name="localres" value="$env{'form.localres'}" />
  681:   </form>
  682: ENDSERVERFORM
  683:     my $coursecatalog;
  684:     if (($showcoursecat eq '') || ($showcoursecat)) {
  685:         $coursecatalog = &coursecatalog_link($lt{'catalog'}).'<br />';
  686:     }
  687:     my $newuserlink;
  688:     if ($shownewuserlink) {
  689:         $newuserlink = &newuser_link($lt{'newuser'}).'<br />';
  690:     }
  691:     my $logintitle =
  692:         '<h2 class="LC_hcell"'
  693:        .' style="background:'.$loginbox_header_bgcol.';'
  694:        .' color:'.$loginbox_header_textcol.'">'
  695:        .$lt{'log'}
  696:        .'</h2>';
  697: 
  698:     my $noscript_warning='<noscript><span class="LC_warning"><b>'
  699:                         .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
  700:                         .'</b></span></noscript>';
  701:     my $helpdeskscript;
  702:     my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail,
  703:                                        $authdomain,\$helpdeskscript,
  704:                                        $showhelpdesk,\@possdoms);
  705: 
  706:     my $mobileargs;
  707:     if ($clientmobile) {
  708:         $mobileargs = 'autocapitalize="off" autocorrect="off"'; 
  709:     }
  710:     my $loginform=(<<LFORM);
  711: <form name="client" action="" onsubmit="return(send())">
  712:   <input type="hidden" name="lextkey" value="$lextkey" />
  713:   <input type="hidden" name="uextkey" value="$uextkey" />
  714:   <b><label for="uname">$lt{'un'}</label>:</b><br />
  715:   <input type="text" name="uname" id="uname" size="15" value="$authusername" readonly="readonly" $mobileargs /><br />
  716:   <b><label for="upass$now">$lt{'pw'}</label>:</b><br />
  717:   <input type="password" name="upass$now" id="upass$now" size="15" readonly="readonly" /><br />
  718:   <b><label for="udom">$lt{'dom'}</label>:</b><br />
  719:   <input type="text" name="udom" id="udom" size="15" value="$authdomain" readonly="readonly" $mobileargs /><br />
  720:   <input type="submit" value="$lt{'log'}" />
  721: </form>
  722: LFORM
  723: 
  724:     if ($showbanner) {
  725:         $r->print(<<HEADER);
  726: <!-- The LON-CAPA Header -->
  727: <div style="background:$pgbg;margin:0;width:100%;">
  728:   <img src="$img" border="0" alt="The Learning Online Network with CAPA" class="LC_maxwidth" />
  729: </div>
  730: HEADER
  731:     }
  732:     $r->print(<<ENDTOP);
  733: <div style="float:left;margin-top:0;">
  734: <div class="LC_Box" style="background:$loginbox_bg;">
  735:   $logintitle
  736:   $loginform
  737:   $noscript_warning
  738: </div>
  739:   
  740: <div class="LC_Box" style="padding-top: 10px;">
  741:   $loginhelp
  742:   $forgotpw
  743:   $contactblock
  744:   $newuserlink
  745:   $coursecatalog
  746: </div>
  747: </div>
  748: 
  749: <div>
  750: ENDTOP
  751:     if ($showmainlogo) {
  752:         $r->print(' <img src="'.$logo.'" alt="" class="LC_maxwidth" />'."\n");
  753:     }
  754: $r->print(<<ENDTOP);
  755: $announcements
  756: </div>
  757: <hr style="clear:both;" />
  758: ENDTOP
  759:     my ($domainrow,$serverrow,$loadrow,$userloadrow,$versionrow);
  760:     $domainrow = <<"END";
  761:       <tr>
  762:        <td  align="left" valign="top">
  763:         <small><b>$lt{'dom'}:&nbsp;</b></small>
  764:        </td>
  765:        <td  align="left" valign="top">
  766:         <small><tt>&nbsp;$domain</tt></small>
  767:        </td>
  768:       </tr>
  769: END
  770:     $serverrow = <<"END";
  771:       <tr>
  772:        <td  align="left" valign="top">
  773:         <small><b>$lt{'serv'}:&nbsp;</b></small>
  774:        </td>
  775:        <td align="left" valign="top">
  776:         <small><tt>&nbsp;$lonhost ($role)</tt></small>
  777:        </td>
  778:       </tr>
  779: END
  780:     if ($loadlim) {
  781:         $loadrow = <<"END";
  782:       <tr>
  783:        <td align="left" valign="top">
  784:         <small><b>$lt{'load'}:&nbsp;</b></small>
  785:        </td>
  786:        <td align="left" valign="top">
  787:         <small><tt>&nbsp;$loadpercent $lt{'perc'}</tt></small>
  788:        </td>
  789:       </tr>
  790: END
  791:     }
  792:     if ($uloadlim) {
  793:         $userloadrow = <<"END";
  794:       <tr>
  795:        <td align="left" valign="top">
  796:         <small><b>$lt{'userload'}:&nbsp;</b></small>
  797:        </td>
  798:        <td align="left" valign="top">
  799:         <small><tt>&nbsp;$userloadpercent $lt{'perc'}</tt></small>
  800:        </td>
  801:       </tr>
  802: END
  803:     }
  804:     if (($version ne '') && ($version ne '<!-- VERSION -->')) {
  805:         $versionrow = <<"END";
  806:       <tr>
  807:        <td colspan="2" align="left">
  808:         <small>$version</small>
  809:        </td>
  810:       </tr>
  811: END
  812:     }
  813: 
  814:     $r->print(<<ENDDOCUMENT);
  815:     <div style="float: left;">
  816:      <table border="0" cellspacing="0" cellpadding="0">
  817: $domainrow
  818: $serverrow
  819: $loadrow    
  820: $userloadrow
  821: $versionrow
  822:      </table>
  823:     </div>
  824:     <div style="float: right;">
  825:     $domainlogo
  826:     </div>
  827:     <br style="clear:both;" />
  828:  </div>
  829: 
  830: <script type="text/javascript">
  831: // <![CDATA[
  832: // the if prevents the script error if the browser can not handle this
  833: if ( document.client.uname ) { document.client.uname.focus(); }
  834: // ]]>
  835: </script>
  836: $helpdeskscript
  837: 
  838: ENDDOCUMENT
  839:     my %endargs = ( 'noredirectlink' => 1, );
  840:     $r->print(&Apache::loncommon::end_page(\%endargs));
  841:     return OK;
  842: }
  843: 
  844: sub check_loginvia {
  845:     my ($domain,$lonhost,$lonidsdir,$balcookie,$linkprot) = @_;
  846:     if ($domain eq '' || $lonhost eq '' || $lonidsdir eq '') {
  847:         return;
  848:     }
  849:     my %domconfhash = &Apache::loncommon::get_domainconf($domain);
  850:     my $loginvia = $domconfhash{$domain.'.login.loginvia_'.$lonhost};
  851:     my $loginvia_exempt = $domconfhash{$domain.'.login.loginvia_exempt_'.$lonhost};
  852:     my $output;
  853:     if ($loginvia ne '') {
  854:         my $noredirect;
  855:         my $ip = &Apache::lonnet::get_requestor_ip();   
  856:         if ($ip eq '127.0.0.1') {
  857:             $noredirect = 1;
  858:         } else {
  859:             if ($loginvia_exempt ne '') {
  860:                 my @exempt = split(',',$loginvia_exempt);
  861:                 if (grep(/^\Q$ip\E$/,@exempt)) {
  862:                     $noredirect = 1;
  863:                 }
  864:             }
  865:         }
  866:         unless ($noredirect) {
  867:             my ($newhost,$path);
  868:             if ($loginvia =~ /:/) {
  869:                 ($newhost,$path) = split(':',$loginvia);
  870:             } else {
  871:                 $newhost = $loginvia;
  872:             }
  873:             if ($newhost ne $lonhost) {
  874:                 if (&Apache::lonnet::hostname($newhost) ne '') {
  875:                     if ($balcookie) {
  876:                         my ($balancer,$cookie) = split(/:/,$balcookie);
  877:                         if ($cookie =~ /^($match_domain)_($match_username)_([a-f0-9]+)$/) {
  878:                             my ($udom,$uname,$cookieid) = ($1,$2,$3);
  879:                             unless (&Apache::lonnet::delbalcookie($cookie,$balancer) eq 'ok') {
  880:                                 if ((-d $lonidsdir) && (opendir(my $dh,$lonidsdir))) {
  881:                                     while (my $filename=readdir($dh)) {
  882:                                         if ($filename=~/^(\Q$uname\E_\d+_\Q$udom\E_$match_lonid)\.id$/) {
  883:                                             my $handle = $1;
  884:                                             my %hash =
  885:                                                 &Apache::lonnet::get_sessionfile_vars($handle,$lonidsdir,
  886:                                                                                      ['request.balancercookie',
  887:                                                                                       'user.linkedenv']);
  888:                                             if ($hash{'request.balancercookie'} eq "$balancer:$cookieid") {
  889:                                                 if (unlink("$lonidsdir/$filename")) {
  890:                                                     if (($hash{'user.linkedenv'} =~ /^[a-f0-9]+_linked$/) &&
  891:                                                         (-l "$lonidsdir/$hash{'user.linkedenv'}.id") &&
  892:                                                         (readlink("$lonidsdir/$hash{'user.linkedenv'}.id") eq "$lonidsdir/$filename")) {
  893:                                                         unlink("$lonidsdir/$hash{'user.linkedenv'}.id");
  894:                                                     }
  895:                                                 }
  896:                                             }
  897:                                             last;
  898:                                         }
  899:                                     }
  900:                                     closedir($dh);
  901:                                 }
  902:                             }
  903:                         }
  904:                     }
  905:                     $output = &redirect_page($newhost,$path,$linkprot);
  906:                 }
  907:             }
  908:         }
  909:     }
  910:     return $output;
  911: }
  912: 
  913: sub redirect_page {
  914:     my ($desthost,$path,$linkprot) = @_;
  915:     my $hostname = &Apache::lonnet::hostname($desthost);
  916:     my $protocol = $Apache::lonnet::protocol{$desthost};
  917:     $protocol = 'http' if ($protocol ne 'https');
  918:     unless ($path =~ m{^/}) {
  919:         $path = '/'.$path;
  920:     }
  921:     my $url = $protocol.'://'.$hostname.$path;
  922:     if ($env{'form.firsturl'} ne '') {
  923:         $url .='?firsturl='.$env{'form.firsturl'};
  924:     }
  925:     if ($linkprot) {
  926:         my $ltoken = &Apache::lonnet::tmpput({linkprot => $linkprot},$desthost);
  927:         if ($ltoken) {
  928:             $url .= (($url =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;
  929:         }
  930:     }
  931:     my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef,
  932:                                                     {'redirect' => [0,$url],});
  933:     my $end_page   = &Apache::loncommon::end_page();
  934:     return $start_page.$end_page;
  935: }
  936: 
  937: sub contactdisplay {
  938:     my ($lt,$servadm,$showadminmail,$authdomain,$helpdeskscript,$showhelpdesk,
  939:         $possdoms) = @_;
  940:     my $contactblock;
  941:     my $origmail;
  942:     if (ref($possdoms) eq 'ARRAY') {
  943:         if (grep(/^\Q$authdomain\E$/,@{$possdoms})) { 
  944:             $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
  945:         }
  946:     }
  947:     my $requestmail = 
  948:         &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
  949:                                                  $authdomain,$origmail);
  950:     unless ($showhelpdesk eq '0') {
  951:         if ($requestmail =~ m/[^\@]+\@[^\@]+/) {
  952:             $showhelpdesk = 1;
  953:         } else {
  954:             $showhelpdesk = 0;
  955:         }
  956:     }
  957:     if ($servadm && $showadminmail) {
  958:         $contactblock .= $$lt{'servadm'}.':<br />'.
  959:                          '<tt>'.$servadm.'</tt><br />';
  960:     }
  961:     if ($showhelpdesk) {
  962:         $contactblock .= '<a href="javascript:helpdesk()">'.$lt->{'helpdesk'}.'</a><br />';
  963:         my $thisurl = &escape('/adm/login');
  964:         $$helpdeskscript = <<"ENDSCRIPT";
  965: <script type="text/javascript">
  966: // <![CDATA[
  967: function helpdesk() {
  968:     var possdom = document.client.udom.value;
  969:     var codedom = possdom.replace( new RegExp("[^A-Za-z0-9.\\-]","g"),'');
  970:     if (codedom == '') {
  971:         codedom = "$authdomain";
  972:     }
  973:     var querystr = "origurl=$thisurl&codedom="+codedom;
  974:     document.location.href = "/adm/helpdesk?"+querystr;
  975:     return;
  976: }
  977: // ]]>
  978: </script>
  979: ENDSCRIPT
  980:     }
  981:     return $contactblock;
  982: }
  983: 
  984: sub forgotpwdisplay {
  985:     my (%lt) = @_;
  986:     my $prompt_for_resetpw = 1; 
  987:     if ($prompt_for_resetpw) {
  988:         return '<a href="/adm/resetpw">'.$lt{'forgotpw'}.'</a>';
  989:     }
  990:     return;
  991: }
  992: 
  993: sub coursecatalog_link {
  994:     my ($linkname) = @_;
  995:     return <<"END";
  996:       <a href="/adm/coursecatalog">$linkname</a>
  997: END
  998: }
  999: 
 1000: sub newuser_link {
 1001:     my ($linkname) = @_;
 1002:     return '<a href="/adm/createaccount">'.$linkname.'</a>';
 1003: }
 1004: 
 1005: 1;
 1006: __END__

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