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

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.27      www         3: #
1.92    ! bisitz      4: # $Id: lonauth.pm,v 1.91 2008/05/14 18:27:30 raeburn 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.1       albertel   28: 
                     29: package Apache::lonauth;
                     30: 
1.18      albertel   31: use strict;
1.78      albertel   32: use LONCAPA;
1.1       albertel   33: use Apache::Constants qw(:common);
                     34: use CGI qw(:standard);
1.26      harris41   35: use DynaLoader; # for Crypt::DES version
1.8       www        36: use Crypt::DES;
1.45      matthew    37: use Apache::loncommon();
1.66      albertel   38: use Apache::lonnet;
1.12      www        39: use Apache::lonmenu();
1.90      raeburn    40: use Apache::createaccount;
1.18      albertel   41: use Fcntl qw(:flock);
1.56      www        42: use Apache::lonlocal;
1.85      albertel   43:  
1.1       albertel   44: # ------------------------------------------------------------ Successful login
1.85      albertel   45: sub success {
                     46:     my ($r, $username, $domain, $authhost, $lowerurl, $extra_env,
                     47: 	$form) = @_;
1.1       albertel   48: 
1.85      albertel   49: # ------------------------------------------------------------ Get cookie ready
                     50:     my $cookie =
                     51: 	&Apache::loncommon::init_user_environment($r, $username, $domain,
                     52: 						  $authhost, $form,
1.86      albertel   53: 						  {'extra_env' => $extra_env,});
1.4       www        54: 
1.69      albertel   55:     my $public=($username eq 'public' && $domain eq 'public');
                     56: 
1.85      albertel   57:     if ($public or $lowerurl eq 'noredirect') { return $cookie; }
1.78      albertel   58: 
1.7       www        59: # -------------------------------------------------------------------- Log this
                     60: 
                     61:     &Apache::lonnet::log($domain,$username,$authhost,
                     62:                          "Login $ENV{'REMOTE_ADDR'}");
1.4       www        63: 
1.14      www        64: # ------------------------------------------------- Check for critical messages
                     65: 
1.21      www        66:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14      www        67:     if ($what[0]) {
1.22      www        68: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21      www        69: 	    $lowerurl='/adm/email?critical=display';
1.14      www        70:         }
                     71:     }
                     72: 
1.5       www        73: # ------------------------------------------------------------ Get cookie ready
1.1       albertel   74:     $cookie="lonID=$cookie; path=/";
1.12      www        75: # -------------------------------------------------------- Menu script and info
1.85      albertel   76:     my $windowinfo=&Apache::lonmenu::open($env{'browser.os'});
1.35      www        77:     my $startupremote=&Apache::lonmenu::startupremote($lowerurl);
1.63      albertel   78:     my $remoteinfo=&Apache::lonmenu::load_remote_msg($lowerurl);
1.35      www        79:     my $setflags=&Apache::lonmenu::setflags();
                     80:     my $maincall=&Apache::lonmenu::maincall();
1.74      albertel   81:     my $start_page=&Apache::loncommon::start_page('Successful Login',
1.76      albertel   82: 						  $startupremote,
                     83: 						  {'no_inline_link' => 1,});
1.74      albertel   84:     my $end_page  =&Apache::loncommon::end_page();
                     85: 
1.64      albertel   86:     my $continuelink;
1.66      albertel   87:     if (($env{'browser.interface'} eq 'textual') ||
                     88:         ($env{'environment.remote'} eq 'off')) {
1.64      albertel   89: 	$continuelink="<a href=\"$lowerurl\">".&mt('Continue')."</a>";
                     90:     }
1.5       www        91: # ------------------------------------------------- Output for successful login
                     92: 
1.74      albertel   93:     &Apache::loncommon::content_type($r,'text/html');
                     94:     $r->header_out('Set-cookie' => $cookie);
                     95:     $r->send_http_header;
1.1       albertel   96: 
1.58      www        97:     my %lt=&Apache::lonlocal::texthash(
                     98: 				       'wel' => 'Welcome',
1.92    ! bisitz     99: 				       'mes' => 'Welcome to the Learning<i>Online</i> Network with CAPA. Please wait while your session is being set up.',
        !           100: 				       'pro' => 'Login problems?',
1.58      www       101: 				       'log' => 'loginproblems.html',
                    102: 				       );
