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

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.27      www         3: #
1.64    ! albertel    4: # $Id: lonauth.pm,v 1.63 2004/10/28 16:25:19 albertel 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.56      www        49: use Apache::lonlocal;
1.37      www        50: 
                     51: my %FORM;
                     52: 
1.1       albertel   53: # ------------------------------------------------------------ Successful login
                     54: 
                     55: sub success {
1.6       www        56:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
1.1       albertel   57:     my $lonids=$r->dir_config('lonIDsDir');
1.4       www        58: 
                     59: # See if old ID present, if so, remove
1.17      www        60: 
                     61:     my $filename;
                     62:     opendir(DIR,$lonids);
                     63:     while ($filename=readdir(DIR)) {
                     64:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                     65: 	  unlink($lonids.'/'.$filename);
                     66:        }
1.4       www        67:     }
1.17      www        68:     closedir(DIR);
1.4       www        69: 
                     70: # Give them a new cookie
                     71: 
1.17      www        72:     my $cookie;
1.5       www        73:     my $now=time;
                     74:     $cookie="$username\_$now\_$domain\_$authhost";
                     75: 
                     76: # Initialize roles
                     77: 
                     78:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
                     79: 
1.7       www        80: # ------------------------------------ Check browser type and MathML capability
1.6       www        81: 
1.45      matthew    82:     my ($httpbrowser,$clientbrowser,$clientversion,$clientmathml,
                     83:         $clientunicode,$clientos) = &Apache::loncommon::decode_user_agent($r);
1.6       www        84: 
1.40      www        85: # -------------------------------------- Any accessibility options to remember?
                     86:     if (($FORM{'interface'}) && ($FORM{'remember'} eq 'true')) {
1.41      albertel   87: 	foreach ('imagesuppress','appletsuppress',
                     88: 		 'embedsuppress','fontenhance','blackwhite') {
                     89: 	    if ($FORM{$_} eq 'true') {
                     90: 		&Apache::lonnet::put('environment',{$_ => 'on'},
                     91: 				     $domain,$username);
                     92: 	    } else {
                     93: 		&Apache::lonnet::del('environment',[$_],$domain,$username);
                     94: 	    }
                     95: 	}
                     96:     }
1.11      www        97: # ------------------------------------------------------------- Get environment
                     98: 
1.32      albertel   99:     my $userenv;
                    100:     my %userenv=Apache::lonnet::dump('environment',$domain,$username);
                    101:     my ($tmp) = keys(%userenv);
                    102:     if ($tmp !~ /^(con_lost|error|no_such_host)/i) {
                    103: 	foreach my $key (keys(%userenv)) {
                    104: 	    $userenv.="environment.$key=$userenv{$key}\n";
                    105: 	}
1.11      www       106:     }
1.38      www       107:     if (($userenv{'interface'}) && (!$FORM{'interface'})) {
                    108: 	$FORM{'interface'}=$userenv{'interface'};
                    109:     }
1.47      www       110:     $ENV{'environment.remote'}=$userenv{'remote'};
1.54      www       111: # --------------- Do not trust query string to be put directly into environment
                    112:     foreach ('imagesuppress','appletsuppress',
                    113: 	     'embedsuppress','fontenhance','blackwhite',
                    114: 	     'interface','localpath','localres') {
                    115: 	$FORM{$_}=~s/[\n\r\=]//gs;
                    116:     }
