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