File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.60.2.1: download - view: text, annotated - select for diffs
Mon Feb 21 18:22:04 2005 UTC (19 years, 2 months ago) by albertel
Branches: version_1_3_2_nsdl
Diff to branchpoint 1.60: preferred, unified
- nsdl branch needs and fixes

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.60.2.1 2005/02/21 18:22:04 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:     if ($r->header_in('User-Agent')=~/NSDL\_Search\_Bot/) {
   56:        return OK;
   57:     }
   58:     my $lonid=$cookies{'lonID'};
   59:     my $cookie;
   60:     if ($lonid) {
   61: 	my $handle=$lonid->value;
   62:         $handle=~s/\W//g;
   63:         my $lonidsdir=$r->dir_config('lonIDsDir');
   64:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
   65: 
   66: # ------------------------------------------------------ Initialize Environment
   67: 
   68:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
   69: 
   70: # --------------------------------------------------------- Initialize Language
   71: 
   72: 	    &Apache::lonlocal::get_language_handle($r);
   73: 
   74: # -------------------------------------------------------------- Resource State
   75: 
   76:             if ($requrl=~/^\/+(res|uploaded)\//) {
   77:                $ENV{'request.state'} = "published";
   78: 	    } else {
   79: 	       $ENV{'request.state'} = 'unknown';
   80:             }
   81:             $ENV{'request.filename'} = $r->filename;
   82:             $ENV{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
   83: # -------------------------------------------------------- Load POST parameters
   84: 
   85: 	    &Apache::loncommon::get_posted_cgi($r);
   86: 
   87: # ---------------------------------------------------------------- Check access
   88: 
   89:             if ($requrl!~/^\/adm|public|prtspool\//) {
   90: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
   91:                 if ($access eq '1') {
   92: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
   93: 	           return HTTP_NOT_ACCEPTABLE; 
   94:                 }
   95:                 if (($access ne '2') && ($access ne 'F')) {
   96: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
   97: 	           return HTTP_NOT_ACCEPTABLE; 
   98:                 }
   99:             }
  100: 	    if ($requrl =~ m|^/prtspool/|) {
  101: 		my $start='/prtspool/'.$ENV{'user.name'}.'_'.
  102: 		    $ENV{'user.domain'};
  103: 		if ($requrl !~ /^\Q$start\E/) {
  104: 		    $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  105: 		    return HTTP_NOT_ACCEPTABLE;
  106: 		}
  107: 	    }
  108: # ------------------------------------------------------------- This is allowed
  109:           if ($ENV{'request.course.id'}) {
  110: 	    &Apache::lonnet::countacc($requrl);
  111:             $requrl=~/\.(\w+)$/;
  112:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  113:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
  114:  ($requrl=~/^\/adm\/wrapper\//) ||
  115:  ($requrl=~m|\.problem/smpedit$|) ||
  116:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
  117: # ------------------------------------- This is serious stuff, get symb and log
  118: 		my $query=$r->args;
  119:                 my $symb;
  120:                 if ($query) {
  121: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
  122:                 }
  123:                 if ($ENV{'form.symb'}) {
  124: 		    $symb=&Apache::lonnet::symbclean($ENV{'form.symb'});
  125:                     if ($requrl =~ m|^/adm/wrapper/|) {
  126:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  127:                         &Apache::lonnet::symblist($map,$murl => $mid,
  128:                                                'last_known' => $murl);
  129:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
  130: 			     (($requrl=~m|(.*)/smpedit$|) &&
  131: 			      &Apache::lonnet::symbverify($symb,$1))) {
  132:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  133:                       &Apache::lonnet::symblist($map,$murl => $mid,
  134:                                                'last_known' => $murl);
  135: 		    } else {
  136: 			$r->log_reason('Invalid symb for '.$requrl.': '.
  137:                                        $symb);
  138: 		        $ENV{'user.error.msg'}=
  139:                                 "$requrl:bre:1:1:Invalid Access";
  140:   	                return HTTP_NOT_ACCEPTABLE; 
  141:                     }
  142:                 } else {
  143: 	            $symb=&Apache::lonnet::symbread($requrl);
  144: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
  145: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
  146: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
  147: 		        $ENV{'user.error.msg'}=
  148:                                 "$requrl:bre:1:1:Invalid Access";
  149:   	                return HTTP_NOT_ACCEPTABLE; 
  150: 		    }
  151:                     my ($map,$mid,$murl)=split(/\_\_\_/,$symb);
  152: 		    &Apache::lonnet::symblist($map,$murl => $mid,
  153:                                               'last_known' => $murl);
  154:                 }
  155:                 $ENV{'request.symb'}=$symb;
  156:                 &Apache::lonnet::courseacclog($symb);
  157:             } else {
  158: # ------------------------------------------------------- This is other content
  159:                 &Apache::lonnet::courseacclog($requrl);    
  160:             }
  161: 	  }
  162:             return OK; 
  163:         } else { 
  164:             $r->log_reason("Cookie $handle not valid", $r->filename) 
  165:         };
  166:     }
  167: 
  168: # -------------------------------------------- See if this is a public resource
  169:     if ($requrl=~m|^/public/|
  170: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
  171:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
  172: 	&Apache::loncommon::get_posted_cgi($r);
  173: 	$ENV{'user.name'}='public';
  174:         $ENV{'user.domain'}='public';
  175:         $ENV{'request.state'} = "published";
  176:         $ENV{'request.publicaccess'} = 1;
  177:         $ENV{'request.filename'} = $r->filename;
  178: 	my ($httpbrowser,  $clientbrowser, $clientversion,
  179: 	    $clientmathml, $clientunicode, $clientos) =
  180: 		&Apache::loncommon::decode_user_agent($r);
  181: 	$ENV{'browser.type'}=$clientbrowser;
  182:         $ENV{'browser.version'}=$clientversion;
  183:         $ENV{'browser.mathml'}=$clientmathml;
  184:         $ENV{'browser.unicode'}=$clientunicode;
  185:         $ENV{'browser.os'}=$clientos;
  186: 
  187:         return OK;
  188:     }
  189: # -------------------------------------------------------------- Not authorized
  190:     $requrl=~/\.(\w+)$/;
  191: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  192: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
  193: #        ($requrl=~m|^/prtspool/|)) {
  194: # -------------------------- Store where they wanted to go and get login screen
  195: 	$ENV{'request.querystring'}=$r->args;
  196: 	$ENV{'request.firsturl'}=$requrl;
  197:        return FORBIDDEN;
  198: #   } else {
  199: # --------------------------------------------------------------------- Goodbye
  200: #       return HTTP_BAD_REQUEST;
  201: #   }
  202: }
  203: 
  204: 1;
  205: __END__
  206: 
  207: =head1 NAME
  208: 
  209: Apache::lonacc - Cookie Based Access Handler
  210: 
  211: =head1 SYNOPSIS
  212: 
  213: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  214: 
  215:  PerlAccessHandler       Apache::lonacc
  216: 
  217: =head1 INTRODUCTION
  218: 
  219: This module enables cookie based authentication and is used
  220: to control access for many different LON-CAPA URIs.
  221: 
  222: Whenever the client sends the cookie back to the server, 
  223: this cookie is handled by either lonacc.pm or loncacc.pm
  224: (see srm.conf for what is invoked when).  If
  225: the cookie is missing or invalid, the user is re-challenged
  226: for login information.
  227: 
  228: This is part of the LearningOnline Network with CAPA project
  229: described at http://www.lon-capa.org.
  230: 
  231: =head1 HANDLER SUBROUTINE
  232: 
  233: This routine is called by Apache and mod_perl.
  234: 
  235: =over 4
  236: 
  237: =item *
  238: 
  239: transfer profile into environment
  240: 
  241: =item *
  242: 
  243: load POST parameters
  244: 
  245: =item *
  246: 
  247: check access
  248: 
  249: =item *
  250: 
  251: if allowed, get symb, log, generate course statistics if applicable
  252: 
  253: =item *
  254: 
  255: otherwise return error
  256: 
  257: =item *
  258: 
  259: see if public resource
  260: 
  261: =item *
  262: 
  263: store attempted access
  264: 
  265: =back
  266: 
  267: =cut

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