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