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

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.27      www         3: #
1.55    ! www         4: # $Id: lonauth.pm,v 1.54 2003/07/17 15:24:46 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.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.45      matthew    45: use Apache::loncommon();
1.1       albertel   46: use Apache::lonnet();
1.12      www        47: use Apache::lonmenu();
1.18      albertel   48: use Fcntl qw(:flock);
1.37      www        49: 
                     50: my %FORM;
                     51: 
1.1       albertel   52: # ------------------------------------------------------------ Successful login
                     53: 
                     54: sub success {
1.6       www        55:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
1.1       albertel   56:     my $lonids=$r->dir_config('lonIDsDir');
1.4       www        57: 
                     58: # See if old ID present, if so, remove
1.17      www        59: 
                     60:     my $filename;
                     61:     opendir(DIR,$lonids);
                     62:     while ($filename=readdir(DIR)) {
                     63:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                     64: 	  unlink($lonids.'/'.$filename);
                     65:        }
1.4       www        66:     }
1.17      www        67:     closedir(DIR);
1.4       www        68: 
                     69: # Give them a new cookie
                     70: 
1.17      www        71:     my $cookie;
1.5       www        72:     my $now=time;
                     73:     $cookie="$username\_$now\_$domain\_$authhost";
                     74: 
                     75: # Initialize roles
                     76: 
                     77:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
                     78: 
1.7       www        79: # ------------------------------------ Check browser type and MathML capability
1.6       www        80: 
1.45      matthew    81:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                     82:         $clientunicode,$clientos) = &Apache::loncommon::decode_user_agent($r);
1.6       www        83: 
1.40      www        84: # -------------------------------------- Any accessibility options to remember?
                     85:     if (($FORM{'interface'}) && ($FORM{'remember'} eq 'true')) {
1.41      albertel   86: 	foreach ('imagesuppress','appletsuppress',
                     87: 		 'embedsuppress','fontenhance','blackwhite') {
                     88: 	    if ($FORM{$_} eq 'true') {
                     89: 		&Apache::lonnet::put('environment',{$_ => 'on'},
                     90: 				     $domain,$username);
                     91: 	    } else {
                     92: 		&Apache::lonnet::del('environment',[$_],$domain,$username);
                     93: 	    }
                     94: 	}
                     95:     }
1.11      www        96: # ------------------------------------------------------------- Get environment
                     97: 
1.32      albertel   98:     my $userenv;
                     99:     my %userenv=Apache::lonnet::dump('environment',$domain,$username);
                    100:     my ($tmp) = keys(%userenv);
                    101:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    102: 	foreach my $key (keys(%userenv)) {
                    103: 	    $userenv.="environment.$key=$userenv{$key}\n";
                    104: 	}
1.11      www       105:     }
1.38      www       106:     if (($userenv{'interface'}) && (!$FORM{'interface'})) {
                    107: 	$FORM{'interface'}=$userenv{'interface'};
                    108:     }
1.47      www       109:     $ENV{'environment.remote'}=$userenv{'remote'};
1.54      www       110: # --------------- Do not trust query string to be put directly into environment
                    111:     foreach ('imagesuppress','appletsuppress',
                    112: 	     'embedsuppress','fontenhance','blackwhite',
                    113: 	     'interface','localpath','localres') {
                    114: 	$FORM{$_}=~s/[\n\r\=]//gs;
                    115:     }
1.7       www       116: # --------------------------------------------------------- Write first profile
1.5       www       117: 
1.41      albertel  118:     {
                    119: 	my $idf=Apache::File->new(">$lonids/$cookie.id");
                    120: 	unless (flock($idf,LOCK_EX)) {
                    121: 	    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    122: 			   'Could not obtain exclusive lock in lonauth: '.$!);
                    123: 	    $idf->close();
                    124: 	    return 'error: '.$!;
                    125: 	}
                    126: 	if ($userenv ne '') { print $idf "$userenv\n"; }
                    127: 	print $idf "user.name=$username\n";
                    128: 	print $idf "user.domain=$domain\n";
                    129: 	print $idf "user.home=$authhost\n";
                    130: 	print $idf "browser.type=$clientbrowser\n";
                    131: 	print $idf "browser.version=$clientversion\n";
                    132: 	print $idf "browser.mathml=$clientmathml\n";
                    133: 	print $idf "browser.unicode=$clientunicode\n";
                    134: 	print $idf "browser.os=$clientos\n";
1.53      www       135:         if ($FORM{'localpath'}) {
                    136:            print $idf "browser.localpath=$FORM{'localpath'}\n";
                    137:            print $idf "browser.localres=$FORM{'localres'}\n";
                    138:         }
