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

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

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