File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.22: download - view: text, annotated - select for diffs
Sat Aug 4 00:39:32 2001 UTC (22 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Does not go to "critical" if there had never been a critical message

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

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