Allow a new file to inherit the Windows ACL from its parent.
[Samba/bb.git] / source3 / modules / vfs_acl_xattr.c
bloba802dac554b0bcba1ca9ddbfc56b5d3158dd282d
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;
40 ndr_err = ndr_pull_struct_blob(pblob, ctx, NULL, &xacl,
41 (ndr_pull_flags_fn_t)ndr_pull_xattr_NTACL);
43 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
44 DEBUG(5, ("parse_acl_blob: ndr_pull_xattr_NTACL failed: %s\n",
45 ndr_errstr(ndr_err)));
46 return ndr_map_error2ntstatus(ndr_err);;
49 if (xacl.version != 2) {
50 return NT_STATUS_REVISION_MISMATCH;
53 #if 0
55 struct timespec ts;
56 /* Arg. This doesn't work. Too many activities
57 * change the ctime. May have to roll back to
58 * version 1.
61 * Check that the ctime timestamp is ealier
62 * than the stored timestamp.
65 ts = nt_time_to_unix_timespec(&xacl.info.sd_ts->last_changed);
67 if (timespec_compare(&cts, &ts) > 0) {
68 DEBUG(5, ("parse_acl_blob: stored ACL out of date "
69 "(%s > %s.\n",
70 timestring(ctx, cts.tv_sec),
71 timestring(ctx, ts.tv_sec)));
72 return NT_STATUS_EA_CORRUPT_ERROR;
75 #endif
77 *ppdesc = make_sec_desc(ctx, SEC_DESC_REVISION, SEC_DESC_SELF_RELATIVE,
78 (security_info & OWNER_SECURITY_INFORMATION)
79 ? xacl.info.sd_ts->sd->owner_sid : NULL,
80 (security_info & GROUP_SECURITY_INFORMATION)
81 ? xacl.info.sd_ts->sd->group_sid : NULL,
82 (security_info & SACL_SECURITY_INFORMATION)
83 ? xacl.info.sd_ts->sd->sacl : NULL,
84 (security_info & DACL_SECURITY_INFORMATION)
85 ? xacl.info.sd_ts->sd->dacl : NULL,
86 &sd_size);
88 TALLOC_FREE(xacl.info.sd);
90 return (*ppdesc != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
93 static NTSTATUS get_acl_blob(TALLOC_CTX *ctx,
94 vfs_handle_struct *handle,
95 files_struct *fsp,
96 const char *name,
97 DATA_BLOB *pblob)
99 size_t size = 1024;
100 uint8_t *val = NULL;
101 uint8_t *tmp;
102 ssize_t sizeret;
103 int saved_errno = 0;
105 ZERO_STRUCTP(pblob);
107 again:
109 tmp = TALLOC_REALLOC_ARRAY(ctx, val, uint8_t, size);
110 if (tmp == NULL) {
111 TALLOC_FREE(val);
112 return NT_STATUS_NO_MEMORY;
114 val = tmp;
116 become_root();
117 if (fsp && fsp->fh->fd != -1) {
118 sizeret = SMB_VFS_FGETXATTR(fsp, XATTR_NTACL_NAME, val, size);
119 } else {
120 sizeret = SMB_VFS_GETXATTR(handle->conn, name,
121 XATTR_NTACL_NAME, val, size);
123 if (sizeret == -1) {
124 saved_errno = errno;
126 unbecome_root();
128 /* Max ACL size is 65536 bytes. */
129 if (sizeret == -1) {
130 errno = saved_errno;
131 if ((errno == ERANGE) && (size != 65536)) {
132 /* Too small, try again. */
133 size = 65536;
134 goto again;
137 /* Real error - exit here. */
138 TALLOC_FREE(val);
139 return map_nt_error_from_unix(errno);
142 pblob->data = val;
143 pblob->length = sizeret;
144 return NT_STATUS_OK;
147 static NTSTATUS create_acl_blob(const SEC_DESC *psd, DATA_BLOB *pblob)
149 struct xattr_NTACL xacl;
150 struct security_descriptor_timestamp sd_ts;
151 enum ndr_err_code ndr_err;
152 TALLOC_CTX *ctx = talloc_tos();
153 struct timespec curr = timespec_current();
155 ZERO_STRUCT(xacl);
156 ZERO_STRUCT(sd_ts);
158 /* Horrid hack as setting an xattr changes the ctime
159 * on Linux. This gives a race of 1 second during
160 * which we would not see a POSIX ACL set.
162 curr.tv_sec += 1;
164 xacl.version = 2;
165 xacl.info.sd_ts = &sd_ts;
166 xacl.info.sd_ts->sd = CONST_DISCARD(SEC_DESC *, psd);
167 unix_timespec_to_nt_time(&xacl.info.sd_ts->last_changed, curr);
169 DEBUG(10, ("create_acl_blob: timestamp stored as %s\n",
170 timestring(ctx, curr.tv_sec) ));
172 ndr_err = ndr_push_struct_blob(
173 pblob, ctx, NULL, &xacl,
174 (ndr_push_flags_fn_t)ndr_push_xattr_NTACL);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
177 DEBUG(5, ("create_acl_blob: ndr_push_xattr_NTACL failed: %s\n",
178 ndr_errstr(ndr_err)));
179 return ndr_map_error2ntstatus(ndr_err);;
182 return NT_STATUS_OK;
185 static NTSTATUS store_acl_blob(files_struct *fsp,
186 DATA_BLOB *pblob)
188 int ret;
189 int saved_errno = 0;
191 DEBUG(10,("store_acl_blob: storing blob length %u on file %s\n",
192 (unsigned int)pblob->length, fsp->fsp_name));
194 become_root();
195 if (fsp->fh->fd != -1) {
196 ret = SMB_VFS_FSETXATTR(fsp, XATTR_NTACL_NAME,
197 pblob->data, pblob->length, 0);
198 } else {
199 ret = SMB_VFS_SETXATTR(fsp->conn, fsp->fsp_name,
200 XATTR_NTACL_NAME,
201 pblob->data, pblob->length, 0);
203 if (ret) {
204 saved_errno = errno;
206 unbecome_root();
207 if (ret) {
208 errno = saved_errno;
209 DEBUG(5, ("store_acl_blob: setting attr failed for file %s"
210 "with error %s\n",
211 fsp->fsp_name,
212 strerror(errno) ));
213 return map_nt_error_from_unix(errno);
215 return NT_STATUS_OK;
219 static NTSTATUS get_nt_acl_xattr_internal(vfs_handle_struct *handle,
220 files_struct *fsp,
221 const char *name,
222 uint32 security_info,
223 SEC_DESC **ppdesc)
225 TALLOC_CTX *ctx = talloc_tos();
226 DATA_BLOB blob;
227 SMB_STRUCT_STAT sbuf;
228 NTSTATUS status;
230 if (fsp && name == NULL) {
231 name = fsp->fsp_name;
234 DEBUG(10, ("get_nt_acl_xattr_internal: name=%s\n", name));
236 status = get_acl_blob(ctx, handle, fsp, name, &blob);
237 if (!NT_STATUS_IS_OK(status)) {
238 DEBUG(10, ("get_acl_blob returned %s\n", nt_errstr(status)));
239 return status;
242 if (fsp && fsp->fh->fd != -1) {
243 if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
244 return map_nt_error_from_unix(errno);
246 } else {
247 if (SMB_VFS_STAT(handle->conn, name, &sbuf) == -1) {
248 return map_nt_error_from_unix(errno);
252 status = parse_acl_blob(&blob, get_ctimespec(&sbuf),
253 security_info, ppdesc);
254 if (!NT_STATUS_IS_OK(status)) {
255 DEBUG(10, ("parse_acl_blob returned %s\n",
256 nt_errstr(status)));
257 return status;
260 TALLOC_FREE(blob.data);
261 return status;
264 static int mkdir_acl_xattr(vfs_handle_struct *handle, const char *path, mode_t mode)
266 return SMB_VFS_NEXT_MKDIR(handle, path, mode);
269 /*********************************************************************
270 * Currently this only works for existing files. Need to work on
271 * inheritance for new files.
272 *********************************************************************/
274 static NTSTATUS inherit_new_acl(vfs_handle_struct *handle,
275 const char *fname,
276 files_struct *fsp)
278 TALLOC_CTX *ctx = talloc_tos();
279 NTSTATUS status;
280 SEC_DESC *parent_desc = NULL;
281 SEC_DESC *psd = NULL;
282 DATA_BLOB blob;
283 size_t size;
284 char *parent_name;
286 if (!parent_dirname_talloc(ctx,
287 fname,
288 &parent_name,
289 NULL)) {
290 return NT_STATUS_NO_MEMORY;
293 DEBUG(10,("inherit_new_acl: check directory %s\n",
294 parent_name));
296 status = get_nt_acl_xattr_internal(handle,
297 NULL,
298 parent_name,
299 DACL_SECURITY_INFORMATION,
300 &parent_desc);
301 if (!NT_STATUS_IS_OK(status)) {
302 DEBUG(10,("inherit_new_acl: directory %s failed "
303 "to get acl %s\n",
304 parent_name,
305 nt_errstr(status) ));
306 return status;
309 /* Create an inherited descriptor from the parent. */
310 status = se_create_child_secdesc(ctx,
311 &psd,
312 &size,
313 parent_desc,
314 &handle->conn->server_info->ptok->user_sids[PRIMARY_USER_SID_INDEX],
315 &handle->conn->server_info->ptok->user_sids[PRIMARY_GROUP_SID_INDEX],
316 false);
317 if (!NT_STATUS_IS_OK(status)) {
318 return status;
320 status = create_acl_blob(psd, &blob);
321 if (!NT_STATUS_IS_OK(status)) {
322 return status;
324 return store_acl_blob(fsp, &blob);
327 /*********************************************************************
328 Check ACL on open. For new files inherit from parent directory.
329 *********************************************************************/
331 static int open_acl_xattr(vfs_handle_struct *handle,
332 const char *fname,
333 files_struct *fsp,
334 int flags,
335 mode_t mode)
337 uint32_t access_granted = 0;
338 SEC_DESC *pdesc = NULL;
339 bool file_existed = true;
340 NTSTATUS status = get_nt_acl_xattr_internal(handle,
341 NULL,
342 fname,
343 (OWNER_SECURITY_INFORMATION |
344 GROUP_SECURITY_INFORMATION |
345 DACL_SECURITY_INFORMATION),
346 &pdesc);
347 if (NT_STATUS_IS_OK(status)) {
348 /* See if we can access it. */
349 if (!se_access_check(pdesc,
350 handle->conn->server_info->ptok,
351 fsp->access_mask,
352 &access_granted,
353 &status)) {
354 errno = map_errno_from_nt_status(status);
355 return -1;
357 } else if (NT_STATUS_EQUAL(status,NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
358 file_existed = false;
361 DEBUG(10,("open_acl_xattr: get_nt_acl_attr_internal for "
362 "file %s returned %s\n",
363 fname,
364 nt_errstr(status) ));
366 fsp->fh->fd = SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
368 if (!file_existed && fsp->fh->fd != -1) {
369 /* File was created. Inherit from parent directory. */
370 string_set(&fsp->fsp_name, fname);
371 inherit_new_acl(handle, fname, fsp);
374 return fsp->fh->fd;
377 static NTSTATUS fget_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
378 uint32 security_info, SEC_DESC **ppdesc)
380 NTSTATUS status = get_nt_acl_xattr_internal(handle, fsp,
381 NULL, security_info, ppdesc);
382 if (NT_STATUS_IS_OK(status)) {
383 return NT_STATUS_OK;
385 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp,
386 security_info, ppdesc);
389 static NTSTATUS get_nt_acl_xattr(vfs_handle_struct *handle,
390 const char *name, uint32 security_info, SEC_DESC **ppdesc)
392 NTSTATUS status = get_nt_acl_xattr_internal(handle, NULL,
393 name, security_info, ppdesc);
394 if (NT_STATUS_IS_OK(status)) {
395 return NT_STATUS_OK;
397 return SMB_VFS_NEXT_GET_NT_ACL(handle, name,
398 security_info, ppdesc);
401 static NTSTATUS fset_nt_acl_xattr(vfs_handle_struct *handle, files_struct *fsp,
402 uint32 security_info_sent, const SEC_DESC *psd)
404 NTSTATUS status;
405 DATA_BLOB blob;
407 status = SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp, security_info_sent, psd);
408 if (!NT_STATUS_IS_OK(status)) {
409 return status;
412 if ((security_info_sent & DACL_SECURITY_INFORMATION) &&
413 psd->dacl != NULL &&
414 (psd->type & (SE_DESC_DACL_AUTO_INHERITED|
415 SE_DESC_DACL_AUTO_INHERIT_REQ))==
416 (SE_DESC_DACL_AUTO_INHERITED|
417 SE_DESC_DACL_AUTO_INHERIT_REQ) ) {
418 SEC_DESC *new_psd = NULL;
419 status = append_parent_acl(fsp, psd, &new_psd);
420 if (!NT_STATUS_IS_OK(status)) {
421 /* Lower level acl set succeeded,
422 * so still return OK. */
423 return NT_STATUS_OK;
425 psd = new_psd;
428 create_acl_blob(psd, &blob);
429 store_acl_blob(fsp, &blob);
431 return NT_STATUS_OK;
434 /* VFS operations structure */
436 static vfs_op_tuple skel_op_tuples[] =
438 {SMB_VFS_OP(mkdir_acl_xattr), SMB_VFS_OP_MKDIR, SMB_VFS_LAYER_TRANSPARENT},
439 {SMB_VFS_OP(open_acl_xattr), SMB_VFS_OP_OPEN, SMB_VFS_LAYER_TRANSPARENT},
441 /* NT File ACL operations */
443 {SMB_VFS_OP(fget_nt_acl_xattr),SMB_VFS_OP_FGET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
444 {SMB_VFS_OP(get_nt_acl_xattr), SMB_VFS_OP_GET_NT_ACL, SMB_VFS_LAYER_TRANSPARENT},
445 {SMB_VFS_OP(fset_nt_acl_xattr),SMB_VFS_OP_FSET_NT_ACL,SMB_VFS_LAYER_TRANSPARENT},
447 {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
450 NTSTATUS vfs_acl_xattr_init(void)
452 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "acl_xattr", skel_op_tuples);