File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.16: download - view: text, annotated - select for diffs
Mon Oct 30 22:41:55 2000 UTC (23 years, 6 months ago) by www
Branches: MAIN
CVS tags: HEAD
Info for lonnet::allowed added early into the request cycle

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

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