vfs_streams_xattr: add options "prefix" and "store_stream_type"
[Samba/wip.git] / source3 / modules / vfs_streams_xattr.c
blob735db2b9204e784d0df176920d23ac4543d22fa1
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"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_VFS
32 struct streams_xattr_config {
33 const char *prefix;
34 size_t prefix_len;
35 bool store_stream_type;
38 struct stream_io {
39 char *base;
40 char *xattr_name;
41 void *fsp_name_ptr;
42 files_struct *fsp;
43 vfs_handle_struct *handle;
46 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
48 MD5_CTX ctx;
49 unsigned char hash[16];
50 SMB_INO_T result;
51 char *upper_sname;
53 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
54 (unsigned long)sbuf->st_ex_dev,
55 (unsigned long)sbuf->st_ex_ino, sname));
57 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
58 SMB_ASSERT(upper_sname != NULL);
60 MD5Init(&ctx);
61 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
62 sizeof(sbuf->st_ex_dev));
63 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
64 sizeof(sbuf->st_ex_ino));
65 MD5Update(&ctx, (unsigned char *)upper_sname,
66 talloc_get_size(upper_sname)-1);
67 MD5Final(hash, &ctx);
69 TALLOC_FREE(upper_sname);
71 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
72 memcpy(&result, hash, sizeof(result));
74 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
76 return result;
79 static ssize_t get_xattr_size(connection_struct *conn,
80 files_struct *fsp,
81 const char *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, fsp, 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 *stype;
110 struct streams_xattr_config *config;
112 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
113 return NT_STATUS_UNSUCCESSFUL);
115 stype = strchr_m(stream_name + 1, ':');
117 *xattr_name = talloc_asprintf(ctx, "%s%s",
118 config->prefix,
119 stream_name + 1);
120 if (*xattr_name == NULL) {
121 return NT_STATUS_NO_MEMORY;
124 if (stype != NULL) {
125 /* Normalize the stream type to upercase. */
126 if (!strupper_m(strrchr_m(*xattr_name, ':') + 1)) {
127 return NT_STATUS_INVALID_PARAMETER;
129 } else if (config->store_stream_type) {
131 * Append an explicit stream type if one wasn't
132 * specified.
134 *xattr_name = talloc_asprintf(ctx, "%s%s",
135 *xattr_name, ":$DATA");
136 if (*xattr_name == NULL) {
137 return NT_STATUS_NO_MEMORY;
141 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
142 stream_name));
144 return NT_STATUS_OK;
147 static bool streams_xattr_recheck(struct stream_io *sio)
149 NTSTATUS status;
150 char *xattr_name = NULL;
152 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
153 return true;
156 if (sio->fsp->fsp_name->stream_name == NULL) {
157 /* how can this happen */
158 errno = EINVAL;
159 return false;
162 status = streams_xattr_get_name(sio->handle, talloc_tos(),
163 sio->fsp->fsp_name->stream_name,
164 &xattr_name);
165 if (!NT_STATUS_IS_OK(status)) {
166 return false;
169 TALLOC_FREE(sio->xattr_name);
170 TALLOC_FREE(sio->base);
171 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
172 xattr_name);
173 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
174 sio->fsp->fsp_name->base_name);
175 sio->fsp_name_ptr = sio->fsp->fsp_name;
177 TALLOC_FREE(xattr_name);
179 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
180 return false;
183 return true;
187 * Helper to stat/lstat the base file of an smb_fname.
189 static int streams_xattr_stat_base(vfs_handle_struct *handle,
190 struct smb_filename *smb_fname,
191 bool follow_links)
193 char *tmp_stream_name;
194 int result;
196 tmp_stream_name = smb_fname->stream_name;
197 smb_fname->stream_name = NULL;
198 if (follow_links) {
199 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
200 } else {
201 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
203 smb_fname->stream_name = tmp_stream_name;
204 return result;
207 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
208 SMB_STRUCT_STAT *sbuf)
210 struct smb_filename *smb_fname_base = NULL;
211 int ret = -1;
212 struct stream_io *io = (struct stream_io *)
213 VFS_FETCH_FSP_EXTENSION(handle, fsp);
215 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
217 if (io == NULL || fsp->base_fsp == NULL) {
218 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
221 if (!streams_xattr_recheck(io)) {
222 return -1;
225 /* Create an smb_filename with stream_name == NULL. */
226 smb_fname_base = synthetic_smb_fname(talloc_tos(), io->base,
227 NULL, NULL);
228 if (smb_fname_base == NULL) {
229 errno = ENOMEM;
230 return -1;
233 if (lp_posix_pathnames()) {
234 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
235 } else {
236 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
238 *sbuf = smb_fname_base->st;
239 TALLOC_FREE(smb_fname_base);
241 if (ret == -1) {
242 return -1;
245 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
246 io->base, io->xattr_name);
247 if (sbuf->st_ex_size == -1) {
248 return -1;
251 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
253 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
254 sbuf->st_ex_mode &= ~S_IFMT;
255 sbuf->st_ex_mode |= S_IFREG;
256 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
258 return 0;
261 static int streams_xattr_stat(vfs_handle_struct *handle,
262 struct smb_filename *smb_fname)
264 NTSTATUS status;
265 int result = -1;
266 char *xattr_name = NULL;
268 if (!is_ntfs_stream_smb_fname(smb_fname)) {
269 return SMB_VFS_NEXT_STAT(handle, smb_fname);
272 /* Note if lp_posix_paths() is true, we can never
273 * get here as is_ntfs_stream_smb_fname() is
274 * always false. So we never need worry about
275 * not following links here. */
277 /* If the default stream is requested, just stat the base file. */
278 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
279 return streams_xattr_stat_base(handle, smb_fname, true);
282 /* Populate the stat struct with info from the base file. */
283 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
284 return -1;
287 /* Derive the xattr name to lookup. */
288 status = streams_xattr_get_name(handle, talloc_tos(),
289 smb_fname->stream_name, &xattr_name);
290 if (!NT_STATUS_IS_OK(status)) {
291 errno = map_errno_from_nt_status(status);
292 return -1;
295 /* Augment the base file's stat information before returning. */
296 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
297 smb_fname->base_name,
298 xattr_name);
299 if (smb_fname->st.st_ex_size == -1) {
300 errno = ENOENT;
301 result = -1;
302 goto fail;
305 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
306 smb_fname->st.st_ex_mode &= ~S_IFMT;
307 smb_fname->st.st_ex_mode |= S_IFREG;
308 smb_fname->st.st_ex_blocks =
309 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
311 result = 0;
312 fail:
313 TALLOC_FREE(xattr_name);
314 return result;
317 static int streams_xattr_lstat(vfs_handle_struct *handle,
318 struct smb_filename *smb_fname)
320 NTSTATUS status;
321 int result = -1;
322 char *xattr_name = NULL;
324 if (!is_ntfs_stream_smb_fname(smb_fname)) {
325 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
328 /* If the default stream is requested, just stat the base file. */
329 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
330 return streams_xattr_stat_base(handle, smb_fname, false);
333 /* Populate the stat struct with info from the base file. */
334 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
335 return -1;
338 /* Derive the xattr name to lookup. */
339 status = streams_xattr_get_name(handle, talloc_tos(),
340 smb_fname->stream_name, &xattr_name);
341 if (!NT_STATUS_IS_OK(status)) {
342 errno = map_errno_from_nt_status(status);
343 return -1;
346 /* Augment the base file's stat information before returning. */
347 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
348 smb_fname->base_name,
349 xattr_name);
350 if (smb_fname->st.st_ex_size == -1) {
351 errno = ENOENT;
352 result = -1;
353 goto fail;
356 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
357 smb_fname->st.st_ex_mode &= ~S_IFMT;
358 smb_fname->st.st_ex_mode |= S_IFREG;
359 smb_fname->st.st_ex_blocks =
360 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
362 result = 0;
364 fail:
365 TALLOC_FREE(xattr_name);
366 return result;
369 static int streams_xattr_open(vfs_handle_struct *handle,
370 struct smb_filename *smb_fname,
371 files_struct *fsp, int flags, mode_t mode)
373 NTSTATUS status;
374 struct smb_filename *smb_fname_base = NULL;
375 struct stream_io *sio;
376 struct ea_struct ea;
377 char *xattr_name = NULL;
378 int baseflags;
379 int hostfd = -1;
381 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
382 smb_fname_str_dbg(smb_fname), flags));
384 if (!is_ntfs_stream_smb_fname(smb_fname)) {
385 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
388 /* If the default stream is requested, just open the base file. */
389 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
390 char *tmp_stream_name;
391 int ret;
393 tmp_stream_name = smb_fname->stream_name;
394 smb_fname->stream_name = NULL;
396 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
398 smb_fname->stream_name = tmp_stream_name;
400 return ret;
403 status = streams_xattr_get_name(handle, talloc_tos(),
404 smb_fname->stream_name, &xattr_name);
405 if (!NT_STATUS_IS_OK(status)) {
406 errno = map_errno_from_nt_status(status);
407 goto fail;
410 /* Create an smb_filename with stream_name == NULL. */
411 smb_fname_base = synthetic_smb_fname(
412 talloc_tos(), smb_fname->base_name, NULL, NULL);
413 if (smb_fname_base == NULL) {
414 errno = ENOMEM;
415 goto fail;
419 * We use baseflags to turn off nasty side-effects when opening the
420 * underlying file.
422 baseflags = flags;
423 baseflags &= ~O_TRUNC;
424 baseflags &= ~O_EXCL;
425 baseflags &= ~O_CREAT;
427 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
428 baseflags, mode);
430 TALLOC_FREE(smb_fname_base);
432 /* It is legit to open a stream on a directory, but the base
433 * fd has to be read-only.
435 if ((hostfd == -1) && (errno == EISDIR)) {
436 baseflags &= ~O_ACCMODE;
437 baseflags |= O_RDONLY;
438 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
439 mode);
442 if (hostfd == -1) {
443 goto fail;
446 status = get_ea_value(talloc_tos(), handle->conn, NULL,
447 smb_fname->base_name, xattr_name, &ea);
449 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
451 if (!NT_STATUS_IS_OK(status)
452 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
454 * The base file is not there. This is an error even if we got
455 * O_CREAT, the higher levels should have created the base
456 * file for us.
458 DEBUG(10, ("streams_xattr_open: base file %s not around, "
459 "returning ENOENT\n", smb_fname->base_name));
460 errno = ENOENT;
461 goto fail;
464 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
465 (flags & O_TRUNC)) {
467 * The attribute does not exist or needs to be truncated
471 * Darn, xattrs need at least 1 byte
473 char null = '\0';
475 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
476 xattr_name, smb_fname->base_name));
478 if (fsp->base_fsp->fh->fd != -1) {
479 if (SMB_VFS_FSETXATTR(
480 fsp->base_fsp, xattr_name,
481 &null, sizeof(null),
482 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
483 goto fail;
485 } else {
486 if (SMB_VFS_SETXATTR(
487 handle->conn, smb_fname->base_name,
488 xattr_name, &null, sizeof(null),
489 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
490 goto fail;
495 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
496 struct stream_io,
497 NULL);
498 if (sio == NULL) {
499 errno = ENOMEM;
500 goto fail;
503 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
504 xattr_name);
505 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
506 smb_fname->base_name);
507 sio->fsp_name_ptr = fsp->fsp_name;
508 sio->handle = handle;
509 sio->fsp = fsp;
511 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
512 errno = ENOMEM;
513 goto fail;
516 return hostfd;
518 fail:
519 if (hostfd >= 0) {
521 * BUGBUGBUG -- we would need to call fd_close_posix here, but
522 * we don't have a full fsp yet
524 fsp->fh->fd = hostfd;
525 SMB_VFS_CLOSE(fsp);
528 return -1;
531 static int streams_xattr_unlink(vfs_handle_struct *handle,
532 const struct smb_filename *smb_fname)
534 NTSTATUS status;
535 int ret = -1;
536 char *xattr_name;
538 if (!is_ntfs_stream_smb_fname(smb_fname)) {
539 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
542 /* If the default stream is requested, just open the base file. */
543 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
544 struct smb_filename *smb_fname_base = NULL;
546 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
547 if (smb_fname_base == NULL) {
548 errno = ENOMEM;
549 return -1;
552 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
554 TALLOC_FREE(smb_fname_base);
555 return ret;
558 status = streams_xattr_get_name(handle, talloc_tos(),
559 smb_fname->stream_name, &xattr_name);
560 if (!NT_STATUS_IS_OK(status)) {
561 errno = map_errno_from_nt_status(status);
562 goto fail;
565 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
567 if ((ret == -1) && (errno == ENOATTR)) {
568 errno = ENOENT;
569 goto fail;
572 ret = 0;
574 fail:
575 TALLOC_FREE(xattr_name);
576 return ret;
579 static int streams_xattr_rename(vfs_handle_struct *handle,
580 const struct smb_filename *smb_fname_src,
581 const struct smb_filename *smb_fname_dst)
583 NTSTATUS status;
584 int ret = -1;
585 char *src_xattr_name = NULL;
586 char *dst_xattr_name = NULL;
587 bool src_is_stream, dst_is_stream;
588 ssize_t oret;
589 ssize_t nret;
590 struct ea_struct ea;
592 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
593 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
595 if (!src_is_stream && !dst_is_stream) {
596 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
597 smb_fname_dst);
600 /* For now don't allow renames from or to the default stream. */
601 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
602 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
603 errno = ENOSYS;
604 goto done;
607 /* Don't rename if the streams are identical. */
608 if (strcasecmp_m(smb_fname_src->stream_name,
609 smb_fname_dst->stream_name) == 0) {
610 goto done;
613 /* Get the xattr names. */
614 status = streams_xattr_get_name(handle, talloc_tos(),
615 smb_fname_src->stream_name,
616 &src_xattr_name);
617 if (!NT_STATUS_IS_OK(status)) {
618 errno = map_errno_from_nt_status(status);
619 goto fail;
621 status = streams_xattr_get_name(handle, talloc_tos(),
622 smb_fname_dst->stream_name,
623 &dst_xattr_name);
624 if (!NT_STATUS_IS_OK(status)) {
625 errno = map_errno_from_nt_status(status);
626 goto fail;
629 /* read the old stream */
630 status = get_ea_value(talloc_tos(), handle->conn, NULL,
631 smb_fname_src->base_name, src_xattr_name, &ea);
632 if (!NT_STATUS_IS_OK(status)) {
633 errno = ENOENT;
634 goto fail;
637 /* (over)write the new stream */
638 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
639 dst_xattr_name, ea.value.data, ea.value.length,
641 if (nret < 0) {
642 if (errno == ENOATTR) {
643 errno = ENOENT;
645 goto fail;
648 /* remove the old stream */
649 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
650 src_xattr_name);
651 if (oret < 0) {
652 if (errno == ENOATTR) {
653 errno = ENOENT;
655 goto fail;
658 done:
659 errno = 0;
660 ret = 0;
661 fail:
662 TALLOC_FREE(src_xattr_name);
663 TALLOC_FREE(dst_xattr_name);
664 return ret;
667 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle, files_struct *fsp,
668 const char *fname,
669 bool (*fn)(struct ea_struct *ea,
670 void *private_data),
671 void *private_data)
673 NTSTATUS status;
674 char **names;
675 size_t i, num_names;
676 struct streams_xattr_config *config;
678 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
679 return NT_STATUS_UNSUCCESSFUL);
681 status = get_ea_names_from_file(talloc_tos(), handle->conn, fsp, fname,
682 &names, &num_names);
683 if (!NT_STATUS_IS_OK(status)) {
684 return status;
687 for (i=0; i<num_names; i++) {
688 struct ea_struct ea;
690 if (strncmp(names[i], config->prefix,
691 config->prefix_len) != 0) {
692 continue;
694 if (samba_private_attr_name(names[i])) {
695 continue;
698 status = get_ea_value(names, handle->conn, fsp, fname,
699 names[i], &ea);
700 if (!NT_STATUS_IS_OK(status)) {
701 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
702 names[i], fname, nt_errstr(status)));
703 continue;
706 ea.name = talloc_asprintf(
707 ea.value.data, ":%s%s",
708 names[i] + config->prefix_len,
709 config->store_stream_type ? "" : ":$DATA");
710 if (ea.name == NULL) {
711 DEBUG(0, ("talloc failed\n"));
712 continue;
715 if (!fn(&ea, private_data)) {
716 TALLOC_FREE(ea.value.data);
717 return NT_STATUS_OK;
720 TALLOC_FREE(ea.value.data);
723 TALLOC_FREE(names);
724 return NT_STATUS_OK;
727 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
728 struct stream_struct **streams,
729 const char *name, off_t size,
730 off_t alloc_size)
732 struct stream_struct *tmp;
734 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
735 (*num_streams)+1);
736 if (tmp == NULL) {
737 return false;
740 tmp[*num_streams].name = talloc_strdup(tmp, name);
741 if (tmp[*num_streams].name == NULL) {
742 return false;
745 tmp[*num_streams].size = size;
746 tmp[*num_streams].alloc_size = alloc_size;
748 *streams = tmp;
749 *num_streams += 1;
750 return true;
753 struct streaminfo_state {
754 TALLOC_CTX *mem_ctx;
755 vfs_handle_struct *handle;
756 unsigned int num_streams;
757 struct stream_struct *streams;
758 NTSTATUS status;
761 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
763 struct streaminfo_state *state =
764 (struct streaminfo_state *)private_data;
766 if (!add_one_stream(state->mem_ctx,
767 &state->num_streams, &state->streams,
768 ea->name, ea->value.length-1,
769 smb_roundup(state->handle->conn,
770 ea->value.length-1))) {
771 state->status = NT_STATUS_NO_MEMORY;
772 return false;
775 return true;
778 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
779 struct files_struct *fsp,
780 const char *fname,
781 TALLOC_CTX *mem_ctx,
782 unsigned int *pnum_streams,
783 struct stream_struct **pstreams)
785 SMB_STRUCT_STAT sbuf;
786 int ret;
787 NTSTATUS status;
788 struct streaminfo_state state;
790 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
791 ret = SMB_VFS_FSTAT(fsp, &sbuf);
793 else {
794 struct smb_filename *smb_fname = NULL;
795 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL,
796 NULL);
797 if (smb_fname == NULL) {
798 return NT_STATUS_NO_MEMORY;
800 if (lp_posix_pathnames()) {
801 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
802 } else {
803 ret = SMB_VFS_STAT(handle->conn, smb_fname);
805 sbuf = smb_fname->st;
806 TALLOC_FREE(smb_fname);
809 if (ret == -1) {
810 return map_nt_error_from_unix(errno);
813 state.streams = *pstreams;
814 state.num_streams = *pnum_streams;
815 state.mem_ctx = mem_ctx;
816 state.handle = handle;
817 state.status = NT_STATUS_OK;
819 if (S_ISLNK(sbuf.st_ex_mode)) {
821 * Currently we do't have SMB_VFS_LLISTXATTR
822 * inside the VFS which means there's no way
823 * to cope with a symlink when lp_posix_pathnames().
824 * returns true. For now ignore links.
825 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
827 status = NT_STATUS_OK;
828 } else {
829 status = walk_xattr_streams(handle, fsp, fname,
830 collect_one_stream, &state);
833 if (!NT_STATUS_IS_OK(status)) {
834 TALLOC_FREE(state.streams);
835 return status;
838 if (!NT_STATUS_IS_OK(state.status)) {
839 TALLOC_FREE(state.streams);
840 return state.status;
843 *pnum_streams = state.num_streams;
844 *pstreams = state.streams;
846 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
849 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
850 enum timestamp_set_resolution *p_ts_res)
852 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
855 static int streams_xattr_connect(vfs_handle_struct *handle,
856 const char *service, const char *user)
858 struct streams_xattr_config *config;
859 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
860 const char *prefix;
862 config = talloc_zero(handle->conn, struct streams_xattr_config);
863 if (config == NULL) {
864 DEBUG(1, ("talloc_zero() failed\n"));
865 errno = ENOMEM;
866 return -1;
869 prefix = lp_parm_const_string(SNUM(handle->conn),
870 "streams_xattr", "prefix",
871 default_prefix);
872 config->prefix = talloc_strdup(config, prefix);
873 if (config->prefix == NULL) {
874 DEBUG(1, ("talloc_strdup() failed\n"));
875 errno = ENOMEM;
876 return -1;
878 config->prefix_len = strlen(config->prefix);
879 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
881 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
882 "streams_xattr",
883 "store_stream_type",
884 true);
886 SMB_VFS_HANDLE_SET_DATA(handle, config,
887 NULL, struct stream_xattr_config,
888 return -1);
890 return 0;
893 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
894 files_struct *fsp, const void *data,
895 size_t n, off_t offset)
897 struct stream_io *sio =
898 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
899 struct ea_struct ea;
900 NTSTATUS status;
901 int ret;
903 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
905 if (sio == NULL) {
906 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
909 if (!streams_xattr_recheck(sio)) {
910 return -1;
913 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
914 sio->base, sio->xattr_name, &ea);
915 if (!NT_STATUS_IS_OK(status)) {
916 return -1;
919 if ((offset + n) > ea.value.length-1) {
920 uint8 *tmp;
922 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
923 offset + n + 1);
925 if (tmp == NULL) {
926 TALLOC_FREE(ea.value.data);
927 errno = ENOMEM;
928 return -1;
930 ea.value.data = tmp;
931 ea.value.length = offset + n + 1;
932 ea.value.data[offset+n] = 0;
935 memcpy(ea.value.data + offset, data, n);
937 if (fsp->base_fsp->fh->fd != -1) {
938 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
939 sio->xattr_name,
940 ea.value.data, ea.value.length, 0);
941 } else {
942 ret = SMB_VFS_SETXATTR(fsp->conn,
943 fsp->base_fsp->fsp_name->base_name,
944 sio->xattr_name,
945 ea.value.data, ea.value.length, 0);
947 TALLOC_FREE(ea.value.data);
949 if (ret == -1) {
950 return -1;
953 return n;
956 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
957 files_struct *fsp, void *data,
958 size_t n, off_t offset)
960 struct stream_io *sio =
961 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
962 struct ea_struct ea;
963 NTSTATUS status;
964 size_t length, overlap;
966 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
967 (int)offset, (int)n));
969 if (sio == NULL) {
970 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
973 if (!streams_xattr_recheck(sio)) {
974 return -1;
977 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
978 sio->base, sio->xattr_name, &ea);
979 if (!NT_STATUS_IS_OK(status)) {
980 return -1;
983 length = ea.value.length-1;
985 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
986 (int)length));
988 /* Attempt to read past EOF. */
989 if (length <= offset) {
990 return 0;
993 overlap = (offset + n) > length ? (length - offset) : n;
994 memcpy(data, ea.value.data + offset, overlap);
996 TALLOC_FREE(ea.value.data);
997 return overlap;
1000 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1001 struct files_struct *fsp,
1002 off_t offset)
1004 int ret;
1005 uint8 *tmp;
1006 struct ea_struct ea;
1007 NTSTATUS status;
1008 struct stream_io *sio =
1009 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1011 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1012 fsp_str_dbg(fsp), (double)offset));
1014 if (sio == NULL) {
1015 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1018 if (!streams_xattr_recheck(sio)) {
1019 return -1;
1022 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1023 sio->base, sio->xattr_name, &ea);
1024 if (!NT_STATUS_IS_OK(status)) {
1025 return -1;
1028 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8,
1029 offset + 1);
1031 if (tmp == NULL) {
1032 TALLOC_FREE(ea.value.data);
1033 errno = ENOMEM;
1034 return -1;
1037 /* Did we expand ? */
1038 if (ea.value.length < offset + 1) {
1039 memset(&tmp[ea.value.length], '\0',
1040 offset + 1 - ea.value.length);
1043 ea.value.data = tmp;
1044 ea.value.length = offset + 1;
1045 ea.value.data[offset] = 0;
1047 if (fsp->base_fsp->fh->fd != -1) {
1048 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1049 sio->xattr_name,
1050 ea.value.data, ea.value.length, 0);
1051 } else {
1052 ret = SMB_VFS_SETXATTR(fsp->conn,
1053 fsp->base_fsp->fsp_name->base_name,
1054 sio->xattr_name,
1055 ea.value.data, ea.value.length, 0);
1058 TALLOC_FREE(ea.value.data);
1060 if (ret == -1) {
1061 return -1;
1064 return 0;
1067 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1068 struct files_struct *fsp,
1069 enum vfs_fallocate_mode mode,
1070 off_t offset,
1071 off_t len)
1073 struct stream_io *sio =
1074 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1076 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1077 "len = %.0f\n",
1078 fsp_str_dbg(fsp), (double)offset, (double)len));
1080 if (sio == NULL) {
1081 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1084 if (!streams_xattr_recheck(sio)) {
1085 return errno;
1088 /* Let the pwrite code path handle it. */
1089 return ENOSYS;
1093 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1094 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1095 .connect_fn = streams_xattr_connect,
1096 .open_fn = streams_xattr_open,
1097 .stat_fn = streams_xattr_stat,
1098 .fstat_fn = streams_xattr_fstat,
1099 .lstat_fn = streams_xattr_lstat,
1100 .pread_fn = streams_xattr_pread,
1101 .pwrite_fn = streams_xattr_pwrite,
1102 .unlink_fn = streams_xattr_unlink,
1103 .rename_fn = streams_xattr_rename,
1104 .ftruncate_fn = streams_xattr_ftruncate,
1105 .fallocate_fn = streams_xattr_fallocate,
1106 .streaminfo_fn = streams_xattr_streaminfo,
1109 NTSTATUS vfs_streams_xattr_init(void);
1110 NTSTATUS vfs_streams_xattr_init(void)
1112 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1113 &vfs_streams_xattr_fns);