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