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