vfs_zfsacl: pass nfs4_params to smb_set_nt_acl_nfs4()
[samba.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 #ifdef 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 struct zfsacl_config_data {
40         struct smbacl4_vfs_params nfs4_params;
41         bool zfsacl_denymissingspecial;
42 };
43
44 /* zfs_get_nt_acl()
45  * read the local file's acls and return it in NT form
46  * using the NFSv4 format conversion
47  */
48 static NTSTATUS zfs_get_nt_acl_common(struct connection_struct *conn,
49                                       TALLOC_CTX *mem_ctx,
50                                       const struct smb_filename *smb_fname,
51                                       struct SMB4ACL_T **ppacl)
52 {
53         int naces, i;
54         ace_t *acebuf;
55         struct SMB4ACL_T *pacl;
56         SMB_STRUCT_STAT sbuf;
57         const SMB_STRUCT_STAT *psbuf = NULL;
58         int ret;
59         bool is_dir;
60
61         if (VALID_STAT(smb_fname->st)) {
62                 psbuf = &smb_fname->st;
63         }
64
65         if (psbuf == NULL) {
66                 ret = vfs_stat_smb_basename(conn, smb_fname, &sbuf);
67                 if (ret != 0) {
68                         DBG_INFO("stat [%s]failed: %s\n",
69                                  smb_fname_str_dbg(smb_fname), strerror(errno));
70                         return map_nt_error_from_unix(errno);
71                 }
72                 psbuf = &sbuf;
73         }
74         is_dir = S_ISDIR(psbuf->st_ex_mode);
75
76         /* read the number of file aces */
77         if((naces = acl(smb_fname->base_name, ACE_GETACLCNT, 0, NULL)) == -1) {
78                 if(errno == ENOSYS) {
79                         DEBUG(9, ("acl(ACE_GETACLCNT, %s): Operation is not "
80                                   "supported on the filesystem where the file "
81                                   "reside\n", smb_fname->base_name));
82                 } else {
83                         DEBUG(9, ("acl(ACE_GETACLCNT, %s): %s ", smb_fname->base_name,
84                                         strerror(errno)));
85                 }
86                 return map_nt_error_from_unix(errno);
87         }
88         /* allocate the field of ZFS aces */
89         mem_ctx = talloc_tos();
90         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
91         if(acebuf == NULL) {
92                 return NT_STATUS_NO_MEMORY;
93         }
94         /* read the aces into the field */
95         if(acl(smb_fname->base_name, ACE_GETACL, naces, acebuf) < 0) {
96                 DEBUG(9, ("acl(ACE_GETACL, %s): %s ", smb_fname->base_name,
97                                 strerror(errno)));
98                 return map_nt_error_from_unix(errno);
99         }
100         /* create SMB4ACL data */
101         if((pacl = smb_create_smb4acl(mem_ctx)) == NULL) {
102                 return NT_STATUS_NO_MEMORY;
103         }
104         for(i=0; i<naces; i++) {
105                 SMB_ACE4PROP_T aceprop;
106
107                 aceprop.aceType  = (uint32_t) acebuf[i].a_type;
108                 aceprop.aceFlags = (uint32_t) acebuf[i].a_flags;
109                 aceprop.aceMask  = (uint32_t) acebuf[i].a_access_mask;
110                 aceprop.who.id   = (uint32_t) acebuf[i].a_who;
111
112                 /*
113                  * Windows clients expect SYNC on acls to correctly allow
114                  * rename, cf bug #7909. But not on DENY ace entries, cf bug
115                  * #8442.
116                  */
117                 if (aceprop.aceType == SMB_ACE4_ACCESS_ALLOWED_ACE_TYPE) {
118                         aceprop.aceMask |= SMB_ACE4_SYNCHRONIZE;
119                 }
120
121                 if (is_dir && (aceprop.aceMask & SMB_ACE4_ADD_FILE)) {
122                         aceprop.aceMask |= SMB_ACE4_DELETE_CHILD;
123                 }
124
125                 if(aceprop.aceFlags & ACE_OWNER) {
126                         aceprop.flags = SMB_ACE4_ID_SPECIAL;
127                         aceprop.who.special_id = SMB_ACE4_WHO_OWNER;
128                 } else if(aceprop.aceFlags & ACE_GROUP) {
129                         aceprop.flags = SMB_ACE4_ID_SPECIAL;
130                         aceprop.who.special_id = SMB_ACE4_WHO_GROUP;
131                 } else if(aceprop.aceFlags & ACE_EVERYONE) {
132                         aceprop.flags = SMB_ACE4_ID_SPECIAL;
133                         aceprop.who.special_id = SMB_ACE4_WHO_EVERYONE;
134                 } else {
135                         aceprop.flags   = 0;
136                 }
137                 if(smb_add_ace4(pacl, &aceprop) == NULL)
138                         return NT_STATUS_NO_MEMORY;
139         }
140
141         *ppacl = pacl;
142         return NT_STATUS_OK;
143 }
144
145 /* call-back function processing the NT acl -> ZFS acl using NFSv4 conv. */
146 static bool zfs_process_smbacl(vfs_handle_struct *handle, files_struct *fsp,
147                                struct SMB4ACL_T *smbacl)
148 {
149         int naces = smb_get_naces(smbacl), i;
150         ace_t *acebuf;
151         struct SMB4ACE_T *smbace;
152         TALLOC_CTX      *mem_ctx;
153         bool have_special_id = false;
154         struct zfsacl_config_data *config = NULL;
155
156         SMB_VFS_HANDLE_GET_DATA(handle, config,
157                                 struct zfsacl_config_data,
158                                 return False);
159
160         /* allocate the field of ZFS aces */
161         mem_ctx = talloc_tos();
162         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
163         if(acebuf == NULL) {
164                 errno = ENOMEM;
165                 return False;
166         }
167         /* handle all aces */
168         for(smbace = smb_first_ace4(smbacl), i = 0;
169                         smbace!=NULL;
170                         smbace = smb_next_ace4(smbace), i++) {
171                 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
172
173                 acebuf[i].a_type        = aceprop->aceType;
174                 acebuf[i].a_flags       = aceprop->aceFlags;
175                 acebuf[i].a_access_mask = aceprop->aceMask;
176                 /* SYNC on acls is a no-op on ZFS.
177                    See bug #7909. */
178                 acebuf[i].a_access_mask &= ~SMB_ACE4_SYNCHRONIZE;
179                 acebuf[i].a_who         = aceprop->who.id;
180                 if(aceprop->flags & SMB_ACE4_ID_SPECIAL) {
181                         switch(aceprop->who.special_id) {
182                         case SMB_ACE4_WHO_EVERYONE:
183                                 acebuf[i].a_flags |= ACE_EVERYONE;
184                                 break;
185                         case SMB_ACE4_WHO_OWNER:
186                                 acebuf[i].a_flags |= ACE_OWNER;
187                                 break;
188                         case SMB_ACE4_WHO_GROUP:
189                                 acebuf[i].a_flags |= ACE_GROUP|ACE_IDENTIFIER_GROUP;
190                                 break;
191                         default:
192                                 DEBUG(8, ("unsupported special_id %d\n", \
193                                         aceprop->who.special_id));
194                                 continue; /* don't add it !!! */
195                         }
196                         have_special_id = true;
197                 }
198         }
199
200         if (!have_special_id && config->zfsacl_denymissingspecial) {
201                 errno = EACCES;
202                 return false;
203         }
204
205         SMB_ASSERT(i == naces);
206
207         /* store acl */
208         if(acl(fsp->fsp_name->base_name, ACE_SETACL, naces, acebuf)) {
209                 if(errno == ENOSYS) {
210                         DEBUG(9, ("acl(ACE_SETACL, %s): Operation is not "
211                                   "supported on the filesystem where the file "
212                                   "reside", fsp_str_dbg(fsp)));
213                 } else {
214                         DEBUG(9, ("acl(ACE_SETACL, %s): %s ", fsp_str_dbg(fsp),
215                                   strerror(errno)));
216                 }
217                 return 0;
218         }
219
220         return True;
221 }
222
223 /* zfs_set_nt_acl()
224  * set the local file's acls obtaining it in NT form
225  * using the NFSv4 format conversion
226  */
227 static NTSTATUS zfs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
228                            uint32_t security_info_sent,
229                            const struct security_descriptor *psd)
230 {
231         struct zfsacl_config_data *config = NULL;
232
233         SMB_VFS_HANDLE_GET_DATA(handle, config,
234                                 struct zfsacl_config_data,
235                                 return NT_STATUS_INTERNAL_ERROR);
236
237         return smb_set_nt_acl_nfs4(handle,
238                                 fsp,
239                                 &config->nfs4_params,
240                                 security_info_sent,
241                                 psd,
242                                 zfs_process_smbacl);
243 }
244
245 static NTSTATUS zfsacl_fget_nt_acl(struct vfs_handle_struct *handle,
246                                    struct files_struct *fsp,
247                                    uint32_t security_info,
248                                    TALLOC_CTX *mem_ctx,
249                                    struct security_descriptor **ppdesc)
250 {
251         struct SMB4ACL_T *pacl;
252         NTSTATUS status;
253         TALLOC_CTX *frame = talloc_stackframe();
254
255         status = zfs_get_nt_acl_common(handle->conn, frame,
256                                        fsp->fsp_name, &pacl);
257         if (!NT_STATUS_IS_OK(status)) {
258                 TALLOC_FREE(frame);
259                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
260                         return status;
261                 }
262
263                 status = make_default_filesystem_acl(mem_ctx,
264                                                      DEFAULT_ACL_POSIX,
265                                                      fsp->fsp_name->base_name,
266                                                      &fsp->fsp_name->st,
267                                                      ppdesc);
268                 if (!NT_STATUS_IS_OK(status)) {
269                         return status;
270                 }
271                 (*ppdesc)->type |= SEC_DESC_DACL_PROTECTED;
272                 return NT_STATUS_OK;
273         }
274
275         status = smb_fget_nt_acl_nfs4(fsp, NULL, security_info, mem_ctx,
276                                       ppdesc, pacl);
277         TALLOC_FREE(frame);
278         return status;
279 }
280
281 static NTSTATUS zfsacl_get_nt_acl(struct vfs_handle_struct *handle,
282                                 const struct smb_filename *smb_fname,
283                                 uint32_t security_info,
284                                 TALLOC_CTX *mem_ctx,
285                                 struct security_descriptor **ppdesc)
286 {
287         struct SMB4ACL_T *pacl;
288         NTSTATUS status;
289         TALLOC_CTX *frame = talloc_stackframe();
290
291         status = zfs_get_nt_acl_common(handle->conn, frame, smb_fname, &pacl);
292         if (!NT_STATUS_IS_OK(status)) {
293                 TALLOC_FREE(frame);
294                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
295                         return status;
296                 }
297
298                 if (!VALID_STAT(smb_fname->st)) {
299                         DBG_ERR("No stat info for [%s]\n",
300                                 smb_fname_str_dbg(smb_fname));
301                         return NT_STATUS_INTERNAL_ERROR;
302                 }
303
304                 status = make_default_filesystem_acl(mem_ctx,
305                                                      DEFAULT_ACL_POSIX,
306                                                      smb_fname->base_name,
307                                                      &smb_fname->st,
308                                                      ppdesc);
309                 if (!NT_STATUS_IS_OK(status)) {
310                         return status;
311                 }
312                 (*ppdesc)->type |= SEC_DESC_DACL_PROTECTED;
313                 return NT_STATUS_OK;
314         }
315
316         status = smb_get_nt_acl_nfs4(handle->conn,
317                                         smb_fname,
318                                         NULL,
319                                         security_info,
320                                         mem_ctx,
321                                         ppdesc,
322                                         pacl);
323         TALLOC_FREE(frame);
324         return status;
325 }
326
327 static NTSTATUS zfsacl_fset_nt_acl(vfs_handle_struct *handle,
328                          files_struct *fsp,
329                          uint32_t security_info_sent,
330                          const struct security_descriptor *psd)
331 {
332         return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
333 }
334
335 /* nils.goroll@hamburg.de 2008-06-16 :
336
337    See also
338    - https://bugzilla.samba.org/show_bug.cgi?id=5446
339    - http://bugs.opensolaris.org/view_bug.do?bug_id=6688240
340
341    Solaris supports NFSv4 and ZFS ACLs through a common system call, acl(2)
342    with ACE_SETACL / ACE_GETACL / ACE_GETACLCNT, which is being wrapped for
343    use by samba in this module.
344
345    As the acl(2) interface is identical for ZFS and for NFS, this module,
346    vfs_zfsacl, can not only be used for ZFS, but also for sharing NFSv4
347    mounts on Solaris.
348
349    But while "traditional" POSIX DRAFT ACLs (using acl(2) with SETACL
350    / GETACL / GETACLCNT) fail for ZFS, the Solaris NFS client
351    implemets a compatibility wrapper, which will make calls to
352    traditional ACL calls though vfs_solarisacl succeed. As the
353    compatibility wrapper's implementation is (by design) incomplete,
354    we want to make sure that it is never being called.
355
356    As long as Samba does not support an exiplicit method for a module
357    to define conflicting vfs methods, we should override all conflicting
358    methods here.
359
360    For this to work, we need to make sure that this module is initialised
361    *after* vfs_solarisacl
362
363    Function declarations taken from vfs_solarisacl
364 */
365
366 static SMB_ACL_T zfsacl_fail__sys_acl_get_file(vfs_handle_struct *handle,
367                                         const struct smb_filename *smb_fname,
368                                         SMB_ACL_TYPE_T type,
369                                         TALLOC_CTX *mem_ctx)
370 {
371         return (SMB_ACL_T)NULL;
372 }
373
374 static SMB_ACL_T zfsacl_fail__sys_acl_get_fd(vfs_handle_struct *handle,
375                                              files_struct *fsp,
376                                              TALLOC_CTX *mem_ctx)
377 {
378         return (SMB_ACL_T)NULL;
379 }
380
381 static int zfsacl_fail__sys_acl_set_file(vfs_handle_struct *handle,
382                                          const struct smb_filename *smb_fname,
383                                          SMB_ACL_TYPE_T type,
384                                          SMB_ACL_T theacl)
385 {
386         return -1;
387 }
388
389 static int zfsacl_fail__sys_acl_set_fd(vfs_handle_struct *handle,
390                                        files_struct *fsp,
391                                        SMB_ACL_T theacl)
392 {
393         return -1;
394 }
395
396 static int zfsacl_fail__sys_acl_delete_def_file(vfs_handle_struct *handle,
397                         const struct smb_filename *smb_fname)
398 {
399         return -1;
400 }
401
402 static int zfsacl_fail__sys_acl_blob_get_file(vfs_handle_struct *handle,
403                         const struct smb_filename *smb_fname,
404                         TALLOC_CTX *mem_ctx,
405                         char **blob_description,
406                         DATA_BLOB *blob)
407 {
408         return -1;
409 }
410
411 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)
412 {
413         return -1;
414 }
415
416 static int zfsacl_connect(struct vfs_handle_struct *handle,
417                             const char *service, const char *user)
418 {
419         struct zfsacl_config_data *config = NULL;
420         int ret;
421
422         ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
423         if (ret < 0) {
424                 return ret;
425         }
426
427         config = talloc_zero(handle->conn, struct zfsacl_config_data);
428         if (!config) {
429                 DBG_ERR("talloc_zero() failed\n");
430                 errno = ENOMEM;
431                 return -1;
432         }
433
434         config->zfsacl_denymissingspecial = lp_parm_bool(SNUM(handle->conn),
435                                 "zfsacl", "denymissingspecial", false);
436
437         ret = smbacl4_get_vfs_params(handle->conn, &config->nfs4_params);
438         if (ret < 0) {
439                 TALLOC_FREE(config);
440                 return ret;
441         }
442
443         SMB_VFS_HANDLE_SET_DATA(handle, config,
444                                 NULL, struct zfsacl_config_data,
445                                 return -1);
446
447         return 0;
448 }
449
450 /* VFS operations structure */
451
452 static struct vfs_fn_pointers zfsacl_fns = {
453         .connect_fn = zfsacl_connect,
454         .sys_acl_get_file_fn = zfsacl_fail__sys_acl_get_file,
455         .sys_acl_get_fd_fn = zfsacl_fail__sys_acl_get_fd,
456         .sys_acl_blob_get_file_fn = zfsacl_fail__sys_acl_blob_get_file,
457         .sys_acl_blob_get_fd_fn = zfsacl_fail__sys_acl_blob_get_fd,
458         .sys_acl_set_file_fn = zfsacl_fail__sys_acl_set_file,
459         .sys_acl_set_fd_fn = zfsacl_fail__sys_acl_set_fd,
460         .sys_acl_delete_def_file_fn = zfsacl_fail__sys_acl_delete_def_file,
461         .fget_nt_acl_fn = zfsacl_fget_nt_acl,
462         .get_nt_acl_fn = zfsacl_get_nt_acl,
463         .fset_nt_acl_fn = zfsacl_fset_nt_acl,
464 };
465
466 static_decl_vfs;
467 NTSTATUS vfs_zfsacl_init(TALLOC_CTX *ctx)
468 {
469         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "zfsacl",
470                                 &zfsacl_fns);
471 }