1.7       www       117: # --------------------------------------------------------- Write first profile
1.5       www       118: 
1.41      albertel  119:     {
                    120: 	my $idf=Apache::File->new(">$lonids/$cookie.id");
                    121: 	unless (flock($idf,LOCK_EX)) {
                    122: 	    &Apache::lonnet::logthis("<font color=blue>WARNING: ".
                    123: 			   'Could not obtain exclusive lock in lonauth: '.$!);
                    124: 	    $idf->close();
                    125: 	    return 'error: '.$!;
                    126: 	}
                    127: 	if ($userenv ne '') { print $idf "$userenv\n"; }
                    128: 	print $idf "user.name=$username\n";
                    129: 	print $idf "user.domain=$domain\n";
                    130: 	print $idf "user.home=$authhost\n";
                    131: 	print $idf "browser.type=$clientbrowser\n";
                    132: 	print $idf "browser.version=$clientversion\n";
                    133: 	print $idf "browser.mathml=$clientmathml\n";
                    134: 	print $idf "browser.unicode=$clientunicode\n";
                    135: 	print $idf "browser.os=$clientos\n";
1.53      www       136:         if ($FORM{'localpath'}) {
                    137:            print $idf "browser.localpath=$FORM{'localpath'}\n";
                    138:            print $idf "browser.localres=$FORM{'localres'}\n";
                    139:         }
1.41      albertel  140: 	print $idf "request.course.fn=\n";
                    141: 	print $idf "request.course.uri=\n";
                    142: 	print $idf "request.course.sec=\n";
                    143: 	print $idf "request.role=cm\n";
1.48      www       144:         print $idf "request.role.adv=$ENV{'user.adv'}\n";
1.42      www       145: 	print $idf "request.host=$ENV{'REMOTE_ADDR'}\n";
1.41      albertel  146: 	if ($FORM{'interface'}) {
                    147: 	    $FORM{'interface'}=~s/\W//gs;
                    148: 	    print $idf "browser.interface=$FORM{'interface'}\n";
                    149: 	    $ENV{'browser.interface'}=$FORM{'interface'};
                    150: 	    foreach ('imagesuppress','appletsuppress',
                    151: 		     'embedsuppress','fontenhance','blackwhite') {
                    152: 		if (($FORM{$_} eq 'true') ||
                    153: 		    ($userenv{$_} eq 'on')) {
                    154: 		    print $idf "browser.$_=on\n";
                    155: 		}
1.18      albertel  156: 	    }
1.41      albertel  157: 	}
                    158: 	if ($userroles ne '') { print $idf "$userroles"; }
                    159: 	$idf->close();
                    160:     }
                    161:     $ENV{'request.role'}='cm';
1.48      www       162:     $ENV{'request.role.adv'}=$ENV{'user.adv'};
1.41      albertel  163:     $ENV{'browser.type'}=$clientbrowser;
1.7       www       164: # -------------------------------------------------------------------- Log this
                    165: 
                    166:     &Apache::lonnet::log($domain,$username,$authhost,
                    167:                          "Login $ENV{'REMOTE_ADDR'}");
1.4       www       168: 
1.14      www       169: # ------------------------------------------------- Check for critical messages
                    170: 
1.21      www       171:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14      www       172:     if ($what[0]) {
1.22      www       173: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21      www       174: 	    $lowerurl='/adm/email?critical=display';
1.14      www       175:         }
                    176:     }
                    177: 
1.5       www       178: # ------------------------------------------------------------ Get cookie ready
                    179: 
1.1       albertel  180:     $cookie="lonID=$cookie; path=/";
1.12      www       181: # -------------------------------------------------------- Menu script and info
1.31      www       182:     my $windowinfo=&Apache::lonmenu::open($clientos);
1.35      www       183:     my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
1.63      albertel  184:     my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
1.35      www       185:     my $setflags=&Apache::lonmenu::setflags();
                    186:     my $maincall=&Apache::lonmenu::maincall();
1.52      www       187:     my $bodytag=&Apache::loncommon::bodytag('Successful Login');
1.60      www       188:     my $add=&addcontent();
1.64    ! albertel  189:     my $continuelink;
        !           190:     if (($ENV{'browser.interface'} eq 'textual') ||
        !           191:         ($ENV{'environment.remote'} eq 'off')) {
        !           192: 	$continuelink="<a href=\"$lowerurl\">".&mt('Continue')."</a>";
        !           193:     }
1.5       www       194: # ------------------------------------------------- Output for successful login
                    195: 
1.1       albertel  196:     $r->send_cgi_header(<<ENDHEADER);
1.60      www       197: Content-type: text/html$add
1.1       albertel  198: Set-cookie: $cookie
                    199: 
                    200: ENDHEADER
1.58      www       201:     my %lt=&Apache::lonlocal::texthash(
                    202: 				       'wel' => 'Welcome',
                    203: 				       'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up',
                    204: 				       'pro' => 'Problems',
                    205: 				       'log' => 'loginproblems.html',
                    206: 				       );
1.1       albertel  207:     $r->print(<<ENDSUCCESS);
                    208: <html>
                    209: <head>
1.4       www       210: <title>Successful Login to the LearningOnline Network with CAPA</title>
1.35      www       211: $startupremote
1.1       albertel  212: </head>
1.43      www       213: $bodytag
1.35      www       214: $setflags
1.19      www       215: $windowinfo
1.58      www       216: <h1>$lt{'wel'}</h1>
                    217: $lt{'mes'}.<p>
                    218: <a href="/adm/$lt{'log'}">$lt{'pro'}?</a></p>
