File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.67: download - view: text, annotated - select for diffs
Thu Jul 7 05:53:35 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- restrict public users to /res/* and /adm/roles, /adm/logout, and /adm/randomlabel.png

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

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