blob: 105544eac9a32a84cad6621743ac3d9cf7d4289b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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)
53#define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | 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/* We don't support these bits; insist they be neither allowed nor denied */
59#define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \
60 | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS)
61
62/* flags used to simulate posix default ACLs */
63#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \
64 | NFS4_ACE_DIRECTORY_INHERIT_ACE | NFS4_ACE_INHERIT_ONLY_ACE)
65
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -070066#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS | NFS4_ACE_IDENTIFIER_GROUP)
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define MASK_EQUAL(mask1, mask2) \
69 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
70
71static u32
72mask_from_posix(unsigned short perm, unsigned int flags)
73{
74 int mask = NFS4_ANYONE_MODE;
75
76 if (flags & NFS4_ACL_OWNER)
77 mask |= NFS4_OWNER_MODE;
78 if (perm & ACL_READ)
79 mask |= NFS4_READ_MODE;
80 if (perm & ACL_WRITE)
81 mask |= NFS4_WRITE_MODE;
82 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
83 mask |= NFS4_ACE_DELETE_CHILD;
84 if (perm & ACL_EXECUTE)
85 mask |= NFS4_EXECUTE_MODE;
86 return mask;
87}
88
89static u32
90deny_mask(u32 allow_mask, unsigned int flags)
91{
92 u32 ret = ~allow_mask & ~NFS4_MASK_UNSUPP;
93 if (!(flags & NFS4_ACL_DIR))
94 ret &= ~NFS4_ACE_DELETE_CHILD;
95 return ret;
96}
97
98/* XXX: modify functions to return NFS errors; they're only ever
99 * used by nfs code, after all.... */
100
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700101/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
102 * side of being more restrictive, so the mode bit mapping below is
103 * pessimistic. An optimistic version would be needed to handle DENY's,
104 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
105 * bits. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700107static void
108low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
109{
110 u32 write_mode = NFS4_WRITE_MODE;
111
112 if (flags & NFS4_ACL_DIR)
113 write_mode |= NFS4_ACE_DELETE_CHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 *mode = 0;
115 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
116 *mode |= ACL_READ;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700117 if ((perm & write_mode) == write_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *mode |= ACL_WRITE;
119 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
120 *mode |= ACL_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123struct ace_container {
124 struct nfs4_ace *ace;
125 struct list_head ace_l;
126};
127
128static short ace2type(struct nfs4_ace *);
129static int _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, unsigned int);
130static struct posix_acl *_nfsv4_to_posix_one(struct nfs4_acl *, unsigned int);
131int nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);
NeilBrownfd39ca92005-06-23 22:04:03 -0700132static int nfs4_acl_split(struct nfs4_acl *, struct nfs4_acl *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134struct nfs4_acl *
135nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
136 unsigned int flags)
137{
138 struct nfs4_acl *acl;
139 int error = -EINVAL;
140
141 if ((pacl != NULL &&
142 (posix_acl_valid(pacl) < 0 || pacl->a_count == 0)) ||
143 (dpacl != NULL &&
144 (posix_acl_valid(dpacl) < 0 || dpacl->a_count == 0)))
145 goto out_err;
146
147 acl = nfs4_acl_new();
148 if (acl == NULL) {
149 error = -ENOMEM;
150 goto out_err;
151 }
152
153 if (pacl != NULL) {
154 error = _posix_to_nfsv4_one(pacl, acl,
155 flags & ~NFS4_ACL_TYPE_DEFAULT);
156 if (error < 0)
157 goto out_acl;
158 }
159
160 if (dpacl != NULL) {
161 error = _posix_to_nfsv4_one(dpacl, acl,
162 flags | NFS4_ACL_TYPE_DEFAULT);
163 if (error < 0)
164 goto out_acl;
165 }
166
167 return acl;
168
169out_acl:
170 nfs4_acl_free(acl);
171out_err:
172 acl = ERR_PTR(error);
173
174 return acl;
175}
176
177static int
178nfs4_acl_add_pair(struct nfs4_acl *acl, int eflag, u32 mask, int whotype,
179 uid_t owner, unsigned int flags)
180{
181 int error;
182
183 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
184 eflag, mask, whotype, owner);
185 if (error < 0)
186 return error;
187 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
188 eflag, deny_mask(mask, flags), whotype, owner);
189 return error;
190}
191
192/* We assume the acl has been verified with posix_acl_valid. */
193static int
194_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
195 unsigned int flags)
196{
197 struct posix_acl_entry *pa, *pe, *group_owner_entry;
198 int error = -EINVAL;
199 u32 mask, mask_mask;
200 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
201 NFS4_INHERITANCE_FLAGS : 0);
202
203 BUG_ON(pacl->a_count < 3);
204 pe = pacl->a_entries + pacl->a_count;
205 pa = pe - 2; /* if mask entry exists, it's second from the last. */
206 if (pa->e_tag == ACL_MASK)
207 mask_mask = deny_mask(mask_from_posix(pa->e_perm, flags), flags);
208 else
209 mask_mask = 0;
210
211 pa = pacl->a_entries;
212 BUG_ON(pa->e_tag != ACL_USER_OBJ);
213 mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
214 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_OWNER, 0, flags);
215 if (error < 0)
216 goto out;
217 pa++;
218
219 while (pa->e_tag == ACL_USER) {
220 mask = mask_from_posix(pa->e_perm, flags);
221 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
222 eflag, mask_mask, NFS4_ACL_WHO_NAMED, pa->e_id);
223 if (error < 0)
224 goto out;
225
226
227 error = nfs4_acl_add_pair(acl, eflag, mask,
228 NFS4_ACL_WHO_NAMED, pa->e_id, flags);
229 if (error < 0)
230 goto out;
231 pa++;
232 }
233
234 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
235 * since a user can be in more than one group. */
236
237 /* allow ACEs */
238
239 if (pacl->a_count > 3) {
240 BUG_ON(pa->e_tag != ACL_GROUP_OBJ);
241 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
242 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
243 NFS4_ACL_WHO_GROUP, 0);
244 if (error < 0)
245 goto out;
246 }
247 group_owner_entry = pa;
248 mask = mask_from_posix(pa->e_perm, flags);
249 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
250 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
251 NFS4_ACL_WHO_GROUP, 0);
252 if (error < 0)
253 goto out;
254 pa++;
255
256 while (pa->e_tag == ACL_GROUP) {
257 mask = mask_from_posix(pa->e_perm, flags);
258 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
259 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
260 NFS4_ACL_WHO_NAMED, pa->e_id);
261 if (error < 0)
262 goto out;
263
264 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
265 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
266 NFS4_ACL_WHO_NAMED, pa->e_id);
267 if (error < 0)
268 goto out;
269 pa++;
270 }
271
272 /* deny ACEs */
273
274 pa = group_owner_entry;
275 mask = mask_from_posix(pa->e_perm, flags);
276 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
277 NFS4_ACE_IDENTIFIER_GROUP | eflag,
278 deny_mask(mask, flags), NFS4_ACL_WHO_GROUP, 0);
279 if (error < 0)
280 goto out;
281 pa++;
282 while (pa->e_tag == ACL_GROUP) {
283 mask = mask_from_posix(pa->e_perm, flags);
284 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
285 NFS4_ACE_IDENTIFIER_GROUP | eflag,
286 deny_mask(mask, flags), NFS4_ACL_WHO_NAMED, pa->e_id);
287 if (error < 0)
288 goto out;
289 pa++;
290 }
291
292 if (pa->e_tag == ACL_MASK)
293 pa++;
294 BUG_ON(pa->e_tag != ACL_OTHER);
295 mask = mask_from_posix(pa->e_perm, flags);
296 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_EVERYONE, 0, flags);
297
298out:
299 return error;
300}
301
302static void
303sort_pacl_range(struct posix_acl *pacl, int start, int end) {
304 int sorted = 0, i;
305 struct posix_acl_entry tmp;
306
307 /* We just do a bubble sort; easy to do in place, and we're not
308 * expecting acl's to be long enough to justify anything more. */
309 while (!sorted) {
310 sorted = 1;
311 for (i = start; i < end; i++) {
312 if (pacl->a_entries[i].e_id
313 > pacl->a_entries[i+1].e_id) {
314 sorted = 0;
315 tmp = pacl->a_entries[i];
316 pacl->a_entries[i] = pacl->a_entries[i+1];
317 pacl->a_entries[i+1] = tmp;
318 }
319 }
320 }
321}
322
323static void
324sort_pacl(struct posix_acl *pacl)
325{
326 /* posix_acl_valid requires that users and groups be in order
327 * by uid/gid. */
328 int i, j;
329
330 if (pacl->a_count <= 4)
331 return; /* no users or groups */
332 i = 1;
333 while (pacl->a_entries[i].e_tag == ACL_USER)
334 i++;
335 sort_pacl_range(pacl, 1, i-1);
336
337 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
338 j = i++;
339 while (pacl->a_entries[j].e_tag == ACL_GROUP)
340 j++;
341 sort_pacl_range(pacl, i, j-1);
342 return;
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345int
346nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
347 struct posix_acl **dpacl, unsigned int flags)
348{
349 struct nfs4_acl *dacl;
350 int error = -ENOMEM;
351
352 *pacl = NULL;
353 *dpacl = NULL;
354
355 dacl = nfs4_acl_new();
356 if (dacl == NULL)
357 goto out;
358
359 error = nfs4_acl_split(acl, dacl);
360 if (error < 0)
361 goto out_acl;
362
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700363 if (acl->naces == 0) {
364 error = -ENODATA;
365 goto try_dpacl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700368 *pacl = _nfsv4_to_posix_one(acl, flags);
369 if (IS_ERR(*pacl)) {
370 error = PTR_ERR(*pacl);
371 *pacl = NULL;
372 goto out_acl;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374try_dpacl:
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700375 if (dacl->naces == 0) {
376 if (pacl == NULL || *pacl == NULL)
377 error = -ENODATA;
378 goto out_acl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700381 error = 0;
382 *dpacl = _nfsv4_to_posix_one(dacl, flags);
383 if (IS_ERR(*dpacl)) {
384 error = PTR_ERR(*dpacl);
385 *dpacl = NULL;
386 goto out_acl;
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388out_acl:
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700389 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 posix_acl_release(*pacl);
391 *pacl = NULL;
392 }
393 nfs4_acl_free(dacl);
394out:
395 return error;
396}
397
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700398/*
399 * While processing the NFSv4 ACE, this maintains bitmasks representing
400 * which permission bits have been allowed and which denied to a given
401 * entity: */
402struct posix_ace_state {
403 u32 allow;
404 u32 deny;
405};
406
407struct posix_user_ace_state {
408 uid_t uid;
409 struct posix_ace_state perms;
410};
411
412struct posix_ace_state_array {
413 int n;
414 struct posix_user_ace_state aces[];
415};
416
417/*
418 * While processing the NFSv4 ACE, this maintains the partial permissions
419 * calculated so far: */
420
421struct posix_acl_state {
422 struct posix_ace_state owner;
423 struct posix_ace_state group;
424 struct posix_ace_state other;
425 struct posix_ace_state everyone;
426 struct posix_ace_state mask; /* Deny unused in this case */
427 struct posix_ace_state_array *users;
428 struct posix_ace_state_array *groups;
429};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700432init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700434 int alloc;
435
436 memset(state, 0, sizeof(struct posix_acl_state));
437 /*
438 * In the worst case, each individual acl could be for a distinct
439 * named user or group, but we don't no which, so we allocate
440 * enough space for either:
441 */
442 alloc = sizeof(struct posix_ace_state_array)
443 + cnt*sizeof(struct posix_ace_state);
444 state->users = kzalloc(alloc, GFP_KERNEL);
445 if (!state->users)
446 return -ENOMEM;
447 state->groups = kzalloc(alloc, GFP_KERNEL);
448 if (!state->groups) {
449 kfree(state->users);
450 return -ENOMEM;
451 }
452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700455static void
456free_state(struct posix_acl_state *state) {
457 kfree(state->users);
458 kfree(state->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700461static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700463 state->mask.allow |= astate->allow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700466/*
467 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
468 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
469 * to traditional read/write/execute permissions.
470 *
471 * It's problematic to reject acls that use certain mode bits, because it
472 * places the burden on users to learn the rules about which bits one
473 * particular server sets, without giving the user a lot of help--we return an
474 * error that could mean any number of different things. To make matters
475 * worse, the problematic bits might be introduced by some application that's
476 * automatically mapping from some other acl model.
477 *
478 * So wherever possible we accept anything, possibly erring on the side of
479 * denying more permissions than necessary.
480 *
481 * However we do reject *explicit* DENY's of a few bits representing
482 * permissions we could never deny:
483 */
484
485static inline int check_deny(u32 mask, int isowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700487 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
488 return -EINVAL;
489 if (!isowner)
490 return 0;
491 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
492 return -EINVAL;
493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700496static struct posix_acl *
497posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700499 struct posix_acl_entry *pace;
500 struct posix_acl *pacl;
501 int nace;
502 int i, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700504 nace = 4 + state->users->n + state->groups->n;
505 pacl = posix_acl_alloc(nace, GFP_KERNEL);
506 if (!pacl)
507 return ERR_PTR(-ENOMEM);
508
509 pace = pacl->a_entries;
510 pace->e_tag = ACL_USER_OBJ;
511 error = check_deny(state->owner.deny, 1);
512 if (error)
513 goto out_err;
514 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
515 pace->e_id = ACL_UNDEFINED_ID;
516
517 for (i=0; i < state->users->n; i++) {
518 pace++;
519 pace->e_tag = ACL_USER;
520 error = check_deny(state->users->aces[i].perms.deny, 0);
521 if (error)
522 goto out_err;
523 low_mode_from_nfs4(state->users->aces[i].perms.allow,
524 &pace->e_perm, flags);
525 pace->e_id = state->users->aces[i].uid;
526 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700528
529 pace++;
530 pace->e_tag = ACL_GROUP_OBJ;
531 error = check_deny(state->group.deny, 0);
532 if (error)
533 goto out_err;
534 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
535 pace->e_id = ACL_UNDEFINED_ID;
536 add_to_mask(state, &state->group);
537
538 for (i=0; i < state->groups->n; i++) {
539 pace++;
540 pace->e_tag = ACL_GROUP;
541 error = check_deny(state->groups->aces[i].perms.deny, 0);
542 if (error)
543 goto out_err;
544 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
545 &pace->e_perm, flags);
546 pace->e_id = state->groups->aces[i].uid;
547 add_to_mask(state, &state->groups->aces[i].perms);
548 }
549
550 pace++;
551 pace->e_tag = ACL_MASK;
552 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
553 pace->e_id = ACL_UNDEFINED_ID;
554
555 pace++;
556 pace->e_tag = ACL_OTHER;
557 error = check_deny(state->other.deny, 0);
558 if (error)
559 goto out_err;
560 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
561 pace->e_id = ACL_UNDEFINED_ID;
562
563 return pacl;
564out_err:
565 posix_acl_release(pacl);
566 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700569static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700571 /* Allow all bits in the mask not already denied: */
572 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700575static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700577 /* Deny all bits in the mask not already allowed: */
578 astate->deny |= mask & ~astate->allow;
579}
580
581static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
582{
583 int i;
584
585 for (i = 0; i < a->n; i++)
586 if (a->aces[i].uid == uid)
587 return i;
588 /* Not found: */
589 a->n++;
590 a->aces[i].uid = uid;
591 a->aces[i].perms.allow = state->everyone.allow;
592 a->aces[i].perms.deny = state->everyone.deny;
593
594 return i;
595}
596
597static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
598{
599 int i;
600
601 for (i=0; i < a->n; i++)
602 deny_bits(&a->aces[i].perms, mask);
603}
604
605static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
606{
607 int i;
608
609 for (i=0; i < a->n; i++)
610 allow_bits(&a->aces[i].perms, mask);
611}
612
613static void process_one_v4_ace(struct posix_acl_state *state,
614 struct nfs4_ace *ace)
615{
616 u32 mask = ace->access_mask;
617 int i;
618
619 switch (ace2type(ace)) {
620 case ACL_USER_OBJ:
621 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
622 allow_bits(&state->owner, mask);
623 } else {
624 deny_bits(&state->owner, mask);
625 }
626 break;
627 case ACL_USER:
628 i = find_uid(state, state->users, ace->who);
629 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
630 allow_bits(&state->users->aces[i].perms, mask);
631 } else {
632 deny_bits(&state->users->aces[i].perms, mask);
633 mask = state->users->aces[i].perms.deny;
634 deny_bits(&state->owner, mask);
635 }
636 break;
637 case ACL_GROUP_OBJ:
638 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
639 allow_bits(&state->group, mask);
640 } else {
641 deny_bits(&state->group, mask);
642 mask = state->group.deny;
643 deny_bits(&state->owner, mask);
644 deny_bits(&state->everyone, mask);
645 deny_bits_array(state->users, mask);
646 deny_bits_array(state->groups, mask);
647 }
648 break;
649 case ACL_GROUP:
650 i = find_uid(state, state->groups, ace->who);
651 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
652 allow_bits(&state->groups->aces[i].perms, mask);
653 } else {
654 deny_bits(&state->groups->aces[i].perms, mask);
655 mask = state->groups->aces[i].perms.deny;
656 deny_bits(&state->owner, mask);
657 deny_bits(&state->group, mask);
658 deny_bits(&state->everyone, mask);
659 deny_bits_array(state->users, mask);
660 deny_bits_array(state->groups, mask);
661 }
662 break;
663 case ACL_OTHER:
664 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
665 allow_bits(&state->owner, mask);
666 allow_bits(&state->group, mask);
667 allow_bits(&state->other, mask);
668 allow_bits(&state->everyone, mask);
669 allow_bits_array(state->users, mask);
670 allow_bits_array(state->groups, mask);
671 } else {
672 deny_bits(&state->owner, mask);
673 deny_bits(&state->group, mask);
674 deny_bits(&state->other, mask);
675 deny_bits(&state->everyone, mask);
676 deny_bits_array(state->users, mask);
677 deny_bits_array(state->groups, mask);
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680}
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static struct posix_acl *
683_nfsv4_to_posix_one(struct nfs4_acl *n4acl, unsigned int flags)
684{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700685 struct posix_acl_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct posix_acl *pacl;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700687 struct nfs4_ace *ace;
688 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700690 ret = init_state(&state, n4acl->naces);
691 if (ret)
692 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700694 list_for_each_entry(ace, &n4acl->ace_head, l_ace)
695 process_one_v4_ace(&state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700697 pacl = posix_state_to_acl(&state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700699 free_state(&state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700701 if (!IS_ERR(pacl))
702 sort_pacl(pacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return pacl;
704}
705
NeilBrownfd39ca92005-06-23 22:04:03 -0700706static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707nfs4_acl_split(struct nfs4_acl *acl, struct nfs4_acl *dacl)
708{
709 struct list_head *h, *n;
710 struct nfs4_ace *ace;
711 int error = 0;
712
713 list_for_each_safe(h, n, &acl->ace_head) {
714 ace = list_entry(h, struct nfs4_ace, l_ace);
715
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700716 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
717 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
718 return -EINVAL;
719
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700720 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
721 return -EINVAL;
722
723 switch (ace->flag & NFS4_INHERITANCE_FLAGS) {
724 case 0:
725 /* Leave this ace in the effective acl: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 continue;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700727 case NFS4_INHERITANCE_FLAGS:
728 /* Add this ace to the default acl and remove it
729 * from the effective acl: */
730 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
NeilBrown24992052006-04-10 22:55:24 -0700731 ace->access_mask, ace->whotype, ace->who);
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700732 if (error)
733 return error;
734 list_del(h);
735 kfree(ace);
736 acl->naces--;
737 break;
738 case NFS4_INHERITANCE_FLAGS & ~NFS4_ACE_INHERIT_ONLY_ACE:
739 /* Add this ace to the default, but leave it in
740 * the effective acl as well: */
741 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
742 ace->access_mask, ace->whotype, ace->who);
743 if (error)
744 return error;
745 break;
746 default:
747 return -EINVAL;
748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
753static short
754ace2type(struct nfs4_ace *ace)
755{
756 switch (ace->whotype) {
757 case NFS4_ACL_WHO_NAMED:
758 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
759 ACL_GROUP : ACL_USER);
760 case NFS4_ACL_WHO_OWNER:
761 return ACL_USER_OBJ;
762 case NFS4_ACL_WHO_GROUP:
763 return ACL_GROUP_OBJ;
764 case NFS4_ACL_WHO_EVERYONE:
765 return ACL_OTHER;
766 }
767 BUG();
768 return -1;
769}
770
771EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
772EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
773
774struct nfs4_acl *
775nfs4_acl_new(void)
776{
777 struct nfs4_acl *acl;
778
779 if ((acl = kmalloc(sizeof(*acl), GFP_KERNEL)) == NULL)
780 return NULL;
781
782 acl->naces = 0;
783 INIT_LIST_HEAD(&acl->ace_head);
784
785 return acl;
786}
787
788void
789nfs4_acl_free(struct nfs4_acl *acl)
790{
791 struct list_head *h;
792 struct nfs4_ace *ace;
793
794 if (!acl)
795 return;
796
797 while (!list_empty(&acl->ace_head)) {
798 h = acl->ace_head.next;
799 list_del(h);
800 ace = list_entry(h, struct nfs4_ace, l_ace);
801 kfree(ace);
802 }
803
804 kfree(acl);
805
806 return;
807}
808
809int
810nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
811 int whotype, uid_t who)
812{
813 struct nfs4_ace *ace;
814
815 if ((ace = kmalloc(sizeof(*ace), GFP_KERNEL)) == NULL)
NeilBrownb905b7b2006-04-10 22:55:25 -0700816 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 ace->type = type;
819 ace->flag = flag;
820 ace->access_mask = access_mask;
821 ace->whotype = whotype;
822 ace->who = who;
823
824 list_add_tail(&ace->l_ace, &acl->ace_head);
825 acl->naces++;
826
827 return 0;
828}
829
830static struct {
831 char *string;
832 int stringlen;
833 int type;
834} s2t_map[] = {
835 {
836 .string = "OWNER@",
837 .stringlen = sizeof("OWNER@") - 1,
838 .type = NFS4_ACL_WHO_OWNER,
839 },
840 {
841 .string = "GROUP@",
842 .stringlen = sizeof("GROUP@") - 1,
843 .type = NFS4_ACL_WHO_GROUP,
844 },
845 {
846 .string = "EVERYONE@",
847 .stringlen = sizeof("EVERYONE@") - 1,
848 .type = NFS4_ACL_WHO_EVERYONE,
849 },
850};
851
852int
853nfs4_acl_get_whotype(char *p, u32 len)
854{
855 int i;
856
Tobias Klausere8c96f82006-03-24 03:15:34 -0800857 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (s2t_map[i].stringlen == len &&
859 0 == memcmp(s2t_map[i].string, p, len))
860 return s2t_map[i].type;
861 }
862 return NFS4_ACL_WHO_NAMED;
863}
864
865int
866nfs4_acl_write_who(int who, char *p)
867{
868 int i;
869
Tobias Klausere8c96f82006-03-24 03:15:34 -0800870 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (s2t_map[i].type == who) {
872 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
873 return s2t_map[i].stringlen;
874 }
875 }
876 BUG();
877 return -1;
878}
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880EXPORT_SYMBOL(nfs4_acl_new);
881EXPORT_SYMBOL(nfs4_acl_free);
882EXPORT_SYMBOL(nfs4_acl_add_ace);
883EXPORT_SYMBOL(nfs4_acl_get_whotype);
884EXPORT_SYMBOL(nfs4_acl_write_who);