File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.18: download - view: text, annotated - select for diffs
Tue Jan 9 22:10:40 2001 UTC (23 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- flocks the enviroment

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
    4: # 16/11,12/16,
    5: # 1/14,2/24,2/28,2/29,3/7,5/29,5/30,5/31,6/1,6/5,6/29,
    6: # 7/1,7/10,10/2,10/5,10/9,10/26,10/30,11/10 Gerd Kortemeyer
    7: 
    8: package Apache::lonauth;
    9: 
   10: use strict;
   11: use Apache::Constants qw(:common);
   12: use Apache::File;
   13: use CGI qw(:standard);
   14: use CGI::Cookie();
   15: use Crypt::DES;
   16: use Apache::lonnet();
   17: use Apache::lonmenu();
   18: use Fcntl qw(:flock);
   19: # ------------------------------------------------------------ Successful login
   20: 
   21: sub success {
   22:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
   23:     my $lonids=$r->dir_config('lonIDsDir');
   24: 
   25: # See if old ID present, if so, remove
   26: 
   27:     my $filename;
   28:     opendir(DIR,$lonids);
   29:     while ($filename=readdir(DIR)) {
   30:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
   31: 	  unlink($lonids.'/'.$filename);
   32:        }
   33:     }
   34:     closedir(DIR);
   35: 
   36: # Give them a new cookie
   37: 
   38:     my $cookie;
   39:     my $now=time;
   40:     $cookie="$username\_$now\_$domain\_$authhost";
   41: 
   42: # Initialize roles
   43: 
   44:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
   45: 
   46: # ------------------------------------ Check browser type and MathML capability
   47: 
   48:     my @browsertype=split(/\&/,$r->dir_config("lonBrowsDet"));
   49:     my %mathcap=split(/\&/,$r->dir_config("lonMathML"));
   50:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
   51:     my $i;
   52:     my $clientbrowser='unknown';
   53:     my $clientversion='0';
   54:     my $clientmathml='';
   55:     for ($i=0;$i<=$#browsertype;$i++) {
   56:         my ($bname,$match,$notmatch,$vreg,$minv)=split(/\:/,$browsertype[$i]);
   57: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
   58: 	    $clientbrowser=$bname;
   59:             $httpbrowser=~/$vreg/i;
   60: 	    $clientversion=$1;
   61:             $clientmathml=($clientversion>=$minv);
   62:         }
   63:     }
   64:     my $clientos='unknown';
   65:     if (($httpbrowser=~/linux/i) ||
   66:         ($httpbrowser=~/unix/i) ||
   67:         ($httpbrowser=~/ux/i) ||
   68:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
   69:     if (($httpbrowser=~/vax/i) ||
   70:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
   71:     if ($httpbrowser=~/next/i) { $clientos='next'; }
   72:     if (($httpbrowser=~/mac/i) ||
   73:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
   74:     if ($httpbrowser=~/win/i) { $clientos='win'; }
   75: 
   76: # ------------------------------------------------------------- Get environment
   77: 
   78:     my $userenv=Apache::lonnet::reply("dump:$domain:$username:environment",
   79:                                       $authhost);
   80:     if (($userenv eq 'con_lost') || 
   81:         ($userenv =~ /^error\:/)) {
   82:         $userenv='';
   83:     }
   84:     $userenv=~s/\&/\nenvironment\./g;
   85:     if ($userenv ne '') {
   86: 	$userenv='environment.'.$userenv;
   87:     }
   88: # --------------------------------------------------------- Write first profile
   89: 
   90:        {
   91: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
   92: 	    unless (flock($idf,LOCK_EX)) {
   93: 	      &Apache::lonnet::logthis("<font color=blue>WARNING: ".
   94: 				       'Could not obtain exclusive lock in lonauth: '.$!);
   95: 	      $idf->close();
   96: 	      return 'error: '.$!;
   97: 	    }
   98:             if ($userenv ne '') { print $idf "$userenv\n"; }
   99:             print $idf "user.name=$username\n";
  100:             print $idf "user.domain=$domain\n";
  101:             print $idf "user.home=$authhost\n";
  102:             print $idf "browser.type=$clientbrowser\n";
  103:             print $idf "browser.version=$clientversion\n";
  104:             print $idf "browser.mathml=$clientmathml\n";
  105:             print $idf "browser.os=$clientos\n";
  106:             print $idf "request.course.fn=\n";
  107:             print $idf "request.course.uri=\n";
  108:             print $idf "request.course.sec=\n";
  109:             print $idf "request.role=cm\n";
  110:             print $idf "request.host=$ENV{'HTTP_HOST'}\n"; 
  111:             if ($userroles ne '') { print $idf "$userroles"; }
  112: 	    $idf->close();
  113:         }
  114: 
  115: # -------------------------------------------------------------------- Log this
  116: 
  117:     &Apache::lonnet::log($domain,$username,$authhost,
  118:                          "Login $ENV{'REMOTE_ADDR'}");
  119: 
  120: # ------------------------------------------------- Check for critical messages
  121: 
  122:     my @what=&Apache::lonnet::dump('critical');
  123:     if ($what[0]) {
  124: 	if ($what[0] ne 'con_lost') {
  125: 	    $lowerurl='/adm/email/critical/'.$what[0];
  126:         }
  127:     }
  128: 
  129: # ------------------------------------------------------------ Get cookie ready
  130: 
  131:     $cookie="lonID=$cookie; path=/";
  132: # -------------------------------------------------------- Menu script and info
  133: 
  134:     my $windowinfo=&Apache::lonmenu::open();
  135: # ------------------------------------------------- Output for successful login
  136: 
  137:     $r->send_cgi_header(<<ENDHEADER);
  138: Content-type: text/html
  139: Set-cookie: $cookie
  140: 
  141: ENDHEADER
  142:     $r->print(<<ENDSUCCESS);
  143: <html>
  144: <head>
  145: <title>Successful Login to the LearningOnline Network with CAPA</title>
  146: <meta HTTP-EQUIV="Refresh" CONTENT="1; url=$lowerurl">
  147: </head>
  148: <body bgcolor="#FFFFFF">
  149:     $windowinfo
  150: <h1>Welcome!</h1>
  151: </body>
  152: </html>
  153: ENDSUCCESS
  154: }
  155: 
  156: # --------------------------------------------------------------- Failed login!
  157: 
  158: sub failed {
  159:     my ($r,$message) = @_;
  160:     $r->send_cgi_header(<<ENDFHEADER);
  161: Content-type: text/html
  162: 
  163: ENDFHEADER
  164:     $r->print(<<ENDFAILED);
  165: <html>
  166: <head>
  167: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
  168: </head>
  169: <html>
  170: <body bgcolor="#FFFFFF">
  171: <h1>Sorry ...</h1>
  172: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
  173: </body>
  174: </html>
  175: ENDFAILED
  176: }
  177: 
  178: # ---------------------------------------------------------------- Main handler
  179: 
  180: sub handler {
  181:     my $r = shift;
  182: 
  183:     my $buffer;
  184:     $r->read($buffer,$r->header_in('Content-length'));
  185:     my @pairs=split(/&/,$buffer);
  186:     my $pair; my $name; my $value; my %FORM;
  187:     foreach $pair (@pairs) {
  188:        ($name,$value) = split(/=/,$pair);
  189:        $value =~ tr/+/ /;
  190:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  191:        $FORM{$name}=$value;
  192:     } 
  193: 
  194:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
  195: 	failed($r,'Username, password and domain need to be specified');
  196:         return OK;
  197:     }
  198:     $FORM{'uname'} =~ s/\W//g;
  199:     $FORM{'udom'}  =~ s/\W//g;
  200: 
  201:     my $role   = $r->dir_config('lonRole');
  202:     my $domain = $r->dir_config('lonDefDomain');
  203:     my $prodir = $r->dir_config('lonUsersDir');
  204: 
  205: # ---------------------------------------- Get the information from login token
  206: 
  207:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
  208:                                       $FORM{'serverid'});
  209: 
  210:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  211: 	failed($r,'Login token missing, inaccessible or expired');
  212:         return OK;
  213:     }
  214:     
  215:     my ($key,$firsturl)=split(/&/,$tmpinfo);
  216: 
  217:     my $keybin=pack("H16",$key);
  218: 
  219:     my $cipher=new DES $keybin;
  220: 
  221:     my $upass=$cipher->decrypt(
  222:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
  223: 
  224:     $upass.=$cipher->decrypt(
  225:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
  226: 
  227:     $upass=substr($upass,1,ord(substr($upass,0,1)));
  228: 
  229: # ---------------------------------------------------------------- Authenticate
  230:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  231:                                               $upass,
  232:                                               $FORM{'udom'});
  233:     
  234: # --------------------------------------------------------------------- Failed?
  235: 
  236:     if ($authhost eq 'no_host') {
  237: 	failed($r,'Username and/or password could not be authenticated');
  238:         return OK;
  239:     }
  240: 
  241:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
  242: 	$firsturl='/res/adm/pages/index.html';
  243:     }
  244: 
  245:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
  246:     return OK;
  247: }
  248: 
  249: 1;
  250: __END__
  251: 
  252: 

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