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

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

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