File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.31: download - view: text, annotated - select for diffs
Wed May 29 14:51:42 2002 UTC (21 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_0_5_1, version_0_5, version_0_4, stable_2002_july, STABLE, HEAD
For bug #54.
Needed to exclude linux/unix from automatic resizing and repositioning of
browser windows because of virtual desktop sizes (desktopsize!=screensize).
Looks like this also fixed bug 468 - "misbehavior" was apparently
(not confirmed) due to overlap of windows.

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

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