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

1.1       albertel    1: # The LearningOnline Network
                      2: # Cookie Based Access Handler
1.22      www         3: #
1.40    ! www         4: # $Id: lonacc.pm,v 1.39 2003/02/28 18:14:44 www 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.1       albertel   47: use CGI::Cookie();
1.16      www        48: use Fcntl qw(:flock);
1.1       albertel   49: 
                     50: sub handler {
                     51:     my $r = shift;
                     52:     my $requrl=$r->uri;
                     53:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                     54:     my $lonid=$cookies{'lonID'};
                     55:     my $cookie;
                     56:     if ($lonid) {
                     57: 	my $handle=$lonid->value;
                     58:         $handle=~s/\W//g;
                     59:         my $lonidsdir=$r->dir_config('lonIDsDir');
                     60:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.6       www        61: 
                     62: # ------------------------------------------- Transfer profile into environment
                     63: 
1.2       www        64:             my @profile;
                     65: 	    {
                     66:              my $idf=Apache::File->new("$lonidsdir/$handle.id");
1.16      www        67:              flock($idf,LOCK_SH);
1.2       www        68:              @profile=<$idf>;
1.16      www        69:              $idf->close();
1.2       www        70: 	    }
                     71:             my $envi;
                     72:             for ($envi=0;$envi<=$#profile;$envi++) {
                     73: 		chomp($profile[$envi]);
                     74: 		my ($envname,$envvalue)=split(/=/,$profile[$envi]);
1.9       www        75:                 $ENV{$envname} = $envvalue;
1.2       www        76:             }
1.9       www        77:             $ENV{'user.environment'} = "$lonidsdir/$handle.id";
1.18      www        78:             if ($requrl=~/^\/res\//) {
1.17      www        79:                $ENV{'request.state'} = "published";
                     80: 	    } else {
                     81: 	       $ENV{'request.state'} = 'unknown';
                     82:             }
1.9       www        83:             $ENV{'request.filename'} = $r->filename;
1.12      www        84: 
1.6       www        85: # -------------------------------------------------------- Load POST parameters
                     86: 
1.8       www        87: 
                     88:            
1.14      www        89:         my $buffer;
1.6       www        90: 
1.14      www        91:         $r->read($buffer,$r->header_in('Content-length'));
                     92: 	unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
1.5       www        93:             my @pairs=split(/&/,$buffer);
1.6       www        94:             my $pair;
1.5       www        95:             foreach $pair (@pairs) {
1.6       www        96:                my ($name,$value) = split(/=/,$pair);
                     97:                $value =~ tr/+/ /;
                     98:                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.10      www        99:                $name  =~ tr/+/ /;
                    100:                $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.28      albertel  101: 	       &Apache::loncommon::add_to_env("form.$name",$value);
1.6       www       102:             }
1.14      www       103:         } else {
                    104: 	    my $contentsep=$1;
                    105:             my @lines = split (/\n/,$buffer);
                    106:             my $name='';
                    107:             my $value='';
                    108:             my $fname='';
                    109:             my $fmime='';
                    110:             my $i;
                    111:             for ($i=0;$i<=$#lines;$i++) {
                    112: 		if ($lines[$i]=~/^$contentsep/) {
                    113: 		    if ($name) {
                    114:                         chomp($value);
                    115: 			if ($fname) {
                    116: 			    $ENV{"form.$name.filename"}=$fname;
                    117:                             $ENV{"form.$name.mimetype"}=$fmime;
1.15      www       118:                         } else {
                    119:                             $value=~s/\s+$//s;
1.14      www       120:                         }
1.28      albertel  121: 			&Apache::loncommon::add_to_env("form.$name",$value);
1.14      www       122:                     }
                    123:                     if ($i<$#lines) {
                    124: 			$i++;
                    125:                         $lines[$i]=~
                    126: 		 /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
                    127:                         $name=$1;
                    128:                         $value='';
                    129:                         if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
                    130: 			   $fname=$1;
                    131:                            if 
                    132:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
                    133: 			      $fmime=$1;
                    134:                               $i++;
                    135: 			   } else {
                    136:                               $fmime='';
                    137:                            }
                    138:                         } else {
                    139: 			    $fname='';
                    140:                             $fmime='';
                    141:                         }
                    142:                         $i++;
                    143:                     }
                    144:                 } else {
                    145: 		    $value.=$lines[$i]."\n";
                    146:                 }
                    147:             }
                    148: 	}
1.26      www       149: 	    $ENV{'request.method'}=$ENV{'REQUEST_METHOD'};
1.8       www       150:             $r->method_number(M_GET);
                    151: 	    $r->method('GET');
                    152:             $r->headers_in->unset('Content-length');
1.6       www       153: 
                    154: # ---------------------------------------------------------------- Check access
                    155: 
