2 * Convert ZFS/NFSv4 acls to NT acls and vice versa.
4 * Copyright (C) Jiri Sasek, 2007
5 * based on the foobar.c module which is copyrighted by Volker Lendecke
7 * Many thanks to Axel Apitz for help to fix the special ace's handling
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
26 #include "nfs4_acls.h"
29 #define DBGC_CLASS DBGC_VFS
31 #define ZFSACL_MODULE_NAME "zfsacl"
34 * read the local file's acls and return it in NT form
35 * using the NFSv4 format conversion
37 static NTSTATUS zfs_get_nt_acl(struct files_struct *fsp, uint32 security_info,
38 struct security_descriptor **ppdesc)
45 /* read the number of file aces */
46 if((naces = acl(fsp->fsp_name, ACE_GETACLCNT, 0, NULL)) == -1) {
48 DEBUG(9, ("acl(ACE_GETACLCNT, %s): Operation is not supported on the filesystem where the file reside"));
50 DEBUG(9, ("acl(ACE_GETACLCNT, %s): %s ", fsp->fsp_name,
53 return map_nt_error_from_unix(errno);
55 /* allocate the field of ZFS aces */
56 mem_ctx = talloc_tos();
57 acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
59 return NT_STATUS_NO_MEMORY;
61 /* read the aces into the field */
62 if(acl(fsp->fsp_name, ACE_GETACL, naces, acebuf) < 0) {
63 DEBUG(9, ("acl(ACE_GETACL, %s): %s ", fsp->fsp_name,
65 return map_nt_error_from_unix(errno);
67 /* create SMB4ACL data */
68 if((pacl = smb_create_smb4acl()) == NULL) {
69 return NT_STATUS_NO_MEMORY;
71 for(i=0; i<naces; i++) {
72 SMB_ACE4PROP_T aceprop;
74 aceprop.aceType = (uint32) acebuf[i].a_type;
75 aceprop.aceFlags = (uint32) acebuf[i].a_flags;
76 aceprop.aceMask = (uint32) acebuf[i].a_access_mask;
77 aceprop.who.id = (uint32) acebuf[i].a_who;
79 if(aceprop.aceFlags & ACE_OWNER) {
80 aceprop.flags = SMB_ACE4_ID_SPECIAL;
81 aceprop.who.special_id = SMB_ACE4_WHO_OWNER;
82 } else if(aceprop.aceFlags & ACE_GROUP) {
83 aceprop.flags = SMB_ACE4_ID_SPECIAL;
84 aceprop.who.special_id = SMB_ACE4_WHO_GROUP;
85 } else if(aceprop.aceFlags & ACE_EVERYONE) {
86 aceprop.flags = SMB_ACE4_ID_SPECIAL;
87 aceprop.who.special_id = SMB_ACE4_WHO_EVERYONE;
91 if(smb_add_ace4(pacl, &aceprop) == NULL)
92 return NT_STATUS_NO_MEMORY;
95 return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
98 /* call-back function processing the NT acl -> ZFS acl using NFSv4 conv. */
99 static bool zfs_process_smbacl(files_struct *fsp, SMB4ACL_T *smbacl)
101 int naces = smb_get_naces(smbacl), i;
106 /* allocate the field of ZFS aces */
107 mem_ctx = talloc_tos();
108 acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
113 /* handle all aces */
114 for(smbace = smb_first_ace4(smbacl), i = 0;
116 smbace = smb_next_ace4(smbace), i++) {
117 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
119 acebuf[i].a_type = aceprop->aceType;
120 acebuf[i].a_flags = aceprop->aceFlags;
121 acebuf[i].a_access_mask = aceprop->aceMask;
122 acebuf[i].a_who = aceprop->who.id;
123 if(aceprop->flags & SMB_ACE4_ID_SPECIAL) {
124 switch(aceprop->who.special_id) {
125 case SMB_ACE4_WHO_EVERYONE:
126 acebuf[i].a_flags |= ACE_EVERYONE;
128 case SMB_ACE4_WHO_OWNER:
129 acebuf[i].a_flags |= ACE_OWNER;
131 case SMB_ACE4_WHO_GROUP:
132 acebuf[i].a_flags |= ACE_GROUP;
135 DEBUG(8, ("unsupported special_id %d\n", \
136 aceprop->who.special_id));
137 continue; /* don't add it !!! */
141 SMB_ASSERT(i == naces);
144 if(acl(fsp->fsp_name, ACE_SETACL, naces, acebuf)) {
145 if(errno == ENOSYS) {
146 DEBUG(9, ("acl(ACE_SETACL, %s): Operation is not supported on the filesystem where the file reside"));
148 DEBUG(9, ("acl(ACE_SETACL, %s): %s ", fsp->fsp_name,
158 * set the local file's acls obtaining it in NT form
159 * using the NFSv4 format conversion
161 static NTSTATUS zfs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
162 uint32 security_info_sent,
163 struct security_descriptor *psd)
165 return smb_set_nt_acl_nfs4(fsp, security_info_sent, psd,
169 static NTSTATUS zfsacl_fget_nt_acl(struct vfs_handle_struct *handle,
170 struct files_struct *fsp,
171 int fd, uint32 security_info,
172 struct security_descriptor **ppdesc)
174 return zfs_get_nt_acl(fsp, security_info, ppdesc);
177 static NTSTATUS zfsacl_get_nt_acl(struct vfs_handle_struct *handle,
178 struct files_struct *fsp,
179 const char *name, uint32 security_info,
180 struct security_descriptor **ppdesc)
182 return zfs_get_nt_acl(fsp, security_info, ppdesc);
185 static NTSTATUS zfsacl_fset_nt_acl(vfs_handle_struct *handle,
187 int fd, uint32 security_info_sent,
190 return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
193 static NTSTATUS zfsacl_set_nt_acl(vfs_handle_struct *handle,
195 const char *name, uint32 security_info_sent,
198 return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
201 /* VFS operations structure */
203 static vfs_op_tuple zfsacl_ops[] = {
204 {SMB_VFS_OP(zfsacl_fget_nt_acl), SMB_VFS_OP_FGET_NT_ACL,
205 SMB_VFS_LAYER_OPAQUE},
206 {SMB_VFS_OP(zfsacl_get_nt_acl), SMB_VFS_OP_GET_NT_ACL,
207 SMB_VFS_LAYER_OPAQUE},
208 {SMB_VFS_OP(zfsacl_fset_nt_acl), SMB_VFS_OP_FSET_NT_ACL,
209 SMB_VFS_LAYER_OPAQUE},
210 {SMB_VFS_OP(zfsacl_set_nt_acl), SMB_VFS_OP_SET_NT_ACL,
211 SMB_VFS_LAYER_OPAQUE},
212 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
215 /* != 0 if this module will be compiled as static */
220 NTSTATUS vfs_zfsacl_init(void);
222 NTSTATUS init_module(void);
233 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "zfsacl",