1: # The LearningOnline Network
2: # Cookie Based Access Handler
3: #
4: # $Id: lonacc.pm,v 1.120 2008/11/20 14:37:52 jms Exp $
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: #
28: ###
29:
30:
31: package Apache::lonacc;
32:
33: use strict;
34: use Apache::Constants qw(:common :http :methods);
35: use Apache::File;
36: use Apache::lonnet;
37: use Apache::loncommon();
38: use Apache::lonlocal;
39: use Apache::restrictedaccess();
40: use Apache::blockedaccess();
41: use Fcntl qw(:flock);
42: use LONCAPA;
43:
44: sub cleanup {
45: my ($r)=@_;
46: if (! $r->is_initial_req()) { return DECLINED; }
47: &Apache::lonnet::save_cache();
48: &Apache::lontexconvert::jsMath_reset();
49: return OK;
50: }
51:
52: sub goodbye {
53: my ($r)=@_;
54: &Apache::lonnet::goodbye();
55: return DONE;
56: }
57:
58: ###############################################
59:
60: sub get_posted_cgi {
61: my ($r,$fields) = @_;
62:
63: my $buffer;
64: if ($r->header_in('Content-length')) {
65: $r->read($buffer,$r->header_in('Content-length'),0);
66: }
67: my $content_type = $r->header_in('Content-type');
68: if ($content_type !~ m{^multipart/form-data}) {
69: my @pairs=split(/&/,$buffer);
70: my $pair;
71: foreach $pair (@pairs) {
72: my ($name,$value) = split(/=/,$pair);
73: $value =~ tr/+/ /;
74: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
75: $name =~ tr/+/ /;
76: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
77: if (ref($fields) eq 'ARRAY') {
78: next if (!grep(/^\Q$name\E$/,@{$fields}));
79: }
80: &Apache::loncommon::add_to_env("form.$name",$value);
81: }
82: } else {
83: my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
84: my @lines = split (/\n/,$buffer);
85: my $name='';
86: my $value='';
87: my $fname='';
88: my $fmime='';
89: my $i;
90: for ($i=0;$i<=$#lines;$i++) {
91: if ($lines[$i]=~/^--\Q$contentsep\E/) {
92: if ($name) {
93: chomp($value);
94: if (ref($fields) eq 'ARRAY') {
95: next if (!grep(/^\Q$name\E$/,@{$fields}));
96: }
97: if ($fname) {
98: if ($env{'form.symb'} ne '') {
99: my $size = (length($value))/(1024.0 * 1024.0);
100: if (&upload_size_allowed($name,$size,$fname) eq 'ok') {
101: $env{"form.$name.filename"}=$fname;
102: $env{"form.$name.mimetype"}=$fmime;
103: &Apache::loncommon::add_to_env("form.$name",$value);
104: }
105: } else {
106: $env{"form.$name.filename"}=$fname;
107: $env{"form.$name.mimetype"}=$fmime;
108: &Apache::loncommon::add_to_env("form.$name",$value);
109: }
110: } else {
111: $value=~s/\s+$//s;
112: &Apache::loncommon::add_to_env("form.$name",$value);
113: }
114: }
115: if ($i<$#lines) {
116: $i++;
117: $lines[$i]=~
118: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
119: $name=$1;
120: $value='';
121: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
122: $fname=$1;
123: if
124: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
125: $fmime=$1;
126: $i++;
127: } else {
128: $fmime='';
129: }
130: } else {
131: $fname='';
132: $fmime='';
133: }
134: $i++;
135: }
136: } else {
137: $value.=$lines[$i]."\n";
138: }
139: }
140: }
141: #
142: # Digested POSTed values
143: #
144: # Remember the way this was originally done (GET or POST)
145: #
146: $env{'request.method'}=$ENV{'REQUEST_METHOD'};
147: #
148: # There may also be stuff in the query string
149: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
150: # Also, unset POSTed content length to cover all tracks.
151: #
152:
153: $r->method_number(M_GET);
154:
155: $r->method('GET');
156: $r->headers_in->unset('Content-length');
157: }
158:
159: #
160: # Perform size checks for file uploads to essayresponse items in course context.
161: #
162: # Add form.HWFILESIZE.$part_$id to %env with file size (MB)
163: # If file exceeds maximum allowed size, add form.HWFILETOOBIG.$part_$id to %env.
164: #
165:
166: sub upload_size_allowed {
167: my ($name,$size,$fname) = @_;
168: if ($name =~ /^HWFILE(\w+)$/) {
169: my $ident = $1;
170: my $item = 'HWFILESIZE'.$ident;
171: &Apache::loncommon::add_to_env("form.$item",$size);
172: my $maxsize= &Apache::lonnet::EXT("resource.$ident.maxfilesize");
173: if (!$maxsize) {
174: $maxsize = 100.0;
175: }
176: if ($size > $maxsize) {
177: my $warn = 'HWFILETOOBIG'.$ident;
178: &Apache::loncommon::add_to_env("form.$warn",$fname);
179: return;
180: }
181: }
182: return 'ok';
183: }
184:
185: # handle the case of the single sign on user, at this point $r->user
186: # will be set and valid now need to find the loncapa user info and possibly
187: # balance them
188: # returns OK if it was a SSO and user was handled
189: # undef if not SSO or no means to hanle the user
190:
191: sub sso_login {
192: my ($r,$handle) = @_;
193:
194: my $lonidsdir=$r->dir_config('lonIDsDir');
195: if (!($r->user
196: && (!defined($env{'user.name'}) && !defined($env{'user.domain'}))
197: && ($handle eq ''))) {
198: # not an SSO case or already logged in
199: return undef;
200: }
201:
202: my ($user) = ($r->user =~ m/([a-zA-Z0-9_\-@.]*)/);
203:
204: my $domain = $r->dir_config('lonDefDomain');
205: my $home=&Apache::lonnet::homeserver($user,$domain);
206: if ($home !~ /(con_lost|no_host|no_such_host)/) {
207: &Apache::lonnet::logthis(" SSO authorized user $user ");
208: if ($r->dir_config("lonBalancer") eq 'yes') {
209: # login but immeaditly go to switch server to find us a new
210: # machine
211: &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
212: $env{'request.sso.login'} = 1;
213: if (defined($r->dir_config("lonSSOReloginServer"))) {
214: $env{'request.sso.reloginserver'} =
215: $r->dir_config('lonSSOReloginServer');
216: }
217: $r->internal_redirect('/adm/switchserver');
218: $r->set_handlers('PerlHandler'=> undef);
219: } else {
220: # need to login them in, so generate the need data that
221: # migrate expects to do login
222: my %info=('ip' => $r->connection->remote_ip(),
223: 'domain' => $domain,
224: 'username' => $user,
225: 'server' => $r->dir_config('lonHostID'),
226: 'sso.login' => 1
227: );
228: if ($r->dir_config("ssodirecturl") == 1) {
229: $info{'origurl'} = $r->uri;
230: }
231: if (defined($r->dir_config("lonSSOReloginServer"))) {
232: $info{'sso.reloginserver'} =
233: $r->dir_config('lonSSOReloginServer');
234: }
235: my $token =
236: &Apache::lonnet::tmpput(\%info,
237: $r->dir_config('lonHostID'));
238: $env{'form.token'} = $token;
239: $r->internal_redirect('/adm/migrateuser');
240: $r->set_handlers('PerlHandler'=> undef);
241: }
242: return OK;
243: } elsif (defined($r->dir_config('lonSSOUserUnknownRedirect'))) {
244: &Apache::lonnet::logthis(" SSO authorized unknown user $user ");
245: $r->subprocess_env->set('SSOUserUnknown' => $user);
246: $r->subprocess_env->set('SSOUserDomain' => $domain);
247: my @cancreate;
248: my %domconfig =
249: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
250: if (ref($domconfig{'usercreation'}) eq 'HASH') {
251: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
252: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
253: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
254: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
255: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
256: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
257: }
258: }
259: }
260: if (grep(/^sso$/,@cancreate)) {
261: $r->internal_redirect('/adm/createaccount');
262: } else {
263: $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
264: }
265: $r->set_handlers('PerlHandler'=> undef);
266: return OK;
267: }
268: return undef;
269: }
270:
271: sub handler {
272: my $r = shift;
273: my $requrl=$r->uri;
274: if (&Apache::lonnet::is_domainimage($requrl)) {
275: return OK;
276: }
277:
278:
279: my $handle = &Apache::lonnet::check_for_valid_session($r);
280:
281: my $result = &sso_login($r,$handle);
282: if (defined($result)) {
283: return $result;
284: }
285:
286:
287: if ($r->dir_config("lonBalancer") eq 'yes') {
288: $r->set_handlers('PerlResponseHandler'=>
289: [\&Apache::switchserver::handler]);
290: }
291:
292: if ($handle eq '') {
293: $r->log_reason("Cookie $handle not valid", $r->filename);
294: } elsif ($handle ne '') {
295:
296: # ------------------------------------------------------ Initialize Environment
297: my $lonidsdir=$r->dir_config('lonIDsDir');
298: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
299:
300: # --------------------------------------------------------- Initialize Language
301:
302: &Apache::lonlocal::get_language_handle($r);
303:
304: }
305:
306: # -------------------------------------------------- Should be a valid user now
307: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
308: # -------------------------------------------------------------- Resource State
309:
310: if ($requrl=~/^\/+(res|uploaded)\//) {
311: $env{'request.state'} = "published";
312: } else {
313: $env{'request.state'} = 'unknown';
314: }
315: $env{'request.filename'} = $r->filename;
316: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
317: # -------------------------------------------------------- Load POST parameters
318:
319: &Apache::lonacc::get_posted_cgi($r);
320:
321: # ---------------------------------------------------------------- Check access
322: my $now = time;
323: if ($requrl !~ m{^/(?:adm|public|prtspool)/}
324: || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
325: my $access=&Apache::lonnet::allowed('bre',$requrl);
326: if ($access eq '1') {
327: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
328: return HTTP_NOT_ACCEPTABLE;
329: }
330: if ($access eq 'A') {
331: &Apache::restrictedaccess::setup_handler($r);
332: return OK;
333: }
334: if ($access eq 'B') {
335: &Apache::blockedaccess::setup_handler($r);
336: return OK;
337: }
338: if (($access ne '2') && ($access ne 'F')) {
339: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
340: return HTTP_NOT_ACCEPTABLE;
341: }
342: }
343: if ($requrl =~ m|^/prtspool/|) {
344: my $start='/prtspool/'.$env{'user.name'}.'_'.
345: $env{'user.domain'};
346: if ($requrl !~ /^\Q$start\E/) {
347: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
348: return HTTP_NOT_ACCEPTABLE;
349: }
350: }
351: if ($requrl =~ m|^/zipspool/|) {
352: my $start='/zipspool/zipout/'.$env{'user.name'}.":".
353: $env{'user.domain'};
354: if ($requrl !~ /^\Q$start\E/) {
355: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
356: return HTTP_NOT_ACCEPTABLE;
357: }
358: }
359: if ($env{'user.name'} eq 'public' &&
360: $env{'user.domain'} eq 'public' &&
361: $requrl !~ m{^/+(res|public|uploaded)/} &&
362: $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
363: $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
364: $env{'request.querystring'}=$r->args;
365: $env{'request.firsturl'}=$requrl;
366: return FORBIDDEN;
367: }
368: # ------------------------------------------------------------- This is allowed
369: if ($env{'request.course.id'}) {
370: &Apache::lonnet::countacc($requrl);
371: $requrl=~/\.(\w+)$/;
372: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
373: ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$ )/x) ||
374: ($requrl=~/^\/adm\/wrapper\//) ||
375: ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
376: ($requrl=~m|\.problem/smpedit$|) ||
377: ($requrl=~/^\/public\/.*\/syllabus$/)) {
378: # ------------------------------------- This is serious stuff, get symb and log
379: my $query=$r->args;
380: my $symb;
381: if ($query) {
382: &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
383: }
384: if ($env{'form.symb'}) {
385: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
386: if ($requrl =~ m|^/adm/wrapper/|
387: || $requrl =~ m|^/adm/coursedocs/showdoc/|) {
388: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
389: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
390: 'last_known' =>[$murl,$mid]);
391: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
392: (($requrl=~m|(.*)/smpedit$|) &&
393: &Apache::lonnet::symbverify($symb,$1))) {
394: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
395: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
396: 'last_known' =>[$murl,$mid]);
397: } else {
398: $r->log_reason('Invalid symb for '.$requrl.': '.
399: $symb);
400: $env{'user.error.msg'}=
401: "$requrl:bre:1:1:Invalid Access";
402: return HTTP_NOT_ACCEPTABLE;
403: }
404: } else {
405: $symb=&Apache::lonnet::symbread($requrl);
406: if (&Apache::lonnet::is_on_map($requrl) && $symb &&
407: !&Apache::lonnet::symbverify($symb,$requrl)) {
408: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
409: $env{'user.error.msg'}=
410: "$requrl:bre:1:1:Invalid Access";
411: return HTTP_NOT_ACCEPTABLE;
412: }
413: if ($symb) {
414: my ($map,$mid,$murl)=
415: &Apache::lonnet::decode_symb($symb);
416: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
417: 'last_known' =>[$murl,$mid]);
418: }
419: }
420: $env{'request.symb'}=$symb;
421: &Apache::lonnet::courseacclog($symb);
422: } else {
423: # ------------------------------------------------------- This is other content
424: &Apache::lonnet::courseacclog($requrl);
425: }
426: }
427: return OK;
428: }
429: # -------------------------------------------- See if this is a public resource
430: if ($requrl=~m|^/+adm/+help/+|) {
431: return OK;
432: }
433: # ------------------------------------ See if this is a viewable portfolio file
434: if (&Apache::lonnet::is_portfolio_url($requrl)) {
435: my $access=&Apache::lonnet::allowed('bre',$requrl);
436: if ($access eq 'A') {
437: &Apache::restrictedaccess::setup_handler($r);
438: return OK;
439: }
440: if (($access ne '2') && ($access ne 'F')) {
441: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
442: return HTTP_NOT_ACCEPTABLE;
443: }
444: }
445:
446: # -------------------------------------------------------------- Not authorized
447: $requrl=~/\.(\w+)$/;
448: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
449: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
450: # ($requrl=~m|^/prtspool/|)) {
451: # -------------------------- Store where they wanted to go and get login screen
452: $env{'request.querystring'}=$r->args;
453: $env{'request.firsturl'}=$requrl;
454: return FORBIDDEN;
455: # } else {
456: # --------------------------------------------------------------------- Goodbye
457: # return HTTP_BAD_REQUEST;
458: # }
459: }
460:
461: 1;
462: __END__
463:
464:
465: =head1 NAME
466:
467: Apache::lonacc - Cookie Based Access Handler
468:
469: =head1 SYNOPSIS
470:
471: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
472:
473: PerlAccessHandler Apache::lonacc
474:
475: =head1 INTRODUCTION
476:
477: This module enables cookie based authentication and is used
478: to control access for many different LON-CAPA URIs.
479:
480: Whenever the client sends the cookie back to the server,
481: this cookie is handled by either lonacc.pm or loncacc.pm
482: (see srm.conf for what is invoked when). If
483: the cookie is missing or invalid, the user is re-challenged
484: for login information.
485:
486: This is part of the LearningOnline Network with CAPA project
487: described at http://www.lon-capa.org.
488:
489: =head1 HANDLER SUBROUTINE
490:
491: This routine is called by Apache and mod_perl.
492:
493: =over 4
494:
495: =item *
496:
497: transfer profile into environment
498:
499: =item *
500:
501: load POST parameters
502:
503: =item *
504:
505: check access
506:
507: =item *
508:
509: if allowed, get symb, log, generate course statistics if applicable
510:
511: =item *
512:
513: otherwise return error
514:
515: =item *
516:
517: see if public resource
518:
519: =item *
520:
521: store attempted access
522:
523: =back
524:
525: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>