s3-vfs: include smbd/smbd.h in vfs modules.
[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 "smbd/smbd.h"
27 #include "nfs4_acls.h"
28
29 #if HAVE_FREEBSD_SUNACL_H
30 #include "sunacl.h"
31 #endif
32
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_VFS
35
36 #define ZFSACL_MODULE_NAME "zfsacl"
37
38 /* zfs_get_nt_acl()
39  * read the local file's acls and return it in NT form
40  * using the NFSv4 format conversion
41  */
42 static NTSTATUS zfs_get_nt_acl_common(const char *name,
43                                       uint32 security_info,
44                                       SMB4ACL_T **ppacl)
45 {
46         int naces, i;
47         ace_t *acebuf;
48         SMB4ACL_T *pacl;
49         TALLOC_CTX      *mem_ctx;
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", 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()) == 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) acebuf[i].a_type;
83                 aceprop.aceFlags = (uint32) acebuf[i].a_flags;
84                 aceprop.aceMask  = (uint32) acebuf[i].a_access_mask;
85                 aceprop.who.id   = (uint32) 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(files_struct *fsp, SMB4ACL_T *smbacl)
109 {
110         int naces = smb_get_naces(smbacl), i;
111         ace_t *acebuf;
112         SMB4ACE_T *smbace;
113         TALLOC_CTX      *mem_ctx;
114         bool have_special_id = false;
115
116         /* allocate the field of ZFS aces */
117         mem_ctx = talloc_tos();
118         acebuf = (ace_t *) talloc_size(mem_ctx, sizeof(ace_t)*naces);
119         if(acebuf == NULL) {
120                 errno = ENOMEM;
121                 return False;
122         }
123         /* handle all aces */
124         for(smbace = smb_first_ace4(smbacl), i = 0;
125                         smbace!=NULL;
126                         smbace = smb_next_ace4(smbace), i++) {
127                 SMB_ACE4PROP_T *aceprop = smb_get_ace4(smbace);
128
129                 acebuf[i].a_type        = aceprop->aceType;
130                 acebuf[i].a_flags       = aceprop->aceFlags;
131                 acebuf[i].a_access_mask = aceprop->aceMask;
132                 /* SYNC on acls is a no-op on ZFS.
133                    See bug #7909. */
134                 acebuf[i].a_access_mask &= ~SMB_ACE4_SYNCHRONIZE;
135                 acebuf[i].a_who         = aceprop->who.id;
136                 if(aceprop->flags & SMB_ACE4_ID_SPECIAL) {
137                         switch(aceprop->who.special_id) {
138                         case SMB_ACE4_WHO_EVERYONE:
139                                 acebuf[i].a_flags |= ACE_EVERYONE;
140                                 break;
141                         case SMB_ACE4_WHO_OWNER:
142                                 acebuf[i].a_flags |= ACE_OWNER;
143                                 break;
144                         case SMB_ACE4_WHO_GROUP:
145                                 acebuf[i].a_flags |= ACE_GROUP;
146                                 break;
147                         default:
148                                 DEBUG(8, ("unsupported special_id %d\n", \
149                                         aceprop->who.special_id));
150                                 continue; /* don't add it !!! */
151                         }
152                         have_special_id = true;
153                 }
154         }
155
156         if (!have_special_id
157             && lp_parm_bool(fsp->conn->params->service, "zfsacl",
158                             "denymissingspecial", false)) {
159                 errno = EACCES;
160                 return false;
161         }
162
163         SMB_ASSERT(i == naces);
164
165         /* store acl */
166         if(acl(fsp->fsp_name->base_name, ACE_SETACL, naces, acebuf)) {
167                 if(errno == ENOSYS) {
168                         DEBUG(9, ("acl(ACE_SETACL, %s): Operation is not "
169                                   "supported on the filesystem where the file "
170                                   "reside", fsp_str_dbg(fsp)));
171                 } else {
172                         DEBUG(9, ("acl(ACE_SETACL, %s): %s ", fsp_str_dbg(fsp),
173                                   strerror(errno)));
174                 }
175                 return 0;
176         }
177
178         return True;
179 }
180
181 /* zfs_set_nt_acl()
182  * set the local file's acls obtaining it in NT form
183  * using the NFSv4 format conversion
184  */
185 static NTSTATUS zfs_set_nt_acl(vfs_handle_struct *handle, files_struct *fsp,
186                            uint32 security_info_sent,
187                            const struct security_descriptor *psd)
188 {
189         return smb_set_nt_acl_nfs4(fsp, security_info_sent, psd,
190                         zfs_process_smbacl);
191 }
192
193 static NTSTATUS zfsacl_fget_nt_acl(struct vfs_handle_struct *handle,
194                                  struct files_struct *fsp,
195                                  uint32 security_info,
196                                  struct security_descriptor **ppdesc)
197 {
198         SMB4ACL_T *pacl;
199         NTSTATUS status;
200
201         status = zfs_get_nt_acl_common(fsp->fsp_name->base_name, security_info,
202                                        &pacl);
203         if (!NT_STATUS_IS_OK(status)) {
204                 return status;
205         }
206
207         return smb_fget_nt_acl_nfs4(fsp, security_info, ppdesc, pacl);
208 }
209
210 static NTSTATUS zfsacl_get_nt_acl(struct vfs_handle_struct *handle,
211                                 const char *name,  uint32 security_info,
212                                 struct security_descriptor **ppdesc)
213 {
214         SMB4ACL_T *pacl;
215         NTSTATUS status;
216
217         status = zfs_get_nt_acl_common(name, security_info, &pacl);
218         if (!NT_STATUS_IS_OK(status)) {
219                 return status;
220         }
221
222         return smb_get_nt_acl_nfs4(handle->conn, name, security_info, ppdesc,
223                                    pacl);
224 }
225
226 static NTSTATUS zfsacl_fset_nt_acl(vfs_handle_struct *handle,
227                          files_struct *fsp,
228                          uint32 security_info_sent,
229                          const struct security_descriptor *psd)
230 {
231         return zfs_set_nt_acl(handle, fsp, security_info_sent, psd);
232 }
233
234 /* nils.goroll@hamburg.de 2008-06-16 :
235
236    See also
237    - https://bugzilla.samba.org/show_bug.cgi?id=5446
238    - http://bugs.opensolaris.org/view_bug.do?bug_id=6688240
239
240    Solaris supports NFSv4 and ZFS ACLs through a common system call, acl(2)
241    with ACE_SETACL / ACE_GETACL / ACE_GETACLCNT, which is being wrapped for
242    use by samba in this module.
243
244    As the acl(2) interface is identical for ZFS and for NFS, this module,
245    vfs_zfsacl, can not only be used for ZFS, but also for sharing NFSv4
246    mounts on Solaris.
247
248    But while "traditional" POSIX DRAFT ACLs (using acl(2) with SETACL
249    / GETACL / GETACLCNT) fail for ZFS, the Solaris NFS client
250    implemets a compatibility wrapper, which will make calls to
251    traditional ACL calls though vfs_solarisacl succeed. As the
252    compatibility wrapper's implementation is (by design) incomplete,
253    we want to make sure that it is never being called.
254
255    As long as Samba does not support an exiplicit method for a module
256    to define conflicting vfs methods, we should override all conflicting
257    methods here.
258
259    For this to work, we need to make sure that this module is initialised
260    *after* vfs_solarisacl
261
262    Function declarations taken from vfs_solarisacl
263 */
264
265 static SMB_ACL_T zfsacl_fail__sys_acl_get_file(vfs_handle_struct *handle,
266                                                const char *path_p,
267                                                SMB_ACL_TYPE_T type)
268 {
269         return (SMB_ACL_T)NULL;
270 }
271
272 static SMB_ACL_T zfsacl_fail__sys_acl_get_fd(vfs_handle_struct *handle,
273                                              files_struct *fsp)
274 {
275         return (SMB_ACL_T)NULL;
276 }
277
278 static int zfsacl_fail__sys_acl_set_file(vfs_handle_struct *handle,
279                                          const char *name,
280                                          SMB_ACL_TYPE_T type,
281                                          SMB_ACL_T theacl)
282 {
283         return -1;
284 }
285
286 static int zfsacl_fail__sys_acl_set_fd(vfs_handle_struct *handle,
287                                        files_struct *fsp,
288                                        SMB_ACL_T theacl)
289 {
290         return -1;
291 }
292
293 static int zfsacl_fail__sys_acl_delete_def_file(vfs_handle_struct *handle,
294                                                 const char *path)
295 {
296         return -1;
297 }
298
299 /* VFS operations structure */
300
301 static struct vfs_fn_pointers zfsacl_fns = {
302         .sys_acl_get_file = zfsacl_fail__sys_acl_get_file,
303         .sys_acl_get_fd = zfsacl_fail__sys_acl_get_fd,
304         .sys_acl_set_file = zfsacl_fail__sys_acl_set_file,
305         .sys_acl_set_fd = zfsacl_fail__sys_acl_set_fd,
306         .sys_acl_delete_def_file = zfsacl_fail__sys_acl_delete_def_file,
307         .fget_nt_acl = zfsacl_fget_nt_acl,
308         .get_nt_acl = zfsacl_get_nt_acl,
309         .fset_nt_acl = zfsacl_fset_nt_acl,
310 };
311
312 NTSTATUS vfs_zfsacl_init(void);
313 NTSTATUS vfs_zfsacl_init(void)
314 {
315         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "zfsacl",
316                                 &zfsacl_fns);
317 }