File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.53: download - view: text, annotated - select for diffs
Wed Jul 16 20:42:31 2003 UTC (20 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
Passing of parameters to locate local client-side files.

    1: # The LearningOnline Network
    2: # User Authentication Module
    3: #
    4: # $Id: lonauth.pm,v 1.53 2003/07/16 20:42:31 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/28,08/03 Gerd Kortemeyer
   34: # 8/20 Gerd Kortemeyer
   35: 
   36: package Apache::lonauth;
   37: 
   38: use strict;
   39: use Apache::Constants qw(:common);
   40: use Apache::File;
   41: use CGI qw(:standard);
   42: use CGI::Cookie();
   43: use DynaLoader; # for Crypt::DES version
   44: use Crypt::DES;
   45: use Apache::loncommon();
   46: use Apache::lonnet();
   47: use Apache::lonmenu();
   48: use Fcntl qw(:flock);
   49: 
   50: my %FORM;
   51: 
   52: # ------------------------------------------------------------ Successful login
   53: 
   54: sub success {
   55:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
   56:     my $lonids=$r->dir_config('lonIDsDir');
   57: 
   58: # See if old ID present, if so, remove
   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:        }
   66:     }
   67:     closedir(DIR);
   68: 
   69: # Give them a new cookie
   70: 
   71:     my $cookie;
   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: 
   79: # ------------------------------------ Check browser type and MathML capability
   80: 
   81:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
   82:         $clientunicode,$clientos) = &Apache::loncommon::decode_user_agent($r);
   83: 
   84: # -------------------------------------- Any accessibility options to remember?
   85:     if (($FORM{'interface'}) && ($FORM{'remember'} eq 'true')) {
   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:     }
   96: # ------------------------------------------------------------- Get environment
   97: 
   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: 	}
  105:     }
  106:     if (($userenv{'interface'}) && (!$FORM{'interface'})) {
  107: 	$FORM{'interface'}=$userenv{'interface'};
  108:     }
  109:     $ENV{'environment.remote'}=$userenv{'remote'};
  110: # --------------------------------------------------------- Write first profile
  111: 
  112:     {
  113: 	my $idf=Apache::File->new(">$lonids/$cookie.id");
  114: 	unless (flock($idf,LOCK_EX)) {
  115: 	    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
  116: 			   'Could not obtain exclusive lock in lonauth: '.$!);
  117: 	    $idf->close();
  118: 	    return 'error: '.$!;
  119: 	}
  120: 	if ($userenv ne '') { print $idf "$userenv\n"; }
  121: 	print $idf "user.name=$username\n";
  122: 	print $idf "user.domain=$domain\n";
  123: 	print $idf "user.home=$authhost\n";
  124: 	print $idf "browser.type=$clientbrowser\n";
  125: 	print $idf "browser.version=$clientversion\n";
  126: 	print $idf "browser.mathml=$clientmathml\n";
  127: 	print $idf "browser.unicode=$clientunicode\n";
  128: 	print $idf "browser.os=$clientos\n";
  129:         if ($FORM{'localpath'}) {
  130:            print $idf "browser.localpath=$FORM{'localpath'}\n";
  131:            print $idf "browser.localres=$FORM{'localres'}\n";
  132:         }
  133: 	print $idf "request.course.fn=\n";
  134: 	print $idf "request.course.uri=\n";
  135: 	print $idf "request.course.sec=\n";
  136: 	print $idf "request.role=cm\n";
  137:         print $idf "request.role.adv=$ENV{'user.adv'}\n";
  138: 	print $idf "request.host=$ENV{'REMOTE_ADDR'}\n";
  139: 	if ($FORM{'interface'}) {
  140: 	    $FORM{'interface'}=~s/\W//gs;
  141: 	    print $idf "browser.interface=$FORM{'interface'}\n";
  142: 	    $ENV{'browser.interface'}=$FORM{'interface'};
  143: 	    foreach ('imagesuppress','appletsuppress',
  144: 		     'embedsuppress','fontenhance','blackwhite') {
  145: 		if (($FORM{$_} eq 'true') ||
  146: 		    ($userenv{$_} eq 'on')) {
  147: 		    print $idf "browser.$_=on\n";
  148: 		}
  149: 	    }
  150: 	}
  151: 	if ($userroles ne '') { print $idf "$userroles"; }
  152: 	$idf->close();
  153:     }
  154:     $ENV{'request.role'}='cm';
  155:     $ENV{'request.role.adv'}=$ENV{'user.adv'};
  156:     $ENV{'browser.type'}=$clientbrowser;
  157: # -------------------------------------------------------------------- Log this
  158: 
  159:     &Apache::lonnet::log($domain,$username,$authhost,
  160:                          "Login $ENV{'REMOTE_ADDR'}");
  161: 
  162: # ------------------------------------------------- Check for critical messages
  163: 
  164:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
  165:     if ($what[0]) {
  166: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
  167: 	    $lowerurl='/adm/email?critical=display';
  168:         }
  169:     }
  170: 
  171: # ------------------------------------------------------------ Get cookie ready
  172: 
  173:     $cookie="lonID=$cookie; path=/";
  174: # -------------------------------------------------------- Menu script and info
  175:     my $windowinfo=&Apache::lonmenu::open($clientos);
  176:     my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
  177:     my $setflags=&Apache::lonmenu::setflags();
  178:     my $maincall=&Apache::lonmenu::maincall();
  179:     my $bodytag=&Apache::loncommon::bodytag('Successful Login');
  180: # ------------------------------------------------- Output for successful login
  181: 
  182:     $r->send_cgi_header(<<ENDHEADER);
  183: Content-type: text/html
  184: Set-cookie: $cookie
  185: 
  186: ENDHEADER
  187:     $r->print(<<ENDSUCCESS);
  188: <html>
  189: <head>
  190: <title>Successful Login to the LearningOnline Network with CAPA</title>
  191: $startupremote
  192: </head>
  193: $bodytag
  194: $setflags
  195: $windowinfo
  196: <h1>Welcome!</h1>
  197: Welcome to the Learning<i>Online</i> Network with CAPA.
  198: Please wait while your session
  199: is being set up.<p>
  200: <a href="/adm/loginproblems.html">Problems?</a></p>
  201: $maincall
  202: </body>
  203: </html>
  204: ENDSUCCESS
  205: }
  206: 
  207: # --------------------------------------------------------------- Failed login!
  208: 
  209: sub failed {
  210:     my ($r,$message) = @_;
  211:     my $bodytag=&Apache::loncommon::bodytag('Unsuccessful Login');
  212:     $r->send_cgi_header(<<ENDFHEADER);
  213: Content-type: text/html
  214: 
  215: ENDFHEADER
  216:     $r->print(<<ENDFAILED);
  217: <html>
  218: <head>
  219: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
  220: </head>
  221: <html>
  222: $bodytag
  223: <h1>Sorry ...</h1>
  224: <p><b>$message</b></p>
  225: <p>Please <a href="/adm/login?username=$FORM{'uname'}&domain=$FORM{'udom'}">log in again</a>.</p>
  226: <p>
  227: <a href="/adm/loginproblems.html">Problems?</a></p>
  228: </body>
  229: </html>
  230: ENDFAILED
  231: }
  232: 
  233: # ---------------------------------------------------------------- Main handler
  234: 
  235: sub handler {
  236:     my $r = shift;
  237: 
  238:     my $buffer;
  239:     $r->read($buffer,$r->header_in('Content-length'),0);
  240:     my @pairs=split(/&/,$buffer);
  241:     my $pair; my $name; my $value;
  242:     undef %FORM;
  243:     %FORM=();
  244:     foreach $pair (@pairs) {
  245:        ($name,$value) = split(/=/,$pair);
  246:        $value =~ tr/+/ /;
  247:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  248:        $FORM{$name}=$value;
  249:     } 
  250: 
  251:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
  252: 	failed($r,'Username, password and domain need to be specified.');
  253:         return OK;
  254:     }
  255:     $FORM{'uname'} =~ s/\W//g;
  256:     $FORM{'udom'}  =~ s/\W//g;
  257: 
  258:     my $role   = $r->dir_config('lonRole');
  259:     my $domain = $r->dir_config('lonDefDomain');
  260:     my $prodir = $r->dir_config('lonUsersDir');
  261: 
  262: # ---------------------------------------- Get the information from login token
  263: 
  264:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
  265:                                       $FORM{'serverid'});
  266: 
  267:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
  268: 	failed($r,'Information needed to verify your login information is missing, inaccessible or expired.');
  269:         return OK;
  270:     } else {
  271:         unless (&Apache::lonnet::reply('tmpdel:'.$FORM{'logtoken'},
  272:                                          $FORM{'serverid'}) eq 'ok') {
  273:             &failed($r,'Session could not be opened.');
  274: 	}
  275:     }
  276:     my ($key,$firsturl)=split(/&/,$tmpinfo);
  277: 
  278:     my $keybin=pack("H16",$key);
  279: 
  280:     my $cipher;
  281:     if ($Crypt::DES::VERSION>=2.03) {
  282: 	$cipher=new Crypt::DES $keybin;
  283:     }
  284:     else {
  285: 	$cipher=new DES $keybin;
  286:     }
  287: 
  288:     my $upass=$cipher->decrypt(
  289:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
  290: 
  291:     $upass.=$cipher->decrypt(
  292:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
  293: 
  294:     $upass=substr($upass,1,ord(substr($upass,0,1)));
  295: 
  296: # ---------------------------------------------------------------- Authenticate
  297:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
  298:                                               $upass,
  299:                                               $FORM{'udom'});
  300:     
  301: # --------------------------------------------------------------------- Failed?
  302: 
  303:     if ($authhost eq 'no_host') {
  304: 	failed($r,'Username and/or password could not be authenticated.');
  305:         return OK;
  306:     }
  307: 
  308:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
  309: 	$firsturl='/adm/roles';
  310:     }
  311: 
  312:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
  313:     return OK;
  314: }
  315: 
  316: 1;
  317: __END__
  318: 
  319: 

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