version 1.121.2.20, 2020/10/23 21:21:44
|
version 1.164, 2021/08/07 20:49:10
|
Line 1
|
Line 1
|
# The LearningOnline Network |
# The LearningOnline Network
|
# User Authentication Module |
# User Authentication Module
|
# |
#
|
# $Id$ |
# $Id$
|
# |
#
|
# Copyright Michigan State University Board of Trustees |
# Copyright Michigan State University Board of Trustees
|
# |
#
|
# This file is part of the LearningOnline Network with CAPA (LON-CAPA). |
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
|
# |
#
|
# LON-CAPA is free software; you can redistribute it and/or modify |
# LON-CAPA is free software; you can redistribute it and/or modify
|
# it under the terms of the GNU General Public License as published by |
# it under the terms of the GNU General Public License as published by
|
# the Free Software Foundation; either version 2 of the License, or |
# the Free Software Foundation; either version 2 of the License, or
|
# (at your option) any later version. |
# (at your option) any later version.
|
# |
#
|
# LON-CAPA is distributed in the hope that it will be useful, |
# LON-CAPA is distributed in the hope that it will be useful,
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# GNU General Public License for more details. |
# GNU General Public License for more details.
|
# |
#
|
# You should have received a copy of the GNU General Public License |
# You should have received a copy of the GNU General Public License
|
# along with LON-CAPA; if not, write to the Free Software |
# along with LON-CAPA; if not, write to the Free Software
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
# |
#
|
# /home/httpd/html/adm/gpl.txt |
# /home/httpd/html/adm/gpl.txt
|
# |
#
|
# http://www.lon-capa.org/ |
# http://www.lon-capa.org/
|
# |
#
|
|
|
package Apache::lonauth; |
package Apache::lonauth;
|
|
|
use strict; |
use strict;
|
use LONCAPA; |
use LONCAPA qw(:DEFAULT :match);
|
use Apache::Constants qw(:common); |
use Apache::Constants qw(:common);
|
use CGI qw(:standard); |
use CGI qw(:standard);
|
use Apache::loncommon(); |
use Apache::loncommon();
|
use Apache::lonnet; |
use Apache::lonnet;
|
use Apache::lonmenu(); |
use Apache::lonmenu();
|
use Apache::createaccount; |
use Apache::createaccount;
|
use Fcntl qw(:flock); |
use Apache::ltiauth;
|
use Apache::lonlocal; |
use Fcntl qw(:flock);
|
use Apache::File(); |
use Apache::lonlocal;
|
use HTML::Entities; |
use Apache::File();
|
use Digest::MD5; |
use HTML::Entities;
|
|
use Digest::MD5;
|
# ------------------------------------------------------------ Successful login |
|
sub success { |
# ------------------------------------------------------------ Successful login
|
my ($r, $username, $domain, $authhost, $lowerurl, $extra_env, |
sub success {
|
$form,$cid) = @_; |
my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
|
|
$form,$skipcritical,$cid) = @_;
|
# ------------------------------------------------------------ Get cookie ready |
|
my $cookie = |
# ------------------------------------------------------------ Get cookie ready
|
&Apache::loncommon::init_user_environment($r, $username, $domain, |
my $cookie =
|
$authhost, $form, |
&Apache::loncommon::init_user_environment($r, $username, $domain,
|
{'extra_env' => $extra_env,}); |
$authhost, $form,
|
|
{'extra_env' => $extra_env,});
|
my $public=($username eq 'public' && $domain eq 'public'); |
|
|
my $public=($username eq 'public' && $domain eq 'public');
|
if ($public or $lowerurl eq 'noredirect') { return $cookie; } |
|
|
if ($public or $lowerurl eq 'noredirect') { return $cookie; }
|
# -------------------------------------------------------------------- Log this |
|
|
# -------------------------------------------------------------------- Log this
|
&Apache::lonnet::log($domain,$username,$authhost, |
|
"Login $ENV{'REMOTE_ADDR'}"); |
my $ip = &Apache::lonnet::get_requestor_ip();
|
|
&Apache::lonnet::log($domain,$username,$authhost,
|
# ------------------------------------------------- Check for critical messages |
"Login $ip");
|
|
|
my @what=&Apache::lonnet::dump('critical',$domain,$username); |
# ------------------------------------------------- Check for critical messages
|
if ($what[0]) { |
|
if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) { |
unless ($skipcritical) {
|
$lowerurl='/adm/email?critical=display'; |
my @what=&Apache::lonnet::dump('critical',$domain,$username);
|
} |
if ($what[0]) {
|
} |
if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
|
|
$lowerurl='/adm/email?critical=display';
|
# ------------------------------------------------------------ Get cookies ready |
}
|
my ($securecookie,$defaultcookie); |
}
|
my $ssl = $r->subprocess_env('https'); |
}
|
if ($ssl) { |
|
$securecookie="lonSID=$cookie; path=/; HttpOnly; secure"; |
# ----------------------------------------------------------- Get cookies ready
|
my $lonidsdir=$r->dir_config('lonIDsDir'); |
my ($securecookie,$defaultcookie);
|
if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) { |
my $ssl = $r->subprocess_env('https');
|
my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked'; |
if ($ssl) {
|
if (-e "$lonidsdir/$linkname.id") { |
$securecookie="lonSID=$cookie; path=/; HttpOnly; secure";
|
unlink("$lonidsdir/$linkname.id"); |
my $lonidsdir=$r->dir_config('lonIDsDir');
|
} |
if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {
|
my $made_symlink = eval { symlink("$lonidsdir/$cookie.id", |
my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';
|
"$lonidsdir/$linkname.id"); 1 }; |
if (-e "$lonidsdir/$linkname.id") {
|
if ($made_symlink) { |
unlink("$lonidsdir/$linkname.id");
|
$defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;"; |
}
|
&Apache::lonnet::appenv({'user.linkedenv' => $linkname}); |
my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",
|
} |
"$lonidsdir/$linkname.id"); 1 };
|
} |
if ($made_symlink) {
|
} else { |
$defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";
|
$defaultcookie = "lonID=$cookie; path=/; HttpOnly;"; |
&Apache::lonnet::appenv({'user.linkedenv' => $linkname});
|
} |
}
|
# -------------------------------------------------------- Menu script and info |
}
|
my $destination = $lowerurl; |
} else {
|
|
$defaultcookie = "lonID=$cookie; path=/; HttpOnly;";
|
if (defined($form->{role})) { |
}
|
my $envkey = 'user.role.'.$form->{role}; |
# -------------------------------------------------------- Menu script and info
|
my $now=time; |
my $destination = $lowerurl;
|
my $then=$env{'user.login.time'}; |
if ($env{'request.lti.login'}) {
|
my $refresh=$env{'user.refresh.time'}; |
if (($env{'request.lti.reqcrs'}) && ($env{'request.lti.reqrole'} eq 'cc')) {
|
my $update=$env{'user.update.time'}; |
&Apache::loncommon::content_type($r,'text/html');
|
if (!$update) { |
if ($securecookie) {
|
$update = $then; |
$r->headers_out->add('Set-cookie' => $securecookie);
|
} |
}
|
if (exists($env{$envkey})) { |
if ($defaultcookie) {
|
my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus); |
$r->headers_out->add('Set-cookie' => $defaultcookie);
|
&Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where, |
}
|
\$trolecode,\$tstatus,\$tstart,\$tend); |
$r->send_http_header;
|
if ($tstatus eq 'is') { |
if (ref($form) eq 'HASH') {
|
$destination .= ($destination =~ /\?/) ? '&' : '?'; |
$form->{'lti.login'} = $env{'request.lti.login'};
|
my $newrole = &HTML::Entities::encode($form->{role},'"<>&'); |
$form->{'lti.reqcrs'} = $env{'request.lti.reqcrs'};
|
$destination .= 'selectrole=1&'.$newrole.'=1'; |
$form->{'lti.reqrole'} = $env{'request.lti.reqrole'};
|
} |
$form->{'lti.sourcecrs'} = $env{'request.lti.sourcecrs'};
|
} |
}
|
} |
&Apache::ltiauth::lti_reqcrs($r,$domain,$form,$username,$domain);
|
if (defined($form->{symb})) { |
return;
|
my $destsymb = $form->{symb}; |
}
|
my $encrypted; |
if ($env{'request.lti.selfenrollrole'}) {
|
if ($destsymb =~ m{^/enc/}) { |
if (&Apache::ltiauth::lti_enroll($username,$domain,
|
$encrypted = 1; |
$env{'request.lti.selfenrollrole'}) eq 'ok') {
|
if ($cid) { |
$form->{'role'} = $env{'request.lti.selfenrollrole'};
|
$destsymb = &Apache::lonenc::unencrypted($destsymb,$cid); |
&Apache::lonnet::delenv('request.lti.selfenrollrole');
|
} |
} else {
|
} |
&Apache::ltiauth::invalid_request($r,24);
|
$destination .= ($destination =~ /\?/) ? '&' : '?'; |
}
|
if ($destsymb =~ /___/) { |
}
|
my ($map,$resid,$desturl)=split(/___/,$destsymb); |
}
|
$desturl = &Apache::lonnet::clutter($desturl); |
if (defined($form->{role})) {
|
if ($encrypted) { |
my $envkey = 'user.role.'.$form->{role};
|
$desturl = &Apache::lonenc::encrypted($desturl,1,$cid); |
my $now=time;
|
$destsymb = $form->{symb}; |
my $then=$env{'user.login.time'};
|
} |
my $refresh=$env{'user.refresh.time'};
|
$desturl = &HTML::Entities::encode($desturl,'"<>&'); |
my $update=$env{'user.update.time'};
|
$destsymb = &HTML::Entities::encode($destsymb,'"<>&'); |
if (!$update) {
|
$destination .= 'destinationurl='.$desturl. |
$update = $then;
|
'&destsymb='.$destsymb; |
}
|
} elsif (!$encrypted) { |
if (exists($env{$envkey})) {
|
$destsymb = &HTML::Entities::encode($destsymb,'"<>&'); |
my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
|
$destination .= 'destinationurl='.$destsymb; |
&Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
|
} |
\$trolecode,\$tstatus,\$tstart,\$tend);
|
} |
if ($tstatus eq 'is') {
|
if ($destination =~ m{^/adm/roles}) { |
$destination .= ($destination =~ /\?/) ? '&' : '?';
|
$destination .= ($destination =~ /\?/) ? '&' : '?'; |
my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
|
$destination .= 'source=login'; |
$destination .= 'selectrole=1&'.$newrole.'=1';
|
} |
}
|
|
}
|
my $windowinfo=&Apache::lonmenu::open($env{'browser.os'}); |
}
|
my $startupremote=&Apache::lonmenu::startupremote($destination); |
if (defined($form->{symb})) {
|
my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl); |
my $destsymb = $form->{symb};
|
my $setflags=&Apache::lonmenu::setflags(); |
my $encrypted;
|
my $maincall=&Apache::lonmenu::maincall(); |
if ($destsymb =~ m{^/enc/}) {
|
my $brcrum = [{'href' => '', |
$encrypted = 1;
|
'text' => 'Successful Login'},]; |
if ($cid) {
|
my $start_page=&Apache::loncommon::start_page('Successful Login', |
$destsymb = &Apache::lonenc::unencrypted($destsymb,$cid);
|
$startupremote, |
}
|
{'no_inline_link' => 1, |
}
|
'bread_crumbs' => $brcrum,}); |
$destination .= ($destination =~ /\?/) ? '&' : '?';
|
my $end_page =&Apache::loncommon::end_page(); |
if ($destsymb =~ /___/) {
|
|
my ($map,$resid,$desturl)=split(/___/,$destsymb);
|
my $continuelink; |
$desturl = &Apache::lonnet::clutter($desturl);
|
if ($env{'environment.remote'} eq 'off') { |
if ($encrypted) {
|
$continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>'; |
$desturl = &Apache::lonenc::encrypted($desturl,1,$cid);
|
} |
$destsymb = $form->{symb};
|
# ------------------------------------------------- Output for successful login |
}
|
|
$desturl = &HTML::Entities::encode($desturl,'"<>&');
|
&Apache::loncommon::content_type($r,'text/html'); |
$destsymb = &HTML::Entities::encode($destsymb,'"<>&');
|
if ($securecookie) { |
$destination .= 'destinationurl='.$desturl.
|
$r->headers_out->add('Set-cookie' => $securecookie); |
'&destsymb='.$destsymb;
|
} |
} elsif (!$encrypted) {
|
if ($defaultcookie) { |
$destsymb = &HTML::Entities::encode($destsymb,'"<>&');
|
$r->headers_out->add('Set-cookie' => $defaultcookie); |
$destination .= 'destinationurl='.$destsymb;
|
} |
}
|
$r->send_http_header; |
}
|
|
if ($destination =~ m{^/adm/roles}) {
|
my %lt=&Apache::lonlocal::texthash( |
$destination .= ($destination =~ /\?/) ? '&' : '?';
|
'wel' => 'Welcome', |
$destination .= 'source=login';
|
'pro' => 'Login problems?', |
}
|
); |
|
my $loginhelp = &loginhelpdisplay($domain); |
my $windowname = 'loncapaclient';
|
if ($loginhelp) { |
if ($env{'request.lti.login'}) {
|
$loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>'; |
$windowname .= 'lti';
|
} |
}
|
|
my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";');
|
my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>'); |
my $brcrum = [{'href' => '',
|
$r->print(<<ENDSUCCESS); |
'text' => 'Successful Login'},];
|
$start_page |
my $args = {'bread_crumbs' => $brcrum,};
|
$setflags |
unless ((defined($form->{role})) || (defined($form->{symb}))) {
|
$windowinfo |
my $update=$env{'user.update.time'};
|
<h1>$lt{'wel'}</h1> |
if (!$update) {
|
$welcome |
$update = $env{'user.login.time'};
|
$loginhelp |
}
|
$remoteinfo |
my %roles_in_env;
|
$maincall |
my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
|
$continuelink |
if ($showcount == 1) {
|
$end_page |
foreach my $rolecode (keys(%roles_in_env)) {
|
ENDSUCCESS |
my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
|
return; |
if ($cid) {
|
} |
my %coursedescription =
|
|
&Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
|
# --------------------------------------------------------------- Failed login! |
if ($coursedescription{'type'} eq 'Placement') {
|
|
$args->{'crstype'} = 'Placement';
|
sub failed { |
}
|
my ($r,$message,$form) = @_; |
last;
|
(undef,undef,undef,my $clientmathml,my $clientunicode) = |
}
|
&Apache::loncommon::decode_user_agent(); |
}
|
my $args = {}; |
}
|
if ($clientunicode && !$clientmathml) { |
}
|
$args = {'browser.unicode' => 1}; |
|
} |
# ------------------------------------------------- Output for successful login
|
|
|
my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args); |
&Apache::loncommon::content_type($r,'text/html');
|
my $uname = &Apache::loncommon::cleanup_html($form->{'uname'}); |
if ($securecookie) {
|
my $udom = &Apache::loncommon::cleanup_html($form->{'udom'}); |
$r->headers_out->add('Set-cookie' => $securecookie);
|
if (&Apache::lonnet::domain($udom,'description') eq '') { |
}
|
undef($udom); |
if ($defaultcookie) {
|
} |
$r->headers_out->add('Set-cookie' => $defaultcookie);
|
my $retry = '/adm/login'; |
}
|
if ($uname eq $form->{'uname'}) { |
$r->send_http_header;
|
$retry .= '?username='.$uname; |
|
} |
my ($start_page,$js,$pagebody,$end_page);
|
if ($udom) { |
if ($env{'request.lti.login'}) {
|
$retry .= (($retry=~/\?/)?'&':'?').'domain='.$udom; |
$args = {'only_body' => 1};
|
} |
if ($env{'request.lti.target'} eq '') {
|
if (exists($form->{role})) { |
my $ltitarget = (($destination =~ /\?/) ? '&' : '?').
|
my $role = &Apache::loncommon::cleanup_html($form->{role}); |
'ltitarget=iframe';
|
if ($role ne '') { |
$js = <<"ENDJS";
|
$retry .= (($retry=~/\?/)?'&':'?').'role='.$role; |
|
} |
<script type="text/javascript">
|
} |
// <![CDATA[
|
if (exists($form->{symb})) { |
function setLTItarget() {
|
my $symb = &Apache::loncommon::cleanup_html($form->{symb}); |
var newloc = '$destination';
|
if ($symb ne '') { |
if (parent !== window) {
|
$retry .= (($retry=~/\?/)?'&':'?').'symb='.$symb; |
newloc += '$ltitarget';
|
} |
}
|
} |
window.location.href=newloc;
|
my $end_page = &Apache::loncommon::end_page(); |
}
|
&Apache::loncommon::content_type($r,'text/html'); |
// ]]>
|
$r->send_http_header; |
</script>
|
my @actions = |
|
(&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>')); |
ENDJS
|
my $loginhelp = &loginhelpdisplay($udom); |
$args->{'add_entries'} = {'onload' => "javascript:setLTItarget();"};
|
if ($loginhelp) { |
$pagebody = '<noscript><span class="LC_warning">'
|
push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>'); |
.&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
|
} |
.'</span></noscript>';
|
#FIXME: link to helpdesk might be added here |
} else {
|
|
$args->{'redirect'} = [0,$destination,1];
|
$r->print( |
}
|
$start_page |
$start_page=&Apache::loncommon::start_page('',$js,$args);
|
.'<h2>'.&mt('Sorry ...').'</h2>' |
} else {
|
.&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />' |
$args->{'redirect'} = [0,$destination];
|
.&Apache::lonhtmlcommon::actionbox(\@actions) |
$start_page=&Apache::loncommon::start_page('Successful Login',
|
.$end_page |
$js,$args);
|
); |
|
} |
my %lt=&Apache::lonlocal::texthash(
|
|
'wel' => 'Welcome',
|
# ------------------------------------------------------------------ Rerouting! |
'pro' => 'Login problems?',
|
|
);
|
sub reroute { |
$pagebody = "<h1>$lt{'wel'}</h1>\n".
|
my ($r) = @_; |
&mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
|
&Apache::loncommon::content_type($r,'text/html'); |
my $loginhelp = &loginhelpdisplay($domain);
|
$r->send_http_header; |
if ($loginhelp) {
|
my $msg='<b>'.&mt('Sorry ...').'</b><br />' |
$pagebody .= '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
|
.&mt('Please [_1]log in again[_2].'); |
}
|
&Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1}); |
}
|
} |
$end_page = &Apache::loncommon::end_page();
|
|
$r->print(<<ENDSUCCESS);
|
# ---------------------------------------------------------------- Main handler |
$start_page
|
|
$windowinfo
|
sub handler { |
$pagebody
|
my $r = shift; |
$end_page
|
my $londocroot = $r->dir_config('lonDocRoot'); |
ENDSUCCESS
|
# Are we re-routing? |
return;
|
if (-e "$londocroot/lon-status/reroute.txt") { |
}
|
&reroute($r); |
|
return OK; |
# --------------------------------------------------------------- Failed login!
|
} |
|
|
sub failed {
|
&Apache::lonlocal::get_language_handle($r); |
my ($r,$message,$form) = @_;
|
|
(undef,undef,undef,my $clientmathml,my $clientunicode) =
|
# -------------------------------- Prevent users from attempting to login twice |
&Apache::loncommon::decode_user_agent();
|
my $handle = &Apache::lonnet::check_for_valid_session($r); |
my $args = {};
|
if ($handle ne '') { |
if ($clientunicode && !$clientmathml) {
|
my $lonidsdir=$r->dir_config('lonIDsDir'); |
$args = {'browser.unicode' => 1};
|
if ($handle=~/^publicuser\_/) { |
}
|
# For "public user" - remove it, we apparently really want to login |
|
unlink($r->dir_config('lonIDsDir')."/$handle.id"); |
my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
|
} else { |
my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
|
# Indeed, a valid token is found |
my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
|
&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle); |
if (&Apache::lonnet::domain($udom,'description') eq '') {
|
&Apache::loncommon::content_type($r,'text/html'); |
undef($udom);
|
$r->send_http_header; |
}
|
my $start_page = |
my $retry = '/adm/login';
|
&Apache::loncommon::start_page('Already logged in'); |
if ($uname eq $form->{'uname'}) {
|
my $end_page = |
$retry .= '?username='.$uname;
|
&Apache::loncommon::end_page(); |
}
|
my $dest = '/adm/roles'; |
if ($udom) {
|
if ($env{'form.firsturl'} ne '') { |
$retry .= (($retry=~/\?/)?'&':'?').'domain='.$udom;
|
$dest = $env{'form.firsturl'}; |
}
|
} |
if (exists($form->{role})) {
|
$r->print( |
my $role = &Apache::loncommon::cleanup_html($form->{role});
|
$start_page |
if ($role ne '') {
|
.'<p class="LC_warning">'.&mt('You are already logged in!').'</p>' |
$retry .= (($retry=~/\?/)?'&':'?').'role='.$role;
|
.'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].' |
}
|
,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>') |
}
|
.'</p>' |
if (exists($form->{symb})) {
|
.$end_page |
my $symb = &Apache::loncommon::cleanup_html($form->{symb});
|
); |
if ($symb ne '') {
|
return OK; |
$retry .= (($retry=~/\?/)?'&':'?').'symb='.$symb;
|
} |
}
|
} |
}
|
|
if (exists($form->{firsturl})) {
|
# ---------------------------------------------------- No valid token, continue |
my $firsturl = &Apache::loncommon::cleanup_html($form->{firsturl});
|
|
if ($firsturl ne '') {
|
|
$retry .= (($retry=~/\?/)?'&':'?').'firsturl='.$firsturl;
|
my $buffer; |
if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
|
if ($r->header_in('Content-length') > 0) { |
unless (exists($form->{linkprot})) {
|
$r->read($buffer,$r->header_in('Content-length'),0); |
if (exists($form->{linkkey})) {
|
} |
$retry .= 'linkkey='.$form->{linkkey};
|
my %form; |
}
|
foreach my $pair (split(/&/,$buffer)) { |
}
|
my ($name,$value) = split(/=/,$pair); |
}
|
$value =~ tr/+/ /; |
}
|
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; |
}
|
$form{$name}=$value; |
if (exists($form->{linkprot})) {
|
} |
my $ltoken = &Apache::lonnet::tmpput({linkprot => $form->{'linkprot'}},
|
|
$r->dir_config('lonHostID'));
|
if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) { |
if ($ltoken) {
|
&failed($r,'Username, password and domain need to be specified.', |
$retry .= (($retry =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;
|
\%form); |
}
|
return OK; |
}
|
} |
my $end_page = &Apache::loncommon::end_page();
|
|
&Apache::loncommon::content_type($r,'text/html');
|
# split user logging in and "su"-user |
$r->send_http_header;
|
|
my @actions =
|
($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'}); |
(&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
|
$form{'uname'} = &LONCAPA::clean_username($form{'uname'}); |
my $loginhelp = &loginhelpdisplay($udom);
|
$form{'suname'}= &LONCAPA::clean_username($form{'suname'}); |
if ($loginhelp) {
|
$form{'udom'} = &LONCAPA::clean_domain($form{'udom'}); |
push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
|
$form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'}); |
}
|
|
#FIXME: link to helpdesk might be added here
|
my $role = $r->dir_config('lonRole'); |
|
my $domain = $r->dir_config('lonDefDomain'); |
$r->print(
|
my $prodir = $r->dir_config('lonUsersDir'); |
$start_page
|
my $contact_name = &mt('LON-CAPA helpdesk'); |
.'<h2>'.&mt('Sorry ...').'</h2>'
|
|
.&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
|
# ---------------------------------------- Get the information from login token |
.&Apache::lonhtmlcommon::actionbox(\@actions)
|
|
.$end_page
|
my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'}, |
);
|
$form{'serverid'}); |
}
|
|
|
if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') || |
# ------------------------------------------------------------------ Rerouting!
|
($tmpinfo eq 'no_such_host')) { |
|
&failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form); |
sub reroute {
|
return OK; |
my ($r) = @_;
|
} else { |
&Apache::loncommon::content_type($r,'text/html');
|
my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'}, |
$r->send_http_header;
|
$form{'serverid'}); |
my $msg='<b>'.&mt('Sorry ...').'</b><br />'
|
if ( $reply ne 'ok' ) { |
.&mt('Please [_1]log in again[_2].');
|
&failed($r,'Session could not be opened.',\%form); |
&Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
|
&Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token"); |
}
|
return OK; |
|
} |
# ---------------------------------------------------------------- Main handler
|
} |
|
|
sub handler {
|
if (!&Apache::lonnet::domain($form{'udom'})) { |
my $r = shift;
|
&failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form); |
my $londocroot = $r->dir_config('lonDocRoot');
|
return OK; |
# Are we re-routing?
|
} |
if (-e "$londocroot/lon-status/reroute.txt") {
|
|
&reroute($r);
|
my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr)=split(/&/,$tmpinfo); |
return OK;
|
if ($rolestr) { |
}
|
$rolestr = &unescape($rolestr); |
|
} |
&Apache::lonlocal::get_language_handle($r);
|
if ($symbstr) { |
|
$symbstr= &unescape($symbstr); |
# -------------------------------- Prevent users from attempting to login twice
|
} |
my $handle = &Apache::lonnet::check_for_valid_session($r);
|
if ($iptokenstr) { |
if ($handle ne '') {
|
$iptokenstr = &unescape($iptokenstr); |
my $lonidsdir=$r->dir_config('lonIDsDir');
|
} |
if ($handle=~/^publicuser\_/) {
|
if ($rolestr =~ /^role=/) { |
# For "public user" - remove it, we apparently really want to login
|
(undef,$form{'role'}) = split('=',$rolestr); |
unlink($r->dir_config('lonIDsDir')."/$handle.id");
|
} |
} else {
|
if ($symbstr =~ /^symb=/) { |
# Indeed, a valid token is found
|
(undef,$form{'symb'}) = split('=',$symbstr); |
&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
|
} |
&Apache::loncommon::content_type($r,'text/html');
|
if ($iptokenstr =~ /^iptoken=/) { |
$r->send_http_header;
|
(undef,$form{'iptoken'}) = split('=',$iptokenstr); |
my $start_page =
|
} |
&Apache::loncommon::start_page('Already logged in');
|
|
my $end_page =
|
my $upass = &Apache::loncommon::des_decrypt($key,$form{'upass0'}); |
&Apache::loncommon::end_page();
|
|
my $dest = '/adm/roles';
|
# ---------------------------------------------------------------- Authenticate |
if ($env{'form.firsturl'} ne '') {
|
|
$dest = $env{'form.firsturl'};
|
my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'}); |
if (($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) &&
|
my ($cancreate,$statustocreate) = |
($env{'request.course.id'})) {
|
&Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'}); |
my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
|
my $defaultauth; |
my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
|
if (ref($cancreate) eq 'ARRAY') { |
my $symb = &Apache::loncommon::symb_from_tinyurl($env{'form.firsturl'},$cnum,$cdom);
|
if (grep(/^login$/,@{$cancreate})) { |
if ($symb) {
|
$defaultauth = 1; |
my $buffer;
|
} |
if ($r->header_in('Content-length') > 0) {
|
} |
$r->read($buffer,$r->header_in('Content-length'),0);
|
my $clientcancheckhost = 1; |
}
|
my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass, |
my %form;
|
$form{'udom'},$defaultauth, |
foreach my $pair (split(/&/,$buffer)) {
|
$clientcancheckhost); |
my ($name,$value) = split(/=/,$pair);
|
|
$value =~ tr/+/ /;
|
# --------------------------------------------------------------------- Failed? |
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
|
|
$form{$name}=$value;
|
if ($authhost eq 'no_host') { |
}
|
&failed($r,'Username and/or password could not be authenticated.', |
&set_deeplink_login(%form);
|
\%form); |
} else {
|
return OK; |
$r->print(
|
} elsif ($authhost eq 'no_account_on_host') { |
$start_page
|
if ($defaultauth) { |
.'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
|
my $domdesc = &Apache::lonnet::domain($form{'udom'},'description'); |
.'<p>'.&mt('Please [_1]log out[_2] first, and then try your access again',
|
unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) { |
'<a href="/adm/logout">','</a>')
|
return OK; |
.'</p>'
|
} |
.$end_page);
|
my $start_page = |
return OK;
|
&Apache::loncommon::start_page('Create a user account in LON-CAPA', |
}
|
'',{'no_inline_link' => 1,}); |
}
|
my $lonhost = $r->dir_config('lonHostID'); |
}
|
my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'}; |
$r->print(
|
my $contacts = |
$start_page
|
&Apache::loncommon::build_recipient_list(undef,'helpdeskmail', |
.'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
|
$form{'udom'},$origmail); |
.'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
|
my ($contact_email) = split(',',$contacts); |
,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
|
my $output = |
.'</p>'
|
&Apache::createaccount::username_check($form{'uname'},$form{'udom'}, |
.$end_page
|
$domdesc,'',$lonhost, |
);
|
$contact_email,$contact_name, |
return OK;
|
undef,$statustocreate); |
}
|
&Apache::loncommon::content_type($r,'text/html'); |
}
|
$r->send_http_header; |
|
&Apache::createaccount::print_header($r,$start_page); |
# ---------------------------------------------------- No valid token, continue
|
$r->print('<h3>'.&mt('Account creation').'</h3>'. |
|
&mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'. |
|
$output.&Apache::loncommon::end_page()); |
my $buffer;
|
return OK; |
if ($r->header_in('Content-length') > 0) {
|
} else { |
$r->read($buffer,$r->header_in('Content-length'),0);
|
&failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form); |
}
|
return OK; |
my %form;
|
} |
foreach my $pair (split(/&/,$buffer)) {
|
} |
my ($name,$value) = split(/=/,$pair);
|
|
$value =~ tr/+/ /;
|
if (($firsturl eq '') || |
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
|
($firsturl=~/^\/adm\/(logout|remote)/)) { |
$form{$name}=$value;
|
$firsturl='/adm/roles'; |
}
|
} |
|
|
if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
|
my $hosthere; |
&failed($r,'Username, password and domain need to be specified.',
|
if ($form{'iptoken'}) { |
\%form);
|
my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'}); |
return OK;
|
my $delete = &Apache::lonnet::tmpdel($form{'iptoken'}); |
}
|
if (($sessiondata{'domain'} eq $form{'udom'}) && |
|
($sessiondata{'username'} eq $form{'uname'})) { |
# split user logging in and "su"-user
|
$hosthere = 1; |
|
} |
($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
|
} |
$form{'uname'} = &LONCAPA::clean_username($form{'uname'});
|
|
$form{'suname'}= &LONCAPA::clean_username($form{'suname'});
|
# --------------------------------- Are we attempting to login as somebody else? |
$form{'udom'} = &LONCAPA::clean_domain($form{'udom'});
|
if ($form{'suname'}) { |
$form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
|
my ($suname,$sudom,$sudomref); |
|
$suname = $form{'suname'}; |
my $role = $r->dir_config('lonRole');
|
$sudom = $form{'udom'}; |
my $domain = $r->dir_config('lonDefDomain');
|
if ($form{'sudom'}) { |
my $prodir = $r->dir_config('lonUsersDir');
|
unless ($sudom eq $form{'sudom'}) { |
my $contact_name = &mt('LON-CAPA helpdesk');
|
if (&Apache::lonnet::domain($form{'sudom'})) { |
|
$sudomref = [$form{'sudom'}]; |
# ---------------------------------------- Get the information from login token
|
$sudom = $form{'sudom'}; |
|
} |
my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
|
} |
$form{'serverid'});
|
} |
|
# ------------ see if the original user has enough privileges to pull this stunt |
if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
|
if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) { |
($tmpinfo eq 'no_such_host')) {
|
# ---------------------------------------------------- see if the su-user exists |
&failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
|
unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') { |
return OK;
|
# ------------------------------ see if the su-user is not too highly privileged |
} else {
|
if (&Apache::lonnet::privileged($suname,$sudom)) { |
my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
|
&Apache::lonnet::logthis('Attempted switch user to privileged user'); |
$form{'serverid'});
|
} else { |
if ( $reply ne 'ok' ) {
|
my $noprivswitch; |
&failed($r,'Session could not be opened.',\%form);
|
# |
&Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
|
# su-user's home server and user's home server must have one of: |
return OK;
|
# (a) same domain |
}
|
# (b) same primary library server for the two domains |
}
|
# (c) same "internet domain" for primary library server(s) for home servers' domains |
|
# |
if (!&Apache::lonnet::domain($form{'udom'})) {
|
my $suprim = &Apache::lonnet::domain($sudom,'primary'); |
&failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
|
my $suintdom = &Apache::lonnet::internet_dom($suprim); |
return OK;
|
unless ($sudom eq $form{'udom'}) { |
}
|
my $uprim = &Apache::lonnet::domain($form{'udom'},'primary'); |
|
my $uintdom = &Apache::lonnet::internet_dom($uprim); |
my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr,$linkstr)=split(/&/,$tmpinfo);
|
unless ($suprim eq $uprim) { |
if ($rolestr) {
|
unless ($suintdom eq $uintdom) { |
$rolestr = &unescape($rolestr);
|
&Apache::lonnet::logthis('Attempted switch user ' |
}
|
.'to user with different "internet domain".'); |
if ($symbstr) {
|
$noprivswitch = 1; |
$symbstr= &unescape($symbstr);
|
} |
}
|
} |
if ($iptokenstr) {
|
} |
$iptokenstr = &unescape($iptokenstr);
|
|
}
|
unless ($noprivswitch) { |
if ($linkstr) {
|
# |
$linkstr = &unescape($linkstr);
|
# server where log-in occurs must have same "internet domain" as su-user's home |
}
|
# server |
if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
|
# |
$form{'firsturl'} = $firsturl;
|
my $lonhost = $r->dir_config('lonHostID'); |
}
|
my $hostintdom = &Apache::lonnet::internet_dom($lonhost); |
if ($rolestr =~ /^role=/) {
|
if ($hostintdom ne $suintdom) { |
(undef,$form{'role'}) = split('=',$rolestr);
|
&Apache::lonnet::logthis('Attempted switch user on a ' |
}
|
.'server with a different "internet domain".'); |
if ($symbstr =~ /^symb=/) {
|
} else { |
(undef,$form{'symb'}) = split('=',$symbstr);
|
|
}
|
# -------------------------------------------------------- actually switch users |
if ($iptokenstr =~ /^iptoken=/) {
|
|
(undef,$form{'iptoken'}) = split('=',$iptokenstr);
|
&Apache::lonnet::logperm('User '.$form{'uname'}.' at '. |
}
|
$form{'udom'}.' logging in as '.$suname.':'.$sudom); |
if ($linkstr =~ /^linkprot=/) {
|
$form{'uname'}=$suname; |
(undef,$form{'linkprot'}) = split('=',$linkstr);
|
if ($form{'udom'} ne $sudom) { |
} elsif ($linkstr =~ /^linkkey=/) {
|
$form{'udom'}=$sudom; |
(undef,$form{'linkkey'}) = split('=',$linkstr);
|
} |
}
|
} |
|
} |
my $upass = $ENV{HTTPS} ? $form{'upass0'}
|
} |
: &Apache::loncommon::des_decrypt($key,$form{'upass0'});
|
} |
|
} else { |
# ---------------------------------------------------------------- Authenticate
|
&Apache::lonnet::logthis('Non-privileged user attempting switch user'); |
|
} |
my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
|
} |
my ($cancreate,$statustocreate) =
|
|
&Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
|
my ($is_balancer,$otherserver); |
my $defaultauth;
|
|
if (ref($cancreate) eq 'ARRAY') {
|
unless ($hosthere) { |
if (grep(/^login$/,@{$cancreate})) {
|
($is_balancer,$otherserver) = |
$defaultauth = 1;
|
&Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login'); |
}
|
if ($is_balancer) { |
}
|
# Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer) |
my $clientcancheckhost = 1;
|
my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r); |
my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
|
if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) { |
$form{'udom'},$defaultauth,
|
$otherserver = $found_server; |
$clientcancheckhost);
|
} |
|
if ($otherserver eq '') { |
# --------------------------------------------------------------------- Failed?
|
my $lowest_load; |
|
($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'}); |
if ($authhost eq 'no_host') {
|
if ($lowest_load > 100) { |
&failed($r,'Username and/or password could not be authenticated.',
|
$otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'}); |
\%form);
|
} |
return OK;
|
} |
} elsif ($authhost eq 'no_account_on_host') {
|
if ($otherserver ne '') { |
if ($defaultauth) {
|
my @hosts = &Apache::lonnet::current_machine_ids(); |
my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
|
if (grep(/^\Q$otherserver\E$/,@hosts)) { |
unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
|
$hosthere = $otherserver; |
return OK;
|
} |
}
|
} |
my $start_page =
|
} |
&Apache::loncommon::start_page('Create a user account in LON-CAPA');
|
} |
my $lonhost = $r->dir_config('lonHostID');
|
|
my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
|
if (($is_balancer) && (!$hosthere)) { |
my $contacts =
|
if ($otherserver) { |
&Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
|
&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef, |
$form{'udom'},$origmail);
|
\%form); |
my ($contact_email) = split(',',$contacts);
|
my $switchto = '/adm/switchserver?otherserver='.$otherserver; |
my $output =
|
if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) { |
&Apache::createaccount::username_check($form{'uname'},$form{'udom'},
|
$switchto .= '&origurl='.$firsturl; |
$domdesc,'',$lonhost,
|
} |
$contact_email,$contact_name,
|
if ($form{'role'}) { |
undef,$statustocreate);
|
$switchto .= '&role='.$form{'role'}; |
&Apache::loncommon::content_type($r,'text/html');
|
} |
$r->send_http_header;
|
if ($form{'symb'}) { |
&Apache::createaccount::print_header($r,$start_page);
|
$switchto .= '&symb='.$form{'symb'}; |
$r->print('<h3>'.&mt('Account creation').'</h3>'.
|
} |
&mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
|
$r->internal_redirect($switchto); |
$output.&Apache::loncommon::end_page());
|
} else { |
return OK;
|
&Apache::loncommon::content_type($r,'text/html'); |
} else {
|
$r->send_http_header; |
&failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);
|
$r->print(&noswitch()); |
return OK;
|
} |
}
|
return OK; |
}
|
} else { |
|
if (!&check_can_host($r,\%form,$authhost)) { |
if (($firsturl eq '') ||
|
my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'}); |
($firsturl=~/^\/adm\/(logout|remote)/)) {
|
if ($otherserver) { |
$firsturl='/adm/roles';
|
&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef, |
}
|
\%form); |
|
my $switchto = '/adm/switchserver?otherserver='.$otherserver; |
my $hosthere;
|
if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) { |
if ($form{'iptoken'}) {
|
$switchto .= '&origurl='.$firsturl; |
my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
|
} |
my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
|
if ($form{'role'}) { |
if (($sessiondata{'domain'} eq $form{'udom'}) &&
|
$switchto .= '&role='.$form{'role'}; |
($sessiondata{'username'} eq $form{'uname'})) {
|
} |
$hosthere = 1;
|
if ($form{'symb'}) { |
}
|
$switchto .= '&symb='.$form{'symb'}; |
}
|
} |
|
$r->internal_redirect($switchto); |
# --------------------------------- Are we attempting to login as somebody else?
|
} else { |
if ($form{'suname'}) {
|
&Apache::loncommon::content_type($r,'text/html'); |
my ($suname,$sudom,$sudomref);
|
$r->send_http_header; |
$suname = $form{'suname'};
|
$r->print(&noswitch()); |
$sudom = $form{'udom'};
|
} |
if ($form{'sudom'}) {
|
return OK; |
unless ($sudom eq $form{'sudom'}) {
|
} |
if (&Apache::lonnet::domain($form{'sudom'})) {
|
|
$sudomref = [$form{'sudom'}];
|
# ------------------------------------------------------- Do the load balancing |
$sudom = $form{'sudom'};
|
|
}
|
# ---------------------------------------------------------- Determine own load |
}
|
my $loadlim = $r->dir_config('lonLoadLim'); |
}
|
my $loadavg; |
# ------------ see if the original user has enough privileges to pull this stunt
|
{ |
if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
|
my $loadfile=Apache::File->new('/proc/loadavg'); |
# ---------------------------------------------------- see if the su-user exists
|
$loadavg=<$loadfile>; |
unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
|
} |
# ------------------------------ see if the su-user is not too highly privileged
|
$loadavg =~ s/\s.*//g; |
if (&Apache::lonnet::privileged($suname,$sudom)) {
|
my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim); |
&Apache::lonnet::logthis('Attempted switch user to privileged user');
|
my $userloadpercent=&Apache::lonnet::userload(); |
} else {
|
|
my $noprivswitch;
|
# ---------------------------------------------------------- Are we overloaded? |
#
|
if ((($userloadpercent>100.0)||($loadpercent>100.0))) { |
# su-user's home server and user's home server must have one of:
|
my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'}); |
# (a) same domain
|
if (!$unloaded) { |
# (b) same primary library server for the two domains
|
($unloaded) = &Apache::lonnet::choose_server($form{'udom'}); |
# (c) same "internet domain" for primary library server(s) for home servers' domains
|
} |
#
|
if ($unloaded) { |
my $suprim = &Apache::lonnet::domain($sudom,'primary');
|
&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect', |
my $suintdom = &Apache::lonnet::internet_dom($suprim);
|
undef,\%form); |
unless ($sudom eq $form{'udom'}) {
|
$r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl); |
my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
|
return OK; |
my $uintdom = &Apache::lonnet::internet_dom($uprim);
|
} |
unless ($suprim eq $uprim) {
|
} |
unless ($suintdom eq $uintdom) {
|
if (($is_balancer) && ($hosthere)) { |
&Apache::lonnet::logthis('Attempted switch user '
|
$form{'noloadbalance'} = $hosthere; |
.'to user with different "internet domain".');
|
} |
$noprivswitch = 1;
|
&success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef, |
}
|
\%form); |
}
|
return OK; |
}
|
} |
|
} |
unless ($noprivswitch) {
|
|
#
|
sub check_can_host { |
# server where log-in occurs must have same "internet domain" as su-user's home
|
my ($r,$form,$authhost,$domdesc) = @_; |
# server
|
return unless (ref($form) eq 'HASH'); |
#
|
my $canhost = 1; |
my $lonhost = $r->dir_config('lonHostID');
|
my $lonhost = $r->dir_config('lonHostID'); |
my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
|
my $udom = $form->{'udom'}; |
if ($hostintdom ne $suintdom) {
|
my @intdoms; |
&Apache::lonnet::logthis('Attempted switch user on a '
|
my $internet_names = &Apache::lonnet::get_internet_names($lonhost); |
.'server with a different "internet domain".');
|
if (ref($internet_names) eq 'ARRAY') { |
} else {
|
@intdoms = @{$internet_names}; |
|
} |
# -------------------------------------------------------- actually switch users
|
my $uprimary_id = &Apache::lonnet::domain($udom,'primary'); |
|
my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id); |
&Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
|
unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) { |
$form{'udom'}.' logging in as '.$suname.':'.$sudom);
|
my $machine_dom = &Apache::lonnet::host_domain($lonhost); |
$form{'uname'}=$suname;
|
my $hostname = &Apache::lonnet::hostname($lonhost); |
if ($form{'udom'} ne $sudom) {
|
my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname); |
$form{'udom'}=$sudom;
|
my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID); |
}
|
my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom); |
}
|
my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom); |
}
|
my $loncaparev; |
}
|
if ($authhost eq 'no_account_on_host') { |
}
|
$loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom); |
} else {
|
} else { |
&Apache::lonnet::logthis('Non-privileged user attempting switch user');
|
$loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost); |
}
|
} |
}
|
$canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev, |
|
$udomdefaults{'remotesessions'}, |
my ($is_balancer,$otherserver);
|
$defdomdefaults{'hostedsessions'}); |
|
} |
unless ($hosthere) {
|
unless ($canhost) { |
($is_balancer,$otherserver) =
|
if ($authhost eq 'no_account_on_host') { |
&Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
|
my $checkloginvia = 1; |
if ($is_balancer) {
|
my ($login_host,$hostname) = |
# Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
|
&Apache::lonnet::choose_server($udom,$checkloginvia); |
my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
|
&Apache::loncommon::content_type($r,'text/html'); |
if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
|
$r->send_http_header; |
$otherserver = $found_server;
|
if ($login_host ne '') { |
}
|
my $protocol = $Apache::lonnet::protocol{$login_host}; |
if ($otherserver eq '') {
|
$protocol = 'http' if ($protocol ne 'https'); |
my $lowest_load;
|
my $newurl = $protocol.'://'.$hostname.'/adm/createaccount'; |
($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
|
$r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA'). |
if ($lowest_load > 100) {
|
'<h3>'.&mt('Account creation').'</h3>'. |
$otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});
|
&mt('You do not currently have a LON-CAPA account at this institution.').'<br />'. |
}
|
'<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'. |
}
|
'<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>'). |
if ($otherserver ne '') {
|
&Apache::loncommon::end_page()); |
my @hosts = &Apache::lonnet::current_machine_ids();
|
} else { |
if (grep(/^\Q$otherserver\E$/,@hosts)) {
|
$r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable'). |
$hosthere = $otherserver;
|
'<h3>'.&mt('Account creation unavailable').'</h3>'. |
}
|
&mt('You do not currently have a LON-CAPA account at this institution.').'<br />'. |
}
|
'<p>'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'</p>'. |
}
|
&Apache::loncommon::end_page()); |
}
|
} |
|
} else { |
if (($is_balancer) && (!$hosthere)) {
|
&success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef, |
if ($otherserver) {
|
$form); |
&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
|
my ($otherserver) = &Apache::lonnet::choose_server($udom); |
\%form);
|
$r->internal_redirect('/adm/switchserver?otherserver='.$otherserver); |
my $switchto = '/adm/switchserver?otherserver='.$otherserver;
|
} |
if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
|
} |
$switchto .= '&origurl='.$firsturl; #should escape
|
return $canhost; |
}
|
} |
if ($form{'role'}) {
|
|
$switchto .= '&role='.$form{'role'};
|
sub noswitch { |
}
|
my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable'). |
if ($form{'symb'}) {
|
'<h3>'.&mt('Session unavailable').'</h3>'. |
$switchto .= '&symb='.$form{'symb'};
|
&mt('This LON-CAPA server is unable to host your session.').'<br />'. |
}
|
'<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'. |
if ($form{'linkprot'}) {
|
&Apache::loncommon::end_page(); |
$env{'request.linkprot'} = $form{'linkprot'};
|
return $result; |
} elsif ($form{'linkkey'}) {
|
} |
$env{'request.linkkey'} = $form{'linkkey'};
|
|
}
|
sub loginhelpdisplay { |
if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
|
my ($authdomain) = @_; |
&set_deeplink_login(%form);
|
my $login_help = 1; |
}
|
my $lang = &Apache::lonlocal::current_language(); |
$r->internal_redirect($switchto);
|
if ($login_help) { |
} else {
|
my $dom = $authdomain; |
&Apache::loncommon::content_type($r,'text/html');
|
if ($dom eq '') { |
$r->send_http_header;
|
$dom = &Apache::lonnet::default_login_domain(); |
$r->print(&noswitch());
|
} |
}
|
my %domconfhash = &Apache::loncommon::get_domainconf($dom); |
return OK;
|
my $loginhelp_url; |
} else {
|
if ($lang) { |
if (!&check_can_host($r,\%form,$authhost)) {
|
$loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang}; |
my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
|
if ($loginhelp_url ne '') { |
if ($otherserver) {
|
return $loginhelp_url; |
&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
|
} |
\%form);
|
} |
my $switchto = '/adm/switchserver?otherserver='.$otherserver;
|
$loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'}; |
if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
|
if ($loginhelp_url ne '') { |
$switchto .= '&origurl='.$firsturl; #should escape
|
return $loginhelp_url; |
}
|
} else { |
if ($form{'role'}) {
|
return '/adm/loginproblems.html'; |
$switchto .= '&role='.$form{'role'};
|
} |
}
|
} |
if ($form{'symb'}) {
|
return; |
$switchto .= '&symb='.$form{'symb'};
|
} |
}
|
|
if ($form{'linkprot'}) {
|
1; |
$env{'request.linkprot'} = $form{'linkprot'};
|
__END__ |
} elsif ($form{'linkkey'}) {
|
|
$env{'request.linkkey'} = $form{'linkkey'};
|
|
}
|
|
if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
|
|
&set_deeplink_login(%form);
|
|
}
|
|
$r->internal_redirect($switchto);
|
|
} else {
|
|
&Apache::loncommon::content_type($r,'text/html');
|
|
$r->send_http_header;
|
|
$r->print(&noswitch());
|
|
}
|
|
return OK;
|
|
}
|
|
|
|
# ------------------------------------------------------- Do the load balancing
|
|
|
|
# ---------------------------------------------------------- Determine own load
|
|
my $loadlim = $r->dir_config('lonLoadLim');
|
|
my $loadavg;
|
|
{
|
|
my $loadfile=Apache::File->new('/proc/loadavg');
|
|
$loadavg=<$loadfile>;
|
|
}
|
|
$loadavg =~ s/\s.*//g;
|
|
my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
|
|
my $userloadpercent=&Apache::lonnet::userload();
|
|
|
|
# ---------------------------------------------------------- Are we overloaded?
|
|
if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
|
|
my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});
|
|
if (!$unloaded) {
|
|
($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
|
|
}
|
|
if ($unloaded) {
|
|
&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
|
|
undef,\%form);
|
|
if ($form{'linkprot'}) {
|
|
$env{'request.linkprot'} = $form{'linkprot'};
|
|
} elsif ($form{'linkkey'}) {
|
|
$env{'request.linkkey'} = $form{'linkkey'};
|
|
}
|
|
if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
|
|
&set_deeplink_login(%form);
|
|
}
|
|
$r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
|
|
return OK;
|
|
}
|
|
}
|
|
if (($is_balancer) && ($hosthere)) {
|
|
$form{'noloadbalance'} = $hosthere;
|
|
}
|
|
my $extra_env;
|
|
if ($form{'linkprot'}) {
|
|
my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);
|
|
if ($linkprotector) {
|
|
$extra_env = {'user.linkprotector' => $linkprotector,
|
|
'user.linkproturi' => $uri};
|
|
}
|
|
} elsif ($form{'linkkey'}) {
|
|
$extra_env = {'user.deeplinkkey' => $form{'linkkey'},
|
|
'user.keyedlinkuri' => $form{'firsturl'}};
|
|
}
|
|
if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
|
|
&set_deeplink_login(%form);
|
|
if ($env{'request.deeplink.login'}) {
|
|
if (ref($extra_env) eq 'HASH') {
|
|
%{$extra_env} = ( %{$extra_env}, 'request.deeplink.login' => $form{'firsturl'} );
|
|
} else {
|
|
$extra_env = {'request.deeplink.login' => $form{'firsturl'}};
|
|
}
|
|
}
|
|
}
|
|
&success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,
|
|
\%form);
|
|
return OK;
|
|
}
|
|
}
|
|
|
|
sub set_deeplink_login {
|
|
my (%form) = @_;
|
|
if ($form{'firsturl'} =~ m{^/tiny/($match_domain)/\w+$}) {
|
|
my $cdom = $1;
|
|
my ($cnum,$symb) = &Apache::loncommon::symb_from_tinyurl($form{'firsturl'},'',$cdom);
|
|
if ($symb) {
|
|
my $disallow;
|
|
my $deeplink = &Apache::lonnet::EXT("resource.0.deeplink",$symb);
|
|
if ($deeplink ne '') {
|
|
my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
|
|
if (($protect ne 'none') && ($protect ne '')) {
|
|
my ($acctype,$item) = split(/:/,$protect);
|
|
if (($acctype eq 'ltic') || ($acctype eq 'ltid')) {
|
|
unless ($form{'linkprot'} eq $protect) {
|
|
$disallow = 1;
|
|
}
|
|
} elsif ($acctype eq 'key') {
|
|
unless (form{'linkkey'} eq $item) {
|
|
$disallow = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
unless ($disallow) {
|
|
$env{'request.deeplink.login'} = $form{'firsturl'};
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
sub check_can_host {
|
|
my ($r,$form,$authhost,$domdesc) = @_;
|
|
return unless (ref($form) eq 'HASH');
|
|
my $canhost = 1;
|
|
my $lonhost = $r->dir_config('lonHostID');
|
|
my $udom = $form->{'udom'};
|
|
my @intdoms;
|
|
my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
|
|
if (ref($internet_names) eq 'ARRAY') {
|
|
@intdoms = @{$internet_names};
|
|
}
|
|
my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
|
|
my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
|
|
unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
|
|
my $machine_dom = &Apache::lonnet::host_domain($lonhost);
|
|
my $hostname = &Apache::lonnet::hostname($lonhost);
|
|
my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
|
|
my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
|
|
my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
|
|
my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
|
|
my $loncaparev;
|
|
if ($authhost eq 'no_account_on_host') {
|
|
$loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
|
|
} else {
|
|
$loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
|
|
}
|
|
$canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
|
|
$udomdefaults{'remotesessions'},
|
|
$defdomdefaults{'hostedsessions'});
|
|
}
|
|
unless ($canhost) {
|
|
if ($authhost eq 'no_account_on_host') {
|
|
my $checkloginvia = 1;
|
|
my ($login_host,$hostname) =
|
|
&Apache::lonnet::choose_server($udom,$checkloginvia);
|
|
&Apache::loncommon::content_type($r,'text/html');
|
|
$r->send_http_header;
|
|
if ($login_host ne '') {
|
|
my $protocol = $Apache::lonnet::protocol{$login_host};
|
|
$protocol = 'http' if ($protocol ne 'https');
|
|
my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);
|
|
$hostname = $alias if ($alias ne '');
|
|
my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
|
|
#FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host
|
|
$r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
|
|
'<h3>'.&mt('Account creation').'</h3>'.
|
|
&mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
|
|
'<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
|
|
'<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
|
|
&Apache::loncommon::end_page());
|
|
} else {
|
|
$r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
|
|
'<h3>'.&mt('Account creation unavailable').'</h3>'.
|
|
&mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
|
|
'<p>'.&mt('Currently a LON-CAPA server is not available within the [_1] domain for you to log-in to, to create an account.',$domdesc).'</p>'.
|
|
&Apache::loncommon::end_page());
|
|
}
|
|
} else {
|
|
&success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
|
|
$form);
|
|
if ($form->{'linkprot'}) {
|
|
$env{'request.linkprot'} = $form->{'linkprot'};
|
|
} elsif ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
|
|
if ($form->{'linkkey'}) {
|
|
$env{'request.linkkey'} = $form->{'linkkey'};
|
|
}
|
|
$env{'request.deeplink.login'} = $form->{'firsturl'};
|
|
}
|
|
my ($otherserver) = &Apache::lonnet::choose_server($udom);
|
|
$r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
|
|
}
|
|
}
|
|
return $canhost;
|
|
}
|
|
|
|
sub noswitch {
|
|
my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
|
|
'<h3>'.&mt('Session unavailable').'</h3>'.
|
|
&mt('This LON-CAPA server is unable to host your session.').'<br />'.
|
|
'<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
|
|
&Apache::loncommon::end_page();
|
|
return $result;
|
|
}
|
|
|
|
sub loginhelpdisplay {
|
|
my ($authdomain) = @_;
|
|
my $login_help = 1;
|
|
my $lang = &Apache::lonlocal::current_language();
|
|
if ($login_help) {
|
|
my $dom = $authdomain;
|
|
if ($dom eq '') {
|
|
$dom = &Apache::lonnet::default_login_domain();
|
|
}
|
|
my %domconfhash = &Apache::loncommon::get_domainconf($dom);
|
|
my $loginhelp_url;
|
|
if ($lang) {
|
|
$loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
|
|
if ($loginhelp_url ne '') {
|
|
return $loginhelp_url;
|
|
}
|
|
}
|
|
$loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
|
|
if ($loginhelp_url ne '') {
|
|
return $loginhelp_url;
|
|
} else {
|
|
return '/adm/loginproblems.html';
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
1;
|
|
__END__
|
|
|
|
|