File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.90: download - view: text, annotated - select for diffs
Fri Jul 21 19:42:12 2006 UTC (17 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_99_2, HEAD
- lonnet::allowed() can now return a 'A' for 'pass phrase authentication needed'
- lonacc redirects to restricted access if A is returned

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.90 2006/07/21 19:42:12 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 handler {
  141:     my $r = shift;
  142:     my $requrl=$r->uri;
  143:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
  144:     my $lonid=$cookies{'lonID'};
  145:     my $cookie;
  146:     my $lonidsdir=$r->dir_config('lonIDsDir');
  147: 
  148:     my $handle;
  149:     if ($lonid) {
  150: 	$handle=$lonid->value;
  151:         $handle=~s/\W//g;
  152:     }
  153:       
  154:     my ($sso_login);
  155:     if ($r->user 
  156: 	&& (!$lonid || !-e "$lonidsdir/$handle.id" || $handle eq '') ) {
  157: 	$sso_login = 1;
  158: 	my $domain = $r->dir_config('lonDefDomain');
  159: 	my $home=&Apache::lonnet::homeserver($r->user,$domain);
  160: 	if ($home !~ /(con_lost|no_such_host)/) {
  161: 	    $handle=&Apache::lonauth::success($r,$r->user,$domain,
  162: 					     $home,'noredirect');
  163: 	    $r->header_out('Set-cookie',"lonID=$handle; path=/");
  164: 	}
  165:     }
  166: 
  167:     if ($sso_login) {
  168: 	&Apache::lonnet::appenv('request.sso.login' => 1);
  169:     }
  170: 
  171:     if ($r->dir_config("lonBalancer") eq 'yes') {
  172: 	$r->set_handlers('PerlResponseHandler'=>
  173: 			 [\&Apache::switchserver::handler]);
  174:     }
  175: 
  176:     if ($handle ne '') {
  177:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
  178: 
  179: # ------------------------------------------------------ Initialize Environment
  180: 
  181:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  182: 
  183: # --------------------------------------------------------- Initialize Language
  184: 
  185: 	    &Apache::lonlocal::get_language_handle($r);
  186: 
  187: # -------------------------------------------------------------- Resource State
  188: 
  189:             if ($requrl=~/^\/+(res|uploaded)\//) {
  190:                $env{'request.state'} = "published";
  191: 	    } else {
  192: 	       $env{'request.state'} = 'unknown';
  193:             }
  194:             $env{'request.filename'} = $r->filename;
  195:             $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
  196: # -------------------------------------------------------- Load POST parameters
  197: 
  198: 	    &Apache::lonacc::get_posted_cgi($r);
  199: 
  200: # ---------------------------------------------------------------- Check access
  201:             my $now = time;
  202:             if ($requrl!~/^\/adm|public|prtspool\//) {
  203: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
  204:                 if ($access eq '1') {
  205: 		   $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  206: 	           return HTTP_NOT_ACCEPTABLE; 
  207:                 }
  208: 		if ($access eq 'A') {
  209: 		    &Apache::restrictedaccess::setup_handler($r);
  210: 		    return OK;
  211:                 }
  212:                 if (($access ne '2') && ($access ne 'F')) {
  213: 		   $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  214: 	           return HTTP_NOT_ACCEPTABLE; 
  215:                 }
  216:             }
  217: 	    if ($requrl =~ m|^/prtspool/|) {
  218: 		my $start='/prtspool/'.$env{'user.name'}.'_'.
  219: 		    $env{'user.domain'};
  220: 		if ($requrl !~ /^\Q$start\E/) {
  221: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  222: 		    return HTTP_NOT_ACCEPTABLE;
  223: 		}
  224: 	    }
  225: 	    if ($env{'user.name'} eq 'public' && 
  226: 		$env{'user.domain'} eq 'public' &&
  227: 		$requrl !~ m{^/+(res|public|uploaded)/} &&
  228: 		$requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
  229: 		$env{'request.querystring'}=$r->args;
  230: 		$env{'request.firsturl'}=$requrl;
  231: 		return FORBIDDEN;
  232: 	    }
  233: # ------------------------------------------------------------- This is allowed
  234:           if ($env{'request.course.id'}) {
  235: 	    &Apache::lonnet::countacc($requrl);
  236:             $requrl=~/\.(\w+)$/;
  237:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  238:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
  239:  ($requrl=~/^\/adm\/wrapper\//) ||
  240:  ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
  241:  ($requrl=~m|\.problem/smpedit$|) ||
  242:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
  243: # ------------------------------------- This is serious stuff, get symb and log
  244: 		my $query=$r->args;
  245:                 my $symb;
  246:                 if ($query) {
  247: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
  248:                 }
  249:                 if ($env{'form.symb'}) {
  250: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
  251:                     if ($requrl =~ m|^/adm/wrapper/|
  252: 			|| $requrl =~ m|^/adm/coursedocs/showdoc/|) {
  253:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  254:                         &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
  255: 						  'last_known' =>[$murl,$mid]);
  256:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
  257: 			     (($requrl=~m|(.*)/smpedit$|) &&
  258: 			      &Apache::lonnet::symbverify($symb,$1))) {
  259:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
  260: 		      &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
  261: 						'last_known' =>[$murl,$mid]);
  262: 		    } else {
  263: 			$r->log_reason('Invalid symb for '.$requrl.': '.
  264:                                        $symb);
  265: 		        $env{'user.error.msg'}=
  266:                                 "$requrl:bre:1:1:Invalid Access";
  267:   	                return HTTP_NOT_ACCEPTABLE; 
  268:                     }
  269:                 } else {
  270: 	            $symb=&Apache::lonnet::symbread($requrl);
  271: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
  272: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
  273: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
  274: 		        $env{'user.error.msg'}=
  275:                                 "$requrl:bre:1:1:Invalid Access";
  276:   	                return HTTP_NOT_ACCEPTABLE; 
  277: 		    }
  278: 		    if ($symb) {
  279: 			my ($map,$mid,$murl)=
  280: 			    &Apache::lonnet::decode_symb($symb);
  281: 			&Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
  282: 						'last_known' =>[$murl,$mid]);
  283: 		    }
  284:                 }
  285:                 $env{'request.symb'}=$symb;
  286:                 &Apache::lonnet::courseacclog($symb);
  287:             } else {
  288: # ------------------------------------------------------- This is other content
  289:                 &Apache::lonnet::courseacclog($requrl);    
  290:             }
  291: 	  }
  292:             return OK; 
  293:         } else { 
  294:             $r->log_reason("Cookie $handle not valid", $r->filename); 
  295:         }
  296: 	}
  297: 
  298: # -------------------------------------------- See if this is a public resource
  299:     if ($requrl=~m|^/public/|
  300: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
  301:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
  302:         &Apache::lonlocal::get_language_handle($r);
  303: 	my $cookie=
  304: 	    &Apache::lonauth::success($r,'public','public','public');
  305:         my $lonidsdir=$r->dir_config('lonIDsDir');
  306: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
  307: 	&Apache::lonacc::get_posted_cgi($r);
  308:         $env{'request.state'} = "published";
  309:         $env{'request.publicaccess'} = 1;
  310:         $env{'request.filename'} = $r->filename;
  311: 
  312: 	$r->header_out('Set-cookie',"lonID=$cookie; path=/");
  313:         return OK;
  314:     }
  315:     if ($requrl=~m|^/+adm/+help/+|) {
  316:  	return OK;
  317:     }
  318: # ------------------------------------ See if this is a viewable portfolio file
  319:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
  320: 	my $access=&Apache::lonnet::allowed('bre',$requrl);
  321: 	if ($access eq 'A') {
  322: 	    &Apache::restrictedaccess::setup_handler($r);
  323: 	    return OK;
  324: 	}
  325: 	if (($access ne '2') && ($access ne 'F')) {
  326: 	    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  327: 	    return HTTP_NOT_ACCEPTABLE;
  328: 	}
  329:     }
  330: 
  331: # -------------------------------------------------------------- Not authorized
  332:     $requrl=~/\.(\w+)$/;
  333: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  334: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
  335: #        ($requrl=~m|^/prtspool/|)) {
  336: # -------------------------- Store where they wanted to go and get login screen
  337: 	$env{'request.querystring'}=$r->args;
  338: 	$env{'request.firsturl'}=$requrl;
  339:        return FORBIDDEN;
  340: #   } else {
  341: # --------------------------------------------------------------------- Goodbye
  342: #       return HTTP_BAD_REQUEST;
  343: #   }
  344: }
  345: 
  346: 1;
  347: __END__
  348: 
  349: =head1 NAME
  350: 
  351: Apache::lonacc - Cookie Based Access Handler
  352: 
  353: =head1 SYNOPSIS
  354: 
  355: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  356: 
  357:  PerlAccessHandler       Apache::lonacc
  358: 
  359: =head1 INTRODUCTION
  360: 
  361: This module enables cookie based authentication and is used
  362: to control access for many different LON-CAPA URIs.
  363: 
  364: Whenever the client sends the cookie back to the server, 
  365: this cookie is handled by either lonacc.pm or loncacc.pm
  366: (see srm.conf for what is invoked when).  If
  367: the cookie is missing or invalid, the user is re-challenged
  368: for login information.
  369: 
  370: This is part of the LearningOnline Network with CAPA project
  371: described at http://www.lon-capa.org.
  372: 
  373: =head1 HANDLER SUBROUTINE
  374: 
  375: This routine is called by Apache and mod_perl.
  376: 
  377: =over 4
  378: 
  379: =item *
  380: 
  381: transfer profile into environment
  382: 
  383: =item *
  384: 
  385: load POST parameters
  386: 
  387: =item *
  388: 
  389: check access
  390: 
  391: =item *
  392: 
  393: if allowed, get symb, log, generate course statistics if applicable
  394: 
  395: =item *
  396: 
  397: otherwise return error
  398: 
  399: =item *
  400: 
  401: see if public resource
  402: 
  403: =item *
  404: 
  405: store attempted access
  406: 
  407: =back
  408: 
  409: =cut

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