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