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