--- loncom/auth/lonlogin.pm 2002/10/14 16:14:58 1.25 +++ loncom/auth/lonlogin.pm 2024/07/13 20:59:10 1.158.2.17 @@ -1,7 +1,7 @@ # The LearningOnline Network # Login Screen # -# $Id: lonlogin.pm,v 1.25 2002/10/14 16:14:58 bowersj2 Exp $ +# $Id: lonlogin.pm,v 1.158.2.17 2024/07/13 20:59:10 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -25,62 +25,291 @@ # # http://www.lon-capa.org/ # -# 5/21/99,5/22,5/25,5/26,5/31,6/2,6/10,7/12,7/14, -# 1/14/00,5/29,5/30,6/1,6/29,7/1,11/9, -# 1/17/01 Gerd Kortemeyer -# -# 2/7/02,2/8,2/12,2/14,2/15,2/19 Josh Brunskole -# -# 7/10/02 Jeremy Bowers package Apache::lonlogin; use strict; use Apache::Constants qw(:common); use Apache::File (); -use Apache::lonnet(); +use Apache::lonnet; use Apache::loncommon(); - +use Apache::lonauth(); +use Apache::lonlocal; +use Apache::migrateuser(); +use lib '/home/httpd/lib/perl/'; +use LONCAPA qw(:DEFAULT :match); +use URI::Escape; +use HTML::Entities(); +use CGI::Cookie(); + sub handler { my $r = shift; - $r->content_type('text/html'); + + &Apache::loncommon::get_unprocessed_cgi + (join('&',$ENV{'QUERY_STRING'},$env{'request.querystring'}, + $ENV{'REDIRECT_QUERY_STRING'}), + ['interface','username','domain','firsturl','localpath','localres', + 'token','role','symb','iptoken','btoken','saml','sso','retry']); + +# -- check if they are a migrating user + if (defined($env{'form.token'})) { + return &Apache::migrateuser::handler($r); + } + + my $lonhost = $r->dir_config('lonHostID'); + if (($env{'form.sso'}) || ($env{'form.retry'})) { + my $infotoken; + if ($env{'form.sso'}) { + $infotoken = $env{'form.sso'}; + } else { + $infotoken = $env{'form.retry'}; + } + my $data = &Apache::lonnet::reply('tmpget:'.$infotoken,$lonhost); + unless (($data=~/^error/) || ($data eq 'con_lost') || + ($data eq 'no_such_host')) { + my %info = &decode_token($data); + foreach my $item (keys(%info)) { + $env{'form.'.$item} = $info{$item}; + } + &Apache::lonnet::tmpdel($infotoken); + } + } else { + if (!defined($env{'form.firsturl'})) { + &Apache::lonacc::get_posted_cgi($r,['firsturl']); + } + if ($env{'form.firsturl'} eq '/adm/logout') { + delete($env{'form.firsturl'}); + } + } + +# For "public user" - remove any exising "public" cookie, as user really wants to log-in + my ($handle,$lonidsdir,$expirepub,$userdom); + $lonidsdir=$r->dir_config('lonIDsDir'); + unless ($r->header_only) { + $handle = &Apache::lonnet::check_for_valid_session($r,'lonID',undef,\$userdom); + if ($handle ne '') { + if ($handle=~/^publicuser\_/) { + unlink($r->dir_config('lonIDsDir')."/$handle.id"); + undef($handle); + undef($userdom); + $expirepub = 1; + } + } + } + &Apache::loncommon::no_cache($r); + &Apache::lonlocal::get_language_handle($r); + &Apache::loncommon::content_type($r,'text/html'); + if ($expirepub) { + my $c = new CGI::Cookie(-name => 'lonPubID', + -value => '', + -expires => '-10y',); + $r->header_out('Set-cookie' => $c); + } elsif (($handle eq '') && ($userdom ne '')) { + my %cookies=CGI::Cookie->parse($r->header_in('Cookie')); + foreach my $name (keys(%cookies)) { + next unless ($name =~ /^lon(|S|Link|Pub)ID$/); + my $c = new CGI::Cookie(-name => $name, + -value => '', + -expires => '-10y',); + $r->headers_out->add('Set-cookie' => $c); + } + } $r->send_http_header; return OK if $r->header_only; - &Apache::loncommon::get_unprocessed_cgi - ($ENV{'QUERY_STRING'},['interface']); +# Are we re-routing? + my $londocroot = $r->dir_config('lonDocRoot'); + if (-e "$londocroot/lon-status/reroute.txt") { + &Apache::lonauth::reroute($r); + return OK; + } + +# Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer) + + my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r,1); + if ($found_server) { + my $hostname = &Apache::lonnet::hostname($found_server); + if ($hostname ne '') { + my $protocol = $Apache::lonnet::protocol{$found_server}; + $protocol = 'http' if ($protocol ne 'https'); + my $dest = '/adm/roles'; + if ($env{'form.firsturl'} ne '') { + $dest = &HTML::Entities::encode($env{'form.firsturl'},'\'"<>&'); + } + my %info = ( + balcookie => $lonhost.':'.$balancer_cookie, + ); + if ($env{'form.role'}) { + $info{'role'} = $env{'form.role'}; + } + if ($env{'form.symb'}) { + $info{'symb'} = $env{'form.symb'}; + } + my $balancer_token = &Apache::lonnet::tmpput(\%info,$found_server); + unless (($balancer_token eq 'con_lost') || ($balancer_token eq 'refused') || + ($balancer_token eq 'unknown_cmd') || ($balancer_token eq 'no_such_host')) { + $dest .= (($dest=~/\?/)?'&':'?') . 'btoken='.$balancer_token; + } + unless ($found_server eq $lonhost) { + my $alias = &Apache::lonnet::use_proxy_alias($r,$found_server); + $hostname = $alias if ($alias ne ''); + } + my $url = $protocol.'://'.$hostname.$dest; + my $start_page = + &Apache::loncommon::start_page('Switching Server ...',undef, + {'redirect' => [0,$url],}); + my $end_page = &Apache::loncommon::end_page(); + $r->print($start_page.$end_page); + return OK; + } + } + +# +# Check if a LON-CAPA load balancer sent user here because user's browser sent +# it a balancer cookie for an active session on this server. +# + + my $balcookie; + if ($env{'form.btoken'}) { + my %info = &Apache::lonnet::tmpget($env{'form.btoken'}); + $balcookie = $info{'balcookie'}; + &Apache::lonnet::tmpdel($env{'form.btoken'}); + delete($env{'form.btoken'}); + } + +# +# If browser sent an old cookie for which the session file had been removed +# check if configuration for user's domain has a portal URL set. If so +# switch user's log-in to the portal. +# + + if (($handle eq '') && ($userdom ne '')) { + my %domdefaults = &Apache::lonnet::get_domain_defaults($userdom); + if ($domdefaults{'portal_def'} =~ /^https?\:/) { + my $start_page = &Apache::loncommon::start_page('Switching Server ...',undef, + {'redirect' => [0,$domdefaults{'portal_def'}],}); + my $end_page = &Apache::loncommon::end_page(); + $r->print($start_page.$end_page); + return OK; + } + } + +# -------------------------------- Prevent users from attempting to login twice + if ($handle ne '') { + &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle); + my $start_page = + &Apache::loncommon::start_page('Already logged in'); + my $end_page = + &Apache::loncommon::end_page(); + my $dest = '/adm/roles'; + if ($env{'form.firsturl'} ne '') { + $dest = &HTML::Entities::encode($env{'form.firsturl'},'\'"<>&'); + } + $r->print( + $start_page + .'
'.&mt('You are already logged in!').'
' + .''.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].', + '','','','').'
' + .$end_page + ); + return OK; + } - $ENV{'form.interface'}=~s/\W//g; +# ---------------------------------------------------- No valid token, continue - my $fullgraph=($ENV{'form.interface'} ne 'textual'); +# ---------------------------- Not possible to really login to domain "public" + if ($env{'form.domain'} eq 'public') { + $env{'form.domain'}=''; + $env{'form.username'}=''; + } + +# ------ Is this page requested because /adm/migrateuser detected an IP change? + my %sessiondata; + if ($env{'form.iptoken'}) { + %sessiondata = &Apache::lonnet::tmpget($env{'form.iptoken'}); + unless ($sessiondata{'sessionserver'}) { + my $delete = &Apache::lonnet::tmpdel($env{'form.iptoken'}); + delete($env{'form.iptoken'}); + } + } +# ----------------------------------------------------------- Process Interface + $env{'form.interface'}=~s/\W//g; + + (undef,undef,undef,undef,undef,undef,my $clientmobile) = + &Apache::loncommon::decode_user_agent($r); + + my $iconpath= + &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL')); + + my $domain = &Apache::lonnet::default_login_domain(); + my $defdom = $domain; + if ($lonhost ne '') { + unless ($sessiondata{'sessionserver'}) { + my $redirect = &check_loginvia($domain,$lonhost,$lonidsdir,$balcookie); + if ($redirect) { + $r->print($redirect); + return OK; + } + } + } + + if (($sessiondata{'domain'}) && + (&Apache::lonnet::domain($sessiondata{'domain'},'description'))) { + $domain=$sessiondata{'domain'}; + } elsif (($env{'form.domain'}) && + (&Apache::lonnet::domain($env{'form.domain'},'description'))) { + $domain=$env{'form.domain'}; + } - my $iconpath= $r->dir_config('lonIconsURL'); - my $domain = $r->dir_config('lonDefDomain'); my $role = $r->dir_config('lonRole'); my $loadlim = $r->dir_config('lonLoadLim'); + my $uloadlim= $r->dir_config('lonUserLoadLim'); my $servadm = $r->dir_config('lonAdmEMail'); - my $sysadm = $r->dir_config('lonSysEMail'); - my $lonhost = $r->dir_config('lonHostID'); my $tabdir = $r->dir_config('lonTabDir'); my $include = $r->dir_config('lonIncludes'); + my $expire = $r->dir_config('lonExpire'); + my $version = $r->dir_config('lonVersion'); + my $host_name = &Apache::lonnet::hostname($lonhost); + +# --------------------------------------------- Default values for login fields + + my ($authusername,$authdomain); + if ($sessiondata{'username'}) { + $authusername=$sessiondata{'username'}; + } else { + $env{'form.username'} = &Apache::loncommon::cleanup_html($env{'form.username'}); + $authusername=($env{'form.username'}?$env{'form.username'}:''); + } + if ($sessiondata{'domain'}) { + $authdomain=$sessiondata{'domain'}; + } else { + $env{'form.domain'} = &Apache::loncommon::cleanup_html($env{'form.domain'}); + $authdomain=($env{'form.domain'}?$env{'form.domain'}:$domain); + } +# ---------------------------------------------------------- Determine own load my $loadavg; - { - my $loadfile=Apache::File->new('/proc/loadavg'); - $loadavg=<$loadfile>; - } + { + my $loadfile=Apache::File->new('/proc/loadavg'); + $loadavg=<$loadfile>; + } $loadavg =~ s/\s.*//g; - my $loadpercent=100*$loadavg/$loadlim; - my $otherserver='http://'.$ENV{'SERVER_NAME'}; - my $firsturl=$ENV{'request.firsturl'}; -# ---------------------------------------- Are we access server and overloaded? - if (($role eq 'access') && ($loadpercent>100.0)) { - $otherserver=Apache::lonnet::spareserver($loadpercent); + my ($loadpercent,$userloadpercent); + if ($loadlim) { + $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim); } + if ($uloadlim) { + $userloadpercent=&Apache::lonnet::userload(); + } + + my $firsturl= + ($env{'request.firsturl'}?$env{'request.firsturl'}:$env{'form.firsturl'}); +# ----------------------------------------------------------- Get announcements + my $announcements=&Apache::lonnet::getannounce(); # -------------------------------------------------------- Set login parameters my @hexstr=('0','1','2','3','4','5','6','7', @@ -101,250 +330,730 @@ sub handler { my $uextkey=hex($ukey); if ($uextkey>2147483647) { $uextkey-=4294967296; } +# -------------------------------------------------------- Store away log token + my $tokenextras; + my @names = ('role','symb','iptoken'); + foreach my $name (@names) { + if ($env{'form.'.$name} ne '') { + $tokenextras .= '&'.$name.'='.&escape($env{'form.'.$name}); + } + } my $logtoken=Apache::lonnet::reply( - 'tmpput:'.$ukey.$lkey.'&'.$firsturl, + 'tmpput:'.$ukey.$lkey.'&'.&escape($firsturl).$tokenextras, $lonhost); - my $domainlogo=&Apache::loncommon::domainlogo(); -# --------------------------------------------------- Print login screen header - $r->print(<'.&mt('Please attempt to login to one of the following servers:') + .'
' + .$spares); + } + $r->print('' + .'' + ); + return OK; } +# ----------------------------------------------- Apparently we are in business + $servadm=~s/\,/\- | ||||||||||||||||
- - - | - - - | - - - | - | |||||||||||||
-
- - - |
-
-
-
- - - - - |
- |
-
-
- - | |||||||||||||
-ENDTOP
-}
+ my $coursecatalog;
+ if (($showcoursecat eq '') || ($showcoursecat)) {
+ $coursecatalog = &coursecatalog_link($lt{'catalog'}).' '; + } + my $newuserlink; + if ($shownewuserlink) { + $newuserlink = &newuser_link($lt{'newuser'}).' '; + } + my $logintitle = + ' ' + .$lt{'log'} + .''; + + my $noscript_warning=''; + my $helpdeskscript; + my $contactblock = &contactdisplay(\%lt,$servadm,$showadminmail, + $authdomain,\$helpdeskscript, + $showhelpdesk,\@possdoms); + + my $mobileargs; + if ($clientmobile) { + $mobileargs = 'autocapitalize="off" autocorrect="off"'; + } + my $loginform=(<+ + : + + : + + + +LFORM + + if ($showbanner) { + my $alttext = &Apache::loncommon::designparm('login.alttext_img',$domain); + if ($alttext eq '') { + $alttext = 'The Learning Online Network with CAPA'; + } + $r->print(<
+
+
+HEADER
+ }
+
+ my $stdauthformstyle = 'inline-block';
+ my $ssoauthstyle = 'none';
+ my $logintype;
+ $r->print('');
+ if ($saml_landing) {
+ $ssoauthstyle = 'inline-block';
+ $stdauthformstyle = 'none';
+ $logintype = $samlssotext;
+ my $ssologin = '/adm/sso';
+ if ($samlssourl ne '') {
+ $ssologin = $samlssourl;
+ }
+ if (($logtoken eq 'con_lost') || ($logtoken eq 'no_such_host')) {
+ my $querystring;
+ if ($env{'form.firsturl'} ne '') {
+ $querystring = 'origurl=';
+ if ($env{'form.firsturl'} =~ /[^\x00-\xFF]/) {
+ $querystring .= &uri_escape_utf8($env{'form.firsturl'});
+ } else {
+ $querystring .= &uri_escape($env{'form.firsturl'});
+ }
+ $querystring = &HTML::Entities::encode($querystring,"'");
+ }
+ if ($querystring ne '') {
+ $ssologin .= (($ssologin=~/\?/)?'&':'?') . $querystring;
+ }
+ } elsif ($logtoken ne '') {
+ $ssologin .= (($ssologin=~/\?/)?'&':'?') . 'logtoken='.$logtoken;
+ }
+ my $ssohref;
+ if ($samlssoimg ne '') {
+ $ssohref = ''.
+ '';
+ } else {
+ $ssohref = ''.$samlssotext.'';
+ }
+ if (($env{'form.saml'} eq 'no') ||
+ (($env{'form.username'} ne '') && ($env{'form.domain'} ne ''))) {
+ $ssoauthstyle = 'none';
+ $stdauthformstyle = 'inline-block';
+ $logintype = $samlnonsso;
+ }
+ $r->print(< +$lt{'change'} + +
+
+ENDSAML
+ }
+
$r->print(<
+$ssohref
+$noscript_warning
+
+
+$loginhelp
+$contactblock
+$coursecatalog
+
+
-
-
- |
- | | ||||||||||||||
- -
- - System Administration: - $sysadm - Server Administration: - $servadm - - |
-
-
- - - - | -$domainlogo - | - - -- | |||||||||||||
- - - | - - - | - - - | - |