Annotation of loncom/auth/lonauth.pm, revision 1.33

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.27      www         3: #
1.33    ! albertel    4: # $Id: lonauth.pm,v 1.32 2002/09/14 05:45:45 albertel Exp $
1.27      www         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: #
1.2       www        28: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
1.4       www        29: # 16/11,12/16,
1.9       www        30: # 1/14,2/24,2/28,2/29,3/7,5/29,5/30,5/31,6/1,6/5,6/29,
1.19      www        31: # 7/1,7/10,10/2,10/5,10/9,10/26,10/30,11/10,
                     32: # 05/28,05/29 Gerd Kortemeyer
1.20      harris41   33: # 07/24 Scott Harrison
1.22      www        34: # 07/28,08/03 Gerd Kortemeyer
1.23      harris41   35: # 8/15 Scott Harrison
1.24      www        36: # 8/20 Gerd Kortemeyer
1.1       albertel   37: 
                     38: package Apache::lonauth;
                     39: 
1.18      albertel   40: use strict;
1.1       albertel   41: use Apache::Constants qw(:common);
                     42: use Apache::File;
                     43: use CGI qw(:standard);
                     44: use CGI::Cookie();
1.26      harris41   45: use DynaLoader; # for Crypt::DES version
1.8       www        46: use Crypt::DES;
1.1       albertel   47: use Apache::lonnet();
1.12      www        48: use Apache::lonmenu();
1.18      albertel   49: use Fcntl qw(:flock);
1.1       albertel   50: # ------------------------------------------------------------ Successful login
                     51: 
                     52: sub success {
1.6       www        53:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
1.1       albertel   54:     my $lonids=$r->dir_config('lonIDsDir');
1.4       www        55: 
                     56: # See if old ID present, if so, remove
1.17      www        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:        }
1.4       www        64:     }
1.17      www        65:     closedir(DIR);
1.4       www        66: 
                     67: # Give them a new cookie
                     68: 
1.17      www        69:     my $cookie;
1.5       www        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: 
1.7       www        77: # ------------------------------------ Check browser type and MathML capability
1.6       www        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='';
1.33    ! albertel   86:     my $clientunicode='0';
1.6       www        87:     for ($i=0;$i<=$#browsertype;$i++) {
1.33    ! albertel   88:         my ($bname,$match,$notmatch,$vreg,$minv,$univ)=split(/\:/,$browsertype[$i]);
1.6       www        89: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                     90: 	    $clientbrowser=$bname;
                     91:             $httpbrowser=~/$vreg/i;
                     92: 	    $clientversion=$1;
                     93:             $clientmathml=($clientversion>=$minv);
1.33    ! albertel   94:             $clientunicode=($clientversion>=$univ);
        !            95: 	}
