File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.58: download - view: text, annotated - select for diffs
Thu Oct 30 00:54:57 2003 UTC (20 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Internationalize Welcome screen.

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: #
    4: # $Id: lonauth.pm,v 1.58 2003/10/30 00:54:57 www 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: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
   29: # 16/11,12/16,
   30: # 1/14,2/24,2/28,2/29,3/7,5/29,5/30,5/31,6/1,6/5,6/29,
   31: # 7/1,7/10,10/2,10/5,10/9,10/26,10/30,11/10,
   32: # 05/28,05/29 Gerd Kortemeyer
   33: # 07/28,08/03 Gerd Kortemeyer
   34: # 8/20 Gerd Kortemeyer
   35: 
   36: package Apache::lonauth;
   37: 
   38: use strict;
   39: use Apache::Constants qw(:common);
   40: use Apache::File;
   41: use CGI qw(:standard);
   42: use CGI::Cookie();
   43: use DynaLoader; # for Crypt::DES version
   44: use Crypt::DES;
   45: use Apache::loncommon();
   46: use Apache::lonnet();
   47: use Apache::lonmenu();
   48: use Fcntl qw(:flock);
   49: use Apache::lonlocal;
   50: 
   51: my %FORM;
   52: 
   53: # ------------------------------------------------------------ Successful login
   54: 
   55: sub success {
   56:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
   57:     my $lonids=$r->dir_config('lonIDsDir');
   58: 
   59: # See if old ID present, if so, remove
   60: 
   61:     my $filename;
   62:     opendir(DIR,$lonids);
   63:     while ($filename=readdir(DIR)) {
   64:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
   65: 	  unlink($lonids.'/'.$filename);
   66:        }
   67:     }
   68:     closedir(DIR);
   69: 
   70: # Give them a new cookie
   71: 
   72:     my $cookie;
   73:     my $now=time;
   74:     $cookie="$username\_$now\_$domain\_$authhost";
   75: 
   76: # Initialize roles
   77: 
   78:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
   79: 
   80: # ------------------------------------ Check browser type and MathML capability
   81: 
   82:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
   83:         $clientunicode,$clientos) = &Apache::loncommon::decode_user_agent($r);
   84: 
   85: # -------------------------------------- Any accessibility options to remember?
   86:     if (($FORM{'interface'}) && ($FORM{'remember'} eq 'true')) {
   87: 	foreach ('imagesuppress','appletsuppress',
   88: 		 'embedsuppress','fontenhance','blackwhite') {
   89: 	    if ($FORM{$_} eq 'true') {
   90: 		&Apache::lonnet::put('environment',{$_ => 'on'},
   91: 				     $domain,$username);
   92: 	    } else {
   93: 		&Apache::lonnet::del('environment',[$_],$domain,$username);
   94: 	    }
   95: 	}
   96:     }
   97: # ------------------------------------------------------------- Get environment
   98: 
   99:     my $userenv;
  100:     my %userenv=Apache::lonnet::dump('environment',$domain,$username);
  101:     my ($tmp) = keys(%userenv);
  102:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
  103: 	foreach my $key (keys(%userenv)) {
  104: 	    $userenv.="environment.$key=$userenv{$key}\n";
  105: 	}
  106:     }
  107:     if (($userenv{'interface'}) && (!$FORM{'interface'})) {
  108: 	$FORM{'interface'}=$userenv{'interface'};
  109:     }
  110:     $ENV{'environment.remote'}=$userenv{'remote'};
  111: # --------------- Do not trust query string to be put directly into environment
  112:     foreach ('imagesuppress','appletsuppress',
  113: 	     'embedsuppress','fontenhance','blackwhite',
  114: 	     'interface','localpath','localres') {
  115: 	$FORM{$_}=~s/[\n\r\=]//gs;
  116:     }
  117: # --------------------------------------------------------- Write first profile
  118: 
  119:     {
  120: 	my $idf=Apache::File->new(">$lonids/$cookie.id");
  121: 	unless (flock($idf,LOCK_EX)) {
  122: 	    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  123: 			   'Could not obtain exclusive lock in lonauth: '.$!);
  124: 	    $idf->close();
  125: 	    return 'error: '.$!;
  126: 	}
  127: 	if ($userenv ne '') { print $idf "$userenv\n"; }
  128: 	print $idf "user.name=$username\n";
  129: 	print $idf "user.domain=$domain\n";
  130: 	print $idf "user.home=$authhost\n";
  131: 	print $idf "browser.type=$clientbrowser\n";
  132: 	print $idf "browser.version=$clientversion\n";
  133: 	print $idf "browser.mathml=$clientmathml\n";
  134: 	print $idf "browser.unicode=$clientunicode\n";
  135: 	print $idf "browser.os=$clientos\n";
  136:         if ($FORM{'localpath'}) {
  137:            print $idf "browser.localpath=$FORM{'localpath'}\n";
  138:            print $idf "browser.localres=$FORM{'localres'}\n";
  139:         }
  140: 	print $idf "request.course.fn=\n";
  141: 	print $idf "request.course.uri=\n";
  142: 	print $idf "request.course.sec=\n";
  143: 	print $idf "request.role=cm\n";
  144:         print $idf "request.role.adv=$ENV{'user.adv'}\n";
  145: 	print $idf "request.host=$ENV{'REMOTE_ADDR'}\n";
  146: 	if ($FORM{'interface'}) {
  147: 	    $FORM{'interface'}=~s/\W//gs;
  148: 	    print $idf "browser.interface=$FORM{'interface'}\n";
  149: 	    $ENV{'browser.interface'}=$FORM{'interface'};
  150: 	    foreach ('imagesuppress','appletsuppress',
  151: 		     'embedsuppress','fontenhance','blackwhite') {
  152: 		if (($FORM{$_} eq 'true') ||
  153: 		    ($userenv{$_} eq 'on')) {
  154: 		    print $idf "browser.$_=on\n";
  155: 		}
  156: 	    }
  157: 	}
  158: 	if ($userroles ne '') { print $idf "$userroles"; }
  159: 	$idf->close();
  160:     }
  161:     $ENV{'request.role'}='cm';
  162:     $ENV{'request.role.adv'}=$ENV{'user.adv'};
  163:     $ENV{'browser.type'}=$clientbrowser;
  164: # -------------------------------------------------------------------- Log this
  165: 
  166:     &Apache::lonnet::log($domain,$username,$authhost,
  167:                          "Login $ENV{'REMOTE_ADDR'}");
  168: 
  169: # ------------------------------------------------- Check for critical messages
  170: 
  171:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
  172:     if ($what[0]) {
  173: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
  174: 	    $lowerurl='/adm/email?critical=display';
  175:         }
  176:     }
  177: 
  178: # ------------------------------------------------------------ Get cookie ready
  179: 
  180:     $cookie="lonID=$cookie; path=/";
  181: # -------------------------------------------------------- Menu script and info
  182:     my $windowinfo=&Apache::lonmenu::open($clientos);
  183:     my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
  184:     my $setflags=&Apache::lonmenu::setflags();
  185:     my $maincall=&Apache::lonmenu::maincall();
  186:     my $bodytag=&Apache::loncommon::bodytag('Successful Login');
  187: # ------------------------------------------------- Output for successful login
  188: 
  189:     $r->send_cgi_header(<<ENDHEADER);
  190: Content-type: text/html
  191: Set-cookie: $cookie
  192: 
  193: ENDHEADER
  194:     my %lt=&Apache::lonlocal::texthash(
  195: 				       'wel' => 'Welcome',
  196: 				       'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up',
  197: 				       'pro' => 'Problems',
  198: 				       'log' => 'loginproblems.html',
  199: 				       );
  200:     $r->print(<<ENDSUCCESS);
  201: <html>
  202: <head>
  203: <title>Successful Login to the LearningOnline Network with CAPA</title>
  204: $startupremote
  205: </head>
  206: $bodytag
  207: $setflags
  208: $windowinfo
  209: <h1>$lt{'wel'}</h1>
  210: $lt{'mes'}.<p>
  211: <a href="/adm/$lt{'log'}">$lt{'pro'}?</a></p>
  212: $maincall
  213: </body>
  214: </html>
  215: ENDSUCCESS
  216: }
  217: 
  218: # --------------------------------------------------------------- Failed login!
  219: 
  220: sub failed {
  221:     my ($r,$message) = @_;
  222:     my $bodytag=&Apache::loncommon::bodytag('Unsuccessful Login');
  223:     $r->send_cgi_header(<<ENDFHEADER);
  224: Content-type: text/html
  225: 
  226: ENDFHEADER
  227:     $r->print(<<ENDFAILED);
  228: <html>
  229: <head>
  230: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
  231: </head>
  232: <html>
  233: $bodytag
  234: <h1>Sorry ...</h1>
  235: <p><b>$message</b></p>
  236: <p>Please <a href="/adm/login?username=$FORM{'uname'}&domain=$FORM{'udom'}">log in again</a>.</p>
  237: <p>
  238: <a href="/adm/loginproblems.html">Problems?</a></p>
  239: </body>
  240: </html>
  241: ENDFAILED
  242: }
  243: 
  244: # ------------------------------------------------------------------ Rerouting!
  245: 
  246: sub reroute {
  247:     my $r=shift;
  248:     my $bodytag=&Apache::loncommon::bodytag('Rerouting');
  249:     $r->send_cgi_header(<<ENDRFHEADER);
  250: Content-type: text/html
  251: 
  252: ENDRFHEADER
  253:     $r->print(<<ENDRFAILED);
  254: <html>
  255: <head>
  256: <title>Rerouting Login to the LearningOnline Network with CAPA</title>
  257: </head>
  258: <html>
  259: $bodytag
  260: <h1>Sorry ...</h1>
  261: Please <a href="/">log in again</a>.
  262: </body>
  263: </html>
  264: ENDRFAILED
  265: }
  266: 
  267: # ---------------------------------------------------------------- Main handler
  268: 
  269: sub handler {
  270:     my $r = shift;
  271: 
  272: # Are we re-routing?
  273:     if (-e '/home/httpd/html/lon-status/reroute.txt') {
  274: 	&reroute($r);
  275: 	return OK;
  276:     }
  277: 
  278:     &Apache::lonlocal::get_language_handle($r);
  279: 
  280:     my $buffer;
  281:     $r->read($buffer,$r->header_in('Content-length'),0);
  282:     my @pairs=split(/&/,$buffer);
  283:     my $pair; my $name; my $value;
  284:     undef %FORM;
  285:     %FORM=();
  286:     foreach $pair (@pairs) {
  287:        ($name,$value) = split(/=/,$pair);
  288:        $value =~ tr/+/ /;
  289:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  290:        $FORM{$name}=$value;
  291:     } 
  292: 
  293:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
  294: 	failed($r,'Username, password and domain need to be specified.');
  295:         return OK;
  296:     }
  297:     $FORM{'uname'} =~ s/\W//g;
  298:     $FORM{'udom'}  =~ s/\W//g;
  299: 
  300:     my $role   = $r->dir_config('lonRole');
  301:     my $domain = $r->dir_config('lonDefDomain');
  302:     my $prodir = $r->dir_config('lonUsersDir');
  303: 
  304: # ---------------------------------------- Get the information from login token
  305: 
  306:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
  307:                                       $FORM{'serverid'});
  308: 
  309:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  310: 	failed($r,'Information needed to verify your login information is missing, inaccessible or expired.');
  311:         return OK;
  312:     } else {
  313:         unless (&Apache::lonnet::reply('tmpdel:'.$FORM{'logtoken'},
  314:                                          $FORM{'serverid'}) eq 'ok') {
  315:             &failed($r,'Session could not be opened.');
  316: 	}
  317:     }
  318:     my ($key,$firsturl)=split(/&/,$tmpinfo);
  319: 
  320:     my $keybin=pack("H16",$key);
  321: 
  322:     my $cipher;
  323:     if ($Crypt::DES::VERSION>=2.03) {
  324: 	$cipher=new Crypt::DES $keybin;
  325:     }
  326:     else {
  327: 	$cipher=new DES $keybin;
  328:     }
  329: 
  330:     my $upass=$cipher->decrypt(
  331:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
  332: 
  333:     $upass.=$cipher->decrypt(
  334:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
  335: 
  336:     $upass=substr($upass,1,ord(substr($upass,0,1)));
  337: 
  338: # ---------------------------------------------------------------- Authenticate
  339:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  340:                                               $upass,
  341:                                               $FORM{'udom'});
  342:     
  343: # --------------------------------------------------------------------- Failed?
  344: 
  345:     if ($authhost eq 'no_host') {
  346: 	failed($r,'Username and/or password could not be authenticated.');
  347:         return OK;
  348:     }
  349: 
  350:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
  351: 	$firsturl='/adm/roles';
  352:     }
  353: 
  354:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
  355:     return OK;
  356: }
  357: 
  358: 1;
  359: __END__
  360: 
  361: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>