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

1.1       albertel    1: # The LearningOnline Network
                      2: # Cookie Based Access Handler
1.22      www         3: #
1.60    ! albertel    4: # $Id: lonacc.pm,v 1.59 2005/01/25 00:28:00 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: # YEAR=1999
1.4       www        29: # 5/21/99,5/22,5/29,5/31,6/15,16/11,22/11,
1.25      harris41   30: # YEAR=2000
1.14      www        31: # 01/06,01/13,05/31,06/01,09/06,09/25,09/28,10/30,11/6,
1.17      www        32: # 12/25,12/26,
1.25      harris41   33: # YEAR=2001
1.23      www        34: # 01/06/01,05/28,8/11,9/26,11/29 Gerd Kortemeyer
1.26      www        35: # YEAR=2002
1.27      www        36: # 1/4,2/25 Gerd Kortemeyer
1.25      harris41   37: #
                     38: ###
1.1       albertel   39: 
                     40: package Apache::lonacc;
                     41: 
                     42: use strict;
1.8       www        43: use Apache::Constants qw(:common :http :methods);
1.2       www        44: use Apache::File;
1.6       www        45: use Apache::lonnet;
1.25      harris41   46: use Apache::loncommon();
1.47      www        47: use Apache::lonlocal;
1.1       albertel   48: use CGI::Cookie();
1.16      www        49: use Fcntl qw(:flock);
1.1       albertel   50: 
                     51: sub handler {
                     52:     my $r = shift;
                     53:     my $requrl=$r->uri;
                     54:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     55:     my $lonid=$cookies{'lonID'};
                     56:     my $cookie;
                     57:     if ($lonid) {
                     58: 	my $handle=$lonid->value;
                     59:         $handle=~s/\W//g;
                     60:         my $lonidsdir=$r->dir_config('lonIDsDir');
                     61:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.6       www        62: 
1.46      www        63: # ------------------------------------------------------ Initialize Environment
                     64: 
                     65:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.47      www        66: 
                     67: # --------------------------------------------------------- Initialize Language
                     68: 
1.48      www        69: 	    &Apache::lonlocal::get_language_handle($r);
1.46      www        70: 
                     71: # -------------------------------------------------------------- Resource State
1.6       www        72: 
1.51      albertel   73:             if ($requrl=~/^\/+(res|uploaded)\//) {
1.17      www        74:                $ENV{'request.state'} = "published";
                     75: 	    } else {
                     76: 	       $ENV{'request.state'} = 'unknown';
                     77:             }
1.9       www        78:             $ENV{'request.filename'} = $r->filename;
1.49      www        79:             $ENV{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
1.6       www        80: # -------------------------------------------------------- Load POST parameters
                     81: 
1.46      www        82: 	    &Apache::loncommon::get_posted_cgi($r);
1.6       www        83: 
                     84: # ---------------------------------------------------------------- Check access
                     85: 
1.37      albertel   86:             if ($requrl!~/^\/adm|public|prtspool\//) {
1.7       www        87: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
                     88:                 if ($access eq '1') {
                     89: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
                     90: 	           return HTTP_NOT_ACCEPTABLE; 
                     91:                 }
                     92:                 if (($access ne '2') && ($access ne 'F')) {
                     93: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
                     94: 	           return HTTP_NOT_ACCEPTABLE; 
                     95:                 }
1.23      www        96:             }
1.37      albertel   97: 	    if ($requrl =~ m|^/prtspool/|) {
                     98: 		my $start='/prtspool/'.$ENV{'user.name'}.'_'.
                     99: 		    $ENV{'user.domain'};
                    100: 		if ($requrl !~ /^\Q$start\E/) {
                    101: 		    $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
                    102: 		    return HTTP_NOT_ACCEPTABLE;
                    103: 		}
                    104: 	    }
1.23      www       105: # ------------------------------------------------------------- This is allowed
                    106:           if ($ENV{'request.course.id'}) {
1.24      www       107: 	    &Apache::lonnet::countacc($requrl);
1.23      www       108:             $requrl=~/\.(\w+)$/;
1.39      www       109:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
1.44      www       110:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
                    111:  ($requrl=~/^\/adm\/wrapper\//) ||
1.53      albertel  112:  ($requrl=~m|\.problem/smpedit$|) ||
1.39      www       113:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
1.23      www       114: # ------------------------------------- This is serious stuff, get symb and log
1.29      www       115: 		my $query=$r->args;
                    116:                 my $symb;
                    117:                 if ($query) {
                    118: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
                    119:                 }
                    120:                 if ($ENV{'form.symb'}) {
1.31      www       121: 		    $symb=&Apache::lonnet::symbclean($ENV{'form.symb'});
1.52      raeburn   122:                     if ($requrl =~ m|^/adm/wrapper/|) {
                    123:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
                    124:                         &Apache::lonnet::symblist($map,$murl => $mid,
                    125:                                                'last_known' => $murl);
1.53      albertel  126:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
                    127: 			     (($requrl=~m|(.*)/smpedit$|) &&
                    128: 			      &Apache::lonnet::symbverify($symb,$1))) {
1.50      albertel  129:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.31      www       130:                       &Apache::lonnet::symblist($map,$murl => $mid,
                    131:                                                'last_known' => $murl);
                    132: 		    } else {
                    133: 			$r->log_reason('Invalid symb for '.$requrl.': '.
                    134:                                        $symb);
                    135: 		        $ENV{'user.error.msg'}=
                    136:                                 "$requrl:bre:1:1:Invalid Access";
                    137:   	                return HTTP_NOT_ACCEPTABLE; 
                    138:                     }
1.29      www       139:                 } else {
1.44      www       140: 	            $symb=&Apache::lonnet::symbread($requrl);
1.58      albertel  141: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
1.56      albertel  142: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
1.58      albertel  143: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1.55      albertel  144: 		        $ENV{'user.error.msg'}=
                    145:                                 "$requrl:bre:1:1:Invalid Access";
                    146:   	                return HTTP_NOT_ACCEPTABLE; 
                    147: 		    }
1.44      www       148:                     my ($map,$mid,$murl)=split(/\_\_\_/,$symb);
                    149: 		    &Apache::lonnet::symblist($map,$murl => $mid,
                    150:                                               'last_known' => $murl);
1.29      www       151:                 }
1.23      www       152:                 $ENV{'request.symb'}=$symb;
                    153:                 &Apache::lonnet::courseacclog($symb);
                    154:             } else {
                    155: # ------------------------------------------------------- This is other content
                    156:                 &Apache::lonnet::courseacclog($requrl);    
                    157:             }
                    158: 	  }
1.2       www       159:             return OK; 
1.1       albertel  160:         } else { 
1.5       www       161:             $r->log_reason("Cookie $handle not valid", $r->filename) 
1.1       albertel  162:         };
                    163:     }
