File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.159.2.21.2.5: download - view: text, annotated - select for diffs
Wed Jul 5 17:31:46 2023 UTC (10 months, 1 week ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.159.2.21: preferred, unified
- For 2.11.4 (modified)
  Include changes in 1.166, 1.167, 1.186 (part), 1.208

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.159.2.21.2.5 2023/07/05 17:31:46 raeburn 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: =head1 NAME
   31: 
   32: Apache::lonacc - Cookie Based Access Handler
   33: 
   34: =head1 SYNOPSIS
   35: 
   36: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
   37: 
   38:  PerlAccessHandler       Apache::lonacc
   39: 
   40: =head1 INTRODUCTION
   41: 
   42: This module enables cookie based authentication and is used
   43: to control access for many different LON-CAPA URIs.
   44: 
   45: Whenever the client sends the cookie back to the server, 
   46: this cookie is handled by either lonacc.pm or loncacc.pm
   47: (see srm.conf for what is invoked when).  If
   48: the cookie is missing or invalid, the user is re-challenged
   49: for login information.
   50: 
   51: This is part of the LearningOnline Network with CAPA project
   52: described at http://www.lon-capa.org.
   53: 
   54: =head1 HANDLER SUBROUTINE
   55: 
   56: This routine is called by Apache and mod_perl.
   57: 
   58: =over 4
   59: 
   60: =item *
   61: 
   62: transfer profile into environment
   63: 
   64: =item *
   65: 
   66: load POST parameters
   67: 
   68: =item *
   69: 
   70: check access
   71: 
   72: =item *
   73: 
   74: if allowed, get symb, log, generate course statistics if applicable
   75: 
   76: =item *
   77: 
   78: otherwise return error
   79: 
   80: =item *
   81: 
   82: see if public resource
   83: 
   84: =item *
   85: 
   86: store attempted access
   87: 
   88: =back
   89: 
   90: =head1 NOTABLE SUBROUTINES
   91: 
   92: =cut
   93: 
   94: 
   95: package Apache::lonacc;
   96: 
   97: use strict;
   98: use Apache::Constants qw(:common :http :methods);
   99: use Apache::File;
  100: use Apache::lonnet;
  101: use Apache::loncommon();
  102: use Apache::lonlocal;
  103: use Apache::restrictedaccess();
  104: use Apache::blockedaccess();
  105: use Apache::lonprotected();
  106: use Fcntl qw(:flock);
  107: use LONCAPA qw(:DEFAULT :match);
  108: 
  109: sub cleanup {
  110:     my ($r)=@_;
  111:     if (! $r->is_initial_req()) { return DECLINED; }
  112:     &Apache::lonnet::save_cache();
  113:     return OK;
  114: }
  115: 
  116: sub goodbye {
  117:     my ($r)=@_;
  118:     &Apache::lonnet::goodbye();
  119:     return DONE;
  120: }
  121: 
  122: ###############################################
  123: 
  124: sub get_posted_cgi {
  125:     my ($r,$fields) = @_;
  126: 
  127:     my $buffer;
  128:     if ($r->header_in('Content-length')) {
  129: 	$r->read($buffer,$r->header_in('Content-length'),0);
  130:     }
  131:     my $content_type = $r->header_in('Content-type');
  132:     if ($content_type !~ m{^multipart/form-data}) {
  133: 	my @pairs=split(/&/,$buffer);
  134: 	my $pair;
  135: 	foreach $pair (@pairs) {
  136: 	    my ($name,$value) = split(/=/,$pair);
  137: 	    $value =~ tr/+/ /;
  138: 	    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  139: 	    $name  =~ tr/+/ /;
  140: 	    $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  141:             if (ref($fields) eq 'ARRAY') {
  142:                 next if (!grep(/^\Q$name\E$/,@{$fields}));
  143:             }
  144: 	    &Apache::loncommon::add_to_env("form.$name",$value);
  145: 	}
  146:     } else {
  147: 	my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
  148: 	my @lines = split (/\n/,$buffer);
  149: 	my $name='';
  150: 	my $value='';
  151: 	my $fname='';
  152: 	my $fmime='';
  153: 	my $i;
  154: 	for ($i=0;$i<=$#lines;$i++) {
  155: 	    if ($lines[$i]=~/^--\Q$contentsep\E/) {
  156: 		if ($name) {
  157:                     chomp($value);
  158:                     if (($r->uri eq '/adm/portfolio') && 
  159:                         ($name eq 'uploaddoc')) {
  160:                         if (length($value) == 1) {
  161:                             $value=~s/[\r\n]$//;
  162:                         }
  163:                     }
  164:                     if ($fname =~ /\.(xls|doc|ppt)(x|m)$/i) {
  165:                         $value=~s/[\r\n]$//;
  166:                     }
  167:                     if (ref($fields) eq 'ARRAY') {
  168:                         next if (!grep(/^\Q$name\E$/,@{$fields}));
  169:                     }
  170:                     if ($fname) {
  171:                         if ($env{'form.symb'} ne '') {
  172:                             my $size = (length($value))/(1024.0 * 1024.0);
  173:                             if (&upload_size_allowed($name,$size,$fname) eq 'ok') {
  174:                                 $env{"form.$name.filename"}=$fname;
  175:                                 $env{"form.$name.mimetype"}=$fmime;
  176:                                 &Apache::loncommon::add_to_env("form.$name",$value);
  177:                             }
  178:                         } else {
  179:                             $env{"form.$name.filename"}=$fname;
  180:                             $env{"form.$name.mimetype"}=$fmime;
  181:                             &Apache::loncommon::add_to_env("form.$name",$value);
  182:                         }
  183:                     } else {
  184:                         $value=~s/\s+$//s;
  185:                         &Apache::loncommon::add_to_env("form.$name",$value);
  186:                     }
  187: 		}
  188: 		if ($i<$#lines) {
  189: 		    $i++;
  190: 		    $lines[$i]=~
  191: 		/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
  192: 		    $name=$1;
  193: 		    $value='';
  194: 		    if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
  195: 			$fname=$1;
  196: 			if 
  197:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
  198: 				$fmime=$1;
  199: 				$i++;
  200: 			    } else {
  201: 				$fmime='';
  202: 			    }
  203: 		    } else {
  204: 			$fname='';
  205: 			$fmime='';
  206: 		    }
  207: 		    $i++;
  208: 		}
  209: 	    } else {
  210: 		$value.=$lines[$i]."\n";
  211: 	    }
  212: 	}
  213:     }
  214: #
  215: # Digested POSTed values
  216: #
  217: # Remember the way this was originally done (GET or POST)
  218: #
  219:     $env{'request.method'}=$ENV{'REQUEST_METHOD'};
  220: #
  221: # There may also be stuff in the query string
  222: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
  223: # Also, unset POSTed content length to cover all tracks.
  224: #
  225: 
  226:     $r->method_number(M_GET);
  227: 
  228:     $r->method('GET');
  229:     $r->headers_in->unset('Content-length');
  230: }
  231: 
  232: =pod
  233: 
  234: =over
  235: 
  236: =item upload_size_allowed()
  237: 
  238: 	Perform size checks for file uploads to essayresponse items in course context.
  239: 	
  240: 	Add form.HWFILESIZE.$part_$id to %env with file size (MB)
  241: 	If file exceeds maximum allowed size, add form.HWFILETOOBIG.$part_$id to %env.
  242: 
  243: =cut
  244:  
  245: sub upload_size_allowed {
  246:     my ($name,$size,$fname) = @_;
  247:     if ($name =~ /^HWFILE(\w+)$/) {
  248:         my $ident = $1;
  249:         my $item = 'HWFILESIZE'.$ident;
  250:         my $savesize = sprintf("%.6f",$size);
  251:         &Apache::loncommon::add_to_env("form.$item",$savesize);
  252:         my $maxsize= &Apache::lonnet::EXT("resource.$ident.maxfilesize");
  253:         if (!$maxsize) {
  254:             $maxsize = 10.0; # FIXME This should become a domain configuration.
  255:         }
  256:         if ($size > $maxsize) {
  257:             my $warn = 'HWFILETOOBIG'.$ident;
  258:             &Apache::loncommon::add_to_env("form.$warn",$fname);
  259:             return;
  260:         }
  261:     }
  262:     return 'ok';
  263: }
  264: 
  265: =pod
  266: 
  267: =item sso_login()
  268: 
  269: 	handle the case of the single sign on user, at this point $r->user 
  270: 	will be set and valid; now need to find the loncapa user info, and possibly
  271: 	balance them. If $r->user() is set this means either it was either set by
  272:         SSO or by checkauthen.pm, if a valid cookie was found. The latter case can
  273:         be identified by the third arg ($usename), except when lonacc is called in 
  274:         an internal redirect to /adm/switchserver (e.g., load-balancing following
  275:         successful authentication) -- no cookie set yet.  For that particular case
  276:         simply skip the call to sso_login().
  277: 
  278: 	returns OK if it was SSO and user was handled.
  279:         returns undef if not SSO or no means to handle the user.
  280: 
  281:         In the case where the session was started from /adm/launch/tiny/$domain/$id,
  282:         i.e., for a protected link, with launch from another CMS, and user information
  283:         is accepted from the LTI payload, then, if the user has privileged roles,
  284:         authentication will be required.  If SSO authentication is with a username
  285:         and/or domain that differ from the username in the LTI payload and domain
  286:         in the launch URL, then $r->user() will be unset and /adm/relaunch will be
  287:         called.
  288:         
  289: =cut
  290: 
  291: sub sso_login {
  292:     my ($r,$handle,$username) = @_;
  293: 
  294:     if (($r->user eq '') || ($username ne '') || ($r->user eq 'public:public') ||
  295:         (defined($env{'user.name'}) && (defined($env{'user.domain'}))
  296: 	  && ($handle ne ''))) {
  297: 	# not an SSO case or already logged in
  298: 	return undef;
  299:     }
  300: 
  301:     my ($user) = ($r->user =~ m/^($match_username)$/);
  302:     if ($user eq '') {
  303:         return undef;
  304:     }
  305: 
  306:     my $query = $r->args;
  307:     my %form;
  308:     if ($query) {
  309:         my @items = ('role','symb','iptoken','origurl','ttoken',
  310:                      'ltoken','linkkey','logtoken','sso','lcssowin');
  311:         &Apache::loncommon::get_unprocessed_cgi($query,\@items);
  312:         foreach my $item (@items) {
  313:             if (defined($env{'form.'.$item})) {
  314:                 $form{$item} = $env{'form.'.$item};
  315:             }
  316:         }
  317:     }
  318: 
  319:     my %sessiondata;
  320:     if ($form{'iptoken'}) {
  321:         %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  322:         my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  323:         unless ($sessiondata{'sessionserver'}) {
  324:             delete($form{'iptoken'});
  325:         }
  326:     }
  327: 
  328:     my ($linkprot,$linkprotuser,$linkprotexit,$linkkey,$deeplinkurl,
  329:         $linkprotpbid,$linkprotpburl);
  330: 
  331: #
  332: # If Shibboleth auth is in use, and a dual SSO and non-SSO login page
  333: # is in use, then the query string will contain the logtoken item with
  334: # a value set to the name of a .tmp file in /home/httpd/perl/tmp
  335: # containing the url to display after authentication, and also,
  336: # optionally, role and symb, or linkprot or linkkey (deep-link access).
  337: #
  338: # If Shibboleth auth is in use, but a dual log-in page is not in use,
  339: # and the originally requested URL was /tiny/$domain/$id (i.e.,
  340: # for deeplinking), then the query string will contain the sso item
  341: # with a value set to the name of a .tmp file in /home/httpd/perl/tmp
  342: # containing the url to display after authentication, and also,
  343: # optionally, linkprot or linkkey (deep-link access).
  344: #
  345: # Otherwise the query string may contain role and symb, or if the
  346: # originally requested URL was /tiny/$domain/$id (i.e. for deeplinking)
  347: # then the query string may contain a ttoken item with a value set
  348: # to the name of a .tmp file in /home/httpd/perl/tmp containing either
  349: # linkprot or linkkey (deep-link access).
  350: #
  351: # If deep-linked, i.e., the originally requested URL was /tiny/$domain/$id
  352: # the linkkey may have originally been sent in POSTed data, which will
  353: # have been processed in lontrans.pm
  354: #
  355: 
  356:     if ($form{'ttoken'}) {
  357:         my %info = &Apache::lonnet::tmpget($form{'ttoken'});
  358:         &Apache::lonnet::tmpdel($form{'ttoken'});
  359:         if ($info{'origurl'}) {
  360:             $form{'origurl'} = $info{'origurl'};
  361:             if ($form{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
  362:                 $deeplinkurl = $form{'origurl'};
  363:             }
  364:         }
  365:         if ($info{'linkprot'}) {
  366:             $linkprot = $info{'linkprot'};
  367:             $linkprotuser = $info{'linkprotuser'};
  368:             $linkprotexit = $info{'linkprotexit'};
  369:             $linkprotpbid = $info{'linkprotpbid'};
  370:             $linkprotpburl = $info{'linkprotpburl'};
  371:         } elsif ($info{'linkkey'} ne '') {
  372:             $linkkey = $info{'linkkey'};
  373:         }
  374:     } elsif ($form{'logtoken'}) {
  375:         my ($firsturl,@rest);
  376:         my $lonhost = $r->dir_config('lonHostID');
  377:         my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},$lonhost);
  378:         my $delete = &Apache::lonnet::tmpdel($form{'logtoken'});
  379:         unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
  380:                 ($tmpinfo eq 'no_such_host')) {
  381:             (undef,$firsturl,@rest) = split(/&/,$tmpinfo);
  382:             if ($firsturl ne '') {
  383:                 $firsturl = &unescape($firsturl);
  384:             }
  385:             foreach my $item (@rest) {
  386:                 my ($key,$value) = split(/=/,$item);
  387:                 $form{$key} = &unescape($value);
  388:             }
  389:             if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
  390:                 $form{'origurl'} = $firsturl;
  391:                 $deeplinkurl = $firsturl;
  392:             } elsif ($firsturl eq '/adm/email') {
  393:                 $form{'origurl'} = $firsturl;
  394:             }
  395:             if ($form{'linkprot'}) {
  396:                 $linkprot = $form{'linkprot'};
  397:                 $linkprotuser = $form{'linkprotuser'};
  398:                 $linkprotexit = $form{'linkprotexit'};
  399:                 $linkprotpbid = $form{'linkprotpbid'};
  400:                 $linkprotpburl = $form{'linkprotpburl'};
  401:             } elsif ($form{'linkkey'} ne '') {
  402:                 $linkkey = $form{'linkkey'};
  403:             }
  404:             if ($form{'iptoken'}) {
  405:                 %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  406:                 my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  407:             }
  408:         }
  409:     } elsif ($form{'sso'}) {
  410:         my $lonhost = $r->dir_config('lonHostID');
  411:         my $info = &Apache::lonnet::reply('tmpget:'.$form{'sso'},$lonhost);
  412:         &Apache::lonnet::tmpdel($form{'sso'});
  413:         unless (($info=~/^error/) || ($info eq 'con_lost') ||
  414:                 ($info eq 'no_such_host')) {
  415:             my ($firsturl,@rest)=split(/\&/,$info);
  416:             if ($firsturl ne '') {
  417:                 $form{'origurl'} = &unescape($firsturl);
  418:                 if ($form{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
  419:                     $deeplinkurl = $form{'origurl'};
  420:                 }
  421:             }
  422:             foreach my $item (@rest) {
  423:                 my ($key,$value) = split(/=/,$item);
  424:                 $form{$key} = &unescape($value);
  425:             }
  426:             if ($form{'linkprot'}) {
  427:                 $linkprot = $form{'linkprot'};
  428:                 $linkprotuser = $form{'linkprotuser'};
  429:                 $linkprotexit = $form{'linkprotexit'};
  430:                 $linkprotpbid = $form{'linkprotpbid'};
  431:                 $linkprotpburl = $form{'linkprotpburl'};
  432:             } elsif ($form{'linkkey'} ne '') {
  433:                 $linkkey = $form{'linkkey'};
  434:             }
  435:         }
  436:     } elsif ($form{'ltoken'}) {
  437:         my %link_info = &Apache::lonnet::tmpget($form{'ltoken'});
  438:         $linkprot = $link_info{'linkprot'};
  439:         if ($linkprot) {
  440:             if ($link_info{'linkprotuser'} ne '') {
  441:                 $linkprotuser = $link_info{'linkprotuser'};
  442:             }
  443:             if ($link_info{'linkprotexit'} ne '') {
  444:                 $linkprotexit = $link_info{'linkprotexit'};
  445:             }
  446:             if ($link_info{'linkprotpbid'} ne '') {
  447:                 $linkprotpbid = $link_info{'linkprotpbid'};
  448:             }
  449:             if ($link_info{'linkprotpburl'} ne '') {
  450:                 $linkprotpburl = $link_info{'linkprotpburl'};
  451:             }
  452:         }
  453:         my $delete = &Apache::lonnet::tmpdel($form{'ltoken'});
  454:         delete($form{'ltoken'});
  455:         if ($form{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
  456:             $deeplinkurl = $form{'origurl'};
  457:         }
  458:     } elsif ($form{'linkkey'} ne '') {
  459:         $linkkey = $form{'linkkey'};
  460:     }
  461: 
  462:     my $domain = $r->dir_config('lonSSOUserDomain');
  463:     if ($domain eq '') {
  464:         $domain = $r->dir_config('lonDefDomain');
  465:     }
  466:     if (($deeplinkurl) && ($linkprot) && ($linkprotuser ne '')) {
  467:         unless ($linkprotuser eq $user.':'.$domain) {
  468:             $r->user();
  469:             my %data = (
  470:                            origurl => $deeplinkurl,
  471:                            linkprot => $linkprot,
  472:                            linkprotuser => $linkprotuser,
  473:                            linkprotexit => $linkprotexit,
  474:                            linkprotpbid => $linkprotpbid,
  475:                            linkprotpburl => $linkprotpburl,
  476:                        );
  477:             if ($env{'form.lcssowin'}) {
  478:                 $data{'lcssowin'} = $env{'form.lcssowin'};
  479:             }
  480:             my $token = &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'link');
  481:             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
  482:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  483:                 $r->internal_redirect('/adm/relaunch?rtoken='.$token);
  484:                 $r->set_handlers('PerlHandler'=> undef);
  485:                 return OK;
  486:             }
  487:         }
  488:     }
  489:     my $home=&Apache::lonnet::homeserver($user,$domain);
  490:     if ($home !~ /(con_lost|no_host|no_such_host)/) {
  491: 	&Apache::lonnet::logthis(" SSO authorized user $user ");
  492:         my ($is_balancer,$otherserver,$hosthere);
  493:         if ($form{'iptoken'}) {
  494:             if (($sessiondata{'domain'} eq $domain) &&
  495:                 ($sessiondata{'username'} eq $user)) {
  496:                 $hosthere = 1;
  497:             }
  498:         }
  499:         unless ($hosthere) {
  500:             ($is_balancer,$otherserver) =
  501:                 &Apache::lonnet::check_loadbalancing($user,$domain,'login');
  502:             if ($is_balancer) {
  503:                 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  504:                 my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  505:                 if (($found_server) && ($balancer_cookie =~ /^\Q$domain\E_\Q$user\E_/)) {
  506:                     $otherserver = $found_server;
  507:                 } elsif ($otherserver eq '') {
  508:                     my $lowest_load;
  509:                     ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($domain);
  510:                     if ($lowest_load > 100) {
  511:                         $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$domain);
  512:                     }
  513:                     if ($otherserver ne '') {
  514:                         my @hosts = &Apache::lonnet::current_machine_ids();
  515:                         if (grep(/^\Q$otherserver\E$/,@hosts)) {
  516:                             $hosthere = $otherserver;
  517:                         }
  518:                     }
  519:                 }
  520:             }
  521:         }
  522: 	if (($is_balancer) && (!$hosthere)) {
  523: 	    # login but immediately go to switch server to find us a new 
  524: 	    # machine
  525: 	    &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
  526:             foreach my $item (keys(%form)) {
  527:                 $env{'form.'.$item} = $form{$item};
  528:             }
  529:             unless (($form{'symb'}) || ($form{'origurl'})) {
  530:                 unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
  531:                     $env{'form.origurl'} = $r->uri;
  532:                 }
  533:             }
  534:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
  535:                 $env{'request.deeplink.login'} = $form{'origurl'};
  536:             } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
  537:                 $env{'request.deeplink.login'} = $r->uri;
  538:             }
  539:             if ($env{'request.deeplink.login'}) {
  540:                 if ($linkprot) {
  541:                     $env{'request.linkprot'} = $linkprot;
  542:                     if ($linkprotuser ne '') {
  543:                         $env{'request.linkprotuser'} = $linkprotuser;
  544:                     }
  545:                     if ($linkprotexit ne '') {
  546:                         $env{'request.linkprotexit'} = $linkprotexit;
  547:                     }
  548:                     if ($linkprotpbid ne '') {
  549:                         $env{'request.linkprotpbid'} = $linkprotpbid;
  550:                     }
  551:                     if ($linkprotpburl ne '') {
  552:                         $env{'request.linkprotpburl'} = $linkprotpburl;
  553:                     }
  554:                 } elsif ($linkkey ne '') {
  555:                     $env{'request.linkkey'} = $linkkey;
  556:                 }
  557:             }
  558:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} eq '/adm/email')) {
  559:                 if ($form{'display'} && ($env{'form.mailrecip'} eq $user.':'.$domain)) {
  560:                     $env{'request.display'} = $form{'display'};
  561:                     $env{'request.mailrecip'} = $env{'form.mailrecip'};
  562:                 }
  563:             }
  564:             $env{'request.sso.login'} = 1;
  565:             if (defined($r->dir_config("lonSSOReloginServer"))) {
  566:                 $env{'request.sso.reloginserver'} =
  567:                     $r->dir_config('lonSSOReloginServer');
  568:             }
  569:             my $redirecturl = '/adm/switchserver';
  570:             if ($otherserver ne '') {
  571:                 $redirecturl .= '?otherserver='.$otherserver;
  572:             }
  573:             if ($form{'lcssowin'}) {
  574:                 $redirecturl .= (($redirecturl=~/\?/)?'&':'?') . 'lcssowin=1';
  575:             }
  576: 	    $r->internal_redirect($redirecturl);
  577: 	    $r->set_handlers('PerlHandler'=> undef);
  578: 	} else {
  579: 	    # need to login them in, so generate the need data that
  580: 	    # migrate expects to do login
  581:             my $ip = &Apache::lonnet::get_requestor_ip($r);
  582: 	    my %info=('ip'        => $ip,
  583: 		      'domain'    => $domain,
  584: 		      'username'  => $user,
  585: 		      'server'    => $r->dir_config('lonHostID'),
  586: 		      'sso.login' => 1
  587: 		      );
  588:             foreach my $item ('role','symb','iptoken','origurl','lcssowin') {
  589:                 if (exists($form{$item})) {
  590:                     $info{$item} = $form{$item};
  591:                 } elsif ($sessiondata{$item} ne '') {
  592:                     $info{$item} = $sessiondata{$item};
  593:                 }
  594:             }
  595:             unless (($info{'symb'}) || ($info{'origurl'})) {
  596:                 unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
  597:                     $info{'origurl'} = $r->uri; 
  598:                 }
  599:             }
  600:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
  601:                 $info{'deeplink.login'} = $form{'origurl'};
  602:             } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
  603:                 $info{'deeplink.login'} = $r->uri;
  604:             }
  605:             if ($info{'deeplink.login'}) {
  606:                 if ($linkprot) {
  607:                     $info{'linkprot'} = $linkprot;
  608:                     if ($linkprotuser ne '') {
  609:                         $info{'linkprotuser'} = $linkprotuser;
  610:                     }
  611:                     if ($linkprotexit ne '') {
  612:                         $info{'linkprotexit'} = $linkprotexit;
  613:                     }
  614:                     if ($linkprotpbid ne '') {
  615:                         $info{'linkprotpbid'} = $linkprotpbid;
  616:                     }
  617:                     if ($linkprotpburl ne '') {
  618:                         $info{'linkprotpburl'} = $linkprotpburl;
  619:                     }
  620:                 } elsif ($linkkey ne '') {
  621:                     $info{'linkkey'} = $linkkey;
  622:                 }
  623:             }
  624:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} eq '/adm/email')) {
  625:                 if ($form{'display'} && ($form{'mailrecip'} eq $user.':'.$domain)) {
  626:                     $info{'display'} = &escape($form{'display'});
  627:                     $info{'mailrecip'} = &escape($form{'mailrecip'});
  628:                 }
  629:             }
  630:             if ($r->dir_config("ssodirecturl") == 1) {
  631:                 $info{'origurl'} = $r->uri;
  632:             }
  633:             if (defined($r->dir_config("lonSSOReloginServer"))) {
  634:                 $info{'sso.reloginserver'} = 
  635:                     $r->dir_config('lonSSOReloginServer'); 
  636:             }
  637:             if (($is_balancer) && ($hosthere)) {
  638:                 $info{'noloadbalance'} = $hosthere;
  639:             }
  640: 	    my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'sso');
  641: 	    $env{'form.token'} = $token;
  642: 	    $r->internal_redirect('/adm/migrateuser');
  643: 	    $r->set_handlers('PerlHandler'=> undef);
  644: 	}
  645: 	return OK;
  646:     } else {
  647: 	&Apache::lonnet::logthis(" SSO authorized unknown user $user ");
  648:         my @cancreate;
  649:         my %domconfig =
  650:             &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
  651:         if (ref($domconfig{'usercreation'}) eq 'HASH') {
  652:             if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
  653:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
  654:                     @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
  655:                 } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
  656:                          ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
  657:                     @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
  658:                 }
  659:             }
  660:         }
  661:         if ((grep(/^sso$/,@cancreate)) || (defined($r->dir_config('lonSSOUserUnknownRedirect')))) {
  662:             $r->subprocess_env->set('SSOUserUnknown' => $user);
  663:             $r->subprocess_env->set('SSOUserDomain' => $domain);
  664:             if (grep(/^sso$/,@cancreate)) {
  665: #FIXME - need to preserve origurl, role and symb, or linkprot or linkkey for use after account
  666: # creation. If lcssowin is 1, createaccount needs to close pop-up and display in main window.
  667:                 $r->set_handlers('PerlHandler'=> [\&Apache::createaccount::handler]);
  668:                 $r->handler('perl-script');
  669:             } else {
  670: 	        $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
  671:                 $r->set_handlers('PerlHandler'=> undef);
  672:             }
  673: 	    return OK;
  674:         }
  675:     }
  676:     return undef;
  677: }
  678: 
  679: sub handler {
  680:     my $r = shift;
  681:     my $requrl=$r->uri;
  682: 
  683:     if ($requrl =~ m{^/res/adm/pages/[^/]+\.(gif|png)$}) {
  684:         return OK;
  685:     }
  686: 
  687:     if (&Apache::lonnet::is_domainimage($requrl)) {
  688:         return OK;
  689:     }
  690: 
  691:     my %user;
  692:     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
  693: 
  694:     unless (($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) {
  695:         my $result = &sso_login($r,$handle,$user{'name'});
  696:         if (defined($result)) {
  697: 	    return $result;
  698:         }
  699:     }
  700: 
  701:     my ($is_balancer,$otherserver);
  702: 
  703:     if ($handle eq '') {
  704:         unless ((($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) ||
  705:                 ($requrl =~ m{^/public/$match_domain/$match_courseid/syllabus}) ||
  706:                 ($requrl =~ m{^/adm/help/}) || ($requrl eq '/adm/sso') ||
  707:                 ($requrl =~ m{^/res/$match_domain/$match_username/})) {
  708: 	    $r->log_reason("Cookie not valid", $r->filename);
  709:         }
  710:     } elsif ($handle ne '') {
  711: 
  712: # ------------------------------------------------------ Initialize Environment
  713: 	my $lonidsdir=$r->dir_config('lonIDsDir');
  714: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  715: 
  716: # --------------------------------------------------------- Initialize Language
  717: 
  718: 	&Apache::lonlocal::get_language_handle($r);
  719: 
  720:     }
  721: 
  722: # -------------------------------------------------- Should be a valid user now
  723:     if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
  724: # -------------------------------------------------------------- Resource State
  725: 
  726:         my ($cdom,$cnum);
  727:         if ($env{'request.course.id'}) {
  728:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  729:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  730:         }
  731: 	if ($requrl=~/^\/+(res|uploaded)\//) {
  732: 	    $env{'request.state'} = "published";
  733: 	} else {
  734: 	    $env{'request.state'} = 'unknown';
  735: 	}
  736: 	$env{'request.filename'} = $r->filename;
  737: 	$env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
  738:         my ($suppext,$checkabsolute);
  739:         if ($requrl =~ m{^/adm/wrapper/ext/}) {
  740:             my $query = $r->args;
  741:             if ($query) {
  742:                 my $preserved;
  743:                 foreach my $pair (split(/&/,$query)) {
  744:                     my ($name, $value) = split(/=/,$pair);
  745:                     unless (($name eq 'symb') || ($name eq 'usehttp')) {
  746:                         $preserved .= $pair.'&';
  747:                     }
  748:                     if (($env{'request.course.id'}) && ($name eq 'folderpath')) {
  749:                         if ($value =~ /^supplemental/) {
  750:                             $suppext = 1;
  751:                         }
  752:                     }
  753:                 }
  754:                 $preserved =~ s/\&$//;
  755:                 if ($preserved) {
  756:                     $env{'request.external.querystring'} = $preserved;
  757:                 }
  758:             }
  759:             if ($env{'request.course.id'}) {
  760:                 $checkabsolute = 1;
  761:             }
  762:         } elsif ($env{'request.course.id'} &&
  763:                  (($requrl =~ m{^/adm/$match_domain/$match_username/aboutme$}) ||
  764:                   ($requrl eq "/public/$cdom/$cnum/syllabus") ||
  765:                   ($requrl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}))) {
  766:             my $query = $r->args;
  767:             if ($query) {
  768:                 foreach my $pair (split(/&/,$query)) {
  769:                     my ($name, $value) = split(/=/,$pair);
  770:                     if ($name eq 'folderpath') {
  771:                         if ($value =~ /^supplemental/) {
  772:                             $suppext = 1;
  773:                         }
  774:                         last;
  775:                     }
  776:                 }
  777:             }
  778:             if ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}) {
  779:                 $checkabsolute = 1;
  780:             }
  781:         }
  782:         if ($checkabsolute) {
  783:             my $hostname = $r->hostname();
  784:             my $lonhost = &Apache::lonnet::host_from_dns($hostname);
  785:             if ($lonhost) {
  786:                 my $actual = &Apache::lonnet::absolute_url($hostname,1,1);
  787:                 my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname;
  788:                 unless ($actual eq $expected) {
  789:                     $env{'request.use_absolute'} = $expected;
  790:                 }
  791:             }
  792:         }
  793: # -------------------------------------------------------- Load POST parameters
  794: 
  795: 	&Apache::lonacc::get_posted_cgi($r);
  796: 
  797: # ------------------------------------------------------ Check if load balancer 
  798: 
  799:         my $checkexempt;
  800:         if ($env{'user.loadbalexempt'} eq $r->dir_config('lonHostID')) {
  801:             if ($env{'user.loadbalcheck.time'} + 600 > time) {
  802:                 $checkexempt = 1;
  803:             }
  804:         }
  805:         if ($env{'user.noloadbalance'} eq $r->dir_config('lonHostID')) {
  806:             $checkexempt = 1;
  807:         }
  808:         unless (($checkexempt) || (($requrl eq '/adm/switchserver') && (!$r->is_initial_req()))) {
  809:             ($is_balancer,$otherserver) =
  810:                 &Apache::lonnet::check_loadbalancing($env{'user.name'},
  811:                                                      $env{'user.domain'});
  812:             if ($is_balancer) {
  813:                 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  814:                 my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  815:                 if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
  816:                     $otherserver = $found_server;
  817:                 }
  818:                 unless ($requrl eq '/adm/switchserver') {
  819:                     $r->set_handlers('PerlResponseHandler'=>
  820:                                      [\&Apache::switchserver::handler]);
  821:                 }
  822:                 if ($otherserver ne '') {
  823:                     $env{'form.otherserver'} = $otherserver;
  824:                 }
  825:                 unless (($env{'form.origurl'}) || ($r->uri eq '/adm/roles') ||
  826:                         ($r->uri eq '/adm/switchserver') || ($r->uri eq '/adm/sso')) {
  827:                     $env{'form.origurl'} = $r->uri;
  828:                 }
  829:             }
  830:         }
  831:         if ($requrl=~m{^/+tiny/+$match_domain/+\w+$}) {
  832:             if ($r->args) {
  833:                 &Apache::loncommon::get_unprocessed_cgi($r->args,['ttoken']);
  834:                 if (defined($env{'form.ttoken'})) {
  835:                     my %info = &Apache::lonnet::tmpget($env{'form.ttoken'});
  836:                     if (($info{'origurl'} ne '') && ($info{'origurl'} eq $requrl)) {
  837:                         my %data;
  838:                         if (($info{'linkprotuser'} ne '') && ($info{'linkprot'}) &&
  839:                             ($info{'linkprotuser'} ne $env{'user.name'}.':'.$env{'user.domain'})) {
  840:                             %data = (
  841:                                 origurl => $requrl,
  842:                                 linkprot => $info{'linkprot'},
  843:                                 linkprotuser => $info{'linkprotuser'},
  844:                                 linkprotexit => $info{'linkprotexit'},
  845:                                 linkprotpbid => $info{'linkprotpbid'},
  846:                                 linkprotpburl => $info{'linkprotpburl'},
  847:                             );
  848:                         } elsif ($info{'ltoken'} ne '') {
  849:                             my %ltoken_info = &Apache::lonnet::tmpget($info{'ltoken'});
  850:                             if (($ltoken_info{'linkprotuser'} ne '') && ($ltoken_info{'linkprot'}) &&
  851:                                 ($ltoken_info{'linkprotuser'} ne $env{'user.name'}.':'.$env{'user.domain'})) {
  852:                                 %data = (
  853:                                     origurl => $requrl,
  854:                                     linkprot => $ltoken_info{'linkprot'},
  855:                                     linkprotuser => $ltoken_info{'linkprotuser'},
  856:                                     linkprotexit => $ltoken_info{'linkprotexit'},
  857:                                     linkprotpbid => $ltoken_info{'linkprotpbid'},
  858:                                     linkprotpburl => $ltoken_info{'linkprotpburl'},
  859:                                 );
  860:                             }
  861:                         }
  862:                         if (keys(%data)) {
  863:                             my $delete = &Apache::lonnet::tmpdel($env{'form.ttoken'});
  864:                             if ($info{'ltoken'} ne '') {
  865:                                 my $delete = &Apache::lonnet::tmpdel($info{'ltoken'});
  866:                             }
  867:                             my $token =
  868:                                 &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'retry');
  869:                             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
  870:                                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  871:                                 $r->internal_redirect('/adm/relaunch?rtoken='.$token);
  872:                                 $r->set_handlers('PerlHandler'=> undef);
  873:                                 return OK;
  874:                             }
  875:                         }
  876:                     }
  877:                 }
  878:             }
  879:             if ($env{'user.name'} eq 'public' &&
  880:                 $env{'user.domain'} eq 'public') {
  881:                 $env{'request.firsturl'}=$requrl;
  882:                 return FORBIDDEN;
  883:             }
  884:             return OK;
  885:         }
  886: # ---------------------------------------------------------------- Check access
  887: 	my $now = time;
  888:         my ($check_symb,$check_access,$check_block,$access,$poss_symb);
  889: 	if ($requrl !~ m{^/(?:adm|public|(?:prt|zip)spool)/}
  890: 	    || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
  891:             $check_access = 1;
  892:         }
  893:         if ((!$check_access) && ($env{'request.course.id'})) {
  894:             if (($requrl eq '/adm/viewclasslist') ||
  895:                 ($requrl =~ m{^(/adm/wrapper|)\Q/uploaded/$cdom/$cnum/docs/\E}) ||
  896:                 ($requrl =~ m{^/adm/.*/aboutme$}) ||
  897:                 ($requrl=~m{^/adm/coursedocs/showdoc/}) ||
  898:                 ($requrl=~m{^(/adm/wrapper|)/adm/$cdom/$cnum/\d+/ext\.tool$})) {
  899:                 $check_block = 1;
  900:             }
  901:         }
  902:         if (($env{'request.course.id'}) && (!$suppext)) {
  903:             $requrl=~/\.(\w+)$/;
  904:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  905:                 ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) ||
  906:                 ($requrl=~/^\/adm\/wrapper\//) ||
  907:                 ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
  908:                 ($requrl=~m|\.problem/smpedit$|) ||
  909:                 ($requrl=~/^\/public\/.*\/syllabus$/) ||
  910:                 ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) ||
  911:                 ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/) ||
  912:                 ($requrl=~m{^/adm/$cdom/$cnum/\d+/ext\.tool$})) {
  913:                 $check_symb = 1;
  914:             }
  915:         }
  916:         if (($check_access) || ($check_block)) {
  917:             if ($check_symb) {
  918:                 if ($env{'form.symb'}) {
  919:                     $poss_symb=&Apache::lonnet::symbclean($env{'form.symb'});
  920:                 } elsif (($env{'request.course.id'}) && ($r->args ne '')) {
  921:                     my $query = $r->args;
  922:                     foreach my $pair (split(/&/,$query)) {
  923:                         my ($name, $value) = split(/=/,$pair);
  924:                         $name = &unescape($name);
  925:                         $value =~ tr/+/ /;
  926:                         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  927:                         if ($name eq 'symb') {
  928:                             $poss_symb = &Apache::lonnet::symbclean($value);
  929:                             last;
  930:                         }
  931:                     }
  932:                 }
  933:                 if ($poss_symb) {
  934:                     my ($possmap,$resid,$url)=&Apache::lonnet::decode_symb($poss_symb);
  935:                     $url = &Apache::lonnet::clutter($url);
  936:                     my $toplevelmap = $env{'course.'.$env{'request.course.id'}.'.url'};
  937:                     unless (($url eq $requrl) && (($possmap eq $toplevelmap) ||
  938:                                                   (&Apache::lonnet::is_on_map($possmap)))) {
  939:                         undef($poss_symb);
  940:                     }
  941:                     if ($poss_symb) {
  942:                         if ((!$env{'request.role.adv'}) && ($env{'acc.randomout'}) &&
  943:                             ($env{'acc.randomout'}=~/\&\Q$poss_symb\E\&/)) {
  944:                             undef($poss_symb);
  945:                         } elsif ((!$env{'request.role.adv'}) && ($env{'acc.deeplinkout'}) &&
  946:                                  ($env{'acc.deeplinkout'}=~/\&\Q$poss_symb\E\&/)) {
  947:                             undef($poss_symb);
  948:                         }
  949:                     }
  950:                 }
  951:                 if ($poss_symb) {
  952:                     $access=&Apache::lonnet::allowed('bre',$requrl,$poss_symb);
  953:                 } else {
  954:                     $access=&Apache::lonnet::allowed('bre',$requrl,'','','','',1);
  955:                 }
  956:             } else {
  957:                 my $nodeeplinkcheck;
  958:                 if (($check_access) && ($requrl =~ /\.(sequence|page)$/)) {
  959:                     unless ($env{'form.navmap'}) {
  960:                         if ($r->args ne '') {
  961:                             &Apache::loncommon::get_unprocessed_cgi($r->args,['navmap']);
  962:                             unless ($env{'form.navmap'}) {
  963:                                 $nodeeplinkcheck = 1;
  964:                             }
  965:                         }
  966:                     }
  967:                 }
  968:                 my $clientip = &Apache::lonnet::get_requestor_ip($r);
  969:                 $access=&Apache::lonnet::allowed('bre',$requrl,'','',$clientip,'','',$nodeeplinkcheck);
  970:             }
  971:         }
  972:         if ($check_block) {
  973:             if ($access eq 'B') {
  974:                 if ($poss_symb) {
  975:                     if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
  976:                         $env{'request.symb'} = $poss_symb;
  977:                     }
  978:                 }
  979:                 &Apache::blockedaccess::setup_handler($r);
  980:                 return OK;
  981:             }
  982:         } elsif ($check_access) {
  983:             if ($handle eq '') {
  984:                 unless ($access eq 'F') {
  985:                     if ($requrl =~ m{^/res/$match_domain/$match_username/}) {
  986:                         $r->log_reason("Cookie not valid", $r->filename);
  987:                     }
  988:                 }
  989:             }
  990: 	    if ($access eq '1') {
  991: 		$env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  992: 		return HTTP_NOT_ACCEPTABLE; 
  993: 	    }
  994: 	    if ($access eq 'A') {
  995: 		&Apache::restrictedaccess::setup_handler($r);
  996: 		return OK;
  997: 	    }
  998:             if ($access eq 'B') {
  999:                 if ($poss_symb) {
 1000:                     if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
 1001:                         $env{'request.symb'} = $poss_symb;
 1002:                     }
 1003:                 }
 1004:                 &Apache::blockedaccess::setup_handler($r);
 1005:                 return OK;
 1006:             }
 1007:             if ($access eq 'D') {
 1008:                 &Apache::lonprotected::setup_handler($r);
 1009:                 return OK;
 1010:             }
 1011: 	    if (($access ne '2') && ($access ne 'F')) {
 1012:                 if ($requrl =~ m{^/res/}) {
 1013:                     $access = &Apache::lonnet::allowed('bro',$requrl);
 1014:                     if ($access ne 'F') {
 1015:                         if ($requrl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
 1016:                             $access = &Apache::lonnet::allowed('bre','/res/lib/templates/simpleproblem.problem');
 1017:                             if ($access ne 'F') {
 1018:                                 $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1019:                                 return HTTP_NOT_ACCEPTABLE;
 1020:                             }
 1021:                         } else {
 1022:                             $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1023:                             return HTTP_NOT_ACCEPTABLE;
 1024:                         }
 1025:                     }
 1026:                 } elsif (($handle =~ /^publicuser_\d+$/) && (&Apache::lonnet::is_portfolio_url($requrl))) {
 1027:                     my $clientip = &Apache::lonnet::get_requestor_ip($r);
 1028:                     if (&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip) ne 'F') {
 1029:                         $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1030:                         return HTTP_NOT_ACCEPTABLE;
 1031:                     }
 1032:                 } else {
 1033: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1034: 		    return HTTP_NOT_ACCEPTABLE;
 1035:                 }
 1036: 	    }
 1037: 	}
 1038: 	if ($requrl =~ m|^/prtspool/|) {
 1039: 	    my $start='/prtspool/'.$env{'user.name'}.'_'.
 1040: 		$env{'user.domain'};
 1041: 	    if ($requrl !~ /^\Q$start\E/) {
 1042: 		$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1043: 		return HTTP_NOT_ACCEPTABLE;
 1044: 	    }
 1045: 	}
 1046: 	if ($requrl =~ m|^/zipspool/|) {
 1047: 	    my $start='/zipspool/zipout/'.$env{'user.name'}.":".
 1048: 		$env{'user.domain'};
 1049: 	    if ($requrl !~ /^\Q$start\E/) {
 1050: 		$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1051: 		return HTTP_NOT_ACCEPTABLE;
 1052: 	    }
 1053: 	}
 1054: 	if ($env{'user.name'} eq 'public' && 
 1055: 	    $env{'user.domain'} eq 'public' &&
 1056: 	    $requrl !~ m{^/+(res|public|uploaded)/} &&
 1057: 	    $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
 1058:             $requrl !~ m{^/adm/blockingstatus/.*$} &&
 1059: 	    $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
 1060: 	    $env{'request.querystring'}=$r->args;
 1061: 	    $env{'request.firsturl'}=$requrl;
 1062: 	    return FORBIDDEN;
 1063: 	}
 1064: # ------------------------------------------------------------- This is allowed
 1065: 	if ($env{'request.course.id'}) {
 1066: 	    &Apache::lonnet::countacc($requrl);
 1067:             my $query=$r->args;
 1068:             if ($check_symb) {
 1069: # ------------------------------------- This is serious stuff, get symb and log
 1070: 		my $symb;
 1071: 		if ($query) {
 1072: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb','folderpath']);
 1073: 		}
 1074: 		if ($env{'form.symb'}) {
 1075: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
 1076:                     if (($requrl eq '/adm/navmaps') ||
 1077:                         ($requrl =~ m{^/adm/wrapper/}) ||
 1078:                         ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
 1079:                         unless (&Apache::lonnet::symbverify($symb,$requrl)) {
 1080:                             if (&Apache::lonnet::is_on_map($requrl)) {
 1081:                                 $symb = &Apache::lonnet::symbread($requrl);
 1082:                                 unless (&Apache::lonnet::symbverify($symb,$requrl)) {
 1083:                                     undef($symb);
 1084:                                 }
 1085:                             }
 1086:                         }
 1087:                         if ($symb) {
 1088:                             if ($requrl eq '/adm/navmaps') {
 1089:                                 my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
 1090:                                 &Apache::lonnet::symblist($map,$murl => [$murl,$mid]);
 1091:                             } elsif (($requrl =~ m{^/adm/wrapper/}) ||
 1092:                                      ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
 1093:                                 my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
 1094:                                 if ($map =~ /\.page$/) {
 1095:                                     my $mapsymb = &Apache::lonnet::symbread($map);
 1096:                                     ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1097:                                 }
 1098:                                 &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
 1099:                                                           'last_known' =>[$murl,$mid]);
 1100:                             }
 1101:                         }
 1102: 		    } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
 1103: 			     (($requrl=~m|(.*)/smpedit$|) &&
 1104: 			      &Apache::lonnet::symbverify($symb,$1)) ||
 1105:                              (($requrl=~m|(.*/aboutme)/portfolio$|) &&
 1106:                               &Apache::lonnet::symbverify($symb,$1))) {
 1107: 			my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
 1108:                         if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
 1109:                             my $mapsymb = &Apache::lonnet::symbread($map);
 1110:                             ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1111:                         }
 1112: 			&Apache::lonnet::symblist($map,$murl => [$murl,$mid],
 1113: 						  'last_known' =>[$murl,$mid]);
 1114: 		    } else {
 1115: 			$r->log_reason('Invalid symb for '.$requrl.': '.
 1116: 				       $symb);
 1117: 			$env{'user.error.msg'}=
 1118: 			    "$requrl:bre:1:1:Invalid Access";
 1119: 			return HTTP_NOT_ACCEPTABLE; 
 1120: 		    }
 1121: 		} else {
 1122:                     if ($requrl=~m{^(/adm/.*/aboutme)/portfolio$}) {
 1123:                         $requrl = $1;
 1124:                     }
 1125: 		    $symb=&Apache::lonnet::symbread($requrl);
 1126:                     if (&Apache::lonnet::is_on_map($requrl) && $symb) {
 1127:                         my ($encstate,$invalidsymb);
 1128:                         unless (&Apache::lonnet::symbverify($symb,$requrl,\$encstate)) {
 1129:                             $invalidsymb = 1;
 1130:                             #
 1131:                             # If $env{'request.enc'} inconsistent with encryption expected for $symb
 1132:                             # retrieved by lonnet::symbread(), call again to check for an instance of
 1133:                             # $requrl in the course for which expected encryption matches request.enc.
 1134:                             # If symb for different instance passes lonnet::symbverify(), use that as
 1135:                             # the symb for $requrl and call &Apache::lonnet::allowed() for that symb.
 1136:                             # Report invalid symb if there is no other symb. Redirect to /adm/ambiguous
 1137:                             # if multiple possible symbs consistent with request.enc available for $requrl.
 1138:                             #
 1139:                             if (($env{'request.enc'} && !$encstate) || (!$env{'request.enc'} && $encstate)) {
 1140:                                 my %possibles;
 1141:                                 my $nocache = 1;
 1142:                                 my $oldsymb = $symb;
 1143:                                 $symb = &Apache::lonnet::symbread($requrl,'','','',\%possibles,$nocache);
 1144:                                 if (($symb) && ($symb ne $oldsymb)) {
 1145:                                     if (&Apache::lonnet::symbverify($symb,$requrl)) {
 1146:                                         my $access=&Apache::lonnet::allowed('bre',$requrl,$symb);
 1147:                                         if ($access eq 'B') {
 1148:                                             $env{'request.symb'} = $symb;
 1149:                                             &Apache::blockedaccess::setup_handler($r);
 1150:                                             return OK;
 1151:                                         } elsif (($access eq '2') || ($access eq 'F')) {
 1152:                                             $invalidsymb = '';
 1153:                                         }
 1154:                                     }
 1155:                                 } elsif (keys(%possibles) > 1) {
 1156:                                     $r->internal_redirect('/adm/ambiguous');
 1157:                                     return OK;
 1158:                                 }
 1159:                             }
 1160:                             if ($invalidsymb) {
 1161:                                 if ($requrl eq '/adm/navmaps') {
 1162:                                     undef($symb);
 1163:                                 } else {
 1164:                                     $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
 1165:                                     $env{'user.error.msg'}=
 1166:                                         "$requrl:bre:1:1:Invalid Access";
 1167:                                     return HTTP_NOT_ACCEPTABLE;
 1168:                                 }
 1169:                             }
 1170:                         }
 1171:                     }
 1172: 		    if ($symb) {
 1173: 			my ($map,$mid,$murl)=
 1174: 			    &Apache::lonnet::decode_symb($symb);
 1175:                         if ($requrl eq '/adm/navmaps') {
 1176:                             &Apache::lonnet::symblist($map,$murl =>[$murl,$mid]);
 1177:                         } else {
 1178:                             if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
 1179:                                 my $mapsymb = &Apache::lonnet::symbread($map);
 1180:                                 ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1181:                             }
 1182:                             &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
 1183:                                                       'last_known' =>[$murl,$mid]);
 1184:                         }
 1185: 		    }
 1186: 		}
 1187: 		$env{'request.symb'}=$symb;
 1188:                 if (($env{'request.symbread.cached.'}) && ($env{'request.symbread.cached.'} ne $symb)) {
 1189:                     $env{'request.symbread.cached.'} = $symb;
 1190:                 }
 1191: 		&Apache::lonnet::courseacclog($symb);
 1192: 	    } else {
 1193: # ------------------------------------------------------- This is other content
 1194: 		&Apache::lonnet::courseacclog($requrl);    
 1195: 	    }
 1196:             if ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/.+\.html?$}) {
 1197:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1198:                     if ($query) {
 1199:                         &Apache::loncommon::get_unprocessed_cgi($query,['forceedit']);
 1200:                         if ($env{'form.forceedit'}) {
 1201:                             $env{'request.state'} = 'edit';
 1202:                         }
 1203:                     }
 1204:                 }
 1205:             } elsif ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/portfolio/syllabus/.+\.html?$}) {
 1206:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1207:                     if ($query) {
 1208:                         &Apache::loncommon::get_unprocessed_cgi($query,['forceedit','editmode']);
 1209:                         if (($env{'form.forceedit'}) || ($env{'form.editmode'})) {
 1210:                             $env{'request.state'} = 'edit';
 1211:                         }
 1212:                     }
 1213:                 }
 1214:             }
 1215: 	}
 1216: 	return OK;
 1217:     } else {
 1218:         my $defdom=$r->dir_config('lonDefDomain');
 1219:         ($is_balancer,$otherserver) =
 1220:             &Apache::lonnet::check_loadbalancing(undef,$defdom);
 1221:         if ($is_balancer) {
 1222:             $r->set_handlers('PerlResponseHandler'=>
 1223:                              [\&Apache::switchserver::handler]);
 1224:             if ($otherserver ne '') {
 1225:                 $env{'form.otherserver'} = $otherserver;
 1226:             }
 1227:         }
 1228:     }
 1229: # -------------------------------------------- See if this is a public resource
 1230:     if ($requrl=~m|^/+adm/+help/+|) {
 1231:  	return OK;
 1232:     }
 1233: # ------------------------------------ See if this is a viewable portfolio file
 1234:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
 1235:         my $clientip = &Apache::lonnet::get_requestor_ip($r);
 1236: 	my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip);
 1237: 	if ($access eq 'A') {
 1238: 	    &Apache::restrictedaccess::setup_handler($r);
 1239: 	    return OK;
 1240: 	}
 1241: 	if (($access ne '2') && ($access ne 'F')) {
 1242: 	    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1243: 	    return HTTP_NOT_ACCEPTABLE;
 1244: 	}
 1245:     }
 1246: 
 1247: # -------------------------------------------------------------- Not authorized
 1248:     $requrl=~/\.(\w+)$/;
 1249: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
 1250: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
 1251: #        ($requrl=~m|^/prtspool/|)) {
 1252: # -------------------------- Store where they wanted to go and get login screen
 1253: 	$env{'request.querystring'}=$r->args;
 1254: 	$env{'request.firsturl'}=$requrl;
 1255:        return FORBIDDEN;
 1256: #   } else {
 1257: # --------------------------------------------------------------------- Goodbye
 1258: #       return HTTP_BAD_REQUEST;
 1259: #   }
 1260: }
 1261: 
 1262: 1;
 1263: __END__
 1264: 
 1265: =pod
 1266: 
 1267: =back
 1268: 
 1269: =cut
 1270: 

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