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