Annotation of loncom/auth/loncacc.pm, revision 1.24

1.1       albertel    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)
1.20      www         4: #
1.24    ! matthew     5: # $Id: loncacc.pm,v 1.23 2002/09/30 20:35:38 albertel Exp $
1.20      www         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
1.4       www        30: # 6/15,16/11,22/11,
1.20      www        31: # YEAR=2001
1.15      www        32: # 01/06,01/11,6/1,9/25,9/28,11/22,12/25,12/26,
1.16      www        33: # 01/06/01,05/04,05/05,05/09 Gerd Kortemeyer
1.20      www        34: # 12/21 Scott Harrison
                     35: # YEAR=2002
                     36: # 1/4 Gerd Kortemeyer
                     37: ###
1.1       albertel   38: 
                     39: package Apache::loncacc;
                     40: 
                     41: use strict;
1.8       www        42: use Apache::Constants qw(:common :http :methods);
1.7       www        43: use Apache::File;
1.1       albertel   44: use CGI::Cookie();
1.14      www        45: use Fcntl qw(:flock);
1.1       albertel   46: 
1.15      www        47: sub constructaccess {
                     48:     my ($url,$ownerdomain)=@_;
1.16      www        49:     my ($ownername)=($url=~/\/(?:\~|priv\/|home\/)(\w+)/);
1.15      www        50:     unless (($ownername) && ($ownerdomain)) { return ''; }
1.24    ! matthew    51:     # We do not allow editing of previous versions of files.
        !            52:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
1.15      www        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;
1.20      www        61:     foreach (keys %ENV) {
1.15      www        62:         if ($_ eq $capriv) {
                     63:            return ($ownername,$ownerdomain);
                     64:         }
1.20      www        65:     }
1.15      www        66: 
                     67:     return '';
                     68: }
                     69: 
1.1       albertel   70: sub handler {
                     71:     my $r = shift;
                     72:     my $requrl=$r->uri;
1.22      www        73:     $ENV{'request.editurl'}=$requrl;
1.1       albertel   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 '')) {
1.2       www        82:             my @profile;
                     83: 	    {
                     84:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
1.14      www        85:              flock($idf,LOCK_SH);
1.2       www        86:              @profile=<$idf>;
1.14      www        87:              $idf->close();
1.2       www        88: 	    }
                     89:             my $envi;
                     90:             for ($envi=0;$envi<=$#profile;$envi++) {
                     91: 		chomp($profile[$envi]);
                     92: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
1.9       www        93:                 $ENV{$envname} = $envvalue;
1.2       www        94:             }
1.9       www        95:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
                     96:             $ENV{'request.state'}    = "construct";
                     97:             $ENV{'request.filename'} = $r->filename;
1.15      www        98: 
                     99:             unless (&constructaccess($requrl,$r->dir_config('lonDefDomain'))) {
                    100:                 $r->log_reason("Unauthorized $requrl", $r->filename); 
                    101: 	        return HTTP_NOT_ACCEPTABLE;
                    102:             }
1.9       www       103: 
                    104: # -------------------------------------------------------- Load POST parameters
                    105: 
                    106: 
1.12      www       107:         my $buffer;
1.8       www       108: 
1.12      www       109:         $r->read($buffer,$r->header_in('Content-length'));
                    110: 
                    111: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
1.5       www       112:             my @pairs=split(/&/,$buffer);
1.9       www       113:             my $pair;
1.5       www       114:             foreach $pair (@pairs) {
1.9       www       115:                my ($name,$value) = split(/=/,$pair);
1.6       www       116:                $value =~ tr/+/ /;
                    117:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.10      www       118:                $name  =~ tr/+/ /;
                    119:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.21      albertel  120: 	       &Apache::loncommon::add_to_env("form.$name",$value);
1.5       www       121:             } 
1.12      www       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;
1.13      www       137:                         } else {
                    138:                             $value=~s/\s+$//s;
1.12      www       139:                         }
1.21      albertel  140: 			&Apache::loncommon::add_to_env("form.$name",$value);
1.12      www       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: 	}
1.20      www       168:             $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
1.8       www       169:             $r->method_number(M_GET);
                    170: 	    $r->method('GET');
                    171:             $r->headers_in->unset('Content-length');
                    172: 
1.1       albertel  173:             return OK; 
                    174:         } else { 
                    175:             $r->log_reason("Cookie $handle not valid", $r->filename) 
                    176:         };
                    177:     }
1.6       www       178: 
                    179: # ----------------------------------------------- Store where they wanted to go
                    180: 
                    181:     $ENV{'request.firsturl'}=$requrl;
1.1       albertel  182:     return FORBIDDEN;
                    183: }
                    184: 
                    185: 1;
                    186: __END__
                    187: 
1.20      www       188: =head1 NAME
                    189: 
                    190: Apache::lonacc - Cookie Based Access Handler for Construction Area
                    191: 
                    192: =head1 SYNOPSIS
                    193: 
1.23      albertel  194: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
1.20      www       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
1.1       albertel  243: 
                    244: 
                    245: 
                    246: 
                    247: 
                    248: 
                    249: 

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