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