Annotation of loncom/auth/lonauth.pm, revision 1.164
1.136 kruse 1: # The LearningOnline Network
2: # User Authentication Module
3: #
1.164 ! raeburn 4: # $Id: lonauth.pm,v 1.163 2021/05/04 18:47:37 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;
389: my $start_page =
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.164 ! raeburn 396: if (($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) &&
! 397: ($env{'request.course.id'})) {
! 398: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
! 399: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
! 400: my $symb = &Apache::loncommon::symb_from_tinyurl($env{'form.firsturl'},$cnum,$cdom);
! 401: if ($symb) {
! 402: my $buffer;
! 403: if ($r->header_in('Content-length') > 0) {
! 404: $r->read($buffer,$r->header_in('Content-length'),0);
! 405: }
! 406: my %form;
! 407: foreach my $pair (split(/&/,$buffer)) {
! 408: my ($name,$value) = split(/=/,$pair);
! 409: $value =~ tr/+/ /;
! 410: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
! 411: $form{$name}=$value;
! 412: }
! 413: &set_deeplink_login(%form);
! 414: } else {
! 415: $r->print(
! 416: $start_page
! 417: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
! 418: .'<p>'.&mt('Please [_1]log out[_2] first, and then try your access again',
! 419: '<a href="/adm/logout">','</a>')
! 420: .'</p>'
! 421: .$end_page);
! 422: return OK;
! 423: }
1.157 raeburn 424: }
1.136 kruse 425: }
426: $r->print(
427: $start_page
428: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
429: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
430: ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
431: .'</p>'
432: .$end_page
433: );
434: return OK;
435: }
436: }
437:
438: # ---------------------------------------------------- No valid token, continue
439:
440:
441: my $buffer;
442: if ($r->header_in('Content-length') > 0) {
443: $r->read($buffer,$r->header_in('Content-length'),0);
444: }
445: my %form;
446: foreach my $pair (split(/&/,$buffer)) {
447: my ($name,$value) = split(/=/,$pair);
448: $value =~ tr/+/ /;
449: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
450: $form{$name}=$value;
451: }
452:
453: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
454: &failed($r,'Username, password and domain need to be specified.',
455: \%form);
456: return OK;
457: }
458:
459: # split user logging in and "su"-user
460:
1.145 raeburn 461: ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
1.136 kruse 462: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
463: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
1.145 raeburn 464: $form{'udom'} = &LONCAPA::clean_domain($form{'udom'});
465: $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
1.136 kruse 466:
467: my $role = $r->dir_config('lonRole');
468: my $domain = $r->dir_config('lonDefDomain');
469: my $prodir = $r->dir_config('lonUsersDir');
470: my $contact_name = &mt('LON-CAPA helpdesk');
471:
472: # ---------------------------------------- Get the information from login token
473:
474: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
475: $form{'serverid'});
476:
477: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
478: ($tmpinfo eq 'no_such_host')) {
479: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
480: return OK;
481: } else {
482: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
483: $form{'serverid'});
484: if ( $reply ne 'ok' ) {
485: &failed($r,'Session could not be opened.',\%form);
486: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
487: return OK;
488: }
489: }
490:
491: if (!&Apache::lonnet::domain($form{'udom'})) {
492: &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
493: return OK;
494: }
495:
1.157 raeburn 496: my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr,$linkstr)=split(/&/,$tmpinfo);
1.136 kruse 497: if ($rolestr) {
498: $rolestr = &unescape($rolestr);
499: }
500: if ($symbstr) {
501: $symbstr= &unescape($symbstr);
502: }
1.138 raeburn 503: if ($iptokenstr) {
504: $iptokenstr = &unescape($iptokenstr);
505: }
1.157 raeburn 506: if ($linkstr) {
507: $linkstr = &unescape($linkstr);
1.156 raeburn 508: }
1.155 raeburn 509: if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
510: $form{'firsturl'} = $firsturl;
511: }
1.136 kruse 512: if ($rolestr =~ /^role=/) {
513: (undef,$form{'role'}) = split('=',$rolestr);
514: }
515: if ($symbstr =~ /^symb=/) {
516: (undef,$form{'symb'}) = split('=',$symbstr);
517: }
1.138 raeburn 518: if ($iptokenstr =~ /^iptoken=/) {
519: (undef,$form{'iptoken'}) = split('=',$iptokenstr);
520: }
1.157 raeburn 521: if ($linkstr =~ /^linkprot=/) {
522: (undef,$form{'linkprot'}) = split('=',$linkstr);
523: } elsif ($linkstr =~ /^linkkey=/) {
524: (undef,$form{'linkkey'}) = split('=',$linkstr);
1.156 raeburn 525: }
1.136 kruse 526:
1.139 raeburn 527: my $upass = $ENV{HTTPS} ? $form{'upass0'}
528: : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
1.136 kruse 529:
530: # ---------------------------------------------------------------- Authenticate
531:
532: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
533: my ($cancreate,$statustocreate) =
534: &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
535: my $defaultauth;
536: if (ref($cancreate) eq 'ARRAY') {
537: if (grep(/^login$/,@{$cancreate})) {
538: $defaultauth = 1;
539: }
540: }
541: my $clientcancheckhost = 1;
542: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
543: $form{'udom'},$defaultauth,
544: $clientcancheckhost);
1.149 raeburn 545:
1.136 kruse 546: # --------------------------------------------------------------------- Failed?
547:
548: if ($authhost eq 'no_host') {
549: &failed($r,'Username and/or password could not be authenticated.',
550: \%form);
551: return OK;
552: } elsif ($authhost eq 'no_account_on_host') {
553: if ($defaultauth) {
554: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
555: unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
556: return OK;
557: }
558: my $start_page =
559: &Apache::loncommon::start_page('Create a user account in LON-CAPA');
560: my $lonhost = $r->dir_config('lonHostID');
561: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
562: my $contacts =
563: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
564: $form{'udom'},$origmail);
565: my ($contact_email) = split(',',$contacts);
566: my $output =
567: &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
568: $domdesc,'',$lonhost,
569: $contact_email,$contact_name,
570: undef,$statustocreate);
571: &Apache::loncommon::content_type($r,'text/html');
572: $r->send_http_header;
573: &Apache::createaccount::print_header($r,$start_page);
574: $r->print('<h3>'.&mt('Account creation').'</h3>'.
575: &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
576: $output.&Apache::loncommon::end_page());
577: return OK;
578: } else {
579: &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);
580: return OK;
581: }
582: }
583:
584: if (($firsturl eq '') ||
585: ($firsturl=~/^\/adm\/(logout|remote)/)) {
586: $firsturl='/adm/roles';
587: }
588:
589: my $hosthere;
590: if ($form{'iptoken'}) {
1.138 raeburn 591: my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
592: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
1.136 kruse 593: if (($sessiondata{'domain'} eq $form{'udom'}) &&
594: ($sessiondata{'username'} eq $form{'uname'})) {
595: $hosthere = 1;
596: }
597: }
598:
599: # --------------------------------- Are we attempting to login as somebody else?
600: if ($form{'suname'}) {
1.145 raeburn 601: my ($suname,$sudom,$sudomref);
602: $suname = $form{'suname'};
603: $sudom = $form{'udom'};
604: if ($form{'sudom'}) {
605: unless ($sudom eq $form{'sudom'}) {
606: if (&Apache::lonnet::domain($form{'sudom'})) {
607: $sudomref = [$form{'sudom'}];
608: $sudom = $form{'sudom'};
609: }
610: }
611: }
1.136 kruse 612: # ------------ see if the original user has enough privileges to pull this stunt
1.145 raeburn 613: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
1.136 kruse 614: # ---------------------------------------------------- see if the su-user exists
1.145 raeburn 615: unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
1.136 kruse 616: # ------------------------------ see if the su-user is not too highly privileged
1.146 raeburn 617: if (&Apache::lonnet::privileged($suname,$sudom)) {
618: &Apache::lonnet::logthis('Attempted switch user to privileged user');
619: } else {
620: my $noprivswitch;
1.145 raeburn 621: #
622: # su-user's home server and user's home server must have one of:
1.147 raeburn 623: # (a) same domain
624: # (b) same primary library server for the two domains
625: # (c) same "internet domain" for primary library server(s) for home servers' domains
1.145 raeburn 626: #
1.148 raeburn 627: my $suprim = &Apache::lonnet::domain($sudom,'primary');
628: my $suintdom = &Apache::lonnet::internet_dom($suprim);
1.145 raeburn 629: unless ($sudom eq $form{'udom'}) {
1.148 raeburn 630: my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
631: my $uintdom = &Apache::lonnet::internet_dom($uprim);
1.145 raeburn 632: unless ($suprim eq $uprim) {
633: unless ($suintdom eq $uintdom) {
634: &Apache::lonnet::logthis('Attempted switch user '
1.146 raeburn 635: .'to user with different "internet domain".');
636: $noprivswitch = 1;
1.145 raeburn 637: }
638: }
639: }
640:
1.146 raeburn 641: unless ($noprivswitch) {
642: #
643: # server where log-in occurs must have same "internet domain" as su-user's home
644: # server
645: #
646: my $lonhost = $r->dir_config('lonHostID');
647: my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
1.148 raeburn 648: if ($hostintdom ne $suintdom) {
1.146 raeburn 649: &Apache::lonnet::logthis('Attempted switch user on a '
650: .'server with a different "internet domain".');
651: } else {
652:
1.136 kruse 653: # -------------------------------------------------------- actually switch users
1.145 raeburn 654:
1.146 raeburn 655: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
656: $form{'udom'}.' logging in as '.$suname.':'.$sudom);
657: $form{'uname'}=$suname;
658: if ($form{'udom'} ne $sudom) {
659: $form{'udom'}=$sudom;
660: }
661: }
1.145 raeburn 662: }
1.136 kruse 663: }
664: }
665: } else {
666: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
667: }
668: }
669:
670: my ($is_balancer,$otherserver);
671:
672: unless ($hosthere) {
673: ($is_balancer,$otherserver) =
1.141 raeburn 674: &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
675: if ($is_balancer) {
1.161 raeburn 676: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
677: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
678: if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
679: $otherserver = $found_server;
680: }
1.141 raeburn 681: if ($otherserver eq '') {
682: my $lowest_load;
683: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
684: if ($lowest_load > 100) {
1.163 raeburn 685: $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});
1.141 raeburn 686: }
687: }
688: if ($otherserver ne '') {
689: my @hosts = &Apache::lonnet::current_machine_ids();
690: if (grep(/^\Q$otherserver\E$/,@hosts)) {
691: $hosthere = $otherserver;
692: }
693: }
694: }
1.136 kruse 695: }
696:
1.141 raeburn 697: if (($is_balancer) && (!$hosthere)) {
1.136 kruse 698: if ($otherserver) {
699: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
700: \%form);
701: my $switchto = '/adm/switchserver?otherserver='.$otherserver;
702: if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
1.156 raeburn 703: $switchto .= '&origurl='.$firsturl; #should escape
1.136 kruse 704: }
705: if ($form{'role'}) {
706: $switchto .= '&role='.$form{'role'};
707: }
708: if ($form{'symb'}) {
709: $switchto .= '&symb='.$form{'symb'};
710: }
1.156 raeburn 711: if ($form{'linkprot'}) {
712: $env{'request.linkprot'} = $form{'linkprot'};
1.164 ! raeburn 713: } elsif ($form{'linkkey'}) {
! 714: $env{'request.linkkey'} = $form{'linkkey'};
! 715: }
! 716: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
! 717: &set_deeplink_login(%form);
1.156 raeburn 718: }
1.136 kruse 719: $r->internal_redirect($switchto);
720: } else {
1.160 raeburn 721: &Apache::loncommon::content_type($r,'text/html');
722: $r->send_http_header;
1.136 kruse 723: $r->print(&noswitch());
724: }
725: return OK;
726: } else {
727: if (!&check_can_host($r,\%form,$authhost)) {
728: my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
729: if ($otherserver) {
730: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
731: \%form);
732: my $switchto = '/adm/switchserver?otherserver='.$otherserver;
733: if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
1.156 raeburn 734: $switchto .= '&origurl='.$firsturl; #should escape
1.136 kruse 735: }
736: if ($form{'role'}) {
737: $switchto .= '&role='.$form{'role'};
738: }
739: if ($form{'symb'}) {
740: $switchto .= '&symb='.$form{'symb'};
741: }
1.156 raeburn 742: if ($form{'linkprot'}) {
743: $env{'request.linkprot'} = $form{'linkprot'};
1.164 ! raeburn 744: } elsif ($form{'linkkey'}) {
! 745: $env{'request.linkkey'} = $form{'linkkey'};
! 746: }
! 747: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
! 748: &set_deeplink_login(%form);
1.156 raeburn 749: }
1.136 kruse 750: $r->internal_redirect($switchto);
751: } else {
1.160 raeburn 752: &Apache::loncommon::content_type($r,'text/html');
753: $r->send_http_header;
1.136 kruse 754: $r->print(&noswitch());
755: }
756: return OK;
757: }
758:
759: # ------------------------------------------------------- Do the load balancing
760:
761: # ---------------------------------------------------------- Determine own load
762: my $loadlim = $r->dir_config('lonLoadLim');
763: my $loadavg;
764: {
765: my $loadfile=Apache::File->new('/proc/loadavg');
766: $loadavg=<$loadfile>;
767: }
768: $loadavg =~ s/\s.*//g;
769: my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
770: my $userloadpercent=&Apache::lonnet::userload();
771:
772: # ---------------------------------------------------------- Are we overloaded?
773: if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
1.163 raeburn 774: my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});
1.136 kruse 775: if (!$unloaded) {
776: ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
777: }
778: if ($unloaded) {
779: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
780: undef,\%form);
1.156 raeburn 781: if ($form{'linkprot'}) {
782: $env{'request.linkprot'} = $form{'linkprot'};
1.164 ! raeburn 783: } elsif ($form{'linkkey'}) {
! 784: $env{'request.linkkey'} = $form{'linkkey'};
! 785: }
! 786: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
! 787: &set_deeplink_login(%form);
1.156 raeburn 788: }
1.136 kruse 789: $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
790: return OK;
791: }
792: }
1.141 raeburn 793: if (($is_balancer) && ($hosthere)) {
794: $form{'noloadbalance'} = $hosthere;
795: }
1.156 raeburn 796: my $extra_env;
797: if ($form{'linkprot'}) {
798: my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);
799: if ($linkprotector) {
800: $extra_env = {'user.linkprotector' => $linkprotector,
1.164 ! raeburn 801: 'user.linkproturi' => $uri};
1.156 raeburn 802: }
1.164 ! raeburn 803: } elsif ($form{'linkkey'}) {
! 804: $extra_env = {'user.deeplinkkey' => $form{'linkkey'},
! 805: 'user.keyedlinkuri' => $form{'firsturl'}};
! 806: }
! 807: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
! 808: &set_deeplink_login(%form);
! 809: if ($env{'request.deeplink.login'}) {
! 810: if (ref($extra_env) eq 'HASH') {
! 811: %{$extra_env} = ( %{$extra_env}, 'request.deeplink.login' => $form{'firsturl'} );
! 812: } else {
! 813: $extra_env = {'request.deeplink.login' => $form{'firsturl'}};
! 814: }
1.157 raeburn 815: }
1.156 raeburn 816: }
817: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,
1.136 kruse 818: \%form);
819: return OK;
820: }
821: }
822:
1.164 ! raeburn 823: sub set_deeplink_login {
! 824: my (%form) = @_;
! 825: if ($form{'firsturl'} =~ m{^/tiny/($match_domain)/\w+$}) {
! 826: my $cdom = $1;
! 827: my ($cnum,$symb) = &Apache::loncommon::symb_from_tinyurl($form{'firsturl'},'',$cdom);
! 828: if ($symb) {
! 829: my $disallow;
! 830: my $deeplink = &Apache::lonnet::EXT("resource.0.deeplink",$symb);
! 831: if ($deeplink ne '') {
! 832: my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
! 833: if (($protect ne 'none') && ($protect ne '')) {
! 834: my ($acctype,$item) = split(/:/,$protect);
! 835: if (($acctype eq 'ltic') || ($acctype eq 'ltid')) {
! 836: unless ($form{'linkprot'} eq $protect) {
! 837: $disallow = 1;
! 838: }
! 839: } elsif ($acctype eq 'key') {
! 840: unless (form{'linkkey'} eq $item) {
! 841: $disallow = 1;
! 842: }
! 843: }
! 844: }
! 845: }
! 846: unless ($disallow) {
! 847: $env{'request.deeplink.login'} = $form{'firsturl'};
! 848: }
! 849: }
! 850: }
! 851: return;
! 852: }
! 853:
1.136 kruse 854: sub check_can_host {
855: my ($r,$form,$authhost,$domdesc) = @_;
856: return unless (ref($form) eq 'HASH');
857: my $canhost = 1;
858: my $lonhost = $r->dir_config('lonHostID');
859: my $udom = $form->{'udom'};
860: my @intdoms;
861: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
862: if (ref($internet_names) eq 'ARRAY') {
863: @intdoms = @{$internet_names};
864: }
865: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
866: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
867: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
868: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
869: my $hostname = &Apache::lonnet::hostname($lonhost);
870: my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
871: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
872: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
873: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
874: my $loncaparev;
875: if ($authhost eq 'no_account_on_host') {
876: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
877: } else {
878: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
879: }
880: $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
881: $udomdefaults{'remotesessions'},
882: $defdomdefaults{'hostedsessions'});
883: }
884: unless ($canhost) {
885: if ($authhost eq 'no_account_on_host') {
886: my $checkloginvia = 1;
887: my ($login_host,$hostname) =
888: &Apache::lonnet::choose_server($udom,$checkloginvia);
889: &Apache::loncommon::content_type($r,'text/html');
890: $r->send_http_header;
891: if ($login_host ne '') {
892: my $protocol = $Apache::lonnet::protocol{$login_host};
893: $protocol = 'http' if ($protocol ne 'https');
1.163 raeburn 894: my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);
895: $hostname = $alias if ($alias ne '');
1.136 kruse 896: my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
1.156 raeburn 897: #FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host
1.136 kruse 898: $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
899: '<h3>'.&mt('Account creation').'</h3>'.
900: &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
901: '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
902: '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
903: &Apache::loncommon::end_page());
904: } else {
905: $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
906: '<h3>'.&mt('Account creation unavailable').'</h3>'.
907: &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
908: '<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>'.
909: &Apache::loncommon::end_page());
910: }
911: } else {
912: &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
913: $form);
1.156 raeburn 914: if ($form->{'linkprot'}) {
915: $env{'request.linkprot'} = $form->{'linkprot'};
1.157 raeburn 916: } elsif ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
917: if ($form->{'linkkey'}) {
918: $env{'request.linkkey'} = $form->{'linkkey'};
919: }
920: $env{'request.deeplink.login'} = $form->{'firsturl'};
1.156 raeburn 921: }
1.136 kruse 922: my ($otherserver) = &Apache::lonnet::choose_server($udom);
923: $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
924: }
925: }
926: return $canhost;
927: }
928:
929: sub noswitch {
930: my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
931: '<h3>'.&mt('Session unavailable').'</h3>'.
932: &mt('This LON-CAPA server is unable to host your session.').'<br />'.
933: '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
934: &Apache::loncommon::end_page();
935: return $result;
936: }
937:
938: sub loginhelpdisplay {
939: my ($authdomain) = @_;
940: my $login_help = 1;
941: my $lang = &Apache::lonlocal::current_language();
942: if ($login_help) {
943: my $dom = $authdomain;
944: if ($dom eq '') {
945: $dom = &Apache::lonnet::default_login_domain();
946: }
947: my %domconfhash = &Apache::loncommon::get_domainconf($dom);
948: my $loginhelp_url;
949: if ($lang) {
950: $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
951: if ($loginhelp_url ne '') {
952: return $loginhelp_url;
953: }
954: }
955: $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
956: if ($loginhelp_url ne '') {
957: return $loginhelp_url;
958: } else {
959: return '/adm/loginproblems.html';
960: }
961: }
962: return;
963: }
964:
965: 1;
966: __END__
967:
968:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>