File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.26: download - view: text, annotated - select for diffs
Mon Oct 8 22:37:50 2001 UTC (22 years, 7 months ago) by harris41
Branches: MAIN
CVS tags: stable_2001_fall, HEAD
making backwards compatible with old Crypt::DES

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

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