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