1.63      albertel  219: $remoteinfo
1.35      www       220: $maincall
1.64    ! albertel  221: $continuelink
1.6       www       222: </body>
1.1       albertel  223: </html>
                    224: ENDSUCCESS
                    225: }
                    226: 
                    227: # --------------------------------------------------------------- Failed login!
                    228: 
                    229: sub failed {
                    230:     my ($r,$message) = @_;
1.52      www       231:     my $bodytag=&Apache::loncommon::bodytag('Unsuccessful Login');
1.60      www       232:     my $add=&addcontent();
1.1       albertel  233:     $r->send_cgi_header(<<ENDFHEADER);
1.60      www       234: Content-type: text/html$add
1.1       albertel  235: 
                    236: ENDFHEADER
                    237:     $r->print(<<ENDFAILED);
                    238: <html>
                    239: <head>
1.4       www       240: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
1.1       albertel  241: </head>
1.43      www       242: $bodytag
1.1       albertel  243: <h1>Sorry ...</h1>
1.43      www       244: <p><b>$message</b></p>
1.46      matthew   245: <p>Please <a href="/adm/login?username=$FORM{'uname'}&domain=$FORM{'udom'}">log in again</a>.</p>
1.43      www       246: <p>
                    247: <a href="/adm/loginproblems.html">Problems?</a></p>
1.1       albertel  248: </body>
                    249: </html>
                    250: ENDFAILED
1.60      www       251: }
                    252: 
                    253: # --------------------------------------------------------------------- Charset
                    254: 
                    255: sub addcontent {
                    256:     my $encoding=&Apache::lonlocal::current_encoding;
                    257:     if ($encoding) {
                    258: 	return '; charset='.$encoding;
                    259:     } else {
                    260: 	return '';
                    261:     }
1.1       albertel  262: }
                    263: 
1.55      www       264: # ------------------------------------------------------------------ Rerouting!
                    265: 
                    266: sub reroute {
                    267:     my $r=shift;
                    268:     my $bodytag=&Apache::loncommon::bodytag('Rerouting');
                    269:     $r->send_cgi_header(<<ENDRFHEADER);
                    270: Content-type: text/html
                    271: 
                    272: ENDRFHEADER
                    273:     $r->print(<<ENDRFAILED);
                    274: <html>
                    275: <head>
                    276: <title>Rerouting Login to the LearningOnline Network with CAPA</title>
                    277: </head>
                    278: $bodytag
                    279: <h1>Sorry ...</h1>
                    280: Please <a href="/">log in again</a>.
                    281: </body>
                    282: </html>
                    283: ENDRFAILED
                    284: }
                    285: 
1.1       albertel  286: # ---------------------------------------------------------------- Main handler
                    287: 
                    288: sub handler {
                    289:     my $r = shift;
1.55      www       290: 
                    291: # Are we re-routing?
                    292:     if (-e '/home/httpd/html/lon-status/reroute.txt') {
                    293: 	&reroute($r);
                    294: 	return OK;
                    295:     }
1.56      www       296: 
1.57      www       297:     &Apache::lonlocal::get_language_handle($r);
1.1       albertel  298: 
1.59      www       299: # -------------------------------- Prevent users from attempting to login twice
                    300:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                    301:     my $lonid=$cookies{'lonID'};
                    302:     my $cookie;
                    303:     if ($lonid) {
                    304: 	my $handle=$lonid->value;
                    305:         $handle=~s/\W//g;
                    306:         my $lonidsdir=$r->dir_config('lonIDsDir');
                    307:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
                    308: # Indeed, a valid token is found
                    309: 	    $r->send_cgi_header(<<ENDFHEADER);
                    310: Content-type: text/html
                    311: 
                    312: ENDFHEADER
                    313: 	    my $bodytag=&Apache::loncommon::bodytag('Already logged in');
                    314: 	    $r->print(<<ENDFAILED);
                    315: <html>
                    316: <head>
                    317: <title>Already logged in</title>
                    318: </head>
                    319: $bodytag
                    320: <h1>You are already logged in</h1>
                    321: <p>Please either <a href="/adm/roles">continue the current session</a> or
                    322: <a href="/adm/logout">logout</a>.</p>
                    323: <p>
                    324: <a href="/adm/loginproblems.html">Problems?</a></p>
                    325: </body>
                    326: </html>
                    327: ENDFAILED
                    328:            return OK;
                    329: 	}
                    330:     }
                    331: 
                    332: # ---------------------------------------------------- No valid token, continue
                    333: 
                    334: 
1.1       albertel  335:     my $buffer;
1.49      albertel  336:     $r->read($buffer,$r->header_in('Content-length'),0);
1.1       albertel  337:     my @pairs=split(/&/,$buffer);
1.37      www       338:     my $pair; my $name; my $value;
                    339:     undef %FORM;
                    340:     %FORM=();
