Annotation of loncom/auth/lonauth.pm, revision 1.131

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.27      www         3: #
1.131   ! raeburn     4: # $Id: lonauth.pm,v 1.130 2014/01/05 11:04:27 raeburn Exp $
1.27      www         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: #
1.1       albertel   28: 
                     29: package Apache::lonauth;
                     30: 
1.18      albertel   31: use strict;
1.78      albertel   32: use LONCAPA;
1.1       albertel   33: use Apache::Constants qw(:common);
                     34: use CGI qw(:standard);
1.26      harris41   35: use DynaLoader; # for Crypt::DES version
1.8       www        36: use Crypt::DES;
1.45      matthew    37: use Apache::loncommon();
1.66      albertel   38: use Apache::lonnet;
1.12      www        39: use Apache::lonmenu();
1.90      raeburn    40: use Apache::createaccount;
1.18      albertel   41: use Fcntl qw(:flock);
1.56      www        42: use Apache::lonlocal;
1.119     raeburn    43: use Apache::File();
1.101     raeburn    44: use HTML::Entities;
1.85      albertel   45:  
1.1       albertel   46: # ------------------------------------------------------------ Successful login
1.85      albertel   47: sub success {
                     48:     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
                     49: 	$form) = @_;
1.1       albertel   50: 
1.85      albertel   51: # ------------------------------------------------------------ Get cookie ready
                     52:     my $cookie =
                     53: 	&Apache::loncommon::init_user_environment($r, $username, $domain,
                     54: 						  $authhost, $form,
1.86      albertel   55: 						  {'extra_env' => $extra_env,});
1.4       www        56: 
1.69      albertel   57:     my $public=($username eq 'public' && $domain eq 'public');
                     58: 
1.85      albertel   59:     if ($public or $lowerurl eq 'noredirect') { return $cookie; }
1.78      albertel   60: 
1.7       www        61: # -------------------------------------------------------------------- Log this
                     62: 
                     63:     &Apache::lonnet::log($domain,$username,$authhost,
                     64:                          "Login $ENV{'REMOTE_ADDR'}");
1.4       www        65: 
1.14      www        66: # ------------------------------------------------- Check for critical messages
                     67: 
1.21      www        68:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14      www        69:     if ($what[0]) {
1.22      www        70: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21      www        71: 	    $lowerurl='/adm/email?critical=display';
1.14      www        72:         }
                     73:     }
                     74: 
1.5       www        75: # ------------------------------------------------------------ Get cookie ready
1.1       albertel   76:     $cookie="lonID=$cookie; path=/";
1.12      www        77: # -------------------------------------------------------- Menu script and info
1.100     raeburn    78:     my $destination = $lowerurl;
                     79: 
                     80:     if (defined($form->{role})) {
                     81:         my $envkey = 'user.role.'.$form->{role};
                     82:         my $now=time;
                     83:         my $then=$env{'user.login.time'};
                     84:         my $refresh=$env{'user.refresh.time'};
1.111     raeburn    85:         my $update=$env{'user.update.time'};
                     86:         if (!$update) {
                     87:             $update = $then;
                     88:         }
1.100     raeburn    89:         if (exists($env{$envkey})) {
                     90:             my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
1.111     raeburn    91:             &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
1.100     raeburn    92:                                          \$trolecode,\$tstatus,\$tstart,\$tend);
                     93:             if ($tstatus eq 'is') {
1.101     raeburn    94:                 $destination  .= ($destination =~ /\?/) ? '&' : '?';
                     95:                 my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
                     96:                 $destination .= 'selectrole=1&'.$newrole.'=1';
1.100     raeburn    97:             }
                     98:         }
                     99:     }
