File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.206: download - view: text, annotated - select for diffs
Wed Aug 24 20:58:50 2022 UTC (20 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Dual SSO and non-SSO login from /adm/login
  - Display of domain configuration for each server split into two tables
    so input textboxes can be longer.
  - "Pop-up if iframe" (Y/N) item added.  If Y, when login page is in an
    iframe, SSO log-in dialog will be displayed in a new window, i.e.,
    not within the iframe, e.g., to satisfy sameorigin requirement in
    x-frame-options in header sent by SSO server.

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

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