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