1.101     raeburn   100:     if (defined($form->{symb})) {
                    101:         my $destsymb = $form->{symb};
                    102:         $destination  .= ($destination =~ /\?/) ? '&' : '?';
                    103:         if ($destsymb =~ /___/) {
                    104:             # FIXME Need to deal with encrypted symbs and urls as needed.
                    105:             my ($map,$resid,$desturl)=split(/___/,$destsymb);
1.124     raeburn   106:             unless ($desturl=~/^(adm|editupload|public)/) {
1.101     raeburn   107:                 $desturl = &Apache::lonnet::clutter($desturl);
                    108:             }
                    109:             $desturl = &HTML::Entities::encode($desturl,'"<>&');
                    110:             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
1.130     raeburn   111:             $destination .= 'destinationurl='.$desturl.
1.101     raeburn   112:                             '&destsymb='.$destsymb;
                    113:         } else {
                    114:             $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
1.130     raeburn   115:             $destination .= 'destinationurl='.$destsymb;
1.101     raeburn   116:         }
                    117:     }
1.111     raeburn   118:     if ($destination =~ m{^/adm/roles}) {
                    119:         $destination  .= ($destination =~ /\?/) ? '&' : '?';
                    120:         $destination .= 'source=login';
                    121:     }
1.100     raeburn   122: 
1.102     droeschl  123:     my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="loncapaclient";');
                    124:     my $header = '<meta HTTP-EQUIV="Refresh" CONTENT="0; url='.$destination.'" />';
1.99      bisitz    125:     my $brcrum = [{'href' => '',
                    126:                    'text' => 'Successful Login'},];
1.74      albertel  127:     my $start_page=&Apache::loncommon::start_page('Successful Login',
1.102     droeschl  128:                                                   $header,
1.104     droeschl  129:                                                   {'bread_crumbs' => $brcrum,});
1.74      albertel  130:     my $end_page  =&Apache::loncommon::end_page();
                    131: 
1.102     droeschl  132: 	my $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
1.5       www       133: # ------------------------------------------------- Output for successful login
                    134: 
1.74      albertel  135:     &Apache::loncommon::content_type($r,'text/html');
                    136:     $r->header_out('Set-cookie' => $cookie);
                    137:     $r->send_http_header;
1.1       albertel  138: 
1.58      www       139:     my %lt=&Apache::lonlocal::texthash(
                    140: 				       'wel' => 'Welcome',
1.92      bisitz    141: 				       'pro' => 'Login problems?',
1.58      www       142: 				       );
1.122     raeburn   143:     my $loginhelp = &loginhelpdisplay($domain);
                    144:     if ($loginhelp) {
                    145:         $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
                    146:     }
                    147: 
1.112     raeburn   148:     my $welcome = &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>'); 
1.1       albertel  149:     $r->print(<<ENDSUCCESS);
1.74      albertel  150: $start_page
1.19      www       151: $windowinfo
1.58      www       152: <h1>$lt{'wel'}</h1>
1.122     raeburn   153: $welcome
                    154: $loginhelp
