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 "librpc/gen_ndr/xattr.h"
23 #include "librpc/gen_ndr/ndr_xattr.h"
26 #define DBGC_CLASS DBGC_VFS
28 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
29 const struct timespec cts,
31 struct security_descriptor **ppdesc)
33 TALLOC_CTX *ctx = talloc_tos();
34 struct xattr_NTACL xacl;
35 enum ndr_err_code ndr_err;
39 ndr_err = ndr_pull_struct_blob(pblob, ctx, &xacl,
40 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
42 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
43 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
44 ndr_errstr(ndr_err)));
45 return ndr_map_error2ntstatus(ndr_err);;
48 if (xacl.version != 2) {
49 return NT_STATUS_REVISION_MISMATCH;
53 * Check that the ctime timestamp is ealier
54 * than the stored timestamp.
57 ts = nt_time_to_unix_timespec(&xacl.info.sd_ts->last_changed);
59 if (timespec_compare(&cts, &ts) > 0) {
60 DEBUG(5, ("parse_acl_blob: stored ACL out of date.\n"));
61 return NT_STATUS_EA_CORRUPT_ERROR;
64 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
65 (security_info & OWNER_SECURITY_INFORMATION)
66 ? xacl.info.sd_ts->sd->owner_sid : NULL,
67 (security_info & GROUP_SECURITY_INFORMATION)
68 ? xacl.info.sd_ts->sd->group_sid : NULL,
69 (security_info & SACL_SECURITY_INFORMATION)
70 ? xacl.info.sd_ts->sd->sacl : NULL,
71 (security_info & DACL_SECURITY_INFORMATION)
72 ? xacl.info.sd_ts->sd->dacl : NULL,
75 TALLOC_FREE(xacl.info.sd);
77 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
80 static NTSTATUS get_acl_blob(vfs_handle_struct *handle,
96 tmp = TALLOC_REALLOC_ARRAY(ctx, val, uint8_t, size);
99 return NT_STATUS_NO_MEMORY;
105 sizeret = SMB_VFS_FGETXATTR(fsp, XATTR_NTACL_NAME, val, size);
107 sizeret = SMB_VFS_GETXATTR(handle->conn, name,
108 XATTR_NTACL_NAME, val, size);
115 /* Max ACL size is 65536 bytes. */
118 if ((errno == ERANGE) && (size != 65536)) {
119 /* Too small, try again. */
124 /* Real error - exit here. */
126 return map_nt_error_from_unix(errno);
130 pblob->length = sizeret;
134 static int mkdir_acl_xattr(vfs_handle_struct *handle, const char *path, mode_t mode)
136 return SMB_VFS_NEXT_MKDIR(handle, path, mode);
139 static int rmdir_acl_xattr(vfs_handle_struct *handle, const char *path)
141 return SMB_VFS_NEXT_RMDIR(handle, path);
144 static int open_acl_xattr(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
146 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
149 static int unlink_acl_xattr(vfs_handle_struct *handle, const char *fname)
151 return SMB_VFS_NEXT_UNLINK(handle, fname);
154 static NTSTATUS get_nt_acl_xattr_internal(vfs_handle_struct *handle,
157 uint32 security_info,
160 TALLOC_CTX *ctx = talloc_tos();
162 SMB_STRUCT_STAT sbuf;
165 if (fsp && name == NULL) {
166 name = fsp->fsp_name;
169 DEBUG(10, ("get_nt_acl_xattr_internal: name=%s\n", name));
171 status = get_acl_blob(ctx, handle, fsp, name, &blob);
172 if (!NT_STATUS_IS_OK(status)) {
173 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
177 if (fsp && fsp->fh->fd != -1) {
178 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
179 return map_nt_error_from_unix(errno);
182 if (SMB_VFS_STAT(handle->conn, name, &sbuf) == -1) {
183 return map_nt_error_from_unix(errno);
187 status = parse_acl_blob(&blob, get_ctimespec(&sbuf),
188 security_info, ppdesc);
189 if (!NT_STATUS_IS_OK(status)) {
190 DEBUG(10, ("parse_acl_blob returned %s\n",
195 TALLOC_FREE(blob.data);
199 static NTSTATUS fget_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
200 uint32 security_info, SEC_DESC **ppdesc)
202 NTSTATUS status = get_nt_acl_xattr_internal(handle, fsp,
203 NULL, security_info, ppdesc);
204 if (NT_STATUS_IS_OK(status)) {
207 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp,
208 security_info, ppdesc);
211 static NTSTATUS get_nt_acl_xattr(vfs_handle_struct *handle,
212 const char *name, uint32 security_info, SEC_DESC **ppdesc)
214 NTSTATUS status = get_nt_acl_xattr_internal(handle, NULL,
215 name, security_info, ppdesc);
216 if (NT_STATUS_IS_OK(status)) {
219 return SMB_VFS_NEXT_GET_NT_ACL(handle, name,
220 security_info, ppdesc);
223 static NTSTATUS create_acl_blob(SEC_DESC *psd, DATA_BLOB *pblob)
225 struct xattr_NTACL xacl;
226 enum ndr_err_code ndr_err;
227 TALLOC_CTX *ctx = talloc_tos();
228 struct timespec curr = timespec_current();
230 /* Horrid hack as setting an xattr changes the ctime
231 * on Linux. This gives a race of 1 second during
232 * which we would not see a POSIX ACL set.
237 xacl.info.sd_ts->sd = psd;
238 unix_timespec_to_nt_time(&xacl.info.sd_ts->last_changed, curr);
240 ndr_err = ndr_push_struct_blob(
242 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
244 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
245 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
246 ndr_errstr(ndr_err)));
247 return ndr_map_error2ntstatus(ndr_err);;
253 static NTSTATUS store_acl_blob(files_struct *fsp,
260 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
261 pblob->data, pblob->length, 0);
268 DEBUG(5, ("store_acl_blob: fsetxattr failed for file %s "
272 return map_nt_error_from_unix(errno);
277 static NTSTATUS fset_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
278 uint32 security_info_sent, SEC_DESC *psd)
283 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
284 if (!NT_STATUS_IS_OK(status)) {
288 create_acl_blob(psd, &blob);
289 store_acl_blob(fsp, &blob);
294 /* VFS operations structure */
296 static vfs_op_tuple skel_op_tuples[] =
298 {SMB_VFS_OP(mkdir_acl_xattr), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
299 {SMB_VFS_OP(rmdir_acl_xattr), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
300 {SMB_VFS_OP(open_acl_xattr), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
301 {SMB_VFS_OP(unlink_acl_xattr),SMB_VFS_OP_UNLINK,SMB_VFS_LAYER_TRANSPARENT},
303 /* NT File ACL operations */
305 {SMB_VFS_OP(fget_nt_acl_xattr),SMB_VFS_OP_FGET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
306 {SMB_VFS_OP(get_nt_acl_xattr), SMB_VFS_OP_GET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
307 {SMB_VFS_OP(fset_nt_acl_xattr),SMB_VFS_OP_FSET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
309 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
312 NTSTATUS vfs_acl_xattr_init(void)
314 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr", skel_op_tuples);