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