1.64      albertel  155: $continuelink
1.74      albertel  156: $end_page
1.1       albertel  157: ENDSUCCESS
                    158: }
                    159: 
                    160: # --------------------------------------------------------------- Failed login!
                    161: 
                    162: sub failed {
1.85      albertel  163:     my ($r,$message,$form) = @_;
1.128     raeburn   164:     (undef,undef,undef,my $clientmathml,my $clientunicode) =
                    165:         &Apache::loncommon::decode_user_agent();
                    166:     my $args = {};
                    167:     if ($clientunicode && !$clientmathml) {
                    168:         $args = {'browser.unicode' => 1};
                    169:     }
                    170: 
                    171:     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
1.129     raeburn   172:     my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
                    173:     my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
                    174:     if (&Apache::lonnet::domain($udom,'description') eq '') {
                    175:         undef($udom);
1.130     raeburn   176:     }
1.129     raeburn   177:     my $retry = '/adm/login';
                    178:     if ($uname eq $form->{'uname'}) {
                    179:         $retry .= '?username='.$uname;
                    180:     }
1.128     raeburn   181:     if ($udom) {
1.129     raeburn   182:         $retry .= (($retry=~/\?/)?'&amp;':'?').'domain='.$udom;
1.128     raeburn   183:     }
1.100     raeburn   184:     if (exists($form->{role})) {
1.129     raeburn   185:         my $role = &Apache::loncommon::cleanup_html($form->{role});
1.130     raeburn   186:         if ($role ne '') {
1.129     raeburn   187:             $retry .= (($retry=~/\?/)?'&amp;':'?').'role='.$role;
                    188:         }
1.100     raeburn   189:     }
                    190:     if (exists($form->{symb})) {
1.129     raeburn   191:         my $symb = &Apache::loncommon::cleanup_html($form->{symb});
                    192:         if ($symb ne '') {
                    193:             $retry .= (($retry=~/\?/)?'&amp;':'?').'symb='.$symb;
                    194:         }
1.100     raeburn   195:     }
1.129     raeburn   196:     my $end_page = &Apache::loncommon::end_page();
1.74      albertel  197:     &Apache::loncommon::content_type($r,'text/html');
                    198:     $r->send_http_header;
1.128     raeburn   199:     my $loginhelp = &loginhelpdisplay($udom);
1.123     raeburn   200:     if ($loginhelp) {
                    201:         $loginhelp = '<p><a href="'.$loginhelp.'">'.&mt('Login problems?').'</a></p>';
                    202:     }
                    203: 
1.92      bisitz    204:     $r->print(
                    205:        $start_page
                    206:       .'<h1>'.&mt('Sorry ...').'</h1>'
1.95      bisitz    207:       .'<p class="LC_warning">'.&mt($message).'</p>'
1.100     raeburn   208:       .'<p>'.&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>')
1.92      bisitz    209:       .'</p>'
1.123     raeburn   210:       .$loginhelp
1.92      bisitz    211:       .$end_page
                    212:     );
                    213:  }
1.60      www       214: 
1.55      www       215: # ------------------------------------------------------------------ Rerouting!
                    216: 
                    217: sub reroute {
1.74      albertel  218:     my ($r) = @_;
                    219:     &Apache::loncommon::content_type($r,'text/html');
                    220:     $r->send_http_header;
1.126     bisitz    221:     my $msg='<b>'.&mt('Sorry ...').'</b><br />'
1.92      bisitz    222:            .&mt('Please [_1]log in again[_2].');
1.126     bisitz    223:     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
1.55      www       224: }
                    225: 
1.1       albertel  226: # ---------------------------------------------------------------- Main handler
                    227: 
                    228: sub handler {
                    229:     my $r = shift;
1.120     raeburn   230:     my $londocroot = $r->dir_config('lonDocRoot');
1.85      albertel  231:     my $form;
1.55      www       232: # Are we re-routing?
1.120     raeburn   233:     if (-e "$londocroot/lon-status/reroute.txt") {
1.55      www       234: 	&reroute($r);
                    235: 	return OK;
                    236:     }
1.56      www       237: 
1.57      www       238:     &Apache::lonlocal::get_language_handle($r);
1.1       albertel  239: 
1.59      www       240: # -------------------------------- Prevent users from attempting to login twice
1.89      albertel  241:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                    242:     if ($handle ne '') {
1.103     raeburn   243:         my $lonidsdir=$r->dir_config('lonIDsDir');
                    244:         if ($handle=~/^publicuser\_/) {
                    245: # For "public user" - remove it, we apparently really want to login
                    246:             unlink($r->dir_config('lonIDsDir')."/$handle.id");
                    247:         } else {
1.59      www       248: # Indeed, a valid token is found
1.103     raeburn   249:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
                    250: 	    &Apache::loncommon::content_type($r,'text/html');
                    251: 	    $r->send_http_header;
                    252: 	    my $start_page = 
                    253: 	        &Apache::loncommon::start_page('Already logged in');
                    254: 	    my $end_page = 
                    255: 	        &Apache::loncommon::end_page();
1.105     raeburn   256:             my $dest = '/adm/roles';
                    257:             if ($env{'form.firsturl'} ne '') {
                    258:                 $dest = $env{'form.firsturl'};
                    259:             }
1.103     raeburn   260:             $r->print(
                    261:                $start_page
1.125     raeburn   262:               .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
1.103     raeburn   263:               .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
1.105     raeburn   264:                     ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
1.103     raeburn   265:               .'</p>'
                    266:               .$end_page
                    267:             );
                    268:             return OK;
                    269:         }
1.59      www       270:     }
                    271: 
                    272: # ---------------------------------------------------- No valid token, continue
                    273: 
                    274: 
1.1       albertel  275:     my $buffer;
1.84      albertel  276:     if ($r->header_in('Content-length') > 0) {
                    277: 	$r->read($buffer,$r->header_in('Content-length'),0);
                    278:     }
1.85      albertel  279:     my %form;
                    280:     foreach my $pair (split(/&/,$buffer)) {
                    281:        my ($name,$value) = split(/=/,$pair);
1.7       www       282:        $value =~ tr/+/ /;
                    283:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.85      albertel  284:        $form{$name}=$value;
1.127     raeburn   285:     }
1.1       albertel  286: 
1.85      albertel  287:     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
                    288: 	&failed($r,'Username, password and domain need to be specified.',
                    289: 		\%form);
1.1       albertel  290:         return OK;
                    291:     }
