File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.25: download - view: text, annotated - select for diffs
Fri Oct 5 05:21:56 2001 UTC (22 years, 7 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
fixing targeting of DES function

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

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