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