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