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

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

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