Annotation of loncom/auth/lonacc.pm, revision 1.89

1.1       albertel    1: # The LearningOnline Network
                      2: # Cookie Based Access Handler
1.22      www         3: #
1.89    ! albertel    4: # $Id: lonacc.pm,v 1.88 2006/07/21 16:07:48 albertel Exp $
1.22      www         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: #
1.25      harris41   28: ###
1.1       albertel   29: 
                     30: package Apache::lonacc;
                     31: 
                     32: use strict;
1.8       www        33: use Apache::Constants qw(:common :http :methods);
1.2       www        34: use Apache::File;
1.6       www        35: use Apache::lonnet;
1.25      harris41   36: use Apache::loncommon();
1.47      www        37: use Apache::lonlocal;
1.86      albertel   38: use Apache::restrictedaccess();
1.1       albertel   39: use CGI::Cookie();
1.16      www        40: use Fcntl qw(:flock);
1.85      raeburn    41: use LONCAPA;
1.1       albertel   42: 
1.75      albertel   43: sub cleanup {
                     44:     my ($r)=@_;
                     45:     if (! $r->is_initial_req()) { return DECLINED; }
                     46:     &Apache::lonnet::save_cache();
                     47:     return OK;
                     48: }
                     49: 
                     50: sub goodbye {
                     51:     my ($r)=@_;
                     52:     &Apache::lonnet::goodbye();
                     53:     return DONE;
                     54: }
                     55: 
1.76      albertel   56: ###############################################
                     57: 
                     58: sub get_posted_cgi {
                     59:     my ($r) = @_;
                     60: 
                     61:     my $buffer;
                     62:     if ($r->header_in('Content-length')) {
                     63: 	$r->read($buffer,$r->header_in('Content-length'),0);
                     64:     }
                     65:     unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
                     66: 	my @pairs=split(/&/,$buffer);
                     67: 	my $pair;
                     68: 	foreach $pair (@pairs) {
                     69: 	    my ($name,$value) = split(/=/,$pair);
                     70: 	    $value =~ tr/+/ /;
                     71: 	    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     72: 	    $name  =~ tr/+/ /;
                     73: 	    $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.77      albertel   74: 	    &Apache::loncommon::add_to_env("form.$name",$value);
1.76      albertel   75: 	}
                     76:     } else {
                     77: 	my $contentsep=$1;
                     78: 	my @lines = split (/\n/,$buffer);
                     79: 	my $name='';
                     80: 	my $value='';
                     81: 	my $fname='';
                     82: 	my $fmime='';
                     83: 	my $i;
                     84: 	for ($i=0;$i<=$#lines;$i++) {
                     85: 	    if ($lines[$i]=~/^$contentsep/) {
                     86: 		if ($name) {
                     87: 		    chomp($value);
                     88: 		    if ($fname) {
                     89: 			$env{"form.$name.filename"}=$fname;
                     90: 			$env{"form.$name.mimetype"}=$fmime;
                     91: 		    } else {
                     92: 			$value=~s/\s+$//s;
                     93: 		    }
1.77      albertel   94: 		    &Apache::loncommon::add_to_env("form.$name",$value);
1.76      albertel   95: 		}
                     96: 		if ($i<$#lines) {
                     97: 		    $i++;
                     98: 		    $lines[$i]=~
                     99: 		/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
                    100: 		    $name=$1;
                    101: 		    $value='';
                    102: 		    if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
                    103: 			$fname=$1;
                    104: 			if 
                    105:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
                    106: 				$fmime=$1;
                    107: 				$i++;
                    108: 			    } else {
                    109: 				$fmime='';
                    110: 			    }
                    111: 		    } else {
                    112: 			$fname='';
                    113: 			$fmime='';
                    114: 		    }
                    115: 		    $i++;
                    116: 		}
                    117: 	    } else {
                    118: 		$value.=$lines[$i]."\n";
                    119: 	    }
                    120: 	}
                    121:     }
                    122: #
                    123: # Digested POSTed values
                    124: #
                    125: # Remember the way this was originally done (GET or POST)
                    126: #
                    127:     $env{'request.method'}=$ENV{'REQUEST_METHOD'};
                    128: #
                    129: # There may also be stuff in the query string
                    130: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
                    131: # Also, unset POSTed content length to cover all tracks.
                    132: #
                    133: 
                    134:     $r->method_number(M_GET);
                    135: 
                    136:     $r->method('GET');
                    137:     $r->headers_in->unset('Content-length');
                    138: }
                    139: 
