File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.36: download - view: text, annotated - select for diffs
Mon Feb 3 18:03:52 2003 UTC (21 years, 3 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
best wishes to all.

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: #
    4: # $Id: lonauth.pm,v 1.36 2003/02/03 18:03:52 harris41 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::lonnet();
   46: use Apache::lonmenu();
   47: use Fcntl qw(:flock);
   48: # ------------------------------------------------------------ Successful login
   49: 
   50: sub success {
   51:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
   52:     my $lonids=$r->dir_config('lonIDsDir');
   53: 
   54: # See if old ID present, if so, remove
   55: 
   56:     my $filename;
   57:     opendir(DIR,$lonids);
   58:     while ($filename=readdir(DIR)) {
   59:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
   60: 	  unlink($lonids.'/'.$filename);
   61:        }
   62:     }
   63:     closedir(DIR);
   64: 
   65: # Give them a new cookie
   66: 
   67:     my $cookie;
   68:     my $now=time;
   69:     $cookie="$username\_$now\_$domain\_$authhost";
   70: 
   71: # Initialize roles
   72: 
   73:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
   74: 
   75: # ------------------------------------ Check browser type and MathML capability
   76: 
   77:     my @browsertype=split(/\&/,$r->dir_config("lonBrowsDet"));
   78:     my %mathcap=split(/\&/,$r->dir_config("lonMathML"));
   79:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
   80:     my $i;
   81:     my $clientbrowser='unknown';
   82:     my $clientversion='0';
   83:     my $clientmathml='';
   84:     my $clientunicode='0';
   85:     for ($i=0;$i<=$#browsertype;$i++) {
   86:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
   87: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
   88: 	    $clientbrowser=$bname;
   89:             $httpbrowser=~/$vreg/i;
   90: 	    $clientversion=$1;
   91:             $clientmathml=($clientversion>=$minv);
   92:             $clientunicode=($clientversion>=$univ);
   93: 	}
   94:     }
   95:     my $clientos='unknown';
   96:     if (($httpbrowser=~/linux/i) ||
   97:         ($httpbrowser=~/unix/i) ||
   98:         ($httpbrowser=~/ux/i) ||
   99:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
  100:     if (($httpbrowser=~/vax/i) ||
  101:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
  102:     if ($httpbrowser=~/next/i) { $clientos='next'; }
  103:     if (($httpbrowser=~/mac/i) ||
  104:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
  105:     if ($httpbrowser=~/win/i) { $clientos='win'; }
  106:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
  107: 
  108: # ------------------------------------------------------------- Get environment
  109: 
  110:     my $userenv;
  111:     my %userenv=Apache::lonnet::dump('environment',$domain,$username);
  112:     my ($tmp) = keys(%userenv);
  113:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
  114: 	foreach my $key (keys(%userenv)) {
  115: 	    $userenv.="environment.$key=$userenv{$key}\n";
  116: 	}
  117:     }
  118: # --------------------------------------------------------- Write first profile
  119: 
  120:        {
  121: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
  122: 	    unless (flock($idf,LOCK_EX)) {
  123: 	      &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  124: 			    'Could not obtain exclusive lock in lonauth: '.$!);
  125: 	      $idf->close();
  126: 	      return 'error: '.$!;
  127: 	    }
  128:             if ($userenv ne '') { print $idf "$userenv\n"; }
  129:             print $idf "user.name=$username\n";
  130:             print $idf "user.domain=$domain\n";
  131:             print $idf "user.home=$authhost\n";
  132:             print $idf "browser.type=$clientbrowser\n";
  133:             print $idf "browser.version=$clientversion\n";
  134:             print $idf "browser.mathml=$clientmathml\n";
  135:             print $idf "browser.unicode=$clientunicode\n";
  136:             print $idf "browser.os=$clientos\n";
  137:             print $idf "request.course.fn=\n";
  138:             print $idf "request.course.uri=\n";
  139:             print $idf "request.course.sec=\n";
  140:             print $idf "request.role=cm\n";
  141:             print $idf "request.host=$ENV{'HTTP_HOST'}\n"; 
  142:             if ($userroles ne '') { print $idf "$userroles"; }
  143: 	    $idf->close();
  144:         }
  145:          $ENV{'request.role'}='cm';
  146:          $ENV{'browser.type'}=$clientbrowser;
  147: # -------------------------------------------------------------------- Log this
  148: 
  149:     &Apache::lonnet::log($domain,$username,$authhost,
  150:                          "Login $ENV{'REMOTE_ADDR'}");
  151: 
  152: # ------------------------------------------------- Check for critical messages
  153: 
  154:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
  155:     if ($what[0]) {
  156: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
  157: 	    $lowerurl='/adm/email?critical=display';
  158:         }
  159:     }
  160: 
  161: # ------------------------------------------------------------ Get cookie ready
  162: 
  163:     $cookie="lonID=$cookie; path=/";
  164: # -------------------------------------------------------- Menu script and info
  165:     my $windowinfo=&Apache::lonmenu::open($clientos);
  166:     my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
  167:     my $setflags=&Apache::lonmenu::setflags();
  168:     my $maincall=&Apache::lonmenu::maincall();
  169: # ------------------------------------------------- Output for successful login
  170: 
  171:     $r->send_cgi_header(<<ENDHEADER);
  172: Content-type: text/html
  173: Set-cookie: $cookie
  174: 
  175: ENDHEADER
  176:     $r->print(<<ENDSUCCESS);
  177: <html>
  178: <head>
  179: <title>Successful Login to the LearningOnline Network with CAPA</title>
  180: $startupremote
  181: </head>
  182: <body bgcolor="#FFFFFF">
  183: $setflags
  184: $windowinfo
  185: <h1>Welcome!</h1>
  186: Welcome to the Learning<i>Online</i> Network with CAPA.
  187: Please wait while your session
  188: is being set up.<p>
  189: <a href="/adm/loginproblems.html">Problems?</a></p>
  190: $maincall
  191: </body>
  192: </html>
  193: ENDSUCCESS
  194: }
  195: 
  196: # --------------------------------------------------------------- Failed login!
  197: 
  198: sub failed {
  199:     my ($r,$message) = @_;
  200:     $r->send_cgi_header(<<ENDFHEADER);
  201: Content-type: text/html
  202: 
  203: ENDFHEADER
  204:     $r->print(<<ENDFAILED);
  205: <html>
  206: <head>
  207: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
  208: </head>
  209: <html>
  210: <body bgcolor="#FFFFFF">
  211: <h1>Sorry ...</h1>
  212: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
  213: </body>
  214: </html>
  215: ENDFAILED
  216: }
  217: 
  218: # ---------------------------------------------------------------- Main handler
  219: 
  220: sub handler {
  221:     my $r = shift;
  222: 
  223:     my $buffer;
  224:     $r->read($buffer,$r->header_in('Content-length'));
  225:     my @pairs=split(/&/,$buffer);
  226:     my $pair; my $name; my $value; my %FORM;
  227:     foreach $pair (@pairs) {
  228:        ($name,$value) = split(/=/,$pair);
  229:        $value =~ tr/+/ /;
  230:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  231:        $FORM{$name}=$value;
  232:     } 
  233: 
  234:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
  235: 	failed($r,'Username, password and domain need to be specified');
  236:         return OK;
  237:     }
  238:     $FORM{'uname'} =~ s/\W//g;
  239:     $FORM{'udom'}  =~ s/\W//g;
  240: 
  241:     my $role   = $r->dir_config('lonRole');
  242:     my $domain = $r->dir_config('lonDefDomain');
  243:     my $prodir = $r->dir_config('lonUsersDir');
  244: 
  245: # ---------------------------------------- Get the information from login token
  246: 
  247:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
  248:                                       $FORM{'serverid'});
  249: 
  250:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  251: 	failed($r,'Login token missing, inaccessible or expired');
  252:         return OK;
  253:     }
  254:     
  255:     my ($key,$firsturl)=split(/&/,$tmpinfo);
  256: 
  257:     my $keybin=pack("H16",$key);
  258: 
  259:     my $cipher;
  260:     if ($Crypt::DES::VERSION>=2.03) {
  261: 	$cipher=new Crypt::DES $keybin;
  262:     }
  263:     else {
  264: 	$cipher=new DES $keybin;
  265:     }
  266: 
  267:     my $upass=$cipher->decrypt(
  268:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
  269: 
  270:     $upass.=$cipher->decrypt(
  271:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
  272: 
  273:     $upass=substr($upass,1,ord(substr($upass,0,1)));
  274: 
  275: # ---------------------------------------------------------------- Authenticate
  276:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  277:                                               $upass,
  278:                                               $FORM{'udom'});
  279:     
  280: # --------------------------------------------------------------------- Failed?
  281: 
  282:     if ($authhost eq 'no_host') {
  283: 	failed($r,'Username and/or password could not be authenticated');
  284:         return OK;
  285:     }
  286: 
  287:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
  288: 	$firsturl='/adm/roles';
  289:     }
  290: 
  291:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
  292:     return OK;
  293: }
  294: 
  295: 1;
  296: __END__
  297: 
  298: 

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