1.61      www       292: 
                    293: # split user logging in and "su"-user
                    294: 
1.85      albertel  295:     ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
1.87      albertel  296:     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
                    297:     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
                    298:     $form{'udom'}  = &LONCAPA::clean_domain(  $form{'udom'});
1.1       albertel  299: 
                    300:     my $role   = $r->dir_config('lonRole');
                    301:     my $domain = $r->dir_config('lonDefDomain');
                    302:     my $prodir = $r->dir_config('lonUsersDir');
1.93      raeburn   303:     my $contact_name = &mt('LON-CAPA helpdesk');
1.1       albertel  304: 
1.8       www       305: # ---------------------------------------- Get the information from login token
                    306: 
1.85      albertel  307:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
                    308:                                       $form{'serverid'});
1.8       www       309: 
1.127     raeburn   310:     my %sessiondata;
                    311:     if ($form{'iptoken'}) {
                    312:         %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
                    313:         my $delete = &Apache::lonnet::tmpdel($form{'token'});
                    314:     }
                    315: 
1.114     raeburn   316:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') || 
                    317:         ($tmpinfo eq 'no_such_host')) {
1.85      albertel  318: 	&failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
1.8       www       319:         return OK;
1.44      www       320:     } else {
1.85      albertel  321: 	my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
                    322: 					   $form{'serverid'});
1.77      albertel  323:         if ( $reply ne 'ok' ) {
1.85      albertel  324:             &failed($r,'Session could not be opened.',\%form);
                    325: 	    &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
1.77      albertel  326: 	    return OK;
1.44      www       327: 	}
1.8       www       328:     }
1.100     raeburn   329: 
1.93      raeburn   330:     if (!&Apache::lonnet::domain($form{'udom'})) {
                    331:         &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
                    332:         return OK;
                    333:     }
1.100     raeburn   334: 
                    335:     my ($key,$firsturl,$rolestr,$symbstr)=split(/&/,$tmpinfo);
                    336:     if ($rolestr) {
                    337:         $rolestr = &unescape($rolestr);
                    338:     }
                    339:     if ($symbstr) {
                    340:         $symbstr= &unescape($symbstr);
                    341:     }
                    342:     if ($rolestr =~ /^role=/) {
                    343:         (undef,$form{'role'}) = split('=',$rolestr);
                    344:     }
                    345:     if ($symbstr =~ /^symb=/) { 
                    346:         (undef,$form{'symb'}) = split('=',$symbstr);
                    347:     }
1.8       www       348: 
                    349:     my $keybin=pack("H16",$key);
                    350: 
1.26      harris41  351:     my $cipher;
                    352:     if ($Crypt::DES::VERSION>=2.03) {
                    353: 	$cipher=new Crypt::DES $keybin;
                    354:     }
                    355:     else {
                    356: 	$cipher=new DES $keybin;
                    357:     }
