libads: abstract out SASL wrapping code
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blob2943e521264cc1c01eabd1fc324e49f371c278f9
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 *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 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
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;
261 TALLOC_FREE(smb_fname_base);
263 if (ret == -1) {
264 return -1;
267 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp,
268 io->base, io->xattr_name);
269 if (sbuf->st_ex_size == -1) {
270 return -1;
273 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
275 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
276 sbuf->st_ex_mode &= ~S_IFMT;
277 sbuf->st_ex_mode |= S_IFREG;
278 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
280 return 0;
283 static int streams_xattr_stat(vfs_handle_struct *handle,
284 struct smb_filename *smb_fname)
286 NTSTATUS status;
287 int result = -1;
288 char *xattr_name = NULL;
290 if (!is_ntfs_stream_smb_fname(smb_fname)) {
291 return SMB_VFS_NEXT_STAT(handle, smb_fname);
294 /* Note if lp_posix_paths() is true, we can never
295 * get here as is_ntfs_stream_smb_fname() is
296 * always false. So we never need worry about
297 * not following links here. */
299 /* If the default stream is requested, just stat the base file. */
300 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
301 return streams_xattr_stat_base(handle, smb_fname, true);
304 /* Populate the stat struct with info from the base file. */
305 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
306 return -1;
309 /* Derive the xattr name to lookup. */
310 status = streams_xattr_get_name(handle, talloc_tos(),
311 smb_fname->stream_name, &xattr_name);
312 if (!NT_STATUS_IS_OK(status)) {
313 errno = map_errno_from_nt_status(status);
314 return -1;
317 /* Augment the base file's stat information before returning. */
318 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
319 smb_fname->base_name,
320 xattr_name);
321 if (smb_fname->st.st_ex_size == -1) {
322 errno = ENOENT;
323 result = -1;
324 goto fail;
327 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
328 smb_fname->st.st_ex_mode &= ~S_IFMT;
329 smb_fname->st.st_ex_mode |= S_IFREG;
330 smb_fname->st.st_ex_blocks =
331 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
333 result = 0;
334 fail:
335 TALLOC_FREE(xattr_name);
336 return result;
339 static int streams_xattr_lstat(vfs_handle_struct *handle,
340 struct smb_filename *smb_fname)
342 NTSTATUS status;
343 int result = -1;
344 char *xattr_name = NULL;
346 if (!is_ntfs_stream_smb_fname(smb_fname)) {
347 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
350 /* If the default stream is requested, just stat the base file. */
351 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
352 return streams_xattr_stat_base(handle, smb_fname, false);
355 /* Populate the stat struct with info from the base file. */
356 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
357 return -1;
360 /* Derive the xattr name to lookup. */
361 status = streams_xattr_get_name(handle, talloc_tos(),
362 smb_fname->stream_name, &xattr_name);
363 if (!NT_STATUS_IS_OK(status)) {
364 errno = map_errno_from_nt_status(status);
365 return -1;
368 /* Augment the base file's stat information before returning. */
369 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
370 smb_fname->base_name,
371 xattr_name);
372 if (smb_fname->st.st_ex_size == -1) {
373 errno = ENOENT;
374 result = -1;
375 goto fail;
378 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
379 smb_fname->st.st_ex_mode &= ~S_IFMT;
380 smb_fname->st.st_ex_mode |= S_IFREG;
381 smb_fname->st.st_ex_blocks =
382 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
384 result = 0;
386 fail:
387 TALLOC_FREE(xattr_name);
388 return result;
391 static int streams_xattr_open(vfs_handle_struct *handle,
392 struct smb_filename *smb_fname,
393 files_struct *fsp, int flags, mode_t mode)
395 NTSTATUS status;
396 struct smb_filename *smb_fname_base = NULL;
397 struct stream_io *sio;
398 struct ea_struct ea;
399 char *xattr_name = NULL;
400 int baseflags;
401 int hostfd = -1;
402 int ret;
404 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
405 smb_fname_str_dbg(smb_fname), flags));
407 if (!is_ntfs_stream_smb_fname(smb_fname)) {
408 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
411 /* If the default stream is requested, just open the base file. */
412 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
413 char *tmp_stream_name;
415 tmp_stream_name = smb_fname->stream_name;
416 smb_fname->stream_name = NULL;
418 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
420 smb_fname->stream_name = tmp_stream_name;
422 return ret;
425 status = streams_xattr_get_name(handle, talloc_tos(),
426 smb_fname->stream_name, &xattr_name);
427 if (!NT_STATUS_IS_OK(status)) {
428 errno = map_errno_from_nt_status(status);
429 goto fail;
432 /* Create an smb_filename with stream_name == NULL. */
433 smb_fname_base = synthetic_smb_fname(talloc_tos(),
434 smb_fname->base_name,
435 NULL,
436 NULL,
437 smb_fname->flags);
438 if (smb_fname_base == NULL) {
439 errno = ENOMEM;
440 goto fail;
444 * We use baseflags to turn off nasty side-effects when opening the
445 * underlying file.
447 baseflags = flags;
448 baseflags &= ~O_TRUNC;
449 baseflags &= ~O_EXCL;
450 baseflags &= ~O_CREAT;
452 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
453 baseflags, mode);
455 /* It is legit to open a stream on a directory, but the base
456 * fd has to be read-only.
458 if ((hostfd == -1) && (errno == EISDIR)) {
459 baseflags &= ~O_ACCMODE;
460 baseflags |= O_RDONLY;
461 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp, baseflags,
462 mode);
465 TALLOC_FREE(smb_fname_base);
467 if (hostfd == -1) {
468 goto fail;
471 status = get_ea_value(talloc_tos(), handle->conn, NULL,
472 smb_fname->base_name, xattr_name, &ea);
474 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
476 if (!NT_STATUS_IS_OK(status)
477 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
479 * The base file is not there. This is an error even if we got
480 * O_CREAT, the higher levels should have created the base
481 * file for us.
483 DEBUG(10, ("streams_xattr_open: base file %s not around, "
484 "returning ENOENT\n", smb_fname->base_name));
485 errno = ENOENT;
486 goto fail;
489 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
490 (flags & O_TRUNC)) {
492 * The attribute does not exist or needs to be truncated
496 * Darn, xattrs need at least 1 byte
498 char null = '\0';
500 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
501 xattr_name, smb_fname->base_name));
503 fsp->fh->fd = hostfd;
504 ret = SMB_VFS_FSETXATTR(fsp, xattr_name,
505 &null, sizeof(null),
506 flags & O_EXCL ? XATTR_CREATE : 0);
507 fsp->fh->fd = -1;
508 if (ret != 0) {
509 goto fail;
513 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
514 struct stream_io,
515 NULL);
516 if (sio == NULL) {
517 errno = ENOMEM;
518 goto fail;
521 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
522 xattr_name);
524 * so->base needs to be a copy of fsp->fsp_name->base_name,
525 * making it identical to streams_xattr_recheck(). If the
526 * open is changing directories, fsp->fsp_name->base_name
527 * will be the full path from the share root, whilst
528 * smb_fname will be relative to the $cwd.
530 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
531 fsp->fsp_name->base_name);
532 sio->fsp_name_ptr = fsp->fsp_name;
533 sio->handle = handle;
534 sio->fsp = fsp;
536 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
537 errno = ENOMEM;
538 goto fail;
541 return hostfd;
543 fail:
544 if (hostfd >= 0) {
546 * BUGBUGBUG -- we would need to call fd_close_posix here, but
547 * we don't have a full fsp yet
549 fsp->fh->fd = hostfd;
550 SMB_VFS_NEXT_CLOSE(handle, fsp);
553 return -1;
556 static int streams_xattr_unlink(vfs_handle_struct *handle,
557 const struct smb_filename *smb_fname)
559 NTSTATUS status;
560 int ret = -1;
561 char *xattr_name = NULL;
563 if (!is_ntfs_stream_smb_fname(smb_fname)) {
564 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
567 /* If the default stream is requested, just open the base file. */
568 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
569 struct smb_filename *smb_fname_base = NULL;
571 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
572 if (smb_fname_base == NULL) {
573 errno = ENOMEM;
574 return -1;
577 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
579 TALLOC_FREE(smb_fname_base);
580 return ret;
583 status = streams_xattr_get_name(handle, talloc_tos(),
584 smb_fname->stream_name, &xattr_name);
585 if (!NT_STATUS_IS_OK(status)) {
586 errno = map_errno_from_nt_status(status);
587 goto fail;
590 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
592 if ((ret == -1) && (errno == ENOATTR)) {
593 errno = ENOENT;
594 goto fail;
597 ret = 0;
599 fail:
600 TALLOC_FREE(xattr_name);
601 return ret;
604 static int streams_xattr_rename(vfs_handle_struct *handle,
605 const struct smb_filename *smb_fname_src,
606 const struct smb_filename *smb_fname_dst)
608 NTSTATUS status;
609 int ret = -1;
610 char *src_xattr_name = NULL;
611 char *dst_xattr_name = NULL;
612 bool src_is_stream, dst_is_stream;
613 ssize_t oret;
614 ssize_t nret;
615 struct ea_struct ea;
617 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
618 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
620 if (!src_is_stream && !dst_is_stream) {
621 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
622 smb_fname_dst);
625 /* For now don't allow renames from or to the default stream. */
626 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
627 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
628 errno = ENOSYS;
629 goto done;
632 /* Don't rename if the streams are identical. */
633 if (strcasecmp_m(smb_fname_src->stream_name,
634 smb_fname_dst->stream_name) == 0) {
635 goto done;
638 /* Get the xattr names. */
639 status = streams_xattr_get_name(handle, talloc_tos(),
640 smb_fname_src->stream_name,
641 &src_xattr_name);
642 if (!NT_STATUS_IS_OK(status)) {
643 errno = map_errno_from_nt_status(status);
644 goto fail;
646 status = streams_xattr_get_name(handle, talloc_tos(),
647 smb_fname_dst->stream_name,
648 &dst_xattr_name);
649 if (!NT_STATUS_IS_OK(status)) {
650 errno = map_errno_from_nt_status(status);
651 goto fail;
654 /* read the old stream */
655 status = get_ea_value(talloc_tos(), handle->conn, NULL,
656 smb_fname_src->base_name, src_xattr_name, &ea);
657 if (!NT_STATUS_IS_OK(status)) {
658 errno = ENOENT;
659 goto fail;
662 /* (over)write the new stream */
663 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
664 dst_xattr_name, ea.value.data, ea.value.length,
666 if (nret < 0) {
667 if (errno == ENOATTR) {
668 errno = ENOENT;
670 goto fail;
673 /* remove the old stream */
674 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
675 src_xattr_name);
676 if (oret < 0) {
677 if (errno == ENOATTR) {
678 errno = ENOENT;
680 goto fail;
683 done:
684 errno = 0;
685 ret = 0;
686 fail:
687 TALLOC_FREE(src_xattr_name);
688 TALLOC_FREE(dst_xattr_name);
689 return ret;
692 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
693 files_struct *fsp,
694 const struct smb_filename *smb_fname,
695 bool (*fn)(struct ea_struct *ea,
696 void *private_data),
697 void *private_data)
699 NTSTATUS status;
700 char **names;
701 size_t i, num_names;
702 struct streams_xattr_config *config;
704 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
705 return NT_STATUS_UNSUCCESSFUL);
707 status = get_ea_names_from_file(talloc_tos(),
708 handle->conn,
709 fsp,
710 smb_fname,
711 &names,
712 &num_names);
713 if (!NT_STATUS_IS_OK(status)) {
714 return status;
717 for (i=0; i<num_names; i++) {
718 struct ea_struct ea;
721 * We want to check with samba_private_attr_name()
722 * whether the xattr name is a private one,
723 * unfortunately it flags xattrs that begin with the
724 * default streams prefix as private.
726 * By only calling samba_private_attr_name() in case
727 * the xattr does NOT begin with the default prefix,
728 * we know that if it returns 'true' it definitely one
729 * of our internal xattr like "user.DOSATTRIB".
731 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
732 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
733 if (samba_private_attr_name(names[i])) {
734 continue;
738 if (strncmp(names[i], config->prefix,
739 config->prefix_len) != 0) {
740 continue;
743 status = get_ea_value(names,
744 handle->conn,
745 fsp,
746 smb_fname->base_name,
747 names[i],
748 &ea);
749 if (!NT_STATUS_IS_OK(status)) {
750 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
751 names[i],
752 smb_fname->base_name,
753 nt_errstr(status)));
754 continue;
757 ea.name = talloc_asprintf(
758 ea.value.data, ":%s%s",
759 names[i] + config->prefix_len,
760 config->store_stream_type ? "" : ":$DATA");
761 if (ea.name == NULL) {
762 DEBUG(0, ("talloc failed\n"));
763 continue;
766 if (!fn(&ea, private_data)) {
767 TALLOC_FREE(ea.value.data);
768 return NT_STATUS_OK;
771 TALLOC_FREE(ea.value.data);
774 TALLOC_FREE(names);
775 return NT_STATUS_OK;
778 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
779 struct stream_struct **streams,
780 const char *name, off_t size,
781 off_t alloc_size)
783 struct stream_struct *tmp;
785 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
786 (*num_streams)+1);
787 if (tmp == NULL) {
788 return false;
791 tmp[*num_streams].name = talloc_strdup(tmp, name);
792 if (tmp[*num_streams].name == NULL) {
793 return false;
796 tmp[*num_streams].size = size;
797 tmp[*num_streams].alloc_size = alloc_size;
799 *streams = tmp;
800 *num_streams += 1;
801 return true;
804 struct streaminfo_state {
805 TALLOC_CTX *mem_ctx;
806 vfs_handle_struct *handle;
807 unsigned int num_streams;
808 struct stream_struct *streams;
809 NTSTATUS status;
812 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
814 struct streaminfo_state *state =
815 (struct streaminfo_state *)private_data;
817 if (!add_one_stream(state->mem_ctx,
818 &state->num_streams, &state->streams,
819 ea->name, ea->value.length-1,
820 smb_roundup(state->handle->conn,
821 ea->value.length-1))) {
822 state->status = NT_STATUS_NO_MEMORY;
823 return false;
826 return true;
829 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
830 struct files_struct *fsp,
831 const struct smb_filename *smb_fname,
832 TALLOC_CTX *mem_ctx,
833 unsigned int *pnum_streams,
834 struct stream_struct **pstreams)
836 SMB_STRUCT_STAT sbuf;
837 int ret;
838 NTSTATUS status;
839 struct streaminfo_state state;
841 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
842 ret = SMB_VFS_FSTAT(fsp, &sbuf);
843 } else {
844 ret = vfs_stat_smb_basename(handle->conn,
845 smb_fname,
846 &sbuf);
849 if (ret == -1) {
850 return map_nt_error_from_unix(errno);
853 state.streams = *pstreams;
854 state.num_streams = *pnum_streams;
855 state.mem_ctx = mem_ctx;
856 state.handle = handle;
857 state.status = NT_STATUS_OK;
859 if (S_ISLNK(sbuf.st_ex_mode)) {
861 * Currently we do't have SMB_VFS_LLISTXATTR
862 * inside the VFS which means there's no way
863 * to cope with a symlink when lp_posix_pathnames().
864 * returns true. For now ignore links.
865 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
867 status = NT_STATUS_OK;
868 } else {
869 status = walk_xattr_streams(handle, fsp, smb_fname,
870 collect_one_stream, &state);
873 if (!NT_STATUS_IS_OK(status)) {
874 TALLOC_FREE(state.streams);
875 return status;
878 if (!NT_STATUS_IS_OK(state.status)) {
879 TALLOC_FREE(state.streams);
880 return state.status;
883 *pnum_streams = state.num_streams;
884 *pstreams = state.streams;
886 return SMB_VFS_NEXT_STREAMINFO(handle,
887 fsp,
888 smb_fname,
889 mem_ctx,
890 pnum_streams,
891 pstreams);
894 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
895 enum timestamp_set_resolution *p_ts_res)
897 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
900 static int streams_xattr_connect(vfs_handle_struct *handle,
901 const char *service, const char *user)
903 struct streams_xattr_config *config;
904 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
905 const char *prefix;
906 int rc;
908 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
909 if (rc != 0) {
910 return rc;
913 config = talloc_zero(handle->conn, struct streams_xattr_config);
914 if (config == NULL) {
915 DEBUG(1, ("talloc_zero() failed\n"));
916 errno = ENOMEM;
917 return -1;
920 prefix = lp_parm_const_string(SNUM(handle->conn),
921 "streams_xattr", "prefix",
922 default_prefix);
923 config->prefix = talloc_strdup(config, prefix);
924 if (config->prefix == NULL) {
925 DEBUG(1, ("talloc_strdup() failed\n"));
926 errno = ENOMEM;
927 return -1;
929 config->prefix_len = strlen(config->prefix);
930 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
932 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
933 "streams_xattr",
934 "store_stream_type",
935 true);
937 SMB_VFS_HANDLE_SET_DATA(handle, config,
938 NULL, struct stream_xattr_config,
939 return -1);
941 return 0;
944 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
945 files_struct *fsp, const void *data,
946 size_t n, off_t offset)
948 struct stream_io *sio =
949 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
950 struct ea_struct ea;
951 NTSTATUS status;
952 int ret;
954 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
956 if (sio == NULL) {
957 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
960 if (!streams_xattr_recheck(sio)) {
961 return -1;
964 status = get_ea_value(talloc_tos(), handle->conn, fsp,
965 sio->base, sio->xattr_name, &ea);
966 if (!NT_STATUS_IS_OK(status)) {
967 return -1;
970 if ((offset + n) > ea.value.length-1) {
971 uint8_t *tmp;
973 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
974 offset + n + 1);
976 if (tmp == NULL) {
977 TALLOC_FREE(ea.value.data);
978 errno = ENOMEM;
979 return -1;
981 ea.value.data = tmp;
982 ea.value.length = offset + n + 1;
983 ea.value.data[offset+n] = 0;
986 memcpy(ea.value.data + offset, data, n);
988 if (fsp->fh->fd != -1) {
989 ret = SMB_VFS_FSETXATTR(fsp,
990 sio->xattr_name,
991 ea.value.data, ea.value.length, 0);
992 } else {
993 ret = SMB_VFS_SETXATTR(fsp->conn,
994 fsp->fsp_name->base_name,
995 sio->xattr_name,
996 ea.value.data, ea.value.length, 0);
998 TALLOC_FREE(ea.value.data);
1000 if (ret == -1) {
1001 return -1;
1004 return n;
1007 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1008 files_struct *fsp, void *data,
1009 size_t n, off_t offset)
1011 struct stream_io *sio =
1012 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1013 struct ea_struct ea;
1014 NTSTATUS status;
1015 size_t length, overlap;
1017 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1018 (int)offset, (int)n));
1020 if (sio == NULL) {
1021 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1024 if (!streams_xattr_recheck(sio)) {
1025 return -1;
1028 status = get_ea_value(talloc_tos(), handle->conn, fsp,
1029 sio->base, sio->xattr_name, &ea);
1030 if (!NT_STATUS_IS_OK(status)) {
1031 return -1;
1034 length = ea.value.length-1;
1036 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1037 (int)length));
1039 /* Attempt to read past EOF. */
1040 if (length <= offset) {
1041 return 0;
1044 overlap = (offset + n) > length ? (length - offset) : n;
1045 memcpy(data, ea.value.data + offset, overlap);
1047 TALLOC_FREE(ea.value.data);
1048 return overlap;
1051 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1052 struct files_struct *fsp,
1053 off_t offset)
1055 int ret;
1056 uint8_t *tmp;
1057 struct ea_struct ea;
1058 NTSTATUS status;
1059 struct stream_io *sio =
1060 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1062 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1063 fsp_str_dbg(fsp), (double)offset));
1065 if (sio == NULL) {
1066 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1069 if (!streams_xattr_recheck(sio)) {
1070 return -1;
1073 status = get_ea_value(talloc_tos(), handle->conn, fsp,
1074 sio->base, sio->xattr_name, &ea);
1075 if (!NT_STATUS_IS_OK(status)) {
1076 return -1;
1079 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1080 offset + 1);
1082 if (tmp == NULL) {
1083 TALLOC_FREE(ea.value.data);
1084 errno = ENOMEM;
1085 return -1;
1088 /* Did we expand ? */
1089 if (ea.value.length < offset + 1) {
1090 memset(&tmp[ea.value.length], '\0',
1091 offset + 1 - ea.value.length);
1094 ea.value.data = tmp;
1095 ea.value.length = offset + 1;
1096 ea.value.data[offset] = 0;
1098 if (fsp->fh->fd != -1) {
1099 ret = SMB_VFS_FSETXATTR(fsp,
1100 sio->xattr_name,
1101 ea.value.data, ea.value.length, 0);
1102 } else {
1103 ret = SMB_VFS_SETXATTR(fsp->conn,
1104 fsp->fsp_name->base_name,
1105 sio->xattr_name,
1106 ea.value.data, ea.value.length, 0);
1109 TALLOC_FREE(ea.value.data);
1111 if (ret == -1) {
1112 return -1;
1115 return 0;
1118 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1119 struct files_struct *fsp,
1120 uint32_t mode,
1121 off_t offset,
1122 off_t len)
1124 struct stream_io *sio =
1125 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1127 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1128 "len = %.0f\n",
1129 fsp_str_dbg(fsp), (double)offset, (double)len));
1131 if (sio == NULL) {
1132 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1135 if (!streams_xattr_recheck(sio)) {
1136 return -1;
1139 /* Let the pwrite code path handle it. */
1140 errno = ENOSYS;
1141 return -1;
1145 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1146 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1147 .connect_fn = streams_xattr_connect,
1148 .open_fn = streams_xattr_open,
1149 .stat_fn = streams_xattr_stat,
1150 .fstat_fn = streams_xattr_fstat,
1151 .lstat_fn = streams_xattr_lstat,
1152 .pread_fn = streams_xattr_pread,
1153 .pwrite_fn = streams_xattr_pwrite,
1154 .unlink_fn = streams_xattr_unlink,
1155 .rename_fn = streams_xattr_rename,
1156 .ftruncate_fn = streams_xattr_ftruncate,
1157 .fallocate_fn = streams_xattr_fallocate,
1158 .streaminfo_fn = streams_xattr_streaminfo,
1161 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *);
1162 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1164 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1165 &vfs_streams_xattr_fns);