File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.20: download - view: text, annotated - select for diffs
Wed Jul 25 01:44:53 2001 UTC (22 years, 9 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
bug fix to stop remote control timeout

    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: 
   10: package Apache::lonauth;
   11: 
   12: use strict;
   13: use Apache::Constants qw(:common);
   14: use Apache::File;
   15: use CGI qw(:standard);
   16: use CGI::Cookie();
   17: use Crypt::DES;
   18: use Apache::lonnet();
   19: use Apache::lonmenu();
   20: use Fcntl qw(:flock);
   21: # ------------------------------------------------------------ Successful login
   22: 
   23: sub success {
   24:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
   25:     my $lonids=$r->dir_config('lonIDsDir');
   26: 
   27: # See if old ID present, if so, remove
   28: 
   29:     my $filename;
   30:     opendir(DIR,$lonids);
   31:     while ($filename=readdir(DIR)) {
   32:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
   33: 	  unlink($lonids.'/'.$filename);
   34:        }
   35:     }
   36:     closedir(DIR);
   37: 
   38: # Give them a new cookie
   39: 
   40:     my $cookie;
   41:     my $now=time;
   42:     $cookie="$username\_$now\_$domain\_$authhost";
   43: 
   44: # Initialize roles
   45: 
   46:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
   47: 
   48: # ------------------------------------ Check browser type and MathML capability
   49: 
   50:     my @browsertype=split(/\&/,$r->dir_config("lonBrowsDet"));
   51:     my %mathcap=split(/\&/,$r->dir_config("lonMathML"));
   52:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
   53:     my $i;
   54:     my $clientbrowser='unknown';
   55:     my $clientversion='0';
   56:     my $clientmathml='';
   57:     for ($i=0;$i<=$#browsertype;$i++) {
   58:         my ($bname,$match,$notmatch,$vreg,$minv)=split(/\:/,$browsertype[$i]);
   59: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
   60: 	    $clientbrowser=$bname;
   61:             $httpbrowser=~/$vreg/i;
   62: 	    $clientversion=$1;
   63:             $clientmathml=($clientversion>=$minv);
   64:         }
   65:     }
   66:     my $clientos='unknown';
   67:     if (($httpbrowser=~/linux/i) ||
   68:         ($httpbrowser=~/unix/i) ||
   69:         ($httpbrowser=~/ux/i) ||
   70:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
   71:     if (($httpbrowser=~/vax/i) ||
   72:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
   73:     if ($httpbrowser=~/next/i) { $clientos='next'; }
   74:     if (($httpbrowser=~/mac/i) ||
   75:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
   76:     if ($httpbrowser=~/win/i) { $clientos='win'; }
   77: 
   78: # ------------------------------------------------------------- Get environment
   79: 
   80:     my $userenv=Apache::lonnet::reply("dump:$domain:$username:environment",
   81:                                       $authhost);
   82:     if (($userenv eq 'con_lost') || 
   83:         ($userenv =~ /^error\:/)) {
   84:         $userenv='';
   85:     }
   86:     $userenv=~s/\&/\nenvironment\./g;
   87:     if ($userenv ne '') {
   88: 	$userenv='environment.'.$userenv;
   89:     }
   90: # --------------------------------------------------------- Write first profile
   91: 
   92:        {
   93: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
   94: 	    unless (flock($idf,LOCK_EX)) {
   95: 	      &Apache::lonnet::logthis("<font color=blue>WARNING: ".
   96: 			    'Could not obtain exclusive lock in lonauth: '.$!);
   97: 	      $idf->close();
   98: 	      return 'error: '.$!;
   99: 	    }
  100:             if ($userenv ne '') { print $idf "$userenv\n"; }
  101:             print $idf "user.name=$username\n";
  102:             print $idf "user.domain=$domain\n";
  103:             print $idf "user.home=$authhost\n";
  104:             print $idf "browser.type=$clientbrowser\n";
  105:             print $idf "browser.version=$clientversion\n";
  106:             print $idf "browser.mathml=$clientmathml\n";
  107:             print $idf "browser.os=$clientos\n";
  108:             print $idf "request.course.fn=\n";
  109:             print $idf "request.course.uri=\n";
  110:             print $idf "request.course.sec=\n";
  111:             print $idf "request.role=cm\n";
  112:             print $idf "request.host=$ENV{'HTTP_HOST'}\n"; 
  113:             if ($userroles ne '') { print $idf "$userroles"; }
  114: 	    $idf->close();
  115:         }
  116:          $ENV{'request.role'}='cm';
  117: # -------------------------------------------------------------------- Log this
  118: 
  119:     &Apache::lonnet::log($domain,$username,$authhost,
  120:                          "Login $ENV{'REMOTE_ADDR'}");
  121: 
  122: # ------------------------------------------------- Check for critical messages
  123: 
  124:     my @what=&Apache::lonnet::dump('critical');
  125:     if ($what[0]) {
  126: 	if ($what[0] ne 'con_lost') {
  127: 	    $lowerurl='/adm/email/critical/'.$what[0];
  128:         }
  129:     }
  130: 
  131: # ------------------------------------------------------------ Get cookie ready
  132: 
  133:     $cookie="lonID=$cookie; path=/";
  134: # -------------------------------------------------------- Menu script and info
  135:     my $windowinfo=&Apache::lonmenu::open();
  136: # ------------------------------------------------------------- Info for Remote
  137:     my $configmenu=&Apache::lonmenu::rawconfig();
  138: # ------------------------------------------------- Output for successful login
  139: 
  140:     $r->send_cgi_header(<<ENDHEADER);
  141: Content-type: text/html
  142: Set-cookie: $cookie
  143: 
  144: ENDHEADER
  145:     $r->print(<<ENDSUCCESS);
  146: <html>
  147: <head>
  148: <title>Successful Login to the LearningOnline Network with CAPA</title>
  149: <script>
  150: 
  151: // --------------------------------------------- Checks if server frame defined
  152: 
  153: function checkdef() {
  154:    if ((menuloaded==0) && (tim==0)) { setTimeout('checkdef()',100); }
  155: }
  156: 
  157: // ---------------------------------------------------------- The wait function
  158: 
  159: function wait() {
  160:    if ((menuloaded==1) || (tim==1)) {
  161:       if (tim==0) {
  162:          clearTimeout(canceltim);
  163:          $configmenu
  164:          window.location='$lowerurl';  
  165:       } else {
  166:          alert("Remote Control Timed Out.");
  167:       }
  168:    } else {
  169:       setTimeout('wait();',100);
  170:    }
  171: }
  172: 
  173: function main() {
  174:    canceltim=setTimeout('tim=1;',20000);
  175:    checkdef();
  176:    wait();
  177: }
  178: 
  179: </script>
  180: </head>
  181: <body bgcolor="#FFFFFF">
  182: <script>
  183:     menuloaded=0;
  184:     tim=0;
  185: </script>
  186: $windowinfo
  187: <h1>Welcome!</h1>
  188: <script>
  189:     main();
  190: </script>
  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=new DES $keybin;
  260: 
  261:     my $upass=$cipher->decrypt(
  262:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
  263: 
  264:     $upass.=$cipher->decrypt(
  265:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
  266: 
  267:     $upass=substr($upass,1,ord(substr($upass,0,1)));
  268: 
  269: # ---------------------------------------------------------------- Authenticate
  270:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  271:                                               $upass,
  272:                                               $FORM{'udom'});
  273:     
  274: # --------------------------------------------------------------------- Failed?
  275: 
  276:     if ($authhost eq 'no_host') {
  277: 	failed($r,'Username and/or password could not be authenticated');
  278:         return OK;
  279:     }
  280: 
  281:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
  282: 	$firsturl='/res/adm/pages/index.html';
  283:     }
  284: 
  285:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
  286:     return OK;
  287: }
  288: 
  289: 1;
  290: __END__
  291: 
  292: 

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