vfs_streams_xattr: always pass NULL as fsp arg to get_ea_value()
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blob5eb2b9b230a5d7cc04b19aadeb666ed003739cde
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/crypto/md5.h"
28 #include "lib/util/tevent_unix.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_VFS
33 struct streams_xattr_config {
34 const char *prefix;
35 size_t prefix_len;
36 bool store_stream_type;
39 struct stream_io {
40 char *base;
41 char *xattr_name;
42 void *fsp_name_ptr;
43 files_struct *fsp;
44 vfs_handle_struct *handle;
47 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
49 MD5_CTX ctx;
50 unsigned char hash[16];
51 SMB_INO_T result;
52 char *upper_sname;
54 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
55 (unsigned long)sbuf->st_ex_dev,
56 (unsigned long)sbuf->st_ex_ino, sname));
58 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
59 SMB_ASSERT(upper_sname != NULL);
61 MD5Init(&ctx);
62 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
63 sizeof(sbuf->st_ex_dev));
64 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
65 sizeof(sbuf->st_ex_ino));
66 MD5Update(&ctx, (unsigned char *)upper_sname,
67 talloc_get_size(upper_sname)-1);
68 MD5Final(hash, &ctx);
70 TALLOC_FREE(upper_sname);
72 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
73 memcpy(&result, hash, sizeof(result));
75 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
77 return result;
80 static ssize_t get_xattr_size(connection_struct *conn,
81 const struct smb_filename *smb_fname,
82 const char *xattr_name)
84 NTSTATUS status;
85 struct ea_struct ea;
86 ssize_t result;
88 status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
89 xattr_name, &ea);
91 if (!NT_STATUS_IS_OK(status)) {
92 return -1;
95 result = ea.value.length-1;
96 TALLOC_FREE(ea.value.data);
97 return result;
101 * Given a stream name, populate xattr_name with the xattr name to use for
102 * accessing the stream.
104 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
105 TALLOC_CTX *ctx,
106 const char *stream_name,
107 char **xattr_name)
109 char *sname;
110 char *stype;
111 struct streams_xattr_config *config;
113 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
114 return NT_STATUS_UNSUCCESSFUL);
116 sname = talloc_strdup(ctx, stream_name + 1);
117 if (sname == NULL) {
118 return NT_STATUS_NO_MEMORY;
122 * With vfs_fruit option "fruit:encoding = native" we're
123 * already converting stream names that contain illegal NTFS
124 * characters from their on-the-wire Unicode Private Range
125 * encoding to their native ASCII representation.
127 * As as result the name of xattrs storing the streams (via
128 * vfs_streams_xattr) may contain a colon, so we have to use
129 * strrchr_m() instead of strchr_m() for matching the stream
130 * type suffix.
132 * In check_path_syntax() we've already ensured the streamname
133 * we got from the client is valid.
135 stype = strrchr_m(sname, ':');
137 if (stype) {
139 * We only support one stream type: "$DATA"
141 if (strcasecmp_m(stype, ":$DATA") != 0) {
142 talloc_free(sname);
143 return NT_STATUS_INVALID_PARAMETER;
146 /* Split name and type */
147 stype[0] = '\0';
150 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
151 config->prefix,
152 sname,
153 config->store_stream_type ? ":$DATA" : "");
154 if (*xattr_name == NULL) {
155 talloc_free(sname);
156 return NT_STATUS_NO_MEMORY;
159 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
160 stream_name));
162 talloc_free(sname);
163 return NT_STATUS_OK;
166 static bool streams_xattr_recheck(struct stream_io *sio)
168 NTSTATUS status;
169 char *xattr_name = NULL;
171 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
172 return true;
175 if (sio->fsp->fsp_name->stream_name == NULL) {
176 /* how can this happen */
177 errno = EINVAL;
178 return false;
181 status = streams_xattr_get_name(sio->handle, talloc_tos(),
182 sio->fsp->fsp_name->stream_name,
183 &xattr_name);
184 if (!NT_STATUS_IS_OK(status)) {
185 return false;
188 TALLOC_FREE(sio->xattr_name);
189 TALLOC_FREE(sio->base);
190 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
191 xattr_name);
192 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
193 sio->fsp->fsp_name->base_name);
194 sio->fsp_name_ptr = sio->fsp->fsp_name;
196 TALLOC_FREE(xattr_name);
198 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
199 return false;
202 return true;
206 * Helper to stat/lstat the base file of an smb_fname.
208 static int streams_xattr_stat_base(vfs_handle_struct *handle,
209 struct smb_filename *smb_fname,
210 bool follow_links)
212 char *tmp_stream_name;
213 int result;
215 tmp_stream_name = smb_fname->stream_name;
216 smb_fname->stream_name = NULL;
217 if (follow_links) {
218 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
219 } else {
220 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
222 smb_fname->stream_name = tmp_stream_name;
223 return result;
226 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
227 SMB_STRUCT_STAT *sbuf)
229 struct smb_filename *smb_fname_base = NULL;
230 int ret = -1;
231 struct stream_io *io = (struct stream_io *)
232 VFS_FETCH_FSP_EXTENSION(handle, fsp);
234 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
236 if (io == NULL || fsp->base_fsp == NULL) {
237 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
240 if (!streams_xattr_recheck(io)) {
241 return -1;
244 /* Create an smb_filename with stream_name == NULL. */
245 smb_fname_base = synthetic_smb_fname(talloc_tos(),
246 io->base,
247 NULL,
248 NULL,
249 fsp->fsp_name->flags);
250 if (smb_fname_base == NULL) {
251 errno = ENOMEM;
252 return -1;
255 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
256 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
257 } else {
258 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
260 *sbuf = smb_fname_base->st;
262 if (ret == -1) {
263 TALLOC_FREE(smb_fname_base);
264 return -1;
267 sbuf->st_ex_size = get_xattr_size(handle->conn,
268 smb_fname_base, io->xattr_name);
269 if (sbuf->st_ex_size == -1) {
270 TALLOC_FREE(smb_fname_base);
271 SET_STAT_INVALID(*sbuf);
272 return -1;
275 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
277 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
278 sbuf->st_ex_mode &= ~S_IFMT;
279 sbuf->st_ex_mode |= S_IFREG;
280 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
282 TALLOC_FREE(smb_fname_base);
283 return 0;
286 static int streams_xattr_stat(vfs_handle_struct *handle,
287 struct smb_filename *smb_fname)
289 NTSTATUS status;
290 int result = -1;
291 char *xattr_name = NULL;
293 if (!is_ntfs_stream_smb_fname(smb_fname)) {
294 return SMB_VFS_NEXT_STAT(handle, smb_fname);
297 /* Note if lp_posix_paths() is true, we can never
298 * get here as is_ntfs_stream_smb_fname() is
299 * always false. So we never need worry about
300 * not following links here. */
302 /* If the default stream is requested, just stat the base file. */
303 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
304 return streams_xattr_stat_base(handle, smb_fname, true);
307 /* Populate the stat struct with info from the base file. */
308 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
309 return -1;
312 /* Derive the xattr name to lookup. */
313 status = streams_xattr_get_name(handle, talloc_tos(),
314 smb_fname->stream_name, &xattr_name);
315 if (!NT_STATUS_IS_OK(status)) {
316 errno = map_errno_from_nt_status(status);
317 return -1;
320 /* Augment the base file's stat information before returning. */
321 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
322 smb_fname,
323 xattr_name);
324 if (smb_fname->st.st_ex_size == -1) {
325 SET_STAT_INVALID(smb_fname->st);
326 errno = ENOENT;
327 result = -1;
328 goto fail;
331 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
332 smb_fname->st.st_ex_mode &= ~S_IFMT;
333 smb_fname->st.st_ex_mode |= S_IFREG;
334 smb_fname->st.st_ex_blocks =
335 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
337 result = 0;
338 fail:
339 TALLOC_FREE(xattr_name);
340 return result;
343 static int streams_xattr_lstat(vfs_handle_struct *handle,
344 struct smb_filename *smb_fname)
346 NTSTATUS status;
347 int result = -1;
348 char *xattr_name = NULL;
350 if (!is_ntfs_stream_smb_fname(smb_fname)) {
351 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
354 /* If the default stream is requested, just stat the base file. */
355 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
356 return streams_xattr_stat_base(handle, smb_fname, false);
359 /* Populate the stat struct with info from the base file. */
360 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
361 return -1;
364 /* Derive the xattr name to lookup. */
365 status = streams_xattr_get_name(handle, talloc_tos(),
366 smb_fname->stream_name, &xattr_name);
367 if (!NT_STATUS_IS_OK(status)) {
368 errno = map_errno_from_nt_status(status);
369 return -1;
372 /* Augment the base file's stat information before returning. */
373 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
374 smb_fname,
375 xattr_name);
376 if (smb_fname->st.st_ex_size == -1) {
377 SET_STAT_INVALID(smb_fname->st);
378 errno = ENOENT;
379 result = -1;
380 goto fail;
383 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
384 smb_fname->st.st_ex_mode &= ~S_IFMT;
385 smb_fname->st.st_ex_mode |= S_IFREG;
386 smb_fname->st.st_ex_blocks =
387 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
389 result = 0;
391 fail:
392 TALLOC_FREE(xattr_name);
393 return result;
396 static int streams_xattr_open(vfs_handle_struct *handle,
397 struct smb_filename *smb_fname,
398 files_struct *fsp, int flags, mode_t mode)
400 NTSTATUS status;
401 struct smb_filename *smb_fname_base = NULL;
402 struct stream_io *sio;
403 struct ea_struct ea;
404 char *xattr_name = NULL;
405 int baseflags;
406 int hostfd = -1;
407 int ret;
409 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
410 smb_fname_str_dbg(smb_fname), flags));
412 if (!is_ntfs_stream_smb_fname(smb_fname)) {
413 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
416 /* If the default stream is requested, just open the base file. */
417 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
418 char *tmp_stream_name;
420 tmp_stream_name = smb_fname->stream_name;
421 smb_fname->stream_name = NULL;
423 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
425 smb_fname->stream_name = tmp_stream_name;
427 return ret;
430 status = streams_xattr_get_name(handle, talloc_tos(),
431 smb_fname->stream_name, &xattr_name);
432 if (!NT_STATUS_IS_OK(status)) {
433 errno = map_errno_from_nt_status(status);
434 goto fail;
437 /* Create an smb_filename with stream_name == NULL. */
438 smb_fname_base = synthetic_smb_fname(talloc_tos(),
439 smb_fname->base_name,
440 NULL,
441 NULL,
442 smb_fname->flags);
443 if (smb_fname_base == NULL) {
444 errno = ENOMEM;
445 goto fail;
449 * We use baseflags to turn off nasty side-effects when opening the
450 * underlying file.
452 baseflags = flags;
453 baseflags &= ~O_TRUNC;
454 baseflags &= ~O_EXCL;
455 baseflags &= ~O_CREAT;
457 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
458 baseflags, mode);
460 /* It is legit to open a stream on a directory, but the base
461 * fd has to be read-only.
463 if ((hostfd == -1) && (errno == EISDIR)) {
464 baseflags &= ~O_ACCMODE;
465 baseflags |= O_RDONLY;
466 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp, baseflags,
467 mode);
470 TALLOC_FREE(smb_fname_base);
472 if (hostfd == -1) {
473 goto fail;
476 status = get_ea_value(talloc_tos(), handle->conn, NULL,
477 smb_fname, xattr_name, &ea);
479 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
481 if (!NT_STATUS_IS_OK(status)
482 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
484 * The base file is not there. This is an error even if we got
485 * O_CREAT, the higher levels should have created the base
486 * file for us.
488 DEBUG(10, ("streams_xattr_open: base file %s not around, "
489 "returning ENOENT\n", smb_fname->base_name));
490 errno = ENOENT;
491 goto fail;
494 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
495 (flags & O_TRUNC)) {
497 * The attribute does not exist or needs to be truncated
501 * Darn, xattrs need at least 1 byte
503 char null = '\0';
505 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
506 xattr_name, smb_fname->base_name));
508 ret = SMB_VFS_SETXATTR(fsp->conn,
509 smb_fname,
510 xattr_name,
511 &null, sizeof(null),
512 flags & O_EXCL ? XATTR_CREATE : 0);
513 if (ret != 0) {
514 goto fail;
518 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
519 struct stream_io,
520 NULL);
521 if (sio == NULL) {
522 errno = ENOMEM;
523 goto fail;
526 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
527 xattr_name);
529 * so->base needs to be a copy of fsp->fsp_name->base_name,
530 * making it identical to streams_xattr_recheck(). If the
531 * open is changing directories, fsp->fsp_name->base_name
532 * will be the full path from the share root, whilst
533 * smb_fname will be relative to the $cwd.
535 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
536 fsp->fsp_name->base_name);
537 sio->fsp_name_ptr = fsp->fsp_name;
538 sio->handle = handle;
539 sio->fsp = fsp;
541 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
542 errno = ENOMEM;
543 goto fail;
546 return hostfd;
548 fail:
549 if (hostfd >= 0) {
551 * BUGBUGBUG -- we would need to call fd_close_posix here, but
552 * we don't have a full fsp yet
554 fsp->fh->fd = hostfd;
555 SMB_VFS_NEXT_CLOSE(handle, fsp);
558 return -1;
561 static int streams_xattr_unlink(vfs_handle_struct *handle,
562 const struct smb_filename *smb_fname)
564 NTSTATUS status;
565 int ret = -1;
566 char *xattr_name = NULL;
568 if (!is_ntfs_stream_smb_fname(smb_fname)) {
569 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
572 /* If the default stream is requested, just open the base file. */
573 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
574 struct smb_filename *smb_fname_base = NULL;
576 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
577 if (smb_fname_base == NULL) {
578 errno = ENOMEM;
579 return -1;
582 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
584 TALLOC_FREE(smb_fname_base);
585 return ret;
588 status = streams_xattr_get_name(handle, talloc_tos(),
589 smb_fname->stream_name, &xattr_name);
590 if (!NT_STATUS_IS_OK(status)) {
591 errno = map_errno_from_nt_status(status);
592 goto fail;
595 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
597 if ((ret == -1) && (errno == ENOATTR)) {
598 errno = ENOENT;
599 goto fail;
602 ret = 0;
604 fail:
605 TALLOC_FREE(xattr_name);
606 return ret;
609 static int streams_xattr_rename(vfs_handle_struct *handle,
610 const struct smb_filename *smb_fname_src,
611 const struct smb_filename *smb_fname_dst)
613 NTSTATUS status;
614 int ret = -1;
615 char *src_xattr_name = NULL;
616 char *dst_xattr_name = NULL;
617 bool src_is_stream, dst_is_stream;
618 ssize_t oret;
619 ssize_t nret;
620 struct ea_struct ea;
622 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
623 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
625 if (!src_is_stream && !dst_is_stream) {
626 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
627 smb_fname_dst);
630 /* For now don't allow renames from or to the default stream. */
631 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
632 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
633 errno = ENOSYS;
634 goto done;
637 /* Don't rename if the streams are identical. */
638 if (strcasecmp_m(smb_fname_src->stream_name,
639 smb_fname_dst->stream_name) == 0) {
640 goto done;
643 /* Get the xattr names. */
644 status = streams_xattr_get_name(handle, talloc_tos(),
645 smb_fname_src->stream_name,
646 &src_xattr_name);
647 if (!NT_STATUS_IS_OK(status)) {
648 errno = map_errno_from_nt_status(status);
649 goto fail;
651 status = streams_xattr_get_name(handle, talloc_tos(),
652 smb_fname_dst->stream_name,
653 &dst_xattr_name);
654 if (!NT_STATUS_IS_OK(status)) {
655 errno = map_errno_from_nt_status(status);
656 goto fail;
659 /* read the old stream */
660 status = get_ea_value(talloc_tos(), handle->conn, NULL,
661 smb_fname_src, src_xattr_name, &ea);
662 if (!NT_STATUS_IS_OK(status)) {
663 errno = ENOENT;
664 goto fail;
667 /* (over)write the new stream */
668 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
669 dst_xattr_name, ea.value.data, ea.value.length,
671 if (nret < 0) {
672 if (errno == ENOATTR) {
673 errno = ENOENT;
675 goto fail;
678 /* remove the old stream */
679 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
680 src_xattr_name);
681 if (oret < 0) {
682 if (errno == ENOATTR) {
683 errno = ENOENT;
685 goto fail;
688 done:
689 errno = 0;
690 ret = 0;
691 fail:
692 TALLOC_FREE(src_xattr_name);
693 TALLOC_FREE(dst_xattr_name);
694 return ret;
697 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
698 files_struct *fsp,
699 const struct smb_filename *smb_fname,
700 bool (*fn)(struct ea_struct *ea,
701 void *private_data),
702 void *private_data)
704 NTSTATUS status;
705 char **names;
706 size_t i, num_names;
707 struct streams_xattr_config *config;
709 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
710 return NT_STATUS_UNSUCCESSFUL);
712 status = get_ea_names_from_file(talloc_tos(),
713 handle->conn,
714 fsp,
715 smb_fname,
716 &names,
717 &num_names);
718 if (!NT_STATUS_IS_OK(status)) {
719 return status;
722 for (i=0; i<num_names; i++) {
723 struct ea_struct ea;
726 * We want to check with samba_private_attr_name()
727 * whether the xattr name is a private one,
728 * unfortunately it flags xattrs that begin with the
729 * default streams prefix as private.
731 * By only calling samba_private_attr_name() in case
732 * the xattr does NOT begin with the default prefix,
733 * we know that if it returns 'true' it definitely one
734 * of our internal xattr like "user.DOSATTRIB".
736 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
737 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
738 if (samba_private_attr_name(names[i])) {
739 continue;
743 if (strncmp(names[i], config->prefix,
744 config->prefix_len) != 0) {
745 continue;
748 status = get_ea_value(names,
749 handle->conn,
750 NULL,
751 smb_fname,
752 names[i],
753 &ea);
754 if (!NT_STATUS_IS_OK(status)) {
755 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
756 names[i],
757 smb_fname->base_name,
758 nt_errstr(status)));
759 continue;
762 ea.name = talloc_asprintf(
763 ea.value.data, ":%s%s",
764 names[i] + config->prefix_len,
765 config->store_stream_type ? "" : ":$DATA");
766 if (ea.name == NULL) {
767 DEBUG(0, ("talloc failed\n"));
768 continue;
771 if (!fn(&ea, private_data)) {
772 TALLOC_FREE(ea.value.data);
773 return NT_STATUS_OK;
776 TALLOC_FREE(ea.value.data);
779 TALLOC_FREE(names);
780 return NT_STATUS_OK;
783 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
784 struct stream_struct **streams,
785 const char *name, off_t size,
786 off_t alloc_size)
788 struct stream_struct *tmp;
790 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
791 (*num_streams)+1);
792 if (tmp == NULL) {
793 return false;
796 tmp[*num_streams].name = talloc_strdup(tmp, name);
797 if (tmp[*num_streams].name == NULL) {
798 return false;
801 tmp[*num_streams].size = size;
802 tmp[*num_streams].alloc_size = alloc_size;
804 *streams = tmp;
805 *num_streams += 1;
806 return true;
809 struct streaminfo_state {
810 TALLOC_CTX *mem_ctx;
811 vfs_handle_struct *handle;
812 unsigned int num_streams;
813 struct stream_struct *streams;
814 NTSTATUS status;
817 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
819 struct streaminfo_state *state =
820 (struct streaminfo_state *)private_data;
822 if (!add_one_stream(state->mem_ctx,
823 &state->num_streams, &state->streams,
824 ea->name, ea->value.length-1,
825 smb_roundup(state->handle->conn,
826 ea->value.length-1))) {
827 state->status = NT_STATUS_NO_MEMORY;
828 return false;
831 return true;
834 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
835 struct files_struct *fsp,
836 const struct smb_filename *smb_fname,
837 TALLOC_CTX *mem_ctx,
838 unsigned int *pnum_streams,
839 struct stream_struct **pstreams)
841 SMB_STRUCT_STAT sbuf;
842 int ret;
843 NTSTATUS status;
844 struct streaminfo_state state;
846 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
847 if (ret == -1) {
848 return map_nt_error_from_unix(errno);
851 state.streams = *pstreams;
852 state.num_streams = *pnum_streams;
853 state.mem_ctx = mem_ctx;
854 state.handle = handle;
855 state.status = NT_STATUS_OK;
857 if (S_ISLNK(sbuf.st_ex_mode)) {
859 * Currently we do't have SMB_VFS_LLISTXATTR
860 * inside the VFS which means there's no way
861 * to cope with a symlink when lp_posix_pathnames().
862 * returns true. For now ignore links.
863 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
865 status = NT_STATUS_OK;
866 } else {
867 status = walk_xattr_streams(handle, fsp, smb_fname,
868 collect_one_stream, &state);
871 if (!NT_STATUS_IS_OK(status)) {
872 TALLOC_FREE(state.streams);
873 return status;
876 if (!NT_STATUS_IS_OK(state.status)) {
877 TALLOC_FREE(state.streams);
878 return state.status;
881 *pnum_streams = state.num_streams;
882 *pstreams = state.streams;
884 return SMB_VFS_NEXT_STREAMINFO(handle,
885 fsp,
886 smb_fname,
887 mem_ctx,
888 pnum_streams,
889 pstreams);
892 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
893 enum timestamp_set_resolution *p_ts_res)
895 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
898 static int streams_xattr_connect(vfs_handle_struct *handle,
899 const char *service, const char *user)
901 struct streams_xattr_config *config;
902 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
903 const char *prefix;
904 int rc;
906 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
907 if (rc != 0) {
908 return rc;
911 config = talloc_zero(handle->conn, struct streams_xattr_config);
912 if (config == NULL) {
913 DEBUG(1, ("talloc_zero() failed\n"));
914 errno = ENOMEM;
915 return -1;
918 prefix = lp_parm_const_string(SNUM(handle->conn),
919 "streams_xattr", "prefix",
920 default_prefix);
921 config->prefix = talloc_strdup(config, prefix);
922 if (config->prefix == NULL) {
923 DEBUG(1, ("talloc_strdup() failed\n"));
924 errno = ENOMEM;
925 return -1;
927 config->prefix_len = strlen(config->prefix);
928 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
930 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
931 "streams_xattr",
932 "store_stream_type",
933 true);
935 SMB_VFS_HANDLE_SET_DATA(handle, config,
936 NULL, struct stream_xattr_config,
937 return -1);
939 return 0;
942 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
943 files_struct *fsp, const void *data,
944 size_t n, off_t offset)
946 struct stream_io *sio =
947 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
948 struct ea_struct ea;
949 NTSTATUS status;
950 struct smb_filename *smb_fname_base = NULL;
951 int ret;
953 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
955 if (sio == NULL) {
956 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
959 if (!streams_xattr_recheck(sio)) {
960 return -1;
963 /* Create an smb_filename with stream_name == NULL. */
964 smb_fname_base = synthetic_smb_fname(talloc_tos(),
965 sio->base,
966 NULL,
967 NULL,
968 fsp->fsp_name->flags);
969 if (smb_fname_base == NULL) {
970 errno = ENOMEM;
971 return -1;
974 status = get_ea_value(talloc_tos(), handle->conn, NULL,
975 smb_fname_base, sio->xattr_name, &ea);
976 if (!NT_STATUS_IS_OK(status)) {
977 return -1;
980 if ((offset + n) > ea.value.length-1) {
981 uint8_t *tmp;
983 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
984 offset + n + 1);
986 if (tmp == NULL) {
987 TALLOC_FREE(ea.value.data);
988 errno = ENOMEM;
989 return -1;
991 ea.value.data = tmp;
992 ea.value.length = offset + n + 1;
993 ea.value.data[offset+n] = 0;
996 memcpy(ea.value.data + offset, data, n);
998 ret = SMB_VFS_SETXATTR(fsp->conn,
999 fsp->fsp_name,
1000 sio->xattr_name,
1001 ea.value.data, ea.value.length, 0);
1002 TALLOC_FREE(ea.value.data);
1004 if (ret == -1) {
1005 return -1;
1008 return n;
1011 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1012 files_struct *fsp, void *data,
1013 size_t n, off_t offset)
1015 struct stream_io *sio =
1016 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1017 struct ea_struct ea;
1018 NTSTATUS status;
1019 size_t length, overlap;
1020 struct smb_filename *smb_fname_base = NULL;
1022 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1023 (int)offset, (int)n));
1025 if (sio == NULL) {
1026 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1029 if (!streams_xattr_recheck(sio)) {
1030 return -1;
1033 /* Create an smb_filename with stream_name == NULL. */
1034 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1035 sio->base,
1036 NULL,
1037 NULL,
1038 fsp->fsp_name->flags);
1039 if (smb_fname_base == NULL) {
1040 errno = ENOMEM;
1041 return -1;
1044 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1045 smb_fname_base, sio->xattr_name, &ea);
1046 if (!NT_STATUS_IS_OK(status)) {
1047 return -1;
1050 length = ea.value.length-1;
1052 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1053 (int)length));
1055 /* Attempt to read past EOF. */
1056 if (length <= offset) {
1057 return 0;
1060 overlap = (offset + n) > length ? (length - offset) : n;
1061 memcpy(data, ea.value.data + offset, overlap);
1063 TALLOC_FREE(ea.value.data);
1064 return overlap;
1067 struct streams_xattr_pread_state {
1068 ssize_t nread;
1069 struct vfs_aio_state vfs_aio_state;
1072 static void streams_xattr_pread_done(struct tevent_req *subreq);
1074 static struct tevent_req *streams_xattr_pread_send(
1075 struct vfs_handle_struct *handle,
1076 TALLOC_CTX *mem_ctx,
1077 struct tevent_context *ev,
1078 struct files_struct *fsp,
1079 void *data,
1080 size_t n, off_t offset)
1082 struct tevent_req *req = NULL;
1083 struct tevent_req *subreq = NULL;
1084 struct streams_xattr_pread_state *state = NULL;
1085 struct stream_io *sio =
1086 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1088 req = tevent_req_create(mem_ctx, &state,
1089 struct streams_xattr_pread_state);
1090 if (req == NULL) {
1091 return NULL;
1094 if (sio == NULL) {
1095 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1096 data, n, offset);
1097 if (tevent_req_nomem(req, subreq)) {
1098 return tevent_req_post(req, ev);
1100 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1101 return req;
1104 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1105 if (state->nread != n) {
1106 if (state->nread != -1) {
1107 errno = EIO;
1109 tevent_req_error(req, errno);
1110 return tevent_req_post(req, ev);
1113 tevent_req_done(req);
1114 return tevent_req_post(req, ev);
1117 static void streams_xattr_pread_done(struct tevent_req *subreq)
1119 struct tevent_req *req = tevent_req_callback_data(
1120 subreq, struct tevent_req);
1121 struct streams_xattr_pread_state *state = tevent_req_data(
1122 req, struct streams_xattr_pread_state);
1124 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1125 TALLOC_FREE(subreq);
1127 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1128 return;
1130 tevent_req_done(req);
1133 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1134 struct vfs_aio_state *vfs_aio_state)
1136 struct streams_xattr_pread_state *state = tevent_req_data(
1137 req, struct streams_xattr_pread_state);
1139 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1140 return -1;
1143 *vfs_aio_state = state->vfs_aio_state;
1144 return state->nread;
1147 struct streams_xattr_pwrite_state {
1148 ssize_t nwritten;
1149 struct vfs_aio_state vfs_aio_state;
1152 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1154 static struct tevent_req *streams_xattr_pwrite_send(
1155 struct vfs_handle_struct *handle,
1156 TALLOC_CTX *mem_ctx,
1157 struct tevent_context *ev,
1158 struct files_struct *fsp,
1159 const void *data,
1160 size_t n, off_t offset)
1162 struct tevent_req *req = NULL;
1163 struct tevent_req *subreq = NULL;
1164 struct streams_xattr_pwrite_state *state = NULL;
1165 struct stream_io *sio =
1166 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1168 req = tevent_req_create(mem_ctx, &state,
1169 struct streams_xattr_pwrite_state);
1170 if (req == NULL) {
1171 return NULL;
1174 if (sio == NULL) {
1175 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1176 data, n, offset);
1177 if (tevent_req_nomem(req, subreq)) {
1178 return tevent_req_post(req, ev);
1180 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1181 return req;
1184 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1185 if (state->nwritten != n) {
1186 if (state->nwritten != -1) {
1187 errno = EIO;
1189 tevent_req_error(req, errno);
1190 return tevent_req_post(req, ev);
1193 tevent_req_done(req);
1194 return tevent_req_post(req, ev);
1197 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1199 struct tevent_req *req = tevent_req_callback_data(
1200 subreq, struct tevent_req);
1201 struct streams_xattr_pwrite_state *state = tevent_req_data(
1202 req, struct streams_xattr_pwrite_state);
1204 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1205 TALLOC_FREE(subreq);
1207 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1208 return;
1210 tevent_req_done(req);
1213 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1214 struct vfs_aio_state *vfs_aio_state)
1216 struct streams_xattr_pwrite_state *state = tevent_req_data(
1217 req, struct streams_xattr_pwrite_state);
1219 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1220 return -1;
1223 *vfs_aio_state = state->vfs_aio_state;
1224 return state->nwritten;
1227 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1228 struct files_struct *fsp,
1229 off_t offset)
1231 int ret;
1232 uint8_t *tmp;
1233 struct ea_struct ea;
1234 NTSTATUS status;
1235 struct stream_io *sio =
1236 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1237 struct smb_filename *smb_fname_base = NULL;
1239 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1240 fsp_str_dbg(fsp), (double)offset));
1242 if (sio == NULL) {
1243 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1246 if (!streams_xattr_recheck(sio)) {
1247 return -1;
1250 /* Create an smb_filename with stream_name == NULL. */
1251 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1252 sio->base,
1253 NULL,
1254 NULL,
1255 fsp->fsp_name->flags);
1256 if (smb_fname_base == NULL) {
1257 errno = ENOMEM;
1258 return -1;
1261 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1262 smb_fname_base, sio->xattr_name, &ea);
1263 if (!NT_STATUS_IS_OK(status)) {
1264 return -1;
1267 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1268 offset + 1);
1270 if (tmp == NULL) {
1271 TALLOC_FREE(ea.value.data);
1272 errno = ENOMEM;
1273 return -1;
1276 /* Did we expand ? */
1277 if (ea.value.length < offset + 1) {
1278 memset(&tmp[ea.value.length], '\0',
1279 offset + 1 - ea.value.length);
1282 ea.value.data = tmp;
1283 ea.value.length = offset + 1;
1284 ea.value.data[offset] = 0;
1286 ret = SMB_VFS_SETXATTR(fsp->conn,
1287 fsp->fsp_name,
1288 sio->xattr_name,
1289 ea.value.data, ea.value.length, 0);
1290 TALLOC_FREE(ea.value.data);
1292 if (ret == -1) {
1293 return -1;
1296 return 0;
1299 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1300 struct files_struct *fsp,
1301 uint32_t mode,
1302 off_t offset,
1303 off_t len)
1305 struct stream_io *sio =
1306 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1308 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1309 "len = %.0f\n",
1310 fsp_str_dbg(fsp), (double)offset, (double)len));
1312 if (sio == NULL) {
1313 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1316 if (!streams_xattr_recheck(sio)) {
1317 return -1;
1320 /* Let the pwrite code path handle it. */
1321 errno = ENOSYS;
1322 return -1;
1326 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1327 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1328 .connect_fn = streams_xattr_connect,
1329 .open_fn = streams_xattr_open,
1330 .stat_fn = streams_xattr_stat,
1331 .fstat_fn = streams_xattr_fstat,
1332 .lstat_fn = streams_xattr_lstat,
1333 .pread_fn = streams_xattr_pread,
1334 .pwrite_fn = streams_xattr_pwrite,
1335 .pread_send_fn = streams_xattr_pread_send,
1336 .pread_recv_fn = streams_xattr_pread_recv,
1337 .pwrite_send_fn = streams_xattr_pwrite_send,
1338 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1339 .unlink_fn = streams_xattr_unlink,
1340 .rename_fn = streams_xattr_rename,
1341 .ftruncate_fn = streams_xattr_ftruncate,
1342 .fallocate_fn = streams_xattr_fallocate,
1343 .streaminfo_fn = streams_xattr_streaminfo,
1346 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *);
1347 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1349 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1350 &vfs_streams_xattr_fns);