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

1.1       albertel    1: # The LearningOnline Network
                      2: # Cookie Based Access Handler
1.22      www         3: #
1.91    ! albertel    4: # $Id: lonacc.pm,v 1.90 2006/07/21 19:42:12 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.86      albertel   38: use Apache::restrictedaccess();
1.1       albertel   39: use CGI::Cookie();
1.16      www        40: use Fcntl qw(:flock);
1.85      raeburn    41: use LONCAPA;
1.1       albertel   42: 
1.75      albertel   43: sub cleanup {
                     44:     my ($r)=@_;
                     45:     if (! $r->is_initial_req()) { return DECLINED; }
                     46:     &Apache::lonnet::save_cache();
                     47:     return OK;
                     48: }
                     49: 
                     50: sub goodbye {
                     51:     my ($r)=@_;
                     52:     &Apache::lonnet::goodbye();
                     53:     return DONE;
                     54: }
                     55: 
1.76      albertel   56: ###############################################
                     57: 
                     58: sub get_posted_cgi {
                     59:     my ($r) = @_;
                     60: 
                     61:     my $buffer;
                     62:     if ($r->header_in('Content-length')) {
                     63: 	$r->read($buffer,$r->header_in('Content-length'),0);
                     64:     }
                     65:     unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
                     66: 	my @pairs=split(/&/,$buffer);
                     67: 	my $pair;
                     68: 	foreach $pair (@pairs) {
                     69: 	    my ($name,$value) = split(/=/,$pair);
                     70: 	    $value =~ tr/+/ /;
                     71: 	    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                     72: 	    $name  =~ tr/+/ /;
                     73: 	    $name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.77      albertel   74: 	    &Apache::loncommon::add_to_env("form.$name",$value);
1.76      albertel   75: 	}
                     76:     } else {
                     77: 	my $contentsep=$1;
                     78: 	my @lines = split (/\n/,$buffer);
                     79: 	my $name='';
                     80: 	my $value='';
                     81: 	my $fname='';
                     82: 	my $fmime='';
                     83: 	my $i;
                     84: 	for ($i=0;$i<=$#lines;$i++) {
                     85: 	    if ($lines[$i]=~/^$contentsep/) {
                     86: 		if ($name) {
                     87: 		    chomp($value);
                     88: 		    if ($fname) {
                     89: 			$env{"form.$name.filename"}=$fname;
                     90: 			$env{"form.$name.mimetype"}=$fmime;
                     91: 		    } else {
                     92: 			$value=~s/\s+$//s;
                     93: 		    }
1.77      albertel   94: 		    &Apache::loncommon::add_to_env("form.$name",$value);
1.76      albertel   95: 		}
                     96: 		if ($i<$#lines) {
                     97: 		    $i++;
                     98: 		    $lines[$i]=~
                     99: 		/Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
                    100: 		    $name=$1;
                    101: 		    $value='';
                    102: 		    if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
                    103: 			$fname=$1;
                    104: 			if 
                    105:                             ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
                    106: 				$fmime=$1;
                    107: 				$i++;
                    108: 			    } else {
                    109: 				$fmime='';
                    110: 			    }
                    111: 		    } else {
                    112: 			$fname='';
                    113: 			$fmime='';
                    114: 		    }
                    115: 		    $i++;
                    116: 		}
                    117: 	    } else {
                    118: 		$value.=$lines[$i]."\n";
                    119: 	    }
                    120: 	}
                    121:     }
                    122: #
                    123: # Digested POSTed values
                    124: #
                    125: # Remember the way this was originally done (GET or POST)
                    126: #
                    127:     $env{'request.method'}=$ENV{'REQUEST_METHOD'};
                    128: #
                    129: # There may also be stuff in the query string
                    130: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
                    131: # Also, unset POSTed content length to cover all tracks.
                    132: #
                    133: 
                    134:     $r->method_number(M_GET);
                    135: 
                    136:     $r->method('GET');
                    137:     $r->headers_in->unset('Content-length');
                    138: }
                    139: 
