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 "librpc/gen_ndr/xattr.h"
24 #include "librpc/gen_ndr/ndr_xattr.h"
25 #include "../lib/crypto/sha256.h"
29 #define DBGC_CLASS DBGC_VFS
31 /* Pull in the common functions. */
32 #define ACL_MODULE_NAME "acl_xattr"
34 #include "modules/vfs_acl_common.c"
36 /*******************************************************************
37 Pull a security descriptor into a DATA_BLOB from a xattr.
38 *******************************************************************/
40 static ssize_t
getxattr_do(vfs_handle_struct
*handle
,
42 const struct smb_filename
*smb_fname
,
43 const char *xattr_name
,
51 if (fsp
&& fsp
->fh
->fd
!= -1) {
52 sizeret
= SMB_VFS_FGETXATTR(fsp
, xattr_name
, val
, size
);
54 sizeret
= SMB_VFS_GETXATTR(handle
->conn
, smb_fname
,
55 XATTR_NTACL_NAME
, val
, size
);
62 if (saved_errno
!= 0) {
69 static NTSTATUS
get_acl_blob(TALLOC_CTX
*ctx
,
70 vfs_handle_struct
*handle
,
72 const struct smb_filename
*smb_fname
,
84 tmp
= talloc_realloc(ctx
, val
, uint8_t, size
);
87 return NT_STATUS_NO_MEMORY
;
92 getxattr_do(handle
, fsp
, smb_fname
, XATTR_NTACL_NAME
, val
, size
);
96 pblob
->length
= sizeret
;
100 if (errno
!= ERANGE
) {
104 /* Too small, try again. */
106 getxattr_do(handle
, fsp
, smb_fname
, XATTR_NTACL_NAME
, NULL
, 0);
111 if (size
< sizeret
) {
116 /* Max ACL size is 65536 bytes. */
123 /* Real error - exit here. */
125 return map_nt_error_from_unix(errno
);
128 /*******************************************************************
129 Store a DATA_BLOB into an xattr given an fsp pointer.
130 *******************************************************************/
132 static NTSTATUS
store_acl_blob_fsp(vfs_handle_struct
*handle
,
139 DEBUG(10,("store_acl_blob_fsp: storing blob length %u on file %s\n",
140 (unsigned int)pblob
->length
, fsp_str_dbg(fsp
)));
143 if (fsp
->fh
->fd
!= -1) {
144 ret
= SMB_VFS_FSETXATTR(fsp
, XATTR_NTACL_NAME
,
145 pblob
->data
, pblob
->length
, 0);
147 ret
= SMB_VFS_SETXATTR(fsp
->conn
, fsp
->fsp_name
,
149 pblob
->data
, pblob
->length
, 0);
156 DEBUG(5, ("store_acl_blob_fsp: setting attr failed for file %s"
159 strerror(saved_errno
) ));
161 return map_nt_error_from_unix(saved_errno
);
166 /*********************************************************************
167 Remove a Windows ACL - we're setting the underlying POSIX ACL.
168 *********************************************************************/
170 static int sys_acl_set_file_xattr(vfs_handle_struct
*handle
,
171 const struct smb_filename
*smb_fname
,
175 int ret
= SMB_VFS_NEXT_SYS_ACL_SET_FILE(handle
,
184 SMB_VFS_REMOVEXATTR(handle
->conn
, smb_fname
,
191 /*********************************************************************
192 Remove a Windows ACL - we're setting the underlying POSIX ACL.
193 *********************************************************************/
195 static int sys_acl_set_fd_xattr(vfs_handle_struct
*handle
,
199 int ret
= SMB_VFS_NEXT_SYS_ACL_SET_FD(handle
,
207 SMB_VFS_FREMOVEXATTR(fsp
, XATTR_NTACL_NAME
);
213 static int connect_acl_xattr(struct vfs_handle_struct
*handle
,
217 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
219 struct acl_common_config
*config
= NULL
;
225 ok
= init_acl_common_config(handle
);
227 DBG_ERR("init_acl_common_config failed\n");
231 /* Ensure we have the parameters correct if we're
232 * using this module. */
233 DEBUG(2,("connect_acl_xattr: setting 'inherit acls = true' "
234 "'dos filemode = true' and "
235 "'force unknown acl user = true' for service %s\n",
238 lp_do_parameter(SNUM(handle
->conn
), "inherit acls", "true");
239 lp_do_parameter(SNUM(handle
->conn
), "dos filemode", "true");
240 lp_do_parameter(SNUM(handle
->conn
), "force unknown acl user", "true");
242 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
243 struct acl_common_config
,
246 if (config
->ignore_system_acls
) {
247 mode_t create_mask
= lp_create_mask(SNUM(handle
->conn
));
248 char *create_mask_str
= NULL
;
250 if ((create_mask
& 0666) != 0666) {
252 create_mask_str
= talloc_asprintf(handle
, "0%o",
254 if (create_mask_str
== NULL
) {
255 DBG_ERR("talloc_asprintf failed\n");
259 DBG_NOTICE("setting 'create mask = %s'\n", create_mask_str
);
261 lp_do_parameter (SNUM(handle
->conn
),
262 "create mask", create_mask_str
);
264 TALLOC_FREE(create_mask_str
);
267 DBG_NOTICE("setting 'directory mask = 0777', "
268 "'store dos attributes = yes' and all "
269 "'map ...' options to 'no'\n");
271 lp_do_parameter(SNUM(handle
->conn
), "directory mask", "0777");
272 lp_do_parameter(SNUM(handle
->conn
), "map archive", "no");
273 lp_do_parameter(SNUM(handle
->conn
), "map hidden", "no");
274 lp_do_parameter(SNUM(handle
->conn
), "map readonly", "no");
275 lp_do_parameter(SNUM(handle
->conn
), "map system", "no");
276 lp_do_parameter(SNUM(handle
->conn
), "store dos attributes",
283 static struct vfs_fn_pointers vfs_acl_xattr_fns
= {
284 .connect_fn
= connect_acl_xattr
,
285 .rmdir_fn
= rmdir_acl_common
,
286 .unlink_fn
= unlink_acl_common
,
287 .chmod_fn
= chmod_acl_module_common
,
288 .fchmod_fn
= fchmod_acl_module_common
,
289 .fget_nt_acl_fn
= fget_nt_acl_common
,
290 .get_nt_acl_fn
= get_nt_acl_common
,
291 .fset_nt_acl_fn
= fset_nt_acl_common
,
292 .chmod_acl_fn
= chmod_acl_acl_module_common
,
293 .fchmod_acl_fn
= fchmod_acl_acl_module_common
,
294 .sys_acl_set_file_fn
= sys_acl_set_file_xattr
,
295 .sys_acl_set_fd_fn
= sys_acl_set_fd_xattr
299 NTSTATUS
vfs_acl_xattr_init(TALLOC_CTX
*ctx
)
301 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "acl_xattr",