File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.89: download - view: text, annotated - select for diffs
Fri Jul 21 18:52:32 2006 UTC (17 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- move portfolio access restriction checking code into lonnet.pm
- some changes to take care of public users access passphrase files

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.89 2006/07/21 18:52:32 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 Apache::restrictedaccess();
   39: use CGI::Cookie();
   40: use Fcntl qw(:flock);
   41: use LONCAPA;
   42: 
   43: sub cleanup {
   44:     my ($r)=@_;
   45:     if (! $r->is_initial_req()) { return DECLINED; }
   46:     &Apache::lonnet::save_cache();
   47:     return OK;
   48: }
   49: 
   50: sub goodbye {
   51:     my ($r)=@_;
   52:     &Apache::lonnet::goodbye();
   53:     return DONE;
   54: }
   55: 
   56: ###############################################
   57: 
   58: sub get_posted_cgi {
   59:     my ($r) = @_;
   60: 
   61:     my $buffer;
   62:     if ($r->header_in('Content-length')) {
   63: 	$r->read($buffer,$r->header_in('Content-length'),0);
   64:     }
   65:     unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
   66: 	my @pairs=split(/&/,$buffer);
   67: 	my $pair;
   68: 	foreach $pair (@pairs) {
   69: 	    my ($name,$value) = split(/=/,$pair);
   70: 	    $value =~ tr/+/ /;
   71: 	    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   72: 	    $name  =~ tr/+/ /;
   73: 	    $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
   74: 	    &Apache::loncommon::add_to_env("form.$name",$value);
   75: 	}
   76:     } else {
   77: 	my $contentsep=$1;
   78: 	my @lines = split (/\n/,$buffer);
   79: 	my $name='';
   80: 	my $value='';
   81: 	my $fname='';
   82: 	my $fmime='';
   83: 	my $i;
   84: 	for ($i=0;$i<=$#lines;$i++) {
   85: 	    if ($lines[$i]=~/^$contentsep/) {
   86: 		if ($name) {
   87: 		    chomp($value);
   88: 		    if ($fname) {
   89: 			$env{"form.$name.filename"}=$fname;
   90: 			$env{"form.$name.mimetype"}=$fmime;
   91: 		    } else {
   92: 			$value=~s/\s+$//s;
   93: 		    }
   94: 		    &Apache::loncommon::add_to_env("form.$name",$value);
   95: 		}
   96: 		if ($i<$#lines) {
   97: 		    $i++;
   98: 		    $lines[$i]=~
   99: 		/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  100: 		    $name=$1;
  101: 		    $value='';
  102: 		    if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  103: 			$fname=$1;
  104: 			if 
  105:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  106: 				$fmime=$1;
  107: 				$i++;
  108: 			    } else {
  109: 				$fmime='';
  110: 			    }
  111: 		    } else {
  112: 			$fname='';
  113: 			$fmime='';
  114: 		    }
  115: 		    $i++;
  116: 		}
  117: 	    } else {
  118: 		$value.=$lines[$i]."\n";
  119: 	    }
  120: 	}
  121:     }
  122: #
  123: # Digested POSTed values
  124: #
  125: # Remember the way this was originally done (GET or POST)
  126: #
  127:     $env{'request.method'}=$ENV{'REQUEST_METHOD'};
  128: #
  129: # There may also be stuff in the query string
  130: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
  131: # Also, unset POSTed content length to cover all tracks.
  132: #
  133: 
  134:     $r->method_number(M_GET);
  135: 
  136:     $r->method('GET');
  137:     $r->headers_in->unset('Content-length');
  138: }
  139: 
  140: sub passphrase_access_checker {
  141:     my ($r,$guestkey,$requrl) = @_;
  142:     my ($num,$scope,$end,$start) = ($guestkey =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
  143:     if ($scope eq 'guest') {
  144:         if (exists($env{'user.passphrase_access_'.$requrl})) {
  145:             if (($env{'user.passphrase_access_'.$requrl} == 0) || 
  146:                 ($env{'user.passphrase_access_'.$requrl} > time)) {
  147:                 $env{'request.publicaccess'} = 1;
  148:                 return 'ok'; 
  149:             }
  150:         }
  151:     }
  152:     $r->set_handlers('PerlHandler'=> \&Apache::restrictedaccess::handler);
  153:     $r->content_type('perl-script');
  154:     return;
  155: }
  156: 
  157: sub handler {
  158:     my $r = shift;
  159:     my $requrl=$r->uri;
  160:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  161:     my $lonid=$cookies{'lonID'};
  162:     my $cookie;
  163:     my $lonidsdir=$r->dir_config('lonIDsDir');
  164: 
  165:     my $handle;
  166:     if ($lonid) {
  167: 	$handle=$lonid->value;
  168:         $handle=~s/\W//g;
  169:     }
  170:       
  171:     my ($sso_login);
  172:     if ($r->user 
  173: 	&& (!$lonid || !-e "$lonidsdir/$handle.id" || $handle eq '') ) {
  174: 	$sso_login = 1;
  175: 	my $domain = $r->dir_config('lonDefDomain');
  176: 	my $home=&Apache::lonnet::homeserver($r->user,$domain);
  177: 	if ($home !~ /(con_lost|no_such_host)/) {
  178: 	    $handle=&Apache::lonauth::success($r,$r->user,$domain,
  179: 					     $home,'noredirect');
  180: 	    $r->header_out('Set-cookie',"lonID=$handle; path=/");
  181: 	}
  182:     }
  183: 
  184:     if ($sso_login) {
  185: 	&Apache::lonnet::appenv('request.sso.login' => 1);
  186:     }
  187: 
  188:     if ($r->dir_config("lonBalancer") eq 'yes') {
  189: 	$r->set_handlers('PerlResponseHandler'=>
  190: 			 [\&Apache::switchserver::handler]);
  191:     }
  192: 
  193:     if ($handle ne '') {
  194:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
  195: 
  196: # ------------------------------------------------------ Initialize Environment
  197: 
  198:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  199: 
  200: # --------------------------------------------------------- Initialize Language
  201: 
  202: 	    &Apache::lonlocal::get_language_handle($r);
  203: 
  204: # -------------------------------------------------------------- Resource State
  205: 
  206:             if ($requrl=~/^\/+(res|uploaded)\//) {
  207:                $env{'request.state'} = "published";
  208: 	    } else {
  209: 	       $env{'request.state'} = 'unknown';
  210:             }
  211:             $env{'request.filename'} = $r->filename;
  212:             $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
  213: # -------------------------------------------------------- Load POST parameters
  214: 
  215: 	    &Apache::lonacc::get_posted_cgi($r);
  216: 
  217: # ---------------------------------------------------------------- Check access
  218:             my $now = time;
  219: 	    if (&Apache::lonnet::is_portfolio_url($requrl)) {
  220: 		my $result = &Apache::lonnet::portfolio_access($r,$requrl);
  221: 		if ($result eq 'ok') { return OK; }
  222: 	    }
  223:             if ($requrl!~/^\/adm|public|prtspool\//) {
  224: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
  225:                 if ($access eq '1') {
  226: 		   $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  227: 	           return HTTP_NOT_ACCEPTABLE; 
  228:                 }
  229:                 if (($access ne '2') && ($access ne 'F')) {
  230: 		   $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  231: 	           return HTTP_NOT_ACCEPTABLE; 
  232:                 }
  233:             }
  234: 	    if ($requrl =~ m|^/prtspool/|) {
  235: 		my $start='/prtspool/'.$env{'user.name'}.'_'.
  236: 		    $env{'user.domain'};
  237: 		if ($requrl !~ /^\Q$start\E/) {
  238: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  239: 		    return HTTP_NOT_ACCEPTABLE;
  240: 		}
  241: 	    }
  242: 	    if ($env{'user.name'} eq 'public' && 
  243: 		$env{'user.domain'} eq 'public' &&
  244: 		$requrl !~ m{^/+(res|public)/} &&
  245: 		$requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
  246: 		$env{'request.querystring'}=$r->args;
  247: 		$env{'request.firsturl'}=$requrl;
  248: 		return FORBIDDEN;
  249: 	    }
  250: # ------------------------------------------------------------- This is allowed
  251:           if ($env{'request.course.id'}) {
  252: 	    &Apache::lonnet::countacc($requrl);
  253:             $requrl=~/\.(\w+)$/;
  254:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  255:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
  256:  ($requrl=~/^\/adm\/wrapper\//) ||
  257:  ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
  258:  ($requrl=~m|\.problem/smpedit$|) ||
  259:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
  260: # ------------------------------------- This is serious stuff, get symb and log
  261: 		my $query=$r->args;
  262:                 my $symb;
  263:                 if ($query) {
  264: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
  265:                 }
  266:                 if ($env{'form.symb'}) {
  267: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
  268:                     if ($requrl =~ m|^/adm/wrapper/|
  269: 			|| $requrl =~ m|^/adm/coursedocs/showdoc/|) {
  270:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  271:                         &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
  272: 						  'last_known' =>[$murl,$mid]);
  273:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
  274: 			     (($requrl=~m|(.*)/smpedit$|) &&
  275: 			      &Apache::lonnet::symbverify($symb,$1))) {
  276:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  277: 		      &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
  278: 						'last_known' =>[$murl,$mid]);
  279: 		    } else {
  280: 			$r->log_reason('Invalid symb for '.$requrl.': '.
  281:                                        $symb);
  282: 		        $env{'user.error.msg'}=
  283:                                 "$requrl:bre:1:1:Invalid Access";
  284:   	                return HTTP_NOT_ACCEPTABLE; 
  285:                     }
  286:                 } else {
  287: 	            $symb=&Apache::lonnet::symbread($requrl);
  288: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
  289: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
  290: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
  291: 		        $env{'user.error.msg'}=
  292:                                 "$requrl:bre:1:1:Invalid Access";
  293:   	                return HTTP_NOT_ACCEPTABLE; 
  294: 		    }
  295: 		    if ($symb) {
  296: 			my ($map,$mid,$murl)=
  297: 			    &Apache::lonnet::decode_symb($symb);
  298: 			&Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
  299: 						'last_known' =>[$murl,$mid]);
  300: 		    }
  301:                 }
  302:                 $env{'request.symb'}=$symb;
  303:                 &Apache::lonnet::courseacclog($symb);
  304:             } else {
  305: # ------------------------------------------------------- This is other content
  306:                 &Apache::lonnet::courseacclog($requrl);    
  307:             }
  308: 	  }
  309:             return OK; 
  310:         } else { 
  311:             $r->log_reason("Cookie $handle not valid", $r->filename); 
  312:         }
  313: 	}
  314: 
  315: # -------------------------------------------- See if this is a public resource
  316:     if ($requrl=~m|^/public/|
  317: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
  318:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
  319:         &Apache::lonlocal::get_language_handle($r);
  320: 	my $cookie=
  321: 	    &Apache::lonauth::success($r,'public','public','public');
  322:         my $lonidsdir=$r->dir_config('lonIDsDir');
  323: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
  324: 	&Apache::lonacc::get_posted_cgi($r);
  325:         $env{'request.state'} = "published";
  326:         $env{'request.publicaccess'} = 1;
  327:         $env{'request.filename'} = $r->filename;
  328: 
  329: 	$r->header_out('Set-cookie',"lonID=$cookie; path=/");
  330:         return OK;
  331:     }
  332:     if ($requrl=~m|^/+adm/+help/+|) {
  333:  	return OK;
  334:     }
  335: # ------------------------------------- See if this is a viewable portfolio file
  336:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
  337: 	my $result = &Apache::lonnet::portfolio_access($r,$requrl);
  338: 	if ($result eq 'ok' ) { return OK; }
  339:     }
  340: 
  341: # -------------------------------------------------------------- Not authorized
  342:     $requrl=~/\.(\w+)$/;
  343: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  344: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
  345: #        ($requrl=~m|^/prtspool/|)) {
  346: # -------------------------- Store where they wanted to go and get login screen
  347: 	$env{'request.querystring'}=$r->args;
  348: 	$env{'request.firsturl'}=$requrl;
  349:        return FORBIDDEN;
  350: #   } else {
  351: # --------------------------------------------------------------------- Goodbye
  352: #       return HTTP_BAD_REQUEST;
  353: #   }
  354: }
  355: 
  356: 1;
  357: __END__
  358: 
  359: =head1 NAME
  360: 
  361: Apache::lonacc - Cookie Based Access Handler
  362: 
  363: =head1 SYNOPSIS
  364: 
  365: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  366: 
  367:  PerlAccessHandler       Apache::lonacc
  368: 
  369: =head1 INTRODUCTION
  370: 
  371: This module enables cookie based authentication and is used
  372: to control access for many different LON-CAPA URIs.
  373: 
  374: Whenever the client sends the cookie back to the server, 
  375: this cookie is handled by either lonacc.pm or loncacc.pm
  376: (see srm.conf for what is invoked when).  If
  377: the cookie is missing or invalid, the user is re-challenged
  378: for login information.
  379: 
  380: This is part of the LearningOnline Network with CAPA project
  381: described at http://www.lon-capa.org.
  382: 
  383: =head1 HANDLER SUBROUTINE
  384: 
  385: This routine is called by Apache and mod_perl.
  386: 
  387: =over 4
  388: 
  389: =item *
  390: 
  391: transfer profile into environment
  392: 
  393: =item *
  394: 
  395: load POST parameters
  396: 
  397: =item *
  398: 
  399: check access
  400: 
  401: =item *
  402: 
  403: if allowed, get symb, log, generate course statistics if applicable
  404: 
  405: =item *
  406: 
  407: otherwise return error
  408: 
  409: =item *
  410: 
  411: see if public resource
  412: 
  413: =item *
  414: 
  415: store attempted access
  416: 
  417: =back
  418: 
  419: =cut

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