1 /*
2 * fs/nfs4acl/acl.c
3 *
4 * Common NFSv4 ACL handling code.
5 *
6 * Copyright (c) 2002, 2003 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Marius Aamodt Eriksen <marius@umich.edu>
10 * Jeff Sedlak <jsedlak@umich.edu>
11 * J. Bruce Fields <bfields@umich.edu>
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <linux/string.h>
40 #include <linux/slab.h>
41 #include <linux/list.h>
42 #include <linux/types.h>
43 #include <linux/fs.h>
44 #include <linux/module.h>
45 #include <linux/nfs_fs.h>
46 #include <linux/posix_acl.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs4_acl.h>
49
50
51 /* mode bit translations: */
52 #define NFS4_READ_MODE (NFS4_ACE_READ_DATA | NFS4_ACE_READ_NAMED_ATTRS)
53 #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_WRITE_NAMED_ATTRS | NFS4_ACE_APPEND_DATA)
54 #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE
55 #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)
56 #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)
57
58 /* flags used to simulate posix default ACLs */
59 #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
60 | NFS4_ACE_DIRECTORY_INHERIT_ACE | NFS4_ACE_INHERIT_ONLY_ACE)
61
62 #define MASK_EQUAL(mask1, mask2) \
63 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
64
65 static u32
66 mask_from_posix(unsigned short perm, unsigned int flags)
67 {
68 int mask = NFS4_ANYONE_MODE;
69
70 if (flags & NFS4_ACL_OWNER)
71 mask |= NFS4_OWNER_MODE;
72 if (perm & ACL_READ)
73 mask |= NFS4_READ_MODE;
74 if (perm & ACL_WRITE)
75 mask |= NFS4_WRITE_MODE;
76 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
77 mask |= NFS4_ACE_DELETE_CHILD;
78 if (perm & ACL_EXECUTE)
79 mask |= NFS4_EXECUTE_MODE;
80 return mask;
81 }
82
83 static u32
84 deny_mask(u32 allow_mask, unsigned int flags)
85 {
86 u32 ret = ~allow_mask & ~NFS4_ACE_DELETE;
87 if (!(flags & NFS4_ACL_DIR))
88 ret &= ~NFS4_ACE_DELETE_CHILD;
89 return ret;
90 }
91
92 static int
93 mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
94 {
95 u32 ignore = 0;
96
97 if (!(flags & NFS4_ACL_DIR))
98 ignore |= NFS4_ACE_DELETE_CHILD; /* ignore it */
99 perm |= ignore;
100 *mode = 0;
101 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
102 *mode |= ACL_READ;
103 if ((perm & NFS4_WRITE_MODE) == NFS4_WRITE_MODE)
104 *mode |= ACL_WRITE;
105 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
106 *mode |= ACL_EXECUTE;
107 if (!MASK_EQUAL(perm, ignore|mask_from_posix(*mode, flags)))
108 return -EINVAL;
109 return 0;
110 }
111
112 struct ace_container {
113 struct nfs4_ace *ace;
114 struct list_head ace_l;
115 };
116
117 static short ace2type(struct nfs4_ace *);
118 static int _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, unsigned int);
119 static struct posix_acl *_nfsv4_to_posix_one(struct nfs4_acl *, unsigned int);
120 int nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);
121 int nfs4_acl_split(struct nfs4_acl *, struct nfs4_acl *);
122
123 struct nfs4_acl *
124 nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
125 unsigned int flags)
126 {
127 struct nfs4_acl *acl;
128 int error = -EINVAL;
129
130 if ((pacl != NULL &&
131 (posix_acl_valid(pacl) < 0 || pacl->a_count == 0)) ||
132 (dpacl != NULL &&
133 (posix_acl_valid(dpacl) < 0 || dpacl->a_count == 0)))
134 goto out_err;
135
136 acl = nfs4_acl_new();
137 if (acl == NULL) {
138 error = -ENOMEM;
139 goto out_err;
140 }
141
142 if (pacl != NULL) {
143 error = _posix_to_nfsv4_one(pacl, acl,
144 flags & ~NFS4_ACL_TYPE_DEFAULT);
145 if (error < 0)
146 goto out_acl;
147 }
148
149 if (dpacl != NULL) {
150 error = _posix_to_nfsv4_one(dpacl, acl,
151 flags | NFS4_ACL_TYPE_DEFAULT);
152 if (error < 0)
153 goto out_acl;
154 }
155
156 return acl;
157
158 out_acl:
159 nfs4_acl_free(acl);
160 out_err:
161 acl = ERR_PTR(error);
162
163 return acl;
164 }
165
166 static int
167 nfs4_acl_add_pair(struct nfs4_acl *acl, int eflag, u32 mask, int whotype,
168 uid_t owner, unsigned int flags)
169 {
170 int error;
171
172 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
173 eflag, mask, whotype, owner);
174 if (error < 0)
175 return error;
176 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
177 eflag, deny_mask(mask, flags), whotype, owner);
178 return error;
179 }
180
181 /* We assume the acl has been verified with posix_acl_valid. */
182 static int
183 _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
184 unsigned int flags)
185 {
186 struct posix_acl_entry *pa, *pe, *group_owner_entry;
187 int error = -EINVAL;
188 u32 mask, mask_mask;
189 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
190 NFS4_INHERITANCE_FLAGS : 0);
191
192 BUG_ON(pacl->a_count < 3);
193 pe = pacl->a_entries + pacl->a_count;
194 pa = pe - 2; /* if mask entry exists, it's second from the last. */
195 if (pa->e_tag == ACL_MASK)
196 mask_mask = deny_mask(mask_from_posix(pa->e_perm, flags), flags);
197 else
198 mask_mask = 0;
199
200 pa = pacl->a_entries;
201 BUG_ON(pa->e_tag != ACL_USER_OBJ);
202 mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
203 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_OWNER, 0, flags);
204 if (error < 0)
205 goto out;
206 pa++;
207
208 while (pa->e_tag == ACL_USER) {
209 mask = mask_from_posix(pa->e_perm, flags);
210 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
211 eflag, mask_mask, NFS4_ACL_WHO_NAMED, pa->e_id);
212 if (error < 0)
213 goto out;
214
215
216 error = nfs4_acl_add_pair(acl, eflag, mask,
217 NFS4_ACL_WHO_NAMED, pa->e_id, flags);
218 if (error < 0)
219 goto out;
220 pa++;
221 }
222
223 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
224 * since a user can be in more than one group. */
225
226 /* allow ACEs */
227
228 if (pacl->a_count > 3) {
229 BUG_ON(pa->e_tag != ACL_GROUP_OBJ);
230 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
231 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
232 NFS4_ACL_WHO_GROUP, 0);
233 if (error < 0)
234 goto out;
235 }
236 group_owner_entry = pa;
237 mask = mask_from_posix(pa->e_perm, flags);
238 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
239 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
240 NFS4_ACL_WHO_GROUP, 0);
241 if (error < 0)
242 goto out;
243 pa++;
244
245 while (pa->e_tag == ACL_GROUP) {
246 mask = mask_from_posix(pa->e_perm, flags);
247 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
248 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
249 NFS4_ACL_WHO_NAMED, pa->e_id);
250 if (error < 0)
251 goto out;
252
253 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
254 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
255 NFS4_ACL_WHO_NAMED, pa->e_id);
256 if (error < 0)
257 goto out;
258 pa++;
259 }
260
261 /* deny ACEs */
262
263 pa = group_owner_entry;
264 mask = mask_from_posix(pa->e_perm, flags);
265 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
266 NFS4_ACE_IDENTIFIER_GROUP | eflag,
267 deny_mask(mask, flags), NFS4_ACL_WHO_GROUP, 0);
268 if (error < 0)
269 goto out;
270 pa++;
271 while (pa->e_tag == ACL_GROUP) {
272 mask = mask_from_posix(pa->e_perm, flags);
273 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
274 NFS4_ACE_IDENTIFIER_GROUP | eflag,
275 deny_mask(mask, flags), NFS4_ACL_WHO_NAMED, pa->e_id);
276 if (error < 0)
277 goto out;
278 pa++;
279 }
280
281 if (pa->e_tag == ACL_MASK)
282 pa++;
283 BUG_ON(pa->e_tag != ACL_OTHER);
284 mask = mask_from_posix(pa->e_perm, flags);
285 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_EVERYONE, 0, flags);
286
287 out:
288 return error;
289 }
290
291 static void
292 sort_pacl_range(struct posix_acl *pacl, int start, int end) {
293 int sorted = 0, i;
294 struct posix_acl_entry tmp;
295
296 /* We just do a bubble sort; easy to do in place, and we're not
297 * expecting acl's to be long enough to justify anything more. */
298 while (!sorted) {
299 sorted = 1;
300 for (i = start; i < end; i++) {
301 if (pacl->a_entries[i].e_id
302 > pacl->a_entries[i+1].e_id) {
303 sorted = 0;
304 tmp = pacl->a_entries[i];
305 pacl->a_entries[i] = pacl->a_entries[i+1];
306 pacl->a_entries[i+1] = tmp;
307 }
308 }
309 }
310 }
311
312 static void
313 sort_pacl(struct posix_acl *pacl)
314 {
315 /* posix_acl_valid requires that users and groups be in order
316 * by uid/gid. */
317 int i, j;
318
319 if (pacl->a_count <= 4)
320 return; /* no users or groups */
321 i = 1;
322 while (pacl->a_entries[i].e_tag == ACL_USER)
323 i++;
324 sort_pacl_range(pacl, 1, i-1);
325
326 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
327 j = i++;
328 while (pacl->a_entries[j].e_tag == ACL_GROUP)
329 j++;
330 sort_pacl_range(pacl, i, j-1);
331 return;
332 }
333
334 static int
335 write_pace(struct nfs4_ace *ace, struct posix_acl *pacl,
336 struct posix_acl_entry **pace, short tag, unsigned int flags)
337 {
338 struct posix_acl_entry *this = *pace;
339
340 if (*pace == pacl->a_entries + pacl->a_count)
341 return -EINVAL; /* fell off the end */
342 (*pace)++;
343 this->e_tag = tag;
344 if (tag == ACL_USER_OBJ)
345 flags |= NFS4_ACL_OWNER;
346 if (mode_from_nfs4(ace->access_mask, &this->e_perm, flags))
347 return -EINVAL;
348 this->e_id = (tag == ACL_USER || tag == ACL_GROUP ?
349 ace->who : ACL_UNDEFINED_ID);
350 return 0;
351 }
352
353 static struct nfs4_ace *
354 get_next_v4_ace(struct list_head **p, struct list_head *head)
355 {
356 struct nfs4_ace *ace;
357
358 *p = (*p)->next;
359 if (*p == head)
360 return NULL;
361 ace = list_entry(*p, struct nfs4_ace, l_ace);
362
363 return ace;
364 }
365
366 int
367 nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
368 struct posix_acl **dpacl, unsigned int flags)
369 {
370 struct nfs4_acl *dacl;
371 int error = -ENOMEM;
372
373 *pacl = NULL;
374 *dpacl = NULL;
375
376 dacl = nfs4_acl_new();
377 if (dacl == NULL)
378 goto out;
379
380 error = nfs4_acl_split(acl, dacl);
381 if (error < 0)
382 goto out_acl;
383
384 if (pacl != NULL) {
385 if (acl->naces == 0) {
386 error = -ENODATA;
387 goto try_dpacl;
388 }
389
390 *pacl = _nfsv4_to_posix_one(acl, flags);
391 if (IS_ERR(*pacl)) {
392 error = PTR_ERR(*pacl);
393 *pacl = NULL;
394 goto out_acl;
395 }
396 }
397
398 try_dpacl:
399 if (dpacl != NULL) {
400 if (dacl->naces == 0) {
401 if (pacl == NULL || *pacl == NULL)
402 error = -ENODATA;
403 goto out_acl;
404 }
405
406 error = 0;
407 *dpacl = _nfsv4_to_posix_one(dacl, flags);
408 if (IS_ERR(*dpacl)) {
409 error = PTR_ERR(*dpacl);
410 *dpacl = NULL;
411 goto out_acl;
412 }
413 }
414
415 out_acl:
416 if (error && pacl) {
417 posix_acl_release(*pacl);
418 *pacl = NULL;
419 }
420 nfs4_acl_free(dacl);
421 out:
422 return error;
423 }
424
425 static int
426 same_who(struct nfs4_ace *a, struct nfs4_ace *b)
427 {
428 return a->whotype == b->whotype &&
429 (a->whotype != NFS4_ACL_WHO_NAMED || a->who == b->who);
430 }
431
432 static int
433 complementary_ace_pair(struct nfs4_ace *allow, struct nfs4_ace *deny,
434 unsigned int flags)
435 {
436 int ignore = 0;
437 if (!(flags & NFS4_ACL_DIR))
438 ignore |= NFS4_ACE_DELETE_CHILD;
439 return MASK_EQUAL(ignore|deny_mask(allow->access_mask, flags),
440 ignore|deny->access_mask) &&
441 allow->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
442 deny->type == NFS4_ACE_ACCESS_DENIED_ACE_TYPE &&
443 allow->flag == deny->flag &&
444 same_who(allow, deny);
445 }
446
447 static inline int
448 user_obj_from_v4(struct nfs4_acl *n4acl, struct list_head **p,
449 struct posix_acl *pacl, struct posix_acl_entry **pace,
450 unsigned int flags)
451 {
452 int error = -EINVAL;
453 struct nfs4_ace *ace, *ace2;
454
455 ace = get_next_v4_ace(p, &n4acl->ace_head);
456 if (ace == NULL)
457 goto out;
458 if (ace2type(ace) != ACL_USER_OBJ)
459 goto out;
460 error = write_pace(ace, pacl, pace, ACL_USER_OBJ, flags);
461 if (error < 0)
462 goto out;
463 error = -EINVAL;
464 ace2 = get_next_v4_ace(p, &n4acl->ace_head);
465 if (ace2 == NULL)
466 goto out;
467 if (!complementary_ace_pair(ace, ace2, flags))
468 goto out;
469 error = 0;
470 out:
471 return error;
472 }
473
474 static inline int
475 users_from_v4(struct nfs4_acl *n4acl, struct list_head **p,
476 struct nfs4_ace **mask_ace,
477 struct posix_acl *pacl, struct posix_acl_entry **pace,
478 unsigned int flags)
479 {
480 int error = -EINVAL;
481 struct nfs4_ace *ace, *ace2;
482
483 ace = get_next_v4_ace(p, &n4acl->ace_head);
484 if (ace == NULL)
485 goto out;
486 while (ace2type(ace) == ACL_USER) {
487 if (ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
488 goto out;
489 if (*mask_ace &&
490 !MASK_EQUAL(ace->access_mask, (*mask_ace)->access_mask))
491 goto out;
492 *mask_ace = ace;
493 ace = get_next_v4_ace(p, &n4acl->ace_head);
494 if (ace == NULL)
495 goto out;
496 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE)
497 goto out;
498 error = write_pace(ace, pacl, pace, ACL_USER, flags);
499 if (error < 0)
500 goto out;
501 error = -EINVAL;
502 ace2 = get_next_v4_ace(p, &n4acl->ace_head);
503 if (ace2 == NULL)
504 goto out;
505 if (!complementary_ace_pair(ace, ace2, flags))
506 goto out;
507 if ((*mask_ace)->flag != ace2->flag ||
508 !same_who(*mask_ace, ace2))
509 goto out;
510 ace = get_next_v4_ace(p, &n4acl->ace_head);
511 if (ace == NULL)
512 goto out;
513 }
514 error = 0;
515 out:
516 return error;
517 }
518
519 static inline int
520 group_obj_and_groups_from_v4(struct nfs4_acl *n4acl, struct list_head **p,
521 struct nfs4_ace **mask_ace,
522 struct posix_acl *pacl, struct posix_acl_entry **pace,
523 unsigned int flags)
524 {
525 int error = -EINVAL;
526 struct nfs4_ace *ace, *ace2;
527 struct ace_container *ac;
528 struct list_head group_l;
529
530 INIT_LIST_HEAD(&group_l);
531 ace = list_entry(*p, struct nfs4_ace, l_ace);
532
533 /* group owner (mask and allow aces) */
534
535 if (pacl->a_count != 3) {
536 /* then the group owner should be preceded by mask */
537 if (ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
538 goto out;
539 if (*mask_ace &&
540 !MASK_EQUAL(ace->access_mask, (*mask_ace)->access_mask))
541 goto out;
542 *mask_ace = ace;
543 ace = get_next_v4_ace(p, &n4acl->ace_head);
544 if (ace == NULL)
545 goto out;
546
547 if ((*mask_ace)->flag != ace->flag || !same_who(*mask_ace, ace))
548 goto out;
549 }
550
551 if (ace2type(ace) != ACL_GROUP_OBJ)
552 goto out;
553
554 ac = kmalloc(sizeof(*ac), GFP_KERNEL);
555 error = -ENOMEM;
556 if (ac == NULL)
557 goto out;
558 ac->ace = ace;
559 list_add_tail(&ac->ace_l, &group_l);
560
561 error = -EINVAL;
562 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE)
563 goto out;
564
565 error = write_pace(ace, pacl, pace, ACL_GROUP_OBJ, flags);
566 if (error < 0)
567 goto out;
568
569 error = -EINVAL;
570 ace = get_next_v4_ace(p, &n4acl->ace_head);
571 if (ace == NULL)
572 goto out;
573
574 /* groups (mask and allow aces) */
575
576 while (ace2type(ace) == ACL_GROUP) {
577 if (*mask_ace == NULL)
578 goto out;
579
580 if (ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE ||
581 !MASK_EQUAL(ace->access_mask, (*mask_ace)->access_mask))
582 goto out;
583 *mask_ace = ace;
584
585 ace = get_next_v4_ace(p, &n4acl->ace_head);
586 if (ace == NULL)
587 goto out;
588 ac = kmalloc(sizeof(*ac), GFP_KERNEL);
589 error = -ENOMEM;
590 if (ac == NULL)
591 goto out;
592 error = -EINVAL;
593 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE ||
594 !same_who(ace, *mask_ace))
595 goto out;
596
597 ac->ace = ace;
598 list_add_tail(&ac->ace_l, &group_l);
599
600 error = write_pace(ace, pacl, pace, ACL_GROUP, flags);
601 if (error < 0)
602 goto out;
603 error = -EINVAL;
604 ace = get_next_v4_ace(p, &n4acl->ace_head);
605 if (ace == NULL)
606 goto out;
607 }
608
609 /* group owner (deny ace) */
610
611 if (ace2type(ace) != ACL_GROUP_OBJ)
612 goto out;
613 ac = list_entry(group_l.next, struct ace_container, ace_l);
614 ace2 = ac->ace;
615 if (!complementary_ace_pair(ace2, ace, flags))
616 goto out;
617 list_del(group_l.next);
618 kfree(ac);
619
620 /* groups (deny aces) */
621
622 while (!list_empty(&group_l)) {
623 ace = get_next_v4_ace(p, &n4acl->ace_head);
624 if (ace == NULL)
625 goto out;
626 if (ace2type(ace) != ACL_GROUP)
627 goto out;
628 ac = list_entry(group_l.next, struct ace_container, ace_l);
629 ace2 = ac->ace;
630 if (!complementary_ace_pair(ace2, ace, flags))
631 goto out;
632 list_del(group_l.next);
633 kfree(ac);
634 }
635
636 ace = get_next_v4_ace(p, &n4acl->ace_head);
637 if (ace == NULL)
638 goto out;
639 if (ace2type(ace) != ACL_OTHER)
640 goto out;
641 error = 0;
642 out:
643 while (!list_empty(&group_l)) {
644 ac = list_entry(group_l.next, struct ace_container, ace_l);
645 list_del(group_l.next);
646 kfree(ac);
647 }
648 return error;
649 }
650
651 static inline int
652 mask_from_v4(struct nfs4_acl *n4acl, struct list_head **p,
653 struct nfs4_ace **mask_ace,
654 struct posix_acl *pacl, struct posix_acl_entry **pace,
655 unsigned int flags)
656 {
657 int error = -EINVAL;
658 struct nfs4_ace *ace;
659
660 ace = list_entry(*p, struct nfs4_ace, l_ace);
661 if (pacl->a_count != 3) {
662 if (*mask_ace == NULL)
663 goto out;
664 (*mask_ace)->access_mask = deny_mask((*mask_ace)->access_mask, flags);
665 write_pace(*mask_ace, pacl, pace, ACL_MASK, flags);
666 }
667 error = 0;
668 out:
669 return error;
670 }
671
672 static inline int
673 other_from_v4(struct nfs4_acl *n4acl, struct list_head **p,
674 struct posix_acl *pacl, struct posix_acl_entry **pace,
675 unsigned int flags)
676 {
677 int error = -EINVAL;
678 struct nfs4_ace *ace, *ace2;
679
680 ace = list_entry(*p, struct nfs4_ace, l_ace);
681 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE)
682 goto out;
683 error = write_pace(ace, pacl, pace, ACL_OTHER, flags);
684 if (error < 0)
685 goto out;
686 error = -EINVAL;
687 ace2 = get_next_v4_ace(p, &n4acl->ace_head);
688 if (ace2 == NULL)
689 goto out;
690 if (!complementary_ace_pair(ace, ace2, flags))
691 goto out;
692 error = 0;
693 out:
694 return error;
695 }
696
697 static int
698 calculate_posix_ace_count(struct nfs4_acl *n4acl)
699 {
700 if (n4acl->naces == 6) /* owner, owner group, and other only */
701 return 3;
702 else { /* Otherwise there must be a mask entry. */
703 /* Also, the remaining entries are for named users and
704 * groups, and come in threes (mask, allow, deny): */
705 if (n4acl->naces < 7)
706 return -1;
707 if ((n4acl->naces - 7) % 3)
708 return -1;
709 return 4 + (n4acl->naces - 7)/3;
710 }
711 }
712
713
714 static struct posix_acl *
715 _nfsv4_to_posix_one(struct nfs4_acl *n4acl, unsigned int flags)
716 {
717 struct posix_acl *pacl;
718 int error = -EINVAL, nace = 0;
719 struct list_head *p;
720 struct nfs4_ace *mask_ace = NULL;
721 struct posix_acl_entry *pace;
722
723 nace = calculate_posix_ace_count(n4acl);
724 if (nace < 0)
725 goto out_err;
726
727 pacl = posix_acl_alloc(nace, GFP_KERNEL);
728 error = -ENOMEM;
729 if (pacl == NULL)
730 goto out_err;
731
732 pace = &pacl->a_entries[0];
733 p = &n4acl->ace_head;
734
735 error = user_obj_from_v4(n4acl, &p, pacl, &pace, flags);
736 if (error)
737 goto out_acl;
738
739 error = users_from_v4(n4acl, &p, &mask_ace, pacl, &pace, flags);
740 if (error)
741 goto out_acl;
742
743 error = group_obj_and_groups_from_v4(n4acl, &p, &mask_ace, pacl, &pace,
744 flags);
745 if (error)
746 goto out_acl;
747
748 error = mask_from_v4(n4acl, &p, &mask_ace, pacl, &pace, flags);
749 if (error)
750 goto out_acl;
751 error = other_from_v4(n4acl, &p, pacl, &pace, flags);
752 if (error)
753 goto out_acl;
754
755 error = -EINVAL;
756 if (p->next != &n4acl->ace_head)
757 goto out_acl;
758 if (pace != pacl->a_entries + pacl->a_count)
759 goto out_acl;
760
761 sort_pacl(pacl);
762
763 return pacl;
764 out_acl:
765 posix_acl_release(pacl);
766 out_err:
767 pacl = ERR_PTR(error);
768 return pacl;
769 }
770
771 int
772 nfs4_acl_split(struct nfs4_acl *acl, struct nfs4_acl *dacl)
773 {
774 struct list_head *h, *n;
775 struct nfs4_ace *ace;
776 int error = 0;
777
778 list_for_each_safe(h, n, &acl->ace_head) {
779 ace = list_entry(h, struct nfs4_ace, l_ace);
780
781 if ((ace->flag & NFS4_INHERITANCE_FLAGS)
782 != NFS4_INHERITANCE_FLAGS)
783 continue;
784
785 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
786 ace->access_mask, ace->whotype, ace->who) == -1;
787 if (error < 0)
788 goto out;
789
790 list_del(h);
791 kfree(ace);
792 acl->naces--;
793 }
794
795 out:
796 return error;
797 }
798
799 static short
800 ace2type(struct nfs4_ace *ace)
801 {
802 switch (ace->whotype) {
803 case NFS4_ACL_WHO_NAMED:
804 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
805 ACL_GROUP : ACL_USER);
806 case NFS4_ACL_WHO_OWNER:
807 return ACL_USER_OBJ;
808 case NFS4_ACL_WHO_GROUP:
809 return ACL_GROUP_OBJ;
810 case NFS4_ACL_WHO_EVERYONE:
811 return ACL_OTHER;
812 }
813 BUG();
814 return -1;
815 }
816
817 EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
818 EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
819
820 struct nfs4_acl *
821 nfs4_acl_new(void)
822 {
823 struct nfs4_acl *acl;
824
825 if ((acl = kmalloc(sizeof(*acl), GFP_KERNEL)) == NULL)
826 return NULL;
827
828 acl->naces = 0;
829 INIT_LIST_HEAD(&acl->ace_head);
830
831 return acl;
832 }
833
834 void
835 nfs4_acl_free(struct nfs4_acl *acl)
836 {
837 struct list_head *h;
838 struct nfs4_ace *ace;
839
840 if (!acl)
841 return;
842
843 while (!list_empty(&acl->ace_head)) {
844 h = acl->ace_head.next;
845 list_del(h);
846 ace = list_entry(h, struct nfs4_ace, l_ace);
847 kfree(ace);
848 }
849
850 kfree(acl);
851
852 return;
853 }
854
855 int
856 nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
857 int whotype, uid_t who)
858 {
859 struct nfs4_ace *ace;
860
861 if ((ace = kmalloc(sizeof(*ace), GFP_KERNEL)) == NULL)
862 return -1;
863
864 ace->type = type;
865 ace->flag = flag;
866 ace->access_mask = access_mask;
867 ace->whotype = whotype;
868 ace->who = who;
869
870 list_add_tail(&ace->l_ace, &acl->ace_head);
871 acl->naces++;
872
873 return 0;
874 }
875
876 static struct {
877 char *string;
878 int stringlen;
879 int type;
880 } s2t_map[] = {
881 {
882 .string = "OWNER@",
883 .stringlen = sizeof("OWNER@") - 1,
884 .type = NFS4_ACL_WHO_OWNER,
885 },
886 {
887 .string = "GROUP@",
888 .stringlen = sizeof("GROUP@") - 1,
889 .type = NFS4_ACL_WHO_GROUP,
890 },
891 {
892 .string = "EVERYONE@",
893 .stringlen = sizeof("EVERYONE@") - 1,
894 .type = NFS4_ACL_WHO_EVERYONE,
895 },
896 };
897
898 int
899 nfs4_acl_get_whotype(char *p, u32 len)
900 {
901 int i;
902
903 for (i=0; i < sizeof(s2t_map) / sizeof(*s2t_map); i++) {
904 if (s2t_map[i].stringlen == len &&
905 0 == memcmp(s2t_map[i].string, p, len))
906 return s2t_map[i].type;
907 }
908 return NFS4_ACL_WHO_NAMED;
909 }
910
911 int
912 nfs4_acl_write_who(int who, char *p)
913 {
914 int i;
915
916 for (i=0; i < sizeof(s2t_map) / sizeof(*s2t_map); i++) {
917 if (s2t_map[i].type == who) {
918 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
919 return s2t_map[i].stringlen;
920 }
921 }
922 BUG();
923 return -1;
924 }
925
926 static inline int
927 match_who(struct nfs4_ace *ace, uid_t owner, gid_t group, uid_t who)
928 {
929 switch (ace->whotype) {
930 case NFS4_ACL_WHO_NAMED:
931 return who == ace->who;
932 case NFS4_ACL_WHO_OWNER:
933 return who == owner;
934 case NFS4_ACL_WHO_GROUP:
935 return who == group;
936 case NFS4_ACL_WHO_EVERYONE:
937 return 1;
938 default:
939 return 0;
940 }
941 }
942
943 /* 0 = granted, -EACCES = denied; mask is an nfsv4 mask, not mode bits */
944 int
945 nfs4_acl_permission(struct nfs4_acl *acl, uid_t owner, gid_t group,
946 uid_t who, u32 mask)
947 {
948 struct nfs4_ace *ace;
949 u32 allowed = 0;
950
951 list_for_each_entry(ace, &acl->ace_head, l_ace) {
952 if (!match_who(ace, group, owner, who))
953 continue;
954 switch (ace->type) {
955 case NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE:
956 allowed |= ace->access_mask;
957 if ((allowed & mask) == mask)
958 return 0;
959 break;
960 case NFS4_ACE_ACCESS_DENIED_ACE_TYPE:
961 if (ace->access_mask & mask)
962 return -EACCES;
963 break;
964 }
965 }
966 return -EACCES;
967 }
968
969 EXPORT_SYMBOL(nfs4_acl_new);
970 EXPORT_SYMBOL(nfs4_acl_free);
971 EXPORT_SYMBOL(nfs4_acl_add_ace);
972 EXPORT_SYMBOL(nfs4_acl_get_whotype);
973 EXPORT_SYMBOL(nfs4_acl_write_who);
974 EXPORT_SYMBOL(nfs4_acl_permission);
975
|
This page was automatically generated by the
LXR engine.
|