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

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.42    ! albertel    5: # $Id: loncacc.pm,v 1.41 2006/04/13 19:07:33 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: #
1.1       albertel   29: 
                     30: package Apache::loncacc;
                     31: 
                     32: use strict;
1.26      www        33: use Apache::Constants qw(:common :http :methods REDIRECT);
1.1       albertel   34: use CGI::Cookie();
1.14      www        35: use Fcntl qw(:flock);
1.30      www        36: use Apache::lonlocal;
1.38      albertel   37: use Apache::lonnet;
1.41      albertel   38: use Apache::lonacc;
1.1       albertel   39: 
1.15      www        40: sub constructaccess {
                     41:     my ($url,$ownerdomain)=@_;
1.32      www        42:     my ($ownername)=($url=~/\/(?:\~|priv\/|home\/)(\w+)\//);
1.15      www        43:     unless (($ownername) && ($ownerdomain)) { return ''; }
1.24      matthew    44:     # We do not allow editing of previous versions of files.
                     45:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
1.33      albertel   46:     my @possibledomains = &Apache::lonnet::current_machine_domains();
1.38      albertel   47:     if ($ownername eq $env{'user.name'}) {
1.33      albertel   48: 	foreach my $domain (@possibledomains) {
1.38      albertel   49: 	    if ($domain eq $env{'user.domain'}) {
1.33      albertel   50: 		return ($ownername,$domain);
                     51: 	    }
                     52: 	}
1.15      www        53:     }
1.33      albertel   54:     
                     55:     foreach my $domain (@possibledomains) {
1.40      albertel   56: 	if (exists($env{'user.priv.ca./'.$domain.'/'.$ownername.'./'}) ||
                     57: 	    exists($env{'user.priv.aa./'.$domain.'/'.$ownername.'./'}) ) {
                     58: 	    return ($ownername,$domain);
1.39      www        59: 	}
1.20      www        60:     }
1.15      www        61:     return '';
                     62: }
                     63: 
1.1       albertel   64: sub handler {
                     65:     my $r = shift;
                     66:     my $requrl=$r->uri;
1.38      albertel   67:     $env{'request.editurl'}=$requrl;
1.1       albertel   68:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     69:     my $lonid=$cookies{'lonID'};
                     70:     my $cookie;
                     71:     if ($lonid) {
1.42    ! albertel   72:       	my $handle=&LONCAPA::clean_handle($lonid->value);
1.1       albertel   73:         my $lonidsdir=$r->dir_config('lonIDsDir');
                     74:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.28      www        75: 
                     76: # ------------------------------------------------------ Initialize Environment
                     77: 
                     78:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.30      www        79: 
                     80: # --------------------------------------------------------- Initialize Language
                     81:  
1.31      www        82:  	    &Apache::lonlocal::get_language_handle($r);
1.28      www        83: 
                     84: # -------------------------------------------------------------- Resource State
                     85: 
1.38      albertel   86:             $env{'request.state'}    = "construct";
                     87:             $env{'request.filename'} = $r->filename;
1.15      www        88: 
                     89:             unless (&constructaccess($requrl,$r->dir_config('lonDefDomain'))) {
                     90:                 $r->log_reason("Unauthorized $requrl", $r->filename); 
                     91: 	        return HTTP_NOT_ACCEPTABLE;
1.26      www        92:             }
1.9       www        93: 
                     94: # -------------------------------------------------------- Load POST parameters
                     95: 
1.41      albertel   96: 	    &Apache::lonacc::get_posted_cgi($r);
1.8       www        97: 
1.1       albertel   98:             return OK; 
                     99:         } else { 
                    100:             $r->log_reason("Cookie $handle not valid", $r->filename) 
                    101:         };
                    102:     }
1.6       www       103: 
                    104: # ----------------------------------------------- Store where they wanted to go
                    105: 
1.38      albertel  106:     $env{'request.firsturl'}=$requrl;
1.1       albertel  107:     return FORBIDDEN;
                    108: }
                    109: 
                    110: 1;
                    111: __END__
                    112: 
1.20      www       113: =head1 NAME
                    114: 
                    115: Apache::lonacc - Cookie Based Access Handler for Construction Area
                    116: 
                    117: =head1 SYNOPSIS
                    118: 
1.23      albertel  119: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
1.20      www       120: 
                    121:  PerlAccessHandler       Apache::loncacc
                    122: 
                    123: =head1 INTRODUCTION
                    124: 
                    125: This module enables cookie based authentication for construction area
                    126: and is used to control access for three (essentially equivalent) URIs.
                    127: 
                    128:  <LocationMatch "^/priv.*">
                    129:  <LocationMatch "^/\~.*">
                    130:  <LocationMatch "^/\~.*/$">
                    131: 
                    132: Whenever the client sends the cookie back to the server, 
                    133: if the cookie is missing or invalid, the user is re-challenged
                    134: for login information.
                    135: 
                    136: This is part of the LearningOnline Network with CAPA project
                    137: described at http://www.lon-capa.org.
                    138: 
                    139: =head1 HANDLER SUBROUTINE
                    140: 
                    141: This routine is called by Apache and mod_perl.
                    142: 
                    143: =over 4
                    144: 
                    145: =item *
                    146: 
                    147: load POST parameters
                    148: 
                    149: =item *
                    150: 
                    151: store where they wanted to go (first url entered)
                    152: 
                    153: =back
                    154: 
                    155: =head1 OTHERSUBROUTINES
                    156: 
                    157: =over 4
                    158: 
                    159: =item *
                    160: 
                    161: constructaccess($url,$ownerdomain) : See if the owner domain and name
                    162: in the URL match those in the expected environment.  If so, return 
                    163: two element list ($ownername,$ownerdomain).  Else, return null string.
                    164: 
                    165: =back
                    166: 
                    167: =cut
1.1       albertel  168: 
                    169: 
                    170: 
                    171: 
                    172: 
                    173: 
                    174: 

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