File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.159.2.21.2.3: download - view: text, annotated - select for diffs
Tue Aug 30 11:58:44 2022 UTC (20 months, 2 weeks ago) by raeburn
Branches: version_2_11_4_msu
Diff to branchpoint 1.159.2.21: preferred, unified
- For 2.11.4 (modified)
  Include changes in rev. 1.206

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

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