Annotation of loncom/auth/lonacc.pm, revision 1.159.2.21
1.1 albertel 1: # The LearningOnline Network
2: # Cookie Based Access Handler
1.22 www 3: #
1.159.2.21! raeburn 4: # $Id: lonacc.pm,v 1.159.2.20 2021/02/10 11:36:15 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:
1.121 jms 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:
1.119 jms 94:
1.1 albertel 95: package Apache::lonacc;
96:
97: use strict;
1.8 www 98: use Apache::Constants qw(:common :http :methods);
1.2 www 99: use Apache::File;
1.6 www 100: use Apache::lonnet;
1.25 harris41 101: use Apache::loncommon();
1.47 www 102: use Apache::lonlocal;
1.86 albertel 103: use Apache::restrictedaccess();
1.148 raeburn 104: use Apache::blockedaccess();
1.16 www 105: use Fcntl qw(:flock);
1.141 raeburn 106: use LONCAPA qw(:DEFAULT :match);
1.1 albertel 107:
1.75 albertel 108: sub cleanup {
109: my ($r)=@_;
110: if (! $r->is_initial_req()) { return DECLINED; }
111: &Apache::lonnet::save_cache();
112: return OK;
113: }
114:
115: sub goodbye {
116: my ($r)=@_;
117: &Apache::lonnet::goodbye();
118: return DONE;
119: }
120:
1.76 albertel 121: ###############################################
122:
123: sub get_posted_cgi {
1.114 raeburn 124: my ($r,$fields) = @_;
1.76 albertel 125:
126: my $buffer;
127: if ($r->header_in('Content-length')) {
128: $r->read($buffer,$r->header_in('Content-length'),0);
129: }
1.113 albertel 130: my $content_type = $r->header_in('Content-type');
131: if ($content_type !~ m{^multipart/form-data}) {
1.76 albertel 132: my @pairs=split(/&/,$buffer);
133: my $pair;
134: foreach $pair (@pairs) {
135: my ($name,$value) = split(/=/,$pair);
136: $value =~ tr/+/ /;
137: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
138: $name =~ tr/+/ /;
139: $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.114 raeburn 140: if (ref($fields) eq 'ARRAY') {
141: next if (!grep(/^\Q$name\E$/,@{$fields}));
142: }
1.77 albertel 143: &Apache::loncommon::add_to_env("form.$name",$value);
1.76 albertel 144: }
145: } else {
1.113 albertel 146: my ($contentsep) = ($content_type =~ /boundary=\"?([^\";,]+)\"?/);
1.76 albertel 147: my @lines = split (/\n/,$buffer);
148: my $name='';
149: my $value='';
150: my $fname='';
151: my $fmime='';
152: my $i;
153: for ($i=0;$i<=$#lines;$i++) {
1.113 albertel 154: if ($lines[$i]=~/^--\Q$contentsep\E/) {
1.76 albertel 155: if ($name) {
1.139 raeburn 156: chomp($value);
157: if (($r->uri eq '/adm/portfolio') &&
158: ($name eq 'uploaddoc')) {
159: if (length($value) == 1) {
160: $value=~s/[\r\n]$//;
161: }
1.159.2.21! raeburn 162: }
! 163: if ($fname =~ /\.(xls|doc|ppt)(x|m)$/i) {
1.154 raeburn 164: $value=~s/[\r\n]$//;
1.139 raeburn 165: }
1.114 raeburn 166: if (ref($fields) eq 'ARRAY') {
167: next if (!grep(/^\Q$name\E$/,@{$fields}));
168: }
1.118 raeburn 169: if ($fname) {
170: if ($env{'form.symb'} ne '') {
171: my $size = (length($value))/(1024.0 * 1024.0);
172: if (&upload_size_allowed($name,$size,$fname) eq 'ok') {
173: $env{"form.$name.filename"}=$fname;
174: $env{"form.$name.mimetype"}=$fmime;
175: &Apache::loncommon::add_to_env("form.$name",$value);
176: }
177: } else {
178: $env{"form.$name.filename"}=$fname;
179: $env{"form.$name.mimetype"}=$fmime;
180: &Apache::loncommon::add_to_env("form.$name",$value);
181: }
182: } else {
183: $value=~s/\s+$//s;
184: &Apache::loncommon::add_to_env("form.$name",$value);
185: }
1.76 albertel 186: }
187: if ($i<$#lines) {
188: $i++;
189: $lines[$i]=~
190: /Content\-Disposition\:\s*form\-data\;\s*name\=\"([^\"]+)\"/i;
191: $name=$1;
192: $value='';
193: if ($lines[$i]=~/filename\=\"([^\"]+)\"/i) {
194: $fname=$1;
195: if
196: ($lines[$i+1]=~/Content\-Type\:\s*([\w\-\/]+)/i) {
197: $fmime=$1;
198: $i++;
199: } else {
200: $fmime='';
201: }
202: } else {
203: $fname='';
204: $fmime='';
205: }
206: $i++;
207: }
208: } else {
209: $value.=$lines[$i]."\n";
210: }
211: }
212: }
213: #
214: # Digested POSTed values
215: #
216: # Remember the way this was originally done (GET or POST)
217: #
218: $env{'request.method'}=$ENV{'REQUEST_METHOD'};
219: #
220: # There may also be stuff in the query string
221: # Tell subsequent handlers that this was GET, not POST, so they can access query string.
222: # Also, unset POSTed content length to cover all tracks.
223: #
224:
225: $r->method_number(M_GET);
226:
227: $r->method('GET');
228: $r->headers_in->unset('Content-length');
229: }
230:
1.121 jms 231: =pod
232:
1.146 raeburn 233: =over
234:
1.121 jms 235: =item upload_size_allowed()
236:
237: Perform size checks for file uploads to essayresponse items in course context.
238:
239: Add form.HWFILESIZE.$part_$id to %env with file size (MB)
240: If file exceeds maximum allowed size, add form.HWFILETOOBIG.$part_$id to %env.
241:
242: =cut
1.118 raeburn 243:
244: sub upload_size_allowed {
245: my ($name,$size,$fname) = @_;
246: if ($name =~ /^HWFILE(\w+)$/) {
247: my $ident = $1;
248: my $item = 'HWFILESIZE'.$ident;
1.122 raeburn 249: my $savesize = sprintf("%.6f",$size);
250: &Apache::loncommon::add_to_env("form.$item",$savesize);
1.118 raeburn 251: my $maxsize= &Apache::lonnet::EXT("resource.$ident.maxfilesize");
252: if (!$maxsize) {
1.123 raeburn 253: $maxsize = 10.0; # FIXME This should become a domain configuration.
1.118 raeburn 254: }
255: if ($size > $maxsize) {
256: my $warn = 'HWFILETOOBIG'.$ident;
257: &Apache::loncommon::add_to_env("form.$warn",$fname);
258: return;
259: }
260: }
261: return 'ok';
262: }
263:
1.121 jms 264: =pod
265:
266: =item sso_login()
267:
268: handle the case of the single sign on user, at this point $r->user
1.150 raeburn 269: will be set and valid; now need to find the loncapa user info, and possibly
1.149 raeburn 270: balance them. If $r->user() is set this means either it was either set by
1.150 raeburn 271: SSO or by checkauthen.pm, if a valid cookie was found. The latter case can
272: be identified by the third arg ($usename), except when lonacc is called in
273: an internal redirect to /adm/switchserver (e.g., load-balancing following
274: successful authentication) -- no cookie set yet. For that particular case
275: simply skip the call to sso_login().
1.149 raeburn 276:
277: returns OK if it was SSO and user was handled.
278: returns undef if not SSO or no means to handle the user.
1.121 jms 279:
280: =cut
1.117 jms 281:
1.94 albertel 282: sub sso_login {
1.149 raeburn 283: my ($r,$handle,$username) = @_;
1.94 albertel 284:
1.156 raeburn 285: if (($r->user eq '') || ($username ne '') || ($r->user eq 'public:public') ||
1.143 raeburn 286: (defined($env{'user.name'}) && (defined($env{'user.domain'}))
287: && ($handle ne ''))) {
1.94 albertel 288: # not an SSO case or already logged in
289: return undef;
290: }
291:
1.159.2.1 raeburn 292: my ($user) = ($r->user =~ m/^($match_username)$/);
293: if ($user eq '') {
294: return undef;
295: }
1.97 albertel 296:
1.126 raeburn 297: my $query = $r->args;
298: my %form;
299: if ($query) {
1.159.2.21! raeburn 300: my @items = ('role','symb','iptoken','origurl','logtoken');
1.127 raeburn 301: &Apache::loncommon::get_unprocessed_cgi($query,\@items);
302: foreach my $item (@items) {
303: if (defined($env{'form.'.$item})) {
304: $form{$item} = $env{'form.'.$item};
1.126 raeburn 305: }
306: }
307: }
308:
1.145 raeburn 309: my %sessiondata;
310: if ($form{'iptoken'}) {
311: %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
1.158 raeburn 312: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
313: unless ($sessiondata{'sessionserver'}) {
314: delete($form{'iptoken'});
315: }
1.145 raeburn 316: }
317:
1.159.2.21! raeburn 318: #
! 319: # If Shibboleth auth is in use, and a dual SSO and non-SSO login page
! 320: # is in use, then the query string will contain the logtoken item with
! 321: # a value set to the name of a .tmp file in /home/httpd/perl/tmp
! 322: # containing the url to display after authentication, and also,
! 323: # optionally, role and symb.
! 324: #
! 325: # Otherwise the query string may contain role and symb.
! 326: #
! 327:
! 328: if ($form{'logtoken'}) {
! 329: my ($firsturl,@rest);
! 330: my $lonhost = $r->dir_config('lonHostID');
! 331: my $tmpinfo = &Apache::lonnet::reply('tmpget:'.$form{'logtoken'},$lonhost);
! 332: my $delete = &Apache::lonnet::tmpdel($form{'logtoken'});
! 333: unless (($tmpinfo=~/^error/) || ($tmpinfo eq 'con_lost') ||
! 334: ($tmpinfo eq 'no_such_host')) {
! 335: (undef,$firsturl,@rest) = split(/&/,$tmpinfo);
! 336: if ($firsturl ne '') {
! 337: $firsturl = &unescape($firsturl);
! 338: }
! 339: foreach my $item (@rest) {
! 340: my ($key,$value) = split(/=/,$item);
! 341: $form{$key} = &unescape($value);
! 342: }
! 343: if ($form{'iptoken'}) {
! 344: %sessiondata = &Apache::lonnet::tmpget($form{'iptoken'});
! 345: my $delete = &Apache::lonnet::tmpdel($form{'iptoken'});
! 346: }
! 347: }
! 348: }
! 349:
1.136 raeburn 350: my $domain = $r->dir_config('lonSSOUserDomain');
351: if ($domain eq '') {
352: $domain = $r->dir_config('lonDefDomain');
353: }
1.97 albertel 354: my $home=&Apache::lonnet::homeserver($user,$domain);
1.94 albertel 355: if ($home !~ /(con_lost|no_host|no_such_host)/) {
1.107 albertel 356: &Apache::lonnet::logthis(" SSO authorized user $user ");
1.145 raeburn 357: my ($is_balancer,$otherserver,$hosthere);
358: if ($form{'iptoken'}) {
1.158 raeburn 359: if (($sessiondata{'domain'} eq $domain) &&
360: ($sessiondata{'username'} eq $user)) {
1.145 raeburn 361: $hosthere = 1;
362: }
363: }
364: unless ($hosthere) {
365: ($is_balancer,$otherserver) =
1.159.2.5 raeburn 366: &Apache::lonnet::check_loadbalancing($user,$domain,'login');
367: if ($is_balancer) {
1.159.2.8 raeburn 368: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
369: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
370: if (($found_server) && ($balancer_cookie =~ /^\Q$domain\E_\Q$user\E_/)) {
371: $otherserver = $found_server;
372: } elsif ($otherserver eq '') {
1.159.2.5 raeburn 373: my $lowest_load;
374: ($otherserver,undef,undef,undef,$lowest_load) = &Apache::lonnet::choose_server($domain);
375: if ($lowest_load > 100) {
1.159.2.21! raeburn 376: $otherserver = &Apache::lonnet::spareserver($r,$lowest_load,$lowest_load,1,$domain);
1.159.2.5 raeburn 377: }
1.159.2.8 raeburn 378: if ($otherserver ne '') {
379: my @hosts = &Apache::lonnet::current_machine_ids();
380: if (grep(/^\Q$otherserver\E$/,@hosts)) {
381: $hosthere = $otherserver;
382: }
1.159.2.5 raeburn 383: }
384: }
385: }
1.145 raeburn 386: }
1.159.2.5 raeburn 387: if (($is_balancer) && (!$hosthere)) {
1.136 raeburn 388: # login but immediately go to switch server to find us a new
1.94 albertel 389: # machine
1.127 raeburn 390: &Apache::lonauth::success($r,$user,$domain,$home,'noredirect');
1.159.2.21! raeburn 391: foreach my $item (keys(%form)) {
! 392: $env{'form.'.$item} = $form{$item};
! 393: }
! 394: unless (($form{'symb'}) || ($form{'origurl'})) {
! 395: unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
! 396: $env{'form.origurl'} = $r->uri;
! 397: }
! 398: }
1.105 raeburn 399: $env{'request.sso.login'} = 1;
1.106 raeburn 400: if (defined($r->dir_config("lonSSOReloginServer"))) {
401: $env{'request.sso.reloginserver'} =
402: $r->dir_config('lonSSOReloginServer');
403: }
1.137 raeburn 404: my $redirecturl = '/adm/switchserver';
405: if ($otherserver ne '') {
406: $redirecturl .= '?otherserver='.$otherserver;
407: }
408: $r->internal_redirect($redirecturl);
1.101 albertel 409: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 410: } else {
411: # need to login them in, so generate the need data that
412: # migrate expects to do login
1.159.2.18 raeburn 413: my $ip = &Apache::lonnet::get_requestor_ip($r);
1.147 raeburn 414: my %info=('ip' => $ip,
1.94 albertel 415: 'domain' => $domain,
1.97 albertel 416: 'username' => $user,
1.94 albertel 417: 'server' => $r->dir_config('lonHostID'),
418: 'sso.login' => 1
419: );
1.159.2.21! raeburn 420: foreach my $item ('role','symb','iptoken','origurl') {
1.126 raeburn 421: if (exists($form{$item})) {
422: $info{$item} = $form{$item};
1.159.2.21! raeburn 423: } elsif ($sessiondata{$item} ne '') {
! 424: $info{$item} = $sessiondata{$item};
1.126 raeburn 425: }
426: }
1.159.2.21! raeburn 427: unless (($info{'symb'}) || ($info{'origurl'})) {
1.152 raeburn 428: unless (($r->uri eq '/adm/roles') || ($r->uri eq '/adm/sso')) {
1.151 raeburn 429: $info{'origurl'} = $r->uri;
430: }
431: }
1.116 raeburn 432: if ($r->dir_config("ssodirecturl") == 1) {
433: $info{'origurl'} = $r->uri;
434: }
1.106 raeburn 435: if (defined($r->dir_config("lonSSOReloginServer"))) {
436: $info{'sso.reloginserver'} =
437: $r->dir_config('lonSSOReloginServer');
438: }
1.159.2.5 raeburn 439: if (($is_balancer) && ($hosthere)) {
440: $info{'noloadbalance'} = $hosthere;
441: }
1.94 albertel 442: my $token =
443: &Apache::lonnet::tmpput(\%info,
444: $r->dir_config('lonHostID'));
445: $env{'form.token'} = $token;
446: $r->internal_redirect('/adm/migrateuser');
1.101 albertel 447: $r->set_handlers('PerlHandler'=> undef);
1.94 albertel 448: }
449: return OK;
1.155 raeburn 450: } else {
1.107 albertel 451: &Apache::lonnet::logthis(" SSO authorized unknown user $user ");
1.115 raeburn 452: my @cancreate;
453: my %domconfig =
454: &Apache::lonnet::get_dom('configuration',['usercreation'],$domain);
455: if (ref($domconfig{'usercreation'}) eq 'HASH') {
456: if (ref($domconfig{'usercreation'}{'cancreate'}) eq 'HASH') {
457: if (ref($domconfig{'usercreation'}{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
458: @cancreate = @{$domconfig{'usercreation'}{'cancreate'}{'selfcreate'}};
459: } elsif (($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne 'none') &&
460: ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'} ne '')) {
461: @cancreate = ($domconfig{'usercreation'}{'cancreate'}{'selfcreate'});
462: }
463: }
464: }
1.155 raeburn 465: if ((grep(/^sso$/,@cancreate)) || (defined($r->dir_config('lonSSOUserUnknownRedirect')))) {
466: $r->subprocess_env->set('SSOUserUnknown' => $user);
467: $r->subprocess_env->set('SSOUserDomain' => $domain);
468: if (grep(/^sso$/,@cancreate)) {
1.159.2.21! raeburn 469: #FIXME - need to preserve origurl, role and symb for use after account
! 470: # creation
1.155 raeburn 471: $r->set_handlers('PerlHandler'=> [\&Apache::createaccount::handler]);
472: $r->handler('perl-script');
473: } else {
474: $r->internal_redirect($r->dir_config('lonSSOUserUnknownRedirect'));
475: $r->set_handlers('PerlHandler'=> undef);
476: }
477: return OK;
1.115 raeburn 478: }
1.94 albertel 479: }
480: return undef;
481: }
482:
1.1 albertel 483: sub handler {
484: my $r = shift;
485: my $requrl=$r->uri;
1.149 raeburn 486:
487: if ($requrl =~ m{^/res/adm/pages/[^/]+\.(gif|png)$}) {
1.108 raeburn 488: return OK;
489: }
1.70 albertel 490:
1.149 raeburn 491: if (&Apache::lonnet::is_domainimage($requrl)) {
1.124 raeburn 492: return OK;
493: }
494:
1.149 raeburn 495: my %user;
496: my $handle = &Apache::lonnet::check_for_valid_session($r,undef,\%user);
1.93 albertel 497:
1.150 raeburn 498: unless (($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) {
499: my $result = &sso_login($r,$handle,$user{'name'});
500: if (defined($result)) {
501: return $result;
502: }
1.70 albertel 503: }
504:
1.137 raeburn 505: my ($is_balancer,$otherserver);
1.142 raeburn 506:
1.92 albertel 507: if ($handle eq '') {
1.159 raeburn 508: unless ((($requrl eq '/adm/switchserver') && (!$r->is_initial_req())) ||
509: ($requrl =~ m{^/public/$match_domain/$match_courseid/syllabus}) ||
1.159.2.21! raeburn 510: ($requrl =~ m{^/adm/help/}) || ($requrl eq '/adm/sso') ||
1.159 raeburn 511: ($requrl =~ m{^/res/$match_domain/$match_username/})) {
1.149 raeburn 512: $r->log_reason("Cookie not valid", $r->filename);
1.142 raeburn 513: }
1.111 albertel 514: } elsif ($handle ne '') {
1.6 www 515:
1.46 www 516: # ------------------------------------------------------ Initialize Environment
1.111 albertel 517: my $lonidsdir=$r->dir_config('lonIDsDir');
1.92 albertel 518: &Apache::lonnet::transfer_profile_to_env($lonidsdir,$handle);
1.47 www 519:
520: # --------------------------------------------------------- Initialize Language
521:
1.92 albertel 522: &Apache::lonlocal::get_language_handle($r);
523:
524: }
1.46 www 525:
1.92 albertel 526: # -------------------------------------------------- Should be a valid user now
527: if ($env{'user.name'} ne '' && $env{'user.domain'} ne '') {
1.46 www 528: # -------------------------------------------------------------- Resource State
1.6 www 529:
1.141 raeburn 530: my ($cdom,$cnum);
531: if ($env{'request.course.id'}) {
532: $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
533: $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
534: }
1.92 albertel 535: if ($requrl=~/^\/+(res|uploaded)\//) {
536: $env{'request.state'} = "published";
537: } else {
538: $env{'request.state'} = 'unknown';
539: }
540: $env{'request.filename'} = $r->filename;
541: $env{'request.noversionuri'} = &Apache::lonnet::deversion($requrl);
1.157 raeburn 542: my ($suppext,$checkabsolute);
1.129 raeburn 543: if ($requrl =~ m{^/adm/wrapper/ext/}) {
544: my $query = $r->args;
545: if ($query) {
546: my $preserved;
547: foreach my $pair (split(/&/,$query)) {
548: my ($name, $value) = split(/=/,$pair);
1.159.2.7 raeburn 549: unless (($name eq 'symb') || ($name eq 'usehttp')) {
1.129 raeburn 550: $preserved .= $pair.'&';
551: }
1.141 raeburn 552: if (($env{'request.course.id'}) && ($name eq 'folderpath')) {
553: if ($value =~ /^supplemental/) {
554: $suppext = 1;
555: }
556: }
1.129 raeburn 557: }
558: $preserved =~ s/\&$//;
559: if ($preserved) {
560: $env{'request.external.querystring'} = $preserved;
561: }
562: }
1.157 raeburn 563: if ($env{'request.course.id'}) {
564: $checkabsolute = 1;
565: }
1.141 raeburn 566: } elsif ($env{'request.course.id'} &&
567: (($requrl =~ m{^/adm/$match_domain/$match_username/aboutme$}) ||
568: ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}))) {
569: my $query = $r->args;
570: if ($query) {
571: foreach my $pair (split(/&/,$query)) {
572: my ($name, $value) = split(/=/,$pair);
573: if ($name eq 'folderpath') {
574: if ($value =~ /^supplemental/) {
575: $suppext = 1;
576: }
1.159.2.12 raeburn 577: last;
1.141 raeburn 578: }
579: }
580: }
1.157 raeburn 581: if ($requrl =~ m{^/public/$cdom/$cnum/syllabus$}) {
582: $checkabsolute = 1;
583: }
584: }
585: if ($checkabsolute) {
586: my $hostname = $r->hostname();
587: my $lonhost = &Apache::lonnet::host_from_dns($hostname);
588: if ($lonhost) {
1.159.2.21! raeburn 589: my $actual = &Apache::lonnet::absolute_url($hostname,1,1);
1.157 raeburn 590: my $expected = $Apache::lonnet::protocol{$lonhost}.'://'.$hostname;
591: unless ($actual eq $expected) {
592: $env{'request.use_absolute'} = $expected;
593: }
594: }
1.129 raeburn 595: }
1.6 www 596: # -------------------------------------------------------- Load POST parameters
597:
1.92 albertel 598: &Apache::lonacc::get_posted_cgi($r);
1.6 www 599:
1.137 raeburn 600: # ------------------------------------------------------ Check if load balancer
601:
1.138 raeburn 602: my $checkexempt;
603: if ($env{'user.loadbalexempt'} eq $r->dir_config('lonHostID')) {
604: if ($env{'user.loadbalcheck.time'} + 600 > time) {
1.159.2.8 raeburn 605: $checkexempt = 1;
1.138 raeburn 606: }
607: }
1.145 raeburn 608: if ($env{'user.noloadbalance'} eq $r->dir_config('lonHostID')) {
609: $checkexempt = 1;
610: }
1.159.2.15 raeburn 611: unless (($checkexempt) || (($requrl eq '/adm/switchserver') && (!$r->is_initial_req()))) {
1.138 raeburn 612: ($is_balancer,$otherserver) =
613: &Apache::lonnet::check_loadbalancing($env{'user.name'},
614: $env{'user.domain'});
1.159.2.8 raeburn 615: if ($is_balancer) {
1.159.2.15 raeburn 616: # Check if browser sent a LON-CAPA load balancer cookie (and this is a balancer)
617: my ($found_server,$balancer_cookie) = &Apache::lonnet::check_for_balancer_cookie($r);
618: if (($found_server) && ($balancer_cookie =~ /^\Q$env{'user.domain'}\E_\Q$env{'user.name'}\E_/)) {
619: $otherserver = $found_server;
620: }
621: unless ($requrl eq '/adm/switchserver') {
622: $r->set_handlers('PerlResponseHandler'=>
623: [\&Apache::switchserver::handler]);
1.159.2.8 raeburn 624: }
625: if ($otherserver ne '') {
626: $env{'form.otherserver'} = $otherserver;
627: }
1.159.2.15 raeburn 628: unless (($env{'form.origurl'}) || ($r->uri eq '/adm/roles') ||
629: ($r->uri eq '/adm/switchserver') || ($r->uri eq '/adm/sso')) {
630: $env{'form.origurl'} = $r->uri;
631: }
1.152 raeburn 632: }
1.137 raeburn 633: }
634:
1.6 www 635: # ---------------------------------------------------------------- Check access
1.92 albertel 636: my $now = time;
1.159.2.17 raeburn 637: my ($check_symb,$check_access,$check_block,$access,$poss_symb);
1.159.2.10 raeburn 638: if ($requrl !~ m{^/(?:adm|public|(?:prt|zip)spool)/}
1.95 albertel 639: || $requrl =~ /^\/adm\/.*\/(smppg|bulletinboard)(\?|$ )/x) {
1.159.2.16 raeburn 640: $check_access = 1;
641: }
1.159.2.17 raeburn 642: if ((!$check_access) && ($env{'request.course.id'})) {
643: if (($requrl eq '/adm/viewclasslist') ||
644: ($requrl =~ m{^(/adm/wrapper|)\Q/uploaded/$cdom/$cnum/docs/\E}) ||
645: ($requrl =~ m{^/adm/.*/aboutme$}) ||
646: ($requrl=~m{^/adm/coursedocs/showdoc/})) {
647: $check_block = 1;
648: }
649: }
1.159.2.16 raeburn 650: if (($env{'request.course.id'}) && (!$suppext)) {
651: $requrl=~/\.(\w+)$/;
652: if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
653: ($requrl=~/^\/adm\/.*\/(aboutme|smppg|bulletinboard)(\?|$ )/x) ||
654: ($requrl=~/^\/adm\/wrapper\//) ||
655: ($requrl=~m|^/adm/coursedocs/showdoc/|) ||
656: ($requrl=~m|\.problem/smpedit$|) ||
657: ($requrl=~/^\/public\/.*\/syllabus$/) ||
658: ($requrl=~/^\/adm\/(viewclasslist|navmaps)$/) ||
659: ($requrl=~/^\/adm\/.*\/aboutme\/portfolio(\?|$)/)) {
660: $check_symb = 1;
1.159.2.12 raeburn 661: }
1.159.2.16 raeburn 662: }
1.159.2.17 raeburn 663: if (($check_access) || ($check_block)) {
1.159.2.12 raeburn 664: if ($check_symb) {
1.159.2.11 raeburn 665: if ($env{'form.symb'}) {
666: $poss_symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.159.2.12 raeburn 667: } elsif (($env{'request.course.id'}) && ($r->args ne '')) {
668: my $query = $r->args;
669: foreach my $pair (split(/&/,$query)) {
670: my ($name, $value) = split(/=/,$pair);
1.159.2.13 raeburn 671: $name = &unescape($name);
672: $value =~ tr/+/ /;
673: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
1.159.2.12 raeburn 674: if ($name eq 'symb') {
675: $poss_symb = &Apache::lonnet::symbclean($value);
676: last;
677: }
678: }
1.159.2.11 raeburn 679: }
680: if ($poss_symb) {
681: my ($possmap,$resid,$url)=&Apache::lonnet::decode_symb($poss_symb);
682: $url = &Apache::lonnet::clutter($url);
1.159.2.17 raeburn 683: my $toplevelmap = $env{'course.'.$env{'request.course.id'}.'.url'};
684: unless (($url eq $requrl) && (($possmap eq $toplevelmap) ||
685: (&Apache::lonnet::is_on_map($possmap)))) {
1.159.2.11 raeburn 686: undef($poss_symb);
687: }
688: if ($poss_symb) {
689: if ((!$env{'request.role.adv'}) && ($env{'acc.randomout'}) &&
690: ($env{'acc.randomout'}=~/\&\Q$poss_symb\E\&/)) {
691: undef($poss_symb);
692: }
693: }
694: }
695: if ($poss_symb) {
696: $access=&Apache::lonnet::allowed('bre',$requrl,$poss_symb);
697: } else {
698: $access=&Apache::lonnet::allowed('bre',$requrl,'','','','',1);
699: }
700: } else {
1.159.2.21! raeburn 701: my $clientip = &Apache::lonnet::get_requestor_ip($r);
! 702: $access=&Apache::lonnet::allowed('bre',$requrl,'','',$clientip);
1.159.2.11 raeburn 703: }
1.159.2.17 raeburn 704: }
705: if ($check_block) {
706: if ($access eq 'B') {
707: if ($poss_symb) {
708: if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
709: $env{'request.symb'} = $poss_symb;
710: }
711: }
712: &Apache::blockedaccess::setup_handler($r);
713: return OK;
714: }
715: } elsif ($check_access) {
1.159 raeburn 716: if ($handle eq '') {
717: unless ($access eq 'F') {
718: if ($requrl =~ m{^/res/$match_domain/$match_username/}) {
719: $r->log_reason("Cookie not valid", $r->filename);
720: }
721: }
722: }
1.92 albertel 723: if ($access eq '1') {
724: $env{'user.error.msg'}="$requrl:bre:0:0:Choose Course";
725: return HTTP_NOT_ACCEPTABLE;
726: }
727: if ($access eq 'A') {
728: &Apache::restrictedaccess::setup_handler($r);
729: return OK;
730: }
1.103 raeburn 731: if ($access eq 'B') {
1.159.2.11 raeburn 732: if ($poss_symb) {
733: if (&Apache::lonnet::symbverify($poss_symb,$requrl)) {
734: $env{'request.symb'} = $poss_symb;
735: }
736: }
1.103 raeburn 737: &Apache::blockedaccess::setup_handler($r);
738: return OK;
739: }
1.92 albertel 740: if (($access ne '2') && ($access ne 'F')) {
1.130 raeburn 741: if ($requrl =~ m{^/res/}) {
742: $access = &Apache::lonnet::allowed('bro',$requrl);
743: if ($access ne 'F') {
1.132 raeburn 744: if ($requrl eq '/res/lib/templates/simpleproblem.problem/smpedit') {
745: $access = &Apache::lonnet::allowed('bre','/res/lib/templates/simpleproblem.problem');
746: if ($access ne 'F') {
747: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
748: return HTTP_NOT_ACCEPTABLE;
749: }
750: } else {
751: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
752: return HTTP_NOT_ACCEPTABLE;
753: }
1.130 raeburn 754: }
1.159.2.3 raeburn 755: } elsif (($handle =~ /^publicuser_\d+$/) && (&Apache::lonnet::is_portfolio_url($requrl))) {
1.159.2.18 raeburn 756: my $clientip = &Apache::lonnet::get_requestor_ip($r);
1.159.2.3 raeburn 757: if (&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip) ne 'F') {
758: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
759: return HTTP_NOT_ACCEPTABLE;
760: }
1.130 raeburn 761: } else {
762: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
763: return HTTP_NOT_ACCEPTABLE;
764: }
1.37 albertel 765: }
1.92 albertel 766: }
767: if ($requrl =~ m|^/prtspool/|) {
768: my $start='/prtspool/'.$env{'user.name'}.'_'.
769: $env{'user.domain'};
770: if ($requrl !~ /^\Q$start\E/) {
771: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
772: return HTTP_NOT_ACCEPTABLE;
1.67 albertel 773: }
1.92 albertel 774: }
1.109 banghart 775: if ($requrl =~ m|^/zipspool/|) {
1.110 banghart 776: my $start='/zipspool/zipout/'.$env{'user.name'}.":".
1.109 banghart 777: $env{'user.domain'};
778: if ($requrl !~ /^\Q$start\E/) {
779: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
780: return HTTP_NOT_ACCEPTABLE;
781: }
782: }
1.92 albertel 783: if ($env{'user.name'} eq 'public' &&
784: $env{'user.domain'} eq 'public' &&
785: $requrl !~ m{^/+(res|public|uploaded)/} &&
786: $requrl !~ m{^/adm/[^/]+/[^/]+/aboutme/portfolio$ }x &&
1.159.2.12 raeburn 787: $requrl !~ m{^/adm/blockingstatus/.*$} &&
1.92 albertel 788: $requrl !~ m{^/+adm/(help|logout|restrictedaccess|randomlabel\.png)}) {
789: $env{'request.querystring'}=$r->args;
790: $env{'request.firsturl'}=$requrl;
791: return FORBIDDEN;
792: }
1.23 www 793: # ------------------------------------------------------------- This is allowed
1.92 albertel 794: if ($env{'request.course.id'}) {
1.24 www 795: &Apache::lonnet::countacc($requrl);
1.125 raeburn 796: my $query=$r->args;
1.159.2.12 raeburn 797: if ($check_symb) {
1.23 www 798: # ------------------------------------- This is serious stuff, get symb and log
1.92 albertel 799: my $symb;
800: if ($query) {
1.141 raeburn 801: &Apache::loncommon::get_unprocessed_cgi($query,['symb','folderpath']);
1.92 albertel 802: }
803: if ($env{'form.symb'}) {
1.64 albertel 804: $symb=&Apache::lonnet::symbclean($env{'form.symb'});
1.159.2.19 raeburn 805: if (($requrl eq '/adm/navmaps') ||
806: ($requrl =~ m{^/adm/wrapper/}) ||
807: ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
808: unless (&Apache::lonnet::symbverify($symb,$requrl)) {
809: if (&Apache::lonnet::is_on_map($requrl)) {
810: $symb = &Apache::lonnet::symbread($requrl);
811: unless (&Apache::lonnet::symbverify($symb,$requrl)) {
812: undef($symb);
813: }
814: }
815: }
816: if ($symb) {
817: if ($requrl eq '/adm/navmaps') {
818: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
819: &Apache::lonnet::symblist($map,$murl => [$murl,$mid]);
820: } elsif (($requrl =~ m{^/adm/wrapper/}) ||
821: ($requrl =~ m{^/adm/coursedocs/showdoc/})) {
822: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
823: if ($map =~ /\.page$/) {
824: my $mapsymb = &Apache::lonnet::symbread($map);
825: ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
826: }
827: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
828: 'last_known' =>[$murl,$mid]);
829: }
1.159.2.9 raeburn 830: }
1.92 albertel 831: } elsif ((&Apache::lonnet::symbverify($symb,$requrl)) ||
1.53 albertel 832: (($requrl=~m|(.*)/smpedit$|) &&
1.134 raeburn 833: &Apache::lonnet::symbverify($symb,$1)) ||
834: (($requrl=~m|(.*/aboutme)/portfolio$|) &&
835: &Apache::lonnet::symbverify($symb,$1))) {
1.92 albertel 836: my ($map,$mid,$murl)=&Apache::lonnet::decode_symb($symb);
1.159.2.9 raeburn 837: if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
838: my $mapsymb = &Apache::lonnet::symbread($map);
839: ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
840: }
1.92 albertel 841: &Apache::lonnet::symblist($map,$murl => [$murl,$mid],
842: 'last_known' =>[$murl,$mid]);
1.31 www 843: } else {
844: $r->log_reason('Invalid symb for '.$requrl.': '.
1.92 albertel 845: $symb);
846: $env{'user.error.msg'}=
847: "$requrl:bre:1:1:Invalid Access";
848: return HTTP_NOT_ACCEPTABLE;
849: }
850: } else {
1.134 raeburn 851: if ($requrl=~m{^(/adm/.*/aboutme)/portfolio$}) {
852: $requrl = $1;
853: }
1.159.2.12 raeburn 854: $symb=&Apache::lonnet::symbread($requrl);
855: if (&Apache::lonnet::is_on_map($requrl) && $symb) {
856: my ($encstate,$invalidsymb);
857: unless (&Apache::lonnet::symbverify($symb,$requrl,\$encstate)) {
858: $invalidsymb = 1;
859: #
1.159.2.14 raeburn 860: # If $env{'request.enc'} inconsistent with encryption expected for $symb
861: # retrieved by lonnet::symbread(), call again to check for an instance of
862: # $requrl in the course for which expected encryption matches request.enc.
863: # If symb for different instance passes lonnet::symbverify(), use that as
864: # the symb for $requrl and call &Apache::lonnet::allowed() for that symb.
865: # Report invalid symb if there is no other symb. Redirect to /adm/ambiguous
866: # if multiple possible symbs consistent with request.enc available for $requrl.
1.159.2.12 raeburn 867: #
1.159.2.14 raeburn 868: if (($env{'request.enc'} && !$encstate) || (!$env{'request.enc'} && $encstate)) {
1.159.2.12 raeburn 869: my %possibles;
870: my $nocache = 1;
1.159.2.14 raeburn 871: my $oldsymb = $symb;
1.159.2.12 raeburn 872: $symb = &Apache::lonnet::symbread($requrl,'','','',\%possibles,$nocache);
1.159.2.14 raeburn 873: if (($symb) && ($symb ne $oldsymb)) {
1.159.2.12 raeburn 874: if (&Apache::lonnet::symbverify($symb,$requrl)) {
1.159.2.14 raeburn 875: my $access=&Apache::lonnet::allowed('bre',$requrl,$symb);
876: if ($access eq 'B') {
877: $env{'request.symb'} = $symb;
878: &Apache::blockedaccess::setup_handler($r);
879: return OK;
880: } elsif (($access eq '2') || ($access eq 'F')) {
881: $invalidsymb = '';
882: }
1.159.2.11 raeburn 883: }
1.159.2.12 raeburn 884: } elsif (keys(%possibles) > 1) {
885: $r->internal_redirect('/adm/ambiguous');
886: return OK;
1.159.2.11 raeburn 887: }
1.159.2.12 raeburn 888: }
889: if ($invalidsymb) {
1.159.2.19 raeburn 890: if ($requrl eq '/adm/navmaps') {
1.159.2.20 raeburn 891: undef($symb);
1.159.2.19 raeburn 892: } else {
893: $r->log_reason('Invalid symb for '.$requrl.': '.$symb);
894: $env{'user.error.msg'}=
895: "$requrl:bre:1:1:Invalid Access";
896: return HTTP_NOT_ACCEPTABLE;
897: }
1.159.2.11 raeburn 898: }
899: }
1.159.2.12 raeburn 900: }
901: if ($symb) {
902: my ($map,$mid,$murl)=
903: &Apache::lonnet::decode_symb($symb);
904: if ($requrl eq '/adm/navmaps') {
905: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid]);
906: } else {
907: if (($map =~ /\.page$/) && ($requrl !~ /\.page$/)) {
908: my $mapsymb = &Apache::lonnet::symbread($map);
909: ($map,$mid,$murl)=&Apache::lonnet::decode_symb($mapsymb);
1.159.2.9 raeburn 910: }
1.159.2.12 raeburn 911: &Apache::lonnet::symblist($map,$murl =>[$murl,$mid],
912: 'last_known' =>[$murl,$mid]);
913: }
1.61 albertel 914: }
1.92 albertel 915: }
916: $env{'request.symb'}=$symb;
1.159.2.11 raeburn 917: if (($env{'request.symbread.cached.'}) && ($env{'request.symbread.cached.'} ne $symb)) {
918: $env{'request.symbread.cached.'} = $symb;
919: }
1.92 albertel 920: &Apache::lonnet::courseacclog($symb);
921: } else {
1.23 www 922: # ------------------------------------------------------- This is other content
1.92 albertel 923: &Apache::lonnet::courseacclog($requrl);
924: }
1.140 raeburn 925: if ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/(docs|supplemental)/.+\.html?$}) {
1.125 raeburn 926: if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
927: if ($query) {
928: &Apache::loncommon::get_unprocessed_cgi($query,['forceedit']);
929: if ($env{'form.forceedit'}) {
930: $env{'request.state'} = 'edit';
931: }
932: }
933: }
1.144 raeburn 934: } elsif ($requrl =~ m{^/+uploaded/\Q$cdom\E/\Q$cnum\E/portfolio/syllabus/.+\.html?$}) {
935: if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
936: if ($query) {
937: &Apache::loncommon::get_unprocessed_cgi($query,['forceedit','editmode']);
938: if (($env{'form.forceedit'}) || ($env{'form.editmode'})) {
939: $env{'request.state'} = 'edit';
940: }
941: }
942: }
1.125 raeburn 943: }
1.88 albertel 944: }
1.92 albertel 945: return OK;
1.137 raeburn 946: } else {
947: my $defdom=$r->dir_config('lonDefDomain');
948: ($is_balancer,$otherserver) =
949: &Apache::lonnet::check_loadbalancing(undef,$defdom);
950: if ($is_balancer) {
951: $r->set_handlers('PerlResponseHandler'=>
952: [\&Apache::switchserver::handler]);
953: if ($otherserver ne '') {
954: $env{'form.otherserver'} = $otherserver;
955: }
956: }
1.92 albertel 957: }
1.21 www 958: # -------------------------------------------- See if this is a public resource
1.68 albertel 959: if ($requrl=~m|^/+adm/+help/+|) {
1.89 albertel 960: return OK;
1.68 albertel 961: }
1.90 albertel 962: # ------------------------------------ See if this is a viewable portfolio file
1.89 albertel 963: if (&Apache::lonnet::is_portfolio_url($requrl)) {
1.159.2.18 raeburn 964: my $clientip = &Apache::lonnet::get_requestor_ip($r);
1.159.2.3 raeburn 965: my $access=&Apache::lonnet::allowed('bre',$requrl,undef,undef,$clientip);
1.90 albertel 966: if ($access eq 'A') {
967: &Apache::restrictedaccess::setup_handler($r);
968: return OK;
969: }
970: if (($access ne '2') && ($access ne 'F')) {
971: $env{'user.error.msg'}="$requrl:bre:1:1:Access Denied";
972: return HTTP_NOT_ACCEPTABLE;
973: }
1.79 raeburn 974: }
1.87 albertel 975:
1.34 www 976: # -------------------------------------------------------------- Not authorized
977: $requrl=~/\.(\w+)$/;
1.62 albertel 978: # if ((&Apache::loncommon::fileembstyle($1) eq 'ssi') ||
979: # ($requrl=~/^\/adm\/(roles|logout|email|menu|remote)/) ||
980: # ($requrl=~m|^/prtspool/|)) {
1.34 www 981: # -------------------------- Store where they wanted to go and get login screen
1.64 albertel 982: $env{'request.querystring'}=$r->args;
983: $env{'request.firsturl'}=$requrl;
1.34 www 984: return FORBIDDEN;
1.62 albertel 985: # } else {
1.34 www 986: # --------------------------------------------------------------------- Goodbye
1.62 albertel 987: # return HTTP_BAD_REQUEST;
988: # }
1.1 albertel 989: }
990:
991: 1;
992: __END__
1.120 jms 993:
1.121 jms 994: =pod
1.120 jms 995:
1.121 jms 996: =back
1.120 jms 997:
1.121 jms 998: =cut
1.120 jms 999:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>