1.6       www        96:     }
                     97:     my $clientos='unknown';
                     98:     if (($httpbrowser=~/linux/i) ||
                     99:         ($httpbrowser=~/unix/i) ||
                    100:         ($httpbrowser=~/ux/i) ||
                    101:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                    102:     if (($httpbrowser=~/vax/i) ||
                    103:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                    104:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                    105:     if (($httpbrowser=~/mac/i) ||
                    106:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
1.10      www       107:     if ($httpbrowser=~/win/i) { $clientos='win'; }
1.31      www       108:     if ($httpbrowser=~/embed/i) { $clientos='pda'; }
1.6       www       109: 
1.11      www       110: # ------------------------------------------------------------- Get environment
                    111: 
1.32      albertel  112:     my $userenv;
                    113:     my %userenv=Apache::lonnet::dump('environment',$domain,$username);
                    114:     my ($tmp) = keys(%userenv);
                    115:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    116: 	foreach my $key (keys(%userenv)) {
                    117: 	    $userenv.="environment.$key=$userenv{$key}\n";
                    118: 	}
1.11      www       119:     }
1.7       www       120: # --------------------------------------------------------- Write first profile
1.5       www       121: 
                    122:        {
1.1       albertel  123: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
1.18      albertel  124: 	    unless (flock($idf,LOCK_EX)) {
                    125: 	      &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.19      www       126: 			    'Could not obtain exclusive lock in lonauth: '.$!);
1.18      albertel  127: 	      $idf->close();
                    128: 	      return 'error: '.$!;
                    129: 	    }
1.11      www       130:             if ($userenv ne '') { print $idf "$userenv\n"; }
1.4       www       131:             print $idf "user.name=$username\n";
                    132:             print $idf "user.domain=$domain\n";
                    133:             print $idf "user.home=$authhost\n";
1.6       www       134:             print $idf "browser.type=$clientbrowser\n";
                    135:             print $idf "browser.version=$clientversion\n";
                    136:             print $idf "browser.mathml=$clientmathml\n";
1.33    ! albertel  137:             print $idf "browser.unicode=$clientunicode\n";
1.6       www       138:             print $idf "browser.os=$clientos\n";
1.15      www       139:             print $idf "request.course.fn=\n";
                    140:             print $idf "request.course.uri=\n";
                    141:             print $idf "request.course.sec=\n";
1.16      www       142:             print $idf "request.role=cm\n";
                    143:             print $idf "request.host=$ENV{'HTTP_HOST'}\n"; 
1.11      www       144:             if ($userroles ne '') { print $idf "$userroles"; }
1.18      albertel  145: 	    $idf->close();
1.1       albertel  146:         }
1.19      www       147:          $ENV{'request.role'}='cm';
1.28      www       148:          $ENV{'browser.type'}=$clientbrowser;
1.7       www       149: # -------------------------------------------------------------------- Log this
                    150: 
                    151:     &Apache::lonnet::log($domain,$username,$authhost,
                    152:                          "Login $ENV{'REMOTE_ADDR'}");
1.4       www       153: 
1.14      www       154: # ------------------------------------------------- Check for critical messages
                    155: 
1.21      www       156:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14      www       157:     if ($what[0]) {
1.22      www       158: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21      www       159: 	    $lowerurl='/adm/email?critical=display';
1.14      www       160:         }
                    161:     }
                    162: 
1.5       www       163: # ------------------------------------------------------------ Get cookie ready
                    164: 
1.1       albertel  165:     $cookie="lonID=$cookie; path=/";
1.12      www       166: # -------------------------------------------------------- Menu script and info
1.31      www       167:     my $windowinfo=&Apache::lonmenu::open($clientos);
1.19      www       168: # ------------------------------------------------------------- Info for Remote
1.29      matthew   169:     my $configmenu=&Apache::lonmenu::rawconfig($r);
1.5       www       170: # ------------------------------------------------- Output for successful login
                    171: 
1.1       albertel  172:     $r->send_cgi_header(<<ENDHEADER);
                    173: Content-type: text/html
                    174: Set-cookie: $cookie
                    175: 
                    176: ENDHEADER
                    177:     $r->print(<<ENDSUCCESS);
                    178: <html>
                    179: <head>
1.4       www       180: <title>Successful Login to the LearningOnline Network with CAPA</title>
1.19      www       181: <script>
                    182: 
                    183: // --------------------------------------------- Checks if server frame defined
                    184: 
                    185: function checkdef() {
                    186:    if ((menuloaded==0) && (tim==0)) { setTimeout('checkdef()',100); }
                    187: }
                    188: 
                    189: // ---------------------------------------------------------- The wait function
1.30      matthew   190: var canceltim;
1.19      www       191: function wait() {
                    192:    if ((menuloaded==1) || (tim==1)) {
                    193:       if (tim==0) {
                    194:          clearTimeout(canceltim);
                    195:          $configmenu
                    196:          window.location='$lowerurl';  
                    197:       } else {
                    198:          alert("Remote Control Timed Out.");
                    199:       }
                    200:    } else {
                    201:       setTimeout('wait();',100);
                    202:    }
                    203: }
                    204: 
                    205: function main() {
1.23      harris41  206:    canceltim=setTimeout('tim=1;',80000);
1.19      www       207:    checkdef();
                    208:    wait();
                    209: }
                    210: 
                    211: </script>
