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

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

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