1.1       albertel  103:     $r->print(<<ENDSUCCESS);
1.74      albertel  104: $start_page
1.35      www       105: $setflags
1.19      www       106: $windowinfo
1.58      www       107: <h1>$lt{'wel'}</h1>
1.92    ! bisitz    108: $lt{'mes'}<p>
        !           109: <a href="/adm/$lt{'log'}">$lt{'pro'}</a></p>
1.63      albertel  110: $remoteinfo
1.35      www       111: $maincall
1.64      albertel  112: $continuelink
1.74      albertel  113: $end_page
1.1       albertel  114: ENDSUCCESS
                    115: }
                    116: 
                    117: # --------------------------------------------------------------- Failed login!
                    118: 
                    119: sub failed {
1.85      albertel  120:     my ($r,$message,$form) = @_;
1.76      albertel  121:     my $start_page = &Apache::loncommon::start_page('Unsuccessful Login',undef,
                    122: 						    {'no_inline_link' => 1,});
1.74      albertel  123:     my $end_page   = &Apache::loncommon::end_page();
                    124:     &Apache::loncommon::content_type($r,'text/html');
                    125:     $r->send_http_header;
1.92    ! bisitz    126:     $r->print(
        !           127:        $start_page
        !           128:       .'<h1>'.&mt('Sorry ...').'</h1>'
        !           129:       .'<p><b>'.&mt($message).'</b></p>'
        !           130:       .'<p>'.&mt('Please [_1]log in again[_2].'
        !           131:                 ,"<a href=\"/adm/login?username=$form->{'uname'}&domain=$form->{'udom'}\">",'</a>')
        !           132:       .'</p>'
        !           133:       .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'
        !           134:       .$end_page
        !           135:     );
        !           136:  }
1.60      www       137: 
1.55      www       138: # ------------------------------------------------------------------ Rerouting!
                    139: 
                    140: sub reroute {
1.74      albertel  141:     my ($r) = @_;
                    142:     &Apache::loncommon::content_type($r,'text/html');
                    143:     $r->send_http_header;
1.92    ! bisitz    144:     my $msg='<h1>'.&mt('Sorry ...').'</h1>'
        !           145:            .&mt('Please [_1]log in again[_2].');
1.74      albertel  146:     &Apache::loncommon::simple_error_page($r,'Rerouting',$msg);
1.55      www       147: }
                    148: 
1.1       albertel  149: # ---------------------------------------------------------------- Main handler
                    150: 
                    151: sub handler {
                    152:     my $r = shift;
1.85      albertel  153:     my $form;
1.55      www       154: # Are we re-routing?
                    155:     if (-e '/home/httpd/html/lon-status/reroute.txt') {
                    156: 	&reroute($r);
                    157: 	return OK;
                    158:     }
1.56      www       159: 
1.57      www       160:     &Apache::lonlocal::get_language_handle($r);
1.1       albertel  161: 
1.59      www       162: # -------------------------------- Prevent users from attempting to login twice
1.89      albertel  163:     my $handle = &Apache::lonnet::check_for_valid_session($r);
                    164:     if ($handle ne '') {
1.59      www       165: # Indeed, a valid token is found
1.89      albertel  166: 	&Apache::loncommon::content_type($r,'text/html');
                    167: 	$r->send_http_header;
                    168: 	my $start_page = 
                    169: 	    &Apache::loncommon::start_page('Already logged in');
                    170: 	my $end_page = 
                    171: 	    &Apache::loncommon::end_page();
1.92    ! bisitz    172:         $r->print(
        !           173:            $start_page
        !           174:           .'<h1>'.&mt('You are already logged in!').'</h1>'
        !           175:           .'<p>'.&mt('Please either [_1]continue the current session[_2] or [_3]logout[_4].'
        !           176:                     ,'<a href="/adm/roles">','</a>','<a href="/adm/logout">','</a>')
        !           177:           .'</p>'
        !           178:           .'<p><a href="/adm/loginproblems.html">'.&mt('Login problems?').'</a></p>'
        !           179:           .$end_page
        !           180:         );
        !           181:         return OK;
1.59      www       182:     }
                    183: 
                    184: # ---------------------------------------------------- No valid token, continue
                    185: 
                    186: 
1.1       albertel  187:     my $buffer;
1.84      albertel  188:     if ($r->header_in('Content-length') > 0) {
                    189: 	$r->read($buffer,$r->header_in('Content-length'),0);
                    190:     }
1.85      albertel  191:     my %form;
                    192:     foreach my $pair (split(/&/,$buffer)) {
                    193:        my ($name,$value) = split(/=/,$pair);
1.7       www       194:        $value =~ tr/+/ /;
                    195:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.85      albertel  196:        $form{$name}=$value;
1.1       albertel  197:     } 
                    198: 
1.85      albertel  199:     if ((!$form{'uname'}) || (!$form{'upass0'}) || (!$form{'udom'})) {
                    200: 	&failed($r,'Username, password and domain need to be specified.',
                    201: 		\%form);
1.1       albertel  202:         return OK;
                    203:     }
1.61      www       204: 
                    205: # split user logging in and "su"-user
                    206: 
1.85      albertel  207:     ($form{'uname'},$form{'suname'})=split(/\:/,$form{'uname'});
1.87      albertel  208:     $form{'uname'} = &LONCAPA::clean_username($form{'uname'});
                    209:     $form{'suname'}= &LONCAPA::clean_username($form{'suname'});
                    210:     $form{'udom'}  = &LONCAPA::clean_domain(  $form{'udom'});
1.1       albertel  211: 
                    212:     my $role   = $r->dir_config('lonRole');
                    213:     my $domain = $r->dir_config('lonDefDomain');
                    214:     my $prodir = $r->dir_config('lonUsersDir');
                    215: 
1.8       www       216: # ---------------------------------------- Get the information from login token
                    217: 
1.85      albertel  218:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$form{'logtoken'},
                    219:                                       $form{'serverid'});
