File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.29: download - view: text, annotated - select for diffs
Tue Mar 26 16:05:01 2002 UTC (22 years, 1 month ago) by www
Branches: MAIN
CVS tags: HEAD
Accepts symb now

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

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