File:  [LON-CAPA] / loncom / auth / loncacc.pm
Revision 1.54: download - view: text, annotated - select for diffs
Fri Oct 21 16:03:11 2011 UTC (12 years, 7 months ago) by www
Branches: MAIN
CVS tags: HEAD
Saving my work on Bug #1320.

    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.54 2011/10/21 16:03:11 www 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: 
   30: =pod
   31: 
   32: =head1 NAME
   33: 
   34: Apache::lonacc - Cookie Based Access Handler for Construction Area
   35: 
   36: =head1 SYNOPSIS
   37: 
   38: Invoked (for various locations) by /etc/httpd/conf/loncapa_apache.conf:
   39: 
   40:  PerlAccessHandler       Apache::loncacc
   41: 
   42: =head1 INTRODUCTION
   43: 
   44: This module enables cookie based authentication for construction area
   45: and is used to control access for three (essentially equivalent) URIs.
   46: 
   47:  <LocationMatch "^/priv.*">
   48:  <LocationMatch "^/\~.*">
   49:  <LocationMatch "^/\~.*/$">
   50: 
   51: Whenever the client sends the cookie back to the server, 
   52: if the cookie is missing or invalid, the user is re-challenged
   53: for login information.
   54: 
   55: This is part of the LearningOnline Network with CAPA project
   56: described at http://www.lon-capa.org.
   57: 
   58: =head1 HANDLER SUBROUTINE
   59: 
   60: This routine is called by Apache and mod_perl.
   61: 
   62: =over 4
   63: 
   64: =item *
   65: 
   66: load POST parameters
   67: 
   68: =item *
   69: 
   70: store where they wanted to go (first url entered)
   71: 
   72: =back
   73: 
   74: =head1 OTHERSUBROUTINES
   75: 
   76: =over
   77: 
   78: =item constructaccess($url,$ownerdomain)
   79: 
   80: See if the owner domain and name
   81: in the URL match those in the expected environment.  If so, return 
   82: two element list ($ownername,$ownerdomain).  Else, return null string.
   83: 
   84: =back
   85: 
   86: =cut
   87: 
   88: 
   89: package Apache::loncacc;
   90: 
   91: use strict;
   92: use Apache::Constants qw(:common :http :methods REDIRECT);
   93: use Fcntl qw(:flock);
   94: use Apache::lonlocal;
   95: use Apache::lonnet;
   96: use Apache::lonacc;
   97: use LONCAPA qw(:DEFAULT :match);
   98: 
   99: sub constructaccess {
  100:     my ($url,$setpriv)=@_;
  101: 
  102: # We do not allow editing of previous versions of files
  103:     if ($url=~/\.(\d+)\.(\w+)$/) { return ''; }
  104: 
  105: # Get username and domain from URL
  106:     my ($ownerdomain,$ownername)=($url=~/^\/priv\/($match_domain)\/($match_username)\//);
  107: 
  108: # The URL does not really point to any authorspace, forget it
  109:     unless (($ownername) && ($ownerdomain)) { return ''; }
  110:   
  111: # Now we need to see if the user has access to the authorspace of
  112: # $ownername at $ownerdomain
  113: 
  114:     if (($ownername eq $env{'user.name'}) && ($ownerdomain eq $env{'user.domain'})) {
  115: # Real author for this?
  116:        if (exists($env{'user.priv.au./'.$ownerdomain.'/./'})) {
  117:           return ($ownername,$ownerdomain);
  118:        }
  119:     } else {
  120: # Co-author for this?
  121: 	if (exists($env{'user.priv.ca./'.$ownerdomain.'/'.$ownername.'./'}) ||
  122: 	    exists($env{'user.priv.aa./'.$ownerdomain.'/'.$ownername.'./'}) ) {
  123: 	    return ($ownername,$ownerdomain);
  124: 	}
  125:     }
  126: # We don't have any access right now. If we are not possibly going to do anything about this,
  127: # we might as well leave
  128:    unless ($setpriv) { return ''; }
  129: 
  130: # Backdoor access?
  131:     my $allowed=&Apache::lonnet::allowed('eco',$ownerdomain);
  132: # Nope
  133:     unless ($allowed) { return ''; }
  134: # Looks like we may have access, but could be locked by the owner of the construction space
  135:     if ($allowed eq 'U') {
  136:         my %blocked=&Apache::lonnet::get('environment',['domcoord.author'],
  137:                                          $ownerdomain,$ownername);
  138: # Is blocked by owner
  139:         if ($blocked{'domcoord.author'} eq 'blocked') { return ''; }
  140:     }
  141:     if (($allowed eq 'F') || ($allowed eq 'U')) {
  142: # Grant temporary access
  143:         my $then=$env{'user.login.time'};
  144:         my $update==$env{'user.update.time'};
  145:         if (!$update) { $update = $then; }
  146:         my $refresh=$env{'user.refresh.time'};
  147:         if (!$refresh) { $refresh = $update; }
  148:         my $now = time;
  149:         &Apache::lonnet::check_adhoc_privs($ownerdomain,$ownername,
  150:                                            $update,$refresh,$now,'ca',
  151:                                            'constructaccess');
  152:         return($ownername,$ownerdomain);
  153:     }
  154: # No business here
  155:     return '';
  156: }
  157: 
  158: sub handler {
  159:     my $r = shift;
  160:     my $requrl=$r->uri;
  161:     $env{'request.editurl'}=$requrl;
  162: 
  163:     my $handle =  &Apache::lonnet::check_for_valid_session($r);
  164:     if ($handle ne '') {
  165: 
  166: # ------------------------------------------------------ Initialize Environment
  167:         my $lonidsdir=$r->dir_config('lonIDsDir');
  168: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  169: 
  170: # --------------------------------------------------------- Initialize Language
  171:  
  172: 	&Apache::lonlocal::get_language_handle($r);
  173: 
  174: # -------------------------------------------------------------- Resource State
  175: 
  176: 	$env{'request.state'}    = "construct";
  177: 	$env{'request.filename'} = $r->filename;
  178: 
  179: 	unless (&constructaccess($requrl,'setpriv')) {
  180: 	    $r->log_reason("Unauthorized $requrl", $r->filename); 
  181: 	    return HTTP_NOT_ACCEPTABLE;
  182: 	}
  183: 
  184: # -------------------------------------------------------- Load POST parameters
  185: 
  186: 	&Apache::lonacc::get_posted_cgi($r);
  187: 
  188: 	return OK; 
  189:     } else { 
  190: 	$r->log_reason("Cookie $handle not valid", $r->filename) 
  191:     }
  192: 
  193: # ----------------------------------------------- Store where they wanted to go
  194: 
  195:     $env{'request.firsturl'}=$requrl;
  196:     return FORBIDDEN;
  197: }
  198: 
  199: 1;
  200: __END__
  201: 
  202: 
  203: 
  204: 
  205: 
  206: 
  207: 
  208: 

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