1.8       www       220: 
                    221:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
1.85      albertel  222: 	&failed($r,'Information needed to verify your login information is missing, inaccessible or expired.',\%form);
1.8       www       223:         return OK;
1.44      www       224:     } else {
1.85      albertel  225: 	my $reply = &Apache::lonnet::reply('tmpdel:'.$form{'logtoken'},
                    226: 					   $form{'serverid'});
1.77      albertel  227:         if ( $reply ne 'ok' ) {
1.85      albertel  228:             &failed($r,'Session could not be opened.',\%form);
                    229: 	    &Apache::lonnet::logthis("ERROR got a reply of $reply when trying to contact ". $form{'serverid'}." to get login token");
1.77      albertel  230: 	    return OK;
1.44      www       231: 	}
1.8       www       232:     }
1.9       www       233:     my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8       www       234: 
                    235:     my $keybin=pack("H16",$key);
                    236: 
1.26      harris41  237:     my $cipher;
                    238:     if ($Crypt::DES::VERSION>=2.03) {
                    239: 	$cipher=new Crypt::DES $keybin;
                    240:     }
                    241:     else {
                    242: 	$cipher=new DES $keybin;
                    243:     }
1.67      www       244:     my $upass='';
                    245:     for (my $i=0;$i<=2;$i++) {
                    246: 	my $chunk=
1.85      albertel  247: 	    $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},0,16))));
1.8       www       248: 
1.67      www       249: 	$chunk.=
1.85      albertel  250: 	    $cipher->decrypt(unpack("a8",pack("H16",substr($form{'upass'.$i},16,16))));
1.8       www       251: 
1.67      www       252: 	$chunk=substr($chunk,1,ord(substr($chunk,0,1)));
                    253: 	$upass.=$chunk;
                    254:     }
1.8       www       255: 
1.1       albertel  256: # ---------------------------------------------------------------- Authenticate
1.91      raeburn   257:     my @cancreate;
1.90      raeburn   258:     my %domconfig = &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
                    259:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                    260:         if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
1.91      raeburn   261:             if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
                    262:                 @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
                    263:             } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
                    264:                      ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
                    265:                 @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
1.90      raeburn   266:             }
                    267:         }
                    268:     }
                    269:     my $defaultauth;
1.91      raeburn   270:     if (grep(/^login$/,@cancreate)) {
1.90      raeburn   271:         $defaultauth = 1;
                    272:     }
                    273:     my $authhost=Apache::lonnet::authenticate($form{'uname'},$upass,
                    274:                                               $form{'udom'},$defaultauth);
