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

1.1       albertel    1: # The LearningOnline Network
                      2: # User Authentication Module
1.2       www         3: # 5/21/99,5/22,5/25,5/26,5/27,5/29,6/2,6/11,6/14,6/15
1.4       www         4: # 16/11,12/16,
1.9       www         5: # 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         6: # 7/1,7/10,10/2,10/5,10/9,10/26,10/30,11/10,
                      7: # 05/28,05/29 Gerd Kortemeyer
1.20      harris41    8: # 07/24 Scott Harrison
1.22    ! www         9: # 07/28,08/03 Gerd Kortemeyer
1.1       albertel   10: 
                     11: package Apache::lonauth;
                     12: 
1.18      albertel   13: use strict;
1.1       albertel   14: use Apache::Constants qw(:common);
                     15: use Apache::File;
                     16: use CGI qw(:standard);
                     17: use CGI::Cookie();
1.8       www        18: use Crypt::DES;
1.1       albertel   19: use Apache::lonnet();
1.12      www        20: use Apache::lonmenu();
1.18      albertel   21: use Fcntl qw(:flock);
1.1       albertel   22: # ------------------------------------------------------------ Successful login
                     23: 
                     24: sub success {
1.6       www        25:     my ($r, $username, $domain, $authhost,$lowerurl) = @_;
1.1       albertel   26:     my $lonids=$r->dir_config('lonIDsDir');
1.4       www        27: 
                     28: # See if old ID present, if so, remove
1.17      www        29: 
                     30:     my $filename;
                     31:     opendir(DIR,$lonids);
                     32:     while ($filename=readdir(DIR)) {
                     33:        if ($filename=~/^$username\_\d+\_$domain\_$authhost\.id$/) {
                     34: 	  unlink($lonids.'/'.$filename);
                     35:        }
1.4       www        36:     }
1.17      www        37:     closedir(DIR);
1.4       www        38: 
                     39: # Give them a new cookie
                     40: 
1.17      www        41:     my $cookie;
1.5       www        42:     my $now=time;
                     43:     $cookie="$username\_$now\_$domain\_$authhost";
                     44: 
                     45: # Initialize roles
                     46: 
                     47:     my $userroles=Apache::lonnet::rolesinit($domain,$username,$authhost);
                     48: 
1.7       www        49: # ------------------------------------ Check browser type and MathML capability
1.6       www        50: 
                     51:     my @browsertype=split(/\&/,$r->dir_config("lonBrowsDet"));
                     52:     my %mathcap=split(/\&/,$r->dir_config("lonMathML"));
                     53:     my $httpbrowser=$ENV{"HTTP_USER_AGENT"};
                     54:     my $i;
                     55:     my $clientbrowser='unknown';
                     56:     my $clientversion='0';
                     57:     my $clientmathml='';
                     58:     for ($i=0;$i<=$#browsertype;$i++) {
                     59:         my ($bname,$match,$notmatch,$vreg,$minv)=split(/\:/,$browsertype[$i]);
                     60: 	if (($httpbrowser=~/$match/i)  && ($httpbrowser!~/$notmatch/i)) {
                     61: 	    $clientbrowser=$bname;
                     62:             $httpbrowser=~/$vreg/i;
                     63: 	    $clientversion=$1;
                     64:             $clientmathml=($clientversion>=$minv);
                     65:         }
                     66:     }
                     67:     my $clientos='unknown';
                     68:     if (($httpbrowser=~/linux/i) ||
                     69:         ($httpbrowser=~/unix/i) ||
                     70:         ($httpbrowser=~/ux/i) ||
                     71:         ($httpbrowser=~/solaris/i)) { $clientos='unix'; }
                     72:     if (($httpbrowser=~/vax/i) ||
                     73:         ($httpbrowser=~/vms/i)) { $clientos='vms'; }
                     74:     if ($httpbrowser=~/next/i) { $clientos='next'; }
                     75:     if (($httpbrowser=~/mac/i) ||
                     76:         ($httpbrowser=~/powerpc/i)) { $clientos='mac'; }
1.10      www        77:     if ($httpbrowser=~/win/i) { $clientos='win'; }
1.6       www        78: 
1.11      www        79: # ------------------------------------------------------------- Get environment
                     80: 
                     81:     my $userenv=Apache::lonnet::reply("dump:$domain:$username:environment",
                     82:                                       $authhost);
                     83:     if (($userenv eq 'con_lost') || 
                     84:         ($userenv =~ /^error\:/)) {
                     85:         $userenv='';
                     86:     }
                     87:     $userenv=~s/\&/\nenvironment\./g;
                     88:     if ($userenv ne '') {
                     89: 	$userenv='environment.'.$userenv;
                     90:     }
1.7       www        91: # --------------------------------------------------------- Write first profile
1.5       www        92: 
                     93:        {
1.1       albertel   94: 	    my $idf=Apache::File->new(">$lonids/$cookie.id");
1.18      albertel   95: 	    unless (flock($idf,LOCK_EX)) {
                     96: 	      &Apache::lonnet::logthis("<font color=blue>WARNING: ".
1.19      www        97: 			    'Could not obtain exclusive lock in lonauth: '.$!);
1.18      albertel   98: 	      $idf->close();
                     99: 	      return 'error: '.$!;
                    100: 	    }
1.11      www       101:             if ($userenv ne '') { print $idf "$userenv\n"; }
1.4       www       102:             print $idf "user.name=$username\n";
                    103:             print $idf "user.domain=$domain\n";
                    104:             print $idf "user.home=$authhost\n";
1.6       www       105:             print $idf "browser.type=$clientbrowser\n";
                    106:             print $idf "browser.version=$clientversion\n";
                    107:             print $idf "browser.mathml=$clientmathml\n";
                    108:             print $idf "browser.os=$clientos\n";
1.15      www       109:             print $idf "request.course.fn=\n";
                    110:             print $idf "request.course.uri=\n";
                    111:             print $idf "request.course.sec=\n";
1.16      www       112:             print $idf "request.role=cm\n";
                    113:             print $idf "request.host=$ENV{'HTTP_HOST'}\n"; 
1.11      www       114:             if ($userroles ne '') { print $idf "$userroles"; }
1.18      albertel  115: 	    $idf->close();
1.1       albertel  116:         }
1.19      www       117:          $ENV{'request.role'}='cm';
1.7       www       118: # -------------------------------------------------------------------- Log this
                    119: 
                    120:     &Apache::lonnet::log($domain,$username,$authhost,
                    121:                          "Login $ENV{'REMOTE_ADDR'}");
1.4       www       122: 
1.14      www       123: # ------------------------------------------------- Check for critical messages
                    124: 
1.21      www       125:     my @what=&Apache::lonnet::dump('critical',$domain,$username);
1.14      www       126:     if ($what[0]) {
1.22    ! www       127: 	if (($what[0] ne 'con_lost') && ($what[0]!~/^error\:/)) {
1.21      www       128: 	    $lowerurl='/adm/email?critical=display';
1.14      www       129:         }
                    130:     }
                    131: 
1.5       www       132: # ------------------------------------------------------------ Get cookie ready
                    133: 
1.1       albertel  134:     $cookie="lonID=$cookie; path=/";
1.12      www       135: # -------------------------------------------------------- Menu script and info
                    136:     my $windowinfo=&Apache::lonmenu::open();
1.19      www       137: # ------------------------------------------------------------- Info for Remote
                    138:     my $configmenu=&Apache::lonmenu::rawconfig();
1.5       www       139: # ------------------------------------------------- Output for successful login
                    140: 
1.1       albertel  141:     $r->send_cgi_header(<<ENDHEADER);
                    142: Content-type: text/html
                    143: Set-cookie: $cookie
                    144: 
                    145: ENDHEADER
                    146:     $r->print(<<ENDSUCCESS);
                    147: <html>
                    148: <head>
1.4       www       149: <title>Successful Login to the LearningOnline Network with CAPA</title>
1.19      www       150: <script>
                    151: 
                    152: // --------------------------------------------- Checks if server frame defined
                    153: 
                    154: function checkdef() {
                    155:    if ((menuloaded==0) && (tim==0)) { setTimeout('checkdef()',100); }
                    156: }
                    157: 
                    158: // ---------------------------------------------------------- The wait function
                    159: 
                    160: function wait() {
                    161:    if ((menuloaded==1) || (tim==1)) {
                    162:       if (tim==0) {
                    163:          clearTimeout(canceltim);
                    164:          $configmenu
                    165:          window.location='$lowerurl';  
                    166:       } else {
                    167:          alert("Remote Control Timed Out.");
                    168:       }
                    169:    } else {
                    170:       setTimeout('wait();',100);
                    171:    }
                    172: }
                    173: 
                    174: function main() {
                    175:    canceltim=setTimeout('tim=1;',20000);
                    176:    checkdef();
                    177:    wait();
                    178: }
                    179: 
                    180: </script>
1.1       albertel  181: </head>
1.20      harris41  182: <body bgcolor="#FFFFFF">
1.19      www       183: <script>
                    184:     menuloaded=0;
                    185:     tim=0;
                    186: </script>
                    187: $windowinfo
1.6       www       188: <h1>Welcome!</h1>
1.20      harris41  189: <script>
                    190:     main();
                    191: </script>
1.6       www       192: </body>
1.1       albertel  193: </html>
                    194: ENDSUCCESS
                    195: }
                    196: 
                    197: # --------------------------------------------------------------- Failed login!
                    198: 
                    199: sub failed {
                    200:     my ($r,$message) = @_;
                    201:     $r->send_cgi_header(<<ENDFHEADER);
                    202: Content-type: text/html
                    203: 
                    204: ENDFHEADER
                    205:     $r->print(<<ENDFAILED);
                    206: <html>
                    207: <head>
1.4       www       208: <title>Unsuccessful Login to the LearningOnline Network with CAPA</title>
1.1       albertel  209: </head>
                    210: <html>
                    211: <body bgcolor="#FFFFFF">
                    212: <h1>Sorry ...</h1>
1.4       www       213: <h2>$message to use the Learning<i>Online</i> Network with CAPA</h2>
1.1       albertel  214: </body>
                    215: </html>
                    216: ENDFAILED
                    217: }
                    218: 
                    219: # ---------------------------------------------------------------- Main handler
                    220: 
                    221: sub handler {
                    222:     my $r = shift;
                    223: 
                    224:     my $buffer;
                    225:     $r->read($buffer,$r->header_in('Content-length'));
                    226:     my @pairs=split(/&/,$buffer);
                    227:     my $pair; my $name; my $value; my %FORM;
                    228:     foreach $pair (@pairs) {
                    229:        ($name,$value) = split(/=/,$pair);
1.7       www       230:        $value =~ tr/+/ /;
                    231:        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.1       albertel  232:        $FORM{$name}=$value;
                    233:     } 
                    234: 
                    235:     if ((!$FORM{'uname'}) || (!$FORM{'upass'}) || (!$FORM{'udom'})) {
                    236: 	failed($r,'Username, password and domain need to be specified');
                    237:         return OK;
                    238:     }
                    239:     $FORM{'uname'} =~ s/\W//g;
                    240:     $FORM{'udom'}  =~ s/\W//g;
                    241: 
                    242:     my $role   = $r->dir_config('lonRole');
                    243:     my $domain = $r->dir_config('lonDefDomain');
                    244:     my $prodir = $r->dir_config('lonUsersDir');
                    245: 
1.8       www       246: # ---------------------------------------- Get the information from login token
                    247: 
                    248:     my $tmpinfo=Apache::lonnet::reply('tmpget:'.$FORM{'logtoken'},
                    249:                                       $FORM{'serverid'});
                    250: 
                    251:     if (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost')) {
                    252: 	failed($r,'Login token missing, inaccessible or expired');
                    253:         return OK;
                    254:     }
                    255:     
1.9       www       256:     my ($key,$firsturl)=split(/&/,$tmpinfo);
1.8       www       257: 
                    258:     my $keybin=pack("H16",$key);
                    259: 
                    260:     my $cipher=new DES $keybin;
                    261: 
                    262:     my $upass=$cipher->decrypt(
                    263:        unpack("a8",pack("H16",substr($FORM{'upass'},0,16))));
                    264: 
                    265:     $upass.=$cipher->decrypt(
                    266:        unpack("a8",pack("H16",substr($FORM{'upass'},16,16))));
                    267: 
                    268:     $upass=substr($upass,1,ord(substr($upass,0,1)));
                    269: 
1.1       albertel  270: # ---------------------------------------------------------------- Authenticate
                    271:     my $authhost=Apache::lonnet::authenticate($FORM{'uname'},
1.8       www       272:                                               $upass,
1.1       albertel  273:                                               $FORM{'udom'});
                    274:     
                    275: # --------------------------------------------------------------------- Failed?
                    276: 
                    277:     if ($authhost eq 'no_host') {
                    278: 	failed($r,'Username and/or password could not be authenticated');
                    279:         return OK;
                    280:     }
                    281: 
1.13      www       282:     if (($firsturl eq '') || ($firsturl eq '/adm/logout')) {
1.8       www       283: 	$firsturl='/res/adm/pages/index.html';
1.7       www       284:     }
1.1       albertel  285: 
1.8       www       286:     success($r,$FORM{'uname'},$FORM{'udom'},$authhost,$firsturl);
1.1       albertel  287:     return OK;
                    288: }
                    289: 
                    290: 1;
                    291: __END__
1.7       www       292: 
                    293: 

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