File:  [LON-CAPA] / loncom / auth / lonlogin.pm
Revision 1.158.2.9.2.2: download - view: text, annotated - select for diffs
Mon Oct 26 02:06:16 2020 UTC (3 years, 6 months ago) by raeburn
Branches: version_2_11_3_msu
- For 2.11.3 (modified)
  Include changes in 1.180, 1.181

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

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