1.85      raeburn   140: sub passphrase_access_checker {
                    141:     my ($r,$guestkey,$requrl) = @_;
                    142:     my ($num,$scope,$end,$start) = ($guestkey =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                    143:     if ($scope eq 'guest') {
                    144:         if (exists($env{'user.passphrase_access_'.$requrl})) {
                    145:             if (($env{'user.passphrase_access_'.$requrl} == 0) || 
                    146:                 ($env{'user.passphrase_access_'.$requrl} > time)) {
                    147:                 $env{'request.publicaccess'} = 1;
                    148:                 return 'ok'; 
                    149:             }
                    150:         }
                    151:     }
1.86      albertel  152:     $r->set_handlers('PerlHandler'=> \&Apache::restrictedaccess::handler);
                    153:     $r->content_type('perl-script');
1.85      raeburn   154:     return;
                    155: }
                    156: 
1.1       albertel  157: sub handler {
                    158:     my $r = shift;
                    159:     my $requrl=$r->uri;
                    160:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                    161:     my $lonid=$cookies{'lonID'};
                    162:     my $cookie;
1.70      albertel  163:     my $lonidsdir=$r->dir_config('lonIDsDir');
                    164: 
                    165:     my $handle;
1.1       albertel  166:     if ($lonid) {
1.70      albertel  167: 	$handle=$lonid->value;
1.1       albertel  168:         $handle=~s/\W//g;
1.70      albertel  169:     }
                    170:       
1.78      albertel  171:     my ($sso_login);
1.70      albertel  172:     if ($r->user 
                    173: 	&& (!$lonid || !-e "$lonidsdir/$handle.id" || $handle eq '') ) {
1.78      albertel  174: 	$sso_login = 1;
1.70      albertel  175: 	my $domain = $r->dir_config('lonDefDomain');
                    176: 	my $home=&Apache::lonnet::homeserver($r->user,$domain);
                    177: 	if ($home !~ /(con_lost|no_such_host)/) {
                    178: 	    $handle=&Apache::lonauth::success($r,$r->user,$domain,
                    179: 					     $home,'noredirect');
                    180: 	    $r->header_out('Set-cookie',"lonID=$handle; path=/");
                    181: 	}
                    182:     }
                    183: 
1.78      albertel  184:     if ($sso_login) {
                    185: 	&Apache::lonnet::appenv('request.sso.login' => 1);
                    186:     }
                    187: 
1.70      albertel  188:     if ($r->dir_config("lonBalancer") eq 'yes') {
                    189: 	$r->set_handlers('PerlResponseHandler'=>
                    190: 			 [\&Apache::switchserver::handler]);
                    191:     }
                    192: 
                    193:     if ($handle ne '') {
1.1       albertel  194:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.6       www       195: 
1.46      www       196: # ------------------------------------------------------ Initialize Environment
                    197: 
                    198:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.47      www       199: 
                    200: # --------------------------------------------------------- Initialize Language
                    201: 
1.48      www       202: 	    &Apache::lonlocal::get_language_handle($r);
1.46      www       203: 
                    204: # -------------------------------------------------------------- Resource State
1.6       www       205: 
1.51      albertel  206:             if ($requrl=~/^\/+(res|uploaded)\//) {
1.64      albertel  207:                $env{'request.state'} = "published";
1.17      www       208: 	    } else {
1.64      albertel  209: 	       $env{'request.state'} = 'unknown';
1.17      www       210:             }
1.64      albertel  211:             $env{'request.filename'} = $r->filename;
                    212:             $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
1.6       www       213: # -------------------------------------------------------- Load POST parameters
                    214: 
1.76      albertel  215: 	    &Apache::lonacc::get_posted_cgi($r);
1.6       www       216: 
                    217: # ---------------------------------------------------------------- Check access
1.79      raeburn   218:             my $now = time;
1.89    ! albertel  219: 	    if (&Apache::lonnet::is_portfolio_url($requrl)) {
        !           220: 		my $result = &Apache::lonnet::portfolio_access($r,$requrl);
        !           221: 		if ($result eq 'ok') { return OK; }
1.87      albertel  222: 	    }
1.37      albertel  223:             if ($requrl!~/^\/adm|public|prtspool\//) {
1.7       www       224: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
                    225:                 if ($access eq '1') {
1.64      albertel  226: 		   $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
1.7       www       227: 	           return HTTP_NOT_ACCEPTABLE; 
                    228:                 }
                    229:                 if (($access ne '2') && ($access ne 'F')) {
1.64      albertel  230: 		   $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
1.7       www       231: 	           return HTTP_NOT_ACCEPTABLE; 
                    232:                 }
1.23      www       233:             }
1.37      albertel  234: 	    if ($requrl =~ m|^/prtspool/|) {
1.64      albertel  235: 		my $start='/prtspool/'.$env{'user.name'}.'_'.
                    236: 		    $env{'user.domain'};
1.37      albertel  237: 		if ($requrl !~ /^\Q$start\E/) {
1.64      albertel  238: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
1.37      albertel  239: 		    return HTTP_NOT_ACCEPTABLE;
                    240: 		}
                    241: 	    }
1.67      albertel  242: 	    if ($env{'user.name'} eq 'public' && 
                    243: 		$env{'user.domain'} eq 'public' &&
                    244: 		$requrl !~ m{^/+(res|public)/} &&
1.89    ! albertel  245: 		$requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
1.67      albertel  246: 		$env{'request.querystring'}=$r->args;
                    247: 		$env{'request.firsturl'}=$requrl;
                    248: 		return FORBIDDEN;
                    249: 	    }
1.23      www       250: # ------------------------------------------------------------- This is allowed
1.64      albertel  251:           if ($env{'request.course.id'}) {
1.24      www       252: 	    &Apache::lonnet::countacc($requrl);
1.23      www       253:             $requrl=~/\.(\w+)$/;
1.39      www       254:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
1.84      raeburn   255:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
1.44      www       256:  ($requrl=~/^\/adm\/wrapper\//) ||
1.72      albertel  257:  ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
1.53      albertel  258:  ($requrl=~m|\.problem/smpedit$|) ||
1.39      www       259:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
1.23      www       260: # ------------------------------------- This is serious stuff, get symb and log
1.29      www       261: 		my $query=$r->args;
                    262:                 my $symb;
                    263:                 if ($query) {
                    264: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
                    265:                 }
1.64      albertel  266:                 if ($env{'form.symb'}) {
                    267: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.72      albertel  268:                     if ($requrl =~ m|^/adm/wrapper/|
                    269: 			|| $requrl =~ m|^/adm/coursedocs/showdoc/|) {
1.52      raeburn   270:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.63      albertel  271:                         &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
                    272: 						  'last_known' =>[$murl,$mid]);
1.53      albertel  273:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
                    274: 			     (($requrl=~m|(.*)/smpedit$|) &&
                    275: 			      &Apache::lonnet::symbverify($symb,$1))) {
1.50      albertel  276:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.74      albertel  277: 		      &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
1.63      albertel  278: 						'last_known' =>[$murl,$mid]);
1.31      www       279: 		    } else {
                    280: 			$r->log_reason('Invalid symb for '.$requrl.': '.
                    281:                                        $symb);
1.64      albertel  282: 		        $env{'user.error.msg'}=
1.31      www       283:                                 "$requrl:bre:1:1:Invalid Access";
                    284:   	                return HTTP_NOT_ACCEPTABLE; 
                    285:                     }
1.29      www       286:                 } else {
1.44      www       287: 	            $symb=&Apache::lonnet::symbread($requrl);
1.58      albertel  288: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
1.56      albertel  289: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
1.58      albertel  290: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1.64      albertel  291: 		        $env{'user.error.msg'}=
1.55      albertel  292:                                 "$requrl:bre:1:1:Invalid Access";
                    293:   	                return HTTP_NOT_ACCEPTABLE; 
                    294: 		    }
1.61      albertel  295: 		    if ($symb) {
1.65      albertel  296: 			my ($map,$mid,$murl)=
                    297: 			    &Apache::lonnet::decode_symb($symb);
1.63      albertel  298: 			&Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
                    299: 						'last_known' =>[$murl,$mid]);
1.61      albertel  300: 		    }
1.29      www       301:                 }
1.64      albertel  302:                 $env{'request.symb'}=$symb;
1.23      www       303:                 &Apache::lonnet::courseacclog($symb);
                    304:             } else {
                    305: # ------------------------------------------------------- This is other content
                    306:                 &Apache::lonnet::courseacclog($requrl);    
                    307:             }
                    308: 	  }
