--- loncom/auth/lonacc.pm 2021/12/12 00:53:57 1.159.2.21 +++ loncom/auth/lonacc.pm 2020/12/18 15:23:03 1.184 @@ -1,7 +1,7 @@ # The LearningOnline Network # Cookie Based Access Handler # -# $Id: lonacc.pm,v 1.159.2.21 2021/12/12 00:53:57 raeburn Exp $ +# $Id: lonacc.pm,v 1.184 2020/12/18 15:23:03 raeburn Exp $ # # Copyright Michigan State University Board of Trustees # @@ -102,6 +102,7 @@ use Apache::loncommon(); use Apache::lonlocal; use Apache::restrictedaccess(); use Apache::blockedaccess(); +use Apache::lonprotected(); use Fcntl qw(:flock); use LONCAPA qw(:DEFAULT :match); @@ -159,8 +160,7 @@ sub get_posted_cgi { if (length($value) == 1) { $value=~s/[\r\n]$//; } - } - if ($fname =~ /\.(xls|doc|ppt)(x|m)$/i) { + } elsif ($fname =~ /\.(xls|doc|ppt)(x|m)$/i) { $value=~s/[\r\n]$//; } if (ref($fields) eq 'ARRAY') { @@ -203,6 +203,14 @@ sub get_posted_cgi { $fname=''; $fmime=''; } + if ($i<$#lines && $lines[$i+1]=~/^Content\-Type\:\s*([\w\-\/]+)/i) { + # TODO: something with $1 ! + $i++; + } + if ($i<$#lines && $lines[$i+1]=~/^Content\-transfer\-encoding\:\s*([\w\-\/]+)/i) { + # TODO: something with $1 ! + $i++; + } $i++; } } else { @@ -297,7 +305,7 @@ sub sso_login { my $query = $r->args; my %form; if ($query) { - my @items = ('role','symb','iptoken','origurl','logtoken'); + my @items = ('role','symb','iptoken'); &Apache::loncommon::get_unprocessed_cgi($query,\@items); foreach my $item (@items) { if (defined($env{'form.'.$item})) { @@ -315,38 +323,6 @@ sub sso_login { } } -# -# If Shibboleth auth is in use, and a dual SSO and non-SSO login page -# is in use, then the query string will contain the logtoken item with -# a value set to the name of a .tmp file in /home/httpd/perl/tmp -# containing the url to display after authentication, and also, -# optionally, role and symb. -# -# Otherwise the query string may contain role and symb. -# - - if ($form{'logtoken'}) { - my ($firsturl,@rest); - my $lonhost = $r->dir_config('lonHostID'); - my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},$lonhost); - my $delete = &Apache::lonnet::tmpdel($form{'logtoken'}); - unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') || - ($tmpinfo eq 'no_such_host')) { - (undef,$firsturl,@rest) = split(/&/,$tmpinfo); - if ($firsturl ne '') { - $firsturl = &unescape($firsturl); - } - foreach my $item (@rest) { - my ($key,$value) = split(/=/,$item); - $form{$key} = &unescape($value); - } - if ($form{'iptoken'}) { - %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'}); - my $delete = &Apache::lonnet::tmpdel($form{'iptoken'}); - } - } - } - my $domain = $r->dir_config('lonSSOUserDomain'); if ($domain eq '') { $domain = $r->dir_config('lonDefDomain'); @@ -373,7 +349,7 @@ sub sso_login { my $lowest_load; ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($domain); if ($lowest_load > 100) { - $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$domain); + $otherserver = &Apache::lonnet::spareserver($lowest_load,$lowest_load,1,$domain); } if ($otherserver ne '') { my @hosts = &Apache::lonnet::current_machine_ids(); @@ -391,7 +367,7 @@ sub sso_login { foreach my $item (keys(%form)) { $env{'form.'.$item} = $form{$item}; } - unless (($form{'symb'}) || ($form{'origurl'})) { + unless ($form{'symb'}) { unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) { $env{'form.origurl'} = $r->uri; } @@ -410,21 +386,19 @@ sub sso_login { } else { # need to login them in, so generate the need data that # migrate expects to do login - my $ip = &Apache::lonnet::get_requestor_ip($r); + my $ip = &Apache::lonnet::get_requestor_ip($r); my %info=('ip' => $ip, 'domain' => $domain, 'username' => $user, 'server' => $r->dir_config('lonHostID'), 'sso.login' => 1 ); - foreach my $item ('role','symb','iptoken','origurl') { + foreach my $item ('role','symb','iptoken') { if (exists($form{$item})) { $info{$item} = $form{$item}; - } elsif ($sessiondata{$item} ne '') { - $info{$item} = $sessiondata{$item}; } } - unless (($info{'symb'}) || ($info{'origurl'})) { + unless ($info{'symb'}) { unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) { $info{'origurl'} = $r->uri; } @@ -466,8 +440,6 @@ sub sso_login { $r->subprocess_env->set('SSOUserUnknown' => $user); $r->subprocess_env->set('SSOUserDomain' => $domain); if (grep(/^sso$/,@cancreate)) { -#FIXME - need to preserve origurl, role and symb for use after account -# creation $r->set_handlers('PerlHandler'=> [\&Apache::createaccount::handler]); $r->handler('perl-script'); } else { @@ -507,7 +479,7 @@ sub handler { if ($handle eq '') { unless ((($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) || ($requrl =~ m{^/public/$match_domain/$match_courseid/syllabus}) || - ($requrl =~ m{^/adm/help/}) || ($requrl eq '/adm/sso') || + ($requrl =~ m{^/adm/help/}) || ($requrl =~ m{^/res/$match_domain/$match_username/})) { $r->log_reason("Cookie not valid", $r->filename); } @@ -565,7 +537,8 @@ sub handler { } } elsif ($env{'request.course.id'} && (($requrl =~ m{^/adm/$match_domain/$match_username/aboutme$}) || - ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}))) { + ($requrl eq "/public/$cdom/$cnum/syllabus") || + ($requrl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}))) { my $query = $r->args; if ($query) { foreach my $pair (split(/&/,$query)) { @@ -586,7 +559,8 @@ sub handler { my $hostname = $r->hostname(); my $lonhost = &Apache::lonnet::host_from_dns($hostname); if ($lonhost) { - my $actual = &Apache::lonnet::absolute_url($hostname,1,1); + my $actual = &Apache::lonnet::absolute_url($hostname); + my $exphostname = &Apache::lonnet::hostname($lonhost); my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname; unless ($actual eq $expected) { $env{'request.use_absolute'} = $expected; @@ -618,7 +592,7 @@ sub handler { if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) { $otherserver = $found_server; } - unless ($requrl eq '/adm/switchserver') { + unless ($requrl eq '/adm/switchserver') { $r->set_handlers('PerlResponseHandler'=> [\&Apache::switchserver::handler]); } @@ -631,36 +605,35 @@ sub handler { } } } - + if ($requrl=~m{^/+tiny/+$match_domain/+\w+$}) { + if ($env{'user.name'} eq 'public' && + $env{'user.domain'} eq 'public') { + $env{'request.firsturl'}=$requrl; + return FORBIDDEN; + } else { + return OK; + } + } # ---------------------------------------------------------------- Check access my $now = time; - my ($check_symb,$check_access,$check_block,$access,$poss_symb); + my $check_symb; if ($requrl !~ m{^/(?:adm|public|(?:prt|zip)spool)/} || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) { - $check_access = 1; - } - if ((!$check_access) && ($env{'request.course.id'})) { - if (($requrl eq '/adm/viewclasslist') || - ($requrl =~ m{^(/adm/wrapper|)\Q/uploaded/$cdom/$cnum/docs/\E}) || - ($requrl =~ m{^/adm/.*/aboutme$}) || - ($requrl=~m{^/adm/coursedocs/showdoc/})) { - $check_block = 1; - } - } - if (($env{'request.course.id'}) && (!$suppext)) { - $requrl=~/\.(\w+)$/; - if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') || - ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) || - ($requrl=~/^\/adm\/wrapper\//) || - ($requrl=~m|^/adm/coursedocs/showdoc/|) || - ($requrl=~m|\.problem/smpedit$|) || - ($requrl=~/^\/public\/.*\/syllabus$/) || - ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) || - ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/)) { - $check_symb = 1; + my ($access,$poss_symb); + if (($env{'request.course.id'}) && (!$suppext)) { + $requrl=~/\.(\w+)$/; + if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') || + ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) || + ($requrl=~/^\/adm\/wrapper\//) || + ($requrl=~m|^/adm/coursedocs/showdoc/|) || + ($requrl=~m|\.problem/smpedit$|) || + ($requrl=~/^\/public\/.*\/syllabus$/) || + ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) || + ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/) || + ($requrl=~m{^/adm/$cdom/$cnum/\d+/ext\.tool$})) { + $check_symb = 1; + } } - } - if (($check_access) || ($check_block)) { if ($check_symb) { if ($env{'form.symb'}) { $poss_symb=&Apache::lonnet::symbclean($env{'form.symb'}); @@ -680,9 +653,7 @@ sub handler { if ($poss_symb) { my ($possmap,$resid,$url)=&Apache::lonnet::decode_symb($poss_symb); $url = &Apache::lonnet::clutter($url); - my $toplevelmap = $env{'course.'.$env{'request.course.id'}.'.url'}; - unless (($url eq $requrl) && (($possmap eq $toplevelmap) || - (&Apache::lonnet::is_on_map($possmap)))) { + unless (($url eq $requrl) && (&Apache::lonnet::is_on_map($possmap))) { undef($poss_symb); } if ($poss_symb) { @@ -698,21 +669,8 @@ sub handler { $access=&Apache::lonnet::allowed('bre',$requrl,'','','','',1); } } else { - my $clientip = &Apache::lonnet::get_requestor_ip($r); - $access=&Apache::lonnet::allowed('bre',$requrl,'','',$clientip); + $access=&Apache::lonnet::allowed('bre',$requrl); } - } - if ($check_block) { - if ($access eq 'B') { - if ($poss_symb) { - if (&Apache::lonnet::symbverify($poss_symb,$requrl)) { - $env{'request.symb'} = $poss_symb; - } - } - &Apache::blockedaccess::setup_handler($r); - return OK; - } - } elsif ($check_access) { if ($handle eq '') { unless ($access eq 'F') { if ($requrl =~ m{^/res/$match_domain/$match_username/}) { @@ -730,6 +688,9 @@ sub handler { } if ($access eq 'B') { if ($poss_symb) { + if ($requrl=~m{^(/adm/.*/aboutme)/portfolio$}) { + $requrl = $1; + } if (&Apache::lonnet::symbverify($poss_symb,$requrl)) { $env{'request.symb'} = $poss_symb; } @@ -737,6 +698,10 @@ sub handler { &Apache::blockedaccess::setup_handler($r); return OK; } + if ($access eq 'D') { + &Apache::lonprotected::setup_handler($r); + return OK; + } if (($access ne '2') && ($access ne 'F')) { if ($requrl =~ m{^/res/}) { $access = &Apache::lonnet::allowed('bro',$requrl); @@ -753,7 +718,7 @@ sub handler { } } } elsif (($handle =~ /^publicuser_\d+$/) && (&Apache::lonnet::is_portfolio_url($requrl))) { - my $clientip = &Apache::lonnet::get_requestor_ip($r); + my $clientip = &Apache::lonnet::get_requestor_ip($r); if (&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip) ne 'F') { $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied"; return HTTP_NOT_ACCEPTABLE; @@ -802,32 +767,18 @@ sub handler { } if ($env{'form.symb'}) { $symb=&Apache::lonnet::symbclean($env{'form.symb'}); - if (($requrl eq '/adm/navmaps') || - ($requrl =~ m{^/adm/wrapper/}) || - ($requrl =~ m{^/adm/coursedocs/showdoc/})) { - unless (&Apache::lonnet::symbverify($symb,$requrl)) { - if (&Apache::lonnet::is_on_map($requrl)) { - $symb = &Apache::lonnet::symbread($requrl); - unless (&Apache::lonnet::symbverify($symb,$requrl)) { - undef($symb); - } - } - } - if ($symb) { - if ($requrl eq '/adm/navmaps') { - my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb); - &Apache::lonnet::symblist($map,$murl => [$murl,$mid]); - } elsif (($requrl =~ m{^/adm/wrapper/}) || - ($requrl =~ m{^/adm/coursedocs/showdoc/})) { - my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb); - if ($map =~ /\.page$/) { - my $mapsymb = &Apache::lonnet::symbread($map); - ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb); - } - &Apache::lonnet::symblist($map,$murl => [$murl,$mid], - 'last_known' =>[$murl,$mid]); - } + if ($requrl eq '/adm/navmaps') { + my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb); + &Apache::lonnet::symblist($map,$murl => [$murl,$mid]); + } elsif ($requrl =~ m|^/adm/wrapper/| + || $requrl =~ m|^/adm/coursedocs/showdoc/|) { + my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb); + if ($map =~ /\.page$/) { + my $mapsymb = &Apache::lonnet::symbread($map); + ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb); } + &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)) || @@ -887,14 +838,10 @@ sub handler { } } if ($invalidsymb) { - if ($requrl eq '/adm/navmaps') { - undef($symb); - } else { - $r->log_reason('Invalid symb for '.$requrl.': '.$symb); - $env{'user.error.msg'}= - "$requrl:bre:1:1:Invalid Access"; - return HTTP_NOT_ACCEPTABLE; - } + $r->log_reason('Invalid symb for '.$requrl.': '.$symb); + $env{'user.error.msg'}= + "$requrl:bre:1:1:Invalid Access"; + return HTTP_NOT_ACCEPTABLE; } } } @@ -908,8 +855,8 @@ sub handler { my $mapsymb = &Apache::lonnet::symbread($map); ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb); } - &Apache::lonnet::symblist($map,$murl =>[$murl,$mid], - 'last_known' =>[$murl,$mid]); + &Apache::lonnet::symblist($map,$murl =>[$murl,$mid], + 'last_known' =>[$murl,$mid]); } } } @@ -962,7 +909,7 @@ sub handler { # ------------------------------------ See if this is a viewable portfolio file if (&Apache::lonnet::is_portfolio_url($requrl)) { my $clientip = &Apache::lonnet::get_requestor_ip($r); - my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip); + my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip); if ($access eq 'A') { &Apache::restrictedaccess::setup_handler($r); return OK;