File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.19: download - view: text, annotated - select for diffs
Sat Jun 2 16:10:04 2001 UTC (22 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
For new Remote Control - initialization

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

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