Mark module experimental, fix crash bugs.
[Samba/bb.git] / source / modules / vfs_acl_xattr.c
blobff0c2cbc89c6c5a13fe554a5a6b20c23b4e8e798
1 /*
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/>.
21 /* NOTE: This is an experimental module, not yet finished. JRA. */
23 #include "includes.h"
24 #include "librpc/gen_ndr/xattr.h"
25 #include "librpc/gen_ndr/ndr_xattr.h"
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_VFS
30 static NTSTATUS parse_acl_blob(const DATA_BLOB *pblob,
31 const struct timespec cts,
32 uint32 security_info,
33 struct security_descriptor **ppdesc)
35 TALLOC_CTX *ctx = talloc_tos();
36 struct xattr_NTACL xacl;
37 enum ndr_err_code ndr_err;
38 size_t sd_size;
39 struct timespec ts;
41 ndr_err = ndr_pull_struct_blob(pblob, ctx, &xacl,
42 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
44 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
45 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
46 ndr_errstr(ndr_err)));
47 return ndr_map_error2ntstatus(ndr_err);;
50 if (xacl.version != 2) {
51 return NT_STATUS_REVISION_MISMATCH;
55 * Check that the ctime timestamp is ealier
56 * than the stored timestamp.
59 ts = nt_time_to_unix_timespec(&xacl.info.sd_ts->last_changed);
61 if (timespec_compare(&cts, &ts) > 0) {
62 DEBUG(5, ("parse_acl_blob: stored ACL out of date.\n"));
63 return NT_STATUS_EA_CORRUPT_ERROR;
66 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
67 (security_info & OWNER_SECURITY_INFORMATION)
68 ? xacl.info.sd_ts->sd->owner_sid : NULL,
69 (security_info & GROUP_SECURITY_INFORMATION)
70 ? xacl.info.sd_ts->sd->group_sid : NULL,
71 (security_info & SACL_SECURITY_INFORMATION)
72 ? xacl.info.sd_ts->sd->sacl : NULL,
73 (security_info & DACL_SECURITY_INFORMATION)
74 ? xacl.info.sd_ts->sd->dacl : NULL,
75 &sd_size);
77 TALLOC_FREE(xacl.info.sd);
79 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
82 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
83 vfs_handle_struct *handle,
84 files_struct *fsp,
85 const char *name,
86 DATA_BLOB *pblob)
88 size_t size = 1024;
89 uint8_t *val = NULL;
90 uint8_t *tmp;
91 ssize_t sizeret;
92 int saved_errno;
94 ZERO_STRUCTP(pblob);
96 again:
98 tmp = TALLOC_REALLOC_ARRAY(ctx, val, uint8_t, size);
99 if (tmp == NULL) {
100 TALLOC_FREE(val);
101 return NT_STATUS_NO_MEMORY;
103 val = tmp;
105 become_root();
106 if (fsp) {
107 sizeret = SMB_VFS_FGETXATTR(fsp, XATTR_NTACL_NAME, val, size);
108 } else {
109 sizeret = SMB_VFS_GETXATTR(handle->conn, name,
110 XATTR_NTACL_NAME, val, size);
112 if (sizeret == -1) {
113 saved_errno = errno;
115 unbecome_root();
117 /* Max ACL size is 65536 bytes. */
118 if (sizeret == -1) {
119 errno = saved_errno;
120 if ((errno == ERANGE) && (size != 65536)) {
121 /* Too small, try again. */
122 size = 65536;
123 goto again;
126 /* Real error - exit here. */
127 TALLOC_FREE(val);
128 return map_nt_error_from_unix(errno);
131 pblob->data = val;
132 pblob->length = sizeret;
133 return NT_STATUS_OK;
136 static int mkdir_acl_xattr(vfs_handle_struct *handle, const char *path, mode_t mode)
138 return SMB_VFS_NEXT_MKDIR(handle, path, mode);
141 static int rmdir_acl_xattr(vfs_handle_struct *handle, const char *path)
143 return SMB_VFS_NEXT_RMDIR(handle, path);
146 static int open_acl_xattr(vfs_handle_struct *handle, const char *fname, files_struct *fsp, int flags, mode_t mode)
148 return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
151 static int unlink_acl_xattr(vfs_handle_struct *handle, const char *fname)
153 return SMB_VFS_NEXT_UNLINK(handle, fname);
156 static NTSTATUS get_nt_acl_xattr_internal(vfs_handle_struct *handle,
157 files_struct *fsp,
158 const char *name,
159 uint32 security_info,
160 SEC_DESC **ppdesc)
162 TALLOC_CTX *ctx = talloc_tos();
163 DATA_BLOB blob;
164 SMB_STRUCT_STAT sbuf;
165 NTSTATUS status;
167 if (fsp && name == NULL) {
168 name = fsp->fsp_name;
171 DEBUG(10, ("get_nt_acl_xattr_internal: name=%s\n", name));
173 status = get_acl_blob(ctx, handle, fsp, name, &blob);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
176 return status;
179 if (fsp && fsp->fh->fd != -1) {
180 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
181 return map_nt_error_from_unix(errno);
183 } else {
184 if (SMB_VFS_STAT(handle->conn, name, &sbuf) == -1) {
185 return map_nt_error_from_unix(errno);
189 status = parse_acl_blob(&blob, get_ctimespec(&sbuf),
190 security_info, ppdesc);
191 if (!NT_STATUS_IS_OK(status)) {
192 DEBUG(10, ("parse_acl_blob returned %s\n",
193 nt_errstr(status)));
194 return status;
197 TALLOC_FREE(blob.data);
198 return status;
201 static NTSTATUS fget_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
202 uint32 security_info, SEC_DESC **ppdesc)
204 NTSTATUS status = get_nt_acl_xattr_internal(handle, fsp,
205 NULL, security_info, ppdesc);
206 if (NT_STATUS_IS_OK(status)) {
207 return NT_STATUS_OK;
209 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp,
210 security_info, ppdesc);
213 static NTSTATUS get_nt_acl_xattr(vfs_handle_struct *handle,
214 const char *name, uint32 security_info, SEC_DESC **ppdesc)
216 NTSTATUS status = get_nt_acl_xattr_internal(handle, NULL,
217 name, security_info, ppdesc);
218 if (NT_STATUS_IS_OK(status)) {
219 return NT_STATUS_OK;
221 return SMB_VFS_NEXT_GET_NT_ACL(handle, name,
222 security_info, ppdesc);
225 static NTSTATUS create_acl_blob(SEC_DESC *psd, DATA_BLOB *pblob)
227 struct xattr_NTACL xacl;
228 struct security_descriptor_timestamp sd_ts;
229 enum ndr_err_code ndr_err;
230 TALLOC_CTX *ctx = talloc_tos();
231 struct timespec curr = timespec_current();
233 ZERO_STRUCT(xacl);
234 ZERO_STRUCT(sd_ts);
236 /* Horrid hack as setting an xattr changes the ctime
237 * on Linux. This gives a race of 1 second during
238 * which we would not see a POSIX ACL set.
240 curr.tv_sec += 1;
242 xacl.version = 2;
243 xacl.info.sd_ts = &sd_ts;
244 xacl.info.sd_ts->sd = psd;
245 unix_timespec_to_nt_time(&xacl.info.sd_ts->last_changed, curr);
247 ndr_err = ndr_push_struct_blob(
248 pblob, ctx, &xacl,
249 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
251 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
252 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
253 ndr_errstr(ndr_err)));
254 return ndr_map_error2ntstatus(ndr_err);;
257 return NT_STATUS_OK;
260 static NTSTATUS store_acl_blob(files_struct *fsp,
261 DATA_BLOB *pblob)
263 int ret;
264 int saved_errno;
266 become_root();
267 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
268 pblob->data, pblob->length, 0);
269 if (ret) {
270 saved_errno = errno;
272 unbecome_root();
273 if (ret) {
274 errno = saved_errno;
275 DEBUG(5, ("store_acl_blob: fsetxattr failed for file %s "
276 "with error %s\n",
277 fsp->fsp_name,
278 strerror(errno) ));
279 return map_nt_error_from_unix(errno);
281 return NT_STATUS_OK;
284 static NTSTATUS fset_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
285 uint32 security_info_sent, SEC_DESC *psd)
287 NTSTATUS status;
288 DATA_BLOB blob;
290 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
291 if (!NT_STATUS_IS_OK(status)) {
292 return status;
295 create_acl_blob(psd, &blob);
296 store_acl_blob(fsp, &blob);
298 return NT_STATUS_OK;
301 /* VFS operations structure */
303 static vfs_op_tuple skel_op_tuples[] =
305 {SMB_VFS_OP(mkdir_acl_xattr), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
306 {SMB_VFS_OP(rmdir_acl_xattr), SMB_VFS_OP_RMDIR, SMB_VFS_LAYER_TRANSPARENT},
307 {SMB_VFS_OP(open_acl_xattr), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
308 {SMB_VFS_OP(unlink_acl_xattr),SMB_VFS_OP_UNLINK,SMB_VFS_LAYER_TRANSPARENT},
310 /* NT File ACL operations */
312 {SMB_VFS_OP(fget_nt_acl_xattr),SMB_VFS_OP_FGET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
313 {SMB_VFS_OP(get_nt_acl_xattr), SMB_VFS_OP_GET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
314 {SMB_VFS_OP(fset_nt_acl_xattr),SMB_VFS_OP_FSET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
316 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
319 NTSTATUS vfs_acl_xattr_init(void)
321 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr", skel_op_tuples);