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

1.1       albertel    1: # The LearningOnline Network
                      2: # Cookie Based Access Handler
1.22      www         3: #
1.73    ! raeburn     4: # $Id: lonacc.pm,v 1.72 2006/01/11 20:57:19 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;
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.72      albertel  131:  ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
1.53      albertel  132:  ($requrl=~m|\.problem/smpedit$|) ||
1.39      www       133:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
1.23      www       134: # ------------------------------------- This is serious stuff, get symb and log
1.29      www       135: 		my $query=$r->args;
                    136:                 my $symb;
                    137:                 if ($query) {
                    138: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
                    139:                 }
1.64      albertel  140:                 if ($env{'form.symb'}) {
                    141: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.72      albertel  142:                     if ($requrl =~ m|^/adm/wrapper/|
                    143: 			|| $requrl =~ m|^/adm/coursedocs/showdoc/|) {
1.52      raeburn   144:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.63      albertel  145:                         &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
                    146: 						  'last_known' =>[$murl,$mid]);
1.53      albertel  147:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
                    148: 			     (($requrl=~m|(.*)/smpedit$|) &&
                    149: 			      &Apache::lonnet::symbverify($symb,$1))) {
1.50      albertel  150:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.63      albertel  151:                       &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
                    152: 						'last_known' =>[$murl,$mid]);
1.31      www       153: 		    } else {
                    154: 			$r->log_reason('Invalid symb for '.$requrl.': '.
                    155:                                        $symb);
1.64      albertel  156: 		        $env{'user.error.msg'}=
1.31      www       157:                                 "$requrl:bre:1:1:Invalid Access";
                    158:   	                return HTTP_NOT_ACCEPTABLE; 
                    159:                     }
1.29      www       160:                 } else {
1.44      www       161: 	            $symb=&Apache::lonnet::symbread($requrl);
1.58      albertel  162: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
1.56      albertel  163: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
1.58      albertel  164: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1.64      albertel  165: 		        $env{'user.error.msg'}=
1.55      albertel  166:                                 "$requrl:bre:1:1:Invalid Access";
                    167:   	                return HTTP_NOT_ACCEPTABLE; 
                    168: 		    }
1.61      albertel  169: 		    if ($symb) {
1.65      albertel  170: 			my ($map,$mid,$murl)=
                    171: 			    &Apache::lonnet::decode_symb($symb);
1.63      albertel  172: 			&Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
                    173: 						'last_known' =>[$murl,$mid]);
1.61      albertel  174: 		    }
1.29      www       175:                 }
1.64      albertel  176:                 $env{'request.symb'}=$symb;
1.23      www       177:                 &Apache::lonnet::courseacclog($symb);
                    178:             } else {
                    179: # ------------------------------------------------------- This is other content
                    180:                 &Apache::lonnet::courseacclog($requrl);    
                    181:             }
                    182: 	  }
1.2       www       183:             return OK; 
1.1       albertel  184:         } else { 
1.73    ! raeburn   185:             $r->log_reason("Cookie $handle not valid", $r->filename); 
        !           186:         }
1.1       albertel  187:     }
1.6       www       188: 
1.21      www       189: # -------------------------------------------- See if this is a public resource
1.37      albertel  190:     if ($requrl=~m|^/public/|
                    191: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
1.21      www       192:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
1.71      www       193:         &Apache::lonlocal::get_language_handle($r);
1.66      albertel  194: 	my $cookie=
                    195: 	    &Apache::lonauth::success($r,'public','public','public');
                    196:         my $lonidsdir=$r->dir_config('lonIDsDir');
                    197: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
1.60      albertel  198: 	&Apache::loncommon::get_posted_cgi($r);
1.64      albertel  199:         $env{'request.state'} = "published";
                    200:         $env{'request.publicaccess'} = 1;
                    201:         $env{'request.filename'} = $r->filename;
1.59      albertel  202: 
1.66      albertel  203: 	$r->header_out('Set-cookie',"lonID=$cookie; path=/");
1.21      www       204:         return OK;
                    205:     }
1.68      albertel  206:     if ($requrl=~m|^/+adm/+help/+|) {
                    207: 	return OK;
                    208:     }
1.34      www       209: # -------------------------------------------------------------- Not authorized
                    210:     $requrl=~/\.(\w+)$/;
1.62      albertel  211: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
                    212: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
                    213: #        ($requrl=~m|^/prtspool/|)) {
1.34      www       214: # -------------------------- Store where they wanted to go and get login screen
1.64      albertel  215: 	$env{'request.querystring'}=$r->args;
                    216: 	$env{'request.firsturl'}=$requrl;
1.34      www       217:        return FORBIDDEN;
1.62      albertel  218: #   } else {
1.34      www       219: # --------------------------------------------------------------------- Goodbye
1.62      albertel  220: #       return HTTP_BAD_REQUEST;
                    221: #   }
1.1       albertel  222: }
                    223: 
                    224: 1;
                    225: __END__
1.25      harris41  226: 
                    227: =head1 NAME
                    228: 
                    229: Apache::lonacc - Cookie Based Access Handler
                    230: 
                    231: =head1 SYNOPSIS
                    232: 
                    233: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    234: 
                    235:  PerlAccessHandler       Apache::lonacc
                    236: 
                    237: =head1 INTRODUCTION
                    238: 
                    239: This module enables cookie based authentication and is used
                    240: to control access for many different LON-CAPA URIs.
                    241: 
                    242: Whenever the client sends the cookie back to the server, 
                    243: this cookie is handled by either lonacc.pm or loncacc.pm
                    244: (see srm.conf for what is invoked when).  If
                    245: the cookie is missing or invalid, the user is re-challenged
                    246: for login information.
                    247: 
                    248: This is part of the LearningOnline Network with CAPA project
                    249: described at http://www.lon-capa.org.
                    250: 
                    251: =head1 HANDLER SUBROUTINE
                    252: 
                    253: This routine is called by Apache and mod_perl.
                    254: 
                    255: =over 4
                    256: 
                    257: =item *
                    258: 
                    259: transfer profile into environment
                    260: 
                    261: =item *
                    262: 
                    263: load POST parameters
                    264: 
                    265: =item *
                    266: 
                    267: check access
                    268: 
                    269: =item *
                    270: 
                    271: if allowed, get symb, log, generate course statistics if applicable
                    272: 
                    273: =item *
                    274: 
                    275: otherwise return error
                    276: 
                    277: =item *
                    278: 
                    279: see if public resource
                    280: 
                    281: =item *
                    282: 
                    283: store attempted access
                    284: 
                    285: =back
                    286: 
                    287: =cut

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