1: # The LearningOnline Network
2: # Cookie Based Access Handler
3: #
4: # $Id: lonacc.pm,v 1.116 2008/09/15 19:08:44 raeburn 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: sub sso_login {
154: my ($r,$handle) = @_;
155:
156: my $lonidsdir=$r->dir_config('lonIDsDir');
157: if (!($r->user
158: && (!defined($env{'user.name'}) && !defined($env{'user.domain'}))
159: && ($handle eq ''))) {
160: # not an SSO case or already logged in
161: return undef;
162: }
163:
164: my ($user) = ($r->user =~ m/([a-zA-Z0-9_\-@.]*)/);
165:
166: my $domain = $r->dir_config('lonDefDomain');
167: my $home=&Apache::lonnet::homeserver($user,$domain);
168: if ($home !~ /(con_lost|no_host|no_such_host)/) {
169: &Apache::lonnet::logthis(" SSO authorized user $user ");
170: if ($r->dir_config("lonBalancer") eq 'yes') {
171: # login but immeaditly go to switch server to find us a new
172: # machine
173: &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
174: $env{'request.sso.login'} = 1;
175: if (defined($r->dir_config("lonSSOReloginServer"))) {
176: $env{'request.sso.reloginserver'} =
177: $r->dir_config('lonSSOReloginServer');
178: }
179: $r->internal_redirect('/adm/switchserver');
180: $r->set_handlers('PerlHandler'=> undef);
181: } else {
182: # need to login them in, so generate the need data that
183: # migrate expects to do login
184: my %info=('ip' => $r->connection->remote_ip(),
185: 'domain' => $domain,
186: 'username' => $user,
187: 'server' => $r->dir_config('lonHostID'),
188: 'sso.login' => 1
189: );
190: if ($r->dir_config("ssodirecturl") == 1) {
191: $info{'origurl'} = $r->uri;
192: }
193: if (defined($r->dir_config("lonSSOReloginServer"))) {
194: $info{'sso.reloginserver'} =
195: $r->dir_config('lonSSOReloginServer');
196: }
197: my $token =
198: &Apache::lonnet::tmpput(\%info,
199: $r->dir_config('lonHostID'));
200: $env{'form.token'} = $token;
201: $r->internal_redirect('/adm/migrateuser');
202: $r->set_handlers('PerlHandler'=> undef);
203: }
204: return OK;
205: } elsif (defined($r->dir_config('lonSSOUserUnknownRedirect'))) {
206: &Apache::lonnet::logthis(" SSO authorized unknown user $user ");
207: $r->subprocess_env->set('SSOUserUnknown' => $user);
208: $r->subprocess_env->set('SSOUserDomain' => $domain);
209: my @cancreate;
210: my %domconfig =
211: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
212: if (ref($domconfig{'usercreation'}) eq 'HASH') {
213: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
214: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
215: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
216: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
217: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
218: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
219: }
220: }
221: }
222: if (grep(/^sso$/,@cancreate)) {
223: $r->internal_redirect('/adm/createaccount');
224: } else {
225: $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
226: }
227: $r->set_handlers('PerlHandler'=> undef);
228: return OK;
229: }
230: return undef;
231: }
232:
233: sub handler {
234: my $r = shift;
235: my $requrl=$r->uri;
236: if (&Apache::lonnet::is_domainimage($requrl)) {
237: return OK;
238: }
239:
240:
241: my $handle = &Apache::lonnet::check_for_valid_session($r);
242:
243: my $result = &sso_login($r,$handle);
244: if (defined($result)) {
245: return $result;
246: }
247:
248:
249: if ($r->dir_config("lonBalancer") eq 'yes') {
250: $r->set_handlers('PerlResponseHandler'=>
251: [\&Apache::switchserver::handler]);
252: }
253:
254: if ($handle eq '') {
255: $r->log_reason("Cookie $handle not valid", $r->filename);
256: } elsif ($handle ne '') {
257:
258: # ------------------------------------------------------ Initialize Environment
259: my $lonidsdir=$r->dir_config('lonIDsDir');
260: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
261:
262: # --------------------------------------------------------- Initialize Language
263:
264: &Apache::lonlocal::get_language_handle($r);
265:
266: }
267:
268: # -------------------------------------------------- Should be a valid user now
269: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
270: # -------------------------------------------------------------- Resource State
271:
272: if ($requrl=~/^\/+(res|uploaded)\//) {
273: $env{'request.state'} = "published";
274: } else {
275: $env{'request.state'} = 'unknown';
276: }
277: $env{'request.filename'} = $r->filename;
278: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
279: # -------------------------------------------------------- Load POST parameters
280:
281: &Apache::lonacc::get_posted_cgi($r);
282:
283: # ---------------------------------------------------------------- Check access
284: my $now = time;
285: if ($requrl !~ m{^/(?:adm|public|prtspool)/}
286: || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
287: my $access=&Apache::lonnet::allowed('bre',$requrl);
288: if ($access eq '1') {
289: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
290: return HTTP_NOT_ACCEPTABLE;
291: }
292: if ($access eq 'A') {
293: &Apache::restrictedaccess::setup_handler($r);
294: return OK;
295: }
296: if ($access eq 'B') {
297: &Apache::blockedaccess::setup_handler($r);
298: return OK;
299: }
300: if (($access ne '2') && ($access ne 'F')) {
301: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
302: return HTTP_NOT_ACCEPTABLE;
303: }
304: }
305: if ($requrl =~ m|^/prtspool/|) {
306: my $start='/prtspool/'.$env{'user.name'}.'_'.
307: $env{'user.domain'};
308: if ($requrl !~ /^\Q$start\E/) {
309: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
310: return HTTP_NOT_ACCEPTABLE;
311: }
312: }
313: if ($requrl =~ m|^/zipspool/|) {
314: my $start='/zipspool/zipout/'.$env{'user.name'}.":".
315: $env{'user.domain'};
316: if ($requrl !~ /^\Q$start\E/) {
317: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
318: return HTTP_NOT_ACCEPTABLE;
319: }
320: }
321: if ($env{'user.name'} eq 'public' &&
322: $env{'user.domain'} eq 'public' &&
323: $requrl !~ m{^/+(res|public|uploaded)/} &&
324: $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
325: $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
326: $env{'request.querystring'}=$r->args;
327: $env{'request.firsturl'}=$requrl;
328: return FORBIDDEN;
329: }
330: # ------------------------------------------------------------- This is allowed
331: if ($env{'request.course.id'}) {
332: &Apache::lonnet::countacc($requrl);
333: $requrl=~/\.(\w+)$/;
334: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
335: ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$ )/x) ||
336: ($requrl=~/^\/adm\/wrapper\//) ||
337: ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
338: ($requrl=~m|\.problem/smpedit$|) ||
339: ($requrl=~/^\/public\/.*\/syllabus$/)) {
340: # ------------------------------------- This is serious stuff, get symb and log
341: my $query=$r->args;
342: my $symb;
343: if ($query) {
344: &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
345: }
346: if ($env{'form.symb'}) {
347: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
348: if ($requrl =~ m|^/adm/wrapper/|
349: || $requrl =~ m|^/adm/coursedocs/showdoc/|) {
350: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
351: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
352: 'last_known' =>[$murl,$mid]);
353: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
354: (($requrl=~m|(.*)/smpedit$|) &&
355: &Apache::lonnet::symbverify($symb,$1))) {
356: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
357: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
358: 'last_known' =>[$murl,$mid]);
359: } else {
360: $r->log_reason('Invalid symb for '.$requrl.': '.
361: $symb);
362: $env{'user.error.msg'}=
363: "$requrl:bre:1:1:Invalid Access";
364: return HTTP_NOT_ACCEPTABLE;
365: }
366: } else {
367: $symb=&Apache::lonnet::symbread($requrl);
368: if (&Apache::lonnet::is_on_map($requrl) && $symb &&
369: !&Apache::lonnet::symbverify($symb,$requrl)) {
370: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
371: $env{'user.error.msg'}=
372: "$requrl:bre:1:1:Invalid Access";
373: return HTTP_NOT_ACCEPTABLE;
374: }
375: if ($symb) {
376: my ($map,$mid,$murl)=
377: &Apache::lonnet::decode_symb($symb);
378: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
379: 'last_known' =>[$murl,$mid]);
380: }
381: }
382: $env{'request.symb'}=$symb;
383: &Apache::lonnet::courseacclog($symb);
384: } else {
385: # ------------------------------------------------------- This is other content
386: &Apache::lonnet::courseacclog($requrl);
387: }
388: }
389: return OK;
390: }
391: # -------------------------------------------- See if this is a public resource
392: if ($requrl=~m|^/+adm/+help/+|) {
393: return OK;
394: }
395: # ------------------------------------ See if this is a viewable portfolio file
396: if (&Apache::lonnet::is_portfolio_url($requrl)) {
397: my $access=&Apache::lonnet::allowed('bre',$requrl);
398: if ($access eq 'A') {
399: &Apache::restrictedaccess::setup_handler($r);
400: return OK;
401: }
402: if (($access ne '2') && ($access ne 'F')) {
403: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
404: return HTTP_NOT_ACCEPTABLE;
405: }
406: }
407:
408: # -------------------------------------------------------------- Not authorized
409: $requrl=~/\.(\w+)$/;
410: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
411: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
412: # ($requrl=~m|^/prtspool/|)) {
413: # -------------------------- Store where they wanted to go and get login screen
414: $env{'request.querystring'}=$r->args;
415: $env{'request.firsturl'}=$requrl;
416: return FORBIDDEN;
417: # } else {
418: # --------------------------------------------------------------------- Goodbye
419: # return HTTP_BAD_REQUEST;
420: # }
421: }
422:
423: 1;
424: __END__
425:
426: =head1 NAME
427:
428: Apache::lonacc - Cookie Based Access Handler
429:
430: =head1 SYNOPSIS
431:
432: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
433:
434: PerlAccessHandler Apache::lonacc
435:
436: =head1 INTRODUCTION
437:
438: This module enables cookie based authentication and is used
439: to control access for many different LON-CAPA URIs.
440:
441: Whenever the client sends the cookie back to the server,
442: this cookie is handled by either lonacc.pm or loncacc.pm
443: (see srm.conf for what is invoked when). If
444: the cookie is missing or invalid, the user is re-challenged
445: for login information.
446:
447: This is part of the LearningOnline Network with CAPA project
448: described at http://www.lon-capa.org.
449:
450: =head1 HANDLER SUBROUTINE
451:
452: This routine is called by Apache and mod_perl.
453:
454: =over 4
455:
456: =item *
457:
458: transfer profile into environment
459:
460: =item *
461:
462: load POST parameters
463:
464: =item *
465:
466: check access
467:
468: =item *
469:
470: if allowed, get symb, log, generate course statistics if applicable
471:
472: =item *
473:
474: otherwise return error
475:
476: =item *
477:
478: see if public resource
479:
480: =item *
481:
482: store attempted access
483:
484: =back
485:
486: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>