File:  [LON-CAPA] / loncom / auth / loncacc.pm
Revision 1.24: download - view: text, annotated - select for diffs
Tue Dec 10 20:37:21 2002 UTC (21 years, 6 months ago) by matthew
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
Remainder of fix for bug 262.  Do not allow editing of old versions of
resources in the construction space.

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler for Construction Area
    3: # (lonacc: 5/21/99,5/22,5/29,5/31 Gerd Kortemeyer)
    4: #
    5: # $Id: loncacc.pm,v 1.24 2002/12/10 20:37:21 matthew Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: # YEAR=2000
   30: # 6/15,16/11,22/11,
   31: # YEAR=2001
   32: # 01/06,01/11,6/1,9/25,9/28,11/22,12/25,12/26,
   33: # 01/06/01,05/04,05/05,05/09 Gerd Kortemeyer
   34: # 12/21 Scott Harrison
   35: # YEAR=2002
   36: # 1/4 Gerd Kortemeyer
   37: ###
   38: 
   39: package Apache::loncacc;
   40: 
   41: use strict;
   42: use Apache::Constants qw(:common :http :methods);
   43: use Apache::File;
   44: use CGI::Cookie();
   45: use Fcntl qw(:flock);
   46: 
   47: sub constructaccess {
   48:     my ($url,$ownerdomain)=@_;
   49:     my ($ownername)=($url=~/\/(?:\~|priv\/|home\/)(\w+)/);
   50:     unless (($ownername) && ($ownerdomain)) { return ''; }
   51:     # We do not allow editing of previous versions of files.
   52:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
   53:     if (($ownername eq $ENV{'user.name'}) &&
   54:         ($ownerdomain eq $ENV{'user.domain'})) {
   55: 	return ($ownername,$ownerdomain);
   56:     }
   57: 
   58:     my $capriv='user.priv.ca./'.
   59:                $ownerdomain.'/'.$ownername.'./'.
   60: 	       $ownerdomain.'/'.$ownername;
   61:     foreach (keys %ENV) {
   62:         if ($_ eq $capriv) {
   63:            return ($ownername,$ownerdomain);
   64:         }
   65:     }
   66: 
   67:     return '';
   68: }
   69: 
   70: sub handler {
   71:     my $r = shift;
   72:     my $requrl=$r->uri;
   73:     $ENV{'request.editurl'}=$requrl;
   74:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   75:     my $lonid=$cookies{'lonID'};
   76:     my $cookie;
   77:     if ($lonid) {
   78: 	my $handle=$lonid->value;
   79:         $handle=~s/\W//g;
   80:         my $lonidsdir=$r->dir_config('lonIDsDir');
   81:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   82:             my @profile;
   83: 	    {
   84:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
   85:              flock($idf,LOCK_SH);
   86:              @profile=<$idf>;
   87:              $idf->close();
   88: 	    }
   89:             my $envi;
   90:             for ($envi=0;$envi<=$#profile;$envi++) {
   91: 		chomp($profile[$envi]);
   92: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
   93:                 $ENV{$envname} = $envvalue;
   94:             }
   95:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
   96:             $ENV{'request.state'}    = "construct";
   97:             $ENV{'request.filename'} = $r->filename;
   98: 
   99:             unless (&constructaccess($requrl,$r->dir_config('lonDefDomain'))) {
  100:                 $r->log_reason("Unauthorized $requrl", $r->filename); 
  101: 	        return HTTP_NOT_ACCEPTABLE;
  102:             }
  103: 
  104: # -------------------------------------------------------- Load POST parameters
  105: 
  106: 
  107:         my $buffer;
  108: 
  109:         $r->read($buffer,$r->header_in('Content-length'));
  110: 
  111: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
  112:             my @pairs=split(/&/,$buffer);
  113:             my $pair;
  114:             foreach $pair (@pairs) {
  115:                my ($name,$value) = split(/=/,$pair);
  116:                $value =~ tr/+/ /;
  117:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  118:                $name  =~ tr/+/ /;
  119:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  120: 	       &Apache::loncommon::add_to_env("form.$name",$value);
  121:             } 
  122:         } else {
  123: 	    my $contentsep=$1;
  124:             my @lines = split (/\n/,$buffer);
  125:             my $name='';
  126:             my $value='';
  127:             my $fname='';
  128:             my $fmime='';
  129:             my $i;
  130:             for ($i=0;$i<=$#lines;$i++) {
  131: 		if ($lines[$i]=~/^$contentsep/) {
  132: 		    if ($name) {
  133:                         chomp($value);
  134: 			if ($fname) {
  135: 			    $ENV{"form.$name.filename"}=$fname;
  136:                             $ENV{"form.$name.mimetype"}=$fmime;
  137:                         } else {
  138:                             $value=~s/\s+$//s;
  139:                         }
  140: 			&Apache::loncommon::add_to_env("form.$name",$value);
  141:                     }
  142:                     if ($i<$#lines) {
  143: 			$i++;
  144:                         $lines[$i]=~
  145: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  146:                         $name=$1;
  147:                         $value='';
  148:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  149: 			   $fname=$1;
  150:                            if 
  151:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  152: 			      $fmime=$1;
  153:                               $i++;
  154: 			   } else {
  155:                               $fmime='';
  156:                            }
  157:                         } else {
  158: 			    $fname='';
  159:                             $fmime='';
  160:                         }
  161:                         $i++;
  162:                     }
  163:                 } else {
  164: 		    $value.=$lines[$i]."\n";
  165:                 }
  166:             }
  167: 	}
  168:             $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
  169:             $r->method_number(M_GET);
  170: 	    $r->method('GET');
  171:             $r->headers_in->unset('Content-length');
  172: 
  173:             return OK; 
  174:         } else { 
  175:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  176:         };
  177:     }
  178: 
  179: # ----------------------------------------------- Store where they wanted to go
  180: 
  181:     $ENV{'request.firsturl'}=$requrl;
  182:     return FORBIDDEN;
  183: }
  184: 
  185: 1;
  186: __END__
  187: 
  188: =head1 NAME
  189: 
  190: Apache::lonacc - Cookie Based Access Handler for Construction Area
  191: 
  192: =head1 SYNOPSIS
  193: 
  194: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
  195: 
  196:  PerlAccessHandler       Apache::loncacc
  197: 
  198: =head1 INTRODUCTION
  199: 
  200: This module enables cookie based authentication for construction area
  201: and is used to control access for three (essentially equivalent) URIs.
  202: 
  203:  <LocationMatch "^/priv.*">
  204:  <LocationMatch "^/\~.*">
  205:  <LocationMatch "^/\~.*/$">
  206: 
  207: Whenever the client sends the cookie back to the server, 
  208: if the cookie is missing or invalid, the user is re-challenged
  209: for login information.
  210: 
  211: This is part of the LearningOnline Network with CAPA project
  212: described at http://www.lon-capa.org.
  213: 
  214: =head1 HANDLER SUBROUTINE
  215: 
  216: This routine is called by Apache and mod_perl.
  217: 
  218: =over 4
  219: 
  220: =item *
  221: 
  222: load POST parameters
  223: 
  224: =item *
  225: 
  226: store where they wanted to go (first url entered)
  227: 
  228: =back
  229: 
  230: =head1 OTHERSUBROUTINES
  231: 
  232: =over 4
  233: 
  234: =item *
  235: 
  236: constructaccess($url,$ownerdomain) : See if the owner domain and name
  237: in the URL match those in the expected environment.  If so, return 
  238: two element list ($ownername,$ownerdomain).  Else, return null string.
  239: 
  240: =back
  241: 
  242: =cut
  243: 
  244: 
  245: 
  246: 
  247: 
  248: 
  249: 

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