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