1.1       albertel  140: sub handler {
                    141:     my $r = shift;
                    142:     my $requrl=$r->uri;
                    143:     my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
                    144:     my $lonid=$cookies{'lonID'};
                    145:     my $cookie;
1.70      albertel  146:     my $lonidsdir=$r->dir_config('lonIDsDir');
                    147: 
                    148:     my $handle;
1.1       albertel  149:     if ($lonid) {
1.70      albertel  150: 	$handle=$lonid->value;
1.1       albertel  151:         $handle=~s/\W//g;
1.70      albertel  152:     }
                    153:       
1.78      albertel  154:     my ($sso_login);
1.70      albertel  155:     if ($r->user 
                    156: 	&& (!$lonid || !-e "$lonidsdir/$handle.id" || $handle eq '') ) {
1.78      albertel  157: 	$sso_login = 1;
1.70      albertel  158: 	my $domain = $r->dir_config('lonDefDomain');
                    159: 	my $home=&Apache::lonnet::homeserver($r->user,$domain);
                    160: 	if ($home !~ /(con_lost|no_such_host)/) {
                    161: 	    $handle=&Apache::lonauth::success($r,$r->user,$domain,
                    162: 					     $home,'noredirect');
                    163: 	    $r->header_out('Set-cookie',"lonID=$handle; path=/");
                    164: 	}
                    165:     }
                    166: 
1.78      albertel  167:     if ($sso_login) {
                    168: 	&Apache::lonnet::appenv('request.sso.login' => 1);
                    169:     }
                    170: 
1.70      albertel  171:     if ($r->dir_config("lonBalancer") eq 'yes') {
                    172: 	$r->set_handlers('PerlResponseHandler'=>
                    173: 			 [\&Apache::switchserver::handler]);
                    174:     }
                    175: 
                    176:     if ($handle ne '') {
1.1       albertel  177:         if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.6       www       178: 
1.46      www       179: # ------------------------------------------------------ Initialize Environment
                    180: 
                    181:             &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.47      www       182: 
                    183: # --------------------------------------------------------- Initialize Language
                    184: 
1.48      www       185: 	    &Apache::lonlocal::get_language_handle($r);
1.46      www       186: 
                    187: # -------------------------------------------------------------- Resource State
1.6       www       188: 
1.51      albertel  189:             if ($requrl=~/^\/+(res|uploaded)\//) {
1.64      albertel  190:                $env{'request.state'} = "published";
1.17      www       191: 	    } else {
1.64      albertel  192: 	       $env{'request.state'} = 'unknown';
1.17      www       193:             }
1.64      albertel  194:             $env{'request.filename'} = $r->filename;
                    195:             $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
1.6       www       196: # -------------------------------------------------------- Load POST parameters
                    197: 
1.76      albertel  198: 	    &Apache::lonacc::get_posted_cgi($r);
1.6       www       199: 
                    200: # ---------------------------------------------------------------- Check access
1.79      raeburn   201:             my $now = time;
1.37      albertel  202:             if ($requrl!~/^\/adm|public|prtspool\//) {
1.7       www       203: 		my $access=&Apache::lonnet::allowed('bre',$requrl);
                    204:                 if ($access eq '1') {
1.64      albertel  205: 		   $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
1.7       www       206: 	           return HTTP_NOT_ACCEPTABLE; 
                    207:                 }
1.90      albertel  208: 		if ($access eq 'A') {
                    209: 		    &Apache::restrictedaccess::setup_handler($r);
                    210: 		    return OK;
                    211:                 }
1.7       www       212:                 if (($access ne '2') && ($access ne 'F')) {
1.64      albertel  213: 		   $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
1.7       www       214: 	           return HTTP_NOT_ACCEPTABLE; 
                    215:                 }
1.23      www       216:             }
1.37      albertel  217: 	    if ($requrl =~ m|^/prtspool/|) {
1.64      albertel  218: 		my $start='/prtspool/'.$env{'user.name'}.'_'.
                    219: 		    $env{'user.domain'};
1.37      albertel  220: 		if ($requrl !~ /^\Q$start\E/) {
1.64      albertel  221: 		    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
1.37      albertel  222: 		    return HTTP_NOT_ACCEPTABLE;
                    223: 		}
                    224: 	    }
1.67      albertel  225: 	    if ($env{'user.name'} eq 'public' && 
                    226: 		$env{'user.domain'} eq 'public' &&
1.90      albertel  227: 		$requrl !~ m{^/+(res|public|uploaded)/} &&
1.91    ! albertel  228: 		$requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$} &&
1.89      albertel  229: 		$requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
1.67      albertel  230: 		$env{'request.querystring'}=$r->args;
                    231: 		$env{'request.firsturl'}=$requrl;
                    232: 		return FORBIDDEN;
                    233: 	    }
1.23      www       234: # ------------------------------------------------------------- This is allowed
1.64      albertel  235:           if ($env{'request.course.id'}) {
1.24      www       236: 	    &Apache::lonnet::countacc($requrl);
1.23      www       237:             $requrl=~/\.(\w+)$/;
1.39      www       238:             if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
1.84      raeburn   239:  ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
1.44      www       240:  ($requrl=~/^\/adm\/wrapper\//) ||
1.72      albertel  241:  ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
1.53      albertel  242:  ($requrl=~m|\.problem/smpedit$|) ||
1.39      www       243:  ($requrl=~/^\/public\/.*\/syllabus$/)) {
1.23      www       244: # ------------------------------------- This is serious stuff, get symb and log
1.29      www       245: 		my $query=$r->args;
                    246:                 my $symb;
                    247:                 if ($query) {
                    248: 		    &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
                    249:                 }
1.64      albertel  250:                 if ($env{'form.symb'}) {
                    251: 		    $symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.72      albertel  252:                     if ($requrl =~ m|^/adm/wrapper/|
                    253: 			|| $requrl =~ m|^/adm/coursedocs/showdoc/|) {
1.52      raeburn   254:                         my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.63      albertel  255:                         &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
                    256: 						  'last_known' =>[$murl,$mid]);
1.53      albertel  257:                     } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
                    258: 			     (($requrl=~m|(.*)/smpedit$|) &&
                    259: 			      &Apache::lonnet::symbverify($symb,$1))) {
1.50      albertel  260:                       my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.74      albertel  261: 		      &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
1.63      albertel  262: 						'last_known' =>[$murl,$mid]);
1.31      www       263: 		    } else {
                    264: 			$r->log_reason('Invalid symb for '.$requrl.': '.
                    265:                                        $symb);
1.64      albertel  266: 		        $env{'user.error.msg'}=
1.31      www       267:                                 "$requrl:bre:1:1:Invalid Access";
                    268:   	                return HTTP_NOT_ACCEPTABLE; 
                    269:                     }
1.29      www       270:                 } else {
1.44      www       271: 	            $symb=&Apache::lonnet::symbread($requrl);
1.58      albertel  272: 		    if (&Apache::lonnet::is_on_map($requrl) && $symb &&
1.56      albertel  273: 			!&Apache::lonnet::symbverify($symb,$requrl)) {
1.58      albertel  274: 			$r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1.64      albertel  275: 		        $env{'user.error.msg'}=
1.55      albertel  276:                                 "$requrl:bre:1:1:Invalid Access";
                    277:   	                return HTTP_NOT_ACCEPTABLE; 
                    278: 		    }
1.61      albertel  279: 		    if ($symb) {
1.65      albertel  280: 			my ($map,$mid,$murl)=
                    281: 			    &Apache::lonnet::decode_symb($symb);
1.63      albertel  282: 			&Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
                    283: 						'last_known' =>[$murl,$mid]);
1.61      albertel  284: 		    }
1.29      www       285:                 }
1.64      albertel  286:                 $env{'request.symb'}=$symb;
1.23      www       287:                 &Apache::lonnet::courseacclog($symb);
                    288:             } else {
                    289: # ------------------------------------------------------- This is other content
                    290:                 &Apache::lonnet::courseacclog($requrl);    
                    291:             }
                    292: 	  }