1.2       www       309:             return OK; 
1.1       albertel  310:         } else { 
1.73      raeburn   311:             $r->log_reason("Cookie $handle not valid", $r->filename); 
                    312:         }
1.88      albertel  313: 	}
1.6       www       314: 
1.21      www       315: # -------------------------------------------- See if this is a public resource
1.37      albertel  316:     if ($requrl=~m|^/public/|
                    317: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
1.21      www       318:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
1.71      www       319:         &Apache::lonlocal::get_language_handle($r);
1.66      albertel  320: 	my $cookie=
                    321: 	    &Apache::lonauth::success($r,'public','public','public');
                    322:         my $lonidsdir=$r->dir_config('lonIDsDir');
                    323: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
1.76      albertel  324: 	&Apache::lonacc::get_posted_cgi($r);
1.64      albertel  325:         $env{'request.state'} = "published";
                    326:         $env{'request.publicaccess'} = 1;
                    327:         $env{'request.filename'} = $r->filename;
1.59      albertel  328: 
1.66      albertel  329: 	$r->header_out('Set-cookie',"lonID=$cookie; path=/");
1.21      www       330:         return OK;
                    331:     }
1.68      albertel  332:     if ($requrl=~m|^/+adm/+help/+|) {
1.89    ! albertel  333:  	return OK;
1.68      albertel  334:     }
1.79      raeburn   335: # ------------------------------------- See if this is a viewable portfolio file
1.89    ! albertel  336:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
        !           337: 	my $result = &Apache::lonnet::portfolio_access($r,$requrl);
        !           338: 	if ($result eq 'ok' ) { return OK; }
