Annotation of loncom/auth/lonauth.pm, revision 1.158
1.136 kruse 1: # The LearningOnline Network
2: # User Authentication Module
3: #
1.158 ! raeburn 4: # $Id: lonauth.pm,v 1.157 2019/01/27 16:02:43 raeburn Exp $
1.136 kruse 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonauth;
30:
31: use strict;
1.140 raeburn 32: use LONCAPA qw(:DEFAULT :match);
1.136 kruse 33: use Apache::Constants qw(:common);
34: use CGI qw(:standard);
35: use Apache::loncommon();
36: use Apache::lonnet;
37: use Apache::lonmenu();
38: use Apache::createaccount;
1.149 raeburn 39: use Apache::ltiauth;
1.136 kruse 40: use Fcntl qw(:flock);
41: use Apache::lonlocal;
42: use Apache::File();
43: use HTML::Entities;
1.143 raeburn 44: use Digest::MD5;
1.136 kruse 45:
46: # ------------------------------------------------------------ Successful login
47: sub success {
48: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
1.149 raeburn 49: $form,$skipcritical) = @_;
1.136 kruse 50:
51: # ------------------------------------------------------------ Get cookie ready
52: my $cookie =
53: &Apache::loncommon::init_user_environment($r, $username, $domain,
54: $authhost, $form,
55: {'extra_env' => $extra_env,});
56:
57: my $public=($username eq 'public' && $domain eq 'public');
58:
59: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
60:
61: # -------------------------------------------------------------------- Log this
62:
63: &Apache::lonnet::log($domain,$username,$authhost,
64: "Login $ENV{'REMOTE_ADDR'}");
65:
66: # ------------------------------------------------- Check for critical messages
67:
1.149 raeburn 68: unless ($skipcritical) {
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: }
1.136 kruse 74: }
75: }
76:
1.143 raeburn 77: # ----------------------------------------------------------- Get cookies ready
78: my ($securecookie,$defaultcookie);
1.154 raeburn 79: my $ssl = $r->subprocess_env('https');
80: if ($ssl) {
81: $securecookie="lonSID=$cookie; path=/; HttpOnly; secure";
1.143 raeburn 82: my $lonidsdir=$r->dir_config('lonIDsDir');
83: if (($lonidsdir) && (-e "$lonidsdir/$cookie.id")) {
84: my $linkname=substr(Digest::MD5::md5_hex(Digest::MD5::md5_hex(time(). {}. rand(). $$)), 0, 32).'_linked';
85: if (-e "$lonidsdir/$linkname.id") {
86: unlink("$lonidsdir/$linkname.id");
87: }
88: my $made_symlink = eval { symlink("$lonidsdir/$cookie.id",
89: "$lonidsdir/$linkname.id"); 1 };
90: if ($made_symlink) {
91: $defaultcookie = "lonLinkID=$linkname; path=/; HttpOnly;";
1.154 raeburn 92: &Apache::lonnet::appenv({'user.linkedenv' => $linkname});
1.143 raeburn 93: }
94: }
95: } else {
96: $defaultcookie = "lonID=$cookie; path=/; HttpOnly;";
97: }
1.136 kruse 98: # -------------------------------------------------------- Menu script and info
99: my $destination = $lowerurl;
1.149 raeburn 100: if ($env{'request.lti.login'}) {
101: if (($env{'request.lti.reqcrs'}) && ($env{'request.lti.reqrole'} eq 'cc')) {
102: &Apache::loncommon::content_type($r,'text/html');
103: if ($securecookie) {
104: $r->headers_out->add('Set-cookie' => $securecookie);
105: }
106: if ($defaultcookie) {
107: $r->headers_out->add('Set-cookie' => $defaultcookie);
108: }
109: $r->send_http_header;
1.152 raeburn 110: if (ref($form) eq 'HASH') {
1.158 ! raeburn 111: $form->{'lti.login'} = $env{'request.lti.login'};
1.152 raeburn 112: $form->{'lti.reqcrs'} = $env{'request.lti.reqcrs'};
113: $form->{'lti.reqrole'} = $env{'request.lti.reqrole'};
114: $form->{'lti.sourcecrs'} = $env{'request.lti.sourcecrs'};
115: }
1.149 raeburn 116: &Apache::ltiauth::lti_reqcrs($r,$domain,$form,$username,$domain);
117: return;
118: }
119: if ($env{'request.lti.selfenrollrole'}) {
120: if (&Apache::ltiauth::lti_enroll($username,$domain,
121: $env{'request.lti.selfenrollrole'}) eq 'ok') {
122: $form->{'role'} = $env{'request.lti.selfenrollrole'};
123: &Apache::lonnet::delenv('request.lti.selfenrollrole');
124: } else {
125: &Apache::ltiauth::invalid_request($r,24);
126: }
127: }
128: }
1.136 kruse 129: if (defined($form->{role})) {
130: my $envkey = 'user.role.'.$form->{role};
131: my $now=time;
132: my $then=$env{'user.login.time'};
133: my $refresh=$env{'user.refresh.time'};
134: my $update=$env{'user.update.time'};
135: if (!$update) {
136: $update = $then;
137: }
138: if (exists($env{$envkey})) {
139: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
140: &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
141: \$trolecode,\$tstatus,\$tstart,\$tend);
142: if ($tstatus eq 'is') {
143: $destination .= ($destination =~ /\?/) ? '&' : '?';
144: my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
145: $destination .= 'selectrole=1&'.$newrole.'=1';
146: }
147: }
148: }
149: if (defined($form->{symb})) {
150: my $destsymb = $form->{symb};
151: $destination .= ($destination =~ /\?/) ? '&' : '?';
152: if ($destsymb =~ /___/) {
153: # FIXME Need to deal with encrypted symbs and urls as needed.
154: my ($map,$resid,$desturl)=split(/___/,$destsymb);
1.137 raeburn 155: $desturl = &Apache::lonnet::clutter($desturl);
1.136 kruse 156: $desturl = &HTML::Entities::encode($desturl,'"<>&');
157: $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
158: $destination .= 'destinationurl='.$desturl.
159: '&destsymb='.$destsymb;
160: } else {
161: $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
162: $destination .= 'destinationurl='.$destsymb;
163: }
164: }
165: if ($destination =~ m{^/adm/roles}) {
166: $destination .= ($destination =~ /\?/) ? '&' : '?';
167: $destination .= 'source=login';
168: }
169:
1.150 raeburn 170: my $windowname = 'loncapaclient';
171: if ($env{'request.lti.login'}) {
172: $windowname .= 'lti';
173: }
174: my $windowinfo = Apache::lonhtmlcommon::scripttag('self.name="'.$windowname.'";');
1.136 kruse 175: my $brcrum = [{'href' => '',
176: 'text' => 'Successful Login'},];
1.140 raeburn 177: my $args = {'bread_crumbs' => $brcrum,};
178: unless ((defined($form->{role})) || (defined($form->{symb}))) {
179: my $update=$env{'user.update.time'};
180: if (!$update) {
181: $update = $env{'user.login.time'};
182: }
183: my %roles_in_env;
184: my $showcount = &Apache::lonroles::roles_from_env(\%roles_in_env,$update);
185: if ($showcount == 1) {
186: foreach my $rolecode (keys(%roles_in_env)) {
187: my ($cid) = ($rolecode =~ m{^\Quser.role.st./\E($match_domain/$match_courseid)(?:/|$)});
188: if ($cid) {
189: my %coursedescription =
190: &Apache::lonnet::coursedescription($cid,{'one_time' => '1'});
191: if ($coursedescription{'type'} eq 'Placement') {
192: $args->{'crstype'} = 'Placement';
193: }
194: last;
195: }
196: }
197: }
198: }
1.136 kruse 199:
200: # ------------------------------------------------- Output for successful login
201:
202: &Apache::loncommon::content_type($r,'text/html');
1.143 raeburn 203: if ($securecookie) {
204: $r->headers_out->add('Set-cookie' => $securecookie);
205: }
206: if ($defaultcookie) {
207: $r->headers_out->add('Set-cookie' => $defaultcookie);
208: }
1.136 kruse 209: $r->send_http_header;
210:
1.153 raeburn 211: my ($start_page,$js,$pagebody,$end_page);
1.151 raeburn 212: if ($env{'request.lti.login'}) {
1.153 raeburn 213: $args = {'only_body' => 1};
214: if ($env{'request.lti.target'} eq '') {
215: my $ltitarget = (($destination =~ /\?/) ? '&' : '?').
216: 'ltitarget=iframe';
217: $js = <<"ENDJS";
218:
219: <script type="text/javascript">
220: // <![CDATA[
221: function setLTItarget() {
222: var newloc = '$destination';
223: if (parent !== window) {
224: newloc += '$ltitarget';
225: }
226: window.location.href=newloc;
227: }
228: // ]]>
229: </script>
230:
231: ENDJS
232: $args->{'add_entries'} = {'onload' => "javascript:setLTItarget();"};
233: $pagebody = '<noscript><span class="LC_warning">'
234: .&mt('Use of LON-CAPA requires Javascript to be enabled in your web browser.')
235: .'</span></noscript>';
236: } else {
237: $args->{'redirect'} = [0,$destination,1];
238: }
239: $start_page=&Apache::loncommon::start_page('',$js,$args);
1.151 raeburn 240: } else {
1.153 raeburn 241: $args->{'redirect'} = [0,$destination];
1.151 raeburn 242: $start_page=&Apache::loncommon::start_page('Successful Login',
1.153 raeburn 243: $js,$args);
1.149 raeburn 244:
1.151 raeburn 245: my %lt=&Apache::lonlocal::texthash(
246: 'wel' => 'Welcome',
247: 'pro' => 'Login problems?',
248: );
249: $pagebody = "<h1>$lt{'wel'}</h1>\n".
250: &mt('Welcome to the Learning[_1]Online[_2] Network with CAPA. Please wait while your session is being set up.','<i>','</i>');
251: my $loginhelp = &loginhelpdisplay($domain);
252: if ($loginhelp) {
253: $pagebody .= '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
254: }
1.136 kruse 255: }
1.153 raeburn 256: $end_page = &Apache::loncommon::end_page();
1.136 kruse 257: $r->print(<<ENDSUCCESS);
258: $start_page
259: $windowinfo
1.151 raeburn 260: $pagebody
1.136 kruse 261: $end_page
262: ENDSUCCESS
263: return;
264: }
265:
266: # --------------------------------------------------------------- Failed login!
267:
268: sub failed {
269: my ($r,$message,$form) = @_;
270: (undef,undef,undef,my $clientmathml,my $clientunicode) =
271: &Apache::loncommon::decode_user_agent();
272: my $args = {};
273: if ($clientunicode && !$clientmathml) {
274: $args = {'browser.unicode' => 1};
275: }
276:
277: my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,$args);
278: my $uname = &Apache::loncommon::cleanup_html($form->{'uname'});
279: my $udom = &Apache::loncommon::cleanup_html($form->{'udom'});
280: if (&Apache::lonnet::domain($udom,'description') eq '') {
281: undef($udom);
282: }
283: my $retry = '/adm/login';
284: if ($uname eq $form->{'uname'}) {
285: $retry .= '?username='.$uname;
286: }
287: if ($udom) {
288: $retry .= (($retry=~/\?/)?'&':'?').'domain='.$udom;
289: }
290: if (exists($form->{role})) {
291: my $role = &Apache::loncommon::cleanup_html($form->{role});
292: if ($role ne '') {
293: $retry .= (($retry=~/\?/)?'&':'?').'role='.$role;
294: }
295: }
296: if (exists($form->{symb})) {
297: my $symb = &Apache::loncommon::cleanup_html($form->{symb});
298: if ($symb ne '') {
299: $retry .= (($retry=~/\?/)?'&':'?').'symb='.$symb;
300: }
301: }
1.155 raeburn 302: if (exists($form->{firsturl})) {
303: my $firsturl = &Apache::loncommon::cleanup_html($form->{firsturl});
304: if ($firsturl ne '') {
305: $retry .= (($retry=~/\?/)?'&':'?').'firsturl='.$firsturl;
1.157 raeburn 306: if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
307: unless (exists($form->{linkprot})) {
308: if (exists($form->{linkkey})) {
309: $retry .= 'linkkey='.$form->{linkkey};
310: }
311: }
312: }
1.155 raeburn 313: }
314: }
1.156 raeburn 315: if (exists($form->{linkprot})) {
316: my $ltoken = &Apache::lonnet::tmpput({linkprot => $form->{'linkprot'}},
317: $r->dir_config('lonHostID'));
318: if ($ltoken) {
319: $retry .= (($retry =~ /\?/) ? '&' : '?').'ltoken='.$ltoken;
320: }
321: }
1.136 kruse 322: my $end_page = &Apache::loncommon::end_page();
323: &Apache::loncommon::content_type($r,'text/html');
324: $r->send_http_header;
325: my @actions =
326: (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
327: my $loginhelp = &loginhelpdisplay($udom);
328: if ($loginhelp) {
329: push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
330: }
331: #FIXME: link to helpdesk might be added here
332:
333: $r->print(
334: $start_page
335: .'<h2>'.&mt('Sorry ...').'</h2>'
336: .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
337: .&Apache::lonhtmlcommon::actionbox(\@actions)
338: .$end_page
339: );
340: }
341:
342: # ------------------------------------------------------------------ Rerouting!
343:
344: sub reroute {
345: my ($r) = @_;
346: &Apache::loncommon::content_type($r,'text/html');
347: $r->send_http_header;
348: my $msg='<b>'.&mt('Sorry ...').'</b><br />'
349: .&mt('Please [_1]log in again[_2].');
350: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
351: }
352:
353: # ---------------------------------------------------------------- Main handler
354:
355: sub handler {
356: my $r = shift;
357: my $londocroot = $r->dir_config('lonDocRoot');
358: # Are we re-routing?
359: if (-e "$londocroot/lon-status/reroute.txt") {
360: &reroute($r);
361: return OK;
362: }
363:
364: &Apache::lonlocal::get_language_handle($r);
365:
366: # -------------------------------- Prevent users from attempting to login twice
367: my $handle = &Apache::lonnet::check_for_valid_session($r);
368: if ($handle ne '') {
369: my $lonidsdir=$r->dir_config('lonIDsDir');
370: if ($handle=~/^publicuser\_/) {
371: # For "public user" - remove it, we apparently really want to login
372: unlink($r->dir_config('lonIDsDir')."/$handle.id");
373: } else {
374: # Indeed, a valid token is found
375: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
376: &Apache::loncommon::content_type($r,'text/html');
377: $r->send_http_header;
378: my $start_page =
379: &Apache::loncommon::start_page('Already logged in');
380: my $end_page =
381: &Apache::loncommon::end_page();
382: my $dest = '/adm/roles';
383: if ($env{'form.firsturl'} ne '') {
384: $dest = $env{'form.firsturl'};
1.157 raeburn 385: if ($env{'form.firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
386: &Apache::lonnet::appenv({'request.deeplink.login' => $env{'form.firsturl'}});
387: }
1.136 kruse 388: }
389: $r->print(
390: $start_page
391: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
392: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
393: ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
394: .'</p>'
395: .$end_page
396: );
397: return OK;
398: }
399: }
400:
401: # ---------------------------------------------------- No valid token, continue
402:
403:
404: my $buffer;
405: if ($r->header_in('Content-length') > 0) {
406: $r->read($buffer,$r->header_in('Content-length'),0);
407: }
408: my %form;
409: foreach my $pair (split(/&/,$buffer)) {
410: my ($name,$value) = split(/=/,$pair);
411: $value =~ tr/+/ /;
412: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
413: $form{$name}=$value;
414: }
415:
416: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
417: &failed($r,'Username, password and domain need to be specified.',
418: \%form);
419: return OK;
420: }
421:
422: # split user logging in and "su"-user
423:
1.145 raeburn 424: ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
1.136 kruse 425: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
426: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
1.145 raeburn 427: $form{'udom'} = &LONCAPA::clean_domain($form{'udom'});
428: $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
1.136 kruse 429:
430: my $role = $r->dir_config('lonRole');
431: my $domain = $r->dir_config('lonDefDomain');
432: my $prodir = $r->dir_config('lonUsersDir');
433: my $contact_name = &mt('LON-CAPA helpdesk');
434:
435: # ---------------------------------------- Get the information from login token
436:
437: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
438: $form{'serverid'});
439:
440: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
441: ($tmpinfo eq 'no_such_host')) {
442: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
443: return OK;
444: } else {
445: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
446: $form{'serverid'});
447: if ( $reply ne 'ok' ) {
448: &failed($r,'Session could not be opened.',\%form);
449: &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
450: return OK;
451: }
452: }
453:
454: if (!&Apache::lonnet::domain($form{'udom'})) {
455: &failed($r,'The domain you provided is not a valid LON-CAPA domain.',\%form);
456: return OK;
457: }
458:
1.157 raeburn 459: my ($key,$firsturl,$rolestr,$symbstr,$iptokenstr,$linkstr)=split(/&/,$tmpinfo);
1.136 kruse 460: if ($rolestr) {
461: $rolestr = &unescape($rolestr);
462: }
463: if ($symbstr) {
464: $symbstr= &unescape($symbstr);
465: }
1.138 raeburn 466: if ($iptokenstr) {
467: $iptokenstr = &unescape($iptokenstr);
468: }
1.157 raeburn 469: if ($linkstr) {
470: $linkstr = &unescape($linkstr);
1.156 raeburn 471: }
1.155 raeburn 472: if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
473: $form{'firsturl'} = $firsturl;
474: }
1.136 kruse 475: if ($rolestr =~ /^role=/) {
476: (undef,$form{'role'}) = split('=',$rolestr);
477: }
478: if ($symbstr =~ /^symb=/) {
479: (undef,$form{'symb'}) = split('=',$symbstr);
480: }
1.138 raeburn 481: if ($iptokenstr =~ /^iptoken=/) {
482: (undef,$form{'iptoken'}) = split('=',$iptokenstr);
483: }
1.157 raeburn 484: if ($linkstr =~ /^linkprot=/) {
485: (undef,$form{'linkprot'}) = split('=',$linkstr);
486: } elsif ($linkstr =~ /^linkkey=/) {
487: (undef,$form{'linkkey'}) = split('=',$linkstr);
1.156 raeburn 488: }
1.136 kruse 489:
1.139 raeburn 490: my $upass = $ENV{HTTPS} ? $form{'upass0'}
491: : &Apache::loncommon::des_decrypt($key,$form{'upass0'});
1.136 kruse 492:
493: # ---------------------------------------------------------------- Authenticate
494:
495: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
496: my ($cancreate,$statustocreate) =
497: &Apache::createaccount::get_creation_controls($form{'udom'},$domconfig{'usercreation'});
498: my $defaultauth;
499: if (ref($cancreate) eq 'ARRAY') {
500: if (grep(/^login$/,@{$cancreate})) {
501: $defaultauth = 1;
502: }
503: }
504: my $clientcancheckhost = 1;
505: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
506: $form{'udom'},$defaultauth,
507: $clientcancheckhost);
1.149 raeburn 508:
1.136 kruse 509: # --------------------------------------------------------------------- Failed?
510:
511: if ($authhost eq 'no_host') {
512: &failed($r,'Username and/or password could not be authenticated.',
513: \%form);
514: return OK;
515: } elsif ($authhost eq 'no_account_on_host') {
516: if ($defaultauth) {
517: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
518: unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
519: return OK;
520: }
521: my $start_page =
522: &Apache::loncommon::start_page('Create a user account in LON-CAPA');
523: my $lonhost = $r->dir_config('lonHostID');
524: my $origmail = $Apache::lonnet::perlvar{'lonSupportEMail'};
525: my $contacts =
526: &Apache::loncommon::build_recipient_list(undef,'helpdeskmail',
527: $form{'udom'},$origmail);
528: my ($contact_email) = split(',',$contacts);
529: my $output =
530: &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
531: $domdesc,'',$lonhost,
532: $contact_email,$contact_name,
533: undef,$statustocreate);
534: &Apache::loncommon::content_type($r,'text/html');
535: $r->send_http_header;
536: &Apache::createaccount::print_header($r,$start_page);
537: $r->print('<h3>'.&mt('Account creation').'</h3>'.
538: &mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account at this institution.').'<br />'.
539: $output.&Apache::loncommon::end_page());
540: return OK;
541: } else {
542: &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);
543: return OK;
544: }
545: }
546:
547: if (($firsturl eq '') ||
548: ($firsturl=~/^\/adm\/(logout|remote)/)) {
549: $firsturl='/adm/roles';
550: }
551:
552: my $hosthere;
553: if ($form{'iptoken'}) {
1.138 raeburn 554: my %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
555: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
1.136 kruse 556: if (($sessiondata{'domain'} eq $form{'udom'}) &&
557: ($sessiondata{'username'} eq $form{'uname'})) {
558: $hosthere = 1;
559: }
560: }
561:
562: # --------------------------------- Are we attempting to login as somebody else?
563: if ($form{'suname'}) {
1.145 raeburn 564: my ($suname,$sudom,$sudomref);
565: $suname = $form{'suname'};
566: $sudom = $form{'udom'};
567: if ($form{'sudom'}) {
568: unless ($sudom eq $form{'sudom'}) {
569: if (&Apache::lonnet::domain($form{'sudom'})) {
570: $sudomref = [$form{'sudom'}];
571: $sudom = $form{'sudom'};
572: }
573: }
574: }
1.136 kruse 575: # ------------ see if the original user has enough privileges to pull this stunt
1.145 raeburn 576: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
1.136 kruse 577: # ---------------------------------------------------- see if the su-user exists
1.145 raeburn 578: unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
1.136 kruse 579: # ------------------------------ see if the su-user is not too highly privileged
1.146 raeburn 580: if (&Apache::lonnet::privileged($suname,$sudom)) {
581: &Apache::lonnet::logthis('Attempted switch user to privileged user');
582: } else {
583: my $noprivswitch;
1.145 raeburn 584: #
585: # su-user's home server and user's home server must have one of:
1.147 raeburn 586: # (a) same domain
587: # (b) same primary library server for the two domains
588: # (c) same "internet domain" for primary library server(s) for home servers' domains
1.145 raeburn 589: #
1.148 raeburn 590: my $suprim = &Apache::lonnet::domain($sudom,'primary');
591: my $suintdom = &Apache::lonnet::internet_dom($suprim);
1.145 raeburn 592: unless ($sudom eq $form{'udom'}) {
1.148 raeburn 593: my $uprim = &Apache::lonnet::domain($form{'udom'},'primary');
594: my $uintdom = &Apache::lonnet::internet_dom($uprim);
1.145 raeburn 595: unless ($suprim eq $uprim) {
596: unless ($suintdom eq $uintdom) {
597: &Apache::lonnet::logthis('Attempted switch user '
1.146 raeburn 598: .'to user with different "internet domain".');
599: $noprivswitch = 1;
1.145 raeburn 600: }
601: }
602: }
603:
1.146 raeburn 604: unless ($noprivswitch) {
605: #
606: # server where log-in occurs must have same "internet domain" as su-user's home
607: # server
608: #
609: my $lonhost = $r->dir_config('lonHostID');
610: my $hostintdom = &Apache::lonnet::internet_dom($lonhost);
1.148 raeburn 611: if ($hostintdom ne $suintdom) {
1.146 raeburn 612: &Apache::lonnet::logthis('Attempted switch user on a '
613: .'server with a different "internet domain".');
614: } else {
615:
1.136 kruse 616: # -------------------------------------------------------- actually switch users
1.145 raeburn 617:
1.146 raeburn 618: &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.
619: $form{'udom'}.' logging in as '.$suname.':'.$sudom);
620: $form{'uname'}=$suname;
621: if ($form{'udom'} ne $sudom) {
622: $form{'udom'}=$sudom;
623: }
624: }
1.145 raeburn 625: }
1.136 kruse 626: }
627: }
628: } else {
629: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
630: }
631: }
632:
633: my ($is_balancer,$otherserver);
634:
635: unless ($hosthere) {
636: ($is_balancer,$otherserver) =
1.141 raeburn 637: &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
638: if ($is_balancer) {
639: if ($otherserver eq '') {
640: my $lowest_load;
641: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($form{'udom'});
642: if ($lowest_load > 100) {
643: $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$form{'udom'});
644: }
645: }
646: if ($otherserver ne '') {
647: my @hosts = &Apache::lonnet::current_machine_ids();
648: if (grep(/^\Q$otherserver\E$/,@hosts)) {
649: $hosthere = $otherserver;
650: }
651: }
652: }
1.136 kruse 653: }
654:
1.141 raeburn 655: if (($is_balancer) && (!$hosthere)) {
1.136 kruse 656: if ($otherserver) {
657: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
658: \%form);
659: my $switchto = '/adm/switchserver?otherserver='.$otherserver;
660: if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
1.156 raeburn 661: $switchto .= '&origurl='.$firsturl; #should escape
1.136 kruse 662: }
663: if ($form{'role'}) {
664: $switchto .= '&role='.$form{'role'};
665: }
666: if ($form{'symb'}) {
667: $switchto .= '&symb='.$form{'symb'};
668: }
1.156 raeburn 669: if ($form{'linkprot'}) {
670: $env{'request.linkprot'} = $form{'linkprot'};
1.157 raeburn 671: } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
672: if ($form{'linkkey'}) {
673: $env{'request.linkkey'} = $form{'linkkey'};
674: }
675: $env{'request.deeplink.login'} = $form{'firsturl'};
1.156 raeburn 676: }
1.136 kruse 677: $r->internal_redirect($switchto);
678: } else {
679: $r->print(&noswitch());
680: }
681: return OK;
682: } else {
683: if (!&check_can_host($r,\%form,$authhost)) {
684: my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
685: if ($otherserver) {
686: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
687: \%form);
688: my $switchto = '/adm/switchserver?otherserver='.$otherserver;
689: if (($firsturl) && ($firsturl ne '/adm/switchserver') && ($firsturl ne '/adm/roles')) {
1.156 raeburn 690: $switchto .= '&origurl='.$firsturl; #should escape
1.136 kruse 691: }
692: if ($form{'role'}) {
693: $switchto .= '&role='.$form{'role'};
694: }
695: if ($form{'symb'}) {
696: $switchto .= '&symb='.$form{'symb'};
697: }
1.156 raeburn 698: if ($form{'linkprot'}) {
699: $env{'request.linkprot'} = $form{'linkprot'};
1.157 raeburn 700: } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
701: if ($form{'linkkey'}) {
702: $env{'request.linkkey'} = $form{'linkkey'};
703: }
704: $env{'request.deeplink.login'} = $form{'firsturl'};
705:
1.156 raeburn 706: }
1.136 kruse 707: $r->internal_redirect($switchto);
708: } else {
709: $r->print(&noswitch());
710: }
711: return OK;
712: }
713:
714: # ------------------------------------------------------- Do the load balancing
715:
716: # ---------------------------------------------------------- Determine own load
717: my $loadlim = $r->dir_config('lonLoadLim');
718: my $loadavg;
719: {
720: my $loadfile=Apache::File->new('/proc/loadavg');
721: $loadavg=<$loadfile>;
722: }
723: $loadavg =~ s/\s.*//g;
724: my $loadpercent=sprintf("%.1f",100*$loadavg/$loadlim);
725: my $userloadpercent=&Apache::lonnet::userload();
726:
727: # ---------------------------------------------------------- Are we overloaded?
728: if ((($userloadpercent>100.0)||($loadpercent>100.0))) {
729: my $unloaded=Apache::lonnet::spareserver($loadpercent,$userloadpercent,1,$form{'udom'});
730: if (!$unloaded) {
731: ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
732: }
733: if ($unloaded) {
734: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',
735: undef,\%form);
1.156 raeburn 736: if ($form{'linkprot'}) {
737: $env{'request.linkprot'} = $form{'linkprot'};
1.157 raeburn 738: } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
739: if ($form{'linkkey'}) {
740: $env{'request.linkkey'} = $form{'linkkey'};
741: }
742: $env{'request.deeplink.login'} = $form{'firsturl'};
1.156 raeburn 743: }
1.136 kruse 744: $r->internal_redirect('/adm/switchserver?otherserver='.$unloaded.'&origurl='.$firsturl);
745: return OK;
746: }
747: }
1.141 raeburn 748: if (($is_balancer) && ($hosthere)) {
749: $form{'noloadbalance'} = $hosthere;
750: }
1.156 raeburn 751: my $extra_env;
752: if ($form{'linkprot'}) {
753: my ($linkprotector,$uri) = split(/:/,$form{'linkprot'},2);
754: if ($linkprotector) {
755: $extra_env = {'user.linkprotector' => $linkprotector,
756: 'user.linkproturi' => $uri,};
757: }
1.157 raeburn 758: } elsif ($form{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
759: if ($form{'linkkey'}) {
760: $extra_env = {'user.deeplinkkey' => $form{'linkkey'},
761: 'user.keyedlinkuri' => $form{'firsturl'},
762: 'request.deeplink.login' => $form{'firsturl'}};
763: } else {
764: $extra_env = {'request.deeplink.login' => $form{'firsturl'}};
765: }
1.156 raeburn 766: }
767: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,$extra_env,
1.136 kruse 768: \%form);
769: return OK;
770: }
771: }
772:
773: sub check_can_host {
774: my ($r,$form,$authhost,$domdesc) = @_;
775: return unless (ref($form) eq 'HASH');
776: my $canhost = 1;
777: my $lonhost = $r->dir_config('lonHostID');
778: my $udom = $form->{'udom'};
779: my @intdoms;
780: my $internet_names = &Apache::lonnet::get_internet_names($lonhost);
781: if (ref($internet_names) eq 'ARRAY') {
782: @intdoms = @{$internet_names};
783: }
784: my $uprimary_id = &Apache::lonnet::domain($udom,'primary');
785: my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
786: unless ($uint_dom ne '' && grep(/^\Q$uint_dom\E$/,@intdoms)) {
787: my $machine_dom = &Apache::lonnet::host_domain($lonhost);
788: my $hostname = &Apache::lonnet::hostname($lonhost);
789: my $serverhomeID = &Apache::lonnet::get_server_homeID($hostname);
790: my $serverhomedom = &Apache::lonnet::host_domain($serverhomeID);
791: my %defdomdefaults = &Apache::lonnet::get_domain_defaults($serverhomedom);
792: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
793: my $loncaparev;
794: if ($authhost eq 'no_account_on_host') {
795: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
796: } else {
797: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
798: }
799: $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
800: $udomdefaults{'remotesessions'},
801: $defdomdefaults{'hostedsessions'});
802: }
803: unless ($canhost) {
804: if ($authhost eq 'no_account_on_host') {
805: my $checkloginvia = 1;
806: my ($login_host,$hostname) =
807: &Apache::lonnet::choose_server($udom,$checkloginvia);
808: &Apache::loncommon::content_type($r,'text/html');
809: $r->send_http_header;
810: if ($login_host ne '') {
811: my $protocol = $Apache::lonnet::protocol{$login_host};
812: $protocol = 'http' if ($protocol ne 'https');
813: my $newurl = $protocol.'://'.$hostname.'/adm/createaccount';
1.156 raeburn 814: #FIXME Should preserve where user was going and linkprot by setting ltoken at $login_host
1.136 kruse 815: $r->print(&Apache::loncommon::start_page('Create a user account in LON-CAPA').
816: '<h3>'.&mt('Account creation').'</h3>'.
817: &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
818: '<p>'.&mt('You will be able to create one by logging into a LON-CAPA server within the [_1] domain.',$domdesc).'</p>'.
819: '<p>'.&mt('[_1]Log in[_2]','<a href="'.$newurl.'">','</a>').
820: &Apache::loncommon::end_page());
821: } else {
822: $r->print(&Apache::loncommon::start_page('Access to LON-CAPA unavailable').
823: '<h3>'.&mt('Account creation unavailable').'</h3>'.
824: &mt('You do not currently have a LON-CAPA account at this institution.').'<br />'.
825: '<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>'.
826: &Apache::loncommon::end_page());
827: }
828: } else {
829: &success($r,$form->{'uname'},$udom,$authhost,'noredirect',undef,
830: $form);
1.156 raeburn 831: if ($form->{'linkprot'}) {
832: $env{'request.linkprot'} = $form->{'linkprot'};
1.157 raeburn 833: } elsif ($form->{'firsturl'} =~ m{^/tiny/$match_domain/\w+$}) {
834: if ($form->{'linkkey'}) {
835: $env{'request.linkkey'} = $form->{'linkkey'};
836: }
837: $env{'request.deeplink.login'} = $form->{'firsturl'};
1.156 raeburn 838: }
1.136 kruse 839: my ($otherserver) = &Apache::lonnet::choose_server($udom);
840: $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
841: }
842: }
843: return $canhost;
844: }
845:
846: sub noswitch {
847: my $result = &Apache::loncommon::start_page('Access to LON-CAPA unavailable').
848: '<h3>'.&mt('Session unavailable').'</h3>'.
849: &mt('This LON-CAPA server is unable to host your session.').'<br />'.
850: '<p>'.&mt('Currently no other LON-CAPA server is available to host your session either.').'</p>'.
851: &Apache::loncommon::end_page();
852: return $result;
853: }
854:
855: sub loginhelpdisplay {
856: my ($authdomain) = @_;
857: my $login_help = 1;
858: my $lang = &Apache::lonlocal::current_language();
859: if ($login_help) {
860: my $dom = $authdomain;
861: if ($dom eq '') {
862: $dom = &Apache::lonnet::default_login_domain();
863: }
864: my %domconfhash = &Apache::loncommon::get_domainconf($dom);
865: my $loginhelp_url;
866: if ($lang) {
867: $loginhelp_url = $domconfhash{$dom.'.login.helpurl_'.$lang};
868: if ($loginhelp_url ne '') {
869: return $loginhelp_url;
870: }
871: }
872: $loginhelp_url = $domconfhash{$dom.'.login.helpurl_nolang'};
873: if ($loginhelp_url ne '') {
874: return $loginhelp_url;
875: } else {
876: return '/adm/loginproblems.html';
877: }
878: }
879: return;
880: }
881:
882: 1;
883: __END__
884:
885:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>