File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.45: download - view: text, annotated - select for diffs
Tue May 6 21:45:25 2003 UTC (21 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Perl 5.80 + mod_perl 2.0 + APache 2.0  really want the 0 in there

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

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