Annotation of loncom/auth/lonacc.pm, revision 1.110.2.1
1.1 albertel 1: # The LearningOnline Network
2: # Cookie Based Access Handler
1.22 www 3: #
1.110.2.1! albertel 4: # $Id: lonacc.pm,v 1.110 2007/04/27 22:03:02 banghart Exp $
1.22 www 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: #
1.25 harris41 28: ###
1.1 albertel 29:
30: package Apache::lonacc;
31:
32: use strict;
1.8 www 33: use Apache::Constants qw(:common :http :methods);
1.2 www 34: use Apache::File;
1.6 www 35: use Apache::lonnet;
1.25 harris41 36: use Apache::loncommon();
1.47 www 37: use Apache::lonlocal;
1.86 albertel 38: use Apache::restrictedaccess();
1.103 raeburn 39: use Apache::blockedaccess();
1.1 albertel 40: use CGI::Cookie();
1.16 www 41: use Fcntl qw(:flock);
1.85 raeburn 42: use LONCAPA;
1.1 albertel 43:
1.75 albertel 44: sub cleanup {
45: my ($r)=@_;
46: if (! $r->is_initial_req()) { return DECLINED; }
47: &Apache::lonnet::save_cache();
1.96 albertel 48: &Apache::lontexconvert::jsMath_reset();
1.75 albertel 49: return OK;
50: }
51:
52: sub goodbye {
53: my ($r)=@_;
54: &Apache::lonnet::goodbye();
55: return DONE;
56: }
57:
1.76 albertel 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: }
1.110.2.1! albertel 67: my $content_type = $r->header_in('Content-type');
! 68: if ($content_type !~ m{^multipart/form-data}) {
1.76 albertel 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;
1.77 albertel 77: &Apache::loncommon::add_to_env("form.$name",$value);
1.76 albertel 78: }
79: } else {
1.110.2.1! albertel 80: my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
1.76 albertel 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++) {
1.110.2.1! albertel 88: if ($lines[$i]=~/^--\Q$contentsep\E/) {
1.76 albertel 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: }
1.77 albertel 97: &Apache::loncommon::add_to_env("form.$name",$value);
1.76 albertel 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:
1.94 albertel 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:
1.97 albertel 159: my ($user) = ($r->user =~ m/([a-zA-Z0-9_\-@.]*)/);
160:
1.94 albertel 161: my $domain = $r->dir_config('lonDefDomain');
1.97 albertel 162: my $home=&Apache::lonnet::homeserver($user,$domain);
1.94 albertel 163: if ($home !~ /(con_lost|no_host|no_such_host)/) {
1.107 albertel 164: &Apache::lonnet::logthis(" SSO authorized user $user ");
1.94 albertel 165: if ($r->dir_config("lonBalancer") eq 'yes') {
166: # login but immeaditly go to switch server to find us a new
167: # machine
1.97 albertel 168: &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
1.105 raeburn 169: $env{'request.sso.login'} = 1;
1.106 raeburn 170: if (defined($r->dir_config("lonSSOReloginServer"))) {
171: $env{'request.sso.reloginserver'} =
172: $r->dir_config('lonSSOReloginServer');
173: }
1.94 albertel 174: $r->internal_redirect('/adm/switchserver');
1.101 albertel 175: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 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,
1.97 albertel 181: 'username' => $user,
1.94 albertel 182: 'server' => $r->dir_config('lonHostID'),
183: 'sso.login' => 1
184: );
1.106 raeburn 185: if (defined($r->dir_config("lonSSOReloginServer"))) {
186: $info{'sso.reloginserver'} =
187: $r->dir_config('lonSSOReloginServer');
188: }
1.94 albertel 189: my $token =
190: &Apache::lonnet::tmpput(\%info,
191: $r->dir_config('lonHostID'));
192: $env{'form.token'} = $token;
193: $r->internal_redirect('/adm/migrateuser');
1.101 albertel 194: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 195: }
196: return OK;
1.98 albertel 197: } elsif (defined($r->dir_config('lonSSOUserUnknownRedirect'))) {
1.107 albertel 198: &Apache::lonnet::logthis(" SSO authorized unknown user $user ");
1.104 raeburn 199: $r->subprocess_env->set('SSOUserUnknown' => $user);
200: $r->subprocess_env->set('SSOUserDomain' => $domain);
1.98 albertel 201: $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
1.101 albertel 202: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 203: return OK;
204: }
205: return undef;
206: }
207:
1.1 albertel 208: sub handler {
209: my $r = shift;
210: my $requrl=$r->uri;
1.108 raeburn 211: if (&Apache::lonnet::is_domainimage($requrl)) {
212: return OK;
213: }
1.1 albertel 214: my %cookies=CGI::Cookie->parse($r->header_in('Cookie'));
215: my $lonid=$cookies{'lonID'};
216: my $cookie;
1.70 albertel 217: my $lonidsdir=$r->dir_config('lonIDsDir');
218:
219: my $handle;
1.1 albertel 220: if ($lonid) {
1.102 albertel 221: $handle=&LONCAPA::clean_handle($lonid->value);
1.70 albertel 222: }
1.93 albertel 223:
1.99 albertel 224: my $result = &sso_login($r,$lonid,$handle);
1.100 albertel 225: if (defined($result)) {
1.94 albertel 226: return $result
1.70 albertel 227: }
228:
1.94 albertel 229:
1.70 albertel 230: if ($r->dir_config("lonBalancer") eq 'yes') {
231: $r->set_handlers('PerlResponseHandler'=>
232: [\&Apache::switchserver::handler]);
233: }
1.92 albertel 234:
235: if ($handle eq '') {
236: $r->log_reason("Cookie $handle not valid", $r->filename);
237: } elsif ((-e "$lonidsdir/$handle.id") && ($handle ne '')) {
1.6 www 238:
1.46 www 239: # ------------------------------------------------------ Initialize Environment
240:
1.92 albertel 241: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.47 www 242:
243: # --------------------------------------------------------- Initialize Language
244:
1.92 albertel 245: &Apache::lonlocal::get_language_handle($r);
246:
247: }
1.46 www 248:
1.92 albertel 249: # -------------------------------------------------- Should be a valid user now
250: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
1.46 www 251: # -------------------------------------------------------------- Resource State
1.6 www 252:
1.92 albertel 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);
1.6 www 260: # -------------------------------------------------------- Load POST parameters
261:
1.92 albertel 262: &Apache::lonacc::get_posted_cgi($r);
1.6 www 263:
264: # ---------------------------------------------------------------- Check access
1.92 albertel 265: my $now = time;
1.95 albertel 266: if ($requrl !~ m{^/(?:adm|public|prtspool)/}
267: || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
1.92 albertel 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: }
1.103 raeburn 277: if ($access eq 'B') {
278: &Apache::blockedaccess::setup_handler($r);
279: return OK;
280: }
1.92 albertel 281: if (($access ne '2') && ($access ne 'F')) {
282: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
283: return HTTP_NOT_ACCEPTABLE;
1.37 albertel 284: }
1.92 albertel 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;
1.67 albertel 292: }
1.92 albertel 293: }
1.109 banghart 294: if ($requrl =~ m|^/zipspool/|) {
1.110 banghart 295: my $start='/zipspool/zipout/'.$env{'user.name'}.":".
1.109 banghart 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: }
1.92 albertel 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: }
1.23 www 311: # ------------------------------------------------------------- This is allowed
1.92 albertel 312: if ($env{'request.course.id'}) {
1.24 www 313: &Apache::lonnet::countacc($requrl);
1.92 albertel 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$/)) {
1.23 www 321: # ------------------------------------- This is serious stuff, get symb and log
1.29 www 322: my $query=$r->args;
1.92 albertel 323: my $symb;
324: if ($query) {
1.29 www 325: &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
1.92 albertel 326: }
327: if ($env{'form.symb'}) {
1.64 albertel 328: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.92 albertel 329: if ($requrl =~ m|^/adm/wrapper/|
1.72 albertel 330: || $requrl =~ m|^/adm/coursedocs/showdoc/|) {
1.92 albertel 331: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
332: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
1.63 albertel 333: 'last_known' =>[$murl,$mid]);
1.92 albertel 334: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
1.53 albertel 335: (($requrl=~m|(.*)/smpedit$|) &&
336: &Apache::lonnet::symbverify($symb,$1))) {
1.92 albertel 337: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
338: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
339: 'last_known' =>[$murl,$mid]);
1.31 www 340: } else {
341: $r->log_reason('Invalid symb for '.$requrl.': '.
1.92 albertel 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);
1.58 albertel 349: if (&Apache::lonnet::is_on_map($requrl) && $symb &&
1.56 albertel 350: !&Apache::lonnet::symbverify($symb,$requrl)) {
1.58 albertel 351: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1.92 albertel 352: $env{'user.error.msg'}=
353: "$requrl:bre:1:1:Invalid Access";
354: return HTTP_NOT_ACCEPTABLE;
1.55 albertel 355: }
1.61 albertel 356: if ($symb) {
1.65 albertel 357: my ($map,$mid,$murl)=
358: &Apache::lonnet::decode_symb($symb);
1.63 albertel 359: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
1.92 albertel 360: 'last_known' =>[$murl,$mid]);
1.61 albertel 361: }
1.92 albertel 362: }
363: $env{'request.symb'}=$symb;
364: &Apache::lonnet::courseacclog($symb);
365: } else {
1.23 www 366: # ------------------------------------------------------- This is other content
1.92 albertel 367: &Apache::lonnet::courseacclog($requrl);
368: }
1.88 albertel 369: }
1.92 albertel 370: return OK;
371: }
1.21 www 372: # -------------------------------------------- See if this is a public resource
1.68 albertel 373: if ($requrl=~m|^/+adm/+help/+|) {
1.89 albertel 374: return OK;
1.68 albertel 375: }
1.90 albertel 376: # ------------------------------------ See if this is a viewable portfolio file
1.89 albertel 377: if (&Apache::lonnet::is_portfolio_url($requrl)) {
1.90 albertel 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: }
1.79 raeburn 387: }
1.87 albertel 388:
1.34 www 389: # -------------------------------------------------------------- Not authorized
390: $requrl=~/\.(\w+)$/;
1.62 albertel 391: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
392: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
393: # ($requrl=~m|^/prtspool/|)) {
1.34 www 394: # -------------------------- Store where they wanted to go and get login screen
1.64 albertel 395: $env{'request.querystring'}=$r->args;
396: $env{'request.firsturl'}=$requrl;
1.34 www 397: return FORBIDDEN;
1.62 albertel 398: # } else {
1.34 www 399: # --------------------------------------------------------------------- Goodbye
1.62 albertel 400: # return HTTP_BAD_REQUEST;
401: # }
1.1 albertel 402: }
403:
404: 1;
405: __END__
1.25 harris41 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>