File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.76: download - view: text, annotated - select for diffs
Thu Apr 13 19:07:33 2006 UTC (18 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- moving get_posted_cgi from loncommon to lonacc

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

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