File:
[LON-CAPA] /
loncom /
auth /
lonwebdavacc.pm
Revision
1.7:
download - view:
text,
annotated -
select for diffs
Fri Dec 18 15:23:03 2020 UTC (3 years, 8 months ago) by
raeburn
Branches:
MAIN
CVS tags:
version_2_12_X,
version_2_11_X,
version_2_11_5_msu,
version_2_11_5,
version_2_11_4_uiuc,
version_2_11_4_msu,
version_2_11_4,
version_2_11_3_uiuc,
version_2_11_3_msu,
version_2_11_3,
HEAD
- Retrieval of requestor's IP address centralized in lonnet::get_requestor_ip()
- Domain configuration to allow domain's LON-CAPA nodes to operate behind a
WAF/Reverse Proxy using aliased hostname (CNAME).
- Web requests from other nodes bypass the WAF as their requests are made
directly to the server hostname (A record); same for internal LON-CAPA
connections for lonc -> lond.
1: # The LearningOnline Network
2: # Authorization Handler for webDAV access to Authoring Space.
3: #
4: # $Id: lonwebdavacc.pm,v 1.7 2020/12/18 15:23:03 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: =pod
30:
31: =head1 NAME
32:
33: Apache::lonwebdavacc - webDAV Authorization Handler
34:
35: =head1 SYNOPSIS
36:
37: Invoked for ^/+webdav/[\w\-.]+/\w[\w.\-\@]+/ by
38: /etc/httpd/conf/loncapa_apache.conf:
39:
40: PerlAccessHandler Apache::lonwebdavacc
41:
42: =head1 INTRODUCTION
43:
44: This module enables authorization for authoring space
45: and is used to control access for the following type of URI:
46:
47: <LocationMatch "^/+webdav/[\w\-.]+/\w[\w.\-\@]+/">
48:
49: This module is only called following successful authentication.
50: Successful authentication will have created a session file and
51: transferred the contents to the user's environment.
52:
53: Note: because Apache Basic Auth is used for authentication
54: webDAV access is only available for servers running Apache with SSL.
55:
56: This is part of the LearningOnline Network with CAPA project
57: described at http://www.lon-capa.org.
58:
59: =head1 HANDLER SUBROUTINE
60:
61: This routine is called by Apache and mod_perl.
62:
63: =over 4
64:
65: =item *
66:
67: Checks if $env{'user.environment'} is defined.
68:
69: =item *
70:
71: If no %env, calls Apache::lonnet::check_for_valid_session()
72: to retrieve a valid sessionID (webDAV client needs to support
73: cookies for session retrieval to be successful). If a session is
74: found Apache::lonnet::transfer_profile_to_env() is called
75: to populate %env.
76:
77: =item *
78:
79: Checks if requested URL (of form /webdav/authordomain/authorname) is valid
80: and whether authenticated user has an active author or co-author
81: role in the corresponding Authoring Space.
82:
83: =back
84:
85: =head1 NOTABLE SUBROUTINES
86:
87: =over
88:
89: =item * sso_login()
90:
91: =over
92:
93: =item *
94:
95: Not currently used.
96:
97: =item *
98:
99: Checks if $r->user contains a valid user.
100:
101: =item *
102:
103: Domain is set either from lonSSOUserDomain perlvar (if defined)
104: or from lonDefDomain perlvar.
105:
106: =item *
107:
108: For a valid user a new session file and is created, and the corresponding
109: cookie is returned to the client in an Apache response header.
110:
111: =back
112:
113: =back
114:
115: =cut
116:
117: package Apache::lonwebdavacc;
118:
119: use strict;
120: use GDBM_File;
121: use Apache::Constants qw(:common :http :methods);
122: use Apache::lonnet;
123: use Apache::londiff();
124: use LONCAPA qw(:DEFAULT :match);
125:
126: sub handler {
127: my $r = shift;
128: my $timetolive = 600;
129: my $now = time;
130: my $sessiondir=$r->dir_config('lonDAVsessDir');
131:
132: my ($adom,$aname) = ($r->uri =~ m{^/webdav/($match_domain)/($match_username)/});
133: my $author = "$aname:$adom";
134: unless ($env{'user.environment'}) {
135: my $handle = &Apache::lonnet::check_for_valid_session($r,'lonDAV');
136: if ($handle ne '') {
137: &Apache::lonnet::transfer_profile_to_env($sessiondir,$handle);
138: } else {
139: return FORBIDDEN;
140: }
141: }
142: my $uhome=&Apache::lonnet::homeserver($env{'user.name'},$env{'user.domain'});
143: if ($uhome =~ /^(con_lost|no_host|no_such_host)$/) {
144: return FORBIDDEN;
145: }
146:
147: my $docroot = $r->dir_config('lonDocRoot');
148: if ($adom eq '' || $aname eq '') {
149: return FORBIDDEN;
150: } elsif (!-d "$docroot/priv/$adom/$aname") {
151: return FORBIDDEN;
152: }
153: my $allowed;
154: if (($env{'user.name'} eq $aname) && ($env{'user.domain'} eq $adom)) {
155: if ($env{"user.role.au./$adom/"}) {
156: $allowed = 1;
157: }
158: } else {
159: if (($env{"user.role.ca./$adom/$aname"}) ||
160: ($env{"user.role.aa./$adom/$aname"})) {
161: $allowed = 1;
162: }
163: }
164: if ($allowed) {
165: my $method = $r->method();
166: if (($r->filename =~ /.+\.(log|bak|meta|save)$/) || ($r->filename =~ /\.\d+\.\w+$/) ||
167: ($r->filename =~ m{/\.+[^_/]+$})) {
168: if (($method eq 'MKCOL') || ($method eq 'PUT')) {
169: return FORBIDDEN;
170: } elsif ($method eq 'MOVE') {
171: if (($r->filename =~ /\.\d+\.\w+$/) || ($r->filename =~ m{/\.+[^_/]+$})) {
172: return FORBIDDEN;
173: }
174: }
175: }
176: if (($method eq 'DELETE') || ($method eq 'MOVE')) {
177: unless (($r->filename =~ m{/\._[^/]+$}) || ($r->filename =~ m{/\.DS_Store$})) {
178: my $dirptr=16384;
179: my ($cmode,$cmtime)=(stat($r->filename))[2,9];
180: if (($cmode&$dirptr)) {
181: my $numpub = 0;
182: $numpub = &recurse_dir($r->filename,$r->dir_config('lonDocRoot'),$numpub);
183: if ($numpub) {
184: return FORBIDDEN;
185: }
186: } else {
187: if ($r->filename =~ /^(.+)\.(log|bak|save|meta)$/) {
188: my $conjugate = $1;
189: my $type = $2;
190: if (($type eq 'log') || ($type eq 'meta')) {
191: if (-e $conjugate) {
192: my $conjstatus = &pubstatus($conjugate,$r->dir_config('lonDocRoot'));
193: unless (($conjstatus eq 'unpublished') || ($conjstatus eq 'obsolete')) {
194: return FORBIDDEN;
195: }
196: }
197: }
198: } else {
199: my $status = &pubstatus($r->filename,$r->dir_config('lonDocRoot'));
200: unless (($status eq 'unpublished') || ($status eq 'obsolete')) {
201: return FORBIDDEN;
202: }
203: }
204: }
205: }
206: }
207: return OK;
208: }
209: return FORBIDDEN;
210: }
211:
212: sub sso_login {
213: my ($r,$sessiondir,$now,$timetolive,$author) = @_;
214: my ($uname,$udom);
215: my ($uname) = ($r->user =~ m/([a-zA-Z0-9_\-@.]*)/);
216: unless ($uname =~ /^$match_username$/) {
217: return;
218: }
219: $udom = $r->dir_config('lonSSOUserDomain');
220: if ($udom eq '') {
221: $udom = $r->dir_config('lonDefDomain');
222: }
223: unless (($udom =~ /^$match_domain$/)) {
224: return;
225: }
226: my $uhome = &Apache::lonnet::homeserver($uname,$udom);
227: if ($uhome =~ /^(con_lost|no_host|no_such_host)$/) {
228: return;
229: }
230: my $handle =
231: &Apache::lonwebdavauth::init_webdav_env($r,$sessiondir,$uname,$udom,
232: $uhome,$now,$timetolive,$author);
233: if ($handle ne '') {
234: if (&Apache::lonnet::usertools_access($uname,$udom,'webdav')) {
235: my ($webdav) =
236: ($r->uri =~ m{^(/webdav/$match_domain/$match_username/)});
237: my $ip = &Apache::lonnet::get_requestor_ip();
238: &Apache::lonnet::log($udom,$uname,$uhome,
239: "SSO log-in to $webdav from $ip");
240: my $cookie = "lonDAV=$handle; path=/webdav/; secure; HttpOnly;";
241: $r->header_out('Set-cookie' => $cookie);
242: $r->send_http_header;
243: }
244: }
245: return ($handle);
246: }
247:
248: sub pubstatus {
249: my ($fn,$docroot,$cmtime) = @_;
250: my $privfn = $fn;
251: my $thisdisfn = $fn;
252: $thisdisfn=~s/^\Q$docroot\E\/priv//;
253: my $resfn=$docroot.'/res'.$thisdisfn;
254: my $targetfn = '/res'.$thisdisfn;
255: my $status = 'unpublished';
256: if (-e $resfn) {
257: $status = 'published';
258: my $same = 0;
259: if ((stat($resfn))[9] >= $cmtime) {
260: $same = 1;
261: } else {
262: if (&Apache::londiff::are_different_files($resfn,$privfn)) {
263: $same = 0;
264: } else {
265: $same = 1;
266: }
267: }
268: if ($same) {
269: if (&Apache::lonnet::metadata($targetfn,'obsolete')) {
270: $status = 'obsolete';
271: }
272: }
273: }
274: return $status;
275: }
276:
277: sub recurse_dir {
278: my ($dirname,$docroot,$numpub) = @_;
279: $dirname =~ s{/$}{};
280: my $dirptr=16384;
281: if (opendir(my $dirh,$dirname)) {
282: my @items = readdir($dirh);
283: closedir($dirh);
284: foreach my $item (@items) {
285: next if ($item =~ /.+\.(log|bak|save|meta)$/);
286: next if ($item =~ /^\.+/);
287: my ($cmode,$cmtime)=(stat("$dirname/$item"))[2,9];
288: if (!($cmode&$dirptr)) {
289: if (&pubstatus("$dirname/$item",$docroot,$cmtime) eq 'published') {
290: $numpub ++;
291: }
292: } else {
293: $numpub = &recurse_dir("$dirname/$item",$docroot,$numpub);
294: }
295: }
296: }
297: return $numpub;
298: }
299:
300: 1;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>