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

📄 acls.c

📁 Rsync 3.0.5 source code
💻 C
📖 第 1 页 / 共 2 页
字号:
				  sxp->st.st_mode) < 0) {			free_acl(sxp);			return -1;		}	}	return 0;}/* === Send functions === *//* Send the ida list over the file descriptor. */static void send_ida_entries(const ida_entries *idal, int f){	id_access *ida;	size_t count = idal->count;	write_varint(f, idal->count);	for (ida = idal->idas; count--; ida++) {		uint32 xbits = ida->access << 2;		const char *name;		if (ida->access & NAME_IS_USER) {			xbits |= XFLAG_NAME_IS_USER;			name = add_uid(ida->id);		} else			name = add_gid(ida->id);		write_varint(f, ida->id);		if (inc_recurse && name) {			int len = strlen(name);			write_varint(f, xbits | XFLAG_NAME_FOLLOWS);			write_byte(f, len);			write_buf(f, name, len);		} else			write_varint(f, xbits);	}}static void send_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type,			   item_list *racl_list, int f){	int ndx = find_matching_rsync_acl(racl, type, racl_list);	/* Send 0 (-1 + 1) to indicate that literal ACL data follows. */	write_varint(f, ndx + 1);	if (ndx < 0) {		rsync_acl *new_racl = EXPAND_ITEM_LIST(racl_list, rsync_acl, 1000);		uchar flags = 0;		if (racl->user_obj != NO_ENTRY)			flags |= XMIT_USER_OBJ;		if (racl->group_obj != NO_ENTRY)			flags |= XMIT_GROUP_OBJ;		if (racl->mask_obj != NO_ENTRY)			flags |= XMIT_MASK_OBJ;		if (racl->other_obj != NO_ENTRY)			flags |= XMIT_OTHER_OBJ;		if (racl->names.count)			flags |= XMIT_NAME_LIST;		write_byte(f, flags);		if (flags & XMIT_USER_OBJ)			write_varint(f, racl->user_obj);		if (flags & XMIT_GROUP_OBJ)			write_varint(f, racl->group_obj);		if (flags & XMIT_MASK_OBJ)			write_varint(f, racl->mask_obj);		if (flags & XMIT_OTHER_OBJ)			write_varint(f, racl->other_obj);		if (flags & XMIT_NAME_LIST)			send_ida_entries(&racl->names, f);		/* Give the allocated data to the new list object. */		*new_racl = *racl;		*racl = empty_rsync_acl;	}}/* Send the ACL from the stat_x structure down the indicated file descriptor. * This also frees the ACL data. */void send_acl(stat_x *sxp, int f){	if (!sxp->acc_acl) {		sxp->acc_acl = create_racl();		rsync_acl_fake_perms(sxp->acc_acl, sxp->st.st_mode);	}	/* Avoid sending values that can be inferred from other data. */	rsync_acl_strip_perms(sxp->acc_acl);	send_rsync_acl(sxp->acc_acl, SMB_ACL_TYPE_ACCESS, &access_acl_list, f);	if (S_ISDIR(sxp->st.st_mode)) {		if (!sxp->def_acl)			sxp->def_acl = create_racl();		send_rsync_acl(sxp->def_acl, SMB_ACL_TYPE_DEFAULT, &default_acl_list, f);	}}/* === Receive functions === */static uint32 recv_acl_access(uchar *name_follows_ptr, int f){	uint32 access = read_varint(f);	if (name_follows_ptr) {		int flags = access & 3;		access >>= 2;		if (am_root >= 0 && access & ~SMB_ACL_VALID_NAME_BITS)			goto value_error;		if (flags & XFLAG_NAME_FOLLOWS)			*name_follows_ptr = 1;		else			*name_follows_ptr = 0;		if (flags & XFLAG_NAME_IS_USER)			access |= NAME_IS_USER;	} else if (am_root >= 0 && access & ~SMB_ACL_VALID_OBJ_BITS) {	  value_error:		rprintf(FERROR_XFER, "recv_acl_access: value out of range: %x\n",			access);		exit_cleanup(RERR_STREAMIO);	}	return access;}static uchar recv_ida_entries(ida_entries *ent, int f){	uchar computed_mask_bits = 0;	int i, count = read_varint(f);	if (count) {		if (!(ent->idas = new_array(id_access, count)))			out_of_memory("recv_ida_entries");	} else		ent->idas = NULL;	ent->count = count;	for (i = 0; i < count; i++) {		uchar has_name;		id_t id = read_varint(f);		uint32 access = recv_acl_access(&has_name, f);		if (has_name) {			if (access & NAME_IS_USER)				id = recv_user_name(f, id);			else				id = recv_group_name(f, id, NULL);		} else if (access & NAME_IS_USER) {			if (inc_recurse && am_root && !numeric_ids)				id = match_uid(id);		} else {			if (inc_recurse && (!am_root || !numeric_ids))				id = match_gid(id, NULL);		}		ent->idas[i].id = id;		ent->idas[i].access = access;		computed_mask_bits |= access;	}	return computed_mask_bits & ~NO_ENTRY;}static int recv_rsync_acl(item_list *racl_list, SMB_ACL_TYPE_T type, int f){	uchar computed_mask_bits = 0;	acl_duo *duo_item;	uchar flags;	int ndx = read_varint(f);	if (ndx < 0 || (size_t)ndx > racl_list->count) {		rprintf(FERROR_XFER, "recv_acl_index: %s ACL index %d > %d\n",			str_acl_type(type), ndx, (int)racl_list->count);		exit_cleanup(RERR_STREAMIO);	}	if (ndx != 0)		return ndx - 1;		ndx = racl_list->count;	duo_item = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);	duo_item->racl = empty_rsync_acl;	flags = read_byte(f);	if (flags & XMIT_USER_OBJ)		duo_item->racl.user_obj = recv_acl_access(NULL, f);	if (flags & XMIT_GROUP_OBJ)		duo_item->racl.group_obj = recv_acl_access(NULL, f);	if (flags & XMIT_MASK_OBJ)		duo_item->racl.mask_obj = recv_acl_access(NULL, f);	if (flags & XMIT_OTHER_OBJ)		duo_item->racl.other_obj = recv_acl_access(NULL, f);	if (flags & XMIT_NAME_LIST)		computed_mask_bits |= recv_ida_entries(&duo_item->racl.names, f);#ifdef HAVE_OSX_ACLS	/* If we received a superfluous mask, throw it away. */	duo_item->racl.mask_obj = NO_ENTRY;#else	if (!duo_item->racl.names.count) {		/* If we received a superfluous mask, throw it away. */		if (duo_item->racl.mask_obj != NO_ENTRY) {			/* Mask off the group perms with it first. */			duo_item->racl.group_obj &= duo_item->racl.mask_obj | NO_ENTRY;			duo_item->racl.mask_obj = NO_ENTRY;		}	} else if (duo_item->racl.mask_obj == NO_ENTRY) /* Must be non-empty with lists. */		duo_item->racl.mask_obj = (computed_mask_bits | duo_item->racl.group_obj) & ~NO_ENTRY;#endif	duo_item->sacl = NULL;	return ndx;}/* Receive the ACL info the sender has included for this file-list entry. */void receive_acl(struct file_struct *file, int f){	F_ACL(file) = recv_rsync_acl(&access_acl_list, SMB_ACL_TYPE_ACCESS, f);	if (S_ISDIR(file->mode))		F_DIR_DEFACL(file) = recv_rsync_acl(&default_acl_list, SMB_ACL_TYPE_DEFAULT, f);}static int cache_rsync_acl(rsync_acl *racl, SMB_ACL_TYPE_T type, item_list *racl_list){	int ndx;	if (!racl)		ndx = -1;	else if ((ndx = find_matching_rsync_acl(racl, type, racl_list)) == -1) {		acl_duo *new_duo;		ndx = racl_list->count;		new_duo = EXPAND_ITEM_LIST(racl_list, acl_duo, 1000);		new_duo->racl = *racl;		new_duo->sacl = NULL;		*racl = empty_rsync_acl;	}	return ndx;}/* Turn the ACL data in stat_x into cached ACL data, setting the index * values in the file struct. */void cache_acl(struct file_struct *file, stat_x *sxp){	F_ACL(file) = cache_rsync_acl(sxp->acc_acl,				      SMB_ACL_TYPE_ACCESS, &access_acl_list);	if (S_ISDIR(sxp->st.st_mode)) {		F_DIR_DEFACL(file) = cache_rsync_acl(sxp->def_acl,				      SMB_ACL_TYPE_DEFAULT, &default_acl_list);	}}#ifndef HAVE_OSX_ACLSstatic mode_t change_sacl_perms(SMB_ACL_T sacl, rsync_acl *racl, mode_t old_mode, mode_t mode){	SMB_ACL_ENTRY_T entry;	const char *errfun;	int rc;	if (S_ISDIR(mode)) {		/* If the sticky bit is going on, it's not safe to allow all		 * the new ACL to go into effect before it gets set. */#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS		if (mode & S_ISVTX)			mode &= ~0077;#else		if (mode & S_ISVTX && !(old_mode & S_ISVTX))			mode &= ~0077;	} else {		/* If setuid or setgid is going off, it's not safe to allow all		 * the new ACL to go into effect before they get cleared. */		if ((old_mode & S_ISUID && !(mode & S_ISUID))		 || (old_mode & S_ISGID && !(mode & S_ISGID)))			mode &= ~0077;#endif	}	errfun = "sys_acl_get_entry";	for (rc = sys_acl_get_entry(sacl, SMB_ACL_FIRST_ENTRY, &entry);	     rc == 1;	     rc = sys_acl_get_entry(sacl, SMB_ACL_NEXT_ENTRY, &entry)) {		SMB_ACL_TAG_T tag_type;		if ((rc = sys_acl_get_tag_type(entry, &tag_type)) != 0) {			errfun = "sys_acl_get_tag_type";			break;		}		switch (tag_type) {		case SMB_ACL_USER_OBJ:			COE2( store_access_in_entry,((mode >> 6) & 7, entry) );			break;		case SMB_ACL_GROUP_OBJ:			/* group is only empty when identical to group perms. */			if (racl->group_obj != NO_ENTRY)				break;			COE2( store_access_in_entry,((mode >> 3) & 7, entry) );			break;		case SMB_ACL_MASK:#ifndef ACLS_NEED_MASK			/* mask is only empty when we don't need it. */			if (racl->mask_obj == NO_ENTRY)				break;#endif			COE2( store_access_in_entry,((mode >> 3) & 7, entry) );			break;		case SMB_ACL_OTHER:			COE2( store_access_in_entry,(mode & 7, entry) );			break;		}	}	if (rc) {	  error_exit:		if (errfun) {			rsyserr(FERROR_XFER, errno, "change_sacl_perms: %s()",				errfun);		}		return (mode_t)~0;	}#ifdef SMB_ACL_LOSES_SPECIAL_MODE_BITS	/* Ensure that chmod() will be called to restore any lost setid bits. */	if (old_mode & (S_ISUID | S_ISGID | S_ISVTX)	 && BITS_EQUAL(old_mode, mode, CHMOD_BITS))		old_mode &= ~(S_ISUID | S_ISGID | S_ISVTX);#endif	/* Return the mode of the file on disk, as we will set them. */	return (old_mode & ~ACCESSPERMS) | (mode & ACCESSPERMS);}#endifstatic int set_rsync_acl(const char *fname, acl_duo *duo_item,			 SMB_ACL_TYPE_T type, stat_x *sxp, mode_t mode){	if (type == SMB_ACL_TYPE_DEFAULT	 && duo_item->racl.user_obj == NO_ENTRY) {		int rc;#ifdef SUPPORT_XATTRS		/* --fake-super support: delete default ACL from xattrs. */		if (am_root < 0)			rc = del_def_xattr_acl(fname);		else#endif			rc = sys_acl_delete_def_file(fname);		if (rc < 0) {			rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_delete_def_file(%s)",				fname);			return -1;		}#ifdef SUPPORT_XATTRS	} else if (am_root < 0) {		/* --fake-super support: store ACLs in an xattr. */		int cnt = duo_item->racl.names.count;		size_t len = 4*4 + cnt * (4+4);		char *buf = new_array(char, len);		int rc;		SIVAL(buf, 0, duo_item->racl.user_obj);		SIVAL(buf, 4, duo_item->racl.group_obj);		SIVAL(buf, 8, duo_item->racl.mask_obj);		SIVAL(buf, 12, duo_item->racl.other_obj);		if (cnt) {			char *bp = buf + 4*4;			id_access *ida = duo_item->racl.names.idas;			for ( ; cnt--; ida++, bp += 4+4) {				SIVAL(bp, 0, ida->id);				SIVAL(bp, 4, ida->access);			}		}		rc = set_xattr_acl(fname, type == SMB_ACL_TYPE_ACCESS, buf, len);		free(buf);		return rc;#endif	} else {		mode_t cur_mode = sxp->st.st_mode;		if (!duo_item->sacl		 && !pack_smb_acl(&duo_item->sacl, &duo_item->racl))			return -1;#ifdef HAVE_OSX_ACLS		mode = 0; /* eliminate compiler warning */#else		if (type == SMB_ACL_TYPE_ACCESS) {			cur_mode = change_sacl_perms(duo_item->sacl, &duo_item->racl,						     cur_mode, mode);			if (cur_mode == (mode_t)~0)				return 0;		}#endif		if (sys_acl_set_file(fname, type, duo_item->sacl) < 0) {			rsyserr(FERROR_XFER, errno, "set_acl: sys_acl_set_file(%s, %s)",				fname, str_acl_type(type));			return -1;		}		if (type == SMB_ACL_TYPE_ACCESS)			sxp->st.st_mode = cur_mode;	}	return 0;}/* Set ACL on indicated filename. * * This sets extended access ACL entries and default ACL.  If convenient, * it sets permission bits along with the access ACL and signals having * done so by modifying sxp->st.st_mode. * * Returns 1 for unchanged, 0 for changed, -1 for failed.  Call this * with fname set to NULL to just check if the ACL is unchanged. */int set_acl(const char *fname, const struct file_struct *file, stat_x *sxp){	int unchanged = 1;	int32 ndx;	BOOL eq;	if (!dry_run && (read_only || list_only)) {		errno = EROFS;		return -1;	}	ndx = F_ACL(file);	if (ndx >= 0 && (size_t)ndx < access_acl_list.count) {		acl_duo *duo_item = access_acl_list.items;		duo_item += ndx;		eq = sxp->acc_acl		  && rsync_acl_equal_enough(sxp->acc_acl, &duo_item->racl, file->mode);		if (!eq) {			unchanged = 0;			if (!dry_run && fname			 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_ACCESS,					  sxp, file->mode) < 0)				unchanged = -1;		}	}	if (!S_ISDIR(sxp->st.st_mode))		return unchanged;	ndx = F_DIR_DEFACL(file);	if (ndx >= 0 && (size_t)ndx < default_acl_list.count) {		acl_duo *duo_item = default_acl_list.items;		duo_item += ndx;		eq = sxp->def_acl && rsync_acl_equal(sxp->def_acl, &duo_item->racl);		if (!eq) {			if (unchanged > 0)				unchanged = 0;			if (!dry_run && fname			 && set_rsync_acl(fname, duo_item, SMB_ACL_TYPE_DEFAULT,					  sxp, file->mode) < 0)				unchanged = -1;		}	}	return unchanged;}/* Non-incremental recursion needs to convert all the received IDs. * This is done in a single pass after receiving the whole file-list. */static void match_racl_ids(const item_list *racl_list){	int list_cnt, name_cnt;	acl_duo *duo_item = racl_list->items;	for (list_cnt = racl_list->count; list_cnt--; duo_item++) {		ida_entries *idal = &duo_item->racl.names;		id_access *ida = idal->idas;		for (name_cnt = idal->count; name_cnt--; ida++) {			if (ida->access & NAME_IS_USER)				ida->id = match_uid(ida->id);			else				ida->id = match_gid(ida->id, NULL);		}	}}void match_acl_ids(void){	match_racl_ids(&access_acl_list);	match_racl_ids(&default_acl_list);}/* This is used by dest_mode(). */int default_perms_for_dir(const char *dir){	rsync_acl racl;	SMB_ACL_T sacl;	BOOL ok;	int perms;	if (dir == NULL)		dir = ".";	perms = ACCESSPERMS & ~orig_umask;	/* Read the directory's default ACL.  If it has none, this will successfully return an empty ACL. */	sacl = sys_acl_get_file(dir, SMB_ACL_TYPE_DEFAULT);	if (sacl == NULL) {		/* Couldn't get an ACL.  Darn. */		switch (errno) {#ifdef ENOTSUP		case ENOTSUP:#endif		case ENOSYS:			/* No ACLs are available. */			break;		case ENOENT:			if (dry_run) {				/* We're doing a dry run, so the containing directory				 * wasn't actually created.  Don't worry about it. */				break;			}			/* Otherwise fall through. */		default:			rprintf(FWARNING,				"default_perms_for_dir: sys_acl_get_file(%s, %s): %s, falling back on umask\n",				dir, str_acl_type(SMB_ACL_TYPE_DEFAULT), strerror(errno));		}		return perms;	}	/* Convert it. */	racl = empty_rsync_acl;	ok = unpack_smb_acl(sacl, &racl);	sys_acl_free_acl(sacl);	if (!ok) {		rprintf(FWARNING, "default_perms_for_dir: unpack_smb_acl failed, falling back on umask\n");		return perms;	}	/* Apply the permission-bit entries of the default ACL, if any. */	if (racl.user_obj != NO_ENTRY) {		perms = rsync_acl_get_perms(&racl);		if (verbose > 2)			rprintf(FINFO, "got ACL-based default perms %o for directory %s\n", perms, dir);	}	rsync_acl_free(&racl);	return perms;}#endif /* SUPPORT_ACLS */

⌨️ 快捷键说明

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