File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.54: download - view: text, annotated - select for diffs
Fri Dec 17 19:43:19 2004 UTC (19 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove log spew

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.54 2004/12/17 19:43:19 albertel Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # YEAR=1999
   29: # 5/21/99,5/22,5/29,5/31,6/15,16/11,22/11,
   30: # YEAR=2000
   31: # 01/06,01/13,05/31,06/01,09/06,09/25,09/28,10/30,11/6,
   32: # 12/25,12/26,
   33: # YEAR=2001
   34: # 01/06/01,05/28,8/11,9/26,11/29 Gerd Kortemeyer
   35: # YEAR=2002
   36: # 1/4,2/25 Gerd Kortemeyer
   37: #
   38: ###
   39: 
   40: package Apache::lonacc;
   41: 
   42: use strict;
   43: use Apache::Constants qw(:common :http :methods);
   44: use Apache::File;
   45: use Apache::lonnet;
   46: use Apache::loncommon();
   47: use Apache::lonlocal;
   48: use CGI::Cookie();
   49: use Fcntl qw(:flock);
   50: 
   51: sub handler {
   52:     my $r = shift;
   53:     my $requrl=$r->uri;
   54:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
   55:     my $lonid=$cookies{'lonID'};
   56:     my $cookie;
   57:     if ($lonid) {
   58: 	my $handle=$lonid->value;
   59:         $handle=~s/\W//g;
   60:         my $lonidsdir=$r->dir_config('lonIDsDir');
   61:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   62: 
   63: # ------------------------------------------------------ Initialize Environment
   64: 
   65:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   66: 
   67: # --------------------------------------------------------- Initialize Language
   68: 
   69: 	    &Apache::lonlocal::get_language_handle($r);
   70: 
   71: # -------------------------------------------------------------- Resource State
   72: 
   73:             if ($requrl=~/^\/+(res|uploaded)\//) {
   74:                $ENV{'request.state'} = "published";
   75: 	    } else {
   76: 	       $ENV{'request.state'} = 'unknown';
   77:             }
   78:             $ENV{'request.filename'} = $r->filename;
   79:             $ENV{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
   80: # -------------------------------------------------------- Load POST parameters
   81: 
   82: 	    &Apache::loncommon::get_posted_cgi($r);
   83: 
   84: # ---------------------------------------------------------------- Check access
   85: 
   86:             if ($requrl!~/^\/adm|public|prtspool\//) {
   87: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
   88:                 if ($access eq '1') {
   89: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
   90: 	           return HTTP_NOT_ACCEPTABLE; 
   91:                 }
   92:                 if (($access ne '2') && ($access ne 'F')) {
   93: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
   94: 	           return HTTP_NOT_ACCEPTABLE; 
   95:                 }
   96:             }
   97: 	    if ($requrl =~ m|^/prtspool/|) {
   98: 		my $start='/prtspool/'.$ENV{'user.name'}.'_'.
   99: 		    $ENV{'user.domain'};
  100: 		if ($requrl !~ /^\Q$start\E/) {
  101: 		    $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  102: 		    return HTTP_NOT_ACCEPTABLE;
  103: 		}
  104: 	    }
  105: # ------------------------------------------------------------- This is allowed
  106:           if ($ENV{'request.course.id'}) {
  107: 	    &Apache::lonnet::countacc($requrl);
  108:             $requrl=~/\.(\w+)$/;
  109:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  110:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
  111:  ($requrl=~/^\/adm\/wrapper\//) ||
  112:  ($requrl=~m|\.problem/smpedit$|) ||
  113:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
  114: # ------------------------------------- This is serious stuff, get symb and log
  115: 		my $query=$r->args;
  116:                 my $symb;
  117:                 if ($query) {
  118: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
  119:                 }
  120:                 if ($ENV{'form.symb'}) {
  121: 		    $symb=&Apache::lonnet::symbclean($ENV{'form.symb'});
  122:                     if ($requrl =~ m|^/adm/wrapper/|) {
  123:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  124:                         &Apache::lonnet::symblist($map,$murl => $mid,
  125:                                                'last_known' => $murl);
  126:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
  127: 			     (($requrl=~m|(.*)/smpedit$|) &&
  128: 			      &Apache::lonnet::symbverify($symb,$1))) {
  129:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  130:                       &Apache::lonnet::symblist($map,$murl => $mid,
  131:                                                'last_known' => $murl);
  132: 		    } else {
  133: 			$r->log_reason('Invalid symb for '.$requrl.': '.
  134:                                        $symb);
  135: 		        $ENV{'user.error.msg'}=
  136:                                 "$requrl:bre:1:1:Invalid Access";
  137:   	                return HTTP_NOT_ACCEPTABLE; 
  138:                     }
  139:                 } else {
  140: 	            $symb=&Apache::lonnet::symbread($requrl);
  141:                     my ($map,$mid,$murl)=split(/\_\_\_/,$symb);
  142: 		    &Apache::lonnet::symblist($map,$murl => $mid,
  143:                                               'last_known' => $murl);
  144:                 }
  145:                 $ENV{'request.symb'}=$symb;
  146:                 &Apache::lonnet::courseacclog($symb);
  147:             } else {
  148: # ------------------------------------------------------- This is other content
  149:                 &Apache::lonnet::courseacclog($requrl);    
  150:             }
  151: 	  }
  152:             return OK; 
  153:         } else { 
  154:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  155:         };
  156:     }
  157: 
  158: # -------------------------------------------- See if this is a public resource
  159:     if ($requrl=~m|^/public/|
  160: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
  161:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
  162: 	my $buffer;
  163: 	$r->read($buffer,$r->header_in('Content-length'),0);
  164: 	&Apache::loncommon::get_unprocessed_cgi($buffer);
  165: 	$ENV{'user.name'}='public';
  166:         $ENV{'user.domain'}='public';
  167:         $ENV{'request.state'} = "published";
  168:         $ENV{'request.publicaccess'} = 1;
  169:         $ENV{'request.filename'} = $r->filename;
  170:         return OK;
  171:     }
  172: # -------------------------------------------------------------- Not authorized
  173:     $requrl=~/\.(\w+)$/;
  174:     if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  175:         ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
  176:         ($requrl=~m|^/prtspool/|)) {
  177: # -------------------------- Store where they wanted to go and get login screen
  178: 	$ENV{'request.querystring'}=$r->args;
  179: 	$ENV{'request.firsturl'}=$requrl;
  180:        return FORBIDDEN;
  181:    } else {
  182: # --------------------------------------------------------------------- Goodbye
  183:        return HTTP_BAD_REQUEST;
  184:    }
  185: }
  186: 
  187: 1;
  188: __END__
  189: 
  190: =head1 NAME
  191: 
  192: Apache::lonacc - Cookie Based Access Handler
  193: 
  194: =head1 SYNOPSIS
  195: 
  196: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  197: 
  198:  PerlAccessHandler       Apache::lonacc
  199: 
  200: =head1 INTRODUCTION
  201: 
  202: This module enables cookie based authentication and is used
  203: to control access for many different LON-CAPA URIs.
  204: 
  205: Whenever the client sends the cookie back to the server, 
  206: this cookie is handled by either lonacc.pm or loncacc.pm
  207: (see srm.conf for what is invoked when).  If
  208: 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: transfer profile into environment
  223: 
  224: =item *
  225: 
  226: load POST parameters
  227: 
  228: =item *
  229: 
  230: check access
  231: 
  232: =item *
  233: 
  234: if allowed, get symb, log, generate course statistics if applicable
  235: 
  236: =item *
  237: 
  238: otherwise return error
  239: 
  240: =item *
  241: 
  242: see if public resource
  243: 
  244: =item *
  245: 
  246: store attempted access
  247: 
  248: =back
  249: 
  250: =cut

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