File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.210: download - view: text, annotated - select for diffs
Mon Aug 28 20:40:00 2023 UTC (8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Use standard LON-CAPA error documents for 406, 403 and 500 errors for
  /daxeopen/.
- Use loncacc.pm as authz handler for /daxeopen/priv/domain/username/
- Use lonacc.pm as authz handler for /daxeopen/, /daxeopen/priv/, and
  /daxeopen/priv/domain/

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

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