File:  [LON-CAPA] / loncom / auth / lonauth.pm
Revision 1.38: download - view: text, annotated - select for diffs
Tue Feb 18 19:12:38 2003 UTC (21 years, 4 months ago) by www
Branches: MAIN
CVS tags: HEAD
Transfer login screen accessibility choices to session environment

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

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