File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.201: download - view: text, annotated - select for diffs
Tue Nov 30 15:55:40 2021 UTC (2 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6955 IP-based blocking. Pass user'sIP address as third arg to
  loncommon::blockcheck() and second to loncommon::blocking_status().

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

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