File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.11: download - view: text, annotated - select for diffs
Mon Oct 2 19:43:49 2000 UTC (23 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Load environment.db into session environment

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

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