File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.204: download - view: text, annotated - select for diffs
Mon Jun 20 15:07:21 2022 UTC (22 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6907
  For LTI-protected deep links in which username is included in launch payload
  compare username in payload with username for any existing LON-CAPA session
  in current web browser and expire old session, if different user.

    1: # The LearningOnline Network
    2: # Cookie Based Access Handler
    3: #
    4: # $Id: lonacc.pm,v 1.204 2022/06/20 15:07:21 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');
  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,$linkkey,$deeplinkurl);
  337: 
  338: #
  339: # If Shibboleth auth is in use, and a dual SSO and non-SSO login page
  340: # is in use, then the query string will contain the logtoken item with
  341: # a value set to the name of a .tmp file in /home/httpd/perl/tmp
  342: # containing the url to display after authentication, and also,
  343: # optionally, role and symb, or linkprot or linkkey (deep-link access).
  344: #
  345: # If Shibboleth auth is in use, but a dual log-in page is not in use,
  346: # and the originally requested URL was /tiny/$domain/$id (i.e.,
  347: # for deeplinking), then the query string will contain the sso item
  348: # with a value set to the name of a .tmp file in /home/httpd/perl/tmp
  349: # containing the url to display after authentication, and also,
  350: # optionally, linkprot or linkkey (deep-link access).
  351: #
  352: # Otherwise the query string may contain role and symb, or if the
  353: # originally requested URL was /tiny/$domain/$id (i.e. for deeplinking)
  354: # then the query string may contain a ttoken item with a value set
  355: # to the name of a .tmp file in /home/httpd/perl/tmp containing either
  356: # linkprot or linkkey (deep-link access).
  357: #
  358: # If deep-linked, i.e., the originally requested URL was /tiny/$domain/$id
  359: # the linkkey may have originally been sent in POSTed data, which will
  360: # have been processed in lontrans.pm
  361: #
  362: 
  363:     if ($form{'ttoken'}) {
  364:         my %info = &Apache::lonnet::tmpget($form{'ttoken'});
  365:         &Apache::lonnet::tmpdel($form{'ttoken'});
  366:         if ($info{'origurl'}) {
  367:             $form{'origurl'} = $info{'origurl'};
  368:             if ($form{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
  369:                 $deeplinkurl = $form{'origurl'};
  370:             }
  371:         }
  372:         if ($info{'linkprot'}) {
  373:             $linkprot = $info{'linkprot'};
  374:             $linkprotuser = $info{'linkprotuser'};
  375:         } elsif ($info{'linkkey'} ne '') {
  376:             $linkkey = $info{'linkkey'};
  377:         }
  378:     } elsif ($form{'logtoken'}) {
  379:         my ($firsturl,@rest);
  380:         my $lonhost = $r->dir_config('lonHostID');
  381:         my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},$lonhost);
  382:         my $delete = &Apache::lonnet::tmpdel($form{'logtoken'});
  383:         unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
  384:                 ($tmpinfo eq 'no_such_host')) {
  385:             (undef,$firsturl,@rest) = split(/&/,$tmpinfo);
  386:             if ($firsturl ne '') {
  387:                 $firsturl = &unescape($firsturl);
  388:             }
  389:             foreach my $item (@rest) {
  390:                 my ($key,$value) = split(/=/,$item);
  391:                 $form{$key} = &unescape($value);
  392:             }
  393:             if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
  394:                 $form{'origurl'} = $firsturl;
  395:                 $deeplinkurl = $firsturl;
  396:             }
  397:             if ($form{'linkprot'}) {
  398:                 $linkprot = $form{'linkprot'};
  399:                 $linkprotuser = $form{'linkprotuser'};
  400:             } elsif ($form{'linkkey'} ne '') {
  401:                 $linkkey = $form{'linkkey'};
  402:             }
  403:             if ($form{'iptoken'}) {
  404:                 %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
  405:                 my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
  406:             }
  407:         }
  408:     } elsif ($form{'sso'}) {
  409:         my $lonhost = $r->dir_config('lonHostID');
  410:         my $info = &Apache::lonnet::reply('tmpget:'.$form{'sso'},$lonhost);
  411:         &Apache::lonnet::tmpdel($form{'sso'});
  412:         unless (($info=~/^error/) || ($info eq 'con_lost') ||
  413:                 ($info eq 'no_such_host')) {
  414:             my ($firsturl,@rest)=split(/\&/,$info);
  415:             if ($firsturl ne '') {
  416:                 $form{'origurl'} = &unescape($firsturl);
  417:                 if ($form{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
  418:                     $deeplinkurl = $form{'origurl'};
  419:                 }
  420:             }
  421:             foreach my $item (@rest) {
  422:                 my ($key,$value) = split(/=/,$item);
  423:                 $form{$key} = &unescape($value);
  424:             }
  425:             if ($form{'linkprot'}) {
  426:                 $linkprot = $form{'linkprot'};
  427:                 $linkprotuser = $form{'linkprotuser'};
  428:             } elsif ($form{'linkkey'} ne '') {
  429:                 $linkkey = $form{'linkkey'};
  430:             }
  431:         }
  432:     } elsif ($form{'ltoken'}) {
  433:         my %link_info = &Apache::lonnet::tmpget($form{'ltoken'});
  434:         $linkprot = $link_info{'linkprot'};
  435:         if (($linkprot) && ($link_info{'linkprotuser'} ne '')) {
  436:             $linkprotuser = $link_info{'linkprotuser'};    
  437:         }
  438:         my $delete = &Apache::lonnet::tmpdel($form{'ltoken'});
  439:         delete($form{'ltoken'});
  440:         if ($form{'origurl'} =~ m{^/tiny/$match_domain/\w+$}) {
  441:             $deeplinkurl = $form{'origurl'};
  442:         }
  443:     } elsif ($form{'linkkey'} ne '') {
  444:         $linkkey = $form{'linkkey'};
  445:     }
  446: 
  447:     my $domain = $r->dir_config('lonSSOUserDomain');
  448:     if ($domain eq '') {
  449:         $domain = $r->dir_config('lonDefDomain');
  450:     }
  451:     if (($deeplinkurl) && ($linkprot) && ($linkprotuser ne '')) {
  452:         unless ($linkprotuser eq $user.':'.$domain) {
  453:             $r->user();
  454:             my %data = (
  455:                            origurl => $deeplinkurl,
  456:                            linkprot => $linkprot,
  457:                            linkprotuser => $linkprotuser,
  458:                        );
  459:             my $token = &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'link');
  460:             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
  461:                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  462:                 $r->internal_redirect('/adm/relaunch?rtoken='.$token);
  463:                 $r->set_handlers('PerlHandler'=> undef);
  464:                 return OK;
  465:             }
  466:         }
  467:     }
  468:     my $home=&Apache::lonnet::homeserver($user,$domain);
  469:     if ($home !~ /(con_lost|no_host|no_such_host)/) {
  470: 	&Apache::lonnet::logthis(" SSO authorized user $user ");
  471:         my ($is_balancer,$otherserver,$hosthere);
  472:         if ($form{'iptoken'}) {
  473:             if (($sessiondata{'domain'} eq $domain) &&
  474:                 ($sessiondata{'username'} eq $user)) {
  475:                 $hosthere = 1;
  476:             }
  477:         }
  478:         unless ($hosthere) {
  479:             ($is_balancer,$otherserver) =
  480:                 &Apache::lonnet::check_loadbalancing($user,$domain,'login');
  481:             if ($is_balancer) {
  482:                 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  483:                 my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  484:                 if (($found_server) && ($balancer_cookie =~ /^\Q$domain\E_\Q$user\E_/)) {
  485:                     $otherserver = $found_server;
  486:                 } elsif ($otherserver eq '') {
  487:                     my $lowest_load;
  488:                     ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($domain);
  489:                     if ($lowest_load > 100) {
  490:                         $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$domain);
  491:                     }
  492:                     if ($otherserver ne '') {
  493:                         my @hosts = &Apache::lonnet::current_machine_ids();
  494:                         if (grep(/^\Q$otherserver\E$/,@hosts)) {
  495:                             $hosthere = $otherserver;
  496:                         }
  497:                     }
  498:                 }
  499:             }
  500:         }
  501: 	if (($is_balancer) && (!$hosthere)) {
  502: 	    # login but immediately go to switch server to find us a new 
  503: 	    # machine
  504: 	    &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
  505:             foreach my $item (keys(%form)) {
  506:                 $env{'form.'.$item} = $form{$item};
  507:             }
  508:             unless (($form{'symb'}) || ($form{'origurl'})) {
  509:                 unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
  510:                     $env{'form.origurl'} = $r->uri;
  511:                 }
  512:             }
  513:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
  514:                 $env{'request.deeplink.login'} = $form{'origurl'};
  515:             } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
  516:                 $env{'request.deeplink.login'} = $r->uri;
  517:             }
  518:             if ($env{'request.deeplink.login'}) {
  519:                 if ($linkprot) {
  520:                     $env{'request.linkprot'} = $linkprot;
  521:                     if ($linkprotuser ne '') {
  522:                         $env{'request.linkprotuser'} = $linkprotuser;
  523:                     }
  524:                 } elsif ($linkkey ne '') {
  525:                     $env{'request.linkkey'} = $linkkey;
  526:                 }
  527:             }
  528:             $env{'request.sso.login'} = 1;
  529:             if (defined($r->dir_config("lonSSOReloginServer"))) {
  530:                 $env{'request.sso.reloginserver'} =
  531:                     $r->dir_config('lonSSOReloginServer');
  532:             }
  533:             my $redirecturl = '/adm/switchserver';
  534:             if ($otherserver ne '') {
  535:                 $redirecturl .= '?otherserver='.$otherserver;
  536:             }
  537: 	    $r->internal_redirect($redirecturl);
  538: 	    $r->set_handlers('PerlHandler'=> undef);
  539: 	} else {
  540: 	    # need to login them in, so generate the need data that
  541: 	    # migrate expects to do login
  542: 	    my $ip = &Apache::lonnet::get_requestor_ip($r);
  543: 	    my %info=('ip'        => $ip,
  544: 		      'domain'    => $domain,
  545: 		      'username'  => $user,
  546: 		      'server'    => $r->dir_config('lonHostID'),
  547: 		      'sso.login' => 1
  548: 		      );
  549:             foreach my $item ('role','symb','iptoken','origurl') {
  550:                 if (exists($form{$item})) {
  551:                     $info{$item} = $form{$item};
  552:                 } elsif ($sessiondata{$item} ne '') {
  553:                     $info{$item} = $sessiondata{$item};
  554:                 }
  555:             }
  556:             unless (($info{'symb'}) || ($info{'origurl'})) {
  557:                 unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
  558:                     $info{'origurl'} = $r->uri; 
  559:                 }
  560:             }
  561:             if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
  562:                 $info{'deeplink.login'} = $form{'origurl'};
  563:             } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
  564:                 $info{'deeplink.login'} = $r->uri;
  565:             }
  566:             if ($info{'deeplink.login'}) {
  567:                 if ($linkprot) {
  568:                     $info{'linkprot'} = $linkprot;
  569:                 } elsif ($linkkey ne '') {
  570:                     $info{'linkkey'} = $linkkey;
  571:                 }
  572:             }
  573:             if ($r->dir_config("ssodirecturl") == 1) {
  574:                 $info{'origurl'} = $r->uri;
  575:             }
  576:             if (defined($r->dir_config("lonSSOReloginServer"))) {
  577:                 $info{'sso.reloginserver'} = 
  578:                     $r->dir_config('lonSSOReloginServer'); 
  579:             }
  580:             if (($is_balancer) && ($hosthere)) {
  581:                 $info{'noloadbalance'} = $hosthere;
  582:             }
  583: 	    my $token = &Apache::lonnet::tmpput(\%info,$r->dir_config('lonHostID'),'sso');
  584: 	    $env{'form.token'} = $token;
  585: 	    $r->internal_redirect('/adm/migrateuser');
  586: 	    $r->set_handlers('PerlHandler'=> undef);
  587: 	}
  588: 	return OK;
  589:     } else {
  590: 	&Apache::lonnet::logthis(" SSO authorized unknown user $user ");
  591:         my @cancreate;
  592:         my %domconfig =
  593:             &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
  594:         if (ref($domconfig{'usercreation'}) eq 'HASH') {
  595:             if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
  596:                 if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
  597:                     @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
  598:                 } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') && 
  599:                          ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
  600:                     @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
  601:                 }
  602:             }
  603:         }
  604:         if ((grep(/^sso$/,@cancreate)) || (defined($r->dir_config('lonSSOUserUnknownRedirect')))) {
  605:             $r->subprocess_env->set('SSOUserUnknown' => $user);
  606:             $r->subprocess_env->set('SSOUserDomain' => $domain);
  607:             if (grep(/^sso$/,@cancreate)) {
  608: #FIXME - need to preserve origurl, role and symb, or linkprot or linkkey for use after account
  609: # creation
  610:                 $r->set_handlers('PerlHandler'=> [\&Apache::createaccount::handler]);
  611:                 $r->handler('perl-script');
  612:             } else {
  613: 	        $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
  614:                 $r->set_handlers('PerlHandler'=> undef);
  615:             }
  616: 	    return OK;
  617:         }
  618:     }
  619:     return undef;
  620: }
  621: 
  622: sub handler {
  623:     my $r = shift;
  624:     my $requrl=$r->uri;
  625: 
  626:     if ($requrl =~ m{^/res/adm/pages/[^/]+\.(gif|png)$}) {
  627:         return OK;
  628:     }
  629: 
  630:     if (&Apache::lonnet::is_domainimage($requrl)) {
  631:         return OK;
  632:     }
  633: 
  634:     my %user;
  635:     my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
  636: 
  637:     unless (($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) {
  638:         my $result = &sso_login($r,$handle,$user{'name'});
  639:         if (defined($result)) {
  640: 	    return $result;
  641:         }
  642:     }
  643: 
  644:     my ($is_balancer,$otherserver);
  645: 
  646:     if ($handle eq '') {
  647:         unless ((($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) ||
  648:                 ($requrl =~ m{^/public/$match_domain/$match_courseid/syllabus}) ||
  649:                 ($requrl =~ m{^/adm/help/}) || ($requrl eq '/adm/sso') ||
  650:                 ($requrl =~ m{^/res/$match_domain/$match_username/})) {
  651: 	    $r->log_reason("Cookie not valid", $r->filename);
  652:         }
  653:     } elsif ($handle ne '') {
  654: 
  655: # ------------------------------------------------------ Initialize Environment
  656: 	my $lonidsdir=$r->dir_config('lonIDsDir');
  657: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
  658: 
  659: # --------------------------------------------------------- Initialize Language
  660: 
  661: 	&Apache::lonlocal::get_language_handle($r);
  662: 
  663:     }
  664: 
  665: # -------------------------------------------------- Should be a valid user now
  666:     if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
  667: # -------------------------------------------------------------- Resource State
  668: 
  669:         my ($cdom,$cnum);
  670:         if ($env{'request.course.id'}) {
  671:             $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  672:             $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  673:         }
  674: 	if ($requrl=~/^\/+(res|uploaded)\//) {
  675: 	    $env{'request.state'} = "published";
  676: 	} else {
  677: 	    $env{'request.state'} = 'unknown';
  678: 	}
  679: 	$env{'request.filename'} = $r->filename;
  680: 	$env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
  681:         my ($suppext,$checkabsolute);
  682:         if ($requrl =~ m{^/adm/wrapper/ext/}) {
  683:             my $query = $r->args;
  684:             if ($query) {
  685:                 my $preserved;
  686:                 foreach my $pair (split(/&/,$query)) {
  687:                     my ($name, $value) = split(/=/,$pair);
  688:                     unless (($name eq 'symb') || ($name eq 'usehttp')) {
  689:                         $preserved .= $pair.'&';
  690:                     }
  691:                     if (($env{'request.course.id'}) && ($name eq 'folderpath')) {
  692:                         if ($value =~ /^supplemental/) {
  693:                             $suppext = 1;
  694:                         }
  695:                     }
  696:                 }
  697:                 $preserved =~ s/\&$//;
  698:                 if ($preserved) {
  699:                     $env{'request.external.querystring'} = $preserved;
  700:                 }
  701:             }
  702:             if ($env{'request.course.id'}) {
  703:                 $checkabsolute = 1;
  704:             }
  705:         } elsif ($env{'request.course.id'} &&
  706:                  (($requrl =~ m{^/adm/$match_domain/$match_username/aboutme$}) ||
  707:                   ($requrl eq "/public/$cdom/$cnum/syllabus") ||
  708:                   ($requrl =~ m{^/adm/$cdom/$cnum/\d+/ext\.tool$}))) {
  709:             my $query = $r->args;
  710:             if ($query) {
  711:                 foreach my $pair (split(/&/,$query)) {
  712:                     my ($name, $value) = split(/=/,$pair);
  713:                     if ($name eq 'folderpath') {
  714:                         if ($value =~ /^supplemental/) {
  715:                             $suppext = 1;
  716:                         }
  717:                         last;
  718:                     }
  719:                 }
  720:             }
  721:             if ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}) {
  722:                 $checkabsolute = 1;
  723:             }
  724:         }
  725:         if ($checkabsolute) {
  726:             my $hostname = $r->hostname();
  727:             my $lonhost = &Apache::lonnet::host_from_dns($hostname);
  728:             if ($lonhost) {
  729:                 my $actual = &Apache::lonnet::absolute_url($hostname,1,1);
  730:                 my $exphostname = &Apache::lonnet::hostname($lonhost);
  731:                 my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname;
  732:                 unless ($actual eq $expected) {
  733:                     $env{'request.use_absolute'} = $expected;
  734:                 }
  735:             }
  736:         }
  737: # -------------------------------------------------------- Load POST parameters
  738: 
  739: 	&Apache::lonacc::get_posted_cgi($r);
  740: 
  741: # ------------------------------------------------------ Check if load balancer 
  742: 
  743:         my $checkexempt;
  744:         if ($env{'user.loadbalexempt'} eq $r->dir_config('lonHostID')) {
  745:             if ($env{'user.loadbalcheck.time'} + 600 > time) {
  746:                 $checkexempt = 1;
  747:             }
  748:         }
  749:         if ($env{'user.noloadbalance'} eq $r->dir_config('lonHostID')) {
  750:             $checkexempt = 1;
  751:         }
  752:         unless (($checkexempt) || (($requrl eq '/adm/switchserver') && (!$r->is_initial_req()))) {
  753:             ($is_balancer,$otherserver) =
  754:                 &Apache::lonnet::check_loadbalancing($env{'user.name'},
  755:                                                      $env{'user.domain'});
  756:             if ($is_balancer) {
  757:                 # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
  758:                 my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
  759:                 if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
  760:                     $otherserver = $found_server;
  761:                 }
  762:                 unless ($requrl eq '/adm/switchserver') {
  763:                     $r->set_handlers('PerlResponseHandler'=>
  764:                                      [\&Apache::switchserver::handler]);
  765:                 }
  766:                 if ($otherserver ne '') {
  767:                     $env{'form.otherserver'} = $otherserver;
  768:                 }
  769:                 unless (($env{'form.origurl'}) || ($r->uri eq '/adm/roles') ||
  770:                         ($r->uri eq '/adm/switchserver') || ($r->uri eq '/adm/sso')) {
  771:                     $env{'form.origurl'} = $r->uri;
  772:                 }
  773:             }
  774:         }
  775:         if ($requrl=~m{^/+tiny/+$match_domain/+\w+$}) {
  776:             if ($r->args) {
  777:                 &Apache::loncommon::get_unprocessed_cgi($r->args,['ttoken']);
  778:                 if (defined($env{'form.ttoken'})) {
  779:                     my %info = &Apache::lonnet::tmpget($env{'form.ttoken'});
  780:                     if (($info{'origurl'} ne '') && ($info{'origurl'} eq $requrl)) {
  781:                         my %data;
  782:                         if (($info{'linkprotuser'} ne '') && ($info{'linkprot'}) &&
  783:                             ($info{'linkprotuser'} ne $env{'user.name'}.':'.$env{'user.domain'})) {
  784:                             %data = (
  785:                                 origurl => $requrl,
  786:                                 linkprot => $info{'linkprot'},
  787:                                 linkprotuser => $info{'linkprotuser'},
  788:                             );
  789:                         } elsif ($info{'ltoken'} ne '') {
  790:                             my %ltoken_info = &Apache::lonnet::tmpget($info{'ltoken'});
  791:                             if (($ltoken_info{'linkprotuser'} ne '') && ($ltoken_info{'linkprot'}) &&
  792:                                 ($ltoken_info{'linkprotuser'} ne $env{'user.name'}.':'.$env{'user.domain'})) {
  793:                                 %data = (
  794:                                     origurl => $requrl,
  795:                                     linkprot => $ltoken_info{'linkprot'},
  796:                                     linkprotuser => $ltoken_info{'linkprotuser'},
  797:                                 );
  798:                             }
  799:                         }
  800:                         if (keys(%data)) {
  801:                             my $delete = &Apache::lonnet::tmpdel($env{'form.ttoken'});
  802:                             if ($info{'ltoken'} ne '') {
  803:                                 my $delete = &Apache::lonnet::tmpdel($info{'ltoken'});
  804:                             }
  805:                             my $token =
  806:                                 &Apache::lonnet::tmpput(\%data,$r->dir_config('lonHostID'),'retry');
  807:                             unless (($token eq 'con_lost') || ($token eq 'refused') || ($token =~ /^error:/) ||
  808:                                     ($token eq 'unknown_cmd') || ($token eq 'no_such_host')) {
  809:                                 $r->internal_redirect('/adm/relaunch?rtoken='.$token);
  810:                                 $r->set_handlers('PerlHandler'=> undef);
  811:                                 return OK;
  812:                             }
  813:                         }
  814:                     }
  815:                 }
  816:             }
  817:             if ($env{'user.name'} eq 'public' &&
  818:                 $env{'user.domain'} eq 'public') {
  819:                 $env{'request.firsturl'}=$requrl;
  820:                 return FORBIDDEN;
  821:             }
  822:             return OK;
  823:         }
  824: # ---------------------------------------------------------------- Check access
  825: 	my $now = time;
  826:         my ($check_symb,$check_access,$check_block,$access,$poss_symb);
  827: 	if ($requrl !~ m{^/(?:adm|public|(?:prt|zip)spool)/}
  828: 	    || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
  829:             $check_access = 1;
  830:         }
  831:         if ((!$check_access) && ($env{'request.course.id'})) {
  832:             if (($requrl eq '/adm/viewclasslist') ||
  833:                 ($requrl =~ m{^(/adm/wrapper|)\Q/uploaded/$cdom/$cnum/docs/\E}) ||
  834:                 ($requrl =~ m{^/adm/.*/aboutme$}) ||
  835:                 ($requrl=~m{^/adm/coursedocs/showdoc/}) ||
  836:                 ($requrl=~m{^(/adm/wrapper|)/adm/$cdom/$cnum/\d+/ext\.tool$})) {
  837:                 $check_block = 1;
  838:             }
  839:         }
  840:         if (($env{'request.course.id'}) && (!$suppext)) {
  841:             $requrl=~/\.(\w+)$/;
  842:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
  843:                 ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) ||
  844:                 ($requrl=~/^\/adm\/wrapper\//) ||
  845:                 ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
  846:                 ($requrl=~m|\.problem/smpedit$|) ||
  847:                 ($requrl=~/^\/public\/.*\/syllabus$/) ||
  848:                 ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) ||
  849:                 ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/) ||
  850:                 ($requrl=~m{^/adm/$cdom/$cnum/\d+/ext\.tool$})) {
  851:                 $check_symb = 1;
  852:             }
  853:         }
  854:         if (($check_access) || ($check_block)) {
  855:             if ($check_symb) {
  856:                 if ($env{'form.symb'}) {
  857:                     $poss_symb=&Apache::lonnet::symbclean($env{'form.symb'});
  858:                 } elsif (($env{'request.course.id'}) && ($r->args ne '')) {
  859:                     my $query = $r->args;
  860:                     foreach my $pair (split(/&/,$query)) {
  861:                         my ($name, $value) = split(/=/,$pair);
  862:                         $name = &unescape($name);
  863:                         $value =~ tr/+/ /;
  864:                         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  865:                         if ($name eq 'symb') {
  866:                             $poss_symb = &Apache::lonnet::symbclean($value);
  867:                             last;
  868:                         }
  869:                     }
  870:                 }
  871:                 if ($poss_symb) {
  872:                     my ($possmap,$resid,$url)=&Apache::lonnet::decode_symb($poss_symb);
  873:                     $url = &Apache::lonnet::clutter($url);
  874:                     my $toplevelmap = $env{'course.'.$env{'request.course.id'}.'.url'};
  875:                     unless (($url eq $requrl) && (($possmap eq $toplevelmap) ||
  876:                                                   (&Apache::lonnet::is_on_map($possmap)))) {
  877:                         undef($poss_symb);
  878:                     }
  879:                     if ($poss_symb) {
  880:                         if ((!$env{'request.role.adv'}) && ($env{'acc.randomout'}) &&
  881:                             ($env{'acc.randomout'}=~/\&\Q$poss_symb\E\&/)) {
  882:                             undef($poss_symb);
  883:                         } elsif ((!$env{'request.role.adv'}) && ($env{'acc.deeplinkout'}) &&
  884:                                  ($env{'acc.deeplinkout'}=~/\&\Q$poss_symb\E\&/)) {
  885:                             undef($poss_symb);
  886:                         }
  887:                     }
  888:                 }
  889:                 if ($poss_symb) {
  890:                     $access=&Apache::lonnet::allowed('bre',$requrl,$poss_symb);
  891:                 } else {
  892:                     $access=&Apache::lonnet::allowed('bre',$requrl,'','','','',1);
  893:                 }
  894:             } else {
  895:                 my $nodeeplinkcheck;
  896:                 if (($check_access) && ($requrl =~ /\.(sequence|page)$/)) {
  897:                     unless ($env{'form.navmap'}) {
  898:                         if ($r->args ne '') {
  899:                             &Apache::loncommon::get_unprocessed_cgi($r->args,['navmap']);
  900:                             unless ($env{'form.navmap'}) {
  901:                                 $nodeeplinkcheck = 1;
  902:                             }
  903:                         }
  904:                     }
  905:                 }
  906:                 my $clientip = &Apache::lonnet::get_requestor_ip($r);
  907:                 $access=&Apache::lonnet::allowed('bre',$requrl,'','',$clientip,'','',$nodeeplinkcheck);
  908:             }
  909:         }
  910:         if ($check_block) {
  911:             if ($access eq 'B') {
  912:                 if ($poss_symb) {
  913:                     if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
  914:                         $env{'request.symb'} = $poss_symb;
  915:                     }
  916:                 }
  917:                 &Apache::blockedaccess::setup_handler($r);
  918:                 return OK;
  919:             }
  920:         } elsif ($check_access) {
  921:             if ($handle eq '') {
  922:                 unless ($access eq 'F') {
  923:                     if ($requrl =~ m{^/res/$match_domain/$match_username/}) {
  924:                         $r->log_reason("Cookie not valid", $r->filename);
  925:                     }
  926:                 }
  927:             }
  928: 	    if ($access eq '1') {
  929: 		$env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
  930: 		return HTTP_NOT_ACCEPTABLE; 
  931: 	    }
  932: 	    if ($access eq 'A') {
  933: 		&Apache::restrictedaccess::setup_handler($r);
  934: 		return OK;
  935: 	    }
  936:             if ($access eq 'B') {
  937:                 if ($poss_symb) {
  938:                     if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
  939:                         $env{'request.symb'} = $poss_symb;
  940:                     }
  941:                 }
  942:                 &Apache::blockedaccess::setup_handler($r);
  943:                 return OK;
  944:             }
  945:             if ($access eq 'D') {
  946:                 &Apache::lonprotected::setup_handler($r);
  947:                 return OK;
  948:             }
  949: 	    if (($access ne '2') && ($access ne 'F')) {
  950:                 if ($requrl =~ m{^/res/}) {
  951:                     $access = &Apache::lonnet::allowed('bro',$requrl);
  952:                     if ($access ne 'F') {
  953:                         if ($requrl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
  954:                             $access = &Apache::lonnet::allowed('bre','/res/lib/templates/simpleproblem.problem');
  955:                             if ($access ne 'F') {
  956:                                 $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  957:                                 return HTTP_NOT_ACCEPTABLE;
  958:                             }
  959:                         } else {
  960:                             $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  961:                             return HTTP_NOT_ACCEPTABLE;
  962:                         }
  963:                     }
  964:                 } elsif (($handle =~ /^publicuser_\d+$/) && (&Apache::lonnet::is_portfolio_url($requrl))) {
  965:                     my $clientip = &Apache::lonnet::get_requestor_ip($r);
  966:                     if (&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip) ne 'F') {
  967:                         $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  968:                         return HTTP_NOT_ACCEPTABLE;
  969:                     }
  970:                 } else {
  971: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  972: 		    return HTTP_NOT_ACCEPTABLE;
  973:                 }
  974: 	    }
  975: 	}
  976: 	if ($requrl =~ m|^/prtspool/|) {
  977: 	    my $start='/prtspool/'.$env{'user.name'}.'_'.
  978: 		$env{'user.domain'};
  979: 	    if ($requrl !~ /^\Q$start\E/) {
  980: 		$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  981: 		return HTTP_NOT_ACCEPTABLE;
  982: 	    }
  983: 	}
  984: 	if ($requrl =~ m|^/zipspool/|) {
  985: 	    my $start='/zipspool/zipout/'.$env{'user.name'}.":".
  986: 		$env{'user.domain'};
  987: 	    if ($requrl !~ /^\Q$start\E/) {
  988: 		$env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
  989: 		return HTTP_NOT_ACCEPTABLE;
  990: 	    }
  991: 	}
  992: 	if ($env{'user.name'} eq 'public' && 
  993: 	    $env{'user.domain'} eq 'public' &&
  994: 	    $requrl !~ m{^/+(res|public|uploaded)/} &&
  995: 	    $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
  996:             $requrl !~ m{^/adm/blockingstatus/.*$} &&
  997: 	    $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
  998: 	    $env{'request.querystring'}=$r->args;
  999: 	    $env{'request.firsturl'}=$requrl;
 1000: 	    return FORBIDDEN;
 1001: 	}
 1002: # ------------------------------------------------------------- This is allowed
 1003: 	if ($env{'request.course.id'}) {
 1004: 	    &Apache::lonnet::countacc($requrl);
 1005:             my $query=$r->args;
 1006:             if ($check_symb) {
 1007: # ------------------------------------- This is serious stuff, get symb and log
 1008: 		my $symb;
 1009: 		if ($query) {
 1010: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb','folderpath']);
 1011: 		}
 1012: 		if ($env{'form.symb'}) {
 1013: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
 1014:                     if (($requrl eq '/adm/navmaps') ||
 1015:                         ($requrl =~ m{^/adm/wrapper/}) ||
 1016:                         ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
 1017:                         unless (&Apache::lonnet::symbverify($symb,$requrl)) {
 1018:                             if (&Apache::lonnet::is_on_map($requrl)) {
 1019:                                 $symb = &Apache::lonnet::symbread($requrl);
 1020:                                 unless (&Apache::lonnet::symbverify($symb,$requrl)) {
 1021:                                     undef($symb);
 1022:                                 }
 1023:                             }
 1024:                         }
 1025:                         if ($symb) {
 1026:                             if ($requrl eq '/adm/navmaps') {
 1027:                                 my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
 1028:                                 &Apache::lonnet::symblist($map,$murl => [$murl,$mid]);
 1029:                             } elsif (($requrl =~ m{^/adm/wrapper/}) ||
 1030:                                      ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
 1031:                                 my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
 1032:                                 if ($map =~ /\.page$/) {
 1033:                                     my $mapsymb = &Apache::lonnet::symbread($map);
 1034:                                     ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1035:                                 }
 1036:                                 &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
 1037:                                                           'last_known' =>[$murl,$mid]);
 1038:                             }
 1039:                         }
 1040: 		    } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
 1041: 			     (($requrl=~m|(.*)/smpedit$|) &&
 1042: 			      &Apache::lonnet::symbverify($symb,$1)) ||
 1043:                              (($requrl=~m|(.*/aboutme)/portfolio$|) &&
 1044:                               &Apache::lonnet::symbverify($symb,$1))) {
 1045: 			my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
 1046:                         if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
 1047:                             my $mapsymb = &Apache::lonnet::symbread($map);
 1048:                             ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1049:                         }
 1050: 			&Apache::lonnet::symblist($map,$murl => [$murl,$mid],
 1051: 						  'last_known' =>[$murl,$mid]);
 1052: 		    } else {
 1053: 			$r->log_reason('Invalid symb for '.$requrl.': '.
 1054: 				       $symb);
 1055: 			$env{'user.error.msg'}=
 1056: 			    "$requrl:bre:1:1:Invalid Access";
 1057: 			return HTTP_NOT_ACCEPTABLE; 
 1058: 		    }
 1059: 		} else {
 1060:                     if ($requrl=~m{^(/adm/.*/aboutme)/portfolio$}) {
 1061:                         $requrl = $1;
 1062:                     }
 1063: 		    $symb=&Apache::lonnet::symbread($requrl);
 1064:                     if (&Apache::lonnet::is_on_map($requrl) && $symb) {
 1065:                         my ($encstate,$invalidsymb);
 1066:                         unless (&Apache::lonnet::symbverify($symb,$requrl,\$encstate)) {
 1067:                             $invalidsymb = 1;
 1068:                             #
 1069:                             # If $env{'request.enc'} inconsistent with encryption expected for $symb
 1070:                             # retrieved by lonnet::symbread(), call again to check for an instance of
 1071:                             # $requrl in the course for which expected encryption matches request.enc.
 1072:                             # If symb for different instance passes lonnet::symbverify(), use that as
 1073:                             # the symb for $requrl and call &Apache::lonnet::allowed() for that symb.
 1074:                             # Report invalid symb if there is no other symb. Redirect to /adm/ambiguous
 1075:                             # if multiple possible symbs consistent with request.enc available for $requrl.
 1076:                             #
 1077:                             if (($env{'request.enc'} && !$encstate) || (!$env{'request.enc'} && $encstate)) {
 1078:                                 my %possibles;
 1079:                                 my $nocache = 1;
 1080:                                 my $oldsymb = $symb;
 1081:                                 $symb = &Apache::lonnet::symbread($requrl,'','','',\%possibles,$nocache);
 1082:                                 if (($symb) && ($symb ne $oldsymb)) {
 1083:                                     if (&Apache::lonnet::symbverify($symb,$requrl)) {
 1084:                                         my $access=&Apache::lonnet::allowed('bre',$requrl,$symb);
 1085:                                         if ($access eq 'B') {
 1086:                                             $env{'request.symb'} = $symb;
 1087:                                             &Apache::blockedaccess::setup_handler($r);
 1088:                                             return OK;
 1089:                                         } elsif (($access eq '2') || ($access eq 'F')) {
 1090:                                             $invalidsymb = '';
 1091:                                         }
 1092:                                     }
 1093:                                 } elsif (keys(%possibles) > 1) {
 1094:                                     $r->internal_redirect('/adm/ambiguous');
 1095:                                     return OK;
 1096:                                 }
 1097:                             }
 1098:                             if ($invalidsymb) {
 1099:                                 if ($requrl eq '/adm/navmaps') {
 1100:                                     undef($symb);
 1101:                                 } else {
 1102:                                     $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
 1103:                                     $env{'user.error.msg'}=
 1104:                                         "$requrl:bre:1:1:Invalid Access";
 1105:                                     return HTTP_NOT_ACCEPTABLE;
 1106:                                 }
 1107:                             }
 1108:                         }
 1109:                     }
 1110: 		    if ($symb) {
 1111: 			my ($map,$mid,$murl)=
 1112: 			    &Apache::lonnet::decode_symb($symb);
 1113:                         if ($requrl eq '/adm/navmaps') {
 1114:                             &Apache::lonnet::symblist($map,$murl =>[$murl,$mid]);
 1115:                         } else {
 1116:                             if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
 1117:                                 my $mapsymb = &Apache::lonnet::symbread($map);
 1118:                                 ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
 1119:                             }
 1120: 			    &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
 1121: 						      'last_known' =>[$murl,$mid]);
 1122:                         }
 1123: 		    }
 1124: 		}
 1125: 		$env{'request.symb'}=$symb;
 1126:                 if (($env{'request.symbread.cached.'}) && ($env{'request.symbread.cached.'} ne $symb)) {
 1127:                     $env{'request.symbread.cached.'} = $symb;
 1128:                 }
 1129: 		&Apache::lonnet::courseacclog($symb);
 1130: 	    } else {
 1131: # ------------------------------------------------------- This is other content
 1132: 		&Apache::lonnet::courseacclog($requrl);    
 1133: 	    }
 1134:             if ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/.+\.html?$}) {
 1135:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1136:                     if ($query) {
 1137:                         &Apache::loncommon::get_unprocessed_cgi($query,['forceedit']);
 1138:                         if ($env{'form.forceedit'}) {
 1139:                             $env{'request.state'} = 'edit';
 1140:                         }
 1141:                     }
 1142:                 }
 1143:             } elsif ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/portfolio/syllabus/.+\.html?$}) {
 1144:                 if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 1145:                     if ($query) {
 1146:                         &Apache::loncommon::get_unprocessed_cgi($query,['forceedit','editmode']);
 1147:                         if (($env{'form.forceedit'}) || ($env{'form.editmode'})) {
 1148:                             $env{'request.state'} = 'edit';
 1149:                         }
 1150:                     }
 1151:                 }
 1152:             }
 1153: 	}
 1154: 	return OK;
 1155:     } else {
 1156:         my $defdom=$r->dir_config('lonDefDomain');
 1157:         ($is_balancer,$otherserver) =
 1158:             &Apache::lonnet::check_loadbalancing(undef,$defdom);
 1159:         if ($is_balancer) {
 1160:             $r->set_handlers('PerlResponseHandler'=>
 1161:                              [\&Apache::switchserver::handler]);
 1162:             if ($otherserver ne '') {
 1163:                 $env{'form.otherserver'} = $otherserver;
 1164:             }
 1165:         }
 1166:     }
 1167: # -------------------------------------------- See if this is a public resource
 1168:     if ($requrl=~m|^/+adm/+help/+|) {
 1169:  	return OK;
 1170:     }
 1171: # ------------------------------------ See if this is a viewable portfolio file
 1172:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
 1173:         my $clientip = &Apache::lonnet::get_requestor_ip($r);
 1174:         my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip);
 1175: 	if ($access eq 'A') {
 1176: 	    &Apache::restrictedaccess::setup_handler($r);
 1177: 	    return OK;
 1178: 	}
 1179: 	if (($access ne '2') && ($access ne 'F')) {
 1180: 	    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
 1181: 	    return HTTP_NOT_ACCEPTABLE;
 1182: 	}
 1183:     }
 1184: 
 1185: # -------------------------------------------------------------- Not authorized
 1186:     $requrl=~/\.(\w+)$/;
 1187: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
 1188: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
 1189: #        ($requrl=~m|^/prtspool/|)) {
 1190: # -------------------------- Store where they wanted to go and get login screen
 1191: 	$env{'request.querystring'}=$r->args;
 1192: 	$env{'request.firsturl'}=$requrl;
 1193:        return FORBIDDEN;
 1194: #   } else {
 1195: # --------------------------------------------------------------------- Goodbye
 1196: #       return HTTP_BAD_REQUEST;
 1197: #   }
 1198: }
 1199: 
 1200: 1;
 1201: __END__
 1202: 
 1203: =pod
 1204: 
 1205: =back
 1206: 
 1207: =cut
 1208: 

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