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