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