1: # The LearningOnline Network
2: # Cookie Based Access Handler
3: #
4: # $Id: lonacc.pm,v 1.70 2005/11/29 21:12:03 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 CGI::Cookie();
39: use Fcntl qw(:flock);
40:
41: sub handler {
42: my $r = shift;
43: my $requrl=$r->uri;
44: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
45: my $lonid=$cookies{'lonID'};
46: my $cookie;
47: my $lonidsdir=$r->dir_config('lonIDsDir');
48:
49: my $handle;
50: if ($lonid) {
51: $handle=$lonid->value;
52: $handle=~s/\W//g;
53: }
54:
55: if ($r->user
56: && (!$lonid || !-e "$lonidsdir/$handle.id" || $handle eq '') ) {
57: my $domain = $r->dir_config('lonDefDomain');
58: my $home=&Apache::lonnet::homeserver($r->user,$domain);
59: if ($home !~ /(con_lost|no_such_host)/) {
60: $handle=&Apache::lonauth::success($r,$r->user,$domain,
61: $home,'noredirect');
62: $r->header_out('Set-cookie',"lonID=$handle; path=/");
63: }
64: }
65:
66: if ($r->dir_config("lonBalancer") eq 'yes') {
67: $r->set_handlers('PerlResponseHandler'=>
68: [\&Apache::switchserver::handler]);
69: }
70:
71: if ($handle ne '') {
72: if ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
73:
74: # ------------------------------------------------------ Initialize Environment
75:
76: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
77:
78: # --------------------------------------------------------- Initialize Language
79:
80: &Apache::lonlocal::get_language_handle($r);
81:
82: # -------------------------------------------------------------- Resource State
83:
84: if ($requrl=~/^\/+(res|uploaded)\//) {
85: $env{'request.state'} = "published";
86: } else {
87: $env{'request.state'} = 'unknown';
88: }
89: $env{'request.filename'} = $r->filename;
90: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
91: # -------------------------------------------------------- Load POST parameters
92:
93: &Apache::loncommon::get_posted_cgi($r);
94:
95: # ---------------------------------------------------------------- Check access
96:
97: if ($requrl!~/^\/adm|public|prtspool\//) {
98: my $access=&Apache::lonnet::allowed('bre',$requrl);
99: if ($access eq '1') {
100: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
101: return HTTP_NOT_ACCEPTABLE;
102: }
103: if (($access ne '2') && ($access ne 'F')) {
104: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
105: return HTTP_NOT_ACCEPTABLE;
106: }
107: }
108: if ($requrl =~ m|^/prtspool/|) {
109: my $start='/prtspool/'.$env{'user.name'}.'_'.
110: $env{'user.domain'};
111: if ($requrl !~ /^\Q$start\E/) {
112: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
113: return HTTP_NOT_ACCEPTABLE;
114: }
115: }
116: if ($env{'user.name'} eq 'public' &&
117: $env{'user.domain'} eq 'public' &&
118: $requrl !~ m{^/+(res|public)/} &&
119: $requrl !~ m{^/+adm/(help|logout|randomlabel\.png)}) {
120: $env{'request.querystring'}=$r->args;
121: $env{'request.firsturl'}=$requrl;
122: return FORBIDDEN;
123: }
124: # ------------------------------------------------------------- This is allowed
125: if ($env{'request.course.id'}) {
126: &Apache::lonnet::countacc($requrl);
127: $requrl=~/\.(\w+)$/;
128: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
129: ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$)/) ||
130: ($requrl=~/^\/adm\/wrapper\//) ||
131: ($requrl=~m|\.problem/smpedit$|) ||
132: ($requrl=~/^\/public\/.*\/syllabus$/)) {
133: # ------------------------------------- This is serious stuff, get symb and log
134: my $query=$r->args;
135: my $symb;
136: if ($query) {
137: &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
138: }
139: if ($env{'form.symb'}) {
140: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
141: if ($requrl =~ m|^/adm/wrapper/|) {
142: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
143: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
144: 'last_known' =>[$murl,$mid]);
145: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
146: (($requrl=~m|(.*)/smpedit$|) &&
147: &Apache::lonnet::symbverify($symb,$1))) {
148: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
149: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
150: 'last_known' =>[$murl,$mid]);
151: } else {
152: $r->log_reason('Invalid symb for '.$requrl.': '.
153: $symb);
154: $env{'user.error.msg'}=
155: "$requrl:bre:1:1:Invalid Access";
156: return HTTP_NOT_ACCEPTABLE;
157: }
158: } else {
159: $symb=&Apache::lonnet::symbread($requrl);
160: if (&Apache::lonnet::is_on_map($requrl) && $symb &&
161: !&Apache::lonnet::symbverify($symb,$requrl)) {
162: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
163: $env{'user.error.msg'}=
164: "$requrl:bre:1:1:Invalid Access";
165: return HTTP_NOT_ACCEPTABLE;
166: }
167: if ($symb) {
168: my ($map,$mid,$murl)=
169: &Apache::lonnet::decode_symb($symb);
170: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
171: 'last_known' =>[$murl,$mid]);
172: }
173: }
174: $env{'request.symb'}=$symb;
175: &Apache::lonnet::courseacclog($symb);
176: } else {
177: # ------------------------------------------------------- This is other content
178: &Apache::lonnet::courseacclog($requrl);
179: }
180: }
181: return OK;
182: } else {
183: $r->log_reason("Cookie $handle not valid", $r->filename)
184: };
185: }
186:
187: # -------------------------------------------- See if this is a public resource
188: if ($requrl=~m|^/public/|
189: || (&Apache::lonnet::metadata($requrl,'copyright') eq 'public')) {
190: &Apache::lonnet::logthis('Granting public access: '.$requrl);
191: my $cookie=
192: &Apache::lonauth::success($r,'public','public','public');
193: my $lonidsdir=$r->dir_config('lonIDsDir');
194: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$cookie);
195: &Apache::loncommon::get_posted_cgi($r);
196: $env{'request.state'} = "published";
197: $env{'request.publicaccess'} = 1;
198: $env{'request.filename'} = $r->filename;
199:
200: $r->header_out('Set-cookie',"lonID=$cookie; path=/");
201: return OK;
202: }
203: if ($requrl=~m|^/+adm/+help/+|) {
204: return OK;
205: }
206: # -------------------------------------------------------------- Not authorized
207: $requrl=~/\.(\w+)$/;
208: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
209: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
210: # ($requrl=~m|^/prtspool/|)) {
211: # -------------------------- Store where they wanted to go and get login screen
212: $env{'request.querystring'}=$r->args;
213: $env{'request.firsturl'}=$requrl;
214: return FORBIDDEN;
215: # } else {
216: # --------------------------------------------------------------------- Goodbye
217: # return HTTP_BAD_REQUEST;
218: # }
219: }
220:
221: 1;
222: __END__
223:
224: =head1 NAME
225:
226: Apache::lonacc - Cookie Based Access Handler
227:
228: =head1 SYNOPSIS
229:
230: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
231:
232: PerlAccessHandler Apache::lonacc
233:
234: =head1 INTRODUCTION
235:
236: This module enables cookie based authentication and is used
237: to control access for many different LON-CAPA URIs.
238:
239: Whenever the client sends the cookie back to the server,
240: this cookie is handled by either lonacc.pm or loncacc.pm
241: (see srm.conf for what is invoked when). If
242: the cookie is missing or invalid, the user is re-challenged
243: for login information.
244:
245: This is part of the LearningOnline Network with CAPA project
246: described at http://www.lon-capa.org.
247:
248: =head1 HANDLER SUBROUTINE
249:
250: This routine is called by Apache and mod_perl.
251:
252: =over 4
253:
254: =item *
255:
256: transfer profile into environment
257:
258: =item *
259:
260: load POST parameters
261:
262: =item *
263:
264: check access
265:
266: =item *
267:
268: if allowed, get symb, log, generate course statistics if applicable
269:
270: =item *
271:
272: otherwise return error
273:
274: =item *
275:
276: see if public resource
277:
278: =item *
279:
280: store attempted access
281:
282: =back
283:
284: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>