⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysacls.c

📁 Rsync 3.0.5 source code
💻 C
📖 第 1 页 / 共 5 页
字号:
/* * Unix SMB/CIFS implementation. * Based on the Samba ACL support code. * Copyright (C) Jeremy Allison 2000. * Copyright (C) 2007-2008 Wayne Davison * * The permission functions have been changed to get/set all bits via * one call.  Some functions that rsync doesn't need were also removed. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * with this program; if not, visit the http://fsf.org website. */#include "rsync.h"#include "sysacls.h"#ifdef SUPPORT_ACLS#ifdef DEBUG#undef DEBUG#endif#define DEBUG(x,y)void SAFE_FREE(void *mem){	if (mem)		free(mem);}/* This file wraps all differing system ACL interfaces into a consistent one based on the POSIX interface. It also returns the correct errors for older UNIX systems that don't support ACLs. The interfaces that each ACL implementation must support are as follows : int sys_acl_get_entry( SMB_ACL_T theacl, int entry_id, SMB_ACL_ENTRY_T *entry_p) int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p) int sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p) SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type) SMB_ACL_T sys_acl_get_fd(int fd) SMB_ACL_T sys_acl_init( int count) int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry) int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id) int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry, uint32 bits) int sys_acl_valid( SMB_ACL_T theacl ) int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl) int sys_acl_set_fd( int fd, SMB_ACL_T theacl) int sys_acl_delete_def_file(const char *path) int sys_acl_free_acl(SMB_ACL_T posix_acl)*/#if defined(HAVE_POSIX_ACLS) /*--------------------------------------------*//* Identity mapping - easy. */int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p){	return acl_get_entry( the_acl, entry_id, entry_p);}int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p){	return acl_get_tag_type( entry_d, tag_type_p);}SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type){	return acl_get_file( path_p, type);}#if 0SMB_ACL_T sys_acl_get_fd(int fd){	return acl_get_fd(fd);}#endif#if defined(HAVE_ACL_GET_PERM_NP)#define acl_get_perm(p, b) acl_get_perm_np(p, b)#endifint sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p){	acl_permset_t permset;	if (acl_get_tag_type(entry, tag_type_p) != 0	 || acl_get_permset(entry, &permset) != 0)		return -1;	*bits_p = (acl_get_perm(permset, ACL_READ) ? 4 : 0)		| (acl_get_perm(permset, ACL_WRITE) ? 2 : 0)		| (acl_get_perm(permset, ACL_EXECUTE) ? 1 : 0);	if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP) {		void *qual;		if ((qual = acl_get_qualifier(entry)) == NULL)			return -1;		*u_g_id_p = *(id_t*)qual;		acl_free(qual);	}	return 0;}SMB_ACL_T sys_acl_init( int count){	return acl_init(count);}int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry){	return acl_create_entry(pacl, pentry);}int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id){	if (acl_set_tag_type(entry, tag_type) != 0)		return -1;	if (tag_type == SMB_ACL_USER || tag_type == SMB_ACL_GROUP) {		if (acl_set_qualifier(entry, (void*)&u_g_id) != 0)			return -1;	}	return sys_acl_set_access_bits(entry, bits);}int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry, uint32 bits){	acl_permset_t permset;	int rc;	if ((rc = acl_get_permset(entry, &permset)) != 0)		return rc;	acl_clear_perms(permset);	if (bits & 4)		acl_add_perm(permset, ACL_READ);	if (bits & 2)		acl_add_perm(permset, ACL_WRITE);	if (bits & 1)		acl_add_perm(permset, ACL_EXECUTE);	return acl_set_permset(entry, permset);}int sys_acl_valid( SMB_ACL_T theacl ){	return acl_valid(theacl);}int sys_acl_set_file(const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl){	return acl_set_file(name, acltype, theacl);}#if 0int sys_acl_set_fd( int fd, SMB_ACL_T theacl){	return acl_set_fd(fd, theacl);}#endifint sys_acl_delete_def_file(const char *name){	return acl_delete_def_file(name);}int sys_acl_free_acl(SMB_ACL_T the_acl) {	return acl_free(the_acl);}#elif defined(HAVE_TRU64_ACLS) /*--------------------------------------------*//* * The interface to DEC/Compaq Tru64 UNIX ACLs * is based on Draft 13 of the POSIX spec which is * slightly different from the Draft 16 interface. *  * Also, some of the permset manipulation functions * such as acl_clear_perm() and acl_add_perm() appear * to be broken on Tru64 so we have to manipulate * the permission bits in the permset directly. */int sys_acl_get_entry( SMB_ACL_T the_acl, int entry_id, SMB_ACL_ENTRY_T *entry_p){	SMB_ACL_ENTRY_T	entry;	if (entry_id == SMB_ACL_FIRST_ENTRY && acl_first_entry(the_acl) != 0) {		return -1;	}	errno = 0;	if ((entry = acl_get_entry(the_acl)) != NULL) {		*entry_p = entry;		return 1;	}	return errno ? -1 : 0;}int sys_acl_get_tag_type( SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *tag_type_p){	return acl_get_tag_type( entry_d, tag_type_p);}SMB_ACL_T sys_acl_get_file( const char *path_p, SMB_ACL_TYPE_T type){	return acl_get_file((char *)path_p, type);}#if 0SMB_ACL_T sys_acl_get_fd(int fd){	return acl_get_fd(fd, ACL_TYPE_ACCESS);}#endifint sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p){	acl_permset_t permset;	if (acl_get_tag_type(entry, tag_type_p) != 0	 || acl_get_permset(entry, &permset) != 0)		return -1;	*bits_p = *permset & 7;	/* Tru64 doesn't have acl_get_perm() */	if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP) {		void *qual;		if ((qual = acl_get_qualifier(entry)) == NULL)			return -1;		*u_g_id_p = *(id_t*)qual;		acl_free_qualifier(qual, *tag_type_p);	}	return 0;}SMB_ACL_T sys_acl_init( int count){	return acl_init(count);}int sys_acl_create_entry( SMB_ACL_T *pacl, SMB_ACL_ENTRY_T *pentry){	SMB_ACL_ENTRY_T entry;	if ((entry = acl_create_entry(pacl)) == NULL) {		return -1;	}	*pentry = entry;	return 0;}int sys_acl_set_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T tag_type, uint32 bits, id_t u_g_id){	if (acl_set_tag_type(entry, tag_type) != 0)		return -1;	if (tag_type == SMB_ACL_USER || tag_type == SMB_ACL_GROUP) {		if (acl_set_qualifier(entry, (void*)&u_g_id) != 0)			return -1;	}	return sys_acl_set_access_bits(entry, bits);}int sys_acl_set_access_bits(SMB_ACL_ENTRY_T entry, uint32 bits){	acl_permset_t permset;	int rc;	if ((rc = acl_get_permset(entry, &permset)) != 0)		return rc;	*permset = bits & 7;	return acl_set_permset(entry, permset);}int sys_acl_valid( SMB_ACL_T theacl ){	acl_entry_t	entry;	return acl_valid(theacl, &entry);}int sys_acl_set_file( const char *name, SMB_ACL_TYPE_T acltype, SMB_ACL_T theacl){	return acl_set_file((char *)name, acltype, theacl);}#if 0int sys_acl_set_fd( int fd, SMB_ACL_T theacl){	return acl_set_fd(fd, ACL_TYPE_ACCESS, theacl);}#endifint sys_acl_delete_def_file(const char *name){	return acl_delete_def_file((char *)name);}int sys_acl_free_acl(SMB_ACL_T the_acl) {	return acl_free(the_acl);}#elif defined(HAVE_UNIXWARE_ACLS) || defined(HAVE_SOLARIS_ACLS) /*-----------*//* * Donated by Michael Davidson <md@sco.COM> for UnixWare / OpenUNIX. * Modified by Toomas Soome <tsoome@ut.ee> for Solaris. *//* * Note that while this code implements sufficient functionality * to support the sys_acl_* interfaces it does not provide all * of the semantics of the POSIX ACL interfaces. * * In particular, an ACL entry descriptor (SMB_ACL_ENTRY_T) returned * from a call to sys_acl_get_entry() should not be assumed to be * valid after calling any of the following functions, which may * reorder the entries in the ACL. * *	sys_acl_valid() *	sys_acl_set_file() *	sys_acl_set_fd() *//* * The only difference between Solaris and UnixWare / OpenUNIX is * that the #defines for the ACL operations have different names */#if defined(HAVE_UNIXWARE_ACLS)#define	SETACL		ACL_SET#define	GETACL		ACL_GET#define	GETACLCNT	ACL_CNT#endifint sys_acl_get_entry(SMB_ACL_T acl_d, int entry_id, SMB_ACL_ENTRY_T *entry_p){	if (entry_id != SMB_ACL_FIRST_ENTRY && entry_id != SMB_ACL_NEXT_ENTRY) {		errno = EINVAL;		return -1;	}	if (entry_p == NULL) {		errno = EINVAL;		return -1;	}	if (entry_id == SMB_ACL_FIRST_ENTRY) {		acl_d->next = 0;	}	if (acl_d->next < 0) {		errno = EINVAL;		return -1;	}	if (acl_d->next >= acl_d->count) {		return 0;	}	*entry_p = &acl_d->acl[acl_d->next++];	return 1;}int sys_acl_get_tag_type(SMB_ACL_ENTRY_T entry_d, SMB_ACL_TAG_T *type_p){	*type_p = entry_d->a_type;	return 0;}/* * There is no way of knowing what size the ACL returned by * GETACL will be unless you first call GETACLCNT which means * making an additional system call. * * In the hope of avoiding the cost of the additional system * call in most cases, we initially allocate enough space for * an ACL with INITIAL_ACL_SIZE entries. If this turns out to * be too small then we use GETACLCNT to find out the actual * size, reallocate the ACL buffer, and then call GETACL again. */#define	INITIAL_ACL_SIZE	16SMB_ACL_T sys_acl_get_file(const char *path_p, SMB_ACL_TYPE_T type){	SMB_ACL_T	acl_d;	int		count;		/* # of ACL entries allocated	*/	int		naccess;	/* # of access ACL entries	*/	int		ndefault;	/* # of default ACL entries	*/	if (type != SMB_ACL_TYPE_ACCESS && type != SMB_ACL_TYPE_DEFAULT) {		errno = EINVAL;		return NULL;	}	count = INITIAL_ACL_SIZE;	if ((acl_d = sys_acl_init(count)) == NULL) {		return NULL;	}	/*	 * If there isn't enough space for the ACL entries we use	 * GETACLCNT to determine the actual number of ACL entries	 * reallocate and try again. This is in a loop because it	 * is possible that someone else could modify the ACL and	 * increase the number of entries between the call to	 * GETACLCNT and the call to GETACL.	 */	while ((count = acl(path_p, GETACL, count, &acl_d->acl[0])) < 0	    && errno == ENOSPC) {		sys_acl_free_acl(acl_d);		if ((count = acl(path_p, GETACLCNT, 0, NULL)) < 0) {			return NULL;		}		if ((acl_d = sys_acl_init(count)) == NULL) {			return NULL;		}	}	if (count < 0) {		sys_acl_free_acl(acl_d);		return NULL;	}	/*	 * calculate the number of access and default ACL entries	 *	 * Note: we assume that the acl() system call returned a	 * well formed ACL which is sorted so that all of the	 * access ACL entries preceed any default ACL entries	 */	for (naccess = 0; naccess < count; naccess++) {		if (acl_d->acl[naccess].a_type & ACL_DEFAULT)			break;	}	ndefault = count - naccess;		/*	 * if the caller wants the default ACL we have to copy	 * the entries down to the start of the acl[] buffer	 * and mask out the ACL_DEFAULT flag from the type field	 */	if (type == SMB_ACL_TYPE_DEFAULT) {		int	i, j;		for (i = 0, j = naccess; i < ndefault; i++, j++) {			acl_d->acl[i] = acl_d->acl[j];			acl_d->acl[i].a_type &= ~ACL_DEFAULT;		}		acl_d->count = ndefault;	} else {		acl_d->count = naccess;	}	return acl_d;}#if 0SMB_ACL_T sys_acl_get_fd(int fd){	SMB_ACL_T	acl_d;	int		count;		/* # of ACL entries allocated	*/	int		naccess;	/* # of access ACL entries	*/	count = INITIAL_ACL_SIZE;	if ((acl_d = sys_acl_init(count)) == NULL) {		return NULL;	}	while ((count = facl(fd, GETACL, count, &acl_d->acl[0])) < 0	    && errno == ENOSPC) {		sys_acl_free_acl(acl_d);		if ((count = facl(fd, GETACLCNT, 0, NULL)) < 0) {			return NULL;		}		if ((acl_d = sys_acl_init(count)) == NULL) {			return NULL;		}	}	if (count < 0) {		sys_acl_free_acl(acl_d);		return NULL;	}	/*	 * calculate the number of access ACL entries	 */	for (naccess = 0; naccess < count; naccess++) {		if (acl_d->acl[naccess].a_type & ACL_DEFAULT)			break;	}		acl_d->count = naccess;	return acl_d;}#endifint sys_acl_get_info(SMB_ACL_ENTRY_T entry, SMB_ACL_TAG_T *tag_type_p, uint32 *bits_p, id_t *u_g_id_p){	*tag_type_p = entry->a_type;	*bits_p = entry->a_perm;	if (*tag_type_p == SMB_ACL_USER || *tag_type_p == SMB_ACL_GROUP)		*u_g_id_p = entry->a_id;		return 0;}SMB_ACL_T sys_acl_init(int count){	SMB_ACL_T	a;	if (count < 0) {		errno = EINVAL;		return NULL;	}

⌨️ 快捷键说明

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