1: # The LearningOnline Network
2: # Cookie Based Access Handler
3: #
4: # $Id: lonacc.pm,v 1.86 2006/07/14 21:38:26 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 CGI::Cookie();
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: return OK;
48: }
49:
50: sub goodbye {
51: my ($r)=@_;
52: &Apache::lonnet::goodbye();
53: return DONE;
54: }
55:
56: ###############################################
57:
58: sub get_posted_cgi {
59: my ($r) = @_;
60:
61: my $buffer;
62: if ($r->header_in('Content-length')) {
63: $r->read($buffer,$r->header_in('Content-length'),0);
64: }
65: unless ($buffer=~/^(\-+\w+)\s+Content\-Disposition\:\s*form\-data/si) {
66: my @pairs=split(/&/,$buffer);
67: my $pair;
68: foreach $pair (@pairs) {
69: my ($name,$value) = split(/=/,$pair);
70: $value =~ tr/+/ /;
71: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
72: $name =~ tr/+/ /;
73: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
74: &Apache::loncommon::add_to_env("form.$name",$value);
75: }
76: } else {
77: my $contentsep=$1;
78: my @lines = split (/\n/,$buffer);
79: my $name='';
80: my $value='';
81: my $fname='';
82: my $fmime='';
83: my $i;
84: for ($i=0;$i<=$#lines;$i++) {
85: if ($lines[$i]=~/^$contentsep/) {
86: if ($name) {
87: chomp($value);
88: if ($fname) {
89: $env{"form.$name.filename"}=$fname;
90: $env{"form.$name.mimetype"}=$fmime;
91: } else {
92: $value=~s/\s+$//s;
93: }
94: &Apache::loncommon::add_to_env("form.$name",$value);
95: }
96: if ($i<$#lines) {
97: $i++;
98: $lines[$i]=~
99: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
100: $name=$1;
101: $value='';
102: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
103: $fname=$1;
104: if
105: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
106: $fmime=$1;
107: $i++;
108: } else {
109: $fmime='';
110: }
111: } else {
112: $fname='';
113: $fmime='';
114: }
115: $i++;
116: }
117: } else {
118: $value.=$lines[$i]."\n";
119: }
120: }
121: }
122: #
123: # Digested POSTed values
124: #
125: # Remember the way this was originally done (GET or POST)
126: #
127: $env{'request.method'}=$ENV{'REQUEST_METHOD'};
128: #
129: # There may also be stuff in the query string
130: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
131: # Also, unset POSTed content length to cover all tracks.
132: #
133:
134: $r->method_number(M_GET);
135:
136: $r->method('GET');
137: $r->headers_in->unset('Content-length');
138: }
139:
140: sub portfolio_access {
141: my ($udom,$unum,$file_name,$group) = @_;
142: my $current_perms = &Apache::lonnet::get_portfile_permissions($udom,$unum);
143: my %access_controls = &Apache::lonnet::get_access_controls(
144: $current_perms,$group,$file_name);
145: my ($public,$guest,@domains,@users,@courses,@groups);
146: my $now = time;
147: my $access_hash = $access_controls{$file_name};
148: if (ref($access_hash) eq 'HASH') {
149: foreach my $key (keys(%{$access_hash})) {
150: my ($num,$scope,$end,$start) = ($key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
151: if ($start > $now) {
152: next;
153: }
154: if ($end && $end<$now) {
155: next;
156: }
157: if ($scope eq 'public') {
158: $public = $key;
159: last;
160: } elsif ($scope eq 'guest') {
161: $guest = $key;
162: } elsif ($scope eq 'domains') {
163: push(@domains,$key);
164: } elsif ($scope eq 'users') {
165: push(@users,$key);
166: } elsif ($scope eq 'course') {
167: push(@courses,$key);
168: } elsif ($scope eq 'group') {
169: push(@groups,$key);
170: }
171: }
172: if ($public) {
173: return 'ok';
174: }
175: if ($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public') {
176: if ($guest) {
177: return $guest;
178: }
179: } else {
180: if (@domains > 0) {
181: foreach my $domkey (@domains) {
182: my %content = &Apache::lonnet::parse_access_controls($$access_hash{$domkey});
183: if (ref($content{'dom'}) eq 'ARRAY') {
184: if (grep(/^\Q$env{'user.domain'}\E$/,@{$content{'dom'}})) {
185: return 'ok';
186: }
187: }
188: }
189: }
190: if (@users > 0) {
191: foreach my $userkey (@users) {
192: my %content = &Apache::lonnet::parse_access_controls($$access_hash{$userkey});
193: if (exists($content{'users'}{$env{'user.name'}.':'.$env{'user.domain'}})) {
194: return 'ok';
195: }
196: }
197: }
198: my %roleshash;
199: my @courses_and_groups = @courses;
200: push(@courses_and_groups,@groups);
201: if (@courses_and_groups > 0) {
202: my (%allgroups,%allroles);
203: my ($start,$end,$role,$sec,$group);
204: foreach my $envkey (%env) {
205: if ($envkey =~ m-^user\.role\.(gr|cc|in|ta|ep|st)\./([^/]+)/([^/]+)/?([^/]*)$-) {
206: my $cid = $2.'_'.$3;
207: if ($1 eq 'gr') {
208: $group = $4;
209: $allgroups{$cid}{$group} = $env{$envkey};
210: } else {
211: if ($4 eq '') {
212: $sec = 'none';
213: } else {
214: $sec = $4;
215: }
216: $allroles{$cid}{$1}{$sec} = $env{$envkey};
217: }
218: } elsif ($envkey =~ m-^user\.role\./cr/(\w+/\w+/\w*)./([^/]+)/([^/]+)/?([^/]*)$-) {
219: my $cid = $2.'_'.$3;
220: if ($4 eq '') {
221: $sec = 'none';
222: } else {
223: $sec = $4;
224: }
225: $allroles{$cid}{$1}{$sec} = $env{$envkey};
226: }
227: }
228: if (keys(%allroles) == 0) {
229: return;
230: }
231: foreach my $key (@courses_and_groups) {
232: my %content = &Apache::lonnet::parse_access_controls($$access_hash{$key});
233: my $cnum = $content{'number'};
234: my $cdom = $content{'domain'};
235: my $cid = $cdom.'_'.$cnum;
236: if (!exists($allroles{$cid})) {
237: next;
238: }
239: foreach my $role_id (keys(%{$content{'roles'}})) {
240: my @sections = @{$content{'roles'}{$role_id}{'section'}};
241: my @groups = @{$content{'roles'}{$role_id}{'group'}};
242: my @status = @{$content{'roles'}{$role_id}{'access'}};
243: my @roles = @{$content{'roles'}{$role_id}{'role'}};
244: foreach my $role (keys(%{$allroles{$cid}})) {
245: if ((grep/^all$/,@roles) || (grep/^\Q$role\E$/,@roles)) {
246: foreach my $sec (keys(%{$allroles{$cid}{$role}})) {
247: if (&course_group_datechecker($allroles{$cid}{$role}{$sec},$now,\@status) eq 'ok') {
248: if (grep/^all$/,@sections) {
249: return 'ok';
250: } else {
251: if (grep/^$sec$/,@sections) {
252: return 'ok';
253: }
254: }
255: }
256: }
257: if (keys(%{$allgroups{$cid}}) == 0) {
258: if (grep/^none$/,@groups) {
259: return 'ok';
260: }
261: } else {
262: if (grep/^all$/,@groups) {
263: return 'ok';
264: }
265: foreach my $group (keys(%{$allgroups{$cid}})) {
266: if (grep/^$group$/,@groups) {
267: return 'ok';
268: }
269: }
270: }
271: }
272: }
273: }
274: }
275: }
276: if ($guest) {
277: return $guest;
278: }
279: }
280: }
281: return;
282: }
283:
284: sub passphrase_access_checker {
285: my ($r,$guestkey,$requrl) = @_;
286: my ($num,$scope,$end,$start) = ($guestkey =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
287: if ($scope eq 'guest') {
288: if (exists($env{'user.passphrase_access_'.$requrl})) {
289: if (($env{'user.passphrase_access_'.$requrl} == 0) ||
290: ($env{'user.passphrase_access_'.$requrl} > time)) {
291: $env{'request.publicaccess'} = 1;
292: return 'ok';
293: }
294: }
295: }
296: $r->set_handlers('PerlHandler'=> \&Apache::restrictedaccess::handler);
297: $r->content_type('perl-script');
298: return;
299: }
300:
301: sub course_group_datechecker {
302: my ($dates,$now,$status) = @_;
303: my ($start,$end) = split(/\./,$dates);
304: if (!$start && !$end) {
305: return 'ok';
306: }
307: if (grep/^active$/,@{$status}) {
308: if (((!$start) || ($start && $start <= $now)) && ((!$end) || ($end && $end >= $now))) {
309: return 'ok';
310: }
311: }
312: if (grep/^previous$/,@{$status}) {
313: if ($end > $now ) {
314: return 'ok';
315: }
316: }
317: if (grep/^future$/,@{$status}) {
318: if ($start > $now) {
319: return 'ok';
320: }
321: }
322: return;
323: }
324:
325: sub handler {
326: my $r = shift;
327: my $requrl=$r->uri;
328: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
329: my $lonid=$cookies{'lonID'};
330: my $cookie;
331: my $lonidsdir=$r->dir_config('lonIDsDir');
332:
333: my $handle;
334: if ($lonid) {
335: $handle=$lonid->value;
336: $handle=~s/\W//g;
337: }
338:
339: my ($sso_login);
340: if ($r->user
341: && (!$lonid || !-e "$lonidsdir/$handle.id" || $handle eq '') ) {
342: $sso_login = 1;
343: my $domain = $r->dir_config('lonDefDomain');
344: my $home=&Apache::lonnet::homeserver($r->user,$domain);
345: if ($home !~ /(con_lost|no_such_host)/) {
346: $handle=&Apache::lonauth::success($r,$r->user,$domain,
347: $home,'noredirect');
348: $r->header_out('Set-cookie',"lonID=$handle; path=/");
349: }
350: }
351:
352: if ($sso_login) {
353: &Apache::lonnet::appenv('request.sso.login' => 1);
354: }
355:
356: if ($r->dir_config("lonBalancer") eq 'yes') {
357: $r->set_handlers('PerlResponseHandler'=>
358: [\&Apache::switchserver::handler]);
359: }
360:
361: if ($handle ne '') {
362: if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
363:
364: # ------------------------------------------------------ Initialize Environment
365:
366: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
367:
368: # --------------------------------------------------------- Initialize Language
369:
370: &Apache::lonlocal::get_language_handle($r);
371:
372: # -------------------------------------------------------------- Resource State
373:
374: if ($requrl=~/^\/+(res|uploaded)\//) {
375: $env{'request.state'} = "published";
376: } else {
377: $env{'request.state'} = 'unknown';
378: }
379: $env{'request.filename'} = $r->filename;
380: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
381: # -------------------------------------------------------- Load POST parameters
382:
383: &Apache::lonacc::get_posted_cgi($r);
384:
385: # ---------------------------------------------------------------- Check access
386: my $now = time;
387: if ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$|) {
388: my $result = &portfolio_access($1,$2,$3);
389: if ($result eq 'ok') {
390: return OK;
391: } elsif ($result =~ /^[^:]+:guest_/) {
392: &passphrase_access_checker($r,$result,$requrl);
393: return OK;
394: }
395: } elsif ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$|) {
396: my $result = &portfolio_access($1,$2,$3.'/'.$4,$3);
397: if ($result eq 'ok') {
398: return OK;
399: } elsif ($result =~ /^[^:]+:guest_/) {
400: &passphrase_access_checker($r,$result,$requrl);
401: return OK;
402: }
403: }
404: if ($requrl!~/^\/adm|public|prtspool\//) {
405: my $access=&Apache::lonnet::allowed('bre',$requrl);
406: if ($access eq '1') {
407: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
408: return HTTP_NOT_ACCEPTABLE;
409: }
410: if (($access ne '2') && ($access ne 'F')) {
411: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
412: return HTTP_NOT_ACCEPTABLE;
413: }
414: }
415: if ($requrl =~ m|^/prtspool/|) {
416: my $start='/prtspool/'.$env{'user.name'}.'_'.
417: $env{'user.domain'};
418: if ($requrl !~ /^\Q$start\E/) {
419: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
420: return HTTP_NOT_ACCEPTABLE;
421: }
422: }
423: if ($env{'user.name'} eq 'public' &&
424: $env{'user.domain'} eq 'public' &&
425: $requrl !~ m{^/+(res|public)/} &&
426: $requrl !~ m{^/+adm/(help|logout|randomlabel\.png)}) {
427: $env{'request.querystring'}=$r->args;
428: $env{'request.firsturl'}=$requrl;
429: return FORBIDDEN;
430: }
431: # ------------------------------------------------------------- This is allowed
432: if ($env{'request.course.id'}) {
433: &Apache::lonnet::countacc($requrl);
434: $requrl=~/\.(\w+)$/;
435: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
436: ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
437: ($requrl=~/^\/adm\/wrapper\//) ||
438: ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
439: ($requrl=~m|\.problem/smpedit$|) ||
440: ($requrl=~/^\/public\/.*\/syllabus$/)) {
441: # ------------------------------------- This is serious stuff, get symb and log
442: my $query=$r->args;
443: my $symb;
444: if ($query) {
445: &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
446: }
447: if ($env{'form.symb'}) {
448: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
449: if ($requrl =~ m|^/adm/wrapper/|
450: || $requrl =~ m|^/adm/coursedocs/showdoc/|) {
451: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
452: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
453: 'last_known' =>[$murl,$mid]);
454: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
455: (($requrl=~m|(.*)/smpedit$|) &&
456: &Apache::lonnet::symbverify($symb,$1))) {
457: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
458: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
459: 'last_known' =>[$murl,$mid]);
460: } else {
461: $r->log_reason('Invalid symb for '.$requrl.': '.
462: $symb);
463: $env{'user.error.msg'}=
464: "$requrl:bre:1:1:Invalid Access";
465: return HTTP_NOT_ACCEPTABLE;
466: }
467: } else {
468: $symb=&Apache::lonnet::symbread($requrl);
469: if (&Apache::lonnet::is_on_map($requrl) && $symb &&
470: !&Apache::lonnet::symbverify($symb,$requrl)) {
471: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
472: $env{'user.error.msg'}=
473: "$requrl:bre:1:1:Invalid Access";
474: return HTTP_NOT_ACCEPTABLE;
475: }
476: if ($symb) {
477: my ($map,$mid,$murl)=
478: &Apache::lonnet::decode_symb($symb);
479: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
480: 'last_known' =>[$murl,$mid]);
481: }
482: }
483: $env{'request.symb'}=$symb;
484: &Apache::lonnet::courseacclog($symb);
485: } else {
486: # ------------------------------------------------------- This is other content
487: &Apache::lonnet::courseacclog($requrl);
488: }
489: }
490: return OK;
491: } else {
492: $r->log_reason("Cookie $handle not valid", $r->filename);
493: }
494: }
495:
496: # -------------------------------------------- See if this is a public resource
497: if ($requrl=~m|^/public/|
498: || (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
499: &Apache::lonnet::logthis('Granting public access: '.$requrl);
500: &Apache::lonlocal::get_language_handle($r);
501: my $cookie=
502: &Apache::lonauth::success($r,'public','public','public');
503: my $lonidsdir=$r->dir_config('lonIDsDir');
504: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
505: &Apache::lonacc::get_posted_cgi($r);
506: $env{'request.state'} = "published";
507: $env{'request.publicaccess'} = 1;
508: $env{'request.filename'} = $r->filename;
509:
510: $r->header_out('Set-cookie',"lonID=$cookie; path=/");
511: return OK;
512: }
513: if ($requrl=~m|^/+adm/+help/+|) {
514: return OK;
515: }
516: # ------------------------------------- See if this is a viewable portfolio file
517: if ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/portfolio(/.+)$|) {
518: my $result = &portfolio_access($1,$2,$3);
519: if ($result eq 'ok') {
520: return OK;
521: } elsif ($result =~ /^[^:]+:guest_/) {
522: &passphrase_access_checker($r,$result,$requrl);
523: return OK;
524: }
525: } elsif ($requrl =~ m|/+uploaded/([^/]+)/([^/]+)/groups/([^/]+)/portfolio/(.+)$|) {
526: my $result = &portfolio_access($1,$2,$3.'/'.$4,$3);
527: if ($result eq 'ok') {
528: return OK;
529: } elsif ($result =~ /^[^:]+:guest_/) {
530: &passphrase_access_checker($r,$result,$requrl);
531: return OK;
532: }
533: }
534: # -------------------------------------------------------------- Not authorized
535: $requrl=~/\.(\w+)$/;
536: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
537: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
538: # ($requrl=~m|^/prtspool/|)) {
539: # -------------------------- Store where they wanted to go and get login screen
540: $env{'request.querystring'}=$r->args;
541: $env{'request.firsturl'}=$requrl;
542: return FORBIDDEN;
543: # } else {
544: # --------------------------------------------------------------------- Goodbye
545: # return HTTP_BAD_REQUEST;
546: # }
547: }
548:
549: 1;
550: __END__
551:
552: =head1 NAME
553:
554: Apache::lonacc - Cookie Based Access Handler
555:
556: =head1 SYNOPSIS
557:
558: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
559:
560: PerlAccessHandler Apache::lonacc
561:
562: =head1 INTRODUCTION
563:
564: This module enables cookie based authentication and is used
565: to control access for many different LON-CAPA URIs.
566:
567: Whenever the client sends the cookie back to the server,
568: this cookie is handled by either lonacc.pm or loncacc.pm
569: (see srm.conf for what is invoked when). If
570: the cookie is missing or invalid, the user is re-challenged
571: for login information.
572:
573: This is part of the LearningOnline Network with CAPA project
574: described at http://www.lon-capa.org.
575:
576: =head1 HANDLER SUBROUTINE
577:
578: This routine is called by Apache and mod_perl.
579:
580: =over 4
581:
582: =item *
583:
584: transfer profile into environment
585:
586: =item *
587:
588: load POST parameters
589:
590: =item *
591:
592: check access
593:
594: =item *
595:
596: if allowed, get symb, log, generate course statistics if applicable
597:
598: =item *
599:
600: otherwise return error
601:
602: =item *
603:
604: see if public resource
605:
606: =item *
607:
608: store attempted access
609:
610: =back
611:
612: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>