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

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

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