File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.159.2.21.2.4: download - view: text, annotated - select for diffs
Mon Jan 23 00:32:03 2023 UTC (15 months, 3 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 1.207

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

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