1: # The LearningOnline Network
2: # User Authentication Module
3: #
4: # $Id: lonauth.pm,v 1.121.2.23 2021/12/16 21:48:19 raeburn Exp $
5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
28:
29: package Apache::lonauth;
30:
31: use strict;
32: use LONCAPA;
33: use Apache::Constants qw(:common);
34: use CGI qw(:standard);
35: use Apache::loncommon();
36: use Apache::lonnet;
37: use Apache::lonmenu();
38: use Apache::createaccount;
39: use Fcntl qw(:flock);
40: use Apache::lonlocal;
41: use Apache::File();
42: use HTML::Entities;
43: use Digest::MD5;
44:
45: # ------------------------------------------------------------ Successful login
46: sub success {
47: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
48: $form,$cid,$expirepub) = @_;
49:
50: # ------------------------------------------------------------ Get cookie ready
51: my $cookie =
52: &Apache::loncommon::init_user_environment($r, $username, $domain,
53: $authhost, $form,
54: {'extra_env' => $extra_env,});
55:
56: my $public=($username eq 'public' && $domain eq 'public');
57:
58: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
59:
60: # -------------------------------------------------------------------- Log this
61:
62: my $ip = &Apache::lonnet::get_requestor_ip();
63: &Apache::lonnet::log($domain,$username,$authhost,
64: "Login $ip");
65:
66: # ------------------------------------------------- Check for critical messages
67:
68: my @what=&Apache::lonnet::dump('critical',$domain,$username);
69: if ($what[0]) {
70: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
71: $lowerurl='/adm/email?critical=display';
72: }
73: }
74:
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: }
96: # -------------------------------------------------------- Menu script and info
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'};
104: my $update=$env{'user.update.time'};
105: if (!$update) {
106: $update = $then;
107: }
108: if (exists($env{$envkey})) {
109: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
110: &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
111: \$trolecode,\$tstatus,\$tstart,\$tend);
112: if ($tstatus eq 'is') {
113: $destination .= ($destination =~ /\?/) ? '&' : '?';
114: my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
115: $destination .= 'selectrole=1&'.$newrole.'=1';
116: }
117: }
118: }
119: if (defined($form->{symb})) {
120: my $destsymb = $form->{symb};
121: my $encrypted;
122: if ($destsymb =~ m{^/enc/}) {
123: $encrypted = 1;
124: if ($cid) {
125: $destsymb = &Apache::lonenc::unencrypted($destsymb,$cid);
126: }
127: }
128: $destination .= ($destination =~ /\?/) ? '&' : '?';
129: if ($destsymb =~ /___/) {
130: my ($map,$resid,$desturl)=split(/___/,$destsymb);
131: $desturl = &Apache::lonnet::clutter($desturl);
132: if ($encrypted) {
133: $desturl = &Apache::lonenc::encrypted($desturl,1,$cid);
134: $destsymb = $form->{symb};
135: }
136: $desturl = &HTML::Entities::encode($desturl,'"<>&');
137: $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
138: $destination .= 'destinationurl='.$desturl.
139: '&destsymb='.$destsymb;
140: } elsif (!$encrypted) {
141: $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
142: $destination .= 'destinationurl='.$destsymb;
143: }
144: }
145: if ($destination =~ m{^/adm/roles}) {
146: $destination .= ($destination =~ /\?/) ? '&' : '?';
147: $destination .= 'source=login';
148: }
149:
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();
155: my $brcrum = [{'href' => '',
156: 'text' => 'Successful Login'},];
157: my $start_page=&Apache::loncommon::start_page('Successful Login',
158: $startupremote,
159: {'no_inline_link' => 1,
160: 'bread_crumbs' => $brcrum,});
161: my $end_page =&Apache::loncommon::end_page();
162:
163: my $continuelink;
164: if ($env{'environment.remote'} eq 'off') {
165: $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
166: }
167: # ------------------------------------------------- Output for successful login
168:
169: &Apache::loncommon::content_type($r,'text/html');
170: if ($securecookie) {
171: $r->headers_out->add('Set-cookie' => $securecookie);
172: }
173: if ($defaultcookie) {
174: $r->headers_out->add('Set-cookie' => $defaultcookie);
175: }
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: }
182: $r->send_http_header;
183:
184: my %lt=&Apache::lonlocal::texthash(
185: 'wel' => 'Welcome',
186: 'pro' => 'Login problems?',
187: );
188: my $loginhelp = &loginhelpdisplay($domain);
189: if ($loginhelp) {
190: $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
191: }
192:
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>');
194: $r->print(<<ENDSUCCESS);
195: $start_page
196: $setflags
197: $windowinfo
198: <h1>$lt{'wel'}</h1>
199: $welcome
200: $loginhelp
201: $remoteinfo
202: $maincall
203: $continuelink
204: $end_page
205: ENDSUCCESS
206: return;
207: }
208:
209: # --------------------------------------------------------------- Failed login!
210:
211: sub failed {
212: my ($r,$message,$form,$authhost) = @_;
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: }
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: }
254: }
255: } elsif ($querystr ne '') {
256: $retry .= (($retry=~/\?/)?'&':'?').$querystr;
257: }
258: my $end_page = &Apache::loncommon::end_page();
259: &Apache::loncommon::content_type($r,'text/html');
260: $r->send_http_header;
261: my @actions =
262: (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
263: my $loginhelp = &loginhelpdisplay($udom);
264: if ($loginhelp) {
265: push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
266: }
267: #FIXME: link to helpdesk might be added here
268:
269: $r->print(
270: $start_page
271: .'<h2>'.&mt('Sorry ...').'</h2>'
272: .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
273: .&Apache::lonhtmlcommon::actionbox(\@actions)
274: .$end_page
275: );
276: }
277:
278: # ------------------------------------------------------------------ Rerouting!
279:
280: sub reroute {
281: my ($r) = @_;
282: &Apache::loncommon::content_type($r,'text/html');
283: $r->send_http_header;
284: my $msg='<b>'.&mt('Sorry ...').'</b><br />'
285: .&mt('Please [_1]log in again[_2].');
286: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
287: }
288:
289: # ---------------------------------------------------------------- Main handler
290:
291: sub handler {
292: my $r = shift;
293: my $londocroot = $r->dir_config('lonDocRoot');
294: # Are we re-routing?
295: if (-e "$londocroot/lon-status/reroute.txt") {
296: &reroute($r);
297: return OK;
298: }
299:
300: &Apache::lonlocal::get_language_handle($r);
301:
302: # -------------------------------- Prevent users from attempting to login twice
303: my $handle = &Apache::lonnet::check_for_valid_session($r);
304: if ($handle ne '') {
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 {
310: # Indeed, a valid token is found
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();
318: my $dest = '/adm/roles';
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')) {
325: my ($des_key,$firsturl,@rest)=split(/&/,$tmpinfo);
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: }
337: }
338: $r->print(
339: $start_page
340: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
341: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
342: ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
343: .'</p>'
344: .$end_page
345: );
346: return OK;
347: }
348: }
349:
350: # ---------------------------------------------------- No valid token, continue
351:
352: my %form = &get_form_items($r);
353: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
354: &failed($r,'Username, password and domain need to be specified.',
355: \%form);
356: return OK;
357: }
358:
359: # split user logging in and "su"-user
360:
361: ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
362: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
363: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
364: $form{'udom'} = &LONCAPA::clean_domain($form{'udom'});
365: $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
366:
367: my $role = $r->dir_config('lonRole');
368: my $domain = $r->dir_config('lonDefDomain');
369: my $prodir = $r->dir_config('lonUsersDir');
370: my $contact_name = &mt('LON-CAPA helpdesk');
371:
372: # ---------------------------------------- Get the information from login token
373:
374: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
375: $form{'serverid'});
376:
377: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
378: ($tmpinfo eq 'no_such_host')) {
379: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
380: return OK;
381: } else {
382: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
383: $form{'serverid'});
384: if ( $reply ne 'ok' ) {
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");
387: return OK;
388: }
389: }
390:
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: }
395:
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);
401: }
402: my $upass = &Apache::loncommon::des_decrypt($des_key,$form{'upass0'});
403:
404: # ---------------------------------------------------------------- Authenticate
405:
406: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
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;
413: }
414: }
415: my $clientcancheckhost = 1;
416: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
417: $form{'udom'},$defaultauth,
418: $clientcancheckhost);
419:
420: # --------------------------------------------------------------------- Failed?
421:
422: if ($authhost eq 'no_host') {
423: &failed($r,'Username and/or password could not be authenticated.',
424: \%form);
425: return OK;
426: } elsif ($authhost eq 'no_account_on_host') {
427: if ($defaultauth) {
428: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
429: unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
430: return OK;
431: }
432: my $start_page =
433: &Apache::loncommon::start_page('Create a user account in LON-CAPA',
434: '',{'no_inline_link' => 1,});
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);
441: my $output =
442: &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
443: $domdesc,'',$lonhost,
444: $contact_email,$contact_name,
445: undef,$statustocreate);
446: &Apache::loncommon::content_type($r,'text/html');
447: $r->send_http_header;
448: &Apache::createaccount::print_header($r,$start_page);
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());
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: }
457: }
458:
459: if (($firsturl eq '') ||
460: ($firsturl=~/^\/adm\/(logout|remote)/)) {
461: $firsturl='/adm/roles';
462: }
463:
464: my ($hosthere,%sessiondata);
465: if ($form{'iptoken'}) {
466: %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
467: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
468: if (($sessiondata{'domain'} eq $form{'udom'}) &&
469: ($sessiondata{'username'} eq $form{'uname'})) {
470: $hosthere = 1;
471: }
472: }
473:
474: # --------------------------------- Are we attempting to login as somebody else?
475: if ($form{'suname'}) {
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: }
487: # ------------ see if the original user has enough privileges to pull this stunt
488: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
489: # ---------------------------------------------------- see if the su-user exists
490: unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
491: # ------------------------------ see if the su-user is not too highly privileged
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:
528: # -------------------------------------------------------- actually switch users
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: }
538: }
539: }
540: } else {
541: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
542: }
543: }
544:
545: my ($is_balancer,$otherserver);
546:
547: unless ($hosthere) {
548: ($is_balancer,$otherserver) =
549: &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
550: if ($is_balancer) {
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: }
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) {
560: $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});
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: }
570: }
571:
572: if (($is_balancer) && (!$hosthere)) {
573: if ($otherserver) {
574: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
575: \%form);
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);
587: } else {
588: &Apache::loncommon::content_type($r,'text/html');
589: $r->send_http_header;
590: $r->print(&noswitch());
591: }
592: return OK;
593: } else {
594: if (!&check_can_host($r,\%form,$authhost)) {
595: my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
596: if ($otherserver) {
597: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
598: \%form);
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);
610: } else {
611: &Apache::loncommon::content_type($r,'text/html');
612: $r->send_http_header;
613: $r->print(&noswitch());
614: }
615: return OK;
616: }
617:
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))) {
633: my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});
634: if (!$unloaded) {
635: ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
636: }
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);
641: return OK;
642: }
643: }
644: if (($is_balancer) && ($hosthere)) {
645: $form{'noloadbalance'} = $hosthere;
646: }
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: }
660: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
661: \%form);
662: return OK;
663: }
664: }
665:
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:
711: sub check_can_host {
712: my ($r,$form,$authhost,$domdesc) = @_;
713: return unless (ref($form) eq 'HASH');
714: my $canhost = 1;
715: my $lonhost = $r->dir_config('lonHostID');
716: my $udom = $form->{'udom'};
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: }
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);
730: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
731: my $loncaparev;
732: if ($authhost eq 'no_account_on_host') {
733: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
734: } else {
735: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
736: }
737: $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
738: $udomdefaults{'remotesessions'},
739: $defdomdefaults{'hostedsessions'});
740: }
741: unless ($canhost) {
742: if ($authhost eq 'no_account_on_host') {
743: my $checkloginvia = 1;
744: my ($login_host,$hostname) =
745: &Apache::lonnet::choose_server($udom,$checkloginvia);
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');
751: my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);
752: $hostname = $alias if ($alias ne '');
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);
770: my ($otherserver) = &Apache::lonnet::choose_server($udom);
771: $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
772: }
773: }
774: return $canhost;
775: }
776:
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:
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:
813: 1;
814: __END__
815:
816:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>