Annotation of loncom/auth/lonacc.pm, revision 1.27

1.1       albertel    1: # The LearningOnline Network
                      2: # Cookie Based Access Handler
1.22      www         3: #
1.27    ! www         4: # $Id: lonacc.pm,v 1.26 2002/01/04 14:57:14 www Exp $
1.22      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.25      harris41   28: # YEAR=1999
1.4       www        29: # 5/21/99,5/22,5/29,5/31,6/15,16/11,22/11,
1.25      harris41   30: # YEAR=2000
1.14      www        31: # 01/06,01/13,05/31,06/01,09/06,09/25,09/28,10/30,11/6,
1.17      www        32: # 12/25,12/26,
1.25      harris41   33: # YEAR=2001
1.23      www        34: # 01/06/01,05/28,8/11,9/26,11/29 Gerd Kortemeyer
1.25      harris41   35: # 12/15 Scott Harrison
1.26      www        36: # YEAR=2002
1.27    ! www        37: # 1/4,2/25 Gerd Kortemeyer
1.25      harris41   38: #
                     39: ###
1.1       albertel   40: 
                     41: package Apache::lonacc;
                     42: 
                     43: use strict;
1.8       www        44: use Apache::Constants qw(:common :http :methods);
1.2       www        45: use Apache::File;
1.6       www        46: use Apache::lonnet;
1.25      harris41   47: use Apache::loncommon();
1.1       albertel   48: use CGI::Cookie();
1.16      www        49: use Fcntl qw(:flock);
1.1       albertel   50: 
                     51: sub handler {
                     52:     my $r = shift;
                     53:     my $requrl=$r->uri;
                     54:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     55:     my $lonid=$cookies{'lonID'};
                     56:     my $cookie;
                     57:     if ($lonid) {
                     58: 	my $handle=$lonid->value;
                     59:         $handle=~s/\W//g;
                     60:         my $lonidsdir=$r->dir_config('lonIDsDir');
                     61:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.6       www        62: 
                     63: # ------------------------------------------- Transfer profile into environment
                     64: 
1.2       www        65:             my @profile;
                     66: 	    {
                     67:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
1.16      www        68:              flock($idf,LOCK_SH);
1.2       www        69:              @profile=<$idf>;
1.16      www        70:              $idf->close();
1.2       www        71: 	    }
                     72:             my $envi;
                     73:             for ($envi=0;$envi<=$#profile;$envi++) {
                     74: 		chomp($profile[$envi]);
                     75: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
1.9       www        76:                 $ENV{$envname} = $envvalue;
1.2       www        77:             }
1.9       www        78:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
1.18      www        79:             if ($requrl=~/^\/res\//) {
1.17      www        80:                $ENV{'request.state'} = "published";
                     81: 	    } else {
                     82: 	       $ENV{'request.state'} = 'unknown';
                     83:             }
1.9       www        84:             $ENV{'request.filename'} = $r->filename;
1.12      www        85: 
1.6       www        86: # -------------------------------------------------------- Load POST parameters
                     87: 
1.8       www        88: 
                     89:            
1.14      www        90:         my $buffer;
1.6       www        91: 
1.14      www        92:         $r->read($buffer,$r->header_in('Content-length'));
                     93: 
                     94: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
1.5       www        95:             my @pairs=split(/&/,$buffer);
1.6       www        96:             my $pair;
1.5       www        97:             foreach $pair (@pairs) {
1.6       www        98:                my ($name,$value) = split(/=/,$pair);
                     99:                $value =~ tr/+/ /;
                    100:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.10      www       101:                $name  =~ tr/+/ /;
                    102:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.27    ! www       103:                if ($ENV{"form.$name"}) {
        !           104:                   $ENV{"form.$name"}.=','.$value;
        !           105: 	       } else {
        !           106:                   $ENV{"form.$name"}=$value;
        !           107: 	       }
1.6       www       108:             }
1.14      www       109:         } else {
                    110: 	    my $contentsep=$1;
                    111:             my @lines = split (/\n/,$buffer);
                    112:             my $name='';
                    113:             my $value='';
                    114:             my $fname='';
                    115:             my $fmime='';
                    116:             my $i;
                    117:             for ($i=0;$i<=$#lines;$i++) {
                    118: 		if ($lines[$i]=~/^$contentsep/) {
                    119: 		    if ($name) {
                    120:                         chomp($value);
                    121: 			if ($fname) {
                    122: 			    $ENV{"form.$name.filename"}=$fname;
                    123:                             $ENV{"form.$name.mimetype"}=$fmime;
1.15      www       124:                         } else {
                    125:                             $value=~s/\s+$//s;
1.14      www       126:                         }
1.15      www       127:                         $ENV{"form.$name"}=$value;
1.14      www       128:                     }
                    129:                     if ($i<$#lines) {
                    130: 			$i++;
                    131:                         $lines[$i]=~
                    132: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
                    133:                         $name=$1;
                    134:                         $value='';
                    135:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
                    136: 			   $fname=$1;
                    137:                            if 
                    138:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
                    139: 			      $fmime=$1;
                    140:                               $i++;
                    141: 			   } else {
                    142:                               $fmime='';
                    143:                            }
                    144:                         } else {
                    145: 			    $fname='';
                    146:                             $fmime='';
                    147:                         }
                    148:                         $i++;
                    149:                     }
                    150:                 } else {
                    151: 		    $value.=$lines[$i]."\n";
                    152:                 }
                    153:             }
                    154: 	}
