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