File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.101: download - view: text, annotated - select for diffs
Tue Sep 19 20:36:27 2006 UTC (17 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_2_2_X, version_2_2_2, HEAD
- need to undef the The Response phase handler since we have already taken care of the needed output

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

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