version 1.40, 2003/04/02 14:57:32
|
version 1.114, 2008/03/08 02:45:14
|
Line 25
|
Line 25
|
# |
# |
# http://www.lon-capa.org/ |
# http://www.lon-capa.org/ |
# |
# |
# YEAR=1999 |
|
# 5/21/99,5/22,5/29,5/31,6/15,16/11,22/11, |
|
# YEAR=2000 |
|
# 01/06,01/13,05/31,06/01,09/06,09/25,09/28,10/30,11/6, |
|
# 12/25,12/26, |
|
# YEAR=2001 |
|
# 01/06/01,05/28,8/11,9/26,11/29 Gerd Kortemeyer |
|
# YEAR=2002 |
|
# 1/4,2/25 Gerd Kortemeyer |
|
# |
|
### |
### |
|
|
package Apache::lonacc; |
package Apache::lonacc; |
Line 44 use Apache::Constants qw(:common :http :
|
Line 34 use Apache::Constants qw(:common :http :
|
use Apache::File; |
use Apache::File; |
use Apache::lonnet; |
use Apache::lonnet; |
use Apache::loncommon(); |
use Apache::loncommon(); |
use CGI::Cookie(); |
use Apache::lonlocal; |
|
use Apache::restrictedaccess(); |
|
use Apache::blockedaccess(); |
use Fcntl qw(:flock); |
use Fcntl qw(:flock); |
|
use LONCAPA; |
|
|
sub handler { |
sub cleanup { |
my $r = shift; |
my ($r)=@_; |
my $requrl=$r->uri; |
if (! $r->is_initial_req()) { return DECLINED; } |
my %cookies=CGI::Cookie->parse($r->header_in('Cookie')); |
&Apache::lonnet::save_cache(); |
my $lonid=$cookies{'lonID'}; |
&Apache::lontexconvert::jsMath_reset(); |
my $cookie; |
return OK; |
if ($lonid) { |
} |
my $handle=$lonid->value; |
|
$handle=~s/\W//g; |
|
my $lonidsdir=$r->dir_config('lonIDsDir'); |
|
if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) { |
|
|
|
# ------------------------------------------- Transfer profile into environment |
|
|
|
my @profile; |
|
{ |
|
my $idf=Apache::File->new("$lonidsdir/$handle.id"); |
|
flock($idf,LOCK_SH); |
|
@profile=<$idf>; |
|
$idf->close(); |
|
} |
|
my $envi; |
|
for ($envi=0;$envi<=$#profile;$envi++) { |
|
chomp($profile[$envi]); |
|
my ($envname,$envvalue)=split(/=/,$profile[$envi]); |
|
$ENV{$envname} = $envvalue; |
|
} |
|
$ENV{'user.environment'} = "$lonidsdir/$handle.id"; |
|
if ($requrl=~/^\/res\//) { |
|
$ENV{'request.state'} = "published"; |
|
} else { |
|
$ENV{'request.state'} = 'unknown'; |
|
} |
|
$ENV{'request.filename'} = $r->filename; |
|
|
|
# -------------------------------------------------------- Load POST parameters |
sub goodbye { |
|
my ($r)=@_; |
|
&Apache::lonnet::goodbye(); |
|
return DONE; |
|
} |
|
|
|
############################################### |
|
|
|
sub get_posted_cgi { |
my $buffer; |
my ($r,$fields) = @_; |
|
|
$r->read($buffer,$r->header_in('Content-length')); |
my $buffer; |
unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) { |
if ($r->header_in('Content-length')) { |
my @pairs=split(/&/,$buffer); |
$r->read($buffer,$r->header_in('Content-length'),0); |
my $pair; |
} |
foreach $pair (@pairs) { |
my $content_type = $r->header_in('Content-type'); |
my ($name,$value) = split(/=/,$pair); |
if ($content_type !~ m{^multipart/form-data}) { |
$value =~ tr/+/ /; |
my @pairs=split(/&/,$buffer); |
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; |
my $pair; |
$name =~ tr/+/ /; |
foreach $pair (@pairs) { |
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; |
my ($name,$value) = split(/=/,$pair); |
&Apache::loncommon::add_to_env("form.$name",$value); |
$value =~ tr/+/ /; |
|
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; |
|
$name =~ tr/+/ /; |
|
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; |
|
if (ref($fields) eq 'ARRAY') { |
|
next if (!grep(/^\Q$name\E$/,@{$fields})); |
} |
} |
} else { |
&Apache::loncommon::add_to_env("form.$name",$value); |
my $contentsep=$1; |
} |
my @lines = split (/\n/,$buffer); |
} else { |
my $name=''; |
my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/); |
my $value=''; |
my @lines = split (/\n/,$buffer); |
my $fname=''; |
my $name=''; |
my $fmime=''; |
my $value=''; |
my $i; |
my $fname=''; |
for ($i=0;$i<=$#lines;$i++) { |
my $fmime=''; |
if ($lines[$i]=~/^$contentsep/) { |
my $i; |
if ($name) { |
for ($i=0;$i<=$#lines;$i++) { |
chomp($value); |
if ($lines[$i]=~/^--\Q$contentsep\E/) { |
if ($fname) { |
if ($name) { |
$ENV{"form.$name.filename"}=$fname; |
chomp($value); |
$ENV{"form.$name.mimetype"}=$fmime; |
if ($fname) { |
} else { |
$env{"form.$name.filename"}=$fname; |
$value=~s/\s+$//s; |
$env{"form.$name.mimetype"}=$fmime; |
} |
} else { |
&Apache::loncommon::add_to_env("form.$name",$value); |
$value=~s/\s+$//s; |
|
} |
|
if (ref($fields) eq 'ARRAY') { |
|
next if (!grep(/^\Q$name\E$/,@{$fields})); |
} |
} |
if ($i<$#lines) { |
&Apache::loncommon::add_to_env("form.$name",$value); |
$i++; |
} |
$lines[$i]=~ |
if ($i<$#lines) { |
/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i; |
$i++; |
$name=$1; |
$lines[$i]=~ |
$value=''; |
/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i; |
if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) { |
$name=$1; |
$fname=$1; |
$value=''; |
if |
if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) { |
|
$fname=$1; |
|
if |
($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) { |
($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) { |
$fmime=$1; |
$fmime=$1; |
$i++; |
$i++; |
} else { |
} else { |
$fmime=''; |
$fmime=''; |
} |
} |
} else { |
} else { |
$fname=''; |
$fname=''; |
$fmime=''; |
$fmime=''; |
} |
} |
$i++; |
$i++; |
} |
} |
} else { |
} else { |
$value.=$lines[$i]."\n"; |
$value.=$lines[$i]."\n"; |
} |
} |
|
} |
|
} |
|
# |
|
# Digested POSTed values |
|
# |
|
# Remember the way this was originally done (GET or POST) |
|
# |
|
$env{'request.method'}=$ENV{'REQUEST_METHOD'}; |
|
# |
|
# There may also be stuff in the query string |
|
# Tell subsequent handlers that this was GET, not POST, so they can access query string. |
|
# Also, unset POSTed content length to cover all tracks. |
|
# |
|
|
|
$r->method_number(M_GET); |
|
|
|
$r->method('GET'); |
|
$r->headers_in->unset('Content-length'); |
|
} |
|
|
|
# handle the case of the single sign on user, at this point $r->user |
|
# will be set and valid now need to find the loncapa user info and possibly |
|
# balance them |
|
# returns OK if it was a SSO and user was handled |
|
# undef if not SSO or no means to hanle the user |
|
sub sso_login { |
|
my ($r,$handle) = @_; |
|
|
|
my $lonidsdir=$r->dir_config('lonIDsDir'); |
|
if (!($r->user |
|
&& (!defined($env{'user.name'}) && !defined($env{'user.domain'})) |
|
&& ($handle eq ''))) { |
|
# not an SSO case or already logged in |
|
return undef; |
|
} |
|
|
|
my ($user) = ($r->user =~ m/([a-zA-Z0-9_\-@.]*)/); |
|
|
|
my $domain = $r->dir_config('lonDefDomain'); |
|
my $home=&Apache::lonnet::homeserver($user,$domain); |
|
if ($home !~ /(con_lost|no_host|no_such_host)/) { |
|
&Apache::lonnet::logthis(" SSO authorized user $user "); |
|
if ($r->dir_config("lonBalancer") eq 'yes') { |
|
# login but immeaditly go to switch server to find us a new |
|
# machine |
|
&Apache::lonauth::success($r,$user,$domain,$home,'noredirect'); |
|
$env{'request.sso.login'} = 1; |
|
if (defined($r->dir_config("lonSSOReloginServer"))) { |
|
$env{'request.sso.reloginserver'} = |
|
$r->dir_config('lonSSOReloginServer'); |
} |
} |
|
$r->internal_redirect('/adm/switchserver'); |
|
$r->set_handlers('PerlHandler'=> undef); |
|
} else { |
|
# need to login them in, so generate the need data that |
|
# migrate expects to do login |
|
my %info=('ip' => $r->connection->remote_ip(), |
|
'domain' => $domain, |
|
'username' => $user, |
|
'server' => $r->dir_config('lonHostID'), |
|
'sso.login' => 1 |
|
); |
|
if (defined($r->dir_config("lonSSOReloginServer"))) { |
|
$info{'sso.reloginserver'} = |
|
$r->dir_config('lonSSOReloginServer'); |
|
} |
|
my $token = |
|
&Apache::lonnet::tmpput(\%info, |
|
$r->dir_config('lonHostID')); |
|
$env{'form.token'} = $token; |
|
$r->internal_redirect('/adm/migrateuser'); |
|
$r->set_handlers('PerlHandler'=> undef); |
} |
} |
$ENV{'request.method'}=$ENV{'REQUEST_METHOD'}; |
return OK; |
$r->method_number(M_GET); |
} elsif (defined($r->dir_config('lonSSOUserUnknownRedirect'))) { |
$r->method('GET'); |
&Apache::lonnet::logthis(" SSO authorized unknown user $user "); |
$r->headers_in->unset('Content-length'); |
$r->subprocess_env->set('SSOUserUnknown' => $user); |
|
$r->subprocess_env->set('SSOUserDomain' => $domain); |
|
$r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect')); |
|
$r->set_handlers('PerlHandler'=> undef); |
|
return OK; |
|
} |
|
return undef; |
|
} |
|
|
# ---------------------------------------------------------------- Check access |
sub handler { |
|
my $r = shift; |
|
my $requrl=$r->uri; |
|
if (&Apache::lonnet::is_domainimage($requrl)) { |
|
return OK; |
|
} |
|
|
|
|
|
my $handle = &Apache::lonnet::check_for_valid_session($r); |
|
|
|
my $result = &sso_login($r,$handle); |
|
if (defined($result)) { |
|
return $result |
|
} |
|
|
|
|
|
if ($r->dir_config("lonBalancer") eq 'yes') { |
|
$r->set_handlers('PerlResponseHandler'=> |
|
[\&Apache::switchserver::handler]); |
|
} |
|
|
|
if ($handle eq '') { |
|
$r->log_reason("Cookie $handle not valid", $r->filename); |
|
} elsif ($handle ne '') { |
|
|
|
# ------------------------------------------------------ Initialize Environment |
|
my $lonidsdir=$r->dir_config('lonIDsDir'); |
|
&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle); |
|
|
|
# --------------------------------------------------------- Initialize Language |
|
|
|
&Apache::lonlocal::get_language_handle($r); |
|
|
if ($requrl!~/^\/adm|public|prtspool\//) { |
} |
my $access=&Apache::lonnet::allowed('bre',$requrl); |
|
if ($access eq '1') { |
# -------------------------------------------------- Should be a valid user now |
$ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course"; |
if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') { |
return HTTP_NOT_ACCEPTABLE; |
# -------------------------------------------------------------- Resource State |
} |
|
if (($access ne '2') && ($access ne 'F')) { |
if ($requrl=~/^\/+(res|uploaded)\//) { |
$ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; |
$env{'request.state'} = "published"; |
return HTTP_NOT_ACCEPTABLE; |
} else { |
} |
$env{'request.state'} = 'unknown'; |
|
} |
|
$env{'request.filename'} = $r->filename; |
|
$env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl); |
|
# -------------------------------------------------------- Load POST parameters |
|
|
|
&Apache::lonacc::get_posted_cgi($r); |
|
|
|
# ---------------------------------------------------------------- Check access |
|
my $now = time; |
|
if ($requrl !~ m{^/(?:adm|public|prtspool)/} |
|
|| $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) { |
|
my $access=&Apache::lonnet::allowed('bre',$requrl); |
|
if ($access eq '1') { |
|
$env{'user.error.msg'}="$requrl:bre:0:0:Choose Course"; |
|
return HTTP_NOT_ACCEPTABLE; |
|
} |
|
if ($access eq 'A') { |
|
&Apache::restrictedaccess::setup_handler($r); |
|
return OK; |
|
} |
|
if ($access eq 'B') { |
|
&Apache::blockedaccess::setup_handler($r); |
|
return OK; |
} |
} |
if ($requrl =~ m|^/prtspool/|) { |
if (($access ne '2') && ($access ne 'F')) { |
my $start='/prtspool/'.$ENV{'user.name'}.'_'. |
$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; |
$ENV{'user.domain'}; |
return HTTP_NOT_ACCEPTABLE; |
if ($requrl !~ /^\Q$start\E/) { |
|
$ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; |
|
return HTTP_NOT_ACCEPTABLE; |
|
} |
|
} |
} |
|
} |
|
if ($requrl =~ m|^/prtspool/|) { |
|
my $start='/prtspool/'.$env{'user.name'}.'_'. |
|
$env{'user.domain'}; |
|
if ($requrl !~ /^\Q$start\E/) { |
|
$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; |
|
return HTTP_NOT_ACCEPTABLE; |
|
} |
|
} |
|
if ($requrl =~ m|^/zipspool/|) { |
|
my $start='/zipspool/zipout/'.$env{'user.name'}.":". |
|
$env{'user.domain'}; |
|
if ($requrl !~ /^\Q$start\E/) { |
|
$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; |
|
return HTTP_NOT_ACCEPTABLE; |
|
} |
|
} |
|
if ($env{'user.name'} eq 'public' && |
|
$env{'user.domain'} eq 'public' && |
|
$requrl !~ m{^/+(res|public|uploaded)/} && |
|
$requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x && |
|
$requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) { |
|
$env{'request.querystring'}=$r->args; |
|
$env{'request.firsturl'}=$requrl; |
|
return FORBIDDEN; |
|
} |
# ------------------------------------------------------------- This is allowed |
# ------------------------------------------------------------- This is allowed |
if ($ENV{'request.course.id'}) { |
if ($env{'request.course.id'}) { |
&Apache::lonnet::countacc($requrl); |
&Apache::lonnet::countacc($requrl); |
$requrl=~/\.(\w+)$/; |
$requrl=~/\.(\w+)$/; |
if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') || |
if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') || |
($requrl=~/^\/adm\/(aboutme|navmaps|smppg|bulletinboard|wrapper)/) || |
($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$ )/x) || |
($requrl=~/^\/public\/.*\/syllabus$/)) { |
($requrl=~/^\/adm\/wrapper\//) || |
|
($requrl=~m|^/adm/coursedocs/showdoc/|) || |
|
($requrl=~m|\.problem/smpedit$|) || |
|
($requrl=~/^\/public\/.*\/syllabus$/)) { |
# ------------------------------------- This is serious stuff, get symb and log |
# ------------------------------------- This is serious stuff, get symb and log |
my $query=$r->args; |
my $query=$r->args; |
my $symb; |
my $symb; |
if ($query) { |
if ($query) { |
&Apache::loncommon::get_unprocessed_cgi($query,['symb']); |
&Apache::loncommon::get_unprocessed_cgi($query,['symb']); |
} |
} |
if ($ENV{'form.symb'}) { |
if ($env{'form.symb'}) { |
$symb=&Apache::lonnet::symbclean($ENV{'form.symb'}); |
$symb=&Apache::lonnet::symbclean($env{'form.symb'}); |
if (&Apache::lonnet::symbverify($symb,$requrl)) { |
if ($requrl =~ m|^/adm/wrapper/| |
my ($map,$mid,$murl)=split(/\_\_\_/,$symb); |
|| $requrl =~ m|^/adm/coursedocs/showdoc/|) { |
&Apache::lonnet::symblist($map,$murl => $mid, |
my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb); |
'last_known' => $murl); |
&Apache::lonnet::symblist($map,$murl => [$murl,$mid], |
|
'last_known' =>[$murl,$mid]); |
|
} elsif ((&Apache::lonnet::symbverify($symb,$requrl)) || |
|
(($requrl=~m|(.*)/smpedit$|) && |
|
&Apache::lonnet::symbverify($symb,$1))) { |
|
my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb); |
|
&Apache::lonnet::symblist($map,$murl => [$murl,$mid], |
|
'last_known' =>[$murl,$mid]); |
} else { |
} else { |
$r->log_reason('Invalid symb for '.$requrl.': '. |
$r->log_reason('Invalid symb for '.$requrl.': '. |
$symb); |
$symb); |
$ENV{'user.error.msg'}= |
$env{'user.error.msg'}= |
"$requrl:bre:1:1:Invalid Access"; |
"$requrl:bre:1:1:Invalid Access"; |
return HTTP_NOT_ACCEPTABLE; |
return HTTP_NOT_ACCEPTABLE; |
} |
} |
} else { |
} else { |
$symb=&Apache::lonnet::symbread; |
$symb=&Apache::lonnet::symbread($requrl); |
} |
if (&Apache::lonnet::is_on_map($requrl) && $symb && |
$ENV{'request.symb'}=$symb; |
!&Apache::lonnet::symbverify($symb,$requrl)) { |
&Apache::lonnet::courseacclog($symb); |
$r->log_reason('Invalid symb for '.$requrl.': '.$symb); |
} else { |
$env{'user.error.msg'}= |
|
"$requrl:bre:1:1:Invalid Access"; |
|
return HTTP_NOT_ACCEPTABLE; |
|
} |
|
if ($symb) { |
|
my ($map,$mid,$murl)= |
|
&Apache::lonnet::decode_symb($symb); |
|
&Apache::lonnet::symblist($map,$murl =>[$murl,$mid], |
|
'last_known' =>[$murl,$mid]); |
|
} |
|
} |
|
$env{'request.symb'}=$symb; |
|
&Apache::lonnet::courseacclog($symb); |
|
} else { |
# ------------------------------------------------------- This is other content |
# ------------------------------------------------------- This is other content |
&Apache::lonnet::courseacclog($requrl); |
&Apache::lonnet::courseacclog($requrl); |
} |
} |
} |
} |
return OK; |
return OK; |
} else { |
|
$r->log_reason("Cookie $handle not valid", $r->filename) |
|
}; |
|
} |
} |
|
|
# -------------------------------------------- See if this is a public resource |
# -------------------------------------------- See if this is a public resource |
if ($requrl=~m|^/public/| |
if ($requrl=~m|^/+adm/+help/+|) { |
|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) { |
return OK; |
&Apache::lonnet::logthis('Granting public access: '.$requrl); |
|
$ENV{'user.name'}='public'; |
|
$ENV{'user.domain'}='public'; |
|
$ENV{'request.state'} = "published"; |
|
$ENV{'request.publicaccess'} = 1; |
|
$ENV{'request.filename'} = $r->filename; |
|
return OK; |
|
} |
} |
|
# ------------------------------------ See if this is a viewable portfolio file |
|
if (&Apache::lonnet::is_portfolio_url($requrl)) { |
|
my $access=&Apache::lonnet::allowed('bre',$requrl); |
|
if ($access eq 'A') { |
|
&Apache::restrictedaccess::setup_handler($r); |
|
return OK; |
|
} |
|
if (($access ne '2') && ($access ne 'F')) { |
|
$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; |
|
return HTTP_NOT_ACCEPTABLE; |
|
} |
|
} |
|
|
# -------------------------------------------------------------- Not authorized |
# -------------------------------------------------------------- Not authorized |
$requrl=~/\.(\w+)$/; |
$requrl=~/\.(\w+)$/; |
if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') || |
# if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') || |
($requrl=~/^\/adm\/(roles|logout|email)/) || |
# ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) || |
($requrl=~m|^/prtspool/|)) { |
# ($requrl=~m|^/prtspool/|)) { |
# -------------------------- Store where they wanted to go and get login screen |
# -------------------------- Store where they wanted to go and get login screen |
$ENV{'request.querystring'}=$r->args; |
$env{'request.querystring'}=$r->args; |
$ENV{'request.firsturl'}=$requrl; |
$env{'request.firsturl'}=$requrl; |
return FORBIDDEN; |
return FORBIDDEN; |
} else { |
# } else { |
# --------------------------------------------------------------------- Goodbye |
# --------------------------------------------------------------------- Goodbye |
return HTTP_BAD_REQUEST; |
# return HTTP_BAD_REQUEST; |
} |
# } |
} |
} |
|
|
1; |
1; |