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