1.41      albertel  139: 	print $idf "request.course.fn=\n";
                    140: 	print $idf "request.course.uri=\n";
                    141: 	print $idf "request.course.sec=\n";
                    142: 	print $idf "request.role=cm\n";
1.48      www       143:         print $idf "request.role.adv=$ENV{'user.adv'}\n";
1.42      www       144: 	print $idf "request.host=$ENV{'REMOTE_ADDR'}\n";
1.41      albertel  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: 	    foreach ('imagesuppress','appletsuppress',
                    150: 		     'embedsuppress','fontenhance','blackwhite') {
                    151: 		if (($FORM{$_} eq 'true') ||
                    152: 		    ($userenv{$_} eq 'on')) {
                    153: 		    print $idf "browser.$_=on\n";
                    154: 		}
1.18      albertel  155: 	    }
1.41      albertel  156: 	}
                    157: 	if ($userroles ne '') { print $idf "$userroles"; }
                    158: 	$idf->close();
                    159:     }
                    160:     $ENV{'request.role'}='cm';
1.48      www       161:     $ENV{'request.role.adv'}=$ENV{'user.adv'};
1.41      albertel  162:     $ENV{'browser.type'}=$clientbrowser;
1.7       www       163: # -------------------------------------------------------------------- Log this
                    164: 
                    165:     &Apache::lonnet::log($domain,$username,$authhost,
                    166:                          "Login $ENV{'REMOTE_ADDR'}");
1.4       www       167: 
1.14      www       168: # ------------------------------------------------- Check for critical messages
                    169: 
1.21      www       170:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14      www       171:     if ($what[0]) {
1.22      www       172: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21      www       173: 	    $lowerurl='/adm/email?critical=display';
1.14      www       174:         }
                    175:     }
                    176: 
1.5       www       177: # ------------------------------------------------------------ Get cookie ready
                    178: 
1.1       albertel  179:     $cookie="lonID=$cookie; path=/";
1.12      www       180: # -------------------------------------------------------- Menu script and info
1.31      www       181:     my $windowinfo=&Apache::lonmenu::open($clientos);
1.35      www       182:     my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
                    183:     my $setflags=&Apache::lonmenu::setflags();
                    184:     my $maincall=&Apache::lonmenu::maincall();
1.52      www       185:     my $bodytag=&Apache::loncommon::bodytag('Successful Login');
1.5       www       186: # ------------------------------------------------- Output for successful login
                    187: 
1.1       albertel  188:     $r->send_cgi_header(<<ENDHEADER);
                    189: Content-type: text/html
                    190: Set-cookie: $cookie
                    191: 
                    192: ENDHEADER
                    193:     $r->print(<<ENDSUCCESS);
                    194: <html>
                    195: <head>
1.4       www       196: <title>Successful Login to the LearningOnline Network with CAPA</title>
1.35      www       197: $startupremote
1.1       albertel  198: </head>
1.43      www       199: $bodytag
1.35      www       200: $setflags
1.19      www       201: $windowinfo
1.6       www       202: <h1>Welcome!</h1>
1.34      www       203: Welcome to the Learning<i>Online</i> Network with CAPA.
                    204: Please wait while your session
                    205: is being set up.<p>
                    206: <a href="/adm/loginproblems.html">Problems?</a></p>
1.35      www       207: $maincall
1.6       www       208: </body>
1.1       albertel  209: </html>
                    210: ENDSUCCESS
                    211: }
                    212: 
                    213: # --------------------------------------------------------------- Failed login!
                    214: 
                    215: sub failed {
                    216:     my ($r,$message) = @_;
1.52      www       217:     my $bodytag=&Apache::loncommon::bodytag('Unsuccessful Login');
1.1       albertel  218:     $r->send_cgi_header(<<ENDFHEADER);
                    219: Content-type: text/html
                    220: 
                    221: ENDFHEADER
                    222:     $r->print(<<ENDFAILED);
                    223: <html>
                    224: <head>
1.4       www       225: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
1.1       albertel  226: </head>
                    227: <html>
1.43      www       228: $bodytag
1.1       albertel  229: <h1>Sorry ...</h1>
1.43      www       230: <p><b>$message</b></p>
1.46      matthew   231: <p>Please <a href="/adm/login?username=$FORM{'uname'}&domain=$FORM{'udom'}">log in again</a>.</p>
1.43      www       232: <p>
                    233: <a href="/adm/loginproblems.html">Problems?</a></p>
1.1       albertel  234: </body>
                    235: </html>
                    236: ENDFAILED
                    237: }
                    238: 
