nfs4acl.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 975 行 · 第 1/2 页

C
975
字号
/* *  fs/nfs4acl/acl.c * *  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. */#include <linux/string.h>#include <linux/slab.h>#include <linux/list.h>#include <linux/types.h>#include <linux/fs.h>#include <linux/module.h>#include <linux/nfs_fs.h>#include <linux/posix_acl.h>#include <linux/nfs4.h>#include <linux/nfs4_acl.h>/* mode bit translations: */#define NFS4_READ_MODE (NFS4_ACE_READ_DATA | NFS4_ACE_READ_NAMED_ATTRS)#define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_WRITE_NAMED_ATTRS | NFS4_ACE_APPEND_DATA)#define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE#define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE)#define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)/* flags used to simulate posix default ACLs */#define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \		| NFS4_ACE_DIRECTORY_INHERIT_ACE | NFS4_ACE_INHERIT_ONLY_ACE)#define MASK_EQUAL(mask1, mask2) \	( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) )static u32mask_from_posix(unsigned short perm, unsigned int flags){	int mask = NFS4_ANYONE_MODE;	if (flags & NFS4_ACL_OWNER)		mask |= NFS4_OWNER_MODE;	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;}static u32deny_mask(u32 allow_mask, unsigned int flags){	u32 ret = ~allow_mask & ~NFS4_ACE_DELETE;	if (!(flags & NFS4_ACL_DIR))		ret &= ~NFS4_ACE_DELETE_CHILD;	return ret;}static intmode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags){	u32 ignore = 0;	if (!(flags & NFS4_ACL_DIR))		ignore |= NFS4_ACE_DELETE_CHILD; /* ignore it */	perm |= ignore;	*mode = 0;	if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE)		*mode |= ACL_READ;	if ((perm & NFS4_WRITE_MODE) == NFS4_WRITE_MODE)		*mode |= ACL_WRITE;	if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE)		*mode |= ACL_EXECUTE;	if (!MASK_EQUAL(perm, ignore|mask_from_posix(*mode, flags)))		return -EINVAL;	return 0;}struct ace_container {	struct nfs4_ace  *ace;	struct list_head  ace_l;};static short ace2type(struct nfs4_ace *);static int _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, unsigned int);static struct posix_acl *_nfsv4_to_posix_one(struct nfs4_acl *, unsigned int);int nfs4_acl_add_ace(struct nfs4_acl *, u32, u32, u32, int, uid_t);int nfs4_acl_split(struct nfs4_acl *, struct nfs4_acl *);struct nfs4_acl *nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl,			unsigned int flags){	struct nfs4_acl *acl;	int error = -EINVAL;	if ((pacl != NULL &&		(posix_acl_valid(pacl) < 0 || pacl->a_count == 0)) ||	    (dpacl != NULL &&		(posix_acl_valid(dpacl) < 0 || dpacl->a_count == 0)))		goto out_err;	acl = nfs4_acl_new();	if (acl == NULL) {		error = -ENOMEM;		goto out_err;	}	if (pacl != NULL) {		error = _posix_to_nfsv4_one(pacl, acl,						flags & ~NFS4_ACL_TYPE_DEFAULT);		if (error < 0)			goto out_acl;	}	if (dpacl != NULL) {		error = _posix_to_nfsv4_one(dpacl, acl,						flags | NFS4_ACL_TYPE_DEFAULT);		if (error < 0)			goto out_acl;	}	return acl;out_acl:	nfs4_acl_free(acl);out_err:	acl = ERR_PTR(error);	return acl;}static intnfs4_acl_add_pair(struct nfs4_acl *acl, int eflag, u32 mask, int whotype,		uid_t owner, unsigned int flags){	int error;	error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,				 eflag, mask, whotype, owner);	if (error < 0)		return error;	error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,				eflag, deny_mask(mask, flags), whotype, owner);	return error;}/* We assume the acl has been verified with posix_acl_valid. */static int_posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl,						unsigned int flags){	struct posix_acl_entry *pa, *pe, *group_owner_entry;	int error = -EINVAL;	u32 mask, mask_mask;	int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ?					NFS4_INHERITANCE_FLAGS : 0);	BUG_ON(pacl->a_count < 3);	pe = pacl->a_entries + pacl->a_count;	pa = pe - 2; /* if mask entry exists, it's second from the last. */	if (pa->e_tag == ACL_MASK)		mask_mask = deny_mask(mask_from_posix(pa->e_perm, flags), flags);	else		mask_mask = 0;	pa = pacl->a_entries;	BUG_ON(pa->e_tag != ACL_USER_OBJ);	mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER);	error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_OWNER, 0, flags);	if (error < 0)		goto out;	pa++;	while (pa->e_tag == ACL_USER) {		mask = mask_from_posix(pa->e_perm, flags);		error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,				eflag,  mask_mask, NFS4_ACL_WHO_NAMED, pa->e_id);		if (error < 0)			goto out;		error = nfs4_acl_add_pair(acl, eflag, mask,				NFS4_ACL_WHO_NAMED, pa->e_id, flags);		if (error < 0)			goto out;		pa++;	}	/* In the case of groups, we apply allow ACEs first, then deny ACEs,	 * since a user can be in more than one group.  */	/* allow ACEs */	if (pacl->a_count > 3) {		BUG_ON(pa->e_tag != ACL_GROUP_OBJ);		error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,				NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,				NFS4_ACL_WHO_GROUP, 0);		if (error < 0)			goto out;	}	group_owner_entry = pa;	mask = mask_from_posix(pa->e_perm, flags);	error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,			NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,			NFS4_ACL_WHO_GROUP, 0);	if (error < 0)		goto out;	pa++;	while (pa->e_tag == ACL_GROUP) {		mask = mask_from_posix(pa->e_perm, flags);		error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,				NFS4_ACE_IDENTIFIER_GROUP | eflag, mask_mask,				NFS4_ACL_WHO_NAMED, pa->e_id);		if (error < 0)			goto out;		error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE,		    		NFS4_ACE_IDENTIFIER_GROUP | eflag, mask,		    		NFS4_ACL_WHO_NAMED, pa->e_id);		if (error < 0)			goto out;		pa++;	}	/* deny ACEs */	pa = group_owner_entry;	mask = mask_from_posix(pa->e_perm, flags);	error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,			NFS4_ACE_IDENTIFIER_GROUP | eflag,			deny_mask(mask, flags), NFS4_ACL_WHO_GROUP, 0);	if (error < 0)		goto out;	pa++;	while (pa->e_tag == ACL_GROUP) {		mask = mask_from_posix(pa->e_perm, flags);		error = nfs4_acl_add_ace(acl, NFS4_ACE_ACCESS_DENIED_ACE_TYPE,		    		NFS4_ACE_IDENTIFIER_GROUP | eflag,		    		deny_mask(mask, flags), NFS4_ACL_WHO_NAMED, pa->e_id);		if (error < 0)			goto out;		pa++;	}	if (pa->e_tag == ACL_MASK)		pa++;	BUG_ON(pa->e_tag != ACL_OTHER);	mask = mask_from_posix(pa->e_perm, flags);	error = nfs4_acl_add_pair(acl, eflag, mask, NFS4_ACL_WHO_EVERYONE, 0, flags);out:	return error;}static voidsort_pacl_range(struct posix_acl *pacl, int start, int end) {	int sorted = 0, i;	struct posix_acl_entry tmp;	/* 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 (pacl->a_entries[i].e_id					> pacl->a_entries[i+1].e_id) {				sorted = 0;				tmp = pacl->a_entries[i];				pacl->a_entries[i] = pacl->a_entries[i+1];				pacl->a_entries[i+1] = tmp;			}		}	}}static voidsort_pacl(struct posix_acl *pacl){	/* posix_acl_valid requires that users and groups be in order	 * by uid/gid. */	int i, j;	if (pacl->a_count <= 4)		return; /* no users or groups */	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;}static intwrite_pace(struct nfs4_ace *ace, struct posix_acl *pacl,		struct posix_acl_entry **pace, short tag, unsigned int flags){	struct posix_acl_entry *this = *pace;	if (*pace == pacl->a_entries + pacl->a_count)		return -EINVAL; /* fell off the end */	(*pace)++;	this->e_tag = tag;	if (tag == ACL_USER_OBJ)		flags |= NFS4_ACL_OWNER;	if (mode_from_nfs4(ace->access_mask, &this->e_perm, flags))		return -EINVAL;	this->e_id = (tag == ACL_USER || tag == ACL_GROUP ?			ace->who : ACL_UNDEFINED_ID);	return 0;}static struct nfs4_ace *get_next_v4_ace(struct list_head **p, struct list_head *head){	struct nfs4_ace *ace;	*p = (*p)->next;	if (*p == head)		return NULL;	ace = list_entry(*p, struct nfs4_ace, l_ace);	return ace;}intnfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl,		struct posix_acl **dpacl, unsigned int flags){	struct nfs4_acl *dacl;	int error = -ENOMEM;	*pacl = NULL;	*dpacl = NULL;	dacl = nfs4_acl_new();	if (dacl == NULL)		goto out;	error = nfs4_acl_split(acl, dacl);	if (error < 0)		goto out_acl;	if (pacl != NULL) {		if (acl->naces == 0) {			error = -ENODATA;			goto try_dpacl;		}		*pacl = _nfsv4_to_posix_one(acl, flags);		if (IS_ERR(*pacl)) {			error = PTR_ERR(*pacl);			*pacl = NULL;			goto out_acl;		}	}try_dpacl:	if (dpacl != NULL) {		if (dacl->naces == 0) {			if (pacl == NULL || *pacl == NULL)				error = -ENODATA;			goto out_acl;		}		error = 0;		*dpacl = _nfsv4_to_posix_one(dacl, flags);		if (IS_ERR(*dpacl)) {			error = PTR_ERR(*dpacl);			*dpacl = NULL;			goto out_acl;		}	}out_acl:	if (error && pacl) {		posix_acl_release(*pacl);		*pacl = NULL;	}	nfs4_acl_free(dacl);out:	return error;}static intsame_who(struct nfs4_ace *a, struct nfs4_ace *b){	return a->whotype == b->whotype &&		(a->whotype != NFS4_ACL_WHO_NAMED || a->who == b->who);}static intcomplementary_ace_pair(struct nfs4_ace *allow, struct nfs4_ace *deny,		unsigned int flags){	int ignore = 0;	if (!(flags & NFS4_ACL_DIR))		ignore |= NFS4_ACE_DELETE_CHILD;	return MASK_EQUAL(ignore|deny_mask(allow->access_mask, flags),			  ignore|deny->access_mask) &&		allow->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE &&		deny->type == NFS4_ACE_ACCESS_DENIED_ACE_TYPE &&		allow->flag == deny->flag &&		same_who(allow, deny);}static inline intuser_obj_from_v4(struct nfs4_acl *n4acl, struct list_head **p,		struct posix_acl *pacl, struct posix_acl_entry **pace,		unsigned int flags){	int error = -EINVAL;	struct nfs4_ace *ace, *ace2;	ace = get_next_v4_ace(p, &n4acl->ace_head);	if (ace == NULL)		goto out;	if (ace2type(ace) != ACL_USER_OBJ)		goto out;	error = write_pace(ace, pacl, pace, ACL_USER_OBJ, flags);	if (error < 0)		goto out;	error = -EINVAL;	ace2 = get_next_v4_ace(p, &n4acl->ace_head);	if (ace2 == NULL)		goto out;	if (!complementary_ace_pair(ace, ace2, flags))		goto out;	error = 0;out:	return error;}static inline intusers_from_v4(struct nfs4_acl *n4acl, struct list_head **p,		struct nfs4_ace **mask_ace,		struct posix_acl *pacl, struct posix_acl_entry **pace,		unsigned int flags){	int error = -EINVAL;	struct nfs4_ace *ace, *ace2;	ace = get_next_v4_ace(p, &n4acl->ace_head);	if (ace == NULL)		goto out;	while (ace2type(ace) == ACL_USER) {		if (ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE)			goto out;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?