1.1       albertel  275:     
                    276: # --------------------------------------------------------------------- Failed?
                    277: 
                    278:     if ($authhost eq 'no_host') {
1.85      albertel  279: 	&failed($r,'Username and/or password could not be authenticated.',
                    280: 		\%form);
1.1       albertel  281:         return OK;
1.90      raeburn   282:     } elsif ($authhost eq 'no_account_on_host') {
                    283:         my %domconfig = 
                    284:             &Apache::lonnet::get_dom('configuration',['usercreation'],$form{'udom'});
1.91      raeburn   285:         if (grep(/^login$/,@cancreate)) {
1.90      raeburn   286:             my $start_page = 
                    287:                 &Apache::loncommon::start_page('Create a user account in LON-CAPA',
                    288:                                                '',{'no_inline_link'   => 1,});
                    289:             my $domdesc = &Apache::lonnet::domain($form{'udom'},'description');
1.91      raeburn   290:             my ($output,$checkfail) = &Apache::createaccount::username_check($form{'uname'},
                    291:                                                                   $form{'udom'},$domdesc);
1.90      raeburn   292:             &Apache::loncommon::content_type($r,'text/html');
                    293:             $r->send_http_header;
                    294:             &Apache::createaccount::print_header($r,$start_page);
1.91      raeburn   295:             my $msg = '<h4>'.&mt('Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain.').'<br />';
                    296:             if ($checkfail) {
                    297:                 $msg .= &mt('A LON-CAPA account may not be created with the username you used.');
                    298:             } else {
                    299:                 $msg .= &mt('To create one, use the table below to provide information about yourself (if appropriate), then click the "Create LON-CAPA account" button.');
                    300:             }
                    301:             $r->print('<h4>'.$msg.'</h4>'.$output);
1.90      raeburn   302:             $r->print(&Apache::loncommon::end_page());
                    303:             return OK;
                    304:         } else {
                    305:             &failed($r,'Although your username and password were authenticated, you do not currently have a LON-CAPA account in this domain, and you are not permitted to create one.',\%form);
                    306:             return OK;
                    307:         }
1.1       albertel  308:     }
                    309: 
1.59      www       310:     if (($firsturl eq '') || 
                    311: 	($firsturl=~/^\/adm\/(logout|remote)/)) {
1.24      www       312: 	$firsturl='/adm/roles';
1.7       www       313:     }
1.61      www       314: # --------------------------------- Are we attempting to login as somebody else?
1.85      albertel  315:     if ($form{'suname'}) {
1.61      www       316: # ------------ see if the original user has enough privileges to pull this stunt
1.85      albertel  317: 	if (&Apache::lonnet::privileged($form{'uname'},$form{'udom'})) {
1.61      www       318: # ---------------------------------------------------- see if the su-user exists
1.85      albertel  319: 	    unless (&Apache::lonnet::homeserver($form{'suname'},$form{'udom'})
1.61      www       320: 		eq 'no_host') {
1.85      albertel  321: 		&Apache::lonnet::logthis(&Apache::lonnet::homeserver($form{'suname'},$form{'udom'}));
1.61      www       322: # ------------------------------ see if the su-user is not too highly privileged
1.85      albertel  323: 		unless (&Apache::lonnet::privileged($form{'suname'},$form{'udom'})) {
1.61      www       324: # -------------------------------------------------------- actually switch users
1.85      albertel  325: 		    &Apache::lonnet::logperm('User '.$form{'uname'}.' at '.$form{'udom'}.
                    326: 			' logging in as '.$form{'suname'});
                    327: 		    $form{'uname'}=$form{'suname'};
1.61      www       328: 		} else {
                    329: 		    &Apache::lonnet::logthis('Attempted switch user to privileged user');
                    330: 		}
                    331: 	    }
                    332: 	} else {
                    333: 	    &Apache::lonnet::logthis('Non-privileged user attempting switch user');
                    334: 	}
                    335:     }
1.85      albertel  336: 
1.81      albertel  337:     if ($r->dir_config("lonBalancer") eq 'yes') {
1.85      albertel  338: 	&success($r,$form{'uname'},$form{'udom'},$authhost,'noredirect',undef,
                    339: 		 \%form);
1.81      albertel  340: 	$r->internal_redirect('/adm/switchserver');
                    341:     } else {
1.85      albertel  342: 	&success($r,$form{'uname'},$form{'udom'},$authhost,$firsturl,undef,
                    343: 		 \%form);
1.81      albertel  344:     }
1.1       albertel  345:     return OK;
                    346: }
                    347: 
                    348: 1;
                    349: __END__
1.7       www       350: 
                    351: 

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