File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.37: download - view: text, annotated - select for diffs
Wed Nov 6 22:43:27 2002 UTC (21 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
- fixes BUG#300
- now log all print accesses
- now force restrictions on acces to files in prtspool

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.37 2002/11/06 22:43:27 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: # 12/15 Scott Harrison
   36: # YEAR=2002
   37: # 1/4,2/25 Gerd Kortemeyer
   38: #
   39: ###
   40: 
   41: package Apache::lonacc;
   42: 
   43: use strict;
   44: use Apache::Constants qw(:common :http :methods);
   45: use Apache::File;
   46: use Apache::lonnet;
   47: use Apache::loncommon();
   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: # ------------------------------------------- Transfer profile into environment
   64: 
   65:             my @profile;
   66: 	    {
   67:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
   68:              flock($idf,LOCK_SH);
   69:              @profile=<$idf>;
   70:              $idf->close();
   71: 	    }
   72:             my $envi;
   73:             for ($envi=0;$envi<=$#profile;$envi++) {
   74: 		chomp($profile[$envi]);
   75: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
   76:                 $ENV{$envname} = $envvalue;
   77:             }
   78:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
   79:             if ($requrl=~/^\/res\//) {
   80:                $ENV{'request.state'} = "published";
   81: 	    } else {
   82: 	       $ENV{'request.state'} = 'unknown';
   83:             }
   84:             $ENV{'request.filename'} = $r->filename;
   85: 
   86: # -------------------------------------------------------- Load POST parameters
   87: 
   88: 
   89:            
   90:         my $buffer;
   91: 
   92:         $r->read($buffer,$r->header_in('Content-length'));
   93: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
   94:             my @pairs=split(/&/,$buffer);
   95:             my $pair;
   96:             foreach $pair (@pairs) {
   97:                my ($name,$value) = split(/=/,$pair);
   98:                $value =~ tr/+/ /;
   99:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  100:                $name  =~ tr/+/ /;
  101:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  102: 	       &Apache::loncommon::add_to_env("form.$name",$value);
  103:             }
  104:         } else {
  105: 	    my $contentsep=$1;
  106:             my @lines = split (/\n/,$buffer);
  107:             my $name='';
  108:             my $value='';
  109:             my $fname='';
  110:             my $fmime='';
  111:             my $i;
  112:             for ($i=0;$i<=$#lines;$i++) {
  113: 		if ($lines[$i]=~/^$contentsep/) {
  114: 		    if ($name) {
  115:                         chomp($value);
  116: 			if ($fname) {
  117: 			    $ENV{"form.$name.filename"}=$fname;
  118:                             $ENV{"form.$name.mimetype"}=$fmime;
  119:                         } else {
  120:                             $value=~s/\s+$//s;
  121:                         }
  122: 			&Apache::loncommon::add_to_env("form.$name",$value);
  123:                     }
  124:                     if ($i<$#lines) {
  125: 			$i++;
  126:                         $lines[$i]=~
  127: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  128:                         $name=$1;
  129:                         $value='';
  130:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  131: 			   $fname=$1;
  132:                            if 
  133:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  134: 			      $fmime=$1;
  135:                               $i++;
  136: 			   } else {
  137:                               $fmime='';
  138:                            }
  139:                         } else {
  140: 			    $fname='';
  141:                             $fmime='';
  142:                         }
  143:                         $i++;
  144:                     }
  145:                 } else {
  146: 		    $value.=$lines[$i]."\n";
  147:                 }
  148:             }
  149: 	}
  150: 	    $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
  151:             $r->method_number(M_GET);
  152: 	    $r->method('GET');
  153:             $r->headers_in->unset('Content-length');
  154: 
  155: # ---------------------------------------------------------------- Check access
  156: 
  157:             if ($requrl!~/^\/adm|public|prtspool\//) {
  158: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
  159:                 if ($access eq '1') {
  160: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  161: 	           return HTTP_NOT_ACCEPTABLE; 
  162:                 }
  163:                 if (($access ne '2') && ($access ne 'F')) {
  164: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  165: 	           return HTTP_NOT_ACCEPTABLE; 
  166:                 }
  167:             }
  168: 	    if ($requrl =~ m|^/prtspool/|) {
  169: 		my $start='/prtspool/'.$ENV{'user.name'}.'_'.
  170: 		    $ENV{'user.domain'};
  171: 		if ($requrl !~ /^\Q$start\E/) {
  172: 		    $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  173: 		    return HTTP_NOT_ACCEPTABLE;
  174: 		}
  175: 	    }
  176: # ------------------------------------------------------------- This is allowed
  177:           if ($ENV{'request.course.id'}) {
  178: 	    &Apache::lonnet::countacc($requrl);
  179:             $requrl=~/\.(\w+)$/;
  180:             if (&Apache::loncommon::fileembstyle($1) eq 'ssi') {
  181: # ------------------------------------- This is serious stuff, get symb and log
  182: 		my $query=$r->args;
  183:                 my $symb;
  184:                 if ($query) {
  185: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
  186:                 }
  187:                 if ($ENV{'form.symb'}) {
  188: 		    $symb=&Apache::lonnet::symbclean($ENV{'form.symb'});
  189:                     if (&Apache::lonnet::symbverify($symb,$requrl)) {
  190:                       my ($map,$mid,$murl)=split(/\_\_\_/,$symb);
  191:                       &Apache::lonnet::symblist($map,$murl => $mid,
  192:                                                'last_known' => $murl);
  193: 		    } else {
  194: 			$r->log_reason('Invalid symb for '.$requrl.': '.
  195:                                        $symb);
  196: 		        $ENV{'user.error.msg'}=
  197:                                 "$requrl:bre:1:1:Invalid Access";
  198:   	                return HTTP_NOT_ACCEPTABLE; 
  199:                     }
  200:                 } else {
  201: 	            $symb=&Apache::lonnet::symbread;
  202:                 }
  203:                 $ENV{'request.symb'}=$symb;
  204:                 &Apache::lonnet::courseacclog($symb);
  205:             } else {
  206: # ------------------------------------------------------- This is other content
  207:                 &Apache::lonnet::courseacclog($requrl);    
  208:             }
  209: 	  }
  210:             return OK; 
  211:         } else { 
  212:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  213:         };
  214:     }
  215: 
  216: # -------------------------------------------- See if this is a public resource
  217:     if ($requrl=~m|^/public/|
  218: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
  219:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
  220: 	$ENV{'user.name'}='public';
  221:         $ENV{'user.domain'}='public';
  222:         $ENV{'request.state'} = "published";
  223:         $ENV{'request.publicaccess'} = 1;
  224:         $ENV{'request.filename'} = $r->filename;
  225:         return OK;
  226:     }
  227: # -------------------------------------------------------------- Not authorized
  228:     $requrl=~/\.(\w+)$/;
  229:     if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  230:         ($requrl=~/^\/adm\/(roles|logout)/) ||
  231:         ($requrl=~m|^/prtspool/|)) {
  232: # -------------------------- Store where they wanted to go and get login screen
  233:        $ENV{'request.firsturl'}=$requrl;
  234:        return FORBIDDEN;
  235:    } else {
  236: # --------------------------------------------------------------------- Goodbye
  237:        return HTTP_BAD_REQUEST;
  238:    }
  239: }
  240: 
  241: 1;
  242: __END__
  243: 
  244: =head1 NAME
  245: 
  246: Apache::lonacc - Cookie Based Access Handler
  247: 
  248: =head1 SYNOPSIS
  249: 
  250: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  251: 
  252:  PerlAccessHandler       Apache::lonacc
  253: 
  254: =head1 INTRODUCTION
  255: 
  256: This module enables cookie based authentication and is used
  257: to control access for many different LON-CAPA URIs.
  258: 
  259: Whenever the client sends the cookie back to the server, 
  260: this cookie is handled by either lonacc.pm or loncacc.pm
  261: (see srm.conf for what is invoked when).  If
  262: the cookie is missing or invalid, the user is re-challenged
  263: for login information.
  264: 
  265: This is part of the LearningOnline Network with CAPA project
  266: described at http://www.lon-capa.org.
  267: 
  268: =head1 HANDLER SUBROUTINE
  269: 
  270: This routine is called by Apache and mod_perl.
  271: 
  272: =over 4
  273: 
  274: =item *
  275: 
  276: transfer profile into environment
  277: 
  278: =item *
  279: 
  280: load POST parameters
  281: 
  282: =item *
  283: 
  284: check access
  285: 
  286: =item *
  287: 
  288: if allowed, get symb, log, generate course statistics if applicable
  289: 
  290: =item *
  291: 
  292: otherwise return error
  293: 
  294: =item *
  295: 
  296: see if public resource
  297: 
  298: =item *
  299: 
  300: store attempted access
  301: 
  302: =back
  303: 
  304: =cut

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