2 * Store Windows ACLs in xattrs.
4 * Copyright (C) Volker Lendecke, 2008
5 * Copyright (C) Jeremy Allison, 2008
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.
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.
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/>.
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "librpc/gen_ndr/xattr.h"
26 #include "vfs_acl_common.h"
28 /* Pull in the common functions. */
29 #define ACL_MODULE_NAME "acl_xattr"
32 #define DBGC_CLASS DBGC_VFS
34 /*******************************************************************
35 Pull a security descriptor into a DATA_BLOB from a xattr.
36 *******************************************************************/
38 static ssize_t getxattr_do(vfs_handle_struct *handle,
40 const struct smb_filename *smb_fname,
41 const char *xattr_name,
49 if (fsp && fsp->fh->fd != -1) {
50 sizeret = SMB_VFS_FGETXATTR(fsp, xattr_name, val, size);
52 sizeret = SMB_VFS_GETXATTR(handle->conn, smb_fname,
53 XATTR_NTACL_NAME, val, size);
60 if (saved_errno != 0) {
67 static NTSTATUS fget_acl_blob(TALLOC_CTX *ctx,
68 vfs_handle_struct *handle,
81 tmp = talloc_realloc(ctx, val, uint8_t, size);
84 return NT_STATUS_NO_MEMORY;
89 getxattr_do(handle, fsp, NULL, XATTR_NTACL_NAME, val, size);
93 pblob->length = sizeret;
97 if (errno != ERANGE) {
101 /* Too small, try again. */
103 getxattr_do(handle, fsp, NULL, XATTR_NTACL_NAME, NULL, 0);
108 if (size < sizeret) {
113 /* Max ACL size is 65536 bytes. */
120 /* Real error - exit here. */
122 return map_nt_error_from_unix(errno);
125 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
126 vfs_handle_struct *handle,
128 const struct smb_filename *smb_fname,
140 tmp = talloc_realloc(ctx, val, uint8_t, size);
143 return NT_STATUS_NO_MEMORY;
148 getxattr_do(handle, fsp, smb_fname, XATTR_NTACL_NAME, val, size);
152 pblob->length = sizeret;
156 if (errno != ERANGE) {
160 /* Too small, try again. */
162 getxattr_do(handle, fsp, smb_fname, XATTR_NTACL_NAME, NULL, 0);
167 if (size < sizeret) {
172 /* Max ACL size is 65536 bytes. */
179 /* Real error - exit here. */
181 return map_nt_error_from_unix(errno);
184 /*******************************************************************
185 Store a DATA_BLOB into an xattr given an fsp pointer.
186 *******************************************************************/
188 static NTSTATUS store_acl_blob_fsp(vfs_handle_struct *handle,
195 DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
196 (unsigned int)pblob->length, fsp_str_dbg(fsp)));
199 if (fsp->fh->fd != -1) {
200 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
201 pblob->data, pblob->length, 0);
203 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name,
205 pblob->data, pblob->length, 0);
212 DEBUG(5, ("store_acl_blob_fsp: setting attr failed for file %s"
215 strerror(saved_errno) ));
217 return map_nt_error_from_unix(saved_errno);
222 /*********************************************************************
223 Remove a Windows ACL - we're setting the underlying POSIX ACL.
224 *********************************************************************/
226 static int sys_acl_set_file_xattr(vfs_handle_struct *handle,
227 const struct smb_filename *smb_fname,
231 int ret = SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle,
240 SMB_VFS_REMOVEXATTR(handle->conn, smb_fname,
247 /*********************************************************************
248 Remove a Windows ACL - we're setting the underlying POSIX ACL.
249 *********************************************************************/
251 static int sys_acl_set_fd_xattr(vfs_handle_struct *handle,
255 int ret = SMB_VFS_NEXT_SYS_ACL_SET_FD(handle,
263 SMB_VFS_FREMOVEXATTR(fsp, XATTR_NTACL_NAME);
269 static int connect_acl_xattr(struct vfs_handle_struct *handle,
273 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
275 struct acl_common_config *config = NULL;
281 ok = init_acl_common_config(handle, ACL_MODULE_NAME);
283 DBG_ERR("init_acl_common_config failed\n");
287 /* Ensure we have the parameters correct if we're
288 * using this module. */
289 DEBUG(2,("connect_acl_xattr: setting 'inherit acls = true' "
290 "'dos filemode = true' and "
291 "'force unknown acl user = true' for service %s\n",
294 lp_do_parameter(SNUM(handle->conn), "inherit acls", "true");
295 lp_do_parameter(SNUM(handle->conn), "dos filemode", "true");
296 lp_do_parameter(SNUM(handle->conn), "force unknown acl user", "true");
298 SMB_VFS_HANDLE_GET_DATA(handle, config,
299 struct acl_common_config,
302 if (config->ignore_system_acls) {
303 mode_t create_mask = lp_create_mask(SNUM(handle->conn));
304 char *create_mask_str = NULL;
306 if ((create_mask & 0666) != 0666) {
308 create_mask_str = talloc_asprintf(handle, "0%o",
310 if (create_mask_str == NULL) {
311 DBG_ERR("talloc_asprintf failed\n");
315 DBG_NOTICE("setting 'create mask = %s'\n", create_mask_str);
317 lp_do_parameter (SNUM(handle->conn),
318 "create mask", create_mask_str);
320 TALLOC_FREE(create_mask_str);
323 DBG_NOTICE("setting 'directory mask = 0777', "
324 "'store dos attributes = yes' and all "
325 "'map ...' options to 'no'\n");
327 lp_do_parameter(SNUM(handle->conn), "directory mask", "0777");
328 lp_do_parameter(SNUM(handle->conn), "map archive", "no");
329 lp_do_parameter(SNUM(handle->conn), "map hidden", "no");
330 lp_do_parameter(SNUM(handle->conn), "map readonly", "no");
331 lp_do_parameter(SNUM(handle->conn), "map system", "no");
332 lp_do_parameter(SNUM(handle->conn), "store dos attributes",
339 static int acl_xattr_unlinkat(vfs_handle_struct *handle,
340 struct files_struct *dirfsp,
341 const struct smb_filename *smb_fname,
346 if (flags & AT_REMOVEDIR) {
347 ret = rmdir_acl_common(handle,
351 ret = unlink_acl_common(handle,
359 static NTSTATUS acl_xattr_fget_nt_acl(vfs_handle_struct *handle,
361 uint32_t security_info,
363 struct security_descriptor **ppdesc)
366 status = get_nt_acl_common(get_acl_blob, handle, fsp, NULL,
367 security_info, mem_ctx, ppdesc);
371 static NTSTATUS acl_xattr_get_nt_acl(vfs_handle_struct *handle,
372 const struct smb_filename *smb_fname,
373 uint32_t security_info,
375 struct security_descriptor **ppdesc)
378 status = get_nt_acl_common(get_acl_blob, handle, NULL, smb_fname,
379 security_info, mem_ctx, ppdesc);
383 static NTSTATUS acl_xattr_fset_nt_acl(vfs_handle_struct *handle,
385 uint32_t security_info_sent,
386 const struct security_descriptor *psd)
389 status = fset_nt_acl_common(fget_acl_blob, store_acl_blob_fsp,
391 handle, fsp, security_info_sent, psd);
395 static struct vfs_fn_pointers vfs_acl_xattr_fns = {
396 .connect_fn = connect_acl_xattr,
397 .unlinkat_fn = acl_xattr_unlinkat,
398 .chmod_fn = chmod_acl_module_common,
399 .fchmod_fn = fchmod_acl_module_common,
400 .fget_nt_acl_fn = acl_xattr_fget_nt_acl,
401 .get_nt_acl_fn = acl_xattr_get_nt_acl,
402 .fset_nt_acl_fn = acl_xattr_fset_nt_acl,
403 .sys_acl_set_file_fn = sys_acl_set_file_xattr,
404 .sys_acl_set_fd_fn = sys_acl_set_fd_xattr
408 NTSTATUS vfs_acl_xattr_init(TALLOC_CTX *ctx)
410 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr",