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