1.67      www       358:     my $upass='';
                    359:     for (my $i=0;$i<=2;$i++) {
                    360: 	my $chunk=
1.85      albertel  361: 	    $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
1.8       www       362: 
1.67      www       363: 	$chunk.=
1.85      albertel  364: 	    $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
1.8       www       365: 
1.67      www       366: 	$chunk=substr($chunk,1,ord(substr($chunk,0,1)));
                    367: 	$upass.=$chunk;
                    368:     }
1.8       www       369: 
1.1       albertel  370: # ---------------------------------------------------------------- Authenticate
1.119     raeburn   371: 
1.90      raeburn   372:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
1.119     raeburn   373:     my ($cancreate,$statustocreate) =
                    374:         &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
                    375:     my $defaultauth;
                    376:     if (ref($cancreate) eq 'ARRAY') {
                    377:         if (grep(/^login$/,@{$cancreate})) {
                    378:             $defaultauth = 1;
1.90      raeburn   379:         }
                    380:     }
1.105     raeburn   381:     my $clientcancheckhost = 1;
1.90      raeburn   382:     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
1.105     raeburn   383:                                               $form{'udom'},$defaultauth,
                    384:                                               $clientcancheckhost);
1.1       albertel  385:     
                    386: # --------------------------------------------------------------------- Failed?
                    387: 
                    388:     if ($authhost eq 'no_host') {
1.85      albertel  389: 	&failed($r,'Username and/or password could not be authenticated.',
                    390: 		\%form);
1.1       albertel  391:         return OK;
1.90      raeburn   392:     } elsif ($authhost eq 'no_account_on_host') {
1.119     raeburn   393:         if ($defaultauth) {
1.105     raeburn   394:             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
1.110     raeburn   395:             unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
                    396:                 return OK;
                    397:             }
1.90      raeburn   398:             my $start_page = 
1.104     droeschl  399:                 &Apache::loncommon::start_page('Create a user account in LON-CAPA');
1.93      raeburn   400:             my $lonhost = $r->dir_config('lonHostID');
                    401:             my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
                    402:             my $contacts = 
                    403:                 &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
                    404:                                                         $form{'udom'},$origmail);
                    405:             my ($contact_email) = split(',',$contacts); 
1.119     raeburn   406:             my $output = 
                    407:                 &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
                    408:                                                        $domdesc,'',$lonhost,
                    409:                                                        $contact_email,$contact_name,
                    410:                                                        undef,$statustocreate);
1.90      raeburn   411:             &Apache::loncommon::content_type($r,'text/html');
                    412:             $r->send_http_header;
                    413:             &Apache::createaccount::print_header($r,$start_page);
1.94      raeburn   414:             $r->print('<h3>'.&mt('Account creation').'</h3>'.
                    415:                       &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
                    416:                       $output.&Apache::loncommon::end_page());
1.90      raeburn   417:             return OK;
                    418:         } else {
                    419:             &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);
                    420:             return OK;
                    421:         }
1.1       albertel  422:     }
                    423: 
1.59      www       424:     if (($firsturl eq '') || 
                    425: 	($firsturl=~/^\/adm\/(logout|remote)/)) {
1.24      www       426: 	$firsturl='/adm/roles';
1.7       www       427:     }
1.127     raeburn   428: 
                    429:     my $hosthere;
                    430:     if ($form{'iptoken'}) {
                    431:         if (($sessiondata{'domain'} eq $form{'udom'}) &&
                    432:             ($sessiondata{'username'} eq $form{'uname'})) {
                    433:             $hosthere = 1;
                    434:         }
                    435:     }
                    436: 
1.61      www       437: # --------------------------------- Are we attempting to login as somebody else?
1.85      albertel  438:     if ($form{'suname'}) {
1.61      www       439: # ------------ see if the original user has enough privileges to pull this stunt
1.85      albertel  440: 	if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
1.61      www       441: # ---------------------------------------------------- see if the su-user exists
1.85      albertel  442: 	    unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
1.61      www       443: 		eq 'no_host') {
1.85      albertel  444: 		&Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
1.61      www       445: # ------------------------------ see if the su-user is not too highly privileged
1.85      albertel  446: 		unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
1.61      www       447: # -------------------------------------------------------- actually switch users
1.85      albertel  448: 		    &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
                    449: 			' logging in as '.$form{'suname'});
                    450: 		    $form{'uname'}=$form{'suname'};
