blob: 33c5f402b4ed143111e6e112f7b684d9e9b9fa93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2001-2002 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_fs.h"
34#include "xfs_types.h"
35#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_inum.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_dir.h"
39#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110041#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "xfs_dir_sf.h"
44#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110045#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_dinode.h"
47#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110048#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include "xfs_acl.h"
50#include "xfs_mac.h"
51#include "xfs_attr.h"
52
53#include <linux/posix_acl_xattr.h>
54
55STATIC int xfs_acl_setmode(vnode_t *, xfs_acl_t *, int *);
56STATIC void xfs_acl_filter_mode(mode_t, xfs_acl_t *);
57STATIC void xfs_acl_get_endian(xfs_acl_t *);
58STATIC int xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
59STATIC int xfs_acl_invalid(xfs_acl_t *);
60STATIC void xfs_acl_sync_mode(mode_t, xfs_acl_t *);
61STATIC void xfs_acl_get_attr(vnode_t *, xfs_acl_t *, int, int, int *);
62STATIC void xfs_acl_set_attr(vnode_t *, xfs_acl_t *, int, int *);
63STATIC int xfs_acl_allow_set(vnode_t *, int);
64
65kmem_zone_t *xfs_acl_zone;
66
67
68/*
69 * Test for existence of access ACL attribute as efficiently as possible.
70 */
71int
72xfs_acl_vhasacl_access(
73 vnode_t *vp)
74{
75 int error;
76
77 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
78 return (error == 0);
79}
80
81/*
82 * Test for existence of default ACL attribute as efficiently as possible.
83 */
84int
85xfs_acl_vhasacl_default(
86 vnode_t *vp)
87{
88 int error;
89
Christoph Hellwig0432dab2005-09-02 16:46:51 +100090 if (!VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92 xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
93 return (error == 0);
94}
95
96/*
97 * Convert from extended attribute representation to in-memory for XFS.
98 */
99STATIC int
100posix_acl_xattr_to_xfs(
101 posix_acl_xattr_header *src,
102 size_t size,
103 xfs_acl_t *dest)
104{
105 posix_acl_xattr_entry *src_entry;
106 xfs_acl_entry_t *dest_entry;
107 int n;
108
109 if (!src || !dest)
110 return EINVAL;
111
112 if (size < sizeof(posix_acl_xattr_header))
113 return EINVAL;
114
115 if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
116 return EOPNOTSUPP;
117
118 memset(dest, 0, sizeof(xfs_acl_t));
119 dest->acl_cnt = posix_acl_xattr_count(size);
120 if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
121 return EINVAL;
122
123 /*
124 * acl_set_file(3) may request that we set default ACLs with
125 * zero length -- defend (gracefully) against that here.
126 */
127 if (!dest->acl_cnt)
128 return 0;
129
130 src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
131 dest_entry = &dest->acl_entry[0];
132
133 for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
134 dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
135 if (_ACL_PERM_INVALID(dest_entry->ae_perm))
136 return EINVAL;
137 dest_entry->ae_tag = le16_to_cpu(src_entry->e_tag);
138 switch(dest_entry->ae_tag) {
139 case ACL_USER:
140 case ACL_GROUP:
141 dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
142 break;
143 case ACL_USER_OBJ:
144 case ACL_GROUP_OBJ:
145 case ACL_MASK:
146 case ACL_OTHER:
147 dest_entry->ae_id = ACL_UNDEFINED_ID;
148 break;
149 default:
150 return EINVAL;
151 }
152 }
153 if (xfs_acl_invalid(dest))
154 return EINVAL;
155
156 return 0;
157}
158
159/*
Nathan Scott380b5dc2005-11-02 11:43:18 +1100160 * Comparison function called from xfs_sort().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * Primary key is ae_tag, secondary key is ae_id.
162 */
163STATIC int
164xfs_acl_entry_compare(
165 const void *va,
166 const void *vb)
167{
168 xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
169 *b = (xfs_acl_entry_t *)vb;
170
171 if (a->ae_tag == b->ae_tag)
172 return (a->ae_id - b->ae_id);
173 return (a->ae_tag - b->ae_tag);
174}
175
176/*
177 * Convert from in-memory XFS to extended attribute representation.
178 */
179STATIC int
180posix_acl_xfs_to_xattr(
181 xfs_acl_t *src,
182 posix_acl_xattr_header *dest,
183 size_t size)
184{
185 int n;
186 size_t new_size = posix_acl_xattr_size(src->acl_cnt);
187 posix_acl_xattr_entry *dest_entry;
188 xfs_acl_entry_t *src_entry;
189
190 if (size < new_size)
191 return -ERANGE;
192
193 /* Need to sort src XFS ACL by <ae_tag,ae_id> */
Nathan Scott380b5dc2005-11-02 11:43:18 +1100194 xfs_sort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
195 xfs_acl_entry_compare);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
198 dest_entry = &dest->a_entries[0];
199 src_entry = &src->acl_entry[0];
200 for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
201 dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
202 if (_ACL_PERM_INVALID(src_entry->ae_perm))
203 return -EINVAL;
204 dest_entry->e_tag = cpu_to_le16(src_entry->ae_tag);
205 switch (src_entry->ae_tag) {
206 case ACL_USER:
207 case ACL_GROUP:
208 dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
209 break;
210 case ACL_USER_OBJ:
211 case ACL_GROUP_OBJ:
212 case ACL_MASK:
213 case ACL_OTHER:
214 dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
215 break;
216 default:
217 return -EINVAL;
218 }
219 }
220 return new_size;
221}
222
223int
224xfs_acl_vget(
225 vnode_t *vp,
226 void *acl,
227 size_t size,
228 int kind)
229{
230 int error;
231 xfs_acl_t *xfs_acl = NULL;
232 posix_acl_xattr_header *ext_acl = acl;
233 int flags = 0;
234
235 VN_HOLD(vp);
236 if(size) {
237 if (!(_ACL_ALLOC(xfs_acl))) {
238 error = ENOMEM;
239 goto out;
240 }
241 memset(xfs_acl, 0, sizeof(xfs_acl_t));
242 } else
243 flags = ATTR_KERNOVAL;
244
245 xfs_acl_get_attr(vp, xfs_acl, kind, flags, &error);
246 if (error)
247 goto out;
248
249 if (!size) {
250 error = -posix_acl_xattr_size(XFS_ACL_MAX_ENTRIES);
251 } else {
252 if (xfs_acl_invalid(xfs_acl)) {
253 error = EINVAL;
254 goto out;
255 }
256 if (kind == _ACL_TYPE_ACCESS) {
257 vattr_t va;
258
259 va.va_mask = XFS_AT_MODE;
260 VOP_GETATTR(vp, &va, 0, sys_cred, error);
261 if (error)
262 goto out;
263 xfs_acl_sync_mode(va.va_mode, xfs_acl);
264 }
265 error = -posix_acl_xfs_to_xattr(xfs_acl, ext_acl, size);
266 }
267out:
268 VN_RELE(vp);
269 if(xfs_acl)
270 _ACL_FREE(xfs_acl);
271 return -error;
272}
273
274int
275xfs_acl_vremove(
276 vnode_t *vp,
277 int kind)
278{
279 int error;
280
281 VN_HOLD(vp);
282 error = xfs_acl_allow_set(vp, kind);
283 if (!error) {
284 VOP_ATTR_REMOVE(vp, kind == _ACL_TYPE_DEFAULT?
285 SGI_ACL_DEFAULT: SGI_ACL_FILE,
286 ATTR_ROOT, sys_cred, error);
287 if (error == ENOATTR)
288 error = 0; /* 'scool */
289 }
290 VN_RELE(vp);
291 return -error;
292}
293
294int
295xfs_acl_vset(
296 vnode_t *vp,
297 void *acl,
298 size_t size,
299 int kind)
300{
301 posix_acl_xattr_header *ext_acl = acl;
302 xfs_acl_t *xfs_acl;
303 int error;
304 int basicperms = 0; /* more than std unix perms? */
305
306 if (!acl)
307 return -EINVAL;
308
309 if (!(_ACL_ALLOC(xfs_acl)))
310 return -ENOMEM;
311
312 error = posix_acl_xattr_to_xfs(ext_acl, size, xfs_acl);
313 if (error) {
314 _ACL_FREE(xfs_acl);
315 return -error;
316 }
317 if (!xfs_acl->acl_cnt) {
318 _ACL_FREE(xfs_acl);
319 return 0;
320 }
321
322 VN_HOLD(vp);
323 error = xfs_acl_allow_set(vp, kind);
324 if (error)
325 goto out;
326
327 /* Incoming ACL exists, set file mode based on its value */
328 if (kind == _ACL_TYPE_ACCESS)
329 xfs_acl_setmode(vp, xfs_acl, &basicperms);
330
331 /*
332 * If we have more than std unix permissions, set up the actual attr.
333 * Otherwise, delete any existing attr. This prevents us from
334 * having actual attrs for permissions that can be stored in the
335 * standard permission bits.
336 */
337 if (!basicperms) {
338 xfs_acl_set_attr(vp, xfs_acl, kind, &error);
339 } else {
340 xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
341 }
342
343out:
344 VN_RELE(vp);
345 _ACL_FREE(xfs_acl);
346 return -error;
347}
348
349int
350xfs_acl_iaccess(
351 xfs_inode_t *ip,
352 mode_t mode,
353 cred_t *cr)
354{
355 xfs_acl_t *acl;
356 int rval;
357
358 if (!(_ACL_ALLOC(acl)))
359 return -1;
360
361 /* If the file has no ACL return -1. */
362 rval = sizeof(xfs_acl_t);
363 if (xfs_attr_fetch(ip, SGI_ACL_FILE, SGI_ACL_FILE_SIZE,
364 (char *)acl, &rval, ATTR_ROOT | ATTR_KERNACCESS, cr)) {
365 _ACL_FREE(acl);
366 return -1;
367 }
368 xfs_acl_get_endian(acl);
369
370 /* If the file has an empty ACL return -1. */
371 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
372 _ACL_FREE(acl);
373 return -1;
374 }
375
376 /* Synchronize ACL with mode bits */
377 xfs_acl_sync_mode(ip->i_d.di_mode, acl);
378
379 rval = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
380 _ACL_FREE(acl);
381 return rval;
382}
383
384STATIC int
385xfs_acl_allow_set(
386 vnode_t *vp,
387 int kind)
388{
389 vattr_t va;
390 int error;
391
392 if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
393 return EPERM;
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000394 if (kind == _ACL_TYPE_DEFAULT && !VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return ENOTDIR;
396 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
397 return EROFS;
398 va.va_mask = XFS_AT_UID;
399 VOP_GETATTR(vp, &va, 0, NULL, error);
400 if (error)
401 return error;
402 if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
403 return EPERM;
404 return error;
405}
406
407/*
408 * The access control process to determine the access permission:
409 * if uid == file owner id, use the file owner bits.
410 * if gid == file owner group id, use the file group bits.
411 * scan ACL for a maching user or group, and use matched entry
412 * permission. Use total permissions of all matching group entries,
413 * until all acl entries are exhausted. The final permission produced
414 * by matching acl entry or entries needs to be & with group permission.
415 * if not owner, owning group, or matching entry in ACL, use file
416 * other bits.
417 */
418STATIC int
419xfs_acl_capability_check(
420 mode_t mode,
421 cred_t *cr)
422{
423 if ((mode & ACL_READ) && !capable_cred(cr, CAP_DAC_READ_SEARCH))
424 return EACCES;
425 if ((mode & ACL_WRITE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
426 return EACCES;
427 if ((mode & ACL_EXECUTE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
428 return EACCES;
429
430 return 0;
431}
432
433/*
434 * Note: cr is only used here for the capability check if the ACL test fails.
435 * It is not used to find out the credentials uid or groups etc, as was
436 * done in IRIX. It is assumed that the uid and groups for the current
437 * thread are taken from "current" instead of the cr parameter.
438 */
439STATIC int
440xfs_acl_access(
441 uid_t fuid,
442 gid_t fgid,
443 xfs_acl_t *fap,
444 mode_t md,
445 cred_t *cr)
446{
447 xfs_acl_entry_t matched;
448 int i, allows;
449 int maskallows = -1; /* true, but not 1, either */
450 int seen_userobj = 0;
451
452 matched.ae_tag = 0; /* Invalid type */
453 md >>= 6; /* Normalize the bits for comparison */
454
455 for (i = 0; i < fap->acl_cnt; i++) {
456 /*
457 * Break out if we've got a user_obj entry or
458 * a user entry and the mask (and have processed USER_OBJ)
459 */
460 if (matched.ae_tag == ACL_USER_OBJ)
461 break;
462 if (matched.ae_tag == ACL_USER) {
463 if (maskallows != -1 && seen_userobj)
464 break;
465 if (fap->acl_entry[i].ae_tag != ACL_MASK &&
466 fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
467 continue;
468 }
469 /* True if this entry allows the requested access */
470 allows = ((fap->acl_entry[i].ae_perm & md) == md);
471
472 switch (fap->acl_entry[i].ae_tag) {
473 case ACL_USER_OBJ:
474 seen_userobj = 1;
475 if (fuid != current->fsuid)
476 continue;
477 matched.ae_tag = ACL_USER_OBJ;
478 matched.ae_perm = allows;
479 break;
480 case ACL_USER:
481 if (fap->acl_entry[i].ae_id != current->fsuid)
482 continue;
483 matched.ae_tag = ACL_USER;
484 matched.ae_perm = allows;
485 break;
486 case ACL_GROUP_OBJ:
487 if ((matched.ae_tag == ACL_GROUP_OBJ ||
488 matched.ae_tag == ACL_GROUP) && !allows)
489 continue;
490 if (!in_group_p(fgid))
491 continue;
492 matched.ae_tag = ACL_GROUP_OBJ;
493 matched.ae_perm = allows;
494 break;
495 case ACL_GROUP:
496 if ((matched.ae_tag == ACL_GROUP_OBJ ||
497 matched.ae_tag == ACL_GROUP) && !allows)
498 continue;
499 if (!in_group_p(fap->acl_entry[i].ae_id))
500 continue;
501 matched.ae_tag = ACL_GROUP;
502 matched.ae_perm = allows;
503 break;
504 case ACL_MASK:
505 maskallows = allows;
506 break;
507 case ACL_OTHER:
508 if (matched.ae_tag != 0)
509 continue;
510 matched.ae_tag = ACL_OTHER;
511 matched.ae_perm = allows;
512 break;
513 }
514 }
515 /*
516 * First possibility is that no matched entry allows access.
517 * The capability to override DAC may exist, so check for it.
518 */
519 switch (matched.ae_tag) {
520 case ACL_OTHER:
521 case ACL_USER_OBJ:
522 if (matched.ae_perm)
523 return 0;
524 break;
525 case ACL_USER:
526 case ACL_GROUP_OBJ:
527 case ACL_GROUP:
528 if (maskallows && matched.ae_perm)
529 return 0;
530 break;
531 case 0:
532 break;
533 }
534
535 return xfs_acl_capability_check(md, cr);
536}
537
538/*
539 * ACL validity checker.
540 * This acl validation routine checks each ACL entry read in makes sense.
541 */
542STATIC int
543xfs_acl_invalid(
544 xfs_acl_t *aclp)
545{
546 xfs_acl_entry_t *entry, *e;
547 int user = 0, group = 0, other = 0, mask = 0;
548 int mask_required = 0;
549 int i, j;
550
551 if (!aclp)
552 goto acl_invalid;
553
554 if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
555 goto acl_invalid;
556
557 for (i = 0; i < aclp->acl_cnt; i++) {
558 entry = &aclp->acl_entry[i];
559 switch (entry->ae_tag) {
560 case ACL_USER_OBJ:
561 if (user++)
562 goto acl_invalid;
563 break;
564 case ACL_GROUP_OBJ:
565 if (group++)
566 goto acl_invalid;
567 break;
568 case ACL_OTHER:
569 if (other++)
570 goto acl_invalid;
571 break;
572 case ACL_USER:
573 case ACL_GROUP:
574 for (j = i + 1; j < aclp->acl_cnt; j++) {
575 e = &aclp->acl_entry[j];
576 if (e->ae_id == entry->ae_id &&
577 e->ae_tag == entry->ae_tag)
578 goto acl_invalid;
579 }
580 mask_required++;
581 break;
582 case ACL_MASK:
583 if (mask++)
584 goto acl_invalid;
585 break;
586 default:
587 goto acl_invalid;
588 }
589 }
590 if (!user || !group || !other || (mask_required && !mask))
591 goto acl_invalid;
592 else
593 return 0;
594acl_invalid:
595 return EINVAL;
596}
597
598/*
599 * Do ACL endian conversion.
600 */
601STATIC void
602xfs_acl_get_endian(
603 xfs_acl_t *aclp)
604{
605 xfs_acl_entry_t *ace, *end;
606
607 INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
608 end = &aclp->acl_entry[0]+aclp->acl_cnt;
609 for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
610 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
611 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
612 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
613 }
614}
615
616/*
617 * Get the ACL from the EA and do endian conversion.
618 */
619STATIC void
620xfs_acl_get_attr(
621 vnode_t *vp,
622 xfs_acl_t *aclp,
623 int kind,
624 int flags,
625 int *error)
626{
627 int len = sizeof(xfs_acl_t);
628
629 ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
630 flags |= ATTR_ROOT;
631 VOP_ATTR_GET(vp,
632 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE : SGI_ACL_DEFAULT,
633 (char *)aclp, &len, flags, sys_cred, *error);
634 if (*error || (flags & ATTR_KERNOVAL))
635 return;
636 xfs_acl_get_endian(aclp);
637}
638
639/*
640 * Set the EA with the ACL and do endian conversion.
641 */
642STATIC void
643xfs_acl_set_attr(
644 vnode_t *vp,
645 xfs_acl_t *aclp,
646 int kind,
647 int *error)
648{
649 xfs_acl_entry_t *ace, *newace, *end;
650 xfs_acl_t *newacl;
651 int len;
652
653 if (!(_ACL_ALLOC(newacl))) {
654 *error = ENOMEM;
655 return;
656 }
657
658 len = sizeof(xfs_acl_t) -
659 (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
660 end = &aclp->acl_entry[0]+aclp->acl_cnt;
661 for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
662 ace < end;
663 ace++, newace++) {
664 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
665 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
666 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
667 }
668 INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
669 VOP_ATTR_SET(vp,
670 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE: SGI_ACL_DEFAULT,
671 (char *)newacl, len, ATTR_ROOT, sys_cred, *error);
672 _ACL_FREE(newacl);
673}
674
675int
676xfs_acl_vtoacl(
677 vnode_t *vp,
678 xfs_acl_t *access_acl,
679 xfs_acl_t *default_acl)
680{
681 vattr_t va;
682 int error = 0;
683
684 if (access_acl) {
685 /*
686 * Get the Access ACL and the mode. If either cannot
687 * be obtained for some reason, invalidate the access ACL.
688 */
689 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
690 if (!error) {
691 /* Got the ACL, need the mode... */
692 va.va_mask = XFS_AT_MODE;
693 VOP_GETATTR(vp, &va, 0, sys_cred, error);
694 }
695
696 if (error)
697 access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
698 else /* We have a good ACL and the file mode, synchronize. */
699 xfs_acl_sync_mode(va.va_mode, access_acl);
700 }
701
702 if (default_acl) {
703 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
704 if (error)
705 default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
706 }
707 return error;
708}
709
710/*
711 * This function retrieves the parent directory's acl, processes it
712 * and lets the child inherit the acl(s) that it should.
713 */
714int
715xfs_acl_inherit(
716 vnode_t *vp,
717 vattr_t *vap,
718 xfs_acl_t *pdaclp)
719{
720 xfs_acl_t *cacl;
721 int error = 0;
722 int basicperms = 0;
723
724 /*
725 * If the parent does not have a default ACL, or it's an
726 * invalid ACL, we're done.
727 */
728 if (!vp)
729 return 0;
730 if (!pdaclp || xfs_acl_invalid(pdaclp))
731 return 0;
732
733 /*
734 * Copy the default ACL of the containing directory to
735 * the access ACL of the new file and use the mode that
736 * was passed in to set up the correct initial values for
737 * the u::,g::[m::], and o:: entries. This is what makes
738 * umask() "work" with ACL's.
739 */
740
741 if (!(_ACL_ALLOC(cacl)))
742 return ENOMEM;
743
744 memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
745 xfs_acl_filter_mode(vap->va_mode, cacl);
746 xfs_acl_setmode(vp, cacl, &basicperms);
747
748 /*
749 * Set the Default and Access ACL on the file. The mode is already
750 * set on the file, so we don't need to worry about that.
751 *
752 * If the new file is a directory, its default ACL is a copy of
753 * the containing directory's default ACL.
754 */
Christoph Hellwig0432dab2005-09-02 16:46:51 +1000755 if (VN_ISDIR(vp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
757 if (!error && !basicperms)
758 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
759 _ACL_FREE(cacl);
760 return error;
761}
762
763/*
764 * Set up the correct mode on the file based on the supplied ACL. This
765 * makes sure that the mode on the file reflects the state of the
766 * u::,g::[m::], and o:: entries in the ACL. Since the mode is where
767 * the ACL is going to get the permissions for these entries, we must
768 * synchronize the mode whenever we set the ACL on a file.
769 */
770STATIC int
771xfs_acl_setmode(
772 vnode_t *vp,
773 xfs_acl_t *acl,
774 int *basicperms)
775{
776 vattr_t va;
777 xfs_acl_entry_t *ap;
778 xfs_acl_entry_t *gap = NULL;
779 int i, error, nomask = 1;
780
781 *basicperms = 1;
782
783 if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
784 return 0;
785
786 /*
787 * Copy the u::, g::, o::, and m:: bits from the ACL into the
788 * mode. The m:: bits take precedence over the g:: bits.
789 */
790 va.va_mask = XFS_AT_MODE;
791 VOP_GETATTR(vp, &va, 0, sys_cred, error);
792 if (error)
793 return error;
794
795 va.va_mask = XFS_AT_MODE;
796 va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
797 ap = acl->acl_entry;
798 for (i = 0; i < acl->acl_cnt; ++i) {
799 switch (ap->ae_tag) {
800 case ACL_USER_OBJ:
801 va.va_mode |= ap->ae_perm << 6;
802 break;
803 case ACL_GROUP_OBJ:
804 gap = ap;
805 break;
806 case ACL_MASK: /* more than just standard modes */
807 nomask = 0;
808 va.va_mode |= ap->ae_perm << 3;
809 *basicperms = 0;
810 break;
811 case ACL_OTHER:
812 va.va_mode |= ap->ae_perm;
813 break;
814 default: /* more than just standard modes */
815 *basicperms = 0;
816 break;
817 }
818 ap++;
819 }
820
821 /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
822 if (gap && nomask)
823 va.va_mode |= gap->ae_perm << 3;
824
825 VOP_SETATTR(vp, &va, 0, sys_cred, error);
826 return error;
827}
828
829/*
830 * The permissions for the special ACL entries (u::, g::[m::], o::) are
831 * actually stored in the file mode (if there is both a group and a mask,
832 * the group is stored in the ACL entry and the mask is stored on the file).
833 * This allows the mode to remain automatically in sync with the ACL without
834 * the need for a call-back to the ACL system at every point where the mode
835 * could change. This function takes the permissions from the specified mode
836 * and places it in the supplied ACL.
837 *
838 * This implementation draws its validity from the fact that, when the ACL
839 * was assigned, the mode was copied from the ACL.
840 * If the mode did not change, therefore, the mode remains exactly what was
841 * taken from the special ACL entries at assignment.
842 * If a subsequent chmod() was done, the POSIX spec says that the change in
843 * mode must cause an update to the ACL seen at user level and used for
844 * access checks. Before and after a mode change, therefore, the file mode
845 * most accurately reflects what the special ACL entries should permit/deny.
846 *
847 * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
848 * the existing mode bits will override whatever is in the
849 * ACL. Similarly, if there is a pre-existing ACL that was
850 * never in sync with its mode (owing to a bug in 6.5 and
851 * before), it will now magically (or mystically) be
852 * synchronized. This could cause slight astonishment, but
853 * it is better than inconsistent permissions.
854 *
855 * The supplied ACL is a template that may contain any combination
856 * of special entries. These are treated as place holders when we fill
857 * out the ACL. This routine does not add or remove special entries, it
858 * simply unites each special entry with its associated set of permissions.
859 */
860STATIC void
861xfs_acl_sync_mode(
862 mode_t mode,
863 xfs_acl_t *acl)
864{
865 int i, nomask = 1;
866 xfs_acl_entry_t *ap;
867 xfs_acl_entry_t *gap = NULL;
868
869 /*
870 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
871 * be set instead of the GROUP entry, if there is a MASK.
872 */
873 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
874 switch (ap->ae_tag) {
875 case ACL_USER_OBJ:
876 ap->ae_perm = (mode >> 6) & 0x7;
877 break;
878 case ACL_GROUP_OBJ:
879 gap = ap;
880 break;
881 case ACL_MASK:
882 nomask = 0;
883 ap->ae_perm = (mode >> 3) & 0x7;
884 break;
885 case ACL_OTHER:
886 ap->ae_perm = mode & 0x7;
887 break;
888 default:
889 break;
890 }
891 }
892 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
893 if (gap && nomask)
894 gap->ae_perm = (mode >> 3) & 0x7;
895}
896
897/*
898 * When inheriting an Access ACL from a directory Default ACL,
899 * the ACL bits are set to the intersection of the ACL default
900 * permission bits and the file permission bits in mode. If there
901 * are no permission bits on the file then we must not give them
902 * the ACL. This is what what makes umask() work with ACLs.
903 */
904STATIC void
905xfs_acl_filter_mode(
906 mode_t mode,
907 xfs_acl_t *acl)
908{
909 int i, nomask = 1;
910 xfs_acl_entry_t *ap;
911 xfs_acl_entry_t *gap = NULL;
912
913 /*
914 * Set ACL entries. POSIX1003.1eD16 requires that the MASK
915 * be merged with GROUP entry, if there is a MASK.
916 */
917 for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
918 switch (ap->ae_tag) {
919 case ACL_USER_OBJ:
920 ap->ae_perm &= (mode >> 6) & 0x7;
921 break;
922 case ACL_GROUP_OBJ:
923 gap = ap;
924 break;
925 case ACL_MASK:
926 nomask = 0;
927 ap->ae_perm &= (mode >> 3) & 0x7;
928 break;
929 case ACL_OTHER:
930 ap->ae_perm &= mode & 0x7;
931 break;
932 default:
933 break;
934 }
935 }
936 /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
937 if (gap && nomask)
938 gap->ae_perm &= (mode >> 3) & 0x7;
939}