1.2       www       293:             return OK; 
1.1       albertel  294:         } else { 
1.73      raeburn   295:             $r->log_reason("Cookie $handle not valid", $r->filename); 
                    296:         }
1.88      albertel  297: 	}
1.6       www       298: 
1.21      www       299: # -------------------------------------------- See if this is a public resource
1.37      albertel  300:     if ($requrl=~m|^/public/|
                    301: 	|| (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
1.21      www       302:         &Apache::lonnet::logthis('Granting public access: '.$requrl);
1.71      www       303:         &Apache::lonlocal::get_language_handle($r);
1.66      albertel  304: 	my $cookie=
                    305: 	    &Apache::lonauth::success($r,'public','public','public');
                    306:         my $lonidsdir=$r->dir_config('lonIDsDir');
                    307: 	&Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
1.76      albertel  308: 	&Apache::lonacc::get_posted_cgi($r);
1.64      albertel  309:         $env{'request.state'} = "published";
                    310:         $env{'request.publicaccess'} = 1;
                    311:         $env{'request.filename'} = $r->filename;
1.59      albertel  312: 
1.66      albertel  313: 	$r->header_out('Set-cookie',"lonID=$cookie; path=/");
1.21      www       314:         return OK;
                    315:     }
1.68      albertel  316:     if ($requrl=~m|^/+adm/+help/+|) {
1.89      albertel  317:  	return OK;
1.68      albertel  318:     }
1.90      albertel  319: # ------------------------------------ See if this is a viewable portfolio file
1.89      albertel  320:     if (&Apache::lonnet::is_portfolio_url($requrl)) {
1.90      albertel  321: 	my $access=&Apache::lonnet::allowed('bre',$requrl);
                    322: 	if ($access eq 'A') {
                    323: 	    &Apache::restrictedaccess::setup_handler($r);
                    324: 	    return OK;
                    325: 	}
                    326: 	if (($access ne '2') && ($access ne 'F')) {
                    327: 	    $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
                    328: 	    return HTTP_NOT_ACCEPTABLE;
                    329: 	}
1.79      raeburn   330:     }
1.87      albertel  331: 
1.34      www       332: # -------------------------------------------------------------- Not authorized
                    333:     $requrl=~/\.(\w+)$/;
1.62      albertel  334: #    if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
                    335: #        ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
                    336: #        ($requrl=~m|^/prtspool/|)) {
1.34      www       337: # -------------------------- Store where they wanted to go and get login screen
1.64      albertel  338: 	$env{'request.querystring'}=$r->args;
                    339: 	$env{'request.firsturl'}=$requrl;
1.34      www       340:        return FORBIDDEN;
1.62      albertel  341: #   } else {
1.34      www       342: # --------------------------------------------------------------------- Goodbye
1.62      albertel  343: #       return HTTP_BAD_REQUEST;
                    344: #   }
1.1       albertel  345: }
                    346: 
                    347: 1;
                    348: __END__
