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