File:  [LON-CAPA] / loncom / auth / loncacc.pm
Revision 1.23: download - view: text, annotated - select for diffs
Mon Sep 30 20:35:38 2002 UTC (21 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- the secret to winning the commit war is many small silly fixes

    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.23 2002/09/30 20:35:38 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: # 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: 
   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: 
  103: # -------------------------------------------------------- Load POST parameters
  104: 
  105: 
  106:         my $buffer;
  107: 
  108:         $r->read($buffer,$r->header_in('Content-length'));
  109: 
  110: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
  111:             my @pairs=split(/&/,$buffer);
  112:             my $pair;
  113:             foreach $pair (@pairs) {
  114:                my ($name,$value) = split(/=/,$pair);
  115:                $value =~ tr/+/ /;
  116:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  117:                $name  =~ tr/+/ /;
  118:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  119: 	       &Apache::loncommon::add_to_env("form.$name",$value);
  120:             } 
  121:         } else {
  122: 	    my $contentsep=$1;
  123:             my @lines = split (/\n/,$buffer);
  124:             my $name='';
  125:             my $value='';
  126:             my $fname='';
  127:             my $fmime='';
  128:             my $i;
  129:             for ($i=0;$i<=$#lines;$i++) {
  130: 		if ($lines[$i]=~/^$contentsep/) {
  131: 		    if ($name) {
  132:                         chomp($value);
  133: 			if ($fname) {
  134: 			    $ENV{"form.$name.filename"}=$fname;
  135:                             $ENV{"form.$name.mimetype"}=$fmime;
  136:                         } else {
  137:                             $value=~s/\s+$//s;
  138:                         }
  139: 			&Apache::loncommon::add_to_env("form.$name",$value);
  140:                     }
  141:                     if ($i<$#lines) {
  142: 			$i++;
  143:                         $lines[$i]=~
  144: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  145:                         $name=$1;
  146:                         $value='';
  147:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  148: 			   $fname=$1;
  149:                            if 
  150:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  151: 			      $fmime=$1;
  152:                               $i++;
  153: 			   } else {
  154:                               $fmime='';
  155:                            }
  156:                         } else {
  157: 			    $fname='';
  158:                             $fmime='';
  159:                         }
  160:                         $i++;
  161:                     }
  162:                 } else {
  163: 		    $value.=$lines[$i]."\n";
  164:                 }
  165:             }
  166: 	}
  167:             $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
  168:             $r->method_number(M_GET);
  169: 	    $r->method('GET');
  170:             $r->headers_in->unset('Content-length');
  171: 
  172:             return OK; 
  173:         } else { 
  174:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  175:         };
  176:     }
  177: 
  178: # ----------------------------------------------- Store where they wanted to go
  179: 
  180:     $ENV{'request.firsturl'}=$requrl;
  181:     return FORBIDDEN;
  182: }
  183: 
  184: 1;
  185: __END__
  186: 
  187: =head1 NAME
  188: 
  189: Apache::lonacc - Cookie Based Access Handler for Construction Area
  190: 
  191: =head1 SYNOPSIS
  192: 
  193: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
  194: 
  195:  PerlAccessHandler       Apache::loncacc
  196: 
  197: =head1 INTRODUCTION
  198: 
  199: This module enables cookie based authentication for construction area
  200: and is used to control access for three (essentially equivalent) URIs.
  201: 
  202:  <LocationMatch "^/priv.*">
  203:  <LocationMatch "^/\~.*">
  204:  <LocationMatch "^/\~.*/$">
  205: 
  206: Whenever the client sends the cookie back to the server, 
  207: if the cookie is missing or invalid, the user is re-challenged
  208: for login information.
  209: 
  210: This is part of the LearningOnline Network with CAPA project
  211: described at http://www.lon-capa.org.
  212: 
  213: =head1 HANDLER SUBROUTINE
  214: 
  215: This routine is called by Apache and mod_perl.
  216: 
  217: =over 4
  218: 
  219: =item *
  220: 
  221: load POST parameters
  222: 
  223: =item *
  224: 
  225: store where they wanted to go (first url entered)
  226: 
  227: =back
  228: 
  229: =head1 OTHERSUBROUTINES
  230: 
  231: =over 4
  232: 
  233: =item *
  234: 
  235: constructaccess($url,$ownerdomain) : See if the owner domain and name
  236: in the URL match those in the expected environment.  If so, return 
  237: two element list ($ownername,$ownerdomain).  Else, return null string.
  238: 
  239: =back
  240: 
  241: =cut
  242: 
  243: 
  244: 
  245: 
  246: 
  247: 
  248: 

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