File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.25: download - view: text, annotated - select for diffs
Sat Dec 15 20:40:23 2001 UTC (22 years, 5 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
adding in POD documentation; using loncommon::fileembstyle instead of
lonnet -Scott Harrison

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

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