File:  [LON-CAPA] / loncom / auth / lonacc.pm
Revision 1.207: download - view: text, annotated - select for diffs
Sat Sep 17 23:38:50 2022 UTC (19 months, 4 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Support access to specific LON-CAPA message after login in cases where a
  LON-CAPA loadbalancer node is used as the portal for a domain.

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

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