File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.75: download - view: text, annotated - select for diffs
Fri Apr 7 22:42:00 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- moving hadnler routines out of lonnet into lonacc

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

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