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