1.6       www       164: 
1.21      www       165: # -------------------------------------------- See if this is a public resource
1.37      albertel  166:     if ($requrl=~m|^/public/|
                    167: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
1.21      www       168:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
1.60    ! albertel  169: 	&Apache::loncommon::get_posted_cgi($r);
1.21      www       170: 	$ENV{'user.name'}='public';
                    171:         $ENV{'user.domain'}='public';
                    172:         $ENV{'request.state'} = "published";
                    173:         $ENV{'request.publicaccess'} = 1;
                    174:         $ENV{'request.filename'} = $r->filename;
1.59      albertel  175: 	my ($httpbrowser,  $clientbrowser, $clientversion,
                    176: 	    $clientmathml, $clientunicode, $clientos) =
                    177: 		&Apache::loncommon::decode_user_agent($r);
                    178: 	$ENV{'browser.type'}=$clientbrowser;
                    179:         $ENV{'browser.version'}=$clientversion;
                    180:         $ENV{'browser.mathml'}=$clientmathml;
                    181:         $ENV{'browser.unicode'}=$clientunicode;
                    182:         $ENV{'browser.os'}=$clientos;
                    183: 
1.21      www       184:         return OK;
                    185:     }
1.34      www       186: # -------------------------------------------------------------- Not authorized
                    187:     $requrl=~/\.(\w+)$/;
                    188:     if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
1.41      www       189:         ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
1.37      albertel  190:         ($requrl=~m|^/prtspool/|)) {
1.34      www       191: # -------------------------- Store where they wanted to go and get login screen
1.40      www       192: 	$ENV{'request.querystring'}=$r->args;
                    193: 	$ENV{'request.firsturl'}=$requrl;
1.34      www       194:        return FORBIDDEN;
                    195:    } else {
                    196: # --------------------------------------------------------------------- Goodbye
                    197:        return HTTP_BAD_REQUEST;
                    198:    }
1.1       albertel  199: }
                    200: 
                    201: 1;
                    202: __END__
1.25      harris41  203: 
                    204: =head1 NAME
                    205: 
                    206: Apache::lonacc - Cookie Based Access Handler
                    207: 
                    208: =head1 SYNOPSIS
                    209: 
                    210: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    211: 
                    212:  PerlAccessHandler       Apache::lonacc
                    213: 
                    214: =head1 INTRODUCTION
                    215: 
                    216: This module enables cookie based authentication and is used
                    217: to control access for many different LON-CAPA URIs.
                    218: 
                    219: Whenever the client sends the cookie back to the server, 
                    220: this cookie is handled by either lonacc.pm or loncacc.pm
                    221: (see srm.conf for what is invoked when).  If
                    222: the cookie is missing or invalid, the user is re-challenged
                    223: for login information.
                    224: 
                    225: This is part of the LearningOnline Network with CAPA project
                    226: described at http://www.lon-capa.org.
                    227: 
                    228: =head1 HANDLER SUBROUTINE
                    229: 
                    230: This routine is called by Apache and mod_perl.
                    231: 
                    232: =over 4
                    233: 
                    234: =item *
                    235: 
                    236: transfer profile into environment
                    237: 
                    238: =item *
                    239: 
                    240: load POST parameters
                    241: 
                    242: =item *
                    243: 
                    244: check access
                    245: 
                    246: =item *
                    247: 
                    248: if allowed, get symb, log, generate course statistics if applicable
                    249: 
                    250: =item *
                    251: 
                    252: otherwise return error
                    253: 
                    254: =item *
                    255: 
                    256: see if public resource
                    257: 
                    258: =item *
                    259: 
                    260: store attempted access
                    261: 
                    262: =back
                    263: 
                    264: =cut

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