File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.95: download - view: text, annotated - select for diffs
Wed Aug 30 21:48:51 2006 UTC (17 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4986, smppg weren't listing to conditions

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

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