lib: modules: Change XXX_init interface from XXX_init(void) to XXX_init(TALLOC_CTX *)
[sfrench/samba-autobuild/.git] / source3 / modules / vfs_zfsacl.c
1 /*
2  * Convert ZFS/NFSv4 acls to NT acls and vice versa.
3  *
4  * Copyright (C) Jiri Sasek, 2007
5  * based on the foobar.c module which is copyrighted by Volker Lendecke
6  *
7  * Many thanks to Axel Apitz for help to fix the special ace's handling
8  * issues.
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 #include "includes.h"
26 #include "system/filesys.h"
27 #include "smbd/smbd.h"
28 #include "nfs4_acls.h"
29
30 #if HAVE_FREEBSD_SUNACL_H
31 #include "sunacl.h"
32 #endif
33
34 #undef DBGC_CLASS
35 #define DBGC_CLASS DBGC_VFS
36
37 #define ZFSACL_MODULE_NAME "zfsacl"
38
39 /* zfs_get_nt_acl()
40  * read the local file's acls and return it in NT form
41  * using the NFSv4 format conversion
42  */
43 static NTSTATUS zfs_get_nt_acl_common(TALLOC_CTX *mem_ctx,
44                                       const char *name,
45                                       struct SMB4ACL_T **ppacl)
46 {
47         int naces, i;
48         ace_t *acebuf;
49         struct SMB4ACL_T *pacl;
50
51         /* read the number of file aces */
52         if((naces = acl(name, ACE_GETACLCNT, 0, NULL)) == -1) {
53                 if(errno == ENOSYS) {
54                         DEBUG(9, ("acl(ACE_GETACLCNT, %s): Operation is not "
55                                   "supported on the filesystem where the file "
56                                   "reside\n", name));
57                 } else {
58                         DEBUG(9, ("acl(ACE_GETACLCNT, %s): %s ", name,
59                                         strerror(errno)));
60                 }
61                 return map_nt_error_from_unix(errno);
62         }
63         /* allocate the field of ZFS aces */
64         mem_ctx = talloc_tos();
65         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
66         if(acebuf == NULL) {
67                 return NT_STATUS_NO_MEMORY;
68         }
69         /* read the aces into the field */
70         if(acl(name, ACE_GETACL, naces, acebuf) < 0) {
71                 DEBUG(9, ("acl(ACE_GETACL, %s): %s ", name,
72                                 strerror(errno)));
73                 return map_nt_error_from_unix(errno);
74         }
75         /* create SMB4ACL data */
76         if((pacl = smb_create_smb4acl(mem_ctx)) == NULL) {
77                 return NT_STATUS_NO_MEMORY;
78         }
79         for(i=0; i<naces; i++) {
80                 SMB_ACE4PROP_T aceprop;
81
82                 aceprop.aceType  = (uint32_t) acebuf[i].a_type;
83                 aceprop.aceFlags = (uint32_t) acebuf[i].a_flags;
84                 aceprop.aceMask  = (uint32_t) acebuf[i].a_access_mask;
85                 aceprop.who.id   = (uint32_t) acebuf[i].a_who;
86
87                 if(aceprop.aceFlags & ACE_OWNER) {
88                         aceprop.flags = SMB_ACE4_ID_SPECIAL;
89                         aceprop.who.special_id = SMB_ACE4_WHO_OWNER;
90                 } else if(aceprop.aceFlags & ACE_GROUP) {
91                         aceprop.flags = SMB_ACE4_ID_SPECIAL;
92                         aceprop.who.special_id = SMB_ACE4_WHO_GROUP;
93                 } else if(aceprop.aceFlags & ACE_EVERYONE) {
94                         aceprop.flags = SMB_ACE4_ID_SPECIAL;
95                         aceprop.who.special_id = SMB_ACE4_WHO_EVERYONE;
96                 } else {
97                         aceprop.flags   = 0;
98                 }
99                 if(smb_add_ace4(pacl, &aceprop) == NULL)
100                         return NT_STATUS_NO_MEMORY;
101         }
102
103         *ppacl = pacl;
104         return NT_STATUS_OK;
105 }
106
107 /* call-back function processing the NT acl -> ZFS acl using NFSv4 conv. */
108 static bool zfs_process_smbacl(vfs_handle_struct *handle, files_struct *fsp,
109                                struct SMB4ACL_T *smbacl)
110 {
111         int naces = smb_get_naces(smbacl), i;
112         ace_t *acebuf;
113         struct SMB4ACE_T *smbace;
114         TALLOC_CTX      *mem_ctx;
115         bool have_special_id = false;
116
117         /* allocate the field of ZFS aces */
118         mem_ctx = talloc_tos();
119         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
120         if(acebuf == NULL) {
121                 errno = ENOMEM;
122                 return False;
123         }
124         /* handle all aces */
125         for(smbace = smb_first_ace4(smbacl), i = 0;
126                         smbace!=NULL;
127                         smbace = smb_next_ace4(smbace), i++) {
128                 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
129
130                 acebuf[i].a_type        = aceprop->aceType;
131                 acebuf[i].a_flags       = aceprop->aceFlags;
132                 acebuf[i].a_access_mask = aceprop->aceMask;
133                 /* SYNC on acls is a no-op on ZFS.
134                    See bug #7909. */
135                 acebuf[i].a_access_mask &= ~SMB_ACE4_SYNCHRONIZE;
136                 acebuf[i].a_who         = aceprop->who.id;
137                 if(aceprop->flags & SMB_ACE4_ID_SPECIAL) {
138                         switch(aceprop->who.special_id) {
139                         case SMB_ACE4_WHO_EVERYONE:
140                                 acebuf[i].a_flags |= ACE_EVERYONE;
141                                 break;
142                         case SMB_ACE4_WHO_OWNER:
143                                 acebuf[i].a_flags |= ACE_OWNER;
144                                 break;
145                         case SMB_ACE4_WHO_GROUP:
146                                 acebuf[i].a_flags |= ACE_GROUP|ACE_IDENTIFIER_GROUP;
147                                 break;
148                         default:
149                                 DEBUG(8, ("unsupported special_id %d\n", \
150                                         aceprop->who.special_id));
151                                 continue; /* don't add it !!! */
152                         }
153                         have_special_id = true;
154                 }
155         }
156
157         if (!have_special_id
158             && lp_parm_bool(fsp->conn->params->service, "zfsacl",
159                             "denymissingspecial", false)) {
160                 errno = EACCES;
161                 return false;
162         }
163
164         SMB_ASSERT(i == naces);
165
166         /* store acl */
167         if(acl(fsp->fsp_name->base_name, ACE_SETACL, naces, acebuf)) {
168                 if(errno == ENOSYS) {
169                         DEBUG(9, ("acl(ACE_SETACL, %s): Operation is not "
170                                   "supported on the filesystem where the file "
171                                   "reside", fsp_str_dbg(fsp)));
172                 } else {
173                         DEBUG(9, ("acl(ACE_SETACL, %s): %s ", fsp_str_dbg(fsp),
174                                   strerror(errno)));
175                 }
176                 return 0;
177         }
178
179         return True;
180 }
181
182 /* zfs_set_nt_acl()
183  * set the local file's acls obtaining it in NT form
184  * using the NFSv4 format conversion
185  */
186 static NTSTATUS zfs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
187                            uint32_t security_info_sent,
188                            const struct security_descriptor *psd)
189 {
190         return smb_set_nt_acl_nfs4(handle, fsp, NULL, security_info_sent, psd,
191                                    zfs_process_smbacl);
192 }
193
194 static NTSTATUS zfsacl_fget_nt_acl(struct vfs_handle_struct *handle,
195                                    struct files_struct *fsp,
196                                    uint32_t security_info,
197                                    TALLOC_CTX *mem_ctx,
198                                    struct security_descriptor **ppdesc)
199 {
200         struct SMB4ACL_T *pacl;
201         NTSTATUS status;
202         TALLOC_CTX *frame = talloc_stackframe();
203
204         status = zfs_get_nt_acl_common(frame,
205                                        fsp->fsp_name->base_name,
206                                        &pacl);
207         if (!NT_STATUS_IS_OK(status)) {
208                 TALLOC_FREE(frame);
209                 return status;
210         }
211
212         status = smb_fget_nt_acl_nfs4(fsp, NULL, security_info, mem_ctx,
213                                       ppdesc, pacl);
214         TALLOC_FREE(frame);
215         return status;
216 }
217
218 static NTSTATUS zfsacl_get_nt_acl(struct vfs_handle_struct *handle,
219                                 const struct smb_filename *smb_fname,
220                                 uint32_t security_info,
221                                 TALLOC_CTX *mem_ctx,
222                                 struct security_descriptor **ppdesc)
223 {
224         struct SMB4ACL_T *pacl;
225         NTSTATUS status;
226         TALLOC_CTX *frame = talloc_stackframe();
227
228         status = zfs_get_nt_acl_common(frame,
229                                         smb_fname->base_name,
230                                         &pacl);
231         if (!NT_STATUS_IS_OK(status)) {
232                 TALLOC_FREE(frame);
233                 return status;
234         }
235
236         status = smb_get_nt_acl_nfs4(handle->conn,
237                                         smb_fname,
238                                         NULL,
239                                         security_info,
240                                         mem_ctx,
241                                         ppdesc,
242                                         pacl);
243         TALLOC_FREE(frame);
244         return status;
245 }
246
247 static NTSTATUS zfsacl_fset_nt_acl(vfs_handle_struct *handle,
248                          files_struct *fsp,
249                          uint32_t security_info_sent,
250                          const struct security_descriptor *psd)
251 {
252         return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
253 }
254
255 /* nils.goroll@hamburg.de 2008-06-16 :
256
257    See also
258    - https://bugzilla.samba.org/show_bug.cgi?id=5446
259    - http://bugs.opensolaris.org/view_bug.do?bug_id=6688240
260
261    Solaris supports NFSv4 and ZFS ACLs through a common system call, acl(2)
262    with ACE_SETACL / ACE_GETACL / ACE_GETACLCNT, which is being wrapped for
263    use by samba in this module.
264
265    As the acl(2) interface is identical for ZFS and for NFS, this module,
266    vfs_zfsacl, can not only be used for ZFS, but also for sharing NFSv4
267    mounts on Solaris.
268
269    But while "traditional" POSIX DRAFT ACLs (using acl(2) with SETACL
270    / GETACL / GETACLCNT) fail for ZFS, the Solaris NFS client
271    implemets a compatibility wrapper, which will make calls to
272    traditional ACL calls though vfs_solarisacl succeed. As the
273    compatibility wrapper's implementation is (by design) incomplete,
274    we want to make sure that it is never being called.
275
276    As long as Samba does not support an exiplicit method for a module
277    to define conflicting vfs methods, we should override all conflicting
278    methods here.
279
280    For this to work, we need to make sure that this module is initialised
281    *after* vfs_solarisacl
282
283    Function declarations taken from vfs_solarisacl
284 */
285
286 static SMB_ACL_T zfsacl_fail__sys_acl_get_file(vfs_handle_struct *handle,
287                                                const char *path_p,
288                                                SMB_ACL_TYPE_T type,
289                                                TALLOC_CTX *mem_ctx)
290 {
291         return (SMB_ACL_T)NULL;
292 }
293
294 static SMB_ACL_T zfsacl_fail__sys_acl_get_fd(vfs_handle_struct *handle,
295                                              files_struct *fsp,
296                                              TALLOC_CTX *mem_ctx)
297 {
298         return (SMB_ACL_T)NULL;
299 }
300
301 static int zfsacl_fail__sys_acl_set_file(vfs_handle_struct *handle,
302                                          const char *name,
303                                          SMB_ACL_TYPE_T type,
304                                          SMB_ACL_T theacl)
305 {
306         return -1;
307 }
308
309 static int zfsacl_fail__sys_acl_set_fd(vfs_handle_struct *handle,
310                                        files_struct *fsp,
311                                        SMB_ACL_T theacl)
312 {
313         return -1;
314 }
315
316 static int zfsacl_fail__sys_acl_delete_def_file(vfs_handle_struct *handle,
317                                                 const char *path)
318 {
319         return -1;
320 }
321
322 static int zfsacl_fail__sys_acl_blob_get_file(vfs_handle_struct *handle, const char *path_p, TALLOC_CTX *mem_ctx, char **blob_description, DATA_BLOB *blob)
323 {
324         return -1;
325 }
326
327 static int zfsacl_fail__sys_acl_blob_get_fd(vfs_handle_struct *handle, files_struct *fsp, TALLOC_CTX *mem_ctx, char **blob_description, DATA_BLOB *blob)
328 {
329         return -1;
330 }
331
332 /* VFS operations structure */
333
334 static struct vfs_fn_pointers zfsacl_fns = {
335         .sys_acl_get_file_fn = zfsacl_fail__sys_acl_get_file,
336         .sys_acl_get_fd_fn = zfsacl_fail__sys_acl_get_fd,
337         .sys_acl_blob_get_file_fn = zfsacl_fail__sys_acl_blob_get_file,
338         .sys_acl_blob_get_fd_fn = zfsacl_fail__sys_acl_blob_get_fd,
339         .sys_acl_set_file_fn = zfsacl_fail__sys_acl_set_file,
340         .sys_acl_set_fd_fn = zfsacl_fail__sys_acl_set_fd,
341         .sys_acl_delete_def_file_fn = zfsacl_fail__sys_acl_delete_def_file,
342         .fget_nt_acl_fn = zfsacl_fget_nt_acl,
343         .get_nt_acl_fn = zfsacl_get_nt_acl,
344         .fset_nt_acl_fn = zfsacl_fset_nt_acl,
345 };
346
347 NTSTATUS vfs_zfsacl_init(TALLOC_CTX *);
348 NTSTATUS vfs_zfsacl_init(TALLOC_CTX *ctx)
349 {
350         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "zfsacl",
351                                 &zfsacl_fns);
352 }