1.61      www       451: 		} else {
                    452: 		    &Apache::lonnet::logthis('Attempted switch user to privileged user');
                    453: 		}
                    454: 	    }
                    455: 	} else {
                    456: 	    &Apache::lonnet::logthis('Non-privileged user attempting switch user');
                    457: 	}
                    458:     }
1.85      albertel  459: 
1.127     raeburn   460:     my ($is_balancer,$otherserver);
                    461: 
                    462:     unless ($hosthere) {
                    463:         ($is_balancer,$otherserver) =
                    464:             &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'});
                    465:     }
1.117     raeburn   466: 
                    467:     if ($is_balancer) {
1.115     raeburn   468:         if (!$otherserver) { 
1.116     raeburn   469:             ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
1.115     raeburn   470:         }
                    471:         if ($otherserver) {
                    472:             &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                    473:                      \%form);
1.131   ! raeburn   474:             my $switchto = '/adm/switchserver?otherserver='.$otherserver;
        !           475:             if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
        !           476:                 $switchto .= '&origurl='.$firsturl;
        !           477:             }
        !           478:             if ($form{'role'}) {
        !           479:                 $switchto .= '&role='.$form{'role'};
        !           480:             }
        !           481:             if ($form{'symb'}) {
        !           482:                 $switchto .= '&symb='.$form{'symb'};
        !           483:             }
        !           484:             $r->internal_redirect($switchto);
1.115     raeburn   485:         } else {
                    486:             $r->print(&noswitch());
                    487:         }
1.110     raeburn   488:         return OK;
1.81      albertel  489:     } else {
1.115     raeburn   490:         if (!&check_can_host($r,\%form,$authhost)) {
1.118     raeburn   491:             my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
1.115     raeburn   492:             if ($otherserver) {
                    493:                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                    494:                          \%form);
1.131   ! raeburn   495:                 my $switchto = '/adm/switchserver?otherserver='.$otherserver;
        !           496:                 if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
        !           497:                     $switchto .= '&origurl='.$firsturl;
        !           498:                 }
        !           499:                 if ($form{'role'}) {
        !           500:                     $switchto .= '&role='.$form{'role'};
        !           501:                 }
        !           502:                 if ($form{'symb'}) {
        !           503:                     $switchto .= '&symb='.$form{'symb'};
        !           504:                 }
        !           505:                 $r->internal_redirect($switchto);
1.115     raeburn   506:             } else {
                    507:                 $r->print(&noswitch());
                    508:             }
                    509:             return OK;
                    510:         }
                    511: 
1.109     raeburn   512: # ------------------------------------------------------- Do the load balancing
                    513: 
                    514: # ---------------------------------------------------------- Determine own load
                    515:         my $loadlim = $r->dir_config('lonLoadLim');
                    516:         my $loadavg;
                    517:         {
                    518:             my $loadfile=Apache::File->new('/proc/loadavg');
                    519:             $loadavg=<$loadfile>;
                    520:         }
                    521:         $loadavg =~ s/\s.*//g;
                    522:         my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
                    523:         my $userloadpercent=&Apache::lonnet::userload();
                    524: 
                    525: # ---------------------------------------------------------- Are we overloaded?
                    526:         if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
                    527:             my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
1.115     raeburn   528:             if (!$unloaded) {
1.118     raeburn   529:                 ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
1.115     raeburn   530:             }
1.109     raeburn   531:             if ($unloaded) {
                    532:                 &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
                    533:                          undef,\%form);
                    534:                 $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
1.110     raeburn   535:                 return OK;
1.109     raeburn   536:             }
                    537:         }
                    538:         &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
                    539:                  \%form);
1.110     raeburn   540:         return OK;
1.81      albertel  541:     }
1.1       albertel  542: }
                    543: 
