]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/kern/subr_acl_nfs4.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / kern / subr_acl_nfs4.c
1 /*-
2  * Copyright (c) 2008-2009 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 /*
28  * ACL support routines specific to NFSv4 access control lists.  These are
29  * utility routines for code common across file systems implementing NFSv4
30  * ACLs.
31  */
32
33 #ifdef _KERNEL
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mount.h>
40 #include <sys/priv.h>
41 #include <sys/vnode.h>
42 #include <sys/errno.h>
43 #include <sys/stat.h>
44 #include <sys/acl.h>
45 #else
46 #include <errno.h>
47 #include <assert.h>
48 #include <sys/acl.h>
49 #include <sys/stat.h>
50 #define KASSERT(a, b) assert(a)
51 #define CTASSERT(a)
52 #endif /* _KERNEL */
53
54 #ifdef _KERNEL
55
56 static struct {
57         accmode_t accmode;
58         int mask;
59 } accmode2mask[] = {{VREAD, ACL_READ_DATA},
60                     {VWRITE, ACL_WRITE_DATA},
61                     {VAPPEND, ACL_APPEND_DATA},
62                     {VEXEC, ACL_EXECUTE},
63                     {VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
64                     {VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
65                     {VDELETE_CHILD, ACL_DELETE_CHILD},
66                     {VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES},
67                     {VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
68                     {VDELETE, ACL_DELETE},
69                     {VREAD_ACL, ACL_READ_ACL},
70                     {VWRITE_ACL, ACL_WRITE_ACL},
71                     {VWRITE_OWNER, ACL_WRITE_OWNER},
72                     {VSYNCHRONIZE, ACL_SYNCHRONIZE},
73                     {0, 0}};
74
75 static int
76 _access_mask_from_accmode(accmode_t accmode)
77 {
78         int access_mask = 0, i;
79
80         for (i = 0; accmode2mask[i].accmode != 0; i++) {
81                 if (accmode & accmode2mask[i].accmode)
82                         access_mask |= accmode2mask[i].mask;
83         }
84
85         /*
86          * VAPPEND is just a modifier for VWRITE; if the caller asked
87          * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only.
88          */
89         if (access_mask & ACL_APPEND_DATA)
90                 access_mask &= ~ACL_WRITE_DATA;
91
92         return (access_mask);
93 }
94
95 /*
96  * Return 0, iff access is allowed, 1 otherwise.
97  */
98 static int
99 _acl_denies(const struct acl *aclp, int access_mask, struct ucred *cred,
100     int file_uid, int file_gid, int *denied_explicitly)
101 {
102         int i;
103         const struct acl_entry *entry;
104
105         if (denied_explicitly != NULL)
106                 *denied_explicitly = 0;
107
108         KASSERT(aclp->acl_cnt > 0, ("aclp->acl_cnt > 0"));
109         KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
110             ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
111
112         for (i = 0; i < aclp->acl_cnt; i++) {
113                 entry = &(aclp->acl_entry[i]);
114
115                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
116                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
117                         continue;
118                 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
119                         continue;
120                 switch (entry->ae_tag) {
121                 case ACL_USER_OBJ:
122                         if (file_uid != cred->cr_uid)
123                                 continue;
124                         break;
125                 case ACL_USER:
126                         if (entry->ae_id != cred->cr_uid)
127                                 continue;
128                         break;
129                 case ACL_GROUP_OBJ:
130                         if (!groupmember(file_gid, cred))
131                                 continue;
132                         break;
133                 case ACL_GROUP:
134                         if (!groupmember(entry->ae_id, cred))
135                                 continue;
136                         break;
137                 default:
138                         KASSERT(entry->ae_tag == ACL_EVERYONE,
139                             ("entry->ae_tag == ACL_EVERYONE"));
140                 }
141
142                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
143                         if (entry->ae_perm & access_mask) {
144                                 if (denied_explicitly != NULL)
145                                         *denied_explicitly = 1;
146                                 return (1);
147                         }
148                 }
149
150                 access_mask &= ~(entry->ae_perm);
151                 if (access_mask == 0)
152                         return (0);
153         }
154
155         return (1);
156 }
157
158 int
159 vaccess_acl_nfs4(enum vtype type, uid_t file_uid, gid_t file_gid,
160     struct acl *aclp, accmode_t accmode, struct ucred *cred, int *privused)
161 {
162         accmode_t priv_granted = 0;
163         int denied, explicitly_denied, access_mask, is_directory,
164             must_be_owner = 0;
165
166         if (privused != NULL)
167                 *privused = 0;
168
169         if (accmode & VADMIN)
170                 must_be_owner = 1;
171
172         /*
173          * Ignore VSYNCHRONIZE permission.
174          */
175         accmode &= ~VSYNCHRONIZE;
176
177         access_mask = _access_mask_from_accmode(accmode);
178
179         if (type == VDIR)
180                 is_directory = 1;
181         else
182                 is_directory = 0;
183
184         /*
185          * File owner is always allowed to read and write the ACL
186          * and basic attributes.  This is to prevent a situation
187          * where user would change ACL in a way that prevents him
188          * from undoing the change.
189          */
190         if (file_uid == cred->cr_uid)
191                 access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL |
192                     ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES);
193
194         /*
195          * Ignore append permission for regular files; use write
196          * permission instead.
197          */
198         if (!is_directory && (access_mask & ACL_APPEND_DATA)) {
199                 access_mask &= ~ACL_APPEND_DATA;
200                 access_mask |= ACL_WRITE_DATA;
201         }
202
203         denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid,
204             &explicitly_denied);
205
206         if (must_be_owner) {
207                 if (file_uid != cred->cr_uid)
208                         denied = EPERM;
209         }
210
211         if (!denied)
212                 return (0);
213
214         /*
215          * Access failed.  Iff it was not denied explicitly and
216          * VEXPLICIT_DENY flag was specified, allow access.
217          */
218         if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0)
219                 return (0);
220
221         accmode &= ~VEXPLICIT_DENY;
222
223         /*
224          * No match.  Try to use privileges, if there are any.
225          */
226         if (is_directory) {
227                 if ((accmode & VEXEC) && !priv_check_cred(cred,
228                     PRIV_VFS_LOOKUP, 0))
229                         priv_granted |= VEXEC;
230         } else {
231                 if ((accmode & VEXEC) && !priv_check_cred(cred,
232                     PRIV_VFS_EXEC, 0))
233                         priv_granted |= VEXEC;
234         }
235
236         if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ, 0))
237                 priv_granted |= VREAD;
238
239         if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) &&
240             !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
241                 priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD);
242
243         if ((accmode & VADMIN_PERMS) &&
244             !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
245                 priv_granted |= VADMIN_PERMS;
246
247         if ((accmode & VSTAT_PERMS) &&
248             !priv_check_cred(cred, PRIV_VFS_STAT, 0))
249                 priv_granted |= VSTAT_PERMS;
250
251         if ((accmode & priv_granted) == accmode) {
252                 if (privused != NULL)
253                         *privused = 1;
254
255                 return (0);
256         }
257
258         if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
259                 denied = EPERM;
260         else
261                 denied = EACCES;
262
263         return (denied);
264 }
265 #endif /* _KERNEL */
266
267 static int
268 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm,
269     acl_entry_type_t entry_type)
270 {
271         if (entry->ae_tag != tag)
272                 return (0);
273
274         if (entry->ae_id != ACL_UNDEFINED_ID)
275                 return (0);
276
277         if (entry->ae_perm != perm)
278                 return (0);
279
280         if (entry->ae_entry_type != entry_type)
281                 return (0);
282
283         if (entry->ae_flags != 0)
284                 return (0);
285
286         return (1);
287 }
288
289 static struct acl_entry *
290 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm,
291     acl_entry_type_t entry_type)
292 {
293         struct acl_entry *entry;
294
295         KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
296             ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
297
298         entry = &(aclp->acl_entry[aclp->acl_cnt]);
299         aclp->acl_cnt++;
300
301         entry->ae_tag = tag;
302         entry->ae_id = ACL_UNDEFINED_ID;
303         entry->ae_perm = perm;
304         entry->ae_entry_type = entry_type;
305         entry->ae_flags = 0;
306
307         return (entry);
308 }
309
310 static struct acl_entry *
311 _acl_duplicate_entry(struct acl *aclp, int entry_index)
312 {
313         int i;
314
315         KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
316             ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
317
318         for (i = aclp->acl_cnt; i > entry_index; i--)
319                 aclp->acl_entry[i] = aclp->acl_entry[i - 1];
320
321         aclp->acl_cnt++;
322
323         return (&(aclp->acl_entry[entry_index + 1]));
324 }
325
326 void
327 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode, int file_owner_id)
328 {
329         int i, meets, must_append;
330         struct acl_entry *entry, *copy, *previous,
331             *a1, *a2, *a3, *a4, *a5, *a6;
332         mode_t amode;
333         const int READ = 04;
334         const int WRITE = 02;
335         const int EXEC = 01;
336
337         KASSERT(aclp->acl_cnt >= 0, ("aclp->acl_cnt >= 0"));
338         KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
339             ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
340
341         /*
342          * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
343          *
344          * 3.16.6.3. Applying a Mode to an Existing ACL
345          */
346
347         /*
348          * 1. For each ACE:
349          */
350         for (i = 0; i < aclp->acl_cnt; i++) {
351                 entry = &(aclp->acl_entry[i]);
352
353                 /*
354                  * 1.1. If the type is neither ALLOW or DENY - skip.
355                  */
356                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
357                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
358                         continue;
359
360                 /*
361                  * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip.
362                  */
363                 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
364                         continue;
365
366                 /*
367                  * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT
368                  *      are set:
369                  */
370                 if (entry->ae_flags &
371                     (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) {
372                         /*
373                          * 1.3.1. A copy of the current ACE is made, and placed
374                          *        in the ACL immediately following the current
375                          *        ACE.
376                          */
377                         copy = _acl_duplicate_entry(aclp, i);
378
379                         /*
380                          * 1.3.2. In the first ACE, the flag
381                          *        ACL_ENTRY_INHERIT_ONLY is set.
382                          */
383                         entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
384
385                         /*
386                          * 1.3.3. In the second ACE, the following flags
387                          *        are cleared:
388                          *        ACL_ENTRY_FILE_INHERIT,
389                          *        ACL_ENTRY_DIRECTORY_INHERIT,
390                          *        ACL_ENTRY_NO_PROPAGATE_INHERIT.
391                          */
392                         copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT |
393                             ACL_ENTRY_DIRECTORY_INHERIT |
394                             ACL_ENTRY_NO_PROPAGATE_INHERIT);
395
396                         /*
397                          * The algorithm continues on with the second ACE.
398                          */
399                         i++;
400                         entry = copy;
401                 }
402
403                 /*
404                  * 1.4. If it's owner@, group@ or everyone@ entry, clear
405                  *      ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA
406                  *      and ACL_EXECUTE.  Continue to the next entry.
407                  */
408                 if (entry->ae_tag == ACL_USER_OBJ ||
409                     entry->ae_tag == ACL_GROUP_OBJ ||
410                     entry->ae_tag == ACL_EVERYONE) {
411                         entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA |
412                             ACL_APPEND_DATA | ACL_EXECUTE);
413                         continue;
414                 }
415
416                 /*
417                  * 1.5. Otherwise, if the "who" field did not match one
418                  *      of OWNER@, GROUP@, EVERYONE@:
419                  *
420                  * 1.5.1. If the type is ALLOW, check the preceding ACE.
421                  *        If it does not meet all of the following criteria:
422                  */
423                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW)
424                         continue;
425
426                 meets = 0;
427                 if (i > 0) {
428                         meets = 1;
429                         previous = &(aclp->acl_entry[i - 1]);
430
431                         /*
432                          * 1.5.1.1. The type field is DENY,
433                          */
434                         if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY)
435                                 meets = 0;
436
437                         /*
438                          * 1.5.1.2. The "who" field is the same as the current
439                          *          ACE,
440                          *
441                          * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP
442                          *          is the same as it is in the current ACE,
443                          *          and no other flag bits are set,
444                          */
445                         if (previous->ae_id != entry->ae_id ||
446                             previous->ae_tag != entry->ae_tag)
447                                 meets = 0;
448
449                         if (previous->ae_flags)
450                                 meets = 0;
451
452                         /*
453                          * 1.5.1.4. The mask bits are a subset of the mask bits
454                          *          of the current ACE, and are also subset of
455                          *          the following: ACL_READ_DATA,
456                          *          ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE
457                          */
458                         if (previous->ae_perm & ~(entry->ae_perm))
459                                 meets = 0;
460
461                         if (previous->ae_perm & ~(ACL_READ_DATA |
462                             ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE))
463                                 meets = 0;
464                 }
465
466                 if (!meets) {
467                         /*
468                          * Then the ACE of type DENY, with a who equal
469                          * to the current ACE, flag bits equal to
470                          * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>)
471                          * and no mask bits, is prepended.
472                          */
473                         previous = entry;
474                         entry = _acl_duplicate_entry(aclp, i);
475
476                         /* Adjust counter, as we've just added an entry. */
477                         i++;
478
479                         previous->ae_tag = entry->ae_tag;
480                         previous->ae_id = entry->ae_id;
481                         previous->ae_flags = entry->ae_flags;
482                         previous->ae_perm = 0;
483                         previous->ae_entry_type = ACL_ENTRY_TYPE_DENY;
484                 }
485
486                 /*
487                  * 1.5.2. The following modifications are made to the prepended
488                  *        ACE.  The intent is to mask the following ACE
489                  *        to disallow ACL_READ_DATA, ACL_WRITE_DATA,
490                  *        ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group
491                  *        permissions of the new mode.  As a special case,
492                  *        if the ACE matches the current owner of the file,
493                  *        the owner bits are used, rather than the group bits.
494                  *        This is reflected in the algorithm below.
495                  */
496                 amode = mode >> 3;
497
498                 /*
499                  * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field
500                  * in ACE matches the owner of the file, we shift amode three
501                  * more bits, in order to have the owner permission bits
502                  * placed in the three low order bits of amode.
503                  */
504                 if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id)
505                         amode = amode >> 3;
506
507                 if (entry->ae_perm & ACL_READ_DATA) {
508                         if (amode & READ)
509                                 previous->ae_perm &= ~ACL_READ_DATA;
510                         else
511                                 previous->ae_perm |= ACL_READ_DATA;
512                 }
513
514                 if (entry->ae_perm & ACL_WRITE_DATA) {
515                         if (amode & WRITE)
516                                 previous->ae_perm &= ~ACL_WRITE_DATA;
517                         else
518                                 previous->ae_perm |= ACL_WRITE_DATA;
519                 }
520
521                 if (entry->ae_perm & ACL_APPEND_DATA) {
522                         if (amode & WRITE)
523                                 previous->ae_perm &= ~ACL_APPEND_DATA;
524                         else
525                                 previous->ae_perm |= ACL_APPEND_DATA;
526                 }
527
528                 if (entry->ae_perm & ACL_EXECUTE) {
529                         if (amode & EXEC)
530                                 previous->ae_perm &= ~ACL_EXECUTE;
531                         else
532                                 previous->ae_perm |= ACL_EXECUTE;
533                 }
534
535                 /*
536                  * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags
537                  *        of the ALLOW ace:
538                  *
539                  * XXX: This point is not there in the Falkner's draft.
540                  */
541                 if (entry->ae_tag == ACL_GROUP &&
542                     entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
543                         mode_t extramode, ownermode;
544                         extramode = (mode >> 3) & 07;
545                         ownermode = mode >> 6;
546                         extramode &= ~ownermode;
547
548                         if (extramode) {
549                                 if (extramode & READ) {
550                                         entry->ae_perm &= ~ACL_READ_DATA;
551                                         previous->ae_perm &= ~ACL_READ_DATA;
552                                 }
553
554                                 if (extramode & WRITE) {
555                                         entry->ae_perm &=
556                                             ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
557                                         previous->ae_perm &=
558                                             ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
559                                 }
560
561                                 if (extramode & EXEC) {
562                                         entry->ae_perm &= ~ACL_EXECUTE;
563                                         previous->ae_perm &= ~ACL_EXECUTE;
564                                 }
565                         }
566                 }
567         }
568
569         /*
570          * 2. If there at least six ACEs, the final six ACEs are examined.
571          *    If they are not equal to what we want, append six ACEs.
572          */
573         must_append = 0;
574         if (aclp->acl_cnt < 6) {
575                 must_append = 1;
576         } else {
577                 a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]);
578                 a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]);
579                 a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]);
580                 a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]);
581                 a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]);
582                 a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]);
583
584                 if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0,
585                     ACL_ENTRY_TYPE_DENY))
586                         must_append = 1;
587                 if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL |
588                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
589                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW))
590                         must_append = 1;
591                 if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0,
592                     ACL_ENTRY_TYPE_DENY))
593                         must_append = 1;
594                 if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0,
595                     ACL_ENTRY_TYPE_ALLOW))
596                         must_append = 1;
597                 if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL |
598                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
599                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY))
600                         must_append = 1;
601                 if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL |
602                     ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
603                     ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW))
604                         must_append = 1;
605         }
606
607         if (must_append) {
608                 KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES,
609                     ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
610
611                 a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY);
612                 a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL |
613                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
614                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW);
615                 a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY);
616                 a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW);
617                 a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL |
618                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
619                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY);
620                 a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL |
621                     ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
622                     ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW);
623
624                 KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL &&
625                     a5 != NULL && a6 != NULL, ("couldn't append to ACL."));
626         }
627
628         /*
629          * 3. The final six ACEs are adjusted according to the incoming mode.
630          */
631         if (mode & S_IRUSR)
632                 a2->ae_perm |= ACL_READ_DATA;
633         else
634                 a1->ae_perm |= ACL_READ_DATA;
635         if (mode & S_IWUSR)
636                 a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
637         else
638                 a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
639         if (mode & S_IXUSR)
640                 a2->ae_perm |= ACL_EXECUTE;
641         else
642                 a1->ae_perm |= ACL_EXECUTE;
643
644         if (mode & S_IRGRP)
645                 a4->ae_perm |= ACL_READ_DATA;
646         else
647                 a3->ae_perm |= ACL_READ_DATA;
648         if (mode & S_IWGRP)
649                 a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
650         else
651                 a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
652         if (mode & S_IXGRP)
653                 a4->ae_perm |= ACL_EXECUTE;
654         else
655                 a3->ae_perm |= ACL_EXECUTE;
656
657         if (mode & S_IROTH)
658                 a6->ae_perm |= ACL_READ_DATA;
659         else
660                 a5->ae_perm |= ACL_READ_DATA;
661         if (mode & S_IWOTH)
662                 a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
663         else
664                 a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
665         if (mode & S_IXOTH)
666                 a6->ae_perm |= ACL_EXECUTE;
667         else
668                 a5->ae_perm |= ACL_EXECUTE;
669 }
670
671 void
672 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp)
673 {
674         int i;
675         mode_t old_mode = *_mode, mode = 0, seen = 0;
676         const struct acl_entry *entry;
677
678         KASSERT(aclp->acl_cnt > 0, ("aclp->acl_cnt > 0"));
679         KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
680             ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
681
682         /*
683          * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
684          *
685          * 3.16.6.1. Recomputing mode upon SETATTR of ACL
686          */
687
688         for (i = 0; i < aclp->acl_cnt; i++) {
689                 entry = &(aclp->acl_entry[i]);
690
691                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
692                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
693                         continue;
694
695                 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
696                         continue;
697
698                 if (entry->ae_tag == ACL_USER_OBJ) {
699                         if ((entry->ae_perm & ACL_READ_DATA) &&
700                             ((seen & S_IRUSR) == 0)) {
701                                 seen |= S_IRUSR;
702                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
703                                         mode |= S_IRUSR;
704                         }
705                         if ((entry->ae_perm & ACL_WRITE_DATA) &&
706                              ((seen & S_IWUSR) == 0)) {
707                                 seen |= S_IWUSR;
708                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
709                                         mode |= S_IWUSR;
710                         }
711                         if ((entry->ae_perm & ACL_EXECUTE) &&
712                             ((seen & S_IXUSR) == 0)) {
713                                 seen |= S_IXUSR;
714                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
715                                         mode |= S_IXUSR;
716                         }
717                 } else if (entry->ae_tag == ACL_GROUP_OBJ) {
718                         if ((entry->ae_perm & ACL_READ_DATA) &&
719                             ((seen & S_IRGRP) == 0)) {
720                                 seen |= S_IRGRP;
721                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
722                                         mode |= S_IRGRP;
723                         }
724                         if ((entry->ae_perm & ACL_WRITE_DATA) &&
725                             ((seen & S_IWGRP) == 0)) {
726                                 seen |= S_IWGRP;
727                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
728                                         mode |= S_IWGRP;
729                         }
730                         if ((entry->ae_perm & ACL_EXECUTE) &&
731                             ((seen & S_IXGRP) == 0)) {
732                                 seen |= S_IXGRP;
733                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
734                                         mode |= S_IXGRP;
735                         }
736                 } else if (entry->ae_tag == ACL_EVERYONE) {
737                         if (entry->ae_perm & ACL_READ_DATA) {
738                                 if ((seen & S_IRUSR) == 0) {
739                                         seen |= S_IRUSR;
740                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
741                                                 mode |= S_IRUSR;
742                                 }
743                                 if ((seen & S_IRGRP) == 0) {
744                                         seen |= S_IRGRP;
745                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
746                                                 mode |= S_IRGRP;
747                                 }
748                                 if ((seen & S_IROTH) == 0) {
749                                         seen |= S_IROTH;
750                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
751                                                 mode |= S_IROTH;
752                                 }
753                         }
754                         if (entry->ae_perm & ACL_WRITE_DATA) {
755                                 if ((seen & S_IWUSR) == 0) {
756                                         seen |= S_IWUSR;
757                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
758                                                 mode |= S_IWUSR;
759                                 }
760                                 if ((seen & S_IWGRP) == 0) {
761                                         seen |= S_IWGRP;
762                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
763                                                 mode |= S_IWGRP;
764                                 }
765                                 if ((seen & S_IWOTH) == 0) {
766                                         seen |= S_IWOTH;
767                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
768                                                 mode |= S_IWOTH;
769                                 }
770                         }
771                         if (entry->ae_perm & ACL_EXECUTE) {
772                                 if ((seen & S_IXUSR) == 0) {
773                                         seen |= S_IXUSR;
774                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
775                                                 mode |= S_IXUSR;
776                                 }
777                                 if ((seen & S_IXGRP) == 0) {
778                                         seen |= S_IXGRP;
779                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
780                                                 mode |= S_IXGRP;
781                                 }
782                                 if ((seen & S_IXOTH) == 0) {
783                                         seen |= S_IXOTH;
784                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
785                                                 mode |= S_IXOTH;
786                                 }
787                         }
788                 }
789         }
790
791         *_mode = mode | (old_mode & ACL_PRESERVE_MASK);
792 }
793
794 void            
795 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp,
796     struct acl *child_aclp, mode_t mode, int file_owner_id,
797     int is_directory)
798 {
799         int i, flags;
800         const struct acl_entry *parent_entry;
801         struct acl_entry *entry, *copy;
802
803         KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0"));
804         KASSERT(parent_aclp->acl_cnt > 0, ("parent_aclp->acl_cnt > 0"));
805         KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
806             ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
807
808         /*
809          * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
810          *
811          * 3.16.6.2. Applying the mode given to CREATE or OPEN
812          *           to an inherited ACL
813          */
814
815         /*
816          * 1. Form an ACL that is the concatenation of all inheritable ACEs.
817          */
818         for (i = 0; i < parent_aclp->acl_cnt; i++) {
819                 parent_entry = &(parent_aclp->acl_entry[i]);
820                 flags = parent_entry->ae_flags;
821
822                 /*
823                  * Entry is not inheritable at all.
824                  */
825                 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
826                     ACL_ENTRY_FILE_INHERIT)) == 0)
827                         continue;
828
829                 /*
830                  * We're creating a file, but entry is not inheritable
831                  * by files.
832                  */
833                 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
834                         continue;
835
836                 /*
837                  * Entry is inheritable only by files, but has NO_PROPAGATE
838                  * flag set, and we're creating a directory, so it wouldn't
839                  * propagate to any file in that directory anyway.
840                  */
841                 if (is_directory &&
842                     (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
843                     (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
844                         continue;
845
846                 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
847                     ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
848                 child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry;
849                 child_aclp->acl_cnt++;
850         }
851
852         /*
853          * 2. For each entry in the new ACL, adjust its flags, possibly
854          *    creating two entries in place of one.
855          */
856         for (i = 0; i < child_aclp->acl_cnt; i++) {
857                 entry = &(child_aclp->acl_entry[i]);
858
859                 /*
860                  * This is not in the specification, but SunOS
861                  * apparently does that.
862                  */
863                 if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) ||
864                     !is_directory) &&
865                     entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
866                         entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
867
868                 /*
869                  * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object
870                  *      being created is not a directory, then clear the
871                  *      following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
872                  *      ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
873                  *      ACL_ENTRY_INHERIT_ONLY.
874                  */
875                 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
876                     !is_directory) {
877                         entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
878                         ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
879                         ACL_ENTRY_INHERIT_ONLY);
880
881                         /*
882                          * Continue on to the next ACE.
883                          */
884                         continue;
885                 }
886
887                 /*
888                  * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT
889                  *      is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure
890                  *      that ACL_ENTRY_INHERIT_ONLY is set.  Continue to the
891                  *      next ACE.  Otherwise...
892                  */
893                 /*
894                  * XXX: Read it again and make sure what does the "otherwise"
895                  *      apply to.
896                  */
897                 if (is_directory &&
898                     (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
899                     ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
900                         entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
901                         continue;
902                 }
903
904                 /*
905                  * 2.C. If the type of the ACE is neither ALLOW nor deny,
906                  *      then continue.
907                  */
908                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
909                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
910                         continue;
911
912                 /*
913                  * 2.D. Copy the original ACE into a second, adjacent ACE.
914                  */
915                 copy = _acl_duplicate_entry(child_aclp, i);
916
917                 /*
918                  * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY
919                  *      is set.
920                  */
921                 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
922
923                 /*
924                  * 2.F. On the second ACE, clear the following flags:
925                  *      ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT,
926                  *      ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY.
927                  */
928                 copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
929                     ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
930                     ACL_ENTRY_INHERIT_ONLY);
931
932                 /*
933                  * 2.G. On the second ACE, if the type is ALLOW,
934                  *      an implementation MAY clear the following
935                  *      mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER.
936                  */
937                 if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
938                         copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
939
940                 /*
941                  * Increment the counter to skip the copied entry.
942                  */
943                 i++;
944         }
945
946         /*
947          * 3. To ensure that the mode is honored, apply the algorithm describe
948          *    in Section 2.16.6.3, using the mode that is to be used for file
949          *    creation.
950          */
951         acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id);
952 }
953
954 #ifdef _KERNEL
955 static int
956 _acls_are_equal(const struct acl *a, const struct acl *b)
957 {
958         int i;
959         const struct acl_entry *entrya, *entryb;
960
961         if (a->acl_cnt != b->acl_cnt)
962                 return (0);
963
964         for (i = 0; i < b->acl_cnt; i++) {
965                 entrya = &(a->acl_entry[i]);
966                 entryb = &(b->acl_entry[i]);
967
968                 if (entrya->ae_tag != entryb->ae_tag ||
969                     entrya->ae_id != entryb->ae_id ||
970                     entrya->ae_perm != entryb->ae_perm ||
971                     entrya->ae_entry_type != entryb->ae_entry_type ||
972                     entrya->ae_flags != entryb->ae_flags)
973                         return (0);
974         }
975
976         return (1);
977 }
978
979 /*
980  * This routine is used to determine whether to remove entry_type attribute
981  * that stores ACL contents.
982  */
983 int
984 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id)
985 {
986         int trivial;
987         mode_t tmpmode = 0;
988         struct acl *tmpaclp;
989
990         if (aclp->acl_cnt != 6)
991                 return (0);
992
993         /*
994          * Compute the mode from the ACL, then compute new ACL from that mode.
995          * If the ACLs are identical, then the ACL is trivial.
996          *
997          * XXX: I guess there is a faster way to do this.  However, even
998          *      this slow implementation significantly speeds things up
999          *      for files that don't have any entry_type ACL entries - it's
1000          *      critical for performance to not use EA when they are not
1001          *      needed.
1002          */
1003         tmpaclp = acl_alloc(M_WAITOK | M_ZERO);
1004         acl_nfs4_sync_mode_from_acl(&tmpmode, aclp);
1005         acl_nfs4_sync_acl_from_mode(tmpaclp, tmpmode, file_owner_id);
1006         trivial = _acls_are_equal(aclp, tmpaclp);
1007         acl_free(tmpaclp);
1008
1009         return (trivial);
1010 }
1011 #endif /* _KERNEL */
1012
1013 int
1014 acl_nfs4_check(const struct acl *aclp, int is_directory)
1015 {
1016         int i;
1017         const struct acl_entry *entry;
1018
1019         /*
1020          * The spec doesn't seem to say anything about ACL validity.
1021          * It seems there is not much to do here.  There is even no need
1022          * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE)
1023          * entries, as there can be several of them and that's perfectly
1024          * valid.  There can be none of them too.  Really.
1025          */
1026
1027         if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0)
1028                 return (EINVAL);
1029
1030         for (i = 0; i < aclp->acl_cnt; i++) {
1031                 entry = &(aclp->acl_entry[i]);
1032
1033                 switch (entry->ae_tag) {
1034                 case ACL_USER_OBJ:
1035                 case ACL_GROUP_OBJ:
1036                 case ACL_EVERYONE:
1037                         if (entry->ae_id != ACL_UNDEFINED_ID)
1038                                 return (EINVAL);
1039                         break;
1040
1041                 case ACL_USER:
1042                 case ACL_GROUP:
1043                         if (entry->ae_id == ACL_UNDEFINED_ID)
1044                                 return (EINVAL);
1045                         break;
1046
1047                 default:
1048                         return (EINVAL);
1049                 }
1050
1051                 if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS)
1052                         return (EINVAL);
1053
1054                 /*
1055                  * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now.
1056                  */
1057                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
1058                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
1059                         return (EINVAL);
1060
1061                 if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS)
1062                         return (EINVAL);
1063
1064                 /* Disallow unimplemented flags. */
1065                 if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS |
1066                     ACL_ENTRY_FAILED_ACCESS))
1067                         return (EINVAL);
1068
1069                 /* Disallow flags not allowed for ordinary files. */
1070                 if (!is_directory) {
1071                         if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT |
1072                             ACL_ENTRY_DIRECTORY_INHERIT |
1073                             ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY))
1074                                 return (EINVAL);
1075                 }
1076         }
1077
1078         return (0);
1079 }