File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.74: download - view: text, annotated - select for diffs
Wed Apr 5 22:39:55 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start_page

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

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