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