1.25      harris41  349: 
                    350: =head1 NAME
                    351: 
                    352: Apache::lonacc - Cookie Based Access Handler
                    353: 
                    354: =head1 SYNOPSIS
                    355: 
                    356: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                    357: 
                    358:  PerlAccessHandler       Apache::lonacc
                    359: 
                    360: =head1 INTRODUCTION
                    361: 
                    362: This module enables cookie based authentication and is used
                    363: to control access for many different LON-CAPA URIs.
                    364: 
                    365: Whenever the client sends the cookie back to the server, 
                    366: this cookie is handled by either lonacc.pm or loncacc.pm
                    367: (see srm.conf for what is invoked when).  If
                    368: the cookie is missing or invalid, the user is re-challenged
                    369: for login information.
                    370: 
                    371: This is part of the LearningOnline Network with CAPA project
                    372: described at http://www.lon-capa.org.
                    373: 
                    374: =head1 HANDLER SUBROUTINE
                    375: 
                    376: This routine is called by Apache and mod_perl.
                    377: 
                    378: =over 4
                    379: 
                    380: =item *
                    381: 
                    382: transfer profile into environment
                    383: 
                    384: =item *
                    385: 
                    386: load POST parameters
                    387: 
                    388: =item *
                    389: 
                    390: check access
                    391: 
                    392: =item *
                    393: 
                    394: if allowed, get symb, log, generate course statistics if applicable
                    395: 
                    396: =item *
                    397: 
                    398: otherwise return error
                    399: 
                    400: =item *
                    401: 
                    402: see if public resource
                    403: 
                    404: =item *
                    405: 
                    406: store attempted access
                    407: 
                    408: =back
                    409: 
                    410: =cut

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