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

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

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