1.1       albertel  212: </head>
1.20      harris41  213: <body bgcolor="#FFFFFF">
1.19      www       214: <script>
                    215:     menuloaded=0;
                    216:     tim=0;
                    217: </script>
                    218: $windowinfo
1.6       www       219: <h1>Welcome!</h1>
1.20      harris41  220: <script>
                    221:     main();
                    222: </script>
1.6       www       223: </body>
1.1       albertel  224: </html>
                    225: ENDSUCCESS
                    226: }
                    227: 
                    228: # --------------------------------------------------------------- Failed login!
                    229: 
                    230: sub failed {
                    231:     my ($r,$message) = @_;
                    232:     $r->send_cgi_header(<<ENDFHEADER);
                    233: Content-type: text/html
                    234: 
                    235: ENDFHEADER
                    236:     $r->print(<<ENDFAILED);
                    237: <html>
                    238: <head>
1.4       www       239: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
1.1       albertel  240: </head>
                    241: <html>
                    242: <body bgcolor="#FFFFFF">
                    243: <h1>Sorry ...</h1>
1.4       www       244: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
1.1       albertel  245: </body>
                    246: </html>
                    247: ENDFAILED
                    248: }
                    249: 
                    250: # ---------------------------------------------------------------- Main handler
                    251: 
                    252: sub handler {
                    253:     my $r = shift;
                    254: 
                    255:     my $buffer;
                    256:     $r->read($buffer,$r->header_in('Content-length'));
                    257:     my @pairs=split(/&/,$buffer);
                    258:     my $pair; my $name; my $value; my %FORM;
                    259:     foreach $pair (@pairs) {
                    260:        ($name,$value) = split(/=/,$pair);
1.7       www       261:        $value =~ tr/+/ /;
                    262:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.1       albertel  263:        $FORM{$name}=$value;
                    264:     } 
                    265: 
                    266:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
                    267: 	failed($r,'Username, password and domain need to be specified');
                    268:         return OK;
                    269:     }
                    270:     $FORM{'uname'} =~ s/\W//g;
                    271:     $FORM{'udom'}  =~ s/\W//g;
                    272: 
                    273:     my $role   = $r->dir_config('lonRole');
                    274:     my $domain = $r->dir_config('lonDefDomain');
                    275:     my $prodir = $r->dir_config('lonUsersDir');
                    276: 
1.8       www       277: # ---------------------------------------- Get the information from login token
                    278: 
                    279:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
                    280:                                       $FORM{'serverid'});
                    281: 
                    282:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
                    283: 	failed($r,'Login token missing, inaccessible or expired');
                    284:         return OK;
                    285:     }
                    286:     
1.9       www       287:     my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8       www       288: 
                    289:     my $keybin=pack("H16",$key);
                    290: 
1.26      harris41  291:     my $cipher;
                    292:     if ($Crypt::DES::VERSION>=2.03) {
                    293: 	$cipher=new Crypt::DES $keybin;
                    294:     }
                    295:     else {
                    296: 	$cipher=new DES $keybin;
                    297:     }
1.8       www       298: 
                    299:     my $upass=$cipher->decrypt(
                    300:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
                    301: 
                    302:     $upass.=$cipher->decrypt(
                    303:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
                    304: 
                    305:     $upass=substr($upass,1,ord(substr($upass,0,1)));
                    306: 
1.1       albertel  307: # ---------------------------------------------------------------- Authenticate
                    308:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
1.8       www       309:                                               $upass,
1.1       albertel  310:                                               $FORM{'udom'});
                    311:     
                    312: # --------------------------------------------------------------------- Failed?
                    313: 
                    314:     if ($authhost eq 'no_host') {
                    315: 	failed($r,'Username and/or password could not be authenticated');
                    316:         return OK;
                    317:     }
                    318: 
1.13      www       319:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
1.24      www       320: 	$firsturl='/adm/roles';
1.7       www       321:     }
1.1       albertel  322: 
1.8       www       323:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
1.1       albertel  324:     return OK;
                    325: }
                    326: 
                    327: 1;
                    328: __END__
1.7       www       329: 
                    330: 

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