Annotation of loncom/auth/lonacc.pm, revision 1.116.2.2
1.1 albertel 1: # The LearningOnline Network
2: # Cookie Based Access Handler
1.22 www 3: #
1.116.2.2! raeburn 4: # $Id: lonacc.pm,v 1.116.2.1 2008/12/10 16:42:51 raeburn 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.16 www 40: use Fcntl qw(:flock);
1.85 raeburn 41: use LONCAPA;
1.1 albertel 42:
1.75 albertel 43: sub cleanup {
44: my ($r)=@_;
45: if (! $r->is_initial_req()) { return DECLINED; }
46: &Apache::lonnet::save_cache();
1.96 albertel 47: &Apache::lontexconvert::jsMath_reset();
1.75 albertel 48: return OK;
49: }
50:
51: sub goodbye {
52: my ($r)=@_;
53: &Apache::lonnet::goodbye();
54: return DONE;
55: }
56:
1.76 albertel 57: ###############################################
58:
59: sub get_posted_cgi {
1.114 raeburn 60: my ($r,$fields) = @_;
1.76 albertel 61:
62: my $buffer;
63: if ($r->header_in('Content-length')) {
64: $r->read($buffer,$r->header_in('Content-length'),0);
65: }
1.113 albertel 66: my $content_type = $r->header_in('Content-type');
67: if ($content_type !~ m{^multipart/form-data}) {
1.76 albertel 68: my @pairs=split(/&/,$buffer);
69: my $pair;
70: foreach $pair (@pairs) {
71: my ($name,$value) = split(/=/,$pair);
72: $value =~ tr/+/ /;
73: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
74: $name =~ tr/+/ /;
75: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.114 raeburn 76: if (ref($fields) eq 'ARRAY') {
77: next if (!grep(/^\Q$name\E$/,@{$fields}));
78: }
1.77 albertel 79: &Apache::loncommon::add_to_env("form.$name",$value);
1.76 albertel 80: }
81: } else {
1.113 albertel 82: my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
1.76 albertel 83: my @lines = split (/\n/,$buffer);
84: my $name='';
85: my $value='';
86: my $fname='';
87: my $fmime='';
88: my $i;
89: for ($i=0;$i<=$#lines;$i++) {
1.113 albertel 90: if ($lines[$i]=~/^--\Q$contentsep\E/) {
1.76 albertel 91: if ($name) {
92: chomp($value);
1.116.2.1 raeburn 93: if (ref($fields) eq 'ARRAY') {
94: next if (!grep(/^\Q$name\E$/,@{$fields}));
95: }
1.76 albertel 96: if ($fname) {
1.116.2.1 raeburn 97: if ($env{'form.symb'} ne '') {
98: my $size = (length($value))/(1024.0 * 1024.0);
99: if (&upload_size_allowed($name,$size,$fname) eq 'ok') {
100: $env{"form.$name.filename"}=$fname;
101: $env{"form.$name.mimetype"}=$fmime;
102: &Apache::loncommon::add_to_env("form.$name",$value);
103: }
104: } else {
105: $env{"form.$name.filename"}=$fname;
106: $env{"form.$name.mimetype"}=$fmime;
107: &Apache::loncommon::add_to_env("form.$name",$value);
108: }
1.76 albertel 109: } else {
110: $value=~s/\s+$//s;
1.116.2.1 raeburn 111: &Apache::loncommon::add_to_env("form.$name",$value);
1.76 albertel 112: }
113: }
114: if ($i<$#lines) {
115: $i++;
116: $lines[$i]=~
117: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
118: $name=$1;
119: $value='';
120: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
121: $fname=$1;
122: if
123: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
124: $fmime=$1;
125: $i++;
126: } else {
127: $fmime='';
128: }
129: } else {
130: $fname='';
131: $fmime='';
132: }
133: $i++;
134: }
135: } else {
136: $value.=$lines[$i]."\n";
137: }
138: }
139: }
140: #
141: # Digested POSTed values
142: #
143: # Remember the way this was originally done (GET or POST)
144: #
145: $env{'request.method'}=$ENV{'REQUEST_METHOD'};
146: #
147: # There may also be stuff in the query string
148: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
149: # Also, unset POSTed content length to cover all tracks.
150: #
151:
152: $r->method_number(M_GET);
153:
154: $r->method('GET');
155: $r->headers_in->unset('Content-length');
156: }
157:
1.116.2.1 raeburn 158: #
159: # Perform size checks for file uploads to essayresponse items in course context.
160: #
161: # Add form.HWFILESIZE.$part_$id to %env with file size (MB)
162: # If file exceeds maximum allowed size, add form.HWFILETOOBIG.$part_$id to %env.
163: #
164:
165: sub upload_size_allowed {
166: my ($name,$size,$fname) = @_;
167: if ($name =~ /^HWFILE(\w+)$/) {
168: my $ident = $1;
169: my $item = 'HWFILESIZE'.$ident;
170: my $savesize = sprintf("%.6f",$size);
171: &Apache::loncommon::add_to_env("form.$item",$savesize);
172: my $maxsize= &Apache::lonnet::EXT("resource.$ident.maxfilesize");
173: if (!$maxsize) {
174: $maxsize = 10.0; # FIXME This should become a domain configuration.
175: }
176: if ($size > $maxsize) {
177: my $warn = 'HWFILETOOBIG'.$ident;
178: &Apache::loncommon::add_to_env("form.$warn",$fname);
179: return;
180: }
181: }
182: return 'ok';
183: }
184:
185:
1.94 albertel 186: # handle the case of the single sign on user, at this point $r->user
187: # will be set and valid now need to find the loncapa user info and possibly
188: # balance them
189: # returns OK if it was a SSO and user was handled
190: # undef if not SSO or no means to hanle the user
191: sub sso_login {
1.111 albertel 192: my ($r,$handle) = @_;
1.94 albertel 193:
194: my $lonidsdir=$r->dir_config('lonIDsDir');
195: if (!($r->user
196: && (!defined($env{'user.name'}) && !defined($env{'user.domain'}))
1.111 albertel 197: && ($handle eq ''))) {
1.94 albertel 198: # not an SSO case or already logged in
199: return undef;
200: }
201:
1.97 albertel 202: my ($user) = ($r->user =~ m/([a-zA-Z0-9_\-@.]*)/);
203:
1.94 albertel 204: my $domain = $r->dir_config('lonDefDomain');
1.97 albertel 205: my $home=&Apache::lonnet::homeserver($user,$domain);
1.94 albertel 206: if ($home !~ /(con_lost|no_host|no_such_host)/) {
1.107 albertel 207: &Apache::lonnet::logthis(" SSO authorized user $user ");
1.94 albertel 208: if ($r->dir_config("lonBalancer") eq 'yes') {
209: # login but immeaditly go to switch server to find us a new
210: # machine
1.97 albertel 211: &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
1.105 raeburn 212: $env{'request.sso.login'} = 1;
1.106 raeburn 213: if (defined($r->dir_config("lonSSOReloginServer"))) {
214: $env{'request.sso.reloginserver'} =
215: $r->dir_config('lonSSOReloginServer');
216: }
1.94 albertel 217: $r->internal_redirect('/adm/switchserver');
1.101 albertel 218: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 219: } else {
220: # need to login them in, so generate the need data that
221: # migrate expects to do login
222: my %info=('ip' => $r->connection->remote_ip(),
223: 'domain' => $domain,
1.97 albertel 224: 'username' => $user,
1.94 albertel 225: 'server' => $r->dir_config('lonHostID'),
226: 'sso.login' => 1
227: );
1.116 raeburn 228: if ($r->dir_config("ssodirecturl") == 1) {
229: $info{'origurl'} = $r->uri;
230: }
1.106 raeburn 231: if (defined($r->dir_config("lonSSOReloginServer"))) {
232: $info{'sso.reloginserver'} =
233: $r->dir_config('lonSSOReloginServer');
234: }
1.94 albertel 235: my $token =
236: &Apache::lonnet::tmpput(\%info,
237: $r->dir_config('lonHostID'));
238: $env{'form.token'} = $token;
239: $r->internal_redirect('/adm/migrateuser');
1.101 albertel 240: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 241: }
242: return OK;
1.98 albertel 243: } elsif (defined($r->dir_config('lonSSOUserUnknownRedirect'))) {
1.107 albertel 244: &Apache::lonnet::logthis(" SSO authorized unknown user $user ");
1.104 raeburn 245: $r->subprocess_env->set('SSOUserUnknown' => $user);
246: $r->subprocess_env->set('SSOUserDomain' => $domain);
1.115 raeburn 247: my @cancreate;
248: my %domconfig =
249: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
250: if (ref($domconfig{'usercreation'}) eq 'HASH') {
251: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
252: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
253: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
254: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
255: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
256: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
257: }
258: }
259: }
260: if (grep(/^sso$/,@cancreate)) {
261: $r->internal_redirect('/adm/createaccount');
262: } else {
263: $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
264: }
1.101 albertel 265: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 266: return OK;
267: }
268: return undef;
269: }
270:
1.1 albertel 271: sub handler {
272: my $r = shift;
273: my $requrl=$r->uri;
1.108 raeburn 274: if (&Apache::lonnet::is_domainimage($requrl)) {
275: return OK;
276: }
1.70 albertel 277:
1.116.2.2! raeburn 278: if ($requrl =~ m{^/res/adm/pages/[^/]+\.(gif|png)$}) {
! 279: return OK;
! 280: }
! 281:
1.111 albertel 282: my $handle = &Apache::lonnet::check_for_valid_session($r);
1.93 albertel 283:
1.111 albertel 284: my $result = &sso_login($r,$handle);
1.100 albertel 285: if (defined($result)) {
1.116 raeburn 286: return $result;
1.70 albertel 287: }
288:
1.94 albertel 289:
1.70 albertel 290: if ($r->dir_config("lonBalancer") eq 'yes') {
291: $r->set_handlers('PerlResponseHandler'=>
292: [\&Apache::switchserver::handler]);
293: }
1.92 albertel 294:
295: if ($handle eq '') {
296: $r->log_reason("Cookie $handle not valid", $r->filename);
1.111 albertel 297: } elsif ($handle ne '') {
1.6 www 298:
1.46 www 299: # ------------------------------------------------------ Initialize Environment
1.111 albertel 300: my $lonidsdir=$r->dir_config('lonIDsDir');
1.92 albertel 301: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.47 www 302:
303: # --------------------------------------------------------- Initialize Language
304:
1.92 albertel 305: &Apache::lonlocal::get_language_handle($r);
306:
307: }
1.46 www 308:
1.92 albertel 309: # -------------------------------------------------- Should be a valid user now
310: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
1.46 www 311: # -------------------------------------------------------------- Resource State
1.6 www 312:
1.92 albertel 313: if ($requrl=~/^\/+(res|uploaded)\//) {
314: $env{'request.state'} = "published";
315: } else {
316: $env{'request.state'} = 'unknown';
317: }
318: $env{'request.filename'} = $r->filename;
319: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
1.6 www 320: # -------------------------------------------------------- Load POST parameters
321:
1.92 albertel 322: &Apache::lonacc::get_posted_cgi($r);
1.6 www 323:
324: # ---------------------------------------------------------------- Check access
1.92 albertel 325: my $now = time;
1.95 albertel 326: if ($requrl !~ m{^/(?:adm|public|prtspool)/}
327: || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
1.92 albertel 328: my $access=&Apache::lonnet::allowed('bre',$requrl);
329: if ($access eq '1') {
330: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
331: return HTTP_NOT_ACCEPTABLE;
332: }
333: if ($access eq 'A') {
334: &Apache::restrictedaccess::setup_handler($r);
335: return OK;
336: }
1.103 raeburn 337: if ($access eq 'B') {
338: &Apache::blockedaccess::setup_handler($r);
339: return OK;
340: }
1.92 albertel 341: if (($access ne '2') && ($access ne 'F')) {
342: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
343: return HTTP_NOT_ACCEPTABLE;
1.37 albertel 344: }
1.92 albertel 345: }
346: if ($requrl =~ m|^/prtspool/|) {
347: my $start='/prtspool/'.$env{'user.name'}.'_'.
348: $env{'user.domain'};
349: if ($requrl !~ /^\Q$start\E/) {
350: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
351: return HTTP_NOT_ACCEPTABLE;
1.67 albertel 352: }
1.92 albertel 353: }
1.109 banghart 354: if ($requrl =~ m|^/zipspool/|) {
1.110 banghart 355: my $start='/zipspool/zipout/'.$env{'user.name'}.":".
1.109 banghart 356: $env{'user.domain'};
357: if ($requrl !~ /^\Q$start\E/) {
358: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
359: return HTTP_NOT_ACCEPTABLE;
360: }
361: }
1.92 albertel 362: if ($env{'user.name'} eq 'public' &&
363: $env{'user.domain'} eq 'public' &&
364: $requrl !~ m{^/+(res|public|uploaded)/} &&
365: $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
366: $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
367: $env{'request.querystring'}=$r->args;
368: $env{'request.firsturl'}=$requrl;
369: return FORBIDDEN;
370: }
1.23 www 371: # ------------------------------------------------------------- This is allowed
1.92 albertel 372: if ($env{'request.course.id'}) {
1.24 www 373: &Apache::lonnet::countacc($requrl);
1.92 albertel 374: $requrl=~/\.(\w+)$/;
375: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
376: ($requrl=~/^\/adm\/.*\/(aboutme|navmaps|smppg|bulletinboard)(\?|$ )/x) ||
377: ($requrl=~/^\/adm\/wrapper\//) ||
378: ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
379: ($requrl=~m|\.problem/smpedit$|) ||
380: ($requrl=~/^\/public\/.*\/syllabus$/)) {
1.23 www 381: # ------------------------------------- This is serious stuff, get symb and log
1.29 www 382: my $query=$r->args;
1.92 albertel 383: my $symb;
384: if ($query) {
1.29 www 385: &Apache::loncommon::get_unprocessed_cgi($query,['symb']);
1.92 albertel 386: }
387: if ($env{'form.symb'}) {
1.64 albertel 388: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.92 albertel 389: if ($requrl =~ m|^/adm/wrapper/|
1.72 albertel 390: || $requrl =~ m|^/adm/coursedocs/showdoc/|) {
1.92 albertel 391: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
392: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
1.63 albertel 393: 'last_known' =>[$murl,$mid]);
1.92 albertel 394: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
1.53 albertel 395: (($requrl=~m|(.*)/smpedit$|) &&
396: &Apache::lonnet::symbverify($symb,$1))) {
1.92 albertel 397: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
398: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
399: 'last_known' =>[$murl,$mid]);
1.31 www 400: } else {
401: $r->log_reason('Invalid symb for '.$requrl.': '.
1.92 albertel 402: $symb);
403: $env{'user.error.msg'}=
404: "$requrl:bre:1:1:Invalid Access";
405: return HTTP_NOT_ACCEPTABLE;
406: }
407: } else {
408: $symb=&Apache::lonnet::symbread($requrl);
1.58 albertel 409: if (&Apache::lonnet::is_on_map($requrl) && $symb &&
1.56 albertel 410: !&Apache::lonnet::symbverify($symb,$requrl)) {
1.58 albertel 411: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1.92 albertel 412: $env{'user.error.msg'}=
413: "$requrl:bre:1:1:Invalid Access";
414: return HTTP_NOT_ACCEPTABLE;
1.55 albertel 415: }
1.61 albertel 416: if ($symb) {
1.65 albertel 417: my ($map,$mid,$murl)=
418: &Apache::lonnet::decode_symb($symb);
1.63 albertel 419: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
1.92 albertel 420: 'last_known' =>[$murl,$mid]);
1.61 albertel 421: }
1.92 albertel 422: }
423: $env{'request.symb'}=$symb;
424: &Apache::lonnet::courseacclog($symb);
425: } else {
1.23 www 426: # ------------------------------------------------------- This is other content
1.92 albertel 427: &Apache::lonnet::courseacclog($requrl);
428: }
1.88 albertel 429: }
1.92 albertel 430: return OK;
431: }
1.21 www 432: # -------------------------------------------- See if this is a public resource
1.68 albertel 433: if ($requrl=~m|^/+adm/+help/+|) {
1.89 albertel 434: return OK;
1.68 albertel 435: }
1.90 albertel 436: # ------------------------------------ See if this is a viewable portfolio file
1.89 albertel 437: if (&Apache::lonnet::is_portfolio_url($requrl)) {
1.90 albertel 438: my $access=&Apache::lonnet::allowed('bre',$requrl);
439: if ($access eq 'A') {
440: &Apache::restrictedaccess::setup_handler($r);
441: return OK;
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: }
1.79 raeburn 447: }
1.87 albertel 448:
1.34 www 449: # -------------------------------------------------------------- Not authorized
450: $requrl=~/\.(\w+)$/;
1.62 albertel 451: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
452: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
453: # ($requrl=~m|^/prtspool/|)) {
1.34 www 454: # -------------------------- Store where they wanted to go and get login screen
1.64 albertel 455: $env{'request.querystring'}=$r->args;
456: $env{'request.firsturl'}=$requrl;
1.34 www 457: return FORBIDDEN;
1.62 albertel 458: # } else {
1.34 www 459: # --------------------------------------------------------------------- Goodbye
1.62 albertel 460: # return HTTP_BAD_REQUEST;
461: # }
1.1 albertel 462: }
463:
464: 1;
465: __END__
1.25 harris41 466:
467: =head1 NAME
468:
469: Apache::lonacc - Cookie Based Access Handler
470:
471: =head1 SYNOPSIS
472:
473: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
474:
475: PerlAccessHandler Apache::lonacc
476:
477: =head1 INTRODUCTION
478:
479: This module enables cookie based authentication and is used
480: to control access for many different LON-CAPA URIs.
481:
482: Whenever the client sends the cookie back to the server,
483: this cookie is handled by either lonacc.pm or loncacc.pm
484: (see srm.conf for what is invoked when). If
485: the cookie is missing or invalid, the user is re-challenged
486: for login information.
487:
488: This is part of the LearningOnline Network with CAPA project
489: described at http://www.lon-capa.org.
490:
491: =head1 HANDLER SUBROUTINE
492:
493: This routine is called by Apache and mod_perl.
494:
495: =over 4
496:
497: =item *
498:
499: transfer profile into environment
500:
501: =item *
502:
503: load POST parameters
504:
505: =item *
506:
507: check access
508:
509: =item *
510:
511: if allowed, get symb, log, generate course statistics if applicable
512:
513: =item *
514:
515: otherwise return error
516:
517: =item *
518:
519: see if public resource
520:
521: =item *
522:
523: store attempted access
524:
525: =back
526:
527: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>