1.55    ! www       239: # ------------------------------------------------------------------ Rerouting!
        !           240: 
        !           241: sub reroute {
        !           242:     my $r=shift;
        !           243:     my $bodytag=&Apache::loncommon::bodytag('Rerouting');
        !           244:     $r->send_cgi_header(<<ENDRFHEADER);
        !           245: Content-type: text/html
        !           246: 
        !           247: ENDRFHEADER
        !           248:     $r->print(<<ENDRFAILED);
        !           249: <html>
        !           250: <head>
        !           251: <title>Rerouting Login to the LearningOnline Network with CAPA</title>
        !           252: </head>
        !           253: <html>
        !           254: $bodytag
        !           255: <h1>Sorry ...</h1>
        !           256: Please <a href="/">log in again</a>.
        !           257: </body>
        !           258: </html>
        !           259: ENDRFAILED
        !           260: }
        !           261: 
1.1       albertel  262: # ---------------------------------------------------------------- Main handler
                    263: 
                    264: sub handler {
                    265:     my $r = shift;
1.55    ! www       266: 
        !           267: # Are we re-routing?
        !           268:     if (-e '/home/httpd/html/lon-status/reroute.txt') {
        !           269: 	&reroute($r);
        !           270: 	return OK;
        !           271:     }
1.1       albertel  272: 
                    273:     my $buffer;
1.49      albertel  274:     $r->read($buffer,$r->header_in('Content-length'),0);
1.1       albertel  275:     my @pairs=split(/&/,$buffer);
1.37      www       276:     my $pair; my $name; my $value;
                    277:     undef %FORM;
                    278:     %FORM=();
1.1       albertel  279:     foreach $pair (@pairs) {
                    280:        ($name,$value) = split(/=/,$pair);
1.7       www       281:        $value =~ tr/+/ /;
                    282:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.1       albertel  283:        $FORM{$name}=$value;
                    284:     } 
                    285: 
                    286:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
1.43      www       287: 	failed($r,'Username, password and domain need to be specified.');
1.1       albertel  288:         return OK;
                    289:     }
                    290:     $FORM{'uname'} =~ s/\W//g;
                    291:     $FORM{'udom'}  =~ s/\W//g;
                    292: 
                    293:     my $role   = $r->dir_config('lonRole');
                    294:     my $domain = $r->dir_config('lonDefDomain');
                    295:     my $prodir = $r->dir_config('lonUsersDir');
                    296: 
1.8       www       297: # ---------------------------------------- Get the information from login token
                    298: 
                    299:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
                    300:                                       $FORM{'serverid'});
                    301: 
                    302:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.43      www       303: 	failed($r,'Information needed to verify your login information is missing, inaccessible or expired.');
1.8       www       304:         return OK;
1.44      www       305:     } else {
                    306:         unless (&Apache::lonnet::reply('tmpdel:'.$FORM{'logtoken'},
                    307:                                          $FORM{'serverid'}) eq 'ok') {
                    308:             &failed($r,'Session could not be opened.');
                    309: 	}
1.8       www       310:     }
1.9       www       311:     my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8       www       312: 
                    313:     my $keybin=pack("H16",$key);
                    314: 
1.26      harris41  315:     my $cipher;
                    316:     if ($Crypt::DES::VERSION>=2.03) {
                    317: 	$cipher=new Crypt::DES $keybin;
                    318:     }
                    319:     else {
                    320: 	$cipher=new DES $keybin;
                    321:     }
1.8       www       322: 
                    323:     my $upass=$cipher->decrypt(
                    324:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
                    325: 
                    326:     $upass.=$cipher->decrypt(
                    327:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
                    328: 
                    329:     $upass=substr($upass,1,ord(substr($upass,0,1)));
                    330: 
1.1       albertel  331: # ---------------------------------------------------------------- Authenticate
                    332:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
1.8       www       333:                                               $upass,
1.1       albertel  334:                                               $FORM{'udom'});
                    335:     
                    336: # --------------------------------------------------------------------- Failed?
                    337: 
                    338:     if ($authhost eq 'no_host') {
1.43      www       339: 	failed($r,'Username and/or password could not be authenticated.');
1.1       albertel  340:         return OK;
                    341:     }
                    342: 
1.13      www       343:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
1.24      www       344: 	$firsturl='/adm/roles';
1.7       www       345:     }
1.1       albertel  346: 
1.8       www       347:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
1.1       albertel  348:     return OK;
                    349: }
                    350: 
                    351: 1;
                    352: __END__
1.7       www       353: 
                    354: 

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