1.1       albertel  341:     foreach $pair (@pairs) {
                    342:        ($name,$value) = split(/=/,$pair);
1.7       www       343:        $value =~ tr/+/ /;
                    344:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.1       albertel  345:        $FORM{$name}=$value;
                    346:     } 
                    347: 
                    348:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
1.43      www       349: 	failed($r,'Username, password and domain need to be specified.');
1.1       albertel  350:         return OK;
                    351:     }
1.61      www       352: 
                    353: # split user logging in and "su"-user
                    354: 
                    355:     ($FORM{'uname'},$FORM{'suname'})=split(/\:/,$FORM{'uname'});
1.1       albertel  356:     $FORM{'uname'} =~ s/\W//g;
1.61      www       357:     $FORM{'suname'} =~ s/\W//g;
1.1       albertel  358:     $FORM{'udom'}  =~ s/\W//g;
                    359: 
                    360:     my $role   = $r->dir_config('lonRole');
                    361:     my $domain = $r->dir_config('lonDefDomain');
                    362:     my $prodir = $r->dir_config('lonUsersDir');
                    363: 
1.8       www       364: # ---------------------------------------- Get the information from login token
                    365: 
                    366:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
                    367:                                       $FORM{'serverid'});
                    368: 
                    369:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.43      www       370: 	failed($r,'Information needed to verify your login information is missing, inaccessible or expired.');
1.8       www       371:         return OK;
1.44      www       372:     } else {
                    373:         unless (&Apache::lonnet::reply('tmpdel:'.$FORM{'logtoken'},
                    374:                                          $FORM{'serverid'}) eq 'ok') {
                    375:             &failed($r,'Session could not be opened.');
                    376: 	}
1.8       www       377:     }
1.9       www       378:     my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8       www       379: 
                    380:     my $keybin=pack("H16",$key);
                    381: 
1.26      harris41  382:     my $cipher;
                    383:     if ($Crypt::DES::VERSION>=2.03) {
                    384: 	$cipher=new Crypt::DES $keybin;
                    385:     }
                    386:     else {
                    387: 	$cipher=new DES $keybin;
                    388:     }
1.8       www       389: 
                    390:     my $upass=$cipher->decrypt(
                    391:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
                    392: 
                    393:     $upass.=$cipher->decrypt(
                    394:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
                    395: 
                    396:     $upass=substr($upass,1,ord(substr($upass,0,1)));
                    397: 
1.1       albertel  398: # ---------------------------------------------------------------- Authenticate
                    399:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
1.8       www       400:                                               $upass,
1.1       albertel  401:                                               $FORM{'udom'});
                    402:     
                    403: # --------------------------------------------------------------------- Failed?
                    404: 
                    405:     if ($authhost eq 'no_host') {
1.43      www       406: 	failed($r,'Username and/or password could not be authenticated.');
1.1       albertel  407:         return OK;
                    408:     }
                    409: 
1.59      www       410:     if (($firsturl eq '') || 
                    411: 	($firsturl=~/^\/adm\/(logout|remote)/)) {
1.24      www       412: 	$firsturl='/adm/roles';
1.7       www       413:     }
1.61      www       414: # --------------------------------- Are we attempting to login as somebody else?
                    415:     if ($FORM{'suname'}) {
                    416: # ------------ see if the original user has enough privileges to pull this stunt
                    417: 	if (&Apache::lonnet::privileged($FORM{'uname'},$FORM{'udom'})) {
                    418: # ---------------------------------------------------- see if the su-user exists
                    419: 	    unless (&Apache::lonnet::homeserver($FORM{'suname'},$FORM{'udom'})
                    420: 		eq 'no_host') {
                    421: 		&Apache::lonnet::logthis(&Apache::lonnet::homeserver($FORM{'suname'},$FORM{'udom'}));
                    422: # ------------------------------ see if the su-user is not too highly privileged
                    423: 		unless (&Apache::lonnet::privileged($FORM{'suname'},$FORM{'udom'})) {
                    424: # -------------------------------------------------------- actually switch users
                    425: 		    &Apache::lonnet::logperm('User '.$FORM{'uname'}.' at '.$FORM{'udom'}.
                    426: 			' logging in as '.$FORM{'suname'});
                    427: 		    $FORM{'uname'}=$FORM{'suname'};
                    428: 		} else {
                    429: 		    &Apache::lonnet::logthis('Attempted switch user to privileged user');
                    430: 		}
                    431: 	    }
                    432: 	} else {
                    433: 	    &Apache::lonnet::logthis('Non-privileged user attempting switch user');
                    434: 	}
                    435:     }
                    436:     &success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
1.1       albertel  437:     return OK;
                    438: }
                    439: 
                    440: 1;
                    441: __END__
1.7       www       442: 
                    443: 

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