1: # The LearningOnline Network
2: # User Authentication Module
3: #
4: # $Id: lonauth.pm,v 1.121.2.24 2021/12/30 05:11:28 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: use CGI::Cookie();
45:
46: # ------------------------------------------------------------ Successful login
47: sub success {
48: my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
49: $form,$cid,$expirepub) = @_;
50:
51: # ------------------------------------------------------------ Get cookie ready
52: my $cookie =
53: &Apache::loncommon::init_user_environment($r, $username, $domain,
54: $authhost, $form,
55: {'extra_env' => $extra_env,});
56:
57: my $public=($username eq 'public' && $domain eq 'public');
58:
59: if ($public or $lowerurl eq 'noredirect') { return $cookie; }
60:
61: # -------------------------------------------------------------------- Log this
62:
63: my $ip = &Apache::lonnet::get_requestor_ip();
64: &Apache::lonnet::log($domain,$username,$authhost,
65: "Login $ip");
66:
67: # ------------------------------------------------- Check for critical messages
68:
69: my @what=&Apache::lonnet::dump('critical',$domain,$username);
70: if ($what[0]) {
71: if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
72: $lowerurl='/adm/email?critical=display';
73: }
74: }
75:
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: }
97: # -------------------------------------------------------- Menu script and info
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'};
105: my $update=$env{'user.update.time'};
106: if (!$update) {
107: $update = $then;
108: }
109: if (exists($env{$envkey})) {
110: my ($role,$where,$trolecode,$tstart,$tend,$tremark,$tstatus);
111: &Apache::lonnet::role_status($envkey,$update,$refresh,$now,\$role,\$where,
112: \$trolecode,\$tstatus,\$tstart,\$tend);
113: if ($tstatus eq 'is') {
114: $destination .= ($destination =~ /\?/) ? '&' : '?';
115: my $newrole = &HTML::Entities::encode($form->{role},'"<>&');
116: $destination .= 'selectrole=1&'.$newrole.'=1';
117: }
118: }
119: }
120: if (defined($form->{symb})) {
121: my $destsymb = $form->{symb};
122: my $encrypted;
123: if ($destsymb =~ m{^/enc/}) {
124: $encrypted = 1;
125: if ($cid) {
126: $destsymb = &Apache::lonenc::unencrypted($destsymb,$cid);
127: }
128: }
129: $destination .= ($destination =~ /\?/) ? '&' : '?';
130: if ($destsymb =~ /___/) {
131: my ($map,$resid,$desturl)=split(/___/,$destsymb);
132: $desturl = &Apache::lonnet::clutter($desturl);
133: if ($encrypted) {
134: $desturl = &Apache::lonenc::encrypted($desturl,1,$cid);
135: $destsymb = $form->{symb};
136: }
137: $desturl = &HTML::Entities::encode($desturl,'"<>&');
138: $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
139: $destination .= 'destinationurl='.$desturl.
140: '&destsymb='.$destsymb;
141: } elsif (!$encrypted) {
142: $destsymb = &HTML::Entities::encode($destsymb,'"<>&');
143: $destination .= 'destinationurl='.$destsymb;
144: }
145: }
146: if ($destination =~ m{^/adm/roles}) {
147: $destination .= ($destination =~ /\?/) ? '&' : '?';
148: $destination .= 'source=login';
149: }
150:
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();
156: my $brcrum = [{'href' => '',
157: 'text' => 'Successful Login'},];
158: my $start_page=&Apache::loncommon::start_page('Successful Login',
159: $startupremote,
160: {'no_inline_link' => 1,
161: 'bread_crumbs' => $brcrum,});
162: my $end_page =&Apache::loncommon::end_page();
163:
164: my $continuelink;
165: if ($env{'environment.remote'} eq 'off') {
166: $continuelink='<a href="'.$destination.'">'.&mt('Continue').'</a>';
167: }
168: # ------------------------------------------------- Output for successful login
169:
170: &Apache::loncommon::content_type($r,'text/html');
171: if ($securecookie) {
172: $r->headers_out->add('Set-cookie' => $securecookie);
173: }
174: if ($defaultcookie) {
175: $r->headers_out->add('Set-cookie' => $defaultcookie);
176: }
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: }
183: $r->send_http_header;
184:
185: my %lt=&Apache::lonlocal::texthash(
186: 'wel' => 'Welcome',
187: 'pro' => 'Login problems?',
188: );
189: my $loginhelp = &loginhelpdisplay($domain);
190: if ($loginhelp) {
191: $loginhelp = '<p><a href="'.$loginhelp.'">'.$lt{'pro'}.'</a></p>';
192: }
193:
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>');
195: $r->print(<<ENDSUCCESS);
196: $start_page
197: $setflags
198: $windowinfo
199: <h1>$lt{'wel'}</h1>
200: $welcome
201: $loginhelp
202: $remoteinfo
203: $maincall
204: $continuelink
205: $end_page
206: ENDSUCCESS
207: return;
208: }
209:
210: # --------------------------------------------------------------- Failed login!
211:
212: sub failed {
213: my ($r,$message,$form,$authhost) = @_;
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: }
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: }
255: }
256: } elsif ($querystr ne '') {
257: $retry .= (($retry=~/\?/)?'&':'?').$querystr;
258: }
259: my $end_page = &Apache::loncommon::end_page();
260: &Apache::loncommon::content_type($r,'text/html');
261: $r->send_http_header;
262: my @actions =
263: (&mt('Please [_1]log in again[_2].','<a href="'.$retry.'">','</a>'));
264: my $loginhelp = &loginhelpdisplay($udom);
265: if ($loginhelp) {
266: push(@actions, '<a href="'.$loginhelp.'">'.&mt('Login problems?').'</a>');
267: }
268: #FIXME: link to helpdesk might be added here
269:
270: $r->print(
271: $start_page
272: .'<h2>'.&mt('Sorry ...').'</h2>'
273: .&Apache::lonhtmlcommon::confirm_success(&mt($message),1).'<br /><br />'
274: .&Apache::lonhtmlcommon::actionbox(\@actions)
275: .$end_page
276: );
277: }
278:
279: # ------------------------------------------------------------------ Rerouting!
280:
281: sub reroute {
282: my ($r) = @_;
283: &Apache::loncommon::content_type($r,'text/html');
284: $r->send_http_header;
285: my $msg='<b>'.&mt('Sorry ...').'</b><br />'
286: .&mt('Please [_1]log in again[_2].');
287: &Apache::loncommon::simple_error_page($r,'Rerouting',$msg,{'no_auto_mt_msg' => 1});
288: }
289:
290: # ---------------------------------------------------------------- Main handler
291:
292: sub handler {
293: my $r = shift;
294: my $londocroot = $r->dir_config('lonDocRoot');
295: # Are we re-routing?
296: if (-e "$londocroot/lon-status/reroute.txt") {
297: &reroute($r);
298: return OK;
299: }
300:
301: &Apache::lonlocal::get_language_handle($r);
302:
303: # -------------------------------- Prevent users from attempting to login twice
304: my $handle = &Apache::lonnet::check_for_valid_session($r);
305: if ($handle ne '') {
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 {
311: # Indeed, a valid token is found
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();
319: my $dest = '/adm/roles';
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')) {
326: my ($des_key,$firsturl,@rest)=split(/&/,$tmpinfo);
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: }
338: }
339: $r->print(
340: $start_page
341: .'<p class="LC_warning">'.&mt('You are already logged in!').'</p>'
342: .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]log out[_4].'
343: ,'<a href="'.$dest.'">','</a>','<a href="/adm/logout">','</a>')
344: .'</p>'
345: .$end_page
346: );
347: return OK;
348: }
349: }
350:
351: # ---------------------------------------------------- No valid token, continue
352:
353: my %form = &get_form_items($r);
354: if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
355: &failed($r,'Username, password and domain need to be specified.',
356: \%form);
357: return OK;
358: }
359:
360: # split user logging in and "su"-user
361:
362: ($form{'uname'},$form{'suname'},$form{'sudom'})=split(/\:/,$form{'uname'});
363: $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
364: $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
365: $form{'udom'} = &LONCAPA::clean_domain($form{'udom'});
366: $form{'sudom'} = &LONCAPA::clean_domain($form{'sudom'});
367:
368: my $role = $r->dir_config('lonRole');
369: my $domain = $r->dir_config('lonDefDomain');
370: my $prodir = $r->dir_config('lonUsersDir');
371: my $contact_name = &mt('LON-CAPA helpdesk');
372:
373: # ---------------------------------------- Get the information from login token
374:
375: my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
376: $form{'serverid'});
377:
378: if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
379: ($tmpinfo eq 'no_such_host')) {
380: &failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
381: return OK;
382: } else {
383: my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
384: $form{'serverid'});
385: if ( $reply ne 'ok' ) {
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");
388: return OK;
389: }
390: }
391:
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: }
396:
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);
402: }
403: my $upass = &Apache::loncommon::des_decrypt($des_key,$form{'upass0'});
404:
405: # ---------------------------------------------------------------- Authenticate
406:
407: my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
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;
414: }
415: }
416: my $clientcancheckhost = 1;
417: my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
418: $form{'udom'},$defaultauth,
419: $clientcancheckhost);
420:
421: # --------------------------------------------------------------------- Failed?
422:
423: if ($authhost eq 'no_host') {
424: &failed($r,'Username and/or password could not be authenticated.',
425: \%form);
426: return OK;
427: } elsif ($authhost eq 'no_account_on_host') {
428: if ($defaultauth) {
429: my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
430: unless (&check_can_host($r,\%form,'no_account_on_host',$domdesc)) {
431: return OK;
432: }
433: my $start_page =
434: &Apache::loncommon::start_page('Create a user account in LON-CAPA',
435: '',{'no_inline_link' => 1,});
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);
442: my $output =
443: &Apache::createaccount::username_check($form{'uname'},$form{'udom'},
444: $domdesc,'',$lonhost,
445: $contact_email,$contact_name,
446: undef,$statustocreate);
447: &Apache::loncommon::content_type($r,'text/html');
448: $r->send_http_header;
449: &Apache::createaccount::print_header($r,$start_page);
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());
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: }
458: }
459:
460: if (($firsturl eq '') ||
461: ($firsturl=~/^\/adm\/(logout|remote)/)) {
462: $firsturl='/adm/roles';
463: }
464:
465: my ($hosthere,%sessiondata);
466: if ($form{'iptoken'}) {
467: %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
468: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
469: if (($sessiondata{'domain'} eq $form{'udom'}) &&
470: ($sessiondata{'username'} eq $form{'uname'})) {
471: $hosthere = 1;
472: }
473: }
474:
475: # --------------------------------- Are we attempting to login as somebody else?
476: if ($form{'suname'}) {
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: }
488: # ------------ see if the original user has enough privileges to pull this stunt
489: if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'},$sudomref)) {
490: # ---------------------------------------------------- see if the su-user exists
491: unless (&Apache::lonnet::homeserver($suname,$sudom) eq 'no_host') {
492: # ------------------------------ see if the su-user is not too highly privileged
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:
529: # -------------------------------------------------------- actually switch users
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: }
539: }
540: }
541: } else {
542: &Apache::lonnet::logthis('Non-privileged user attempting switch user');
543: }
544: }
545:
546: my ($is_balancer,$otherserver);
547:
548: unless ($hosthere) {
549: ($is_balancer,$otherserver) =
550: &Apache::lonnet::check_loadbalancing($form{'uname'},$form{'udom'},'login');
551: if ($is_balancer) {
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: }
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) {
561: $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$form{'udom'});
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: }
571: }
572:
573: if (($is_balancer) && (!$hosthere)) {
574: if ($otherserver) {
575: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
576: \%form);
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);
588: } else {
589: &Apache::loncommon::content_type($r,'text/html');
590: $r->send_http_header;
591: $r->print(&noswitch());
592: }
593: return OK;
594: } else {
595: if (!&check_can_host($r,\%form,$authhost)) {
596: my ($otherserver) = &Apache::lonnet::choose_server($form{'udom'});
597: if ($otherserver) {
598: &success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
599: \%form);
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);
611: } else {
612: &Apache::loncommon::content_type($r,'text/html');
613: $r->send_http_header;
614: $r->print(&noswitch());
615: }
616: return OK;
617: }
618:
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))) {
634: my $unloaded=Apache::lonnet::spareserver($r,$loadpercent,$userloadpercent,1,$form{'udom'});
635: if (!$unloaded) {
636: ($unloaded) = &Apache::lonnet::choose_server($form{'udom'});
637: }
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);
642: return OK;
643: }
644: }
645: if (($is_balancer) && ($hosthere)) {
646: $form{'noloadbalance'} = $hosthere;
647: }
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: }
661: &success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
662: \%form);
663: return OK;
664: }
665: }
666:
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:
712: sub check_can_host {
713: my ($r,$form,$authhost,$domdesc) = @_;
714: return unless (ref($form) eq 'HASH');
715: my $canhost = 1;
716: my $lonhost = $r->dir_config('lonHostID');
717: my $udom = $form->{'udom'};
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: }
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);
731: my %udomdefaults = &Apache::lonnet::get_domain_defaults($udom);
732: my $loncaparev;
733: if ($authhost eq 'no_account_on_host') {
734: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom);
735: } else {
736: $loncaparev = &Apache::lonnet::get_server_loncaparev($machine_dom,$lonhost);
737: }
738: $canhost = &Apache::lonnet::can_host_session($udom,$lonhost,$loncaparev,
739: $udomdefaults{'remotesessions'},
740: $defdomdefaults{'hostedsessions'});
741: }
742: unless ($canhost) {
743: if ($authhost eq 'no_account_on_host') {
744: my $checkloginvia = 1;
745: my ($login_host,$hostname) =
746: &Apache::lonnet::choose_server($udom,$checkloginvia);
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');
752: my $alias = &Apache::lonnet::use_proxy_alias($r,$login_host);
753: $hostname = $alias if ($alias ne '');
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);
771: my ($otherserver) = &Apache::lonnet::choose_server($udom);
772: $r->internal_redirect('/adm/switchserver?otherserver='.$otherserver);
773: }
774: }
775: return $canhost;
776: }
777:
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:
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:
814: 1;
815: __END__
816:
817:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>