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