File:
[LON-CAPA] /
loncom /
auth /
lonacc.pm
Revision
1.159.2.21.2.1:
download - view:
text,
annotated -
select for diffs
Thu Dec 30 04:47:38 2021 UTC (2 years, 7 months ago) by
raeburn
Branches:
version_2_11_4_msu
Diff to branchpoint 1.159.2.21:
preferred,
unified
- For 2.11.4 (modified)
Include changes in 1.169, 1.175, 1.178, 1.192, 1.193, 1.194, 1.195,
1.197, 1.200 (part).
1: # The LearningOnline Network
2: # Cookie Based Access Handler
3: #
4: # $Id: lonacc.pm,v 1.159.2.21.2.1 2021/12/30 04:47:38 raeburn 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: =head1 NAME
31:
32: Apache::lonacc - Cookie Based Access Handler
33:
34: =head1 SYNOPSIS
35:
36: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
37:
38: PerlAccessHandler Apache::lonacc
39:
40: =head1 INTRODUCTION
41:
42: This module enables cookie based authentication and is used
43: to control access for many different LON-CAPA URIs.
44:
45: Whenever the client sends the cookie back to the server,
46: this cookie is handled by either lonacc.pm or loncacc.pm
47: (see srm.conf for what is invoked when). If
48: the cookie is missing or invalid, the user is re-challenged
49: for login information.
50:
51: This is part of the LearningOnline Network with CAPA project
52: described at http://www.lon-capa.org.
53:
54: =head1 HANDLER SUBROUTINE
55:
56: This routine is called by Apache and mod_perl.
57:
58: =over 4
59:
60: =item *
61:
62: transfer profile into environment
63:
64: =item *
65:
66: load POST parameters
67:
68: =item *
69:
70: check access
71:
72: =item *
73:
74: if allowed, get symb, log, generate course statistics if applicable
75:
76: =item *
77:
78: otherwise return error
79:
80: =item *
81:
82: see if public resource
83:
84: =item *
85:
86: store attempted access
87:
88: =back
89:
90: =head1 NOTABLE SUBROUTINES
91:
92: =cut
93:
94:
95: package Apache::lonacc;
96:
97: use strict;
98: use Apache::Constants qw(:common :http :methods);
99: use Apache::File;
100: use Apache::lonnet;
101: use Apache::loncommon();
102: use Apache::lonlocal;
103: use Apache::restrictedaccess();
104: use Apache::blockedaccess();
105: use Apache::lonprotected();
106: use Fcntl qw(:flock);
107: use LONCAPA qw(:DEFAULT :match);
108:
109: sub cleanup {
110: my ($r)=@_;
111: if (! $r->is_initial_req()) { return DECLINED; }
112: &Apache::lonnet::save_cache();
113: return OK;
114: }
115:
116: sub goodbye {
117: my ($r)=@_;
118: &Apache::lonnet::goodbye();
119: return DONE;
120: }
121:
122: ###############################################
123:
124: sub get_posted_cgi {
125: my ($r,$fields) = @_;
126:
127: my $buffer;
128: if ($r->header_in('Content-length')) {
129: $r->read($buffer,$r->header_in('Content-length'),0);
130: }
131: my $content_type = $r->header_in('Content-type');
132: if ($content_type !~ m{^multipart/form-data}) {
133: my @pairs=split(/&/,$buffer);
134: my $pair;
135: foreach $pair (@pairs) {
136: my ($name,$value) = split(/=/,$pair);
137: $value =~ tr/+/ /;
138: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
139: $name =~ tr/+/ /;
140: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
141: if (ref($fields) eq 'ARRAY') {
142: next if (!grep(/^\Q$name\E$/,@{$fields}));
143: }
144: &Apache::loncommon::add_to_env("form.$name",$value);
145: }
146: } else {
147: my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
148: my @lines = split (/\n/,$buffer);
149: my $name='';
150: my $value='';
151: my $fname='';
152: my $fmime='';
153: my $i;
154: for ($i=0;$i<=$#lines;$i++) {
155: if ($lines[$i]=~/^--\Q$contentsep\E/) {
156: if ($name) {
157: chomp($value);
158: if (($r->uri eq '/adm/portfolio') &&
159: ($name eq 'uploaddoc')) {
160: if (length($value) == 1) {
161: $value=~s/[\r\n]$//;
162: }
163: }
164: if ($fname =~ /\.(xls|doc|ppt)(x|m)$/i) {
165: $value=~s/[\r\n]$//;
166: }
167: if (ref($fields) eq 'ARRAY') {
168: next if (!grep(/^\Q$name\E$/,@{$fields}));
169: }
170: if ($fname) {
171: if ($env{'form.symb'} ne '') {
172: my $size = (length($value))/(1024.0 * 1024.0);
173: if (&upload_size_allowed($name,$size,$fname) eq 'ok') {
174: $env{"form.$name.filename"}=$fname;
175: $env{"form.$name.mimetype"}=$fmime;
176: &Apache::loncommon::add_to_env("form.$name",$value);
177: }
178: } else {
179: $env{"form.$name.filename"}=$fname;
180: $env{"form.$name.mimetype"}=$fmime;
181: &Apache::loncommon::add_to_env("form.$name",$value);
182: }
183: } else {
184: $value=~s/\s+$//s;
185: &Apache::loncommon::add_to_env("form.$name",$value);
186: }
187: }
188: if ($i<$#lines) {
189: $i++;
190: $lines[$i]=~
191: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
192: $name=$1;
193: $value='';
194: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
195: $fname=$1;
196: if
197: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
198: $fmime=$1;
199: $i++;
200: } else {
201: $fmime='';
202: }
203: } else {
204: $fname='';
205: $fmime='';
206: }
207: $i++;
208: }
209: } else {
210: $value.=$lines[$i]."\n";
211: }
212: }
213: }
214: #
215: # Digested POSTed values
216: #
217: # Remember the way this was originally done (GET or POST)
218: #
219: $env{'request.method'}=$ENV{'REQUEST_METHOD'};
220: #
221: # There may also be stuff in the query string
222: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
223: # Also, unset POSTed content length to cover all tracks.
224: #
225:
226: $r->method_number(M_GET);
227:
228: $r->method('GET');
229: $r->headers_in->unset('Content-length');
230: }
231:
232: =pod
233:
234: =over
235:
236: =item upload_size_allowed()
237:
238: Perform size checks for file uploads to essayresponse items in course context.
239:
240: Add form.HWFILESIZE.$part_$id to %env with file size (MB)
241: If file exceeds maximum allowed size, add form.HWFILETOOBIG.$part_$id to %env.
242:
243: =cut
244:
245: sub upload_size_allowed {
246: my ($name,$size,$fname) = @_;
247: if ($name =~ /^HWFILE(\w+)$/) {
248: my $ident = $1;
249: my $item = 'HWFILESIZE'.$ident;
250: my $savesize = sprintf("%.6f",$size);
251: &Apache::loncommon::add_to_env("form.$item",$savesize);
252: my $maxsize= &Apache::lonnet::EXT("resource.$ident.maxfilesize");
253: if (!$maxsize) {
254: $maxsize = 10.0; # FIXME This should become a domain configuration.
255: }
256: if ($size > $maxsize) {
257: my $warn = 'HWFILETOOBIG'.$ident;
258: &Apache::loncommon::add_to_env("form.$warn",$fname);
259: return;
260: }
261: }
262: return 'ok';
263: }
264:
265: =pod
266:
267: =item sso_login()
268:
269: handle the case of the single sign on user, at this point $r->user
270: will be set and valid; now need to find the loncapa user info, and possibly
271: balance them. If $r->user() is set this means either it was either set by
272: SSO or by checkauthen.pm, if a valid cookie was found. The latter case can
273: be identified by the third arg ($usename), except when lonacc is called in
274: an internal redirect to /adm/switchserver (e.g., load-balancing following
275: successful authentication) -- no cookie set yet. For that particular case
276: simply skip the call to sso_login().
277:
278: returns OK if it was SSO and user was handled.
279: returns undef if not SSO or no means to handle the user.
280:
281: =cut
282:
283: sub sso_login {
284: my ($r,$handle,$username) = @_;
285:
286: if (($r->user eq '') || ($username ne '') || ($r->user eq 'public:public') ||
287: (defined($env{'user.name'}) && (defined($env{'user.domain'}))
288: && ($handle ne ''))) {
289: # not an SSO case or already logged in
290: return undef;
291: }
292:
293: my ($user) = ($r->user =~ m/^($match_username)$/);
294: if ($user eq '') {
295: return undef;
296: }
297:
298: my $query = $r->args;
299: my %form;
300: if ($query) {
301:
302: my @items = ('role','symb','iptoken','origurl','ttoken',
303: 'ltoken','linkkey','logtoken','sso');
304: &Apache::loncommon::get_unprocessed_cgi($query,\@items);
305: foreach my $item (@items) {
306: if (defined($env{'form.'.$item})) {
307: $form{$item} = $env{'form.'.$item};
308: }
309: }
310: }
311:
312: my %sessiondata;
313: if ($form{'iptoken'}) {
314: %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
315: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
316: unless ($sessiondata{'sessionserver'}) {
317: delete($form{'iptoken'});
318: }
319: }
320:
321: my ($linkprot,$linkkey);
322:
323: #
324: # If Shibboleth auth is in use, and a dual SSO and non-SSO login page
325: # is in use, then the query string will contain the logtoken item with
326: # a value set to the name of a .tmp file in /home/httpd/perl/tmp
327: # containing the url to display after authentication, and also,
328: # optionally, role and symb, or linkprot or linkkey (deep-link access).
329: #
330: # If Shibboleth auth is in use, but a dual log-in page is not in use,
331: # and the originally requested URL was /tiny/$domain/$id (i.e.,
332: # for deeplinking), then the query string will contain the sso item
333: # with a value set to the name of a .tmp file in /home/httpd/perl/tmp
334: # containing the url to display after authentication, and also,
335: # optionally, linkprot or linkkey (deep-link access).
336: #
337: # Otherwise the query string may contain role and symb, or if the
338: # originally requested URL was /tiny/$domain/$id (i.e. for deeplinking)
339: # then the query string may contain a ttoken item with a value set
340: # to the name of a .tmp file in /home/httpd/perl/tmp containing either
341: # linkprot or linkkey (deep-link access).
342: #
343: # If deep-linked, i.e., the originally requested URL was /tiny/$domain/$id
344: # the linkkey may have originally been sent in POSTed data, which will
345: # have been processed in lontrans.pm
346: #
347:
348: if ($form{'ttoken'}) {
349: my %info = &Apache::lonnet::tmpget($form{'ttoken'});
350: &Apache::lonnet::tmpdel($form{'ttoken'});
351: if ($info{'origurl'}) {
352: $form{'origurl'} = $info{'origurl'};
353: }
354: if ($info{'linkprot'}) {
355: $linkprot = $info{'linkprot'};
356: } elsif ($info{'linkkey'} ne '') {
357: $linkkey = $info{'linkkey'};
358: }
359: } elsif ($form{'logtoken'}) {
360: my ($firsturl,@rest);
361: my $lonhost = $r->dir_config('lonHostID');
362: my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},$lonhost);
363: my $delete = &Apache::lonnet::tmpdel($form{'logtoken'});
364: unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
365: ($tmpinfo eq 'no_such_host')) {
366: (undef,$firsturl,@rest) = split(/&/,$tmpinfo);
367: if ($firsturl ne '') {
368: $firsturl = &unescape($firsturl);
369: }
370: foreach my $item (@rest) {
371: my ($key,$value) = split(/=/,$item);
372: $form{$key} = &unescape($value);
373: }
374: if ($firsturl =~ m{^/tiny/$match_domain/\w+$}) {
375: $form{'origurl'} = $firsturl;
376: }
377: if ($form{'linkprot'}) {
378: $linkprot = $form{'linkprot'};
379: } elsif ($form{'linkkey'} ne '') {
380: $linkkey = $form{'linkkey'};
381: }
382: if ($form{'iptoken'}) {
383: %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
384: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
385: }
386: }
387: } elsif ($form{'sso'}) {
388: my $lonhost = $r->dir_config('lonHostID');
389: my $info = &Apache::lonnet::reply('tmpget:'.$form{'sso'},$lonhost);
390: &Apache::lonnet::tmpdel($form{'sso'});
391: unless (($info=~/^error/) || ($info eq 'con_lost') ||
392: ($info eq 'no_such_host')) {
393: my ($firsturl,@rest)=split(/\&/,$info);
394: if ($firsturl ne '') {
395: $form{'origurl'} = &unescape($firsturl);
396: }
397: foreach my $item (@rest) {
398: my ($key,$value) = split(/=/,$item);
399: $form{$key} = &unescape($value);
400: }
401: if ($form{'linkprot'}) {
402: $linkprot = $form{'linkprot'};
403: } elsif ($form{'linkkey'} ne '') {
404: $linkkey = $form{'linkkey'};
405: }
406: }
407: } elsif ($form{'ltoken'}) {
408: my %link_info = &Apache::lonnet::tmpget($form{'ltoken'});
409: $linkprot = $link_info{'linkprot'};
410: my $delete = &Apache::lonnet::tmpdel($form{'ltoken'});
411: delete($form{'ltoken'});
412: } elsif ($form{'linkkey'} ne '') {
413: $linkkey = $form{'linkkey'};
414: }
415:
416: my $domain = $r->dir_config('lonSSOUserDomain');
417: if ($domain eq '') {
418: $domain = $r->dir_config('lonDefDomain');
419: }
420: my $home=&Apache::lonnet::homeserver($user,$domain);
421: if ($home !~ /(con_lost|no_host|no_such_host)/) {
422: &Apache::lonnet::logthis(" SSO authorized user $user ");
423: my ($is_balancer,$otherserver,$hosthere);
424: if ($form{'iptoken'}) {
425: if (($sessiondata{'domain'} eq $domain) &&
426: ($sessiondata{'username'} eq $user)) {
427: $hosthere = 1;
428: }
429: }
430: unless ($hosthere) {
431: ($is_balancer,$otherserver) =
432: &Apache::lonnet::check_loadbalancing($user,$domain,'login');
433: if ($is_balancer) {
434: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
435: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
436: if (($found_server) && ($balancer_cookie =~ /^\Q$domain\E_\Q$user\E_/)) {
437: $otherserver = $found_server;
438: } elsif ($otherserver eq '') {
439: my $lowest_load;
440: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($domain);
441: if ($lowest_load > 100) {
442: $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$domain);
443: }
444: if ($otherserver ne '') {
445: my @hosts = &Apache::lonnet::current_machine_ids();
446: if (grep(/^\Q$otherserver\E$/,@hosts)) {
447: $hosthere = $otherserver;
448: }
449: }
450: }
451: }
452: }
453: if (($is_balancer) && (!$hosthere)) {
454: # login but immediately go to switch server to find us a new
455: # machine
456: &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
457: foreach my $item (keys(%form)) {
458: $env{'form.'.$item} = $form{$item};
459: }
460: unless (($form{'symb'}) || ($form{'origurl'})) {
461: unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
462: $env{'form.origurl'} = $r->uri;
463: }
464: }
465: if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
466: $env{'request.deeplink.login'} = $form{'origurl'};
467: } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
468: $env{'request.deeplink.login'} = $r->uri;
469: }
470: if ($env{'request.deeplink.login'}) {
471: if ($linkprot) {
472: $env{'request.linkprot'} = $linkprot;
473: } elsif ($linkkey ne '') {
474: $env{'request.linkkey'} = $linkkey;
475: }
476: }
477: $env{'request.sso.login'} = 1;
478: if (defined($r->dir_config("lonSSOReloginServer"))) {
479: $env{'request.sso.reloginserver'} =
480: $r->dir_config('lonSSOReloginServer');
481: }
482: my $redirecturl = '/adm/switchserver';
483: if ($otherserver ne '') {
484: $redirecturl .= '?otherserver='.$otherserver;
485: }
486: $r->internal_redirect($redirecturl);
487: $r->set_handlers('PerlHandler'=> undef);
488: } else {
489: # need to login them in, so generate the need data that
490: # migrate expects to do login
491: my $ip = &Apache::lonnet::get_requestor_ip($r);
492: my %info=('ip' => $ip,
493: 'domain' => $domain,
494: 'username' => $user,
495: 'server' => $r->dir_config('lonHostID'),
496: 'sso.login' => 1
497: );
498: foreach my $item ('role','symb','iptoken','origurl') {
499: if (exists($form{$item})) {
500: $info{$item} = $form{$item};
501: } elsif ($sessiondata{$item} ne '') {
502: $info{$item} = $sessiondata{$item};
503: }
504: }
505: unless (($info{'symb'}) || ($info{'origurl'})) {
506: unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
507: $info{'origurl'} = $r->uri;
508: }
509: }
510: if (($r->uri eq '/adm/sso') && ($form{'origurl'} =~ m{^/+tiny/+$match_domain/+\w+$})) {
511: $info{'deeplink.login'} = $form{'origurl'};
512: } elsif ($r->uri =~ m{^/+tiny/+$match_domain/+\w+$}) {
513: $info{'deeplink.login'} = $r->uri;
514: }
515: if ($info{'deeplink.login'}) {
516: if ($linkprot) {
517: $info{'linkprot'} = $linkprot;
518: } elsif ($linkkey ne '') {
519: $info{'linkkey'} = $linkkey;
520: }
521: }
522: if ($r->dir_config("ssodirecturl") == 1) {
523: $info{'origurl'} = $r->uri;
524: }
525: if (defined($r->dir_config("lonSSOReloginServer"))) {
526: $info{'sso.reloginserver'} =
527: $r->dir_config('lonSSOReloginServer');
528: }
529: if (($is_balancer) && ($hosthere)) {
530: $info{'noloadbalance'} = $hosthere;
531: }
532: my $token =
533: &Apache::lonnet::tmpput(\%info,
534: $r->dir_config('lonHostID'));
535: $env{'form.token'} = $token;
536: $r->internal_redirect('/adm/migrateuser');
537: $r->set_handlers('PerlHandler'=> undef);
538: }
539: return OK;
540: } else {
541: &Apache::lonnet::logthis(" SSO authorized unknown user $user ");
542: my @cancreate;
543: my %domconfig =
544: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
545: if (ref($domconfig{'usercreation'}) eq 'HASH') {
546: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
547: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
548: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
549: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
550: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
551: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
552: }
553: }
554: }
555: if ((grep(/^sso$/,@cancreate)) || (defined($r->dir_config('lonSSOUserUnknownRedirect')))) {
556: $r->subprocess_env->set('SSOUserUnknown' => $user);
557: $r->subprocess_env->set('SSOUserDomain' => $domain);
558: if (grep(/^sso$/,@cancreate)) {
559: #FIXME - need to preserve origurl, role and symb, or linkprot or linkkey for use after account
560: # creation
561: $r->set_handlers('PerlHandler'=> [\&Apache::createaccount::handler]);
562: $r->handler('perl-script');
563: } else {
564: $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
565: $r->set_handlers('PerlHandler'=> undef);
566: }
567: return OK;
568: }
569: }
570: return undef;
571: }
572:
573: sub handler {
574: my $r = shift;
575: my $requrl=$r->uri;
576:
577: if ($requrl =~ m{^/res/adm/pages/[^/]+\.(gif|png)$}) {
578: return OK;
579: }
580:
581: if (&Apache::lonnet::is_domainimage($requrl)) {
582: return OK;
583: }
584:
585: my %user;
586: my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
587:
588: unless (($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) {
589: my $result = &sso_login($r,$handle,$user{'name'});
590: if (defined($result)) {
591: return $result;
592: }
593: }
594:
595: my ($is_balancer,$otherserver);
596:
597: if ($handle eq '') {
598: unless ((($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) ||
599: ($requrl =~ m{^/public/$match_domain/$match_courseid/syllabus}) ||
600: ($requrl =~ m{^/adm/help/}) || ($requrl eq '/adm/sso') ||
601: ($requrl =~ m{^/res/$match_domain/$match_username/})) {
602: $r->log_reason("Cookie not valid", $r->filename);
603: }
604: } elsif ($handle ne '') {
605:
606: # ------------------------------------------------------ Initialize Environment
607: my $lonidsdir=$r->dir_config('lonIDsDir');
608: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
609:
610: # --------------------------------------------------------- Initialize Language
611:
612: &Apache::lonlocal::get_language_handle($r);
613:
614: }
615:
616: # -------------------------------------------------- Should be a valid user now
617: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
618: # -------------------------------------------------------------- Resource State
619:
620: my ($cdom,$cnum);
621: if ($env{'request.course.id'}) {
622: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
623: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
624: }
625: if ($requrl=~/^\/+(res|uploaded)\//) {
626: $env{'request.state'} = "published";
627: } else {
628: $env{'request.state'} = 'unknown';
629: }
630: $env{'request.filename'} = $r->filename;
631: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
632: my ($suppext,$checkabsolute);
633: if ($requrl =~ m{^/adm/wrapper/ext/}) {
634: my $query = $r->args;
635: if ($query) {
636: my $preserved;
637: foreach my $pair (split(/&/,$query)) {
638: my ($name, $value) = split(/=/,$pair);
639: unless (($name eq 'symb') || ($name eq 'usehttp')) {
640: $preserved .= $pair.'&';
641: }
642: if (($env{'request.course.id'}) && ($name eq 'folderpath')) {
643: if ($value =~ /^supplemental/) {
644: $suppext = 1;
645: }
646: }
647: }
648: $preserved =~ s/\&$//;
649: if ($preserved) {
650: $env{'request.external.querystring'} = $preserved;
651: }
652: }
653: if ($env{'request.course.id'}) {
654: $checkabsolute = 1;
655: }
656: } elsif ($env{'request.course.id'} &&
657: (($requrl =~ m{^/adm/$match_domain/$match_username/aboutme$}) ||
658: ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}))) {
659: my $query = $r->args;
660: if ($query) {
661: foreach my $pair (split(/&/,$query)) {
662: my ($name, $value) = split(/=/,$pair);
663: if ($name eq 'folderpath') {
664: if ($value =~ /^supplemental/) {
665: $suppext = 1;
666: }
667: last;
668: }
669: }
670: }
671: if ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}) {
672: $checkabsolute = 1;
673: }
674: }
675: if ($checkabsolute) {
676: my $hostname = $r->hostname();
677: my $lonhost = &Apache::lonnet::host_from_dns($hostname);
678: if ($lonhost) {
679: my $actual = &Apache::lonnet::absolute_url($hostname,1,1);
680: my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname;
681: unless ($actual eq $expected) {
682: $env{'request.use_absolute'} = $expected;
683: }
684: }
685: }
686: # -------------------------------------------------------- Load POST parameters
687:
688: &Apache::lonacc::get_posted_cgi($r);
689:
690: # ------------------------------------------------------ Check if load balancer
691:
692: my $checkexempt;
693: if ($env{'user.loadbalexempt'} eq $r->dir_config('lonHostID')) {
694: if ($env{'user.loadbalcheck.time'} + 600 > time) {
695: $checkexempt = 1;
696: }
697: }
698: if ($env{'user.noloadbalance'} eq $r->dir_config('lonHostID')) {
699: $checkexempt = 1;
700: }
701: unless (($checkexempt) || (($requrl eq '/adm/switchserver') && (!$r->is_initial_req()))) {
702: ($is_balancer,$otherserver) =
703: &Apache::lonnet::check_loadbalancing($env{'user.name'},
704: $env{'user.domain'});
705: if ($is_balancer) {
706: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
707: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
708: if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
709: $otherserver = $found_server;
710: }
711: unless ($requrl eq '/adm/switchserver') {
712: $r->set_handlers('PerlResponseHandler'=>
713: [\&Apache::switchserver::handler]);
714: }
715: if ($otherserver ne '') {
716: $env{'form.otherserver'} = $otherserver;
717: }
718: unless (($env{'form.origurl'}) || ($r->uri eq '/adm/roles') ||
719: ($r->uri eq '/adm/switchserver') || ($r->uri eq '/adm/sso')) {
720: $env{'form.origurl'} = $r->uri;
721: }
722: }
723: }
724: if ($requrl=~m{^/+tiny/+$match_domain/+\w+$}) {
725: if ($env{'user.name'} eq 'public' &&
726: $env{'user.domain'} eq 'public') {
727: $env{'request.firsturl'}=$requrl;
728: return FORBIDDEN;
729: } else {
730: return OK;
731: }
732: }
733: # ---------------------------------------------------------------- Check access
734: my $now = time;
735: my ($check_symb,$check_access,$check_block,$access,$poss_symb);
736: if ($requrl !~ m{^/(?:adm|public|(?:prt|zip)spool)/}
737: || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
738: $check_access = 1;
739: }
740: if ((!$check_access) && ($env{'request.course.id'})) {
741: if (($requrl eq '/adm/viewclasslist') ||
742: ($requrl =~ m{^(/adm/wrapper|)\Q/uploaded/$cdom/$cnum/docs/\E}) ||
743: ($requrl =~ m{^/adm/.*/aboutme$}) ||
744: ($requrl=~m{^/adm/coursedocs/showdoc/}) ||
745: ($requrl=~m{^(/adm/wrapper|)/adm/$cdom/$cnum/\d+/ext\.tool$})) {
746: $check_block = 1;
747: }
748: }
749: if (($env{'request.course.id'}) && (!$suppext)) {
750: $requrl=~/\.(\w+)$/;
751: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
752: ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) ||
753: ($requrl=~/^\/adm\/wrapper\//) ||
754: ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
755: ($requrl=~m|\.problem/smpedit$|) ||
756: ($requrl=~/^\/public\/.*\/syllabus$/) ||
757: ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) ||
758: ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/) ||
759: ($requrl=~m{^/adm/$cdom/$cnum/\d+/ext\.tool$})) {
760: $check_symb = 1;
761: }
762: }
763: if (($check_access) || ($check_block)) {
764: if ($check_symb) {
765: if ($env{'form.symb'}) {
766: $poss_symb=&Apache::lonnet::symbclean($env{'form.symb'});
767: } elsif (($env{'request.course.id'}) && ($r->args ne '')) {
768: my $query = $r->args;
769: foreach my $pair (split(/&/,$query)) {
770: my ($name, $value) = split(/=/,$pair);
771: $name = &unescape($name);
772: $value =~ tr/+/ /;
773: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
774: if ($name eq 'symb') {
775: $poss_symb = &Apache::lonnet::symbclean($value);
776: last;
777: }
778: }
779: }
780: if ($poss_symb) {
781: my ($possmap,$resid,$url)=&Apache::lonnet::decode_symb($poss_symb);
782: $url = &Apache::lonnet::clutter($url);
783: my $toplevelmap = $env{'course.'.$env{'request.course.id'}.'.url'};
784: unless (($url eq $requrl) && (($possmap eq $toplevelmap) ||
785: (&Apache::lonnet::is_on_map($possmap)))) {
786: undef($poss_symb);
787: }
788: if ($poss_symb) {
789: if ((!$env{'request.role.adv'}) && ($env{'acc.randomout'}) &&
790: ($env{'acc.randomout'}=~/\&\Q$poss_symb\E\&/)) {
791: undef($poss_symb);
792: } elsif ((!$env{'request.role.adv'}) && ($env{'acc.deeplinkout'}) &&
793: ($env{'acc.deeplinkout'}=~/\&\Q$poss_symb\E\&/)) {
794: undef($poss_symb);
795: }
796: }
797: }
798: if ($poss_symb) {
799: $access=&Apache::lonnet::allowed('bre',$requrl,$poss_symb);
800: } else {
801: $access=&Apache::lonnet::allowed('bre',$requrl,'','','','',1);
802: }
803: } else {
804: my $nodeeplinkcheck;
805: if (($check_access) && ($requrl =~ /\.(sequence|page)$/)) {
806: unless ($env{'form.navmap'}) {
807: if ($r->args ne '') {
808: &Apache::loncommon::get_unprocessed_cgi($r->args,['navmap']);
809: unless ($env{'form.navmap'}) {
810: $nodeeplinkcheck = 1;
811: }
812: }
813: }
814: }
815: my $clientip = &Apache::lonnet::get_requestor_ip($r);
816: $access=&Apache::lonnet::allowed('bre',$requrl,'','',$clientip,'','',$nodeeplinkcheck);
817: }
818: }
819: if ($check_block) {
820: if ($access eq 'B') {
821: if ($poss_symb) {
822: if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
823: $env{'request.symb'} = $poss_symb;
824: }
825: }
826: &Apache::blockedaccess::setup_handler($r);
827: return OK;
828: }
829: } elsif ($check_access) {
830: if ($handle eq '') {
831: unless ($access eq 'F') {
832: if ($requrl =~ m{^/res/$match_domain/$match_username/}) {
833: $r->log_reason("Cookie not valid", $r->filename);
834: }
835: }
836: }
837: if ($access eq '1') {
838: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
839: return HTTP_NOT_ACCEPTABLE;
840: }
841: if ($access eq 'A') {
842: &Apache::restrictedaccess::setup_handler($r);
843: return OK;
844: }
845: if ($access eq 'B') {
846: if ($poss_symb) {
847: if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
848: $env{'request.symb'} = $poss_symb;
849: }
850: }
851: &Apache::blockedaccess::setup_handler($r);
852: return OK;
853: }
854: if ($access eq 'D') {
855: &Apache::lonprotected::setup_handler($r);
856: return OK;
857: }
858: if (($access ne '2') && ($access ne 'F')) {
859: if ($requrl =~ m{^/res/}) {
860: $access = &Apache::lonnet::allowed('bro',$requrl);
861: if ($access ne 'F') {
862: if ($requrl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
863: $access = &Apache::lonnet::allowed('bre','/res/lib/templates/simpleproblem.problem');
864: if ($access ne 'F') {
865: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
866: return HTTP_NOT_ACCEPTABLE;
867: }
868: } else {
869: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
870: return HTTP_NOT_ACCEPTABLE;
871: }
872: }
873: } elsif (($handle =~ /^publicuser_\d+$/) && (&Apache::lonnet::is_portfolio_url($requrl))) {
874: my $clientip = &Apache::lonnet::get_requestor_ip($r);
875: if (&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip) ne 'F') {
876: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
877: return HTTP_NOT_ACCEPTABLE;
878: }
879: } else {
880: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
881: return HTTP_NOT_ACCEPTABLE;
882: }
883: }
884: }
885: if ($requrl =~ m|^/prtspool/|) {
886: my $start='/prtspool/'.$env{'user.name'}.'_'.
887: $env{'user.domain'};
888: if ($requrl !~ /^\Q$start\E/) {
889: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
890: return HTTP_NOT_ACCEPTABLE;
891: }
892: }
893: if ($requrl =~ m|^/zipspool/|) {
894: my $start='/zipspool/zipout/'.$env{'user.name'}.":".
895: $env{'user.domain'};
896: if ($requrl !~ /^\Q$start\E/) {
897: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
898: return HTTP_NOT_ACCEPTABLE;
899: }
900: }
901: if ($env{'user.name'} eq 'public' &&
902: $env{'user.domain'} eq 'public' &&
903: $requrl !~ m{^/+(res|public|uploaded)/} &&
904: $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
905: $requrl !~ m{^/adm/blockingstatus/.*$} &&
906: $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
907: $env{'request.querystring'}=$r->args;
908: $env{'request.firsturl'}=$requrl;
909: return FORBIDDEN;
910: }
911: # ------------------------------------------------------------- This is allowed
912: if ($env{'request.course.id'}) {
913: &Apache::lonnet::countacc($requrl);
914: my $query=$r->args;
915: if ($check_symb) {
916: # ------------------------------------- This is serious stuff, get symb and log
917: my $symb;
918: if ($query) {
919: &Apache::loncommon::get_unprocessed_cgi($query,['symb','folderpath']);
920: }
921: if ($env{'form.symb'}) {
922: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
923: if (($requrl eq '/adm/navmaps') ||
924: ($requrl =~ m{^/adm/wrapper/}) ||
925: ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
926: unless (&Apache::lonnet::symbverify($symb,$requrl)) {
927: if (&Apache::lonnet::is_on_map($requrl)) {
928: $symb = &Apache::lonnet::symbread($requrl);
929: unless (&Apache::lonnet::symbverify($symb,$requrl)) {
930: undef($symb);
931: }
932: }
933: }
934: if ($symb) {
935: if ($requrl eq '/adm/navmaps') {
936: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
937: &Apache::lonnet::symblist($map,$murl => [$murl,$mid]);
938: } elsif (($requrl =~ m{^/adm/wrapper/}) ||
939: ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
940: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
941: if ($map =~ /\.page$/) {
942: my $mapsymb = &Apache::lonnet::symbread($map);
943: ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
944: }
945: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
946: 'last_known' =>[$murl,$mid]);
947: }
948: }
949: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
950: (($requrl=~m|(.*)/smpedit$|) &&
951: &Apache::lonnet::symbverify($symb,$1)) ||
952: (($requrl=~m|(.*/aboutme)/portfolio$|) &&
953: &Apache::lonnet::symbverify($symb,$1))) {
954: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
955: if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
956: my $mapsymb = &Apache::lonnet::symbread($map);
957: ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
958: }
959: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
960: 'last_known' =>[$murl,$mid]);
961: } else {
962: $r->log_reason('Invalid symb for '.$requrl.': '.
963: $symb);
964: $env{'user.error.msg'}=
965: "$requrl:bre:1:1:Invalid Access";
966: return HTTP_NOT_ACCEPTABLE;
967: }
968: } else {
969: if ($requrl=~m{^(/adm/.*/aboutme)/portfolio$}) {
970: $requrl = $1;
971: }
972: $symb=&Apache::lonnet::symbread($requrl);
973: if (&Apache::lonnet::is_on_map($requrl) && $symb) {
974: my ($encstate,$invalidsymb);
975: unless (&Apache::lonnet::symbverify($symb,$requrl,\$encstate)) {
976: $invalidsymb = 1;
977: #
978: # If $env{'request.enc'} inconsistent with encryption expected for $symb
979: # retrieved by lonnet::symbread(), call again to check for an instance of
980: # $requrl in the course for which expected encryption matches request.enc.
981: # If symb for different instance passes lonnet::symbverify(), use that as
982: # the symb for $requrl and call &Apache::lonnet::allowed() for that symb.
983: # Report invalid symb if there is no other symb. Redirect to /adm/ambiguous
984: # if multiple possible symbs consistent with request.enc available for $requrl.
985: #
986: if (($env{'request.enc'} && !$encstate) || (!$env{'request.enc'} && $encstate)) {
987: my %possibles;
988: my $nocache = 1;
989: my $oldsymb = $symb;
990: $symb = &Apache::lonnet::symbread($requrl,'','','',\%possibles,$nocache);
991: if (($symb) && ($symb ne $oldsymb)) {
992: if (&Apache::lonnet::symbverify($symb,$requrl)) {
993: my $access=&Apache::lonnet::allowed('bre',$requrl,$symb);
994: if ($access eq 'B') {
995: $env{'request.symb'} = $symb;
996: &Apache::blockedaccess::setup_handler($r);
997: return OK;
998: } elsif (($access eq '2') || ($access eq 'F')) {
999: $invalidsymb = '';
1000: }
1001: }
1002: } elsif (keys(%possibles) > 1) {
1003: $r->internal_redirect('/adm/ambiguous');
1004: return OK;
1005: }
1006: }
1007: if ($invalidsymb) {
1008: if ($requrl eq '/adm/navmaps') {
1009: undef($symb);
1010: } else {
1011: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
1012: $env{'user.error.msg'}=
1013: "$requrl:bre:1:1:Invalid Access";
1014: return HTTP_NOT_ACCEPTABLE;
1015: }
1016: }
1017: }
1018: }
1019: if ($symb) {
1020: my ($map,$mid,$murl)=
1021: &Apache::lonnet::decode_symb($symb);
1022: if ($requrl eq '/adm/navmaps') {
1023: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid]);
1024: } else {
1025: if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
1026: my $mapsymb = &Apache::lonnet::symbread($map);
1027: ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
1028: }
1029: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
1030: 'last_known' =>[$murl,$mid]);
1031: }
1032: }
1033: }
1034: $env{'request.symb'}=$symb;
1035: if (($env{'request.symbread.cached.'}) && ($env{'request.symbread.cached.'} ne $symb)) {
1036: $env{'request.symbread.cached.'} = $symb;
1037: }
1038: &Apache::lonnet::courseacclog($symb);
1039: } else {
1040: # ------------------------------------------------------- This is other content
1041: &Apache::lonnet::courseacclog($requrl);
1042: }
1043: if ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/.+\.html?$}) {
1044: if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1045: if ($query) {
1046: &Apache::loncommon::get_unprocessed_cgi($query,['forceedit']);
1047: if ($env{'form.forceedit'}) {
1048: $env{'request.state'} = 'edit';
1049: }
1050: }
1051: }
1052: } elsif ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/portfolio/syllabus/.+\.html?$}) {
1053: if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1054: if ($query) {
1055: &Apache::loncommon::get_unprocessed_cgi($query,['forceedit','editmode']);
1056: if (($env{'form.forceedit'}) || ($env{'form.editmode'})) {
1057: $env{'request.state'} = 'edit';
1058: }
1059: }
1060: }
1061: }
1062: }
1063: return OK;
1064: } else {
1065: my $defdom=$r->dir_config('lonDefDomain');
1066: ($is_balancer,$otherserver) =
1067: &Apache::lonnet::check_loadbalancing(undef,$defdom);
1068: if ($is_balancer) {
1069: $r->set_handlers('PerlResponseHandler'=>
1070: [\&Apache::switchserver::handler]);
1071: if ($otherserver ne '') {
1072: $env{'form.otherserver'} = $otherserver;
1073: }
1074: }
1075: }
1076: # -------------------------------------------- See if this is a public resource
1077: if ($requrl=~m|^/+adm/+help/+|) {
1078: return OK;
1079: }
1080: # ------------------------------------ See if this is a viewable portfolio file
1081: if (&Apache::lonnet::is_portfolio_url($requrl)) {
1082: my $clientip = &Apache::lonnet::get_requestor_ip($r);
1083: my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip);
1084: if ($access eq 'A') {
1085: &Apache::restrictedaccess::setup_handler($r);
1086: return OK;
1087: }
1088: if (($access ne '2') && ($access ne 'F')) {
1089: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
1090: return HTTP_NOT_ACCEPTABLE;
1091: }
1092: }
1093:
1094: # -------------------------------------------------------------- Not authorized
1095: $requrl=~/\.(\w+)$/;
1096: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
1097: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
1098: # ($requrl=~m|^/prtspool/|)) {
1099: # -------------------------- Store where they wanted to go and get login screen
1100: $env{'request.querystring'}=$r->args;
1101: $env{'request.firsturl'}=$requrl;
1102: return FORBIDDEN;
1103: # } else {
1104: # --------------------------------------------------------------------- Goodbye
1105: # return HTTP_BAD_REQUEST;
1106: # }
1107: }
1108:
1109: 1;
1110: __END__
1111:
1112: =pod
1113:
1114: =back
1115:
1116: =cut
1117:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>