/* * Common NFSv4 ACL handling code. * * Copyright (c) 2002, 2003 The Regents of the University of Michigan. * All rights reserved. * * Marius Aamodt Eriksen <marius@umich.edu> * Jeff Sedlak <jsedlak@umich.edu> * J. Bruce Fields <bfields@umich.edu> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
if (perm & ACL_READ)
mask |= NFS4_READ_MODE; if (perm & ACL_WRITE)
mask |= NFS4_WRITE_MODE; if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR))
mask |= NFS4_ACE_DELETE_CHILD; if (perm & ACL_EXECUTE)
mask |= NFS4_EXECUTE_MODE; return mask;
}
/* XXX: modify functions to return NFS errors; they're only ever
* used by nfs code, after all.... */
/* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the * side of being more restrictive, so the mode bit mapping below is * pessimistic. An optimistic version would be needed to handle DENY's, * but we expect to coalesce all ALLOWs and DENYs before mapping to mode
* bits. */
/* * Only pas.users and pas.groups need initialization; previous * posix_acl_valid() calls ensure that the other fields will be * initialized in the following loop. But, just to placate gcc:
*/
memset(pas, 0, sizeof(*pas));
pas->mask = 07;
FOREACH_ACL_ENTRY(pa, acl, pe) { switch (pa->e_tag) { case ACL_USER_OBJ:
pas->owner = pa->e_perm; break; case ACL_GROUP_OBJ:
pas->group = pa->e_perm; break; case ACL_USER:
pas->users |= pa->e_perm; break; case ACL_GROUP:
pas->groups |= pa->e_perm; break; case ACL_OTHER:
pas->other = pa->e_perm; break; case ACL_MASK:
pas->mask = pa->e_perm; break;
}
} /* We'll only care about effective permissions: */
pas->users &= pas->mask;
pas->group &= pas->mask;
pas->groups &= pas->mask;
}
/* We assume the acl has been verified with posix_acl_valid. */ staticvoid
_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl, unsignedint flags)
{ struct posix_acl_entry *pa, *group_owner_entry; struct nfs4_ace *ace; struct posix_acl_summary pas; unsignedshort deny; int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?
NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0);
pa = pacl->a_entries;
ace = acl->aces + acl->naces;
/* We could deny everything not granted by the owner: */
deny = ~pas.owner; /* * but it is equivalent (and simpler) to deny only what is not * granted by later entries:
*/
deny &= pas.users | pas.group | pas.groups | pas.other; if (deny) {
ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE;
ace->flag = eflag;
ace->access_mask = deny_mask_from_posix(deny, flags);
ace->whotype = NFS4_ACL_WHO_OWNER;
ace++;
acl->naces++;
}
staticvoid
sort_pacl_range(struct posix_acl *pacl, int start, int end) { int sorted = 0, i;
/* We just do a bubble sort; easy to do in place, and we're not
* expecting acl's to be long enough to justify anything more. */ while (!sorted) {
sorted = 1; for (i = start; i < end; i++) { if (pace_gt(&pacl->a_entries[i],
&pacl->a_entries[i+1])) {
sorted = 0;
swap(pacl->a_entries[i],
pacl->a_entries[i + 1]);
}
}
}
}
staticvoid
sort_pacl(struct posix_acl *pacl)
{ /* posix_acl_valid requires that users and groups be in order
* by uid/gid. */ int i, j;
/* no users or groups */ if (!pacl || pacl->a_count <= 4) return;
i = 1; while (pacl->a_entries[i].e_tag == ACL_USER)
i++;
sort_pacl_range(pacl, 1, i-1);
BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ);
j = ++i; while (pacl->a_entries[j].e_tag == ACL_GROUP)
j++;
sort_pacl_range(pacl, i, j-1); return;
}
/* * While processing the NFSv4 ACE, this maintains bitmasks representing * which permission bits have been allowed and which denied to a given
* entity: */ struct posix_ace_state {
u32 allow;
u32 deny;
};
staticint
init_state(struct posix_acl_state *state, int cnt)
{ int alloc;
memset(state, 0, sizeof(struct posix_acl_state)); /* * In the worst case, each individual acl could be for a distinct * named user or group, but we don't know which, so we allocate * enough space for either:
*/
alloc = sizeof(struct posix_ace_state_array)
+ cnt*sizeof(struct posix_user_ace_state);
state->users = kzalloc(alloc, GFP_KERNEL); if (!state->users) return -ENOMEM;
state->groups = kzalloc(alloc, GFP_KERNEL); if (!state->groups) {
kfree(state->users); return -ENOMEM;
} return 0;
}
staticstruct posix_acl *
posix_state_to_acl(struct posix_acl_state *state, unsignedint flags)
{ struct posix_acl_entry *pace; struct posix_acl *pacl; int nace; int i;
/* * ACLs with no ACEs are treated differently in the inheritable * and effective cases: when there are no inheritable ACEs, * calls ->set_acl with a NULL ACL structure.
*/ if (!state->valid && (flags & NFS4_ACL_TYPE_DEFAULT)) return NULL;
/* * When there are no effective ACEs, the following will end * up setting a 3-element effective posix ACL with all * permissions zero.
*/ if (!state->users->n && !state->groups->n)
nace = 3; else/* Note we also include a MASK ACE in this case: */
nace = 4 + state->users->n + state->groups->n;
pacl = posix_acl_alloc(nace, GFP_KERNEL); if (!pacl) return ERR_PTR(-ENOMEM);
pace = pacl->a_entries;
pace->e_tag = ACL_USER_OBJ;
low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags);
for (i=0; i < state->users->n; i++) {
pace++;
pace->e_tag = ACL_USER;
low_mode_from_nfs4(state->users->aces[i].perms.allow,
&pace->e_perm, flags);
pace->e_uid = state->users->aces[i].uid;
add_to_mask(state, &state->users->aces[i].perms);
}
ret = init_state(&effective_acl_state, acl->naces); if (ret) return ret;
ret = init_state(&default_acl_state, acl->naces); if (ret) goto out_estate;
ret = -EINVAL; for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) { if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&
ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE) goto out_dstate; if (ace->flag & ~NFS4_SUPPORTED_FLAGS) goto out_dstate; if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) {
process_one_v4_ace(&effective_acl_state, ace); continue;
} if (!(flags & NFS4_ACL_DIR)) goto out_dstate; /* * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT * is set, we're effectively turning on the other. That's OK, * according to rfc 3530.
*/
process_one_v4_ace(&default_acl_state, ace);
if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE))
process_one_v4_ace(&effective_acl_state, ace);
}
/* * At this point, the default ACL may have zeroed-out entries for owner, * group and other. That usually results in a non-sensical resulting ACL * that denies all access except to any ACE that was explicitly added. * * The setfacl command solves a similar problem with this logic: * * "If a Default ACL entry is created, and the Default ACL contains * no owner, owning group, or others entry, a copy of the ACL * owner, owning group, or others entry is added to the Default ACL." * * Copy any missing ACEs from the effective set, if any ACEs were * explicitly set.
*/ if (default_acl_state.valid) { if (!(default_acl_state.valid & ACL_USER_OBJ))
default_acl_state.owner = effective_acl_state.owner; if (!(default_acl_state.valid & ACL_GROUP_OBJ))
default_acl_state.group = effective_acl_state.group; if (!(default_acl_state.valid & ACL_OTHER))
default_acl_state.other = effective_acl_state.other;
}
*pacl = posix_state_to_acl(&effective_acl_state, flags); if (IS_ERR(*pacl)) {
ret = PTR_ERR(*pacl);
*pacl = NULL; goto out_dstate;
}
*dpacl = posix_state_to_acl(&default_acl_state,
flags | NFS4_ACL_TYPE_DEFAULT); if (IS_ERR(*dpacl)) {
ret = PTR_ERR(*dpacl);
*dpacl = NULL;
posix_acl_release(*pacl);
*pacl = NULL; goto out_dstate;
}
sort_pacl(*pacl);
sort_pacl(*dpacl);
ret = 0;
out_dstate:
free_state(&default_acl_state);
out_estate:
free_state(&effective_acl_state); return ret;
}
staticshort
ace2type(struct nfs4_ace *ace)
{ switch (ace->whotype) { case NFS4_ACL_WHO_NAMED: return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ?
ACL_GROUP : ACL_USER); case NFS4_ACL_WHO_OWNER: return ACL_USER_OBJ; case NFS4_ACL_WHO_GROUP: return ACL_GROUP_OBJ; case NFS4_ACL_WHO_EVERYONE: return ACL_OTHER;
}
BUG(); return -1;
}
/* * return the size of the struct nfs4_acl required to represent an acl * with @entries entries.
*/ int nfs4_acl_bytes(int entries)
{ returnsizeof(struct nfs4_acl) + entries * sizeof(struct nfs4_ace);
}
int
nfs4_acl_get_whotype(char *p, u32 len)
{ int i;
for (i = 0; i < ARRAY_SIZE(s2t_map); i++) { if (s2t_map[i].stringlen == len &&
0 == memcmp(s2t_map[i].string, p, len)) return s2t_map[i].type;
} return NFS4_ACL_WHO_NAMED;
}
__be32 nfs4_acl_write_who(struct xdr_stream *xdr, int who)
{
__be32 *p; int i;
for (i = 0; i < ARRAY_SIZE(s2t_map); i++) { if (s2t_map[i].type != who) continue;
p = xdr_reserve_space(xdr, s2t_map[i].stringlen + 4); if (!p) return nfserr_resource;
p = xdr_encode_opaque(p, s2t_map[i].string,
s2t_map[i].stringlen); return 0;
}
WARN_ON_ONCE(1); return nfserr_serverfault;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.