File:  [LON-CAPA] / loncom / auth / loncacc.pm
Revision 1.27: download - view: text, annotated - select for diffs
Tue May 6 21:45:25 2003 UTC (21 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Perl 5.80 + mod_perl 2.0 + APache 2.0  really want the 0 in there

    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.27 2003/05/06 21:45:25 albertel 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: # YEAR=2002
   35: # 1/4 Gerd Kortemeyer
   36: ###
   37: 
   38: package Apache::loncacc;
   39: 
   40: use strict;
   41: use Apache::Constants qw(:common :http :methods REDIRECT);
   42: use Apache::File;
   43: use CGI::Cookie();
   44: use Fcntl qw(:flock);
   45: 
   46: sub constructaccess {
   47:     my ($url,$ownerdomain)=@_;
   48:     my ($ownername)=($url=~/\/(?:\~|priv\/|home\/)(\w+)/);
   49:     unless (($ownername) && ($ownerdomain)) { return ''; }
   50:     # We do not allow editing of previous versions of files.
   51:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
   52:     if (($ownername eq $ENV{'user.name'}) &&
   53:         ($ownerdomain eq $ENV{'user.domain'})) {
   54: 	return ($ownername,$ownerdomain);
   55:     }
   56: 
   57:     my $capriv='user.priv.ca./'.
   58:                $ownerdomain.'/'.$ownername.'./'.
   59: 	       $ownerdomain.'/'.$ownername;
   60:     foreach (keys %ENV) {
   61:         if ($_ eq $capriv) {
   62:            return ($ownername,$ownerdomain);
   63:         }
   64:     }
   65: 
   66:     return '';
   67: }
   68: 
   69: sub handler {
   70:     my $r = shift;
   71:     my $requrl=$r->uri;
   72:     $ENV{'request.editurl'}=$requrl;
   73:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   74:     my $lonid=$cookies{'lonID'};
   75:     my $cookie;
   76:     if ($lonid) {
   77: 	my $handle=$lonid->value;
   78:         $handle=~s/\W//g;
   79:         my $lonidsdir=$r->dir_config('lonIDsDir');
   80:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   81:             my @profile;
   82: 	    {
   83:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
   84:              flock($idf,LOCK_SH);
   85:              @profile=<$idf>;
   86:              $idf->close();
   87: 	    }
   88:             my $envi;
   89:             for ($envi=0;$envi<=$#profile;$envi++) {
   90: 		chomp($profile[$envi]);
   91: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
   92:                 $ENV{$envname} = $envvalue;
   93:             }
   94:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
   95:             $ENV{'request.state'}    = "construct";
   96:             $ENV{'request.filename'} = $r->filename;
   97: 
   98:             unless (&constructaccess($requrl,$r->dir_config('lonDefDomain'))) {
   99:                 $r->log_reason("Unauthorized $requrl", $r->filename); 
  100: 	        return HTTP_NOT_ACCEPTABLE;
  101:             }
  102: # Construction space needs Remote to work
  103:             if ($ENV{'environment.remote'} eq 'off') {
  104: 	        $r->content_type('text/html');
  105:                 $r->header_out(Location => 
  106:                     'http://'.$r->server->server_hostname.
  107:                     '/adm/remote?action=launch&url='.
  108:                     &Apache::lonnet::escape($requrl));
  109:                 return REDIRECT;
  110:             }
  111: 
  112: # -------------------------------------------------------- Load POST parameters
  113: 
  114: 
  115:         my $buffer;
  116: 
  117:         $r->read($buffer,$r->header_in('Content-length'),0);
  118: 
  119: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
  120:             my @pairs=split(/&/,$buffer);
  121:             my $pair;
  122:             foreach $pair (@pairs) {
  123:                my ($name,$value) = split(/=/,$pair);
  124:                $value =~ tr/+/ /;
  125:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  126:                $name  =~ tr/+/ /;
  127:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  128: 	       &Apache::loncommon::add_to_env("form.$name",$value);
  129:             } 
  130:         } else {
  131: 	    my $contentsep=$1;
  132:             my @lines = split (/\n/,$buffer);
  133:             my $name='';
  134:             my $value='';
  135:             my $fname='';
  136:             my $fmime='';
  137:             my $i;
  138:             for ($i=0;$i<=$#lines;$i++) {
  139: 		if ($lines[$i]=~/^$contentsep/) {
  140: 		    if ($name) {
  141:                         chomp($value);
  142: 			if ($fname) {
  143: 			    $ENV{"form.$name.filename"}=$fname;
  144:                             $ENV{"form.$name.mimetype"}=$fmime;
  145:                         } else {
  146:                             $value=~s/\s+$//s;
  147:                         }
  148: 			&Apache::loncommon::add_to_env("form.$name",$value);
  149:                     }
  150:                     if ($i<$#lines) {
  151: 			$i++;
  152:                         $lines[$i]=~
  153: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  154:                         $name=$1;
  155:                         $value='';
  156:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  157: 			   $fname=$1;
  158:                            if 
  159:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  160: 			      $fmime=$1;
  161:                               $i++;
  162: 			   } else {
  163:                               $fmime='';
  164:                            }
  165:                         } else {
  166: 			    $fname='';
  167:                             $fmime='';
  168:                         }
  169:                         $i++;
  170:                     }
  171:                 } else {
  172: 		    $value.=$lines[$i]."\n";
  173:                 }
  174:             }
  175: 	}
  176:             $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
  177:             $r->method_number(M_GET);
  178: 	    $r->method('GET');
  179:             $r->headers_in->unset('Content-length');
  180: 
  181:             return OK; 
  182:         } else { 
  183:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  184:         };
  185:     }
  186: 
  187: # ----------------------------------------------- Store where they wanted to go
  188: 
  189:     $ENV{'request.firsturl'}=$requrl;
  190:     return FORBIDDEN;
  191: }
  192: 
  193: 1;
  194: __END__
  195: 
  196: =head1 NAME
  197: 
  198: Apache::lonacc - Cookie Based Access Handler for Construction Area
  199: 
  200: =head1 SYNOPSIS
  201: 
  202: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
  203: 
  204:  PerlAccessHandler       Apache::loncacc
  205: 
  206: =head1 INTRODUCTION
  207: 
  208: This module enables cookie based authentication for construction area
  209: and is used to control access for three (essentially equivalent) URIs.
  210: 
  211:  <LocationMatch "^/priv.*">
  212:  <LocationMatch "^/\~.*">
  213:  <LocationMatch "^/\~.*/$">
  214: 
  215: Whenever the client sends the cookie back to the server, 
  216: if the cookie is missing or invalid, the user is re-challenged
  217: for login information.
  218: 
  219: This is part of the LearningOnline Network with CAPA project
  220: described at http://www.lon-capa.org.
  221: 
  222: =head1 HANDLER SUBROUTINE
  223: 
  224: This routine is called by Apache and mod_perl.
  225: 
  226: =over 4
  227: 
  228: =item *
  229: 
  230: load POST parameters
  231: 
  232: =item *
  233: 
  234: store where they wanted to go (first url entered)
  235: 
  236: =back
  237: 
  238: =head1 OTHERSUBROUTINES
  239: 
  240: =over 4
  241: 
  242: =item *
  243: 
  244: constructaccess($url,$ownerdomain) : See if the owner domain and name
  245: in the URL match those in the expected environment.  If so, return 
  246: two element list ($ownername,$ownerdomain).  Else, return null string.
  247: 
  248: =back
  249: 
  250: =cut
  251: 
  252: 
  253: 
  254: 
  255: 
  256: 
  257: 

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