patch-2_6_7-vs1_9_1_12
[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 out:
344         VN_RELE(vp);
345         _ACL_FREE(xfs_acl);
346         return -error;
347 }
348
349 int
350 xfs_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
384 STATIC int
385 xfs_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;
394         if (kind == _ACL_TYPE_DEFAULT && vp->v_type != VDIR)
395                 return ENOTDIR;
396         if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
397                 return EROFS;
398         if ((error = _MAC_VACCESS(vp, NULL, VWRITE)))
399                 return error;
400         va.va_mask = XFS_AT_UID;
401         VOP_GETATTR(vp, &va, 0, NULL, error);
402         if (error)
403                 return error;
404         if (va.va_uid != current->fsuid && !capable(CAP_FOWNER))
405                 return EPERM;
406         return error;
407 }
408
409 /*
410  * Look for any effective exec access, to allow CAP_DAC_OVERRIDE for exec.
411  * Ignore checking for exec in USER_OBJ when there is no mask, because
412  * in this "minimal acl" case we don't have any actual acls, and we
413  * won't even be here.
414  */
415 STATIC int
416 xfs_acl_find_any_exec(
417         xfs_acl_t       *fap)
418 {
419         int             i;
420         int             masked_aces = 0;
421         int             mask = 0;
422
423         for (i = 0; i < fap->acl_cnt; i++) {
424                 if (fap->acl_entry[i].ae_perm & ACL_EXECUTE) {
425                         if (fap->acl_entry[i].ae_tag & (ACL_USER_OBJ|ACL_OTHER))
426                                 return 1;
427
428                         if (fap->acl_entry[i].ae_tag == ACL_MASK)
429                                 mask = fap->acl_entry[i].ae_perm;
430                         else
431                                 masked_aces |= fap->acl_entry[i].ae_perm;
432
433                         if ((mask & masked_aces) & ACL_EXECUTE)
434                                 return 1;
435                 }
436         }
437
438         return 0;
439 }
440
441 /*
442  * The access control process to determine the access permission:
443  *      if uid == file owner id, use the file owner bits.
444  *      if gid == file owner group id, use the file group bits.
445  *      scan ACL for a maching user or group, and use matched entry
446  *      permission. Use total permissions of all matching group entries,
447  *      until all acl entries are exhausted. The final permission produced
448  *      by matching acl entry or entries needs to be & with group permission.
449  *      if not owner, owning group, or matching entry in ACL, use file
450  *      other bits.  Don't allow CAP_DAC_OVERRIDE on exec access unless
451  *      there is some effective exec access somewhere.
452  */
453 STATIC int
454 xfs_acl_capability_check(
455         mode_t          mode,
456         cred_t          *cr,
457         xfs_acl_t       *fap)
458 {
459         if ((mode & ACL_READ) && !capable_cred(cr, CAP_DAC_READ_SEARCH))
460                 return EACCES;
461         if ((mode & ACL_WRITE) && !capable_cred(cr, CAP_DAC_OVERRIDE))
462                 return EACCES;
463         if ((mode & ACL_EXECUTE) &&
464             (!capable_cred(cr, CAP_DAC_OVERRIDE) ||
465              !xfs_acl_find_any_exec(fap))) {
466                 return EACCES;
467         }
468
469         return 0;
470 }
471
472 /*
473  * Note: cr is only used here for the capability check if the ACL test fails.
474  *       It is not used to find out the credentials uid or groups etc, as was
475  *       done in IRIX. It is assumed that the uid and groups for the current
476  *       thread are taken from "current" instead of the cr parameter.
477  */
478 STATIC int
479 xfs_acl_access(
480         uid_t           fuid,
481         gid_t           fgid,
482         xfs_acl_t       *fap,
483         mode_t          md,
484         cred_t          *cr)
485 {
486         xfs_acl_entry_t matched;
487         int             i, allows;
488         int             maskallows = -1;        /* true, but not 1, either */
489         int             seen_userobj = 0;
490
491         matched.ae_tag = 0;     /* Invalid type */
492         md >>= 6;       /* Normalize the bits for comparison */
493
494         for (i = 0; i < fap->acl_cnt; i++) {
495                 /*
496                  * Break out if we've got a user_obj entry or
497                  * a user entry and the mask (and have processed USER_OBJ)
498                  */
499                 if (matched.ae_tag == ACL_USER_OBJ)
500                         break;
501                 if (matched.ae_tag == ACL_USER) {
502                         if (maskallows != -1 && seen_userobj)
503                                 break;
504                         if (fap->acl_entry[i].ae_tag != ACL_MASK &&
505                             fap->acl_entry[i].ae_tag != ACL_USER_OBJ)
506                                 continue;
507                 }
508                 /* True if this entry allows the requested access */
509                 allows = ((fap->acl_entry[i].ae_perm & md) == md);
510
511                 switch (fap->acl_entry[i].ae_tag) {
512                 case ACL_USER_OBJ:
513                         seen_userobj = 1;
514                         if (fuid != current->fsuid)
515                                 continue;
516                         matched.ae_tag = ACL_USER_OBJ;
517                         matched.ae_perm = allows;
518                         break;
519                 case ACL_USER:
520                         if (fap->acl_entry[i].ae_id != current->fsuid)
521                                 continue;
522                         matched.ae_tag = ACL_USER;
523                         matched.ae_perm = allows;
524                         break;
525                 case ACL_GROUP_OBJ:
526                         if ((matched.ae_tag == ACL_GROUP_OBJ ||
527                             matched.ae_tag == ACL_GROUP) && !allows)
528                                 continue;
529                         if (!in_group_p(fgid))
530                                 continue;
531                         matched.ae_tag = ACL_GROUP_OBJ;
532                         matched.ae_perm = allows;
533                         break;
534                 case ACL_GROUP:
535                         if ((matched.ae_tag == ACL_GROUP_OBJ ||
536                             matched.ae_tag == ACL_GROUP) && !allows)
537                                 continue;
538                         if (!in_group_p(fap->acl_entry[i].ae_id))
539                                 continue;
540                         matched.ae_tag = ACL_GROUP;
541                         matched.ae_perm = allows;
542                         break;
543                 case ACL_MASK:
544                         maskallows = allows;
545                         break;
546                 case ACL_OTHER:
547                         if (matched.ae_tag != 0)
548                                 continue;
549                         matched.ae_tag = ACL_OTHER;
550                         matched.ae_perm = allows;
551                         break;
552                 }
553         }
554         /*
555          * First possibility is that no matched entry allows access.
556          * The capability to override DAC may exist, so check for it.
557          */
558         switch (matched.ae_tag) {
559         case ACL_OTHER:
560         case ACL_USER_OBJ:
561                 if (matched.ae_perm)
562                         return 0;
563                 break;
564         case ACL_USER:
565         case ACL_GROUP_OBJ:
566         case ACL_GROUP:
567                 if (maskallows && matched.ae_perm)
568                         return 0;
569                 break;
570         case 0:
571                 break;
572         }
573
574         return xfs_acl_capability_check(md, cr, fap);
575 }
576
577 /*
578  * ACL validity checker.
579  *   This acl validation routine checks each ACL entry read in makes sense.
580  */
581 STATIC int
582 xfs_acl_invalid(
583         xfs_acl_t       *aclp)
584 {
585         xfs_acl_entry_t *entry, *e;
586         int             user = 0, group = 0, other = 0, mask = 0;
587         int             mask_required = 0;
588         int             i, j;
589
590         if (!aclp)
591                 goto acl_invalid;
592
593         if (aclp->acl_cnt > XFS_ACL_MAX_ENTRIES)
594                 goto acl_invalid;
595
596         for (i = 0; i < aclp->acl_cnt; i++) {
597                 entry = &aclp->acl_entry[i];
598                 switch (entry->ae_tag) {
599                 case ACL_USER_OBJ:
600                         if (user++)
601                                 goto acl_invalid;
602                         break;
603                 case ACL_GROUP_OBJ:
604                         if (group++)
605                                 goto acl_invalid;
606                         break;
607                 case ACL_OTHER:
608                         if (other++)
609                                 goto acl_invalid;
610                         break;
611                 case ACL_USER:
612                 case ACL_GROUP:
613                         for (j = i + 1; j < aclp->acl_cnt; j++) {
614                                 e = &aclp->acl_entry[j];
615                                 if (e->ae_id == entry->ae_id &&
616                                     e->ae_tag == entry->ae_tag)
617                                         goto acl_invalid;
618                         }
619                         mask_required++;
620                         break;
621                 case ACL_MASK:
622                         if (mask++)
623                                 goto acl_invalid;
624                         break;
625                 default:
626                         goto acl_invalid;
627                 }
628         }
629         if (!user || !group || !other || (mask_required && !mask))
630                 goto acl_invalid;
631         else
632                 return 0;
633 acl_invalid:
634         return EINVAL;
635 }
636
637 /*
638  * Do ACL endian conversion.
639  */
640 STATIC void
641 xfs_acl_get_endian(
642         xfs_acl_t       *aclp)
643 {
644         xfs_acl_entry_t *ace, *end;
645
646         INT_SET(aclp->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
647         end = &aclp->acl_entry[0]+aclp->acl_cnt;
648         for (ace = &aclp->acl_entry[0]; ace < end; ace++) {
649                 INT_SET(ace->ae_tag, ARCH_CONVERT, ace->ae_tag);
650                 INT_SET(ace->ae_id, ARCH_CONVERT, ace->ae_id);
651                 INT_SET(ace->ae_perm, ARCH_CONVERT, ace->ae_perm);
652         }
653 }
654
655 /*
656  * Get the ACL from the EA and do endian conversion.
657  */
658 STATIC void
659 xfs_acl_get_attr(
660         vnode_t         *vp,
661         xfs_acl_t       *aclp,
662         int             kind,
663         int             flags,
664         int             *error)
665 {
666         int             len = sizeof(xfs_acl_t);
667
668         ASSERT((flags & ATTR_KERNOVAL) ? (aclp == NULL) : 1);
669         flags |= ATTR_ROOT;
670         VOP_ATTR_GET(vp,
671                 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE : SGI_ACL_DEFAULT,
672                 (char *)aclp, &len, flags, sys_cred, *error);
673         if (*error || (flags & ATTR_KERNOVAL))
674                 return;
675         xfs_acl_get_endian(aclp);
676 }
677
678 /*
679  * Set the EA with the ACL and do endian conversion.
680  */
681 STATIC void
682 xfs_acl_set_attr(
683         vnode_t         *vp,
684         xfs_acl_t       *aclp,
685         int             kind,
686         int             *error)
687 {
688         xfs_acl_entry_t *ace, *newace, *end;
689         xfs_acl_t       *newacl;
690         int             len;
691
692         if (!(_ACL_ALLOC(newacl))) {
693                 *error = ENOMEM;
694                 return;
695         }
696
697         len = sizeof(xfs_acl_t) -
698               (sizeof(xfs_acl_entry_t) * (XFS_ACL_MAX_ENTRIES - aclp->acl_cnt));
699         end = &aclp->acl_entry[0]+aclp->acl_cnt;
700         for (ace = &aclp->acl_entry[0], newace = &newacl->acl_entry[0];
701              ace < end;
702              ace++, newace++) {
703                 INT_SET(newace->ae_tag, ARCH_CONVERT, ace->ae_tag);
704                 INT_SET(newace->ae_id, ARCH_CONVERT, ace->ae_id);
705                 INT_SET(newace->ae_perm, ARCH_CONVERT, ace->ae_perm);
706         }
707         INT_SET(newacl->acl_cnt, ARCH_CONVERT, aclp->acl_cnt);
708         VOP_ATTR_SET(vp,
709                 kind == _ACL_TYPE_ACCESS ? SGI_ACL_FILE: SGI_ACL_DEFAULT,
710                 (char *)newacl, len, ATTR_ROOT, sys_cred, *error);
711         _ACL_FREE(newacl);
712 }
713
714 int
715 xfs_acl_vtoacl(
716         vnode_t         *vp,
717         xfs_acl_t       *access_acl,
718         xfs_acl_t       *default_acl)
719 {
720         vattr_t         va;
721         int             error = 0;
722
723         if (access_acl) {
724                 /*
725                  * Get the Access ACL and the mode.  If either cannot
726                  * be obtained for some reason, invalidate the access ACL.
727                  */
728                 xfs_acl_get_attr(vp, access_acl, _ACL_TYPE_ACCESS, 0, &error);
729                 if (!error) {
730                         /* Got the ACL, need the mode... */
731                         va.va_mask = XFS_AT_MODE;
732                         VOP_GETATTR(vp, &va, 0, sys_cred, error);
733                 }
734
735                 if (error)
736                         access_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
737                 else /* We have a good ACL and the file mode, synchronize. */
738                         xfs_acl_sync_mode(va.va_mode, access_acl);
739         }
740
741         if (default_acl) {
742                 xfs_acl_get_attr(vp, default_acl, _ACL_TYPE_DEFAULT, 0, &error);
743                 if (error)
744                         default_acl->acl_cnt = XFS_ACL_NOT_PRESENT;
745         }
746         return error;
747 }
748
749 /*
750  * This function retrieves the parent directory's acl, processes it
751  * and lets the child inherit the acl(s) that it should.
752  */
753 int
754 xfs_acl_inherit(
755         vnode_t         *vp,
756         vattr_t         *vap,
757         xfs_acl_t       *pdaclp)
758 {
759         xfs_acl_t       *cacl;
760         int             error = 0;
761         int             basicperms = 0;
762
763         /*
764          * If the parent does not have a default ACL, or it's an
765          * invalid ACL, we're done.
766          */
767         if (!vp)
768                 return 0;
769         if (!pdaclp || xfs_acl_invalid(pdaclp))
770                 return 0;
771
772         /*
773          * Copy the default ACL of the containing directory to
774          * the access ACL of the new file and use the mode that
775          * was passed in to set up the correct initial values for
776          * the u::,g::[m::], and o:: entries.  This is what makes
777          * umask() "work" with ACL's.
778          */
779
780         if (!(_ACL_ALLOC(cacl)))
781                 return ENOMEM;
782
783         memcpy(cacl, pdaclp, sizeof(xfs_acl_t));
784         xfs_acl_filter_mode(vap->va_mode, cacl);
785         xfs_acl_setmode(vp, cacl, &basicperms);
786
787         /*
788          * Set the Default and Access ACL on the file.  The mode is already
789          * set on the file, so we don't need to worry about that.
790          *
791          * If the new file is a directory, its default ACL is a copy of
792          * the containing directory's default ACL.
793          */
794         if (vp->v_type == VDIR)
795                 xfs_acl_set_attr(vp, pdaclp, _ACL_TYPE_DEFAULT, &error);
796         if (!error && !basicperms)
797                 xfs_acl_set_attr(vp, cacl, _ACL_TYPE_ACCESS, &error);
798         _ACL_FREE(cacl);
799         return error;
800 }
801
802 /*
803  * Set up the correct mode on the file based on the supplied ACL.  This
804  * makes sure that the mode on the file reflects the state of the
805  * u::,g::[m::], and o:: entries in the ACL.  Since the mode is where
806  * the ACL is going to get the permissions for these entries, we must
807  * synchronize the mode whenever we set the ACL on a file.
808  */
809 STATIC int
810 xfs_acl_setmode(
811         vnode_t         *vp,
812         xfs_acl_t       *acl,
813         int             *basicperms)
814 {
815         vattr_t         va;
816         xfs_acl_entry_t *ap;
817         xfs_acl_entry_t *gap = NULL;
818         int             i, error, nomask = 1;
819
820         *basicperms = 1;
821
822         if (acl->acl_cnt == XFS_ACL_NOT_PRESENT)
823                 return 0;
824
825         /*
826          * Copy the u::, g::, o::, and m:: bits from the ACL into the
827          * mode.  The m:: bits take precedence over the g:: bits.
828          */
829         va.va_mask = XFS_AT_MODE;
830         VOP_GETATTR(vp, &va, 0, sys_cred, error);
831         if (error)
832                 return error;
833
834         va.va_mask = XFS_AT_MODE;
835         va.va_mode &= ~(S_IRWXU|S_IRWXG|S_IRWXO);
836         ap = acl->acl_entry;
837         for (i = 0; i < acl->acl_cnt; ++i) {
838                 switch (ap->ae_tag) {
839                 case ACL_USER_OBJ:
840                         va.va_mode |= ap->ae_perm << 6;
841                         break;
842                 case ACL_GROUP_OBJ:
843                         gap = ap;
844                         break;
845                 case ACL_MASK:  /* more than just standard modes */
846                         nomask = 0;
847                         va.va_mode |= ap->ae_perm << 3;
848                         *basicperms = 0;
849                         break;
850                 case ACL_OTHER:
851                         va.va_mode |= ap->ae_perm;
852                         break;
853                 default:        /* more than just standard modes */
854                         *basicperms = 0;
855                         break;
856                 }
857                 ap++;
858         }
859
860         /* Set the group bits from ACL_GROUP_OBJ if there's no ACL_MASK */
861         if (gap && nomask)
862                 va.va_mode |= gap->ae_perm << 3;
863
864         VOP_SETATTR(vp, &va, 0, sys_cred, error);
865         return error;
866 }
867
868 /*
869  * The permissions for the special ACL entries (u::, g::[m::], o::) are
870  * actually stored in the file mode (if there is both a group and a mask,
871  * the group is stored in the ACL entry and the mask is stored on the file).
872  * This allows the mode to remain automatically in sync with the ACL without
873  * the need for a call-back to the ACL system at every point where the mode
874  * could change.  This function takes the permissions from the specified mode
875  * and places it in the supplied ACL.
876  *
877  * This implementation draws its validity from the fact that, when the ACL
878  * was assigned, the mode was copied from the ACL.
879  * If the mode did not change, therefore, the mode remains exactly what was
880  * taken from the special ACL entries at assignment.
881  * If a subsequent chmod() was done, the POSIX spec says that the change in
882  * mode must cause an update to the ACL seen at user level and used for
883  * access checks.  Before and after a mode change, therefore, the file mode
884  * most accurately reflects what the special ACL entries should permit/deny.
885  *
886  * CAVEAT: If someone sets the SGI_ACL_FILE attribute directly,
887  *         the existing mode bits will override whatever is in the
888  *         ACL. Similarly, if there is a pre-existing ACL that was
889  *         never in sync with its mode (owing to a bug in 6.5 and
890  *         before), it will now magically (or mystically) be
891  *         synchronized.  This could cause slight astonishment, but
892  *         it is better than inconsistent permissions.
893  *
894  * The supplied ACL is a template that may contain any combination
895  * of special entries.  These are treated as place holders when we fill
896  * out the ACL.  This routine does not add or remove special entries, it
897  * simply unites each special entry with its associated set of permissions.
898  */
899 STATIC void
900 xfs_acl_sync_mode(
901         mode_t          mode,
902         xfs_acl_t       *acl)
903 {
904         int             i, nomask = 1;
905         xfs_acl_entry_t *ap;
906         xfs_acl_entry_t *gap = NULL;
907
908         /*
909          * Set ACL entries. POSIX1003.1eD16 requires that the MASK
910          * be set instead of the GROUP entry, if there is a MASK.
911          */
912         for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
913                 switch (ap->ae_tag) {
914                 case ACL_USER_OBJ:
915                         ap->ae_perm = (mode >> 6) & 0x7;
916                         break;
917                 case ACL_GROUP_OBJ:
918                         gap = ap;
919                         break;
920                 case ACL_MASK:
921                         nomask = 0;
922                         ap->ae_perm = (mode >> 3) & 0x7;
923                         break;
924                 case ACL_OTHER:
925                         ap->ae_perm = mode & 0x7;
926                         break;
927                 default:
928                         break;
929                 }
930         }
931         /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
932         if (gap && nomask)
933                 gap->ae_perm = (mode >> 3) & 0x7;
934 }
935
936 /*
937  * When inheriting an Access ACL from a directory Default ACL,
938  * the ACL bits are set to the intersection of the ACL default
939  * permission bits and the file permission bits in mode. If there
940  * are no permission bits on the file then we must not give them
941  * the ACL. This is what what makes umask() work with ACLs.
942  */
943 STATIC void
944 xfs_acl_filter_mode(
945         mode_t          mode,
946         xfs_acl_t       *acl)
947 {
948         int             i, nomask = 1;
949         xfs_acl_entry_t *ap;
950         xfs_acl_entry_t *gap = NULL;
951
952         /*
953          * Set ACL entries. POSIX1003.1eD16 requires that the MASK
954          * be merged with GROUP entry, if there is a MASK.
955          */
956         for (ap = acl->acl_entry, i = 0; i < acl->acl_cnt; ap++, i++) {
957                 switch (ap->ae_tag) {
958                 case ACL_USER_OBJ:
959                         ap->ae_perm &= (mode >> 6) & 0x7;
960                         break;
961                 case ACL_GROUP_OBJ:
962                         gap = ap;
963                         break;
964                 case ACL_MASK:
965                         nomask = 0;
966                         ap->ae_perm &= (mode >> 3) & 0x7;
967                         break;
968                 case ACL_OTHER:
969                         ap->ae_perm &= mode & 0x7;
970                         break;
971                 default:
972                         break;
973                 }
974         }
975         /* Set the ACL_GROUP_OBJ if there's no ACL_MASK */
976         if (gap && nomask)
977                 gap->ae_perm &= (mode >> 3) & 0x7;
978 }