1.105     raeburn   544: sub check_can_host {
                    545:     my ($r,$form,$authhost,$domdesc) = @_;
                    546:     return unless (ref($form) eq 'HASH');
                    547:     my $canhost = 1;
1.106     raeburn   548:     my $lonhost = $r->dir_config('lonHostID');
1.105     raeburn   549:     my $udom = $form->{'udom'};
1.108     raeburn   550:     my @intdoms;
                    551:     my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
                    552:     if (ref($internet_names) eq 'ARRAY') {
                    553:         @intdoms = @{$internet_names};
                    554:     }
1.106     raeburn   555:     my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
                    556:     my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                    557:     unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
                    558:         my $machine_dom = &Apache::lonnet::host_domain($lonhost);
                    559:         my $hostname = &Apache::lonnet::hostname($lonhost);
                    560:         my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
                    561:         my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
                    562:         my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
1.105     raeburn   563:         my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
                    564:         my $loncaparev;
                    565:         if ($authhost eq 'no_account_on_host') {
1.106     raeburn   566:             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
1.105     raeburn   567:         } else {
1.106     raeburn   568:             $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
1.105     raeburn   569:         }
1.106     raeburn   570:         $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
                    571:                                                      $udomdefaults{'remotesessions'},
                    572:                                                      $defdomdefaults{'hostedsessions'});
1.105     raeburn   573:     }
                    574:     unless ($canhost) {
                    575:         if ($authhost eq 'no_account_on_host') {
1.115     raeburn   576:             my $checkloginvia = 1;
                    577:             my ($login_host,$hostname) = 
                    578:                 &Apache::lonnet::choose_server($udom,$checkloginvia);
1.105     raeburn   579:             &Apache::loncommon::content_type($r,'text/html');
                    580:             $r->send_http_header;
                    581:             if ($login_host ne '') {
                    582:                 my $protocol = $Apache::lonnet::protocol{$login_host};
                    583:                 $protocol = 'http' if ($protocol ne 'https');
                    584:                 my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
                    585:                 $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
                    586:                           '<h3>'.&mt('Account creation').'</h3>'.
                    587:                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
                    588:                           '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
                    589:                           '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
                    590:                           &Apache::loncommon::end_page());
                    591:             } else {
                    592:                 $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
                    593:                           '<h3>'.&mt('Account creation unavailable').'</h3>'.
                    594:                           &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
                    595:                           '<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>'.
                    596:                           &Apache::loncommon::end_page());
                    597:             }
                    598:         } else {
                    599:             &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
                    600:                      $form);
1.107     raeburn   601:             my ($otherserver) = &Apache::lonnet::choose_server($udom);
1.105     raeburn   602:             $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
                    603:         }
                    604:     }
1.110     raeburn   605:     return $canhost;
1.105     raeburn   606: }
                    607: 
1.115     raeburn   608: sub noswitch {
                    609:     my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
                    610:                  '<h3>'.&mt('Session unavailable').'</h3>'.
                    611:                  &mt('This LON-CAPA server is unable to host your session.').'<br />'.
                    612:                  '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
                    613:                  &Apache::loncommon::end_page();
                    614:     return $result;
                    615: }
                    616: 
1.122     raeburn   617: sub loginhelpdisplay {
                    618:     my ($authdomain) = @_;
                    619:     my $login_help = 1;
                    620:     my $lang = &Apache::lonlocal::current_language();
                    621:     if ($login_help) {
                    622:         my $dom = $authdomain;
                    623:         if ($dom eq '') {
                    624:             $dom = &Apache::lonnet::default_login_domain();
                    625:         }
                    626:         my %domconfhash = &Apache::loncommon::get_domainconf($dom);
                    627:         my $loginhelp_url;
                    628:         if ($lang) {
                    629:             $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
                    630:             if ($loginhelp_url ne '') {
                    631:                 return $loginhelp_url;
                    632:             }
                    633:         }
                    634:         $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
                    635:         if ($loginhelp_url ne '') {
                    636:             return $loginhelp_url;
                    637:         } else {
                    638:             return '/adm/loginproblems.html';
                    639:         }
                    640:     }
                    641:     return;
                    642: }
                    643: 
1.1       albertel  644: 1;
                    645: __END__
1.7       www       646: 
                    647: 

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