blob: 785418b0b7997dab454de13a43b66cc0c1fd416d [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 \
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080064 | NFS4_ACE_DIRECTORY_INHERIT_ACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
J. Bruce Fields7bdfa682007-02-16 01:28:28 -080066#define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \
67 | NFS4_ACE_INHERIT_ONLY_ACE \
68 | NFS4_ACE_IDENTIFIER_GROUP)
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define MASK_EQUAL(mask1, mask2) \
71 ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )
72
73static u32
74mask_from_posix(unsigned short perm, unsigned int flags)
75{
76 int mask = NFS4_ANYONE_MODE;
77
78 if (flags & NFS4_ACL_OWNER)
79 mask |= NFS4_OWNER_MODE;
80 if (perm & ACL_READ)
81 mask |= NFS4_READ_MODE;
82 if (perm & ACL_WRITE)
83 mask |= NFS4_WRITE_MODE;
84 if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
85 mask |= NFS4_ACE_DELETE_CHILD;
86 if (perm & ACL_EXECUTE)
87 mask |= NFS4_EXECUTE_MODE;
88 return mask;
89}
90
91static u32
92deny_mask(u32 allow_mask, unsigned int flags)
93{
94 u32 ret = ~allow_mask & ~NFS4_MASK_UNSUPP;
95 if (!(flags & NFS4_ACL_DIR))
96 ret &= ~NFS4_ACE_DELETE_CHILD;
97 return ret;
98}
99
100/* XXX: modify functions to return NFS errors; they're only ever
101 * used by nfs code, after all.... */
102
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700103/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the
104 * side of being more restrictive, so the mode bit mapping below is
105 * pessimistic. An optimistic version would be needed to handle DENY's,
106 * but we espect to coalesce all ALLOWs and DENYs before mapping to mode
107 * bits. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700109static void
110low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags)
111{
112 u32 write_mode = NFS4_WRITE_MODE;
113
114 if (flags & NFS4_ACL_DIR)
115 write_mode |= NFS4_ACE_DELETE_CHILD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 *mode = 0;
117 if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)
118 *mode |= ACL_READ;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700119 if ((perm & write_mode) == write_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *mode |= ACL_WRITE;
121 if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)
122 *mode |= ACL_EXECUTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125struct ace_container {
126 struct nfs4_ace *ace;
127 struct list_head ace_l;
128};
129
130static short ace2type(struct nfs4_ace *);
131static int _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, unsigned int);
132static struct posix_acl *_nfsv4_to_posix_one(struct nfs4_acl *, unsigned int);
133int nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);
NeilBrownfd39ca92005-06-23 22:04:03 -0700134static int nfs4_acl_split(struct nfs4_acl *, struct nfs4_acl *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136struct nfs4_acl *
137nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,
138 unsigned int flags)
139{
140 struct nfs4_acl *acl;
141 int error = -EINVAL;
142
143 if ((pacl != NULL &&
144 (posix_acl_valid(pacl) < 0 || pacl->a_count == 0)) ||
145 (dpacl != NULL &&
146 (posix_acl_valid(dpacl) < 0 || dpacl->a_count == 0)))
147 goto out_err;
148
149 acl = nfs4_acl_new();
150 if (acl == NULL) {
151 error = -ENOMEM;
152 goto out_err;
153 }
154
155 if (pacl != NULL) {
156 error = _posix_to_nfsv4_one(pacl, acl,
157 flags & ~NFS4_ACL_TYPE_DEFAULT);
158 if (error < 0)
159 goto out_acl;
160 }
161
162 if (dpacl != NULL) {
163 error = _posix_to_nfsv4_one(dpacl, acl,
164 flags | NFS4_ACL_TYPE_DEFAULT);
165 if (error < 0)
166 goto out_acl;
167 }
168
169 return acl;
170
171out_acl:
172 nfs4_acl_free(acl);
173out_err:
174 acl = ERR_PTR(error);
175
176 return acl;
177}
178
179static int
180nfs4_acl_add_pair(struct nfs4_acl *acl, int eflag, u32 mask, int whotype,
181 uid_t owner, unsigned int flags)
182{
183 int error;
184
185 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
186 eflag, mask, whotype, owner);
187 if (error < 0)
188 return error;
189 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
190 eflag, deny_mask(mask, flags), whotype, owner);
191 return error;
192}
193
194/* We assume the acl has been verified with posix_acl_valid. */
195static int
196_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,
197 unsigned int flags)
198{
199 struct posix_acl_entry *pa, *pe, *group_owner_entry;
200 int error = -EINVAL;
201 u32 mask, mask_mask;
202 int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
203 NFS4_INHERITANCE_FLAGS : 0);
204
205 BUG_ON(pacl->a_count < 3);
206 pe = pacl->a_entries + pacl->a_count;
207 pa = pe - 2; /* if mask entry exists, it's second from the last. */
208 if (pa->e_tag == ACL_MASK)
209 mask_mask = deny_mask(mask_from_posix(pa->e_perm, flags), flags);
210 else
211 mask_mask = 0;
212
213 pa = pacl->a_entries;
214 BUG_ON(pa->e_tag != ACL_USER_OBJ);
215 mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);
216 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_OWNER, 0, flags);
217 if (error < 0)
218 goto out;
219 pa++;
220
221 while (pa->e_tag == ACL_USER) {
222 mask = mask_from_posix(pa->e_perm, flags);
223 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
224 eflag, mask_mask, NFS4_ACL_WHO_NAMED, pa->e_id);
225 if (error < 0)
226 goto out;
227
228
229 error = nfs4_acl_add_pair(acl, eflag, mask,
230 NFS4_ACL_WHO_NAMED, pa->e_id, flags);
231 if (error < 0)
232 goto out;
233 pa++;
234 }
235
236 /* In the case of groups, we apply allow ACEs first, then deny ACEs,
237 * since a user can be in more than one group. */
238
239 /* allow ACEs */
240
241 if (pacl->a_count > 3) {
242 BUG_ON(pa->e_tag != ACL_GROUP_OBJ);
243 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
244 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
245 NFS4_ACL_WHO_GROUP, 0);
246 if (error < 0)
247 goto out;
248 }
249 group_owner_entry = pa;
250 mask = mask_from_posix(pa->e_perm, flags);
251 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
252 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
253 NFS4_ACL_WHO_GROUP, 0);
254 if (error < 0)
255 goto out;
256 pa++;
257
258 while (pa->e_tag == ACL_GROUP) {
259 mask = mask_from_posix(pa->e_perm, flags);
260 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
261 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,
262 NFS4_ACL_WHO_NAMED, pa->e_id);
263 if (error < 0)
264 goto out;
265
266 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,
267 NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,
268 NFS4_ACL_WHO_NAMED, pa->e_id);
269 if (error < 0)
270 goto out;
271 pa++;
272 }
273
274 /* deny ACEs */
275
276 pa = group_owner_entry;
277 mask = mask_from_posix(pa->e_perm, flags);
278 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
279 NFS4_ACE_IDENTIFIER_GROUP | eflag,
280 deny_mask(mask, flags), NFS4_ACL_WHO_GROUP, 0);
281 if (error < 0)
282 goto out;
283 pa++;
284 while (pa->e_tag == ACL_GROUP) {
285 mask = mask_from_posix(pa->e_perm, flags);
286 error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,
287 NFS4_ACE_IDENTIFIER_GROUP | eflag,
288 deny_mask(mask, flags), NFS4_ACL_WHO_NAMED, pa->e_id);
289 if (error < 0)
290 goto out;
291 pa++;
292 }
293
294 if (pa->e_tag == ACL_MASK)
295 pa++;
296 BUG_ON(pa->e_tag != ACL_OTHER);
297 mask = mask_from_posix(pa->e_perm, flags);
298 error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_EVERYONE, 0, flags);
299
300out:
301 return error;
302}
303
304static void
305sort_pacl_range(struct posix_acl *pacl, int start, int end) {
306 int sorted = 0, i;
307 struct posix_acl_entry tmp;
308
309 /* We just do a bubble sort; easy to do in place, and we're not
310 * expecting acl's to be long enough to justify anything more. */
311 while (!sorted) {
312 sorted = 1;
313 for (i = start; i < end; i++) {
314 if (pacl->a_entries[i].e_id
315 > pacl->a_entries[i+1].e_id) {
316 sorted = 0;
317 tmp = pacl->a_entries[i];
318 pacl->a_entries[i] = pacl->a_entries[i+1];
319 pacl->a_entries[i+1] = tmp;
320 }
321 }
322 }
323}
324
325static void
326sort_pacl(struct posix_acl *pacl)
327{
328 /* posix_acl_valid requires that users and groups be in order
329 * by uid/gid. */
330 int i, j;
331
332 if (pacl->a_count <= 4)
333 return; /* no users or groups */
334 i = 1;
335 while (pacl->a_entries[i].e_tag == ACL_USER)
336 i++;
337 sort_pacl_range(pacl, 1, i-1);
338
339 BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
340 j = i++;
341 while (pacl->a_entries[j].e_tag == ACL_GROUP)
342 j++;
343 sort_pacl_range(pacl, i, j-1);
344 return;
345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347int
348nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,
349 struct posix_acl **dpacl, unsigned int flags)
350{
351 struct nfs4_acl *dacl;
352 int error = -ENOMEM;
353
354 *pacl = NULL;
355 *dpacl = NULL;
356
357 dacl = nfs4_acl_new();
358 if (dacl == NULL)
359 goto out;
360
361 error = nfs4_acl_split(acl, dacl);
J.Bruce Fieldsb66285c2006-10-04 02:16:14 -0700362 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto out_acl;
364
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700365 *pacl = _nfsv4_to_posix_one(acl, flags);
366 if (IS_ERR(*pacl)) {
367 error = PTR_ERR(*pacl);
368 *pacl = NULL;
369 goto out_acl;
370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700372 *dpacl = _nfsv4_to_posix_one(dacl, flags);
373 if (IS_ERR(*dpacl)) {
374 error = PTR_ERR(*dpacl);
375 *dpacl = NULL;
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377out_acl:
J.Bruce Fieldsf3b64eb2006-10-04 02:16:13 -0700378 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 posix_acl_release(*pacl);
380 *pacl = NULL;
381 }
382 nfs4_acl_free(dacl);
383out:
384 return error;
385}
386
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700387/*
388 * While processing the NFSv4 ACE, this maintains bitmasks representing
389 * which permission bits have been allowed and which denied to a given
390 * entity: */
391struct posix_ace_state {
392 u32 allow;
393 u32 deny;
394};
395
396struct posix_user_ace_state {
397 uid_t uid;
398 struct posix_ace_state perms;
399};
400
401struct posix_ace_state_array {
402 int n;
403 struct posix_user_ace_state aces[];
404};
405
406/*
407 * While processing the NFSv4 ACE, this maintains the partial permissions
408 * calculated so far: */
409
410struct posix_acl_state {
411 struct posix_ace_state owner;
412 struct posix_ace_state group;
413 struct posix_ace_state other;
414 struct posix_ace_state everyone;
415 struct posix_ace_state mask; /* Deny unused in this case */
416 struct posix_ace_state_array *users;
417 struct posix_ace_state_array *groups;
418};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420static int
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700421init_state(struct posix_acl_state *state, int cnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700423 int alloc;
424
425 memset(state, 0, sizeof(struct posix_acl_state));
426 /*
427 * In the worst case, each individual acl could be for a distinct
428 * named user or group, but we don't no which, so we allocate
429 * enough space for either:
430 */
431 alloc = sizeof(struct posix_ace_state_array)
432 + cnt*sizeof(struct posix_ace_state);
433 state->users = kzalloc(alloc, GFP_KERNEL);
434 if (!state->users)
435 return -ENOMEM;
436 state->groups = kzalloc(alloc, GFP_KERNEL);
437 if (!state->groups) {
438 kfree(state->users);
439 return -ENOMEM;
440 }
441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700444static void
445free_state(struct posix_acl_state *state) {
446 kfree(state->users);
447 kfree(state->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700450static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700452 state->mask.allow |= astate->allow;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700455/*
456 * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS,
457 * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate
458 * to traditional read/write/execute permissions.
459 *
460 * It's problematic to reject acls that use certain mode bits, because it
461 * places the burden on users to learn the rules about which bits one
462 * particular server sets, without giving the user a lot of help--we return an
463 * error that could mean any number of different things. To make matters
464 * worse, the problematic bits might be introduced by some application that's
465 * automatically mapping from some other acl model.
466 *
467 * So wherever possible we accept anything, possibly erring on the side of
468 * denying more permissions than necessary.
469 *
470 * However we do reject *explicit* DENY's of a few bits representing
471 * permissions we could never deny:
472 */
473
474static inline int check_deny(u32 mask, int isowner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700476 if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL))
477 return -EINVAL;
478 if (!isowner)
479 return 0;
480 if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL))
481 return -EINVAL;
482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700485static struct posix_acl *
486posix_state_to_acl(struct posix_acl_state *state, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700488 struct posix_acl_entry *pace;
489 struct posix_acl *pacl;
490 int nace;
491 int i, error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700493 nace = 4 + state->users->n + state->groups->n;
494 pacl = posix_acl_alloc(nace, GFP_KERNEL);
495 if (!pacl)
496 return ERR_PTR(-ENOMEM);
497
498 pace = pacl->a_entries;
499 pace->e_tag = ACL_USER_OBJ;
500 error = check_deny(state->owner.deny, 1);
501 if (error)
502 goto out_err;
503 low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
504 pace->e_id = ACL_UNDEFINED_ID;
505
506 for (i=0; i < state->users->n; i++) {
507 pace++;
508 pace->e_tag = ACL_USER;
509 error = check_deny(state->users->aces[i].perms.deny, 0);
510 if (error)
511 goto out_err;
512 low_mode_from_nfs4(state->users->aces[i].perms.allow,
513 &pace->e_perm, flags);
514 pace->e_id = state->users->aces[i].uid;
515 add_to_mask(state, &state->users->aces[i].perms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700517
518 pace++;
519 pace->e_tag = ACL_GROUP_OBJ;
520 error = check_deny(state->group.deny, 0);
521 if (error)
522 goto out_err;
523 low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags);
524 pace->e_id = ACL_UNDEFINED_ID;
525 add_to_mask(state, &state->group);
526
527 for (i=0; i < state->groups->n; i++) {
528 pace++;
529 pace->e_tag = ACL_GROUP;
530 error = check_deny(state->groups->aces[i].perms.deny, 0);
531 if (error)
532 goto out_err;
533 low_mode_from_nfs4(state->groups->aces[i].perms.allow,
534 &pace->e_perm, flags);
535 pace->e_id = state->groups->aces[i].uid;
536 add_to_mask(state, &state->groups->aces[i].perms);
537 }
538
539 pace++;
540 pace->e_tag = ACL_MASK;
541 low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags);
542 pace->e_id = ACL_UNDEFINED_ID;
543
544 pace++;
545 pace->e_tag = ACL_OTHER;
546 error = check_deny(state->other.deny, 0);
547 if (error)
548 goto out_err;
549 low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags);
550 pace->e_id = ACL_UNDEFINED_ID;
551
552 return pacl;
553out_err:
554 posix_acl_release(pacl);
555 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700558static inline void allow_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700560 /* Allow all bits in the mask not already denied: */
561 astate->allow |= mask & ~astate->deny;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700564static inline void deny_bits(struct posix_ace_state *astate, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700566 /* Deny all bits in the mask not already allowed: */
567 astate->deny |= mask & ~astate->allow;
568}
569
570static int find_uid(struct posix_acl_state *state, struct posix_ace_state_array *a, uid_t uid)
571{
572 int i;
573
574 for (i = 0; i < a->n; i++)
575 if (a->aces[i].uid == uid)
576 return i;
577 /* Not found: */
578 a->n++;
579 a->aces[i].uid = uid;
580 a->aces[i].perms.allow = state->everyone.allow;
581 a->aces[i].perms.deny = state->everyone.deny;
582
583 return i;
584}
585
586static void deny_bits_array(struct posix_ace_state_array *a, u32 mask)
587{
588 int i;
589
590 for (i=0; i < a->n; i++)
591 deny_bits(&a->aces[i].perms, mask);
592}
593
594static void allow_bits_array(struct posix_ace_state_array *a, u32 mask)
595{
596 int i;
597
598 for (i=0; i < a->n; i++)
599 allow_bits(&a->aces[i].perms, mask);
600}
601
602static void process_one_v4_ace(struct posix_acl_state *state,
603 struct nfs4_ace *ace)
604{
605 u32 mask = ace->access_mask;
606 int i;
607
608 switch (ace2type(ace)) {
609 case ACL_USER_OBJ:
610 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
611 allow_bits(&state->owner, mask);
612 } else {
613 deny_bits(&state->owner, mask);
614 }
615 break;
616 case ACL_USER:
617 i = find_uid(state, state->users, ace->who);
618 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
619 allow_bits(&state->users->aces[i].perms, mask);
620 } else {
621 deny_bits(&state->users->aces[i].perms, mask);
622 mask = state->users->aces[i].perms.deny;
623 deny_bits(&state->owner, mask);
624 }
625 break;
626 case ACL_GROUP_OBJ:
627 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
628 allow_bits(&state->group, mask);
629 } else {
630 deny_bits(&state->group, mask);
631 mask = state->group.deny;
632 deny_bits(&state->owner, mask);
633 deny_bits(&state->everyone, mask);
634 deny_bits_array(state->users, mask);
635 deny_bits_array(state->groups, mask);
636 }
637 break;
638 case ACL_GROUP:
639 i = find_uid(state, state->groups, ace->who);
640 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
641 allow_bits(&state->groups->aces[i].perms, mask);
642 } else {
643 deny_bits(&state->groups->aces[i].perms, mask);
644 mask = state->groups->aces[i].perms.deny;
645 deny_bits(&state->owner, mask);
646 deny_bits(&state->group, mask);
647 deny_bits(&state->everyone, mask);
648 deny_bits_array(state->users, mask);
649 deny_bits_array(state->groups, mask);
650 }
651 break;
652 case ACL_OTHER:
653 if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) {
654 allow_bits(&state->owner, mask);
655 allow_bits(&state->group, mask);
656 allow_bits(&state->other, mask);
657 allow_bits(&state->everyone, mask);
658 allow_bits_array(state->users, mask);
659 allow_bits_array(state->groups, mask);
660 } else {
661 deny_bits(&state->owner, mask);
662 deny_bits(&state->group, mask);
663 deny_bits(&state->other, mask);
664 deny_bits(&state->everyone, mask);
665 deny_bits_array(state->users, mask);
666 deny_bits_array(state->groups, mask);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671static struct posix_acl *
672_nfsv4_to_posix_one(struct nfs4_acl *n4acl, unsigned int flags)
673{
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700674 struct posix_acl_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct posix_acl *pacl;
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700676 struct nfs4_ace *ace;
677 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700679 ret = init_state(&state, n4acl->naces);
680 if (ret)
681 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700683 list_for_each_entry(ace, &n4acl->ace_head, l_ace)
684 process_one_v4_ace(&state, ace);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700686 pacl = posix_state_to_acl(&state, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700688 free_state(&state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700690 if (!IS_ERR(pacl))
691 sort_pacl(pacl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return pacl;
693}
694
NeilBrownfd39ca92005-06-23 22:04:03 -0700695static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696nfs4_acl_split(struct nfs4_acl *acl, struct nfs4_acl *dacl)
697{
698 struct list_head *h, *n;
699 struct nfs4_ace *ace;
700 int error = 0;
701
702 list_for_each_safe(h, n, &acl->ace_head) {
703 ace = list_entry(h, struct nfs4_ace, l_ace);
704
J.Bruce Fields09229ed2006-10-04 02:16:11 -0700705 if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
706 ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)
707 return -EINVAL;
708
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700709 if (ace->flag & ~NFS4_SUPPORTED_FLAGS)
710 return -EINVAL;
711
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800712 if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700713 /* Leave this ace in the effective acl: */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 continue;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800715 }
716 /*
717 * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT
718 * is set, we're effectively turning on the other. That's OK,
719 * according to rfc 3530.
720 */
721 if (ace->flag & NFS4_ACE_INHERIT_ONLY_ACE) {
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700722 /* Add this ace to the default acl and remove it
723 * from the effective acl: */
724 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
NeilBrown24992052006-04-10 22:55:24 -0700725 ace->access_mask, ace->whotype, ace->who);
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700726 if (error)
727 return error;
728 list_del(h);
729 kfree(ace);
730 acl->naces--;
J. Bruce Fields7bdfa682007-02-16 01:28:28 -0800731 } else {
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700732 /* Add this ace to the default, but leave it in
733 * the effective acl as well: */
734 error = nfs4_acl_add_ace(dacl, ace->type, ace->flag,
735 ace->access_mask, ace->whotype, ace->who);
736 if (error)
737 return error;
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
J.Bruce Fieldsb548edc2006-10-04 02:16:12 -0700740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static short
744ace2type(struct nfs4_ace *ace)
745{
746 switch (ace->whotype) {
747 case NFS4_ACL_WHO_NAMED:
748 return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
749 ACL_GROUP : ACL_USER);
750 case NFS4_ACL_WHO_OWNER:
751 return ACL_USER_OBJ;
752 case NFS4_ACL_WHO_GROUP:
753 return ACL_GROUP_OBJ;
754 case NFS4_ACL_WHO_EVERYONE:
755 return ACL_OTHER;
756 }
757 BUG();
758 return -1;
759}
760
761EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4);
762EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix);
763
764struct nfs4_acl *
765nfs4_acl_new(void)
766{
767 struct nfs4_acl *acl;
768
769 if ((acl = kmalloc(sizeof(*acl), GFP_KERNEL)) == NULL)
770 return NULL;
771
772 acl->naces = 0;
773 INIT_LIST_HEAD(&acl->ace_head);
774
775 return acl;
776}
777
778void
779nfs4_acl_free(struct nfs4_acl *acl)
780{
781 struct list_head *h;
782 struct nfs4_ace *ace;
783
784 if (!acl)
785 return;
786
787 while (!list_empty(&acl->ace_head)) {
788 h = acl->ace_head.next;
789 list_del(h);
790 ace = list_entry(h, struct nfs4_ace, l_ace);
791 kfree(ace);
792 }
793
794 kfree(acl);
795
796 return;
797}
798
799int
800nfs4_acl_add_ace(struct nfs4_acl *acl, u32 type, u32 flag, u32 access_mask,
801 int whotype, uid_t who)
802{
803 struct nfs4_ace *ace;
804
805 if ((ace = kmalloc(sizeof(*ace), GFP_KERNEL)) == NULL)
NeilBrownb905b7b2006-04-10 22:55:25 -0700806 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 ace->type = type;
809 ace->flag = flag;
810 ace->access_mask = access_mask;
811 ace->whotype = whotype;
812 ace->who = who;
813
814 list_add_tail(&ace->l_ace, &acl->ace_head);
815 acl->naces++;
816
817 return 0;
818}
819
820static struct {
821 char *string;
822 int stringlen;
823 int type;
824} s2t_map[] = {
825 {
826 .string = "OWNER@",
827 .stringlen = sizeof("OWNER@") - 1,
828 .type = NFS4_ACL_WHO_OWNER,
829 },
830 {
831 .string = "GROUP@",
832 .stringlen = sizeof("GROUP@") - 1,
833 .type = NFS4_ACL_WHO_GROUP,
834 },
835 {
836 .string = "EVERYONE@",
837 .stringlen = sizeof("EVERYONE@") - 1,
838 .type = NFS4_ACL_WHO_EVERYONE,
839 },
840};
841
842int
843nfs4_acl_get_whotype(char *p, u32 len)
844{
845 int i;
846
Tobias Klausere8c96f82006-03-24 03:15:34 -0800847 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (s2t_map[i].stringlen == len &&
849 0 == memcmp(s2t_map[i].string, p, len))
850 return s2t_map[i].type;
851 }
852 return NFS4_ACL_WHO_NAMED;
853}
854
855int
856nfs4_acl_write_who(int who, char *p)
857{
858 int i;
859
Tobias Klausere8c96f82006-03-24 03:15:34 -0800860 for (i = 0; i < ARRAY_SIZE(s2t_map); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (s2t_map[i].type == who) {
862 memcpy(p, s2t_map[i].string, s2t_map[i].stringlen);
863 return s2t_map[i].stringlen;
864 }
865 }
866 BUG();
867 return -1;
868}
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870EXPORT_SYMBOL(nfs4_acl_new);
871EXPORT_SYMBOL(nfs4_acl_free);
872EXPORT_SYMBOL(nfs4_acl_add_ace);
873EXPORT_SYMBOL(nfs4_acl_get_whotype);
874EXPORT_SYMBOL(nfs4_acl_write_who);