File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.23: download - view: text, annotated - select for diffs
Thu Aug 16 02:03:15 2001 UTC (22 years, 10 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
setting a wider time limit to accomodate slow connections
mixed with slow servers

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

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