VFS: NULL dirfsp for openat on stream opens
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blobdd6892614495694b44116526063997e3d29d1609
1 /*
2 * Store streams in xattrs
4 * Copyright (C) Volker Lendecke, 2008
6 * Partly based on James Peach's Darwin module, which is
8 * Copyright (C) James Peach 2006-2007
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "lib/util/tevent_unix.h"
28 #include "librpc/gen_ndr/ioctl.h"
29 #include "hash_inode.h"
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config {
35 const char *prefix;
36 size_t prefix_len;
37 bool store_stream_type;
40 struct stream_io {
41 char *base;
42 char *xattr_name;
43 void *fsp_name_ptr;
44 files_struct *fsp;
45 vfs_handle_struct *handle;
48 static ssize_t get_xattr_size_fsp(struct files_struct *fsp,
49 const char *xattr_name)
51 NTSTATUS status;
52 struct ea_struct ea;
53 ssize_t result;
55 status = get_ea_value_fsp(talloc_tos(),
56 fsp,
57 xattr_name,
58 &ea);
59 if (!NT_STATUS_IS_OK(status)) {
60 return -1;
63 result = ea.value.length-1;
64 TALLOC_FREE(ea.value.data);
65 return result;
68 /**
69 * Given a stream name, populate xattr_name with the xattr name to use for
70 * accessing the stream.
72 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
73 TALLOC_CTX *ctx,
74 const char *stream_name,
75 char **xattr_name)
77 char *sname;
78 char *stype;
79 struct streams_xattr_config *config;
81 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
82 return NT_STATUS_UNSUCCESSFUL);
84 sname = talloc_strdup(ctx, stream_name + 1);
85 if (sname == NULL) {
86 return NT_STATUS_NO_MEMORY;
90 * With vfs_fruit option "fruit:encoding = native" we're
91 * already converting stream names that contain illegal NTFS
92 * characters from their on-the-wire Unicode Private Range
93 * encoding to their native ASCII representation.
95 * As as result the name of xattrs storing the streams (via
96 * vfs_streams_xattr) may contain a colon, so we have to use
97 * strrchr_m() instead of strchr_m() for matching the stream
98 * type suffix.
100 * In check_path_syntax() we've already ensured the streamname
101 * we got from the client is valid.
103 stype = strrchr_m(sname, ':');
105 if (stype) {
107 * We only support one stream type: "$DATA"
109 if (strcasecmp_m(stype, ":$DATA") != 0) {
110 talloc_free(sname);
111 return NT_STATUS_INVALID_PARAMETER;
114 /* Split name and type */
115 stype[0] = '\0';
118 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
119 config->prefix,
120 sname,
121 config->store_stream_type ? ":$DATA" : "");
122 if (*xattr_name == NULL) {
123 talloc_free(sname);
124 return NT_STATUS_NO_MEMORY;
127 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
128 stream_name));
130 talloc_free(sname);
131 return NT_STATUS_OK;
134 static bool streams_xattr_recheck(struct stream_io *sio)
136 NTSTATUS status;
137 char *xattr_name = NULL;
139 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
140 return true;
143 if (sio->fsp->fsp_name->stream_name == NULL) {
144 /* how can this happen */
145 errno = EINVAL;
146 return false;
149 status = streams_xattr_get_name(sio->handle, talloc_tos(),
150 sio->fsp->fsp_name->stream_name,
151 &xattr_name);
152 if (!NT_STATUS_IS_OK(status)) {
153 return false;
156 TALLOC_FREE(sio->xattr_name);
157 TALLOC_FREE(sio->base);
158 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
159 xattr_name);
160 if (sio->xattr_name == NULL) {
161 DBG_DEBUG("sio->xattr_name==NULL\n");
162 return false;
164 TALLOC_FREE(xattr_name);
166 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
167 sio->fsp->fsp_name->base_name);
168 if (sio->base == NULL) {
169 DBG_DEBUG("sio->base==NULL\n");
170 return false;
173 sio->fsp_name_ptr = sio->fsp->fsp_name;
175 return true;
178 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
179 SMB_STRUCT_STAT *sbuf)
181 int ret = -1;
182 struct stream_io *io = (struct stream_io *)
183 VFS_FETCH_FSP_EXTENSION(handle, fsp);
185 if (io == NULL || !fsp_is_alternate_stream(fsp)) {
186 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
189 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
191 if (!streams_xattr_recheck(io)) {
192 return -1;
195 ret = SMB_VFS_NEXT_FSTAT(handle, fsp->base_fsp, sbuf);
196 if (ret == -1) {
197 return -1;
200 sbuf->st_ex_size = get_xattr_size_fsp(fsp->base_fsp,
201 io->xattr_name);
202 if (sbuf->st_ex_size == -1) {
203 SET_STAT_INVALID(*sbuf);
204 return -1;
207 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
209 sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
210 sbuf->st_ex_mode &= ~S_IFMT;
211 sbuf->st_ex_mode &= ~S_IFDIR;
212 sbuf->st_ex_mode |= S_IFREG;
213 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
215 return 0;
218 static int streams_xattr_stat(vfs_handle_struct *handle,
219 struct smb_filename *smb_fname)
221 NTSTATUS status;
222 int result = -1;
223 char *xattr_name = NULL;
224 char *tmp_stream_name = NULL;
225 struct smb_filename *pathref = NULL;
226 struct files_struct *fsp = smb_fname->fsp;
228 if (!is_named_stream(smb_fname)) {
229 return SMB_VFS_NEXT_STAT(handle, smb_fname);
232 /* Note if lp_posix_paths() is true, we can never
233 * get here as is_named_stream() is
234 * always false. So we never need worry about
235 * not following links here. */
237 /* Populate the stat struct with info from the base file. */
238 tmp_stream_name = smb_fname->stream_name;
239 smb_fname->stream_name = NULL;
240 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
241 smb_fname->stream_name = tmp_stream_name;
243 if (result == -1) {
244 return -1;
247 /* Derive the xattr name to lookup. */
248 status = streams_xattr_get_name(handle, talloc_tos(),
249 smb_fname->stream_name, &xattr_name);
250 if (!NT_STATUS_IS_OK(status)) {
251 errno = map_errno_from_nt_status(status);
252 return -1;
255 /* Augment the base file's stat information before returning. */
256 if (fsp == NULL) {
257 status = synthetic_pathref(talloc_tos(),
258 handle->conn->cwd_fsp,
259 smb_fname->base_name,
260 NULL,
261 NULL,
262 smb_fname->twrp,
263 smb_fname->flags,
264 &pathref);
265 if (!NT_STATUS_IS_OK(status)) {
266 TALLOC_FREE(xattr_name);
267 SET_STAT_INVALID(smb_fname->st);
268 errno = ENOENT;
269 return -1;
271 fsp = pathref->fsp;
272 } else {
273 fsp = fsp->base_fsp;
276 smb_fname->st.st_ex_size = get_xattr_size_fsp(fsp,
277 xattr_name);
278 if (smb_fname->st.st_ex_size == -1) {
279 TALLOC_FREE(xattr_name);
280 TALLOC_FREE(pathref);
281 SET_STAT_INVALID(smb_fname->st);
282 errno = ENOENT;
283 return -1;
286 smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
287 smb_fname->st.st_ex_mode &= ~S_IFMT;
288 smb_fname->st.st_ex_mode |= S_IFREG;
289 smb_fname->st.st_ex_blocks =
290 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
292 TALLOC_FREE(xattr_name);
293 TALLOC_FREE(pathref);
294 return 0;
297 static int streams_xattr_lstat(vfs_handle_struct *handle,
298 struct smb_filename *smb_fname)
300 if (is_named_stream(smb_fname)) {
302 * There can never be EA's on a symlink.
303 * Windows will never see a symlink, and
304 * in SMB_FILENAME_POSIX_PATH mode we don't
305 * allow EA's on a symlink.
307 SET_STAT_INVALID(smb_fname->st);
308 errno = ENOENT;
309 return -1;
311 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
314 static int streams_xattr_openat(struct vfs_handle_struct *handle,
315 const struct files_struct *dirfsp,
316 const struct smb_filename *smb_fname,
317 files_struct *fsp,
318 int flags,
319 mode_t mode)
321 NTSTATUS status;
322 struct streams_xattr_config *config = NULL;
323 struct stream_io *sio = NULL;
324 struct ea_struct ea;
325 char *xattr_name = NULL;
326 int fakefd = -1;
327 bool set_empty_xattr = false;
328 int ret;
330 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
331 return -1);
333 DBG_DEBUG("called for %s with flags 0x%x\n",
334 smb_fname_str_dbg(smb_fname),
335 flags);
337 if (!is_named_stream(smb_fname)) {
338 return SMB_VFS_NEXT_OPENAT(handle,
339 dirfsp,
340 smb_fname,
341 fsp,
342 flags,
343 mode);
346 SMB_ASSERT(fsp_is_alternate_stream(fsp));
347 SMB_ASSERT(dirfsp == NULL);
349 status = streams_xattr_get_name(handle, talloc_tos(),
350 smb_fname->stream_name, &xattr_name);
351 if (!NT_STATUS_IS_OK(status)) {
352 errno = map_errno_from_nt_status(status);
353 goto fail;
356 status = get_ea_value_fsp(talloc_tos(),
357 fsp->base_fsp,
358 xattr_name,
359 &ea);
361 DBG_DEBUG("get_ea_value_fsp returned %s\n", nt_errstr(status));
363 if (!NT_STATUS_IS_OK(status)) {
364 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
366 * The base file is not there. This is an error even if
367 * we got O_CREAT, the higher levels should have created
368 * the base file for us.
370 DBG_DEBUG("streams_xattr_open: base file %s not around, "
371 "returning ENOENT\n", smb_fname->base_name);
372 errno = ENOENT;
373 goto fail;
376 if (!(flags & O_CREAT)) {
377 errno = ENOATTR;
378 goto fail;
381 set_empty_xattr = true;
384 if (flags & O_TRUNC) {
385 set_empty_xattr = true;
388 if (set_empty_xattr) {
390 * The attribute does not exist or needs to be truncated
394 * Darn, xattrs need at least 1 byte
396 char null = '\0';
398 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
399 xattr_name, smb_fname->base_name));
401 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
402 xattr_name,
403 &null, sizeof(null),
404 flags & O_EXCL ? XATTR_CREATE : 0);
405 if (ret != 0) {
406 goto fail;
410 fakefd = vfs_fake_fd();
412 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
413 if (sio == NULL) {
414 errno = ENOMEM;
415 goto fail;
418 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
419 xattr_name);
420 if (sio->xattr_name == NULL) {
421 errno = ENOMEM;
422 goto fail;
426 * so->base needs to be a copy of fsp->fsp_name->base_name,
427 * making it identical to streams_xattr_recheck(). If the
428 * open is changing directories, fsp->fsp_name->base_name
429 * will be the full path from the share root, whilst
430 * smb_fname will be relative to the $cwd.
432 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
433 fsp->fsp_name->base_name);
434 if (sio->base == NULL) {
435 errno = ENOMEM;
436 goto fail;
439 sio->fsp_name_ptr = fsp->fsp_name;
440 sio->handle = handle;
441 sio->fsp = fsp;
443 return fakefd;
445 fail:
446 if (fakefd >= 0) {
447 vfs_fake_fd_close(fakefd);
448 fakefd = -1;
451 return -1;
454 static int streams_xattr_close(vfs_handle_struct *handle,
455 files_struct *fsp)
457 int ret;
458 int fd;
460 fd = fsp_get_pathref_fd(fsp);
462 DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
463 smb_fname_str_dbg(fsp->fsp_name), fd);
465 if (!fsp_is_alternate_stream(fsp)) {
466 return SMB_VFS_NEXT_CLOSE(handle, fsp);
469 ret = vfs_fake_fd_close(fd);
470 fsp_set_fd(fsp, -1);
472 return ret;
475 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
476 struct files_struct *dirfsp,
477 const struct smb_filename *smb_fname,
478 int flags)
480 NTSTATUS status;
481 int ret = -1;
482 char *xattr_name = NULL;
483 struct smb_filename *pathref = NULL;
484 struct files_struct *fsp = smb_fname->fsp;
486 if (!is_named_stream(smb_fname)) {
487 return SMB_VFS_NEXT_UNLINKAT(handle,
488 dirfsp,
489 smb_fname,
490 flags);
493 /* A stream can never be rmdir'ed */
494 SMB_ASSERT((flags & AT_REMOVEDIR) == 0);
496 status = streams_xattr_get_name(handle, talloc_tos(),
497 smb_fname->stream_name, &xattr_name);
498 if (!NT_STATUS_IS_OK(status)) {
499 errno = map_errno_from_nt_status(status);
500 goto fail;
503 if (fsp == NULL) {
504 status = synthetic_pathref(talloc_tos(),
505 handle->conn->cwd_fsp,
506 smb_fname->base_name,
507 NULL,
508 NULL,
509 smb_fname->twrp,
510 smb_fname->flags,
511 &pathref);
512 if (!NT_STATUS_IS_OK(status)) {
513 errno = ENOENT;
514 goto fail;
516 fsp = pathref->fsp;
517 } else {
518 SMB_ASSERT(fsp_is_alternate_stream(smb_fname->fsp));
519 fsp = fsp->base_fsp;
522 ret = SMB_VFS_FREMOVEXATTR(fsp, xattr_name);
524 if ((ret == -1) && (errno == ENOATTR)) {
525 errno = ENOENT;
526 goto fail;
529 ret = 0;
531 fail:
532 TALLOC_FREE(xattr_name);
533 TALLOC_FREE(pathref);
534 return ret;
537 static int streams_xattr_renameat(vfs_handle_struct *handle,
538 files_struct *srcfsp,
539 const struct smb_filename *smb_fname_src,
540 files_struct *dstfsp,
541 const struct smb_filename *smb_fname_dst)
543 NTSTATUS status;
544 int ret = -1;
545 char *src_xattr_name = NULL;
546 char *dst_xattr_name = NULL;
547 bool src_is_stream, dst_is_stream;
548 ssize_t oret;
549 ssize_t nret;
550 struct ea_struct ea;
551 struct smb_filename *pathref_src = NULL;
552 struct smb_filename *pathref_dst = NULL;
553 struct smb_filename *full_src = NULL;
554 struct smb_filename *full_dst = NULL;
556 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
557 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
559 if (!src_is_stream && !dst_is_stream) {
560 return SMB_VFS_NEXT_RENAMEAT(handle,
561 srcfsp,
562 smb_fname_src,
563 dstfsp,
564 smb_fname_dst);
567 /* For now don't allow renames from or to the default stream. */
568 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
569 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
570 errno = ENOSYS;
571 goto done;
574 /* Don't rename if the streams are identical. */
575 if (strcasecmp_m(smb_fname_src->stream_name,
576 smb_fname_dst->stream_name) == 0) {
577 goto done;
580 /* Get the xattr names. */
581 status = streams_xattr_get_name(handle, talloc_tos(),
582 smb_fname_src->stream_name,
583 &src_xattr_name);
584 if (!NT_STATUS_IS_OK(status)) {
585 errno = map_errno_from_nt_status(status);
586 goto fail;
588 status = streams_xattr_get_name(handle, talloc_tos(),
589 smb_fname_dst->stream_name,
590 &dst_xattr_name);
591 if (!NT_STATUS_IS_OK(status)) {
592 errno = map_errno_from_nt_status(status);
593 goto fail;
596 full_src = full_path_from_dirfsp_atname(talloc_tos(),
597 srcfsp,
598 smb_fname_src);
599 if (full_src == NULL) {
600 errno = ENOMEM;
601 goto fail;
603 full_dst = full_path_from_dirfsp_atname(talloc_tos(),
604 dstfsp,
605 smb_fname_dst);
606 if (full_dst == NULL) {
607 errno = ENOMEM;
608 goto fail;
611 /* Get a pathref for full_src (base file, no stream name). */
612 status = synthetic_pathref(talloc_tos(),
613 handle->conn->cwd_fsp,
614 full_src->base_name,
615 NULL,
616 NULL,
617 full_src->twrp,
618 full_src->flags,
619 &pathref_src);
620 if (!NT_STATUS_IS_OK(status)) {
621 errno = ENOENT;
622 goto fail;
625 /* Read the old stream from the base file fsp. */
626 status = get_ea_value_fsp(talloc_tos(),
627 pathref_src->fsp,
628 src_xattr_name,
629 &ea);
630 if (!NT_STATUS_IS_OK(status)) {
631 errno = map_errno_from_nt_status(status);
632 goto fail;
635 /* Get a pathref for full_dst (base file, no stream name). */
636 status = synthetic_pathref(talloc_tos(),
637 handle->conn->cwd_fsp,
638 full_dst->base_name,
639 NULL,
640 NULL,
641 full_dst->twrp,
642 full_dst->flags,
643 &pathref_dst);
644 if (!NT_STATUS_IS_OK(status)) {
645 errno = ENOENT;
646 goto fail;
649 /* (Over)write the new stream on the base file fsp. */
650 nret = SMB_VFS_FSETXATTR(
651 pathref_dst->fsp,
652 dst_xattr_name,
653 ea.value.data,
654 ea.value.length,
656 if (nret < 0) {
657 if (errno == ENOATTR) {
658 errno = ENOENT;
660 goto fail;
664 * Remove the old stream from the base file fsp.
666 oret = SMB_VFS_FREMOVEXATTR(pathref_src->fsp,
667 src_xattr_name);
668 if (oret < 0) {
669 if (errno == ENOATTR) {
670 errno = ENOENT;
672 goto fail;
675 done:
676 errno = 0;
677 ret = 0;
678 fail:
679 TALLOC_FREE(pathref_src);
680 TALLOC_FREE(pathref_dst);
681 TALLOC_FREE(full_src);
682 TALLOC_FREE(full_dst);
683 TALLOC_FREE(src_xattr_name);
684 TALLOC_FREE(dst_xattr_name);
685 return ret;
688 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
689 files_struct *fsp,
690 const struct smb_filename *smb_fname,
691 bool (*fn)(struct ea_struct *ea,
692 void *private_data),
693 void *private_data)
695 NTSTATUS status;
696 char **names;
697 size_t i, num_names;
698 struct streams_xattr_config *config;
700 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
701 return NT_STATUS_UNSUCCESSFUL);
703 status = get_ea_names_from_fsp(talloc_tos(),
704 smb_fname->fsp,
705 &names,
706 &num_names);
707 if (!NT_STATUS_IS_OK(status)) {
708 return status;
711 for (i=0; i<num_names; i++) {
712 struct ea_struct ea;
715 * We want to check with samba_private_attr_name()
716 * whether the xattr name is a private one,
717 * unfortunately it flags xattrs that begin with the
718 * default streams prefix as private.
720 * By only calling samba_private_attr_name() in case
721 * the xattr does NOT begin with the default prefix,
722 * we know that if it returns 'true' it definitely one
723 * of our internal xattr like "user.DOSATTRIB".
725 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
726 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
727 if (samba_private_attr_name(names[i])) {
728 continue;
732 if (strncmp(names[i], config->prefix,
733 config->prefix_len) != 0) {
734 continue;
737 status = get_ea_value_fsp(names,
738 smb_fname->fsp,
739 names[i],
740 &ea);
741 if (!NT_STATUS_IS_OK(status)) {
742 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
743 names[i],
744 smb_fname->base_name,
745 nt_errstr(status)));
746 continue;
749 ea.name = talloc_asprintf(
750 ea.value.data, ":%s%s",
751 names[i] + config->prefix_len,
752 config->store_stream_type ? "" : ":$DATA");
753 if (ea.name == NULL) {
754 DEBUG(0, ("talloc failed\n"));
755 continue;
758 if (!fn(&ea, private_data)) {
759 TALLOC_FREE(ea.value.data);
760 return NT_STATUS_OK;
763 TALLOC_FREE(ea.value.data);
766 TALLOC_FREE(names);
767 return NT_STATUS_OK;
770 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
771 struct stream_struct **streams,
772 const char *name, off_t size,
773 off_t alloc_size)
775 struct stream_struct *tmp;
777 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
778 (*num_streams)+1);
779 if (tmp == NULL) {
780 return false;
783 tmp[*num_streams].name = talloc_strdup(tmp, name);
784 if (tmp[*num_streams].name == NULL) {
785 return false;
788 tmp[*num_streams].size = size;
789 tmp[*num_streams].alloc_size = alloc_size;
791 *streams = tmp;
792 *num_streams += 1;
793 return true;
796 struct streaminfo_state {
797 TALLOC_CTX *mem_ctx;
798 vfs_handle_struct *handle;
799 unsigned int num_streams;
800 struct stream_struct *streams;
801 NTSTATUS status;
804 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
806 struct streaminfo_state *state =
807 (struct streaminfo_state *)private_data;
809 if (!add_one_stream(state->mem_ctx,
810 &state->num_streams, &state->streams,
811 ea->name, ea->value.length-1,
812 smb_roundup(state->handle->conn,
813 ea->value.length-1))) {
814 state->status = NT_STATUS_NO_MEMORY;
815 return false;
818 return true;
821 static NTSTATUS streams_xattr_fstreaminfo(vfs_handle_struct *handle,
822 struct files_struct *fsp,
823 TALLOC_CTX *mem_ctx,
824 unsigned int *pnum_streams,
825 struct stream_struct **pstreams)
827 NTSTATUS status;
828 struct streaminfo_state state;
830 state.streams = *pstreams;
831 state.num_streams = *pnum_streams;
832 state.mem_ctx = mem_ctx;
833 state.handle = handle;
834 state.status = NT_STATUS_OK;
836 status = walk_xattr_streams(handle,
837 fsp,
838 fsp->fsp_name,
839 collect_one_stream,
840 &state);
842 if (!NT_STATUS_IS_OK(status)) {
843 TALLOC_FREE(state.streams);
844 return status;
847 if (!NT_STATUS_IS_OK(state.status)) {
848 TALLOC_FREE(state.streams);
849 return state.status;
852 *pnum_streams = state.num_streams;
853 *pstreams = state.streams;
855 return SMB_VFS_NEXT_FSTREAMINFO(handle,
856 fsp,
857 mem_ctx,
858 pnum_streams,
859 pstreams);
862 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
863 enum timestamp_set_resolution *p_ts_res)
865 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
868 static int streams_xattr_connect(vfs_handle_struct *handle,
869 const char *service, const char *user)
871 struct streams_xattr_config *config;
872 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
873 const char *prefix;
874 int rc;
876 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
877 if (rc != 0) {
878 return rc;
881 config = talloc_zero(handle->conn, struct streams_xattr_config);
882 if (config == NULL) {
883 DEBUG(1, ("talloc_zero() failed\n"));
884 errno = ENOMEM;
885 return -1;
888 prefix = lp_parm_const_string(SNUM(handle->conn),
889 "streams_xattr", "prefix",
890 default_prefix);
891 config->prefix = talloc_strdup(config, prefix);
892 if (config->prefix == NULL) {
893 DEBUG(1, ("talloc_strdup() failed\n"));
894 errno = ENOMEM;
895 return -1;
897 config->prefix_len = strlen(config->prefix);
898 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
900 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
901 "streams_xattr",
902 "store_stream_type",
903 true);
905 SMB_VFS_HANDLE_SET_DATA(handle, config,
906 NULL, struct stream_xattr_config,
907 return -1);
909 return 0;
912 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
913 files_struct *fsp, const void *data,
914 size_t n, off_t offset)
916 struct stream_io *sio =
917 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
918 struct ea_struct ea;
919 NTSTATUS status;
920 int ret;
922 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
924 if (sio == NULL) {
925 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
928 if (!streams_xattr_recheck(sio)) {
929 return -1;
932 if ((offset + n) >= lp_smbd_max_xattr_size(SNUM(handle->conn))) {
934 * Requested write is beyond what can be read based on
935 * samba configuration.
936 * ReFS returns STATUS_FILESYSTEM_LIMITATION, which causes
937 * entire file to be skipped by File Explorer. VFAT returns
938 * NT_STATUS_OBJECT_NAME_COLLISION causes user to be prompted
939 * to skip writing metadata, but copy data.
941 DBG_ERR("Write to xattr [%s] on file [%s] exceeds maximum "
942 "supported extended attribute size. "
943 "Depending on filesystem type and operating system "
944 "(OS) specifics, this value may be increased using "
945 "the value of the parameter: "
946 "smbd max xattr size = <bytes>. Consult OS and "
947 "filesystem manpages prior to increasing this limit.\n",
948 sio->xattr_name, sio->base);
949 errno = EOVERFLOW;
950 return -1;
953 status = get_ea_value_fsp(talloc_tos(),
954 fsp->base_fsp,
955 sio->xattr_name,
956 &ea);
957 if (!NT_STATUS_IS_OK(status)) {
958 return -1;
961 if ((offset + n) > ea.value.length-1) {
962 uint8_t *tmp;
964 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
965 offset + n + 1);
967 if (tmp == NULL) {
968 TALLOC_FREE(ea.value.data);
969 errno = ENOMEM;
970 return -1;
972 ea.value.data = tmp;
973 ea.value.length = offset + n + 1;
974 ea.value.data[offset+n] = 0;
977 memcpy(ea.value.data + offset, data, n);
979 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
980 sio->xattr_name,
981 ea.value.data,
982 ea.value.length,
984 TALLOC_FREE(ea.value.data);
986 if (ret == -1) {
987 return -1;
990 return n;
993 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
994 files_struct *fsp, void *data,
995 size_t n, off_t offset)
997 struct stream_io *sio =
998 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
999 struct ea_struct ea;
1000 NTSTATUS status;
1001 size_t length, overlap;
1003 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1004 (int)offset, (int)n));
1006 if (sio == NULL) {
1007 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1010 if (!streams_xattr_recheck(sio)) {
1011 return -1;
1014 status = get_ea_value_fsp(talloc_tos(),
1015 fsp->base_fsp,
1016 sio->xattr_name,
1017 &ea);
1018 if (!NT_STATUS_IS_OK(status)) {
1019 return -1;
1022 length = ea.value.length-1;
1024 DBG_DEBUG("get_ea_value_fsp returned %d bytes\n",
1025 (int)length);
1027 /* Attempt to read past EOF. */
1028 if (length <= offset) {
1029 return 0;
1032 overlap = (offset + n) > length ? (length - offset) : n;
1033 memcpy(data, ea.value.data + offset, overlap);
1035 TALLOC_FREE(ea.value.data);
1036 return overlap;
1039 struct streams_xattr_pread_state {
1040 ssize_t nread;
1041 struct vfs_aio_state vfs_aio_state;
1044 static void streams_xattr_pread_done(struct tevent_req *subreq);
1046 static struct tevent_req *streams_xattr_pread_send(
1047 struct vfs_handle_struct *handle,
1048 TALLOC_CTX *mem_ctx,
1049 struct tevent_context *ev,
1050 struct files_struct *fsp,
1051 void *data,
1052 size_t n, off_t offset)
1054 struct tevent_req *req = NULL;
1055 struct tevent_req *subreq = NULL;
1056 struct streams_xattr_pread_state *state = NULL;
1057 struct stream_io *sio =
1058 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1060 req = tevent_req_create(mem_ctx, &state,
1061 struct streams_xattr_pread_state);
1062 if (req == NULL) {
1063 return NULL;
1066 if (sio == NULL) {
1067 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1068 data, n, offset);
1069 if (tevent_req_nomem(req, subreq)) {
1070 return tevent_req_post(req, ev);
1072 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1073 return req;
1076 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1077 if (state->nread != n) {
1078 if (state->nread != -1) {
1079 errno = EIO;
1081 tevent_req_error(req, errno);
1082 return tevent_req_post(req, ev);
1085 tevent_req_done(req);
1086 return tevent_req_post(req, ev);
1089 static void streams_xattr_pread_done(struct tevent_req *subreq)
1091 struct tevent_req *req = tevent_req_callback_data(
1092 subreq, struct tevent_req);
1093 struct streams_xattr_pread_state *state = tevent_req_data(
1094 req, struct streams_xattr_pread_state);
1096 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1097 TALLOC_FREE(subreq);
1099 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1100 return;
1102 tevent_req_done(req);
1105 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1106 struct vfs_aio_state *vfs_aio_state)
1108 struct streams_xattr_pread_state *state = tevent_req_data(
1109 req, struct streams_xattr_pread_state);
1111 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1112 return -1;
1115 *vfs_aio_state = state->vfs_aio_state;
1116 return state->nread;
1119 struct streams_xattr_pwrite_state {
1120 ssize_t nwritten;
1121 struct vfs_aio_state vfs_aio_state;
1124 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1126 static struct tevent_req *streams_xattr_pwrite_send(
1127 struct vfs_handle_struct *handle,
1128 TALLOC_CTX *mem_ctx,
1129 struct tevent_context *ev,
1130 struct files_struct *fsp,
1131 const void *data,
1132 size_t n, off_t offset)
1134 struct tevent_req *req = NULL;
1135 struct tevent_req *subreq = NULL;
1136 struct streams_xattr_pwrite_state *state = NULL;
1137 struct stream_io *sio =
1138 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1140 req = tevent_req_create(mem_ctx, &state,
1141 struct streams_xattr_pwrite_state);
1142 if (req == NULL) {
1143 return NULL;
1146 if (sio == NULL) {
1147 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1148 data, n, offset);
1149 if (tevent_req_nomem(req, subreq)) {
1150 return tevent_req_post(req, ev);
1152 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1153 return req;
1156 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1157 if (state->nwritten != n) {
1158 if (state->nwritten != -1) {
1159 errno = EIO;
1161 tevent_req_error(req, errno);
1162 return tevent_req_post(req, ev);
1165 tevent_req_done(req);
1166 return tevent_req_post(req, ev);
1169 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1171 struct tevent_req *req = tevent_req_callback_data(
1172 subreq, struct tevent_req);
1173 struct streams_xattr_pwrite_state *state = tevent_req_data(
1174 req, struct streams_xattr_pwrite_state);
1176 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1177 TALLOC_FREE(subreq);
1179 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1180 return;
1182 tevent_req_done(req);
1185 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1186 struct vfs_aio_state *vfs_aio_state)
1188 struct streams_xattr_pwrite_state *state = tevent_req_data(
1189 req, struct streams_xattr_pwrite_state);
1191 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1192 return -1;
1195 *vfs_aio_state = state->vfs_aio_state;
1196 return state->nwritten;
1199 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1200 struct files_struct *fsp,
1201 off_t offset)
1203 int ret;
1204 uint8_t *tmp;
1205 struct ea_struct ea;
1206 NTSTATUS status;
1207 struct stream_io *sio =
1208 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1210 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1211 fsp_str_dbg(fsp), (double)offset));
1213 if (sio == NULL) {
1214 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1217 if (!streams_xattr_recheck(sio)) {
1218 return -1;
1221 status = get_ea_value_fsp(talloc_tos(),
1222 fsp->base_fsp,
1223 sio->xattr_name,
1224 &ea);
1225 if (!NT_STATUS_IS_OK(status)) {
1226 return -1;
1229 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1230 offset + 1);
1232 if (tmp == NULL) {
1233 TALLOC_FREE(ea.value.data);
1234 errno = ENOMEM;
1235 return -1;
1238 /* Did we expand ? */
1239 if (ea.value.length < offset + 1) {
1240 memset(&tmp[ea.value.length], '\0',
1241 offset + 1 - ea.value.length);
1244 ea.value.data = tmp;
1245 ea.value.length = offset + 1;
1246 ea.value.data[offset] = 0;
1248 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1249 sio->xattr_name,
1250 ea.value.data,
1251 ea.value.length,
1254 TALLOC_FREE(ea.value.data);
1256 if (ret == -1) {
1257 return -1;
1260 return 0;
1263 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1264 struct files_struct *fsp,
1265 uint32_t mode,
1266 off_t offset,
1267 off_t len)
1269 struct stream_io *sio =
1270 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1272 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1273 "len = %.0f\n",
1274 fsp_str_dbg(fsp), (double)offset, (double)len));
1276 if (sio == NULL) {
1277 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1280 if (!streams_xattr_recheck(sio)) {
1281 return -1;
1284 /* Let the pwrite code path handle it. */
1285 errno = ENOSYS;
1286 return -1;
1289 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1290 uid_t uid, gid_t gid)
1292 struct stream_io *sio =
1293 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1295 if (sio == NULL) {
1296 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1299 return 0;
1302 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1303 files_struct *fsp,
1304 mode_t mode)
1306 struct stream_io *sio =
1307 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1309 if (sio == NULL) {
1310 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1313 return 0;
1316 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1317 struct files_struct *fsp,
1318 const char *name,
1319 void *value,
1320 size_t size)
1322 struct stream_io *sio =
1323 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1325 if (sio == NULL) {
1326 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1329 errno = ENOTSUP;
1330 return -1;
1333 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1334 struct files_struct *fsp,
1335 char *list,
1336 size_t size)
1338 struct stream_io *sio =
1339 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1341 if (sio == NULL) {
1342 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1345 errno = ENOTSUP;
1346 return -1;
1349 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1350 struct files_struct *fsp,
1351 const char *name)
1353 struct stream_io *sio =
1354 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1356 if (sio == NULL) {
1357 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1360 errno = ENOTSUP;
1361 return -1;
1364 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1365 struct files_struct *fsp,
1366 const char *name,
1367 const void *value,
1368 size_t size,
1369 int flags)
1371 struct stream_io *sio =
1372 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1374 if (sio == NULL) {
1375 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1376 size, flags);
1379 errno = ENOTSUP;
1380 return -1;
1383 struct streams_xattr_fsync_state {
1384 int ret;
1385 struct vfs_aio_state vfs_aio_state;
1388 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1390 static struct tevent_req *streams_xattr_fsync_send(
1391 struct vfs_handle_struct *handle,
1392 TALLOC_CTX *mem_ctx,
1393 struct tevent_context *ev,
1394 struct files_struct *fsp)
1396 struct tevent_req *req = NULL;
1397 struct tevent_req *subreq = NULL;
1398 struct streams_xattr_fsync_state *state = NULL;
1399 struct stream_io *sio =
1400 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1402 req = tevent_req_create(mem_ctx, &state,
1403 struct streams_xattr_fsync_state);
1404 if (req == NULL) {
1405 return NULL;
1408 if (sio == NULL) {
1409 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1410 if (tevent_req_nomem(req, subreq)) {
1411 return tevent_req_post(req, ev);
1413 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1414 return req;
1418 * There's no pathname based sync variant and we don't have access to
1419 * the basefile handle, so we can't do anything here.
1422 tevent_req_done(req);
1423 return tevent_req_post(req, ev);
1426 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1428 struct tevent_req *req = tevent_req_callback_data(
1429 subreq, struct tevent_req);
1430 struct streams_xattr_fsync_state *state = tevent_req_data(
1431 req, struct streams_xattr_fsync_state);
1433 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1434 TALLOC_FREE(subreq);
1435 if (state->ret != 0) {
1436 tevent_req_error(req, errno);
1437 return;
1440 tevent_req_done(req);
1443 static int streams_xattr_fsync_recv(struct tevent_req *req,
1444 struct vfs_aio_state *vfs_aio_state)
1446 struct streams_xattr_fsync_state *state = tevent_req_data(
1447 req, struct streams_xattr_fsync_state);
1449 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1450 return -1;
1453 *vfs_aio_state = state->vfs_aio_state;
1454 return state->ret;
1457 static bool streams_xattr_lock(vfs_handle_struct *handle,
1458 files_struct *fsp,
1459 int op,
1460 off_t offset,
1461 off_t count,
1462 int type)
1464 struct stream_io *sio =
1465 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1467 if (sio == NULL) {
1468 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1471 return true;
1474 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1475 files_struct *fsp,
1476 off_t *poffset,
1477 off_t *pcount,
1478 int *ptype,
1479 pid_t *ppid)
1481 struct stream_io *sio =
1482 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1484 if (sio == NULL) {
1485 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1486 pcount, ptype, ppid);
1489 errno = ENOTSUP;
1490 return false;
1493 static int streams_xattr_filesystem_sharemode(vfs_handle_struct *handle,
1494 files_struct *fsp,
1495 uint32_t share_access,
1496 uint32_t access_mask)
1498 struct stream_io *sio =
1499 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1501 if (sio == NULL) {
1502 return SMB_VFS_NEXT_FILESYSTEM_SHAREMODE(handle,
1503 fsp,
1504 share_access,
1505 access_mask);
1508 return 0;
1511 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1512 files_struct *fsp,
1513 int leasetype)
1515 struct stream_io *sio =
1516 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1518 if (sio == NULL) {
1519 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1522 return 0;
1525 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1526 files_struct *fsp,
1527 struct lock_struct *plock)
1529 struct stream_io *sio =
1530 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1532 if (sio == NULL) {
1533 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1536 return true;
1539 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1540 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1541 .connect_fn = streams_xattr_connect,
1542 .openat_fn = streams_xattr_openat,
1543 .close_fn = streams_xattr_close,
1544 .stat_fn = streams_xattr_stat,
1545 .fstat_fn = streams_xattr_fstat,
1546 .lstat_fn = streams_xattr_lstat,
1547 .pread_fn = streams_xattr_pread,
1548 .pwrite_fn = streams_xattr_pwrite,
1549 .pread_send_fn = streams_xattr_pread_send,
1550 .pread_recv_fn = streams_xattr_pread_recv,
1551 .pwrite_send_fn = streams_xattr_pwrite_send,
1552 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1553 .unlinkat_fn = streams_xattr_unlinkat,
1554 .renameat_fn = streams_xattr_renameat,
1555 .ftruncate_fn = streams_xattr_ftruncate,
1556 .fallocate_fn = streams_xattr_fallocate,
1557 .fstreaminfo_fn = streams_xattr_fstreaminfo,
1559 .fsync_send_fn = streams_xattr_fsync_send,
1560 .fsync_recv_fn = streams_xattr_fsync_recv,
1562 .lock_fn = streams_xattr_lock,
1563 .getlock_fn = streams_xattr_getlock,
1564 .filesystem_sharemode_fn = streams_xattr_filesystem_sharemode,
1565 .linux_setlease_fn = streams_xattr_linux_setlease,
1566 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1568 .fchown_fn = streams_xattr_fchown,
1569 .fchmod_fn = streams_xattr_fchmod,
1571 .fgetxattr_fn = streams_xattr_fgetxattr,
1572 .flistxattr_fn = streams_xattr_flistxattr,
1573 .fremovexattr_fn = streams_xattr_fremovexattr,
1574 .fsetxattr_fn = streams_xattr_fsetxattr,
1577 static_decl_vfs;
1578 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1580 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1581 &vfs_streams_xattr_fns);