File:  [LON-CAPA] / loncom / auth / loncacc.pm
Revision 1.35: download - view: text, annotated - select for diffs
Mon Aug 23 18:57:16 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- don't tell student 'Go Away' when an html page is closed
- allow CC to see closed content
   (BUG#3357)

    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.35 2004/08/23 18:57:16 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: use Apache::lonlocal;
   46: use Apache::lonnet();
   47: 
   48: 
   49: sub constructaccess {
   50:     my ($url,$ownerdomain)=@_;
   51:     my ($ownername)=($url=~/\/(?:\~|priv\/|home\/)(\w+)\//);
   52:     unless (($ownername) && ($ownerdomain)) { return ''; }
   53:     # We do not allow editing of previous versions of files.
   54:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
   55:     my @possibledomains = &Apache::lonnet::current_machine_domains();
   56:     if ($ownername eq $ENV{'user.name'}) {
   57: 	foreach my $domain (@possibledomains) {
   58: 	    if ($domain eq $ENV{'user.domain'}) {
   59: 		return ($ownername,$domain);
   60: 	    }
   61: 	}
   62:     }
   63:     
   64:     foreach my $domain (@possibledomains) {
   65: 	my $capriv='user.priv.ca./'.$domain.'/'.$ownername.'./';
   66: 	foreach (keys %ENV) {
   67: 	    if ($_ eq $capriv) {
   68: 		return ($ownername,$domain);
   69: 	    }
   70: 	}
   71:     }
   72:     return '';
   73: }
   74: 
   75: sub handler {
   76:     my $r = shift;
   77:     my $requrl=$r->uri;
   78:     $ENV{'request.editurl'}=$requrl;
   79:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   80:     my $lonid=$cookies{'lonID'};
   81:     my $cookie;
   82:     &Apache::lonnet::logthis("Wha? cookie");
   83:     if ($lonid) {
   84: 	&Apache::lonnet::logthis("Valid cookie");
   85: 	my $handle=$lonid->value;
   86:         $handle=~s/\W//g;
   87:         my $lonidsdir=$r->dir_config('lonIDsDir');
   88:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   89: 
   90: # ------------------------------------------------------ Initialize Environment
   91: 
   92:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   93: 
   94: # --------------------------------------------------------- Initialize Language
   95:  
   96:  	    &Apache::lonlocal::get_language_handle($r);
   97: 
   98: # -------------------------------------------------------------- Resource State
   99: 
  100:             $ENV{'request.state'}    = "construct";
  101:             $ENV{'request.filename'} = $r->filename;
  102: 
  103:             unless (&constructaccess($requrl,$r->dir_config('lonDefDomain'))) {
  104:                 $r->log_reason("Unauthorized $requrl", $r->filename); 
  105: 	        return HTTP_NOT_ACCEPTABLE;
  106:             }
  107: # Construction space needs Remote to work
  108:             if ($ENV{'environment.remote'} eq 'off') {
  109: 	        $r->content_type('text/html');
  110:                 $r->header_out(Location => 
  111:                     'http://'.$r->server->server_hostname.
  112:                     '/adm/remote?action=launch&url='.
  113:                     &Apache::lonnet::escape($requrl));
  114:                 return REDIRECT;
  115:             }
  116: 
  117: # -------------------------------------------------------- Load POST parameters
  118: 
  119: 	    &Apache::loncommon::get_posted_cgi($r);
  120: 
  121:             return OK; 
  122:         } else { 
  123: 	    &Apache::lonnet::logthis("Invalid cookie");
  124:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  125:         };
  126:     }
  127: 
  128: # ----------------------------------------------- Store where they wanted to go
  129: 
  130:     $ENV{'request.firsturl'}=$requrl;
  131:     return FORBIDDEN;
  132: }
  133: 
  134: 1;
  135: __END__
  136: 
  137: =head1 NAME
  138: 
  139: Apache::lonacc - Cookie Based Access Handler for Construction Area
  140: 
  141: =head1 SYNOPSIS
  142: 
  143: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
  144: 
  145:  PerlAccessHandler       Apache::loncacc
  146: 
  147: =head1 INTRODUCTION
  148: 
  149: This module enables cookie based authentication for construction area
  150: and is used to control access for three (essentially equivalent) URIs.
  151: 
  152:  <LocationMatch "^/priv.*">
  153:  <LocationMatch "^/\~.*">
  154:  <LocationMatch "^/\~.*/$">
  155: 
  156: Whenever the client sends the cookie back to the server, 
  157: if the cookie is missing or invalid, the user is re-challenged
  158: for login information.
  159: 
  160: This is part of the LearningOnline Network with CAPA project
  161: described at http://www.lon-capa.org.
  162: 
  163: =head1 HANDLER SUBROUTINE
  164: 
  165: This routine is called by Apache and mod_perl.
  166: 
  167: =over 4
  168: 
  169: =item *
  170: 
  171: load POST parameters
  172: 
  173: =item *
  174: 
  175: store where they wanted to go (first url entered)
  176: 
  177: =back
  178: 
  179: =head1 OTHERSUBROUTINES
  180: 
  181: =over 4
  182: 
  183: =item *
  184: 
  185: constructaccess($url,$ownerdomain) : See if the owner domain and name
  186: in the URL match those in the expected environment.  If so, return 
  187: two element list ($ownername,$ownerdomain).  Else, return null string.
  188: 
  189: =back
  190: 
  191: =cut
  192: 
  193: 
  194: 
  195: 
  196: 
  197: 
  198: 

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