vfs: RIP SMB_VFS_SYS_ACL_SET_FILE()
[samba.git] / source3 / modules / vfs_acl_xattr.c
1 /*
2  * Store Windows ACLs in xattrs.
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  * Copyright (C) Jeremy Allison, 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "auth.h"
26 #include "vfs_acl_common.h"
27
28 /* Pull in the common functions. */
29 #define ACL_MODULE_NAME "acl_xattr"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_VFS
33
34 /*******************************************************************
35  Pull a security descriptor into a DATA_BLOB from a xattr.
36 *******************************************************************/
37
38 static ssize_t getxattr_do(vfs_handle_struct *handle,
39                            files_struct *fsp,
40                            const struct smb_filename *smb_fname,
41                            const char *xattr_name,
42                            uint8_t *val,
43                            size_t size)
44 {
45         ssize_t sizeret;
46         int saved_errno = 0;
47
48         become_root();
49         if (fsp && fsp_get_pathref_fd(fsp) != -1) {
50                 sizeret = SMB_VFS_FGETXATTR(fsp, xattr_name, val, size);
51         } else {
52                 sizeret = SMB_VFS_GETXATTR(handle->conn, smb_fname,
53                                            XATTR_NTACL_NAME, val, size);
54         }
55         if (sizeret == -1) {
56                 saved_errno = errno;
57         }
58         unbecome_root();
59
60         if (saved_errno != 0) {
61                 errno = saved_errno;
62         }
63
64         return sizeret;
65 }
66
67 static NTSTATUS fget_acl_blob(TALLOC_CTX *ctx,
68                         vfs_handle_struct *handle,
69                         files_struct *fsp,
70                         DATA_BLOB *pblob)
71 {
72         size_t size = 4096;
73         uint8_t *val = NULL;
74         uint8_t *tmp;
75         ssize_t sizeret;
76
77         ZERO_STRUCTP(pblob);
78
79   again:
80
81         tmp = talloc_realloc(ctx, val, uint8_t, size);
82         if (tmp == NULL) {
83                 TALLOC_FREE(val);
84                 return NT_STATUS_NO_MEMORY;
85         }
86         val = tmp;
87
88         sizeret =
89             getxattr_do(handle, fsp, NULL, XATTR_NTACL_NAME, val, size);
90
91         if (sizeret >= 0) {
92                 pblob->data = val;
93                 pblob->length = sizeret;
94                 return NT_STATUS_OK;
95         }
96
97         if (errno != ERANGE) {
98                 goto err;
99         }
100
101         /* Too small, try again. */
102         sizeret =
103             getxattr_do(handle, fsp, NULL, XATTR_NTACL_NAME, NULL, 0);
104         if (sizeret < 0) {
105                 goto err;
106         }
107
108         if (size < sizeret) {
109                 size = sizeret;
110         }
111
112         if (size > 65536) {
113                 /* Max ACL size is 65536 bytes. */
114                 errno = ERANGE;
115                 goto err;
116         }
117
118         goto again;
119   err:
120         /* Real error - exit here. */
121         TALLOC_FREE(val);
122         return map_nt_error_from_unix(errno);
123 }
124
125 static NTSTATUS get_acl_blob_at(TALLOC_CTX *ctx,
126                         vfs_handle_struct *handle,
127                         struct files_struct *dirfsp,
128                         const struct smb_filename *smb_fname,
129                         DATA_BLOB *pblob)
130 {
131         size_t size = 4096;
132         uint8_t *val = NULL;
133         uint8_t *tmp;
134         ssize_t sizeret;
135
136         ZERO_STRUCTP(pblob);
137
138   again:
139
140         tmp = talloc_realloc(ctx, val, uint8_t, size);
141         if (tmp == NULL) {
142                 TALLOC_FREE(val);
143                 return NT_STATUS_NO_MEMORY;
144         }
145         val = tmp;
146
147         sizeret =
148             getxattr_do(handle, NULL, smb_fname, XATTR_NTACL_NAME, val, size);
149
150         if (sizeret >= 0) {
151                 pblob->data = val;
152                 pblob->length = sizeret;
153                 return NT_STATUS_OK;
154         }
155
156         if (errno != ERANGE) {
157                 goto err;
158         }
159
160         /* Too small, try again. */
161         sizeret =
162             getxattr_do(handle, NULL, smb_fname, XATTR_NTACL_NAME, NULL, 0);
163         if (sizeret < 0) {
164                 goto err;
165         }
166
167         if (size < sizeret) {
168                 size = sizeret;
169         }
170
171         if (size > 65536) {
172                 /* Max ACL size is 65536 bytes. */
173                 errno = ERANGE;
174                 goto err;
175         }
176
177         goto again;
178   err:
179         /* Real error - exit here. */
180         TALLOC_FREE(val);
181         return map_nt_error_from_unix(errno);
182 }
183
184 /*******************************************************************
185  Store a DATA_BLOB into an xattr given an fsp pointer.
186 *******************************************************************/
187
188 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
189                                 files_struct *fsp,
190                                 DATA_BLOB *pblob)
191 {
192         int ret;
193         int saved_errno = 0;
194
195         DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
196                   (unsigned int)pblob->length, fsp_str_dbg(fsp)));
197
198         become_root();
199         if (fsp_get_pathref_fd(fsp) != -1) {
200                 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
201                         pblob->data, pblob->length, 0);
202         } else {
203                 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name,
204                                 XATTR_NTACL_NAME,
205                                 pblob->data, pblob->length, 0);
206         }
207         if (ret) {
208                 saved_errno = errno;
209         }
210         unbecome_root();
211         if (ret) {
212                 DEBUG(5, ("store_acl_blob_fsp: setting attr failed for file %s"
213                         "with error %s\n",
214                         fsp_str_dbg(fsp),
215                         strerror(saved_errno) ));
216                 errno = saved_errno;
217                 return map_nt_error_from_unix(saved_errno);
218         }
219         return NT_STATUS_OK;
220 }
221
222 /*********************************************************************
223  Remove a Windows ACL - we're setting the underlying POSIX ACL.
224 *********************************************************************/
225
226 static int sys_acl_set_fd_xattr(vfs_handle_struct *handle,
227                             files_struct *fsp,
228                             SMB_ACL_TYPE_T type,
229                             SMB_ACL_T theacl)
230 {
231         int ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
232                                                 fsp,
233                                                 type,
234                                                 theacl);
235         if (ret == -1) {
236                 return -1;
237         }
238
239         become_root();
240         SMB_VFS_FREMOVEXATTR(fsp, XATTR_NTACL_NAME);
241         unbecome_root();
242
243         return ret;
244 }
245
246 static int connect_acl_xattr(struct vfs_handle_struct *handle,
247                                 const char *service,
248                                 const char *user)
249 {
250         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
251         bool ok;
252         struct acl_common_config *config = NULL;
253
254         if (ret < 0) {
255                 return ret;
256         }
257
258         ok = init_acl_common_config(handle, ACL_MODULE_NAME);
259         if (!ok) {
260                 DBG_ERR("init_acl_common_config failed\n");
261                 return -1;
262         }
263
264         /* Ensure we have the parameters correct if we're
265          * using this module. */
266         DEBUG(2,("connect_acl_xattr: setting 'inherit acls = true' "
267                 "'dos filemode = true' and "
268                 "'force unknown acl user = true' for service %s\n",
269                 service ));
270
271         lp_do_parameter(SNUM(handle->conn), "inherit acls", "true");
272         lp_do_parameter(SNUM(handle->conn), "dos filemode", "true");
273         lp_do_parameter(SNUM(handle->conn), "force unknown acl user", "true");
274
275         SMB_VFS_HANDLE_GET_DATA(handle, config,
276                                 struct acl_common_config,
277                                 return -1);
278
279         if (config->ignore_system_acls) {
280                 mode_t create_mask = lp_create_mask(SNUM(handle->conn));
281                 char *create_mask_str = NULL;
282
283                 if ((create_mask & 0666) != 0666) {
284                         create_mask |= 0666;
285                         create_mask_str = talloc_asprintf(handle, "0%o",
286                                                           create_mask);
287                         if (create_mask_str == NULL) {
288                                 DBG_ERR("talloc_asprintf failed\n");
289                                 return -1;
290                         }
291
292                         DBG_NOTICE("setting 'create mask = %s'\n", create_mask_str);
293
294                         lp_do_parameter (SNUM(handle->conn),
295                                         "create mask", create_mask_str);
296
297                         TALLOC_FREE(create_mask_str);
298                 }
299
300                 DBG_NOTICE("setting 'directory mask = 0777', "
301                            "'store dos attributes = yes' and all "
302                            "'map ...' options to 'no'\n");
303
304                 lp_do_parameter(SNUM(handle->conn), "directory mask", "0777");
305                 lp_do_parameter(SNUM(handle->conn), "map archive", "no");
306                 lp_do_parameter(SNUM(handle->conn), "map hidden", "no");
307                 lp_do_parameter(SNUM(handle->conn), "map readonly", "no");
308                 lp_do_parameter(SNUM(handle->conn), "map system", "no");
309                 lp_do_parameter(SNUM(handle->conn), "store dos attributes",
310                                 "yes");
311         }
312
313         return 0;
314 }
315
316 static int acl_xattr_unlinkat(vfs_handle_struct *handle,
317                         struct files_struct *dirfsp,
318                         const struct smb_filename *smb_fname,
319                         int flags)
320 {
321         int ret;
322
323         if (flags & AT_REMOVEDIR) {
324                 ret = rmdir_acl_common(handle,
325                                 dirfsp,
326                                 smb_fname);
327         } else {
328                 ret = unlink_acl_common(handle,
329                                 dirfsp,
330                                 smb_fname,
331                                 flags);
332         }
333         return ret;
334 }
335
336 static NTSTATUS acl_xattr_fget_nt_acl(vfs_handle_struct *handle,
337                                       files_struct *fsp,
338                                       uint32_t security_info,
339                                       TALLOC_CTX *mem_ctx,
340                                       struct security_descriptor **ppdesc)
341 {
342         NTSTATUS status;
343         status = fget_nt_acl_common(fget_acl_blob, handle, fsp,
344                                    security_info, mem_ctx, ppdesc);
345         return status;
346 }
347
348 static NTSTATUS acl_xattr_get_nt_acl_at(vfs_handle_struct *handle,
349                                 struct files_struct *dirfsp,
350                                 const struct smb_filename *smb_fname,
351                                 uint32_t security_info,
352                                 TALLOC_CTX *mem_ctx,
353                                 struct security_descriptor **ppdesc)
354 {
355         NTSTATUS status;
356         status = get_nt_acl_common_at(get_acl_blob_at,
357                                 handle,
358                                 dirfsp,
359                                 smb_fname,
360                                 security_info,
361                                 mem_ctx,
362                                 ppdesc);
363         return status;
364 }
365
366 static NTSTATUS acl_xattr_fset_nt_acl(vfs_handle_struct *handle,
367                                       files_struct *fsp,
368                                       uint32_t security_info_sent,
369                                       const struct security_descriptor *psd)
370 {
371         NTSTATUS status;
372         status = fset_nt_acl_common(fget_acl_blob, store_acl_blob_fsp,
373                                     ACL_MODULE_NAME,
374                                     handle, fsp, security_info_sent, psd);
375         return status;
376 }
377
378 static struct vfs_fn_pointers vfs_acl_xattr_fns = {
379         .connect_fn = connect_acl_xattr,
380         .unlinkat_fn = acl_xattr_unlinkat,
381         .chmod_fn = chmod_acl_module_common,
382         .fchmod_fn = fchmod_acl_module_common,
383         .fget_nt_acl_fn = acl_xattr_fget_nt_acl,
384         .get_nt_acl_at_fn = acl_xattr_get_nt_acl_at,
385         .fset_nt_acl_fn = acl_xattr_fset_nt_acl,
386         .sys_acl_set_fd_fn = sys_acl_set_fd_xattr
387 };
388
389 static_decl_vfs;
390 NTSTATUS vfs_acl_xattr_init(TALLOC_CTX *ctx)
391 {
392         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr",
393                                 &vfs_acl_xattr_fns);
394 }