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