ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / xfs / xfs_acl.c
1 /*
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  */
32
33 #include "xfs.h"
34
35 #include "xfs_inum.h"
36 #include "xfs_dir.h"
37 #include "xfs_dir2.h"
38 #include "xfs_alloc_btree.h"
39 #include "xfs_bmap_btree.h"
40 #include "xfs_ialloc_btree.h"
41 #include "xfs_btree.h"
42 #include "xfs_attr_sf.h"
43 #include "xfs_dir_sf.h"
44 #include "xfs_dir2_sf.h"
45 #include "xfs_dinode.h"
46 #include "xfs_inode.h"
47 #include "xfs_acl.h"
48 #include "xfs_mac.h"
49 #include "xfs_attr.h"
50
51 #include <linux/posix_acl_xattr.h>
52
53 STATIC int      xfs_acl_setmode(vnode_t *, xfs_acl_t *, int *);
54 STATIC void     xfs_acl_filter_mode(mode_t, xfs_acl_t *);
55 STATIC void     xfs_acl_get_endian(xfs_acl_t *);
56 STATIC int      xfs_acl_access(uid_t, gid_t, xfs_acl_t *, mode_t, cred_t *);
57 STATIC int      xfs_acl_invalid(xfs_acl_t *);
58 STATIC void     xfs_acl_sync_mode(mode_t, xfs_acl_t *);
59 STATIC void     xfs_acl_get_attr(vnode_t *, xfs_acl_t *, int, int, int *);
60 STATIC void     xfs_acl_set_attr(vnode_t *, xfs_acl_t *, int, int *);
61 STATIC int      xfs_acl_allow_set(vnode_t *, int);
62
63 kmem_zone_t *xfs_acl_zone;
64
65
66 /*
67  * Test for existence of access ACL attribute as efficiently as possible.
68  */
69 int
70 xfs_acl_vhasacl_access(
71         vnode_t         *vp)
72 {
73         int             error;
74
75         xfs_acl_get_attr(vp, NULL, _ACL_TYPE_ACCESS, ATTR_KERNOVAL, &error);
76         return (error == 0);
77 }
78
79 /*
80  * Test for existence of default ACL attribute as efficiently as possible.
81  */
82 int
83 xfs_acl_vhasacl_default(
84         vnode_t         *vp)
85 {
86         int             error;
87
88         if (vp->v_type != VDIR)
89                 return 0;
90         xfs_acl_get_attr(vp, NULL, _ACL_TYPE_DEFAULT, ATTR_KERNOVAL, &error);
91         return (error == 0);
92 }
93
94 /*
95  * Convert from extended attribute representation to in-memory for XFS.
96  */
97 STATIC int
98 posix_acl_xattr_to_xfs(
99         posix_acl_xattr_header  *src,
100         size_t                  size,
101         xfs_acl_t               *dest)
102 {
103         posix_acl_xattr_entry   *src_entry;
104         xfs_acl_entry_t         *dest_entry;
105         int                     n;
106
107         if (!src || !dest)
108                 return EINVAL;
109
110         if (size < sizeof(posix_acl_xattr_header))
111                 return EINVAL;
112
113         if (src->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
114                 return EOPNOTSUPP;
115
116         memset(dest, 0, sizeof(xfs_acl_t));
117         dest->acl_cnt = posix_acl_xattr_count(size);
118         if (dest->acl_cnt < 0 || dest->acl_cnt > XFS_ACL_MAX_ENTRIES)
119                 return EINVAL;
120
121         /*
122          * acl_set_file(3) may request that we set default ACLs with
123          * zero length -- defend (gracefully) against that here.
124          */
125         if (!dest->acl_cnt)
126                 return 0;
127
128         src_entry = (posix_acl_xattr_entry *)((char *)src + sizeof(*src));
129         dest_entry = &dest->acl_entry[0];
130
131         for (n = 0; n < dest->acl_cnt; n++, src_entry++, dest_entry++) {
132                 dest_entry->ae_perm = le16_to_cpu(src_entry->e_perm);
133                 if (_ACL_PERM_INVALID(dest_entry->ae_perm))
134                         return EINVAL;
135                 dest_entry->ae_tag  = le16_to_cpu(src_entry->e_tag);
136                 switch(dest_entry->ae_tag) {
137                 case ACL_USER:
138                 case ACL_GROUP:
139                         dest_entry->ae_id = le32_to_cpu(src_entry->e_id);
140                         break;
141                 case ACL_USER_OBJ:
142                 case ACL_GROUP_OBJ:
143                 case ACL_MASK:
144                 case ACL_OTHER:
145                         dest_entry->ae_id = ACL_UNDEFINED_ID;
146                         break;
147                 default:
148                         return EINVAL;
149                 }
150         }
151         if (xfs_acl_invalid(dest))
152                 return EINVAL;
153
154         return 0;
155 }
156
157 /*
158  * Comparison function called from qsort().
159  * Primary key is ae_tag, secondary key is ae_id.
160  */
161 STATIC int
162 xfs_acl_entry_compare(
163         const void      *va,
164         const void      *vb)
165 {
166         xfs_acl_entry_t *a = (xfs_acl_entry_t *)va,
167                         *b = (xfs_acl_entry_t *)vb;
168
169         if (a->ae_tag == b->ae_tag)
170                 return (a->ae_id - b->ae_id);
171         return (a->ae_tag - b->ae_tag);
172 }
173
174 /*
175  * Convert from in-memory XFS to extended attribute representation.
176  */
177 STATIC int
178 posix_acl_xfs_to_xattr(
179         xfs_acl_t               *src,
180         posix_acl_xattr_header  *dest,
181         size_t                  size)
182 {
183         int                     n;
184         size_t                  new_size = posix_acl_xattr_size(src->acl_cnt);
185         posix_acl_xattr_entry   *dest_entry;
186         xfs_acl_entry_t         *src_entry;
187
188         if (size < new_size)
189                 return -ERANGE;
190
191         /* Need to sort src XFS ACL by <ae_tag,ae_id> */
192         qsort(src->acl_entry, src->acl_cnt, sizeof(src->acl_entry[0]),
193                 xfs_acl_entry_compare);
194
195         dest->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
196         dest_entry = &dest->a_entries[0];
197         src_entry = &src->acl_entry[0];
198         for (n = 0; n < src->acl_cnt; n++, dest_entry++, src_entry++) {
199                 dest_entry->e_perm = cpu_to_le16(src_entry->ae_perm);
200                 if (_ACL_PERM_INVALID(src_entry->ae_perm))
201                         return -EINVAL;
202                 dest_entry->e_tag  = cpu_to_le16(src_entry->ae_tag);
203                 switch (src_entry->ae_tag) {
204                 case ACL_USER:
205                 case ACL_GROUP:
206                         dest_entry->e_id = cpu_to_le32(src_entry->ae_id);
207                                 break;
208                 case ACL_USER_OBJ:
209                 case ACL_GROUP_OBJ:
210                 case ACL_MASK:
211                 case ACL_OTHER:
212                         dest_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
213                         break;
214                 default:
215                         return -EINVAL;
216                 }
217         }
218         return new_size;
219 }
220
221 int
222 xfs_acl_vget(
223         vnode_t         *vp,
224         void            *acl,
225         size_t          size,
226         int             kind)
227 {
228         int                     error;
229         xfs_acl_t               *xfs_acl = NULL;
230         posix_acl_xattr_header  *ext_acl = acl;
231         int                     flags = 0;
232
233         VN_HOLD(vp);
234         if ((error = _MAC_VACCESS(vp, NULL, VREAD)))
235                 goto out;
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         }
267 out:
268         VN_RELE(vp);
269         if(xfs_acl)
270                 _ACL_FREE(xfs_acl);
271         return -error;
272 }
273
274 int
275 xfs_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
294 int
295 xfs_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
343
344 out:
345         VN_RELE(vp);
346         _ACL_FREE(xfs_acl);
347         return -error;
348 }
349
350 int
351 xfs_acl_iaccess(
352         xfs_inode_t     *ip,
353         mode_t          mode,
354         cred_t          *cr)
355 {
356         xfs_acl_t       *acl;
357         int             error;
358
359         if (!(_ACL_ALLOC(acl)))
360                 return -1;
361
362         /* If the file has no ACL return -1. */
363         if (xfs_attr_fetch(ip, SGI_ACL_FILE, (char *)acl, sizeof(xfs_acl_t))) {
364                 _ACL_FREE(acl);
365                 return -1;
366         }
367         xfs_acl_get_endian(acl);
368
369         /* If the file has an empty ACL return -1. */
370         if (acl->acl_cnt == XFS_ACL_NOT_PRESENT) {
371                 _ACL_FREE(acl);
372                 return -1;
373         }
374
375         /* Synchronize ACL with mode bits */
376         xfs_acl_sync_mode(ip->i_d.di_mode, acl);
377
378         error = xfs_acl_access(ip->i_d.di_uid, ip->i_d.di_gid, acl, mode, cr);
379         _ACL_FREE(acl);
380         return error;
381 }
382
383 STATIC int
384 xfs_acl_allow_set(
385         vnode_t         *vp,
386         int             kind)
387 {
388         vattr_t         va;
389         int             error;
390
391         if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
392                 return EPERM;
393         if (kind == _ACL_TYPE_DEFAULT && vp->v_type != VDIR)
394                 return ENOTDIR;
395         if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
396                 return EROFS;
397         if ((error = _MAC_VACCESS(vp, NULL, VWRITE)))
398                 return error;
399         va.va_mask = XFS_AT_UID;
400         VOP_GETATTR(vp, &va, 0, NULL, error);
401         if (error)
402                 return error;
403         if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
404                 return EPERM;
405         return error;
406 }
407
408 /*
409  * Look for any effective exec access, to allow CAP_DAC_OVERRIDE for exec.
410  * Ignore checking for exec in USER_OBJ when there is no mask, because
411  * in this "minimal acl" case we don't have any actual acls, and we
412  * won't even be here.
413  */
414 STATIC int
415 xfs_acl_find_any_exec(
416         xfs_acl_t       *fap)
417 {
418         int             i;
419         int             masked_aces = 0;
420         int             mask = 0;
421
422         for (i = 0; i < fap->acl_cnt; i++) {
423                 if (fap->acl_entry[i].ae_perm & ACL_EXECUTE) {
424                         if (fap->acl_entry[i].ae_tag & (ACL_USER_OBJ|ACL_OTHER))
425                                 return 1;
426
427                         if (fap->acl_entry[i].ae_tag == ACL_MASK)
428                                 mask = fap->acl_entry[i].ae_perm;
429                         else
430                                 masked_aces |= fap->acl_entry[i].ae_perm;
431
432                         if ((mask & masked_aces) & ACL_EXECUTE)
433                                 return 1;
434                 }
435         }
436
437         return 0;
438 }
439
440 /*
441  * The access control process to determine the access permission:
442  *      if uid == file owner id, use the file owner bits.
443  *      if gid == file owner group id, use the file group bits.
444  *      scan ACL for a maching user or group, and use matched entry
445  *      permission. Use total permissions of all matching group entries,
446  *      until all acl entries are exhausted. The final permission produced
447  *      by matching acl entry or entries needs to be & with group permission.
448  *      if not owner, owning group, or matching entry in ACL, use file
449  *      other bits.  Don't allow CAP_DAC_OVERRIDE on exec access unless
450  *      there is some effective exec access somewhere.
451  */
452 STATIC int
453 xfs_acl_capability_check(
454         mode_t          mode,
455         cred_t          *cr,
456         xfs_acl_t       *fap)
457 {
458         if ((mode & ACL_READ) && !capable_cred(cr, CAP_DAC_READ_SEARCH))
459                 return EACCES;
460         if ((mode & ACL_WRITE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
461                 return EACCES;
462         if ((mode & ACL_EXECUTE) &&
463             (!capable_cred(cr, CAP_DAC_OVERRIDE) ||
464              !xfs_acl_find_any_exec(fap))) {
465                 return EACCES;
466         }
467
468         return 0;
469 }
470
471 /*
472  * Note: cr is only used here for the capability check if the ACL test fails.
473  *       It is not used to find out the credentials uid or groups etc, as was
474  *       done in IRIX. It is assumed that the uid and groups for the current
475  *       thread are taken from "current" instead of the cr parameter.
476  */
477 STATIC int
478 xfs_acl_access(
479         uid_t           fuid,
480         gid_t           fgid,
481         xfs_acl_t       *fap,
482         mode_t          md,
483         cred_t          *cr)
484 {
485         xfs_acl_entry_t matched;
486         int             i, allows;
487         int             maskallows = -1;        /* true, but not 1, either */
488         int             seen_userobj = 0;
489
490         matched.ae_tag = 0;     /* Invalid type */
491         md >>= 6;       /* Normalize the bits for comparison */
492
493         for (i = 0; i < fap->acl_cnt; i++) {
494                 /*
495                  * Break out if we've got a user_obj entry or
496                  * a user entry and the mask (and have processed USER_OBJ)
497                  */
498                 if (matched.ae_tag == ACL_USER_OBJ)
499                         break;
500                 if (matched.ae_tag == ACL_USER) {
501                         if (maskallows != -1 && seen_userobj)
502                                 break;
503                         if (fap->acl_entry[i].ae_tag != ACL_MASK &&
504                             fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
505                                 continue;
506                 }
507                 /* True if this entry allows the requested access */
508                 allows = ((fap->acl_entry[i].ae_perm & md) == md);
509
510                 switch (fap->acl_entry[i].ae_tag) {
511                 case ACL_USER_OBJ:
512                         seen_userobj = 1;
513                         if (fuid != current->fsuid)
514                                 continue;
515                         matched.ae_tag = ACL_USER_OBJ;
516                         matched.ae_perm = allows;
517                         break;
518                 case ACL_USER:
519                         if (fap->acl_entry[i].ae_id != current->fsuid)
520                                 continue;
521                         matched.ae_tag = ACL_USER;
522                         matched.ae_perm = allows;
523                         break;
524                 case ACL_GROUP_OBJ:
525                         if ((matched.ae_tag == ACL_GROUP_OBJ ||
526                             matched.ae_tag == ACL_GROUP) && !allows)
527                                 continue;
528                         if (!in_group_p(fgid))
529                                 continue;
530                         matched.ae_tag = ACL_GROUP_OBJ;
531                         matched.ae_perm = allows;
532                         break;
533                 case ACL_GROUP:
534                         if ((matched.ae_tag == ACL_GROUP_OBJ ||
535                             matched.ae_tag == ACL_GROUP) && !allows)
536                                 continue;
537                         if (!in_group_p(fap->acl_entry[i].ae_id))
538                                 continue;
539                         matched.ae_tag = ACL_GROUP;
540                         matched.ae_perm = allows;
541                         break;
542                 case ACL_MASK:
543                         maskallows = allows;
544                         break;
545                 case ACL_OTHER:
546                         if (matched.ae_tag != 0)
547                                 continue;
548                         matched.ae_tag = ACL_OTHER;
549                         matched.ae_perm = allows;
550                         break;
551                 }
552         }
553         /*
554          * First possibility is that no matched entry allows access.
555          * The capability to override DAC may exist, so check for it.
556          */
557         switch (matched.ae_tag) {
558         case ACL_OTHER:
559         case ACL_USER_OBJ:
560                 if (matched.ae_perm)
561                         return 0;
562                 break;
563         case ACL_USER:
564         case ACL_GROUP_OBJ:
565         case ACL_GROUP:
566                 if (maskallows && matched.ae_perm)
567                         return 0;
568                 break;
569         case 0:
570                 break;
571         }
572
573         return xfs_acl_capability_check(md, cr, fap);
574 }
575
576 /*
577  * ACL validity checker.
578  *   This acl validation routine checks each ACL entry read in makes sense.
579  */
580 STATIC int
581 xfs_acl_invalid(
582         xfs_acl_t       *aclp)
583 {
584         xfs_acl_entry_t *entry, *e;
585         int             user = 0, group = 0, other = 0, mask = 0;
586         int             mask_required = 0;
587         int             i, j;
588
589         if (!aclp)
590                 goto acl_invalid;
591
592         if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
593                 goto acl_invalid;
594
595         for (i = 0; i < aclp->acl_cnt; i++) {
596                 entry = &aclp->acl_entry[i];
597                 switch (entry->ae_tag) {
598                 case ACL_USER_OBJ:
599                         if (user++)
600                                 goto acl_invalid;
601                         break;
602                 case ACL_GROUP_OBJ:
603                         if (group++)
604                                 goto acl_invalid;
605                         break;
606                 case ACL_OTHER:
607                         if (other++)
608                                 goto acl_invalid;
609                         break;
610                 case ACL_USER:
611                 case ACL_GROUP:
612                         for (j = i + 1; j < aclp->acl_cnt; j++) {
613                                 e = &aclp->acl_entry[j];
614                                 if (e->ae_id == entry->ae_id &&
615                                     e->ae_tag == entry->ae_tag)
616                                         goto acl_invalid;
617                         }
618                         mask_required++;
619                         break;
620                 case ACL_MASK:
621                         if (mask++)
622                                 goto acl_invalid;
623                         break;
624                 default:
625                         goto acl_invalid;
626                 }
627         }
628         if (!user || !group || !other || (mask_required && !mask))
629                 goto acl_invalid;
630         else
631                 return 0;
632 acl_invalid:
633         return EINVAL;
634 }
635
636 /*
637  * Do ACL endian conversion.
638  */
639 STATIC void
640 xfs_acl_get_endian(
641         xfs_acl_t       *aclp)
642 {
643         xfs_acl_entry_t *ace, *end;
644
645         INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
646         end = &aclp->acl_entry[0]+aclp->acl_cnt;
647         for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
648                 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
649                 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
650                 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
651         }
652 }
653
654 /*
655  * Get the ACL from the EA and do endian conversion.
656  */
657 STATIC void
658 xfs_acl_get_attr(
659         vnode_t         *vp,
660         xfs_acl_t       *aclp,
661         int             kind,
662         int             flags,
663         int             *error)
664 {
665         int             len = sizeof(xfs_acl_t);
666
667         ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
668         flags |= ATTR_ROOT;
669         VOP_ATTR_GET(vp,
670                 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE : SGI_ACL_DEFAULT,
671                 (char *)aclp, &len, flags, sys_cred, *error);
672         if (*error || (flags & ATTR_KERNOVAL))
673                 return;
674         xfs_acl_get_endian(aclp);
675 }
676
677 /*
678  * Set the EA with the ACL and do endian conversion.
679  */
680 STATIC void
681 xfs_acl_set_attr(
682         vnode_t         *vp,
683         xfs_acl_t       *aclp,
684         int             kind,
685         int             *error)
686 {
687         xfs_acl_entry_t *ace, *newace, *end;
688         xfs_acl_t       *newacl;
689         int             len;
690
691         if (!(_ACL_ALLOC(newacl))) {
692                 *error = ENOMEM;
693                 return;
694         }
695
696         len = sizeof(xfs_acl_t) -
697               (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
698         end = &aclp->acl_entry[0]+aclp->acl_cnt;
699         for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
700              ace < end;
701              ace++, newace++) {
702                 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
703                 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
704                 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
705         }
706         INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
707         VOP_ATTR_SET(vp,
708                 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE: SGI_ACL_DEFAULT,
709                 (char *)newacl, len, ATTR_ROOT, sys_cred, *error);
710         _ACL_FREE(newacl);
711 }
712
713 int
714 xfs_acl_vtoacl(
715         vnode_t         *vp,
716         xfs_acl_t       *access_acl,
717         xfs_acl_t       *default_acl)
718 {
719         vattr_t         va;
720         int             error = 0;
721
722         if (access_acl) {
723                 /*
724                  * Get the Access ACL and the mode.  If either cannot
725                  * be obtained for some reason, invalidate the access ACL.
726                  */
727                 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
728                 if (!error) {
729                         /* Got the ACL, need the mode... */
730                         va.va_mask = XFS_AT_MODE;
731                         VOP_GETATTR(vp, &va, 0, sys_cred, error);
732                 }
733
734                 if (error)
735                         access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
736                 else /* We have a good ACL and the file mode, synchronize. */
737                         xfs_acl_sync_mode(va.va_mode, access_acl);
738         }
739
740         if (default_acl) {
741                 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
742                 if (error)
743                         default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
744         }
745         return error;
746 }
747
748 /*
749  * This function retrieves the parent directory's acl, processes it
750  * and lets the child inherit the acl(s) that it should.
751  */
752 int
753 xfs_acl_inherit(
754         vnode_t         *vp,
755         vattr_t         *vap,
756         xfs_acl_t       *pdaclp)
757 {
758         xfs_acl_t       *cacl;
759         int             error = 0;
760         int             basicperms = 0;
761
762         /*
763          * If the parent does not have a default ACL, or it's an
764          * invalid ACL, we're done.
765          */
766         if (!vp)
767                 return 0;
768         if (!pdaclp || xfs_acl_invalid(pdaclp))
769                 return 0;
770
771         /*
772          * Copy the default ACL of the containing directory to
773          * the access ACL of the new file and use the mode that
774          * was passed in to set up the correct initial values for
775          * the u::,g::[m::], and o:: entries.  This is what makes
776          * umask() "work" with ACL's.
777          */
778
779         if (!(_ACL_ALLOC(cacl)))
780                 return ENOMEM;
781
782         memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
783         xfs_acl_filter_mode(vap->va_mode, cacl);
784         xfs_acl_setmode(vp, cacl, &basicperms);
785
786         /*
787          * Set the Default and Access ACL on the file.  The mode is already
788          * set on the file, so we don't need to worry about that.
789          *
790          * If the new file is a directory, its default ACL is a copy of
791          * the containing directory's default ACL.
792          */
793         if (vp->v_type == VDIR)
794                 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
795         if (!error && !basicperms)
796                 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
797         _ACL_FREE(cacl);
798         return error;
799 }
800
801 /*
802  * Set up the correct mode on the file based on the supplied ACL.  This
803  * makes sure that the mode on the file reflects the state of the
804  * u::,g::[m::], and o:: entries in the ACL.  Since the mode is where
805  * the ACL is going to get the permissions for these entries, we must
806  * synchronize the mode whenever we set the ACL on a file.
807  */
808 STATIC int
809 xfs_acl_setmode(
810         vnode_t         *vp,
811         xfs_acl_t       *acl,
812         int             *basicperms)
813 {
814         vattr_t         va;
815         xfs_acl_entry_t *ap;
816         xfs_acl_entry_t *gap = NULL;
817         int             i, error, nomask = 1;
818
819         *basicperms = 1;
820
821         if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
822                 return 0;
823
824         /*
825          * Copy the u::, g::, o::, and m:: bits from the ACL into the
826          * mode.  The m:: bits take precedence over the g:: bits.
827          */
828         va.va_mask = XFS_AT_MODE;
829         VOP_GETATTR(vp, &va, 0, sys_cred, error);
830         if (error)
831                 return error;
832
833         va.va_mask = XFS_AT_MODE;
834         va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
835         ap = acl->acl_entry;
836         for (i = 0; i < acl->acl_cnt; ++i) {
837                 switch (ap->ae_tag) {
838                 case ACL_USER_OBJ:
839                         va.va_mode |= ap->ae_perm << 6;
840                         break;
841                 case ACL_GROUP_OBJ:
842                         gap = ap;
843                         break;
844                 case ACL_MASK:  /* more than just standard modes */
845                         nomask = 0;
846                         va.va_mode |= ap->ae_perm << 3;
847                         *basicperms = 0;
848                         break;
849                 case ACL_OTHER:
850                         va.va_mode |= ap->ae_perm;
851                         break;
852                 default:        /* more than just standard modes */
853                         *basicperms = 0;
854                         break;
855                 }
856                 ap++;
857         }
858
859         /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
860         if (gap && nomask)
861                 va.va_mode |= gap->ae_perm << 3;
862
863         VOP_SETATTR(vp, &va, 0, sys_cred, error);
864         return error;
865 }
866
867 /*
868  * The permissions for the special ACL entries (u::, g::[m::], o::) are
869  * actually stored in the file mode (if there is both a group and a mask,
870  * the group is stored in the ACL entry and the mask is stored on the file).
871  * This allows the mode to remain automatically in sync with the ACL without
872  * the need for a call-back to the ACL system at every point where the mode
873  * could change.  This function takes the permissions from the specified mode
874  * and places it in the supplied ACL.
875  *
876  * This implementation draws its validity from the fact that, when the ACL
877  * was assigned, the mode was copied from the ACL.
878  * If the mode did not change, therefore, the mode remains exactly what was
879  * taken from the special ACL entries at assignment.
880  * If a subsequent chmod() was done, the POSIX spec says that the change in
881  * mode must cause an update to the ACL seen at user level and used for
882  * access checks.  Before and after a mode change, therefore, the file mode
883  * most accurately reflects what the special ACL entries should permit/deny.
884  *
885  * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
886  *         the existing mode bits will override whatever is in the
887  *         ACL. Similarly, if there is a pre-existing ACL that was
888  *         never in sync with its mode (owing to a bug in 6.5 and
889  *         before), it will now magically (or mystically) be
890  *         synchronized.  This could cause slight astonishment, but
891  *         it is better than inconsistent permissions.
892  *
893  * The supplied ACL is a template that may contain any combination
894  * of special entries.  These are treated as place holders when we fill
895  * out the ACL.  This routine does not add or remove special entries, it
896  * simply unites each special entry with its associated set of permissions.
897  */
898 STATIC void
899 xfs_acl_sync_mode(
900         mode_t          mode,
901         xfs_acl_t       *acl)
902 {
903         int             i, nomask = 1;
904         xfs_acl_entry_t *ap;
905         xfs_acl_entry_t *gap = NULL;
906
907         /*
908          * Set ACL entries. POSIX1003.1eD16 requires that the MASK
909          * be set instead of the GROUP entry, if there is a MASK.
910          */
911         for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
912                 switch (ap->ae_tag) {
913                 case ACL_USER_OBJ:
914                         ap->ae_perm = (mode >> 6) & 0x7;
915                         break;
916                 case ACL_GROUP_OBJ:
917                         gap = ap;
918                         break;
919                 case ACL_MASK:
920                         nomask = 0;
921                         ap->ae_perm = (mode >> 3) & 0x7;
922                         break;
923                 case ACL_OTHER:
924                         ap->ae_perm = mode & 0x7;
925                         break;
926                 default:
927                         break;
928                 }
929         }
930         /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
931         if (gap && nomask)
932                 gap->ae_perm = (mode >> 3) & 0x7;
933 }
934
935 /*
936  * When inheriting an Access ACL from a directory Default ACL,
937  * the ACL bits are set to the intersection of the ACL default
938  * permission bits and the file permission bits in mode. If there
939  * are no permission bits on the file then we must not give them
940  * the ACL. This is what what makes umask() work with ACLs.
941  */
942 STATIC void
943 xfs_acl_filter_mode(
944         mode_t          mode,
945         xfs_acl_t       *acl)
946 {
947         int             i, nomask = 1;
948         xfs_acl_entry_t *ap;
949         xfs_acl_entry_t *gap = NULL;
950
951         /*
952          * Set ACL entries. POSIX1003.1eD16 requires that the MASK
953          * be merged with GROUP entry, if there is a MASK.
954          */
955         for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
956                 switch (ap->ae_tag) {
957                 case ACL_USER_OBJ:
958                         ap->ae_perm &= (mode >> 6) & 0x7;
959                         break;
960                 case ACL_GROUP_OBJ:
961                         gap = ap;
962                         break;
963                 case ACL_MASK:
964                         nomask = 0;
965                         ap->ae_perm &= (mode >> 3) & 0x7;
966                         break;
967                 case ACL_OTHER:
968                         ap->ae_perm &= mode & 0x7;
969                         break;
970                 default:
971                         break;
972                 }
973         }
974         /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
975         if (gap && nomask)
976                 gap->ae_perm &= (mode >> 3) & 0x7;
977 }