1.37      albertel  156:             if ($requrl!~/^\/adm|public|prtspool\//) {
1.7       www       157: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
                    158:                 if ($access eq '1') {
                    159: 		   $ENV{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
                    160: 	           return HTTP_NOT_ACCEPTABLE; 
                    161:                 }
                    162:                 if (($access ne '2') && ($access ne 'F')) {
                    163: 		   $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
                    164: 	           return HTTP_NOT_ACCEPTABLE; 
                    165:                 }
1.23      www       166:             }
1.37      albertel  167: 	    if ($requrl =~ m|^/prtspool/|) {
                    168: 		my $start='/prtspool/'.$ENV{'user.name'}.'_'.
                    169: 		    $ENV{'user.domain'};
                    170: 		if ($requrl !~ /^\Q$start\E/) {
                    171: 		    $ENV{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
                    172: 		    return HTTP_NOT_ACCEPTABLE;
                    173: 		}
                    174: 	    }
1.23      www       175: # ------------------------------------------------------------- This is allowed
                    176:           if ($ENV{'request.course.id'}) {
1.24      www       177: 	    &Apache::lonnet::countacc($requrl);
1.23      www       178:             $requrl=~/\.(\w+)$/;
1.39      www       179:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
                    180:  ($requrl=~/^\/adm\/(aboutme|navmaps|smppg|bulletinboard|wrapper)/) ||
                    181:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
1.23      www       182: # ------------------------------------- This is serious stuff, get symb and log
1.29      www       183: 		my $query=$r->args;
                    184:                 my $symb;
                    185:                 if ($query) {
                    186: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
                    187:                 }
                    188:                 if ($ENV{'form.symb'}) {
1.31      www       189: 		    $symb=&Apache::lonnet::symbclean($ENV{'form.symb'});
                    190:                     if (&Apache::lonnet::symbverify($symb,$requrl)) {
                    191:                       my ($map,$mid,$murl)=split(/\_\_\_/,$symb);
                    192:                       &Apache::lonnet::symblist($map,$murl => $mid,
                    193:                                                'last_known' => $murl);
                    194: 		    } else {
                    195: 			$r->log_reason('Invalid symb for '.$requrl.': '.
                    196:                                        $symb);
                    197: 		        $ENV{'user.error.msg'}=
                    198:                                 "$requrl:bre:1:1:Invalid Access";
                    199:   	                return HTTP_NOT_ACCEPTABLE; 
                    200:                     }
1.29      www       201:                 } else {
                    202: 	            $symb=&Apache::lonnet::symbread;
                    203:                 }
1.23      www       204:                 $ENV{'request.symb'}=$symb;
                    205:                 &Apache::lonnet::courseacclog($symb);
                    206:             } else {
                    207: # ------------------------------------------------------- This is other content
                    208:                 &Apache::lonnet::courseacclog($requrl);    
                    209:             }
                    210: 	  }
1.2       www       211:             return OK; 
1.1       albertel  212:         } else { 
1.5       www       213:             $r->log_reason("Cookie $handle not valid", $r->filename) 
1.1       albertel  214:         };
                    215:     }
1.6       www       216: 
1.21      www       217: # -------------------------------------------- See if this is a public resource
1.37      albertel  218:     if ($requrl=~m|^/public/|
                    219: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
1.21      www       220:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
                    221: 	$ENV{'user.name'}='public';
                    222:         $ENV{'user.domain'}='public';
                    223:         $ENV{'request.state'} = "published";
                    224:         $ENV{'request.publicaccess'} = 1;
                    225:         $ENV{'request.filename'} = $r->filename;
                    226:         return OK;
                    227:     }
1.34      www       228: # -------------------------------------------------------------- Not authorized
                    229:     $requrl=~/\.(\w+)$/;
                    230:     if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
1.40    ! www       231:         ($requrl=~/^\/adm\/(roles|logout|email)/) ||
1.37      albertel  232:         ($requrl=~m|^/prtspool/|)) {
1.34      www       233: # -------------------------- Store where they wanted to go and get login screen
1.40    ! www       234: 	$ENV{'request.querystring'}=$r->args;
        !           235: 	$ENV{'request.firsturl'}=$requrl;
1.34      www       236:        return FORBIDDEN;
                    237:    } else {
                    238: # --------------------------------------------------------------------- Goodbye
                    239:        return HTTP_BAD_REQUEST;
                    240:    }
1.1       albertel  241: }
                    242: 
                    243: 1;
                    244: __END__
1.25      harris41  245: 
                    246: =head1 NAME
                    247: 
                    248: Apache::lonacc - Cookie Based Access Handler
                    249: 
                    250: =head1 SYNOPSIS
                    251: 
                    252: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    253: 
                    254:  PerlAccessHandler       Apache::lonacc
                    255: 
                    256: =head1 INTRODUCTION
                    257: 
                    258: This module enables cookie based authentication and is used
                    259: to control access for many different LON-CAPA URIs.
                    260: 
                    261: Whenever the client sends the cookie back to the server, 
                    262: this cookie is handled by either lonacc.pm or loncacc.pm
                    263: (see srm.conf for what is invoked when).  If
                    264: the cookie is missing or invalid, the user is re-challenged
                    265: for login information.
                    266: 
                    267: This is part of the LearningOnline Network with CAPA project
                    268: described at http://www.lon-capa.org.
                    269: 
                    270: =head1 HANDLER SUBROUTINE
                    271: 
                    272: This routine is called by Apache and mod_perl.
                    273: 
                    274: =over 4
                    275: 
                    276: =item *
                    277: 
                    278: transfer profile into environment
                    279: 
                    280: =item *
                    281: 
                    282: load POST parameters
                    283: 
                    284: =item *
                    285: 
                    286: check access
                    287: 
                    288: =item *
                    289: 
                    290: if allowed, get symb, log, generate course statistics if applicable
                    291: 
                    292: =item *
                    293: 
                    294: otherwise return error
                    295: 
                    296: =item *
                    297: 
                    298: see if public resource
                    299: 
                    300: =item *
                    301: 
                    302: store attempted access
                    303: 
                    304: =back
                    305: 
                    306: =cut

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