Annotation of loncom/auth/lonauth.pm, revision 1.168
1.136 kruse 1: # The LearningOnline Network
2: # User Authentication Module
3: #
1.168 ! raeburn 4: # $Id: lonauth.pm,v 1.167 2021/10/26 15:10:34 raeburn Exp $
1.136 kruse 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonauth;
30:
31: use strict;
1.140 raeburn 32: use LONCAPA qw(:DEFAULT :match);
1.136 kruse 33: use Apache::Constants qw(:common);
34: use CGI qw(:standard);
35: use Apache::loncommon();
36: use Apache::lonnet;
37: use Apache::lonmenu();
38: use Apache::createaccount;
1.149 raeburn 39: use Apache::ltiauth;
1.136 kruse 40: use Fcntl qw(:flock);
41: use Apache::lonlocal;
42: use Apache::File();
43: use HTML::Entities;
1.143 raeburn 44: use Digest::MD5;
1.136 kruse 45:
46: # ------------------------------------------------------------ Successful login
47: sub success {
48: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
1.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: }
1.168 ! raeburn 301: my $lonhost = $r->dir_config('lonHostID');
! 302: my $querystr;
! 303: my $result = &set_retry_token($form,$lonhost,\$querystr);
! 304: if ($result eq 'fail') {
! 305: if (exists($form->{role})) {
! 306: my $role = &Apache::loncommon::cleanup_html($form->{role});
! 307: if ($role ne '') {
! 308: $retry .= (($retry=~/\?/)?'&':'?').'role='.$role;
! 309: }
! 310: }
! 311: if (exists($form->{symb})) {
! 312: my $symb = &Apache::loncommon::cleanup_html($form->{symb});
! 313: if ($symb ne '') {
! 314: $retry .= (($retry=~/\?/)?'&':'?').'symb='.$symb;
! 315: }
! 316: }
! 317: if (exists($form->{firsturl})) {
! 318: my $firsturl = &Apache::loncommon::cleanup_html($form->{firsturl});
! 319: if ($firsturl ne '') {
! 320: $retry .= (($retry=~/\?/)?'&':'?').'firsturl='.$firsturl;
! 321: if ($form->{firsturl} =~ m{^/tiny/$match_domain/\w+$}) {
! 322: unless (exists($form->{linkprot})) {
! 323: if (exists($form->{linkkey})) {
! 324: $retry .= 'linkkey='.$form->{linkkey};
! 325: }
1.157 raeburn 326: }
327: }
328: }
1.155 raeburn 329: }
1.168 ! raeburn 330: if (exists($form->{linkprot})) {
! 331: my $ltoken = &Apache::lonnet::tmpput({linkprot => $form->{'linkprot'}},
! 332: $r->dir_config('lonHostID'));
! 333: if ($ltoken) {
! 334: $retry .= (($retry =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;
! 335: }
1.156 raeburn 336: }
1.168 ! raeburn 337: } elsif ($querystr ne '') {
! 338: $retry .= (($retry=~/\?/)?'&':'?').$querystr;
1.156 raeburn 339: }
1.136 kruse 340: my $end_page = &Apache::loncommon::end_page();
341: &Apache::loncommon::content_type($r,'text/html');
342: $r->send_http_header;
343: my @actions =
344: (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
345: my $loginhelp = &loginhelpdisplay($udom);
346: if ($loginhelp) {
347: push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
348: }
349: #FIXME: link to helpdesk might be added here
350:
351: $r->print(
352: $start_page
353: .'<h2>'.&mt('Sorry ...').'</h2>'
354: .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
355: .&Apache::lonhtmlcommon::actionbox(\@actions)
356: .$end_page
357: );
358: }
359:
360: # ------------------------------------------------------------------ Rerouting!
361:
362: sub reroute {
363: my ($r) = @_;
364: &Apache::loncommon::content_type($r,'text/html');
365: $r->send_http_header;
366: my $msg='<b>'.&mt('Sorry ...').'</b><br />'
367: .&mt('Please [_1]log in again[_2].');
368: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
369: }
370:
371: # ---------------------------------------------------------------- Main handler
372:
373: sub handler {
374: my $r = shift;
375: my $londocroot = $r->dir_config('lonDocRoot');
376: # Are we re-routing?
377: if (-e "$londocroot/lon-status/reroute.txt") {
378: &reroute($r);
379: return OK;
380: }
381:
382: &Apache::lonlocal::get_language_handle($r);
383:
384: # -------------------------------- Prevent users from attempting to login twice
385: my $handle = &Apache::lonnet::check_for_valid_session($r);
386: if ($handle ne '') {
387: my $lonidsdir=$r->dir_config('lonIDsDir');
388: if ($handle=~/^publicuser\_/) {
389: # For "public user" - remove it, we apparently really want to login
390: unlink($r->dir_config('lonIDsDir')."/$handle.id");
391: } else {
392: # Indeed, a valid token is found
393: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
394: &Apache::loncommon::content_type($r,'text/html');
395: $r->send_http_header;
1.166 raeburn 396: my $start_page =
1.136 kruse 397: &Apache::loncommon::start_page('Already logged in');
398: my $end_page =
399: &Apache::loncommon::end_page();
400: my $dest = '/adm/roles';
401: if ($env{'form.firsturl'} ne '') {
402: $dest = $env{'form.firsturl'};
1.166 raeburn 403: if (($dest =~ m{^/tiny/$match_domain/\w+$}) && ($env{'request.course.id'})) {
1.164 raeburn 404: my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
405: my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
1.166 raeburn 406: my $symb = &Apache::loncommon::symb_from_tinyurl($dest,$cnum,$cdom);
1.164 raeburn 407: if ($symb) {
408: my $buffer;
409: if ($r->header_in('Content-length') > 0) {
410: $r->read($buffer,$r->header_in('Content-length'),0);
411: }
412: my %form;
413: foreach my $pair (split(/&/,$buffer)) {
414: my ($name,$value) = split(/=/,$pair);
415: $value =~ tr/+/ /;
416: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
417: $form{$name}=$value;
418: }
419: &set_deeplink_login(%form);
420: } else {
421: $r->print(
422: $start_page
423: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
424: .'<p>'.&mt('Please [_1]log out[_2] first, and then try your access again',
425: '<a href="/adm/logout">','</a>')
426: .'</p>'
427: .$end_page);
428: return OK;
429: }
1.157 raeburn 430: }
1.136 kruse 431: }
432: $r->print(
433: $start_page
434: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
435: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
436: ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
437: .'</p>'
438: .$end_page
439: );
440: return OK;
441: }
442: }
443:
444: # ---------------------------------------------------- No valid token, continue
445:
446:
447: my $buffer;
448: if ($r->header_in('Content-length') > 0) {
449: $r->read($buffer,$r->header_in('Content-length'),0);
450: }
451: my %form;
452: foreach my $pair (split(/&/,$buffer)) {
453: my ($name,$value) = split(/=/,$pair);
454: $value =~ tr/+/ /;
455: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
456: $form{$name}=$value;
457: }
458:
459: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
460: &failed($r,'Username, password and domain need to be specified.',
461: \%form);
462: return OK;
463: }
464:
465: # split user logging in and "su"-user
466:
1.145 raeburn 467: ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
1.136 kruse 468: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
469: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
1.145 raeburn 470: $form{'udom'} = &LONCAPA::clean_domain($form{'udom'});
471: $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
1.136 kruse 472:
473: my $role = $r->dir_config('lonRole');
474: my $domain = $r->dir_config('lonDefDomain');
475: my $prodir = $r->dir_config('lonUsersDir');
476: my $contact_name = &mt('LON-CAPA helpdesk');
477:
478: # ---------------------------------------- Get the information from login token
479:
480: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
481: $form{'serverid'});
482:
483: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
484: ($tmpinfo eq 'no_such_host')) {
485: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
486: return OK;
487: } else {
488: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
489: $form{'serverid'});
490: if ( $reply ne 'ok' ) {
491: &failed($r,'Session could not be opened.',\%form);
492: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
493: return OK;
494: }
495: }
496:
497: if (!&Apache::lonnet::domain($form{'udom'})) {
498: &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
499: return OK;
500: }
501:
1.168 ! raeburn 502: my ($key,$firsturl,@rest)=split(/&/,$tmpinfo);
! 503: foreach my $item (@rest) {
! 504: my ($key,$value) = split(/=/,$item);
! 505: $form{$key} = &unescape($value);
1.156 raeburn 506: }
1.155 raeburn 507: if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
508: $form{'firsturl'} = $firsturl;
509: }
1.139 raeburn 510: my $upass = $ENV{HTTPS} ? $form{'upass0'}
511: : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
1.136 kruse 512:
513: # ---------------------------------------------------------------- Authenticate
514:
515: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
516: my ($cancreate,$statustocreate) =
517: &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
518: my $defaultauth;
519: if (ref($cancreate) eq 'ARRAY') {
520: if (grep(/^login$/,@{$cancreate})) {
521: $defaultauth = 1;
522: }
523: }
524: my $clientcancheckhost = 1;
525: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
526: $form{'udom'},$defaultauth,
527: $clientcancheckhost);
1.149 raeburn 528:
1.136 kruse 529: # --------------------------------------------------------------------- Failed?
530:
531: if ($authhost eq 'no_host') {
532: &failed($r,'Username and/or password could not be authenticated.',
533: \%form);
534: return OK;
535: } elsif ($authhost eq 'no_account_on_host') {
536: if ($defaultauth) {
537: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
538: unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
539: return OK;
540: }
541: my $start_page =
1.168 ! raeburn 542: &Apache::loncommon::start_page('Create a user account in LON-CAPA',
! 543: '',{'no_inline_link' => 1,});
1.136 kruse 544: my $lonhost = $r->dir_config('lonHostID');
545: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
546: my $contacts =
547: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
548: $form{'udom'},$origmail);
549: my ($contact_email) = split(',',$contacts);
550: my $output =
551: &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
552: $domdesc,'',$lonhost,
553: $contact_email,$contact_name,
554: undef,$statustocreate);
555: &Apache::loncommon::content_type($r,'text/html');
556: $r->send_http_header;
557: &Apache::createaccount::print_header($r,$start_page);
558: $r->print('<h3>'.&mt('Account creation').'</h3>'.
559: &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
560: $output.&Apache::loncommon::end_page());
561: return OK;
562: } else {
563: &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);
564: return OK;
565: }
566: }
567:
568: if (($firsturl eq '') ||
569: ($firsturl=~/^\/adm\/(logout|remote)/)) {
570: $firsturl='/adm/roles';
571: }
572:
573: my $hosthere;
574: if ($form{'iptoken'}) {
1.138 raeburn 575: my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
576: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
1.136 kruse 577: if (($sessiondata{'domain'} eq $form{'udom'}) &&
578: ($sessiondata{'username'} eq $form{'uname'})) {
579: $hosthere = 1;
580: }
581: }
582:
583: # --------------------------------- Are we attempting to login as somebody else?
584: if ($form{'suname'}) {
1.145 raeburn 585: my ($suname,$sudom,$sudomref);
586: $suname = $form{'suname'};
587: $sudom = $form{'udom'};
588: if ($form{'sudom'}) {
589: unless ($sudom eq $form{'sudom'}) {
590: if (&Apache::lonnet::domain($form{'sudom'})) {
591: $sudomref = [$form{'sudom'}];
592: $sudom = $form{'sudom'};
593: }
594: }
595: }
1.136 kruse 596: # ------------ see if the original user has enough privileges to pull this stunt
1.145 raeburn 597: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
1.136 kruse 598: # ---------------------------------------------------- see if the su-user exists
1.145 raeburn 599: unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
1.136 kruse 600: # ------------------------------ see if the su-user is not too highly privileged
1.146 raeburn 601: if (&Apache::lonnet::privileged($suname,$sudom)) {
602: &Apache::lonnet::logthis('Attempted switch user to privileged user');
603: } else {
604: my $noprivswitch;
1.145 raeburn 605: #
606: # su-user's home server and user's home server must have one of:
1.147 raeburn 607: # (a) same domain
608: # (b) same primary library server for the two domains
609: # (c) same "internet domain" for primary library server(s) for home servers' domains
1.145 raeburn 610: #
1.148 raeburn 611: my $suprim = &Apache::lonnet::domain($sudom,'primary');
612: my $suintdom = &Apache::lonnet::internet_dom($suprim);
1.145 raeburn 613: unless ($sudom eq $form{'udom'}) {
1.148 raeburn 614: my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
615: my $uintdom = &Apache::lonnet::internet_dom($uprim);
1.145 raeburn 616: unless ($suprim eq $uprim) {
617: unless ($suintdom eq $uintdom) {
618: &Apache::lonnet::logthis('Attempted switch user '
1.146 raeburn 619: .'to user with different "internet domain".');
620: $noprivswitch = 1;
1.145 raeburn 621: }
622: }
623: }
624:
1.146 raeburn 625: unless ($noprivswitch) {
626: #
627: # server where log-in occurs must have same "internet domain" as su-user's home
628: # server
629: #
630: my $lonhost = $r->dir_config('lonHostID');
631: my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
1.148 raeburn 632: if ($hostintdom ne $suintdom) {
1.146 raeburn 633: &Apache::lonnet::logthis('Attempted switch user on a '
634: .'server with a different "internet domain".');
635: } else {
636:
1.136 kruse 637: # -------------------------------------------------------- actually switch users
1.145 raeburn 638:
1.146 raeburn 639: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
640: $form{'udom'}.' logging in as '.$suname.':'.$sudom);
641: $form{'uname'}=$suname;
642: if ($form{'udom'} ne $sudom) {
643: $form{'udom'}=$sudom;
644: }
645: }
1.145 raeburn 646: }
1.136 kruse 647: }
648: }
649: } else {
650: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
651: }
652: }
653:
654: my ($is_balancer,$otherserver);
655:
656: unless ($hosthere) {
657: ($is_balancer,$otherserver) =
1.141 raeburn 658: &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
659: if ($is_balancer) {
1.161 raeburn 660: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
661: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
662: if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
663: $otherserver = $found_server;
664: }
1.141 raeburn 665: if ($otherserver eq '') {
666: my $lowest_load;
667: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
668: if ($lowest_load > 100) {
1.163 raeburn 669: $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});
1.141 raeburn 670: }
671: }
672: if ($otherserver ne '') {
673: my @hosts = &Apache::lonnet::current_machine_ids();
674: if (grep(/^\Q$otherserver\E$/,@hosts)) {
675: $hosthere = $otherserver;
676: }
677: }
678: }
1.136 kruse 679: }
680:
1.141 raeburn 681: if (($is_balancer) && (!$hosthere)) {
1.136 kruse 682: if ($otherserver) {
683: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
684: \%form);
685: my $switchto = '/adm/switchserver?otherserver='.$otherserver;
686: if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
1.168 ! raeburn 687: $switchto .= '&origurl='.$firsturl;
1.136 kruse 688: }
689: if ($form{'role'}) {
690: $switchto .= '&role='.$form{'role'};
691: }
692: if ($form{'symb'}) {
693: $switchto .= '&symb='.$form{'symb'};
694: }
1.156 raeburn 695: if ($form{'linkprot'}) {
696: $env{'request.linkprot'} = $form{'linkprot'};
1.167 raeburn 697: } elsif ($form{'linkkey'} ne '') {
1.164 raeburn 698: $env{'request.linkkey'} = $form{'linkkey'};
699: }
700: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
701: &set_deeplink_login(%form);
1.156 raeburn 702: }
1.136 kruse 703: $r->internal_redirect($switchto);
704: } else {
1.160 raeburn 705: &Apache::loncommon::content_type($r,'text/html');
706: $r->send_http_header;
1.136 kruse 707: $r->print(&noswitch());
708: }
709: return OK;
710: } else {
711: if (!&check_can_host($r,\%form,$authhost)) {
712: my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
713: if ($otherserver) {
714: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
715: \%form);
716: my $switchto = '/adm/switchserver?otherserver='.$otherserver;
717: if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
1.168 ! raeburn 718: $switchto .= '&origurl='.$firsturl;
1.136 kruse 719: }
720: if ($form{'role'}) {
721: $switchto .= '&role='.$form{'role'};
722: }
723: if ($form{'symb'}) {
724: $switchto .= '&symb='.$form{'symb'};
725: }
1.156 raeburn 726: if ($form{'linkprot'}) {
727: $env{'request.linkprot'} = $form{'linkprot'};
1.167 raeburn 728: } elsif ($form{'linkkey'} ne '') {
1.164 raeburn 729: $env{'request.linkkey'} = $form{'linkkey'};
730: }
731: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
732: &set_deeplink_login(%form);
1.156 raeburn 733: }
1.136 kruse 734: $r->internal_redirect($switchto);
735: } else {
1.160 raeburn 736: &Apache::loncommon::content_type($r,'text/html');
737: $r->send_http_header;
1.136 kruse 738: $r->print(&noswitch());
739: }
740: return OK;
741: }
742:
743: # ------------------------------------------------------- Do the load balancing
744:
745: # ---------------------------------------------------------- Determine own load
746: my $loadlim = $r->dir_config('lonLoadLim');
747: my $loadavg;
748: {
749: my $loadfile=Apache::File->new('/proc/loadavg');
750: $loadavg=<$loadfile>;
751: }
752: $loadavg =~ s/\s.*//g;
753: my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
754: my $userloadpercent=&Apache::lonnet::userload();
755:
756: # ---------------------------------------------------------- Are we overloaded?
757: if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
1.163 raeburn 758: my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});
1.136 kruse 759: if (!$unloaded) {
760: ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
761: }
762: if ($unloaded) {
763: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
764: undef,\%form);
1.156 raeburn 765: if ($form{'linkprot'}) {
766: $env{'request.linkprot'} = $form{'linkprot'};
1.167 raeburn 767: } elsif ($form{'linkkey'} ne '') {
1.164 raeburn 768: $env{'request.linkkey'} = $form{'linkkey'};
769: }
770: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
771: &set_deeplink_login(%form);
1.156 raeburn 772: }
1.136 kruse 773: $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
774: return OK;
775: }
776: }
1.141 raeburn 777: if (($is_balancer) && ($hosthere)) {
778: $form{'noloadbalance'} = $hosthere;
779: }
1.156 raeburn 780: my $extra_env;
781: if ($form{'linkprot'}) {
782: my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);
783: if ($linkprotector) {
784: $extra_env = {'user.linkprotector' => $linkprotector,
1.164 raeburn 785: 'user.linkproturi' => $uri};
1.156 raeburn 786: }
1.167 raeburn 787: } elsif ($form{'linkkey'} ne '') {
1.166 raeburn 788: $extra_env = {'user.deeplinkkey' => $form{'linkkey'},
1.164 raeburn 789: 'user.keyedlinkuri' => $form{'firsturl'}};
790: }
791: if ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
792: &set_deeplink_login(%form);
1.166 raeburn 793: if ($form{'linkprot'}) {
794: if (ref($extra_env) eq 'HASH') {
795: %{$extra_env} = ( %{$extra_env}, 'request.linkprot' => $form{'linkprot'} );
796: } else {
797: $extra_env = {'request.linkprot' => $form{'linkprot'}};
798: }
1.167 raeburn 799: } elsif ($form{'linkkey'} ne '') {
1.166 raeburn 800: if (ref($extra_env) eq 'HASH') {
801: %{$extra_env} = ( %{$extra_env}, 'request.linkkey' => $form{'linkkey'} );
802: } else {
803: $extra_env = {'request.linkkey' => $form{'linkkey'}};
804: }
805: }
1.164 raeburn 806: if ($env{'request.deeplink.login'}) {
807: if (ref($extra_env) eq 'HASH') {
808: %{$extra_env} = ( %{$extra_env}, 'request.deeplink.login' => $form{'firsturl'} );
809: } else {
810: $extra_env = {'request.deeplink.login' => $form{'firsturl'}};
811: }
1.157 raeburn 812: }
1.156 raeburn 813: }
814: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,
1.136 kruse 815: \%form);
816: return OK;
817: }
818: }
819:
1.164 raeburn 820: sub set_deeplink_login {
821: my (%form) = @_;
822: if ($form{'firsturl'} =~ m{^/tiny/($match_domain)/\w+$}) {
823: my $cdom = $1;
824: my ($cnum,$symb) = &Apache::loncommon::symb_from_tinyurl($form{'firsturl'},'',$cdom);
825: if ($symb) {
1.166 raeburn 826: if ($env{'request.course.id'} eq $cdom.'_'.$cnum) {
827: my ($disallow,$deeplink);
828: if ($symb =~ /\.(page|sequence)$/) {
829: my $mapname = &Apache::lonnet::deversion((&Apache::lonnet::decode_symb($symb))[2]);
830: my $navmap = Apache::lonnavmaps::navmap->new();
831: if (ref($navmap)) {
832: $deeplink = $navmap->get_mapparam(undef,$mapname,'0.deeplink');
833: }
834: } else {
835: $deeplink = &Apache::lonnet::EXT('resource.0.deeplink',$symb);
836: }
837: if ($deeplink ne '') {
838: my ($state,$others,$listed,$scope,$protect) = split(/,/,$deeplink);
839: if (($protect ne 'none') && ($protect ne '')) {
840: my ($acctype,$item) = split(/:/,$protect);
841: if ($acctype =~ /lti(c|d)$/) {
842: unless ($form{'linkprot'} eq $item.$1.':'.$env{'request.deeplink.login'}) {
843: $disallow = 1;
844: }
845: } elsif ($acctype eq 'key') {
846: unless ($form{'linkkey'} eq $item) {
847: $disallow = 1;
848: }
1.164 raeburn 849: }
850: }
851: }
1.166 raeburn 852: unless ($disallow) {
853: $env{'request.deeplink.login'} = $form{'firsturl'};
854: }
855: } else {
1.164 raeburn 856: $env{'request.deeplink.login'} = $form{'firsturl'};
857: }
858: }
859: }
860: return;
861: }
862:
1.168 ! raeburn 863: sub set_retry_token {
! 864: my ($form,$lonhost,$querystr) = @_;
! 865: if (ref($form) eq 'HASH') {
! 866: my ($firsturl,$token,$extras,@names);
! 867: @names = ('role','symb','linkprot','linkkey');
! 868: foreach my $name (@names) {
! 869: if ($form->{$name} ne '') {
! 870: $extras .= '&'.$name.'='.&escape($form->{$name});
! 871: last if ($name eq 'linkprot');
! 872: }
! 873: }
! 874: my $firsturl = $form->{'firsturl'};
! 875: if (($firsturl ne '') || ($extras ne '')) {
! 876: $extras .= ':retry';
! 877: $token = &Apache::lonnet::reply('tmpput:'.&escape($firsturl).
! 878: $extras,$lonhost);
! 879: if (($token eq 'con_lost') || ($token eq 'no_such_host')) {
! 880: return 'fail';
! 881: } else {
! 882: if (ref($querystr)) {
! 883: $$querystr = 'retry='.$token;
! 884: }
! 885: return 'ok';
! 886: }
! 887: }
! 888: }
! 889: return;
! 890: }
! 891:
1.136 kruse 892: sub check_can_host {
893: my ($r,$form,$authhost,$domdesc) = @_;
894: return unless (ref($form) eq 'HASH');
895: my $canhost = 1;
896: my $lonhost = $r->dir_config('lonHostID');
897: my $udom = $form->{'udom'};
898: my @intdoms;
899: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
900: if (ref($internet_names) eq 'ARRAY') {
901: @intdoms = @{$internet_names};
902: }
903: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
904: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
905: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
906: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
907: my $hostname = &Apache::lonnet::hostname($lonhost);
908: my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
909: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
910: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
911: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
912: my $loncaparev;
913: if ($authhost eq 'no_account_on_host') {
914: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
915: } else {
916: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
917: }
918: $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
919: $udomdefaults{'remotesessions'},
920: $defdomdefaults{'hostedsessions'});
921: }
922: unless ($canhost) {
923: if ($authhost eq 'no_account_on_host') {
924: my $checkloginvia = 1;
925: my ($login_host,$hostname) =
926: &Apache::lonnet::choose_server($udom,$checkloginvia);
927: &Apache::loncommon::content_type($r,'text/html');
928: $r->send_http_header;
929: if ($login_host ne '') {
930: my $protocol = $Apache::lonnet::protocol{$login_host};
931: $protocol = 'http' if ($protocol ne 'https');
1.163 raeburn 932: my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);
933: $hostname = $alias if ($alias ne '');
1.136 kruse 934: my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
1.156 raeburn 935: #FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host
1.136 kruse 936: $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
937: '<h3>'.&mt('Account creation').'</h3>'.
938: &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
939: '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
940: '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
941: &Apache::loncommon::end_page());
942: } else {
943: $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
944: '<h3>'.&mt('Account creation unavailable').'</h3>'.
945: &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
946: '<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>'.
947: &Apache::loncommon::end_page());
948: }
949: } else {
950: &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
951: $form);
1.168 ! raeburn 952: if ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
! 953: $env{'request.deeplink.login'} = $form->{'firsturl'};
! 954: }
1.156 raeburn 955: if ($form->{'linkprot'}) {
956: $env{'request.linkprot'} = $form->{'linkprot'};
1.168 ! raeburn 957: } elsif ($form->{'linkkey'} ne '') {
! 958: $env{'request.linkkey'} = $form->{'linkkey'};
1.156 raeburn 959: }
1.136 kruse 960: my ($otherserver) = &Apache::lonnet::choose_server($udom);
961: $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
962: }
963: }
964: return $canhost;
965: }
966:
967: sub noswitch {
968: my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
969: '<h3>'.&mt('Session unavailable').'</h3>'.
970: &mt('This LON-CAPA server is unable to host your session.').'<br />'.
971: '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
972: &Apache::loncommon::end_page();
973: return $result;
974: }
975:
976: sub loginhelpdisplay {
977: my ($authdomain) = @_;
978: my $login_help = 1;
979: my $lang = &Apache::lonlocal::current_language();
980: if ($login_help) {
981: my $dom = $authdomain;
982: if ($dom eq '') {
983: $dom = &Apache::lonnet::default_login_domain();
984: }
985: my %domconfhash = &Apache::loncommon::get_domainconf($dom);
986: my $loginhelp_url;
987: if ($lang) {
988: $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
989: if ($loginhelp_url ne '') {
990: return $loginhelp_url;
991: }
992: }
993: $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
994: if ($loginhelp_url ne '') {
995: return $loginhelp_url;
996: } else {
997: return '/adm/loginproblems.html';
998: }
999: }
1000: return;
1001: }
1002:
1003: 1;
1004: __END__
1005:
1006:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>