1.79      raeburn   339:     }
1.87      albertel  340: 
1.34      www       341: # -------------------------------------------------------------- Not authorized
                    342:     $requrl=~/\.(\w+)$/;
1.62      albertel  343: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
                    344: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
                    345: #        ($requrl=~m|^/prtspool/|)) {
1.34      www       346: # -------------------------- Store where they wanted to go and get login screen
1.64      albertel  347: 	$env{'request.querystring'}=$r->args;
                    348: 	$env{'request.firsturl'}=$requrl;
1.34      www       349:        return FORBIDDEN;
1.62      albertel  350: #   } else {
1.34      www       351: # --------------------------------------------------------------------- Goodbye
1.62      albertel  352: #       return HTTP_BAD_REQUEST;
                    353: #   }
1.1       albertel  354: }
                    355: 
                    356: 1;
                    357: __END__
1.25      harris41  358: 
                    359: =head1 NAME
                    360: 
                    361: Apache::lonacc - Cookie Based Access Handler
                    362: 
                    363: =head1 SYNOPSIS
                    364: 
                    365: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    366: 
                    367:  PerlAccessHandler       Apache::lonacc
                    368: 
                    369: =head1 INTRODUCTION
                    370: 
                    371: This module enables cookie based authentication and is used
                    372: to control access for many different LON-CAPA URIs.
                    373: 
                    374: Whenever the client sends the cookie back to the server, 
                    375: this cookie is handled by either lonacc.pm or loncacc.pm
                    376: (see srm.conf for what is invoked when).  If
                    377: the cookie is missing or invalid, the user is re-challenged
                    378: for login information.
                    379: 
                    380: This is part of the LearningOnline Network with CAPA project
                    381: described at http://www.lon-capa.org.
                    382: 
                    383: =head1 HANDLER SUBROUTINE
                    384: 
                    385: This routine is called by Apache and mod_perl.
                    386: 
                    387: =over 4
                    388: 
                    389: =item *
                    390: 
                    391: transfer profile into environment
                    392: 
                    393: =item *
                    394: 
                    395: load POST parameters
                    396: 
                    397: =item *
                    398: 
                    399: check access
                    400: 
                    401: =item *
                    402: 
                    403: if allowed, get symb, log, generate course statistics if applicable
                    404: 
                    405: =item *
                    406: 
                    407: otherwise return error
                    408: 
                    409: =item *
                    410: 
                    411: see if public resource
                    412: 
                    413: =item *
                    414: 
                    415: store attempted access
                    416: 
                    417: =back
                    418: 
                    419: =cut

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