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