1.26      www       155: 	    $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
1.8       www       156:             $r->method_number(M_GET);
                    157: 	    $r->method('GET');
                    158:             $r->headers_in->unset('Content-length');
1.6       www       159: 
                    160: # ---------------------------------------------------------------- Check access
                    161: 
                    162:             if ($requrl!~/^\/adm\//) {
1.7       www       163: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
                    164:                 if ($access eq '1') {
                    165: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
                    166: 	           return HTTP_NOT_ACCEPTABLE; 
                    167:                 }
                    168:                 if (($access ne '2') && ($access ne 'F')) {
                    169: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
                    170: 	           return HTTP_NOT_ACCEPTABLE; 
                    171:                 }
1.23      www       172:             }
                    173: # ------------------------------------------------------------- This is allowed
                    174:           if ($ENV{'request.course.id'}) {
1.24      www       175: 	    &Apache::lonnet::countacc($requrl);
1.23      www       176:             $requrl=~/\.(\w+)$/;
1.25      harris41  177:             if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
1.23      www       178: # ------------------------------------- This is serious stuff, get symb and log
                    179: 		my $symb=&Apache::lonnet::symbread;
                    180:                 $ENV{'request.symb'}=$symb;
                    181:                 &Apache::lonnet::courseacclog($symb);
                    182:             } else {
                    183: # ------------------------------------------------------- This is other content
                    184:                 &Apache::lonnet::courseacclog($requrl);    
                    185:             }
                    186: 	  }
1.2       www       187:             return OK; 
1.1       albertel  188:         } else { 
1.5       www       189:             $r->log_reason("Cookie $handle not valid", $r->filename) 
1.1       albertel  190:         };
                    191:     }
1.6       www       192: 
1.21      www       193: # -------------------------------------------- See if this is a public resource
                    194:     if (&Apache::lonnet::metadata($requrl,'copyright') eq 'public') {
                    195:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
                    196: 	$ENV{'user.name'}='public';
                    197:         $ENV{'user.domain'}='public';
                    198:         $ENV{'request.state'} = "published";
                    199:         $ENV{'request.publicaccess'} = 1;
                    200:         $ENV{'request.filename'} = $r->filename;
                    201:         return OK;
                    202:     }
1.6       www       203: # ----------------------------------------------- Store where they wanted to go
1.21      www       204:     
1.6       www       205:     $ENV{'request.firsturl'}=$requrl;
1.1       albertel  206:     return FORBIDDEN;
                    207: }
                    208: 
                    209: 1;
                    210: __END__
1.25      harris41  211: 
                    212: =head1 NAME
                    213: 
                    214: Apache::lonacc - Cookie Based Access Handler
                    215: 
                    216: =head1 SYNOPSIS
                    217: 
                    218: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    219: 
                    220:  PerlAccessHandler       Apache::lonacc
                    221: 
                    222: =head1 INTRODUCTION
                    223: 
                    224: This module enables cookie based authentication and is used
                    225: to control access for many different LON-CAPA URIs.
                    226: 
                    227: Whenever the client sends the cookie back to the server, 
                    228: this cookie is handled by either lonacc.pm or loncacc.pm
                    229: (see srm.conf for what is invoked when).  If
                    230: the cookie is missing or invalid, the user is re-challenged
                    231: for login information.
                    232: 
                    233: This is part of the LearningOnline Network with CAPA project
                    234: described at http://www.lon-capa.org.
                    235: 
                    236: =head1 HANDLER SUBROUTINE
                    237: 
                    238: This routine is called by Apache and mod_perl.
                    239: 
                    240: =over 4
                    241: 
                    242: =item *
                    243: 
                    244: transfer profile into environment
                    245: 
                    246: =item *
                    247: 
                    248: load POST parameters
                    249: 
                    250: =item *
                    251: 
                    252: check access
                    253: 
                    254: =item *
                    255: 
                    256: if allowed, get symb, log, generate course statistics if applicable
                    257: 
                    258: =item *
                    259: 
                    260: otherwise return error
                    261: 
                    262: =item *
                    263: 
                    264: see if public resource
                    265: 
                    266: =item *
                    267: 
                    268: store attempted access
                    269: 
                    270: =back
                    271: 
                    272: =cut

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