ctdb: Accept the key in hex format for the pstore command
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blobd149fc83a7a351f0a46dc2304cf0971e76e8b2ba
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 if (stype) {
118 if (strcasecmp_m(stype, ":$DATA") != 0) {
119 return NT_STATUS_INVALID_PARAMETER;
123 *xattr_name = talloc_asprintf(ctx, "%s%s",
124 config->prefix,
125 stream_name + 1);
126 if (*xattr_name == NULL) {
127 return NT_STATUS_NO_MEMORY;
130 if (stype != NULL) {
131 /* Normalize the stream type to upercase. */
132 if (!strupper_m(strrchr_m(*xattr_name, ':') + 1)) {
133 return NT_STATUS_INVALID_PARAMETER;
135 } else if (config->store_stream_type) {
137 * Append an explicit stream type if one wasn't
138 * specified.
140 *xattr_name = talloc_asprintf(ctx, "%s%s",
141 *xattr_name, ":$DATA");
142 if (*xattr_name == NULL) {
143 return NT_STATUS_NO_MEMORY;
147 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
148 stream_name));
150 return NT_STATUS_OK;
153 static bool streams_xattr_recheck(struct stream_io *sio)
155 NTSTATUS status;
156 char *xattr_name = NULL;
158 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
159 return true;
162 if (sio->fsp->fsp_name->stream_name == NULL) {
163 /* how can this happen */
164 errno = EINVAL;
165 return false;
168 status = streams_xattr_get_name(sio->handle, talloc_tos(),
169 sio->fsp->fsp_name->stream_name,
170 &xattr_name);
171 if (!NT_STATUS_IS_OK(status)) {
172 return false;
175 TALLOC_FREE(sio->xattr_name);
176 TALLOC_FREE(sio->base);
177 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
178 xattr_name);
179 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
180 sio->fsp->fsp_name->base_name);
181 sio->fsp_name_ptr = sio->fsp->fsp_name;
183 TALLOC_FREE(xattr_name);
185 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
186 return false;
189 return true;
193 * Helper to stat/lstat the base file of an smb_fname.
195 static int streams_xattr_stat_base(vfs_handle_struct *handle,
196 struct smb_filename *smb_fname,
197 bool follow_links)
199 char *tmp_stream_name;
200 int result;
202 tmp_stream_name = smb_fname->stream_name;
203 smb_fname->stream_name = NULL;
204 if (follow_links) {
205 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
206 } else {
207 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
209 smb_fname->stream_name = tmp_stream_name;
210 return result;
213 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
214 SMB_STRUCT_STAT *sbuf)
216 struct smb_filename *smb_fname_base = NULL;
217 int ret = -1;
218 struct stream_io *io = (struct stream_io *)
219 VFS_FETCH_FSP_EXTENSION(handle, fsp);
221 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
223 if (io == NULL || fsp->base_fsp == NULL) {
224 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
227 if (!streams_xattr_recheck(io)) {
228 return -1;
231 /* Create an smb_filename with stream_name == NULL. */
232 smb_fname_base = synthetic_smb_fname(talloc_tos(), io->base,
233 NULL, NULL);
234 if (smb_fname_base == NULL) {
235 errno = ENOMEM;
236 return -1;
239 if (lp_posix_pathnames()) {
240 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
241 } else {
242 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
244 *sbuf = smb_fname_base->st;
245 TALLOC_FREE(smb_fname_base);
247 if (ret == -1) {
248 return -1;
251 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
252 io->base, io->xattr_name);
253 if (sbuf->st_ex_size == -1) {
254 return -1;
257 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
259 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
260 sbuf->st_ex_mode &= ~S_IFMT;
261 sbuf->st_ex_mode |= S_IFREG;
262 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
264 return 0;
267 static int streams_xattr_stat(vfs_handle_struct *handle,
268 struct smb_filename *smb_fname)
270 NTSTATUS status;
271 int result = -1;
272 char *xattr_name = NULL;
274 if (!is_ntfs_stream_smb_fname(smb_fname)) {
275 return SMB_VFS_NEXT_STAT(handle, smb_fname);
278 /* Note if lp_posix_paths() is true, we can never
279 * get here as is_ntfs_stream_smb_fname() is
280 * always false. So we never need worry about
281 * not following links here. */
283 /* If the default stream is requested, just stat the base file. */
284 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
285 return streams_xattr_stat_base(handle, smb_fname, true);
288 /* Populate the stat struct with info from the base file. */
289 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
290 return -1;
293 /* Derive the xattr name to lookup. */
294 status = streams_xattr_get_name(handle, talloc_tos(),
295 smb_fname->stream_name, &xattr_name);
296 if (!NT_STATUS_IS_OK(status)) {
297 errno = map_errno_from_nt_status(status);
298 return -1;
301 /* Augment the base file's stat information before returning. */
302 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
303 smb_fname->base_name,
304 xattr_name);
305 if (smb_fname->st.st_ex_size == -1) {
306 errno = ENOENT;
307 result = -1;
308 goto fail;
311 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
312 smb_fname->st.st_ex_mode &= ~S_IFMT;
313 smb_fname->st.st_ex_mode |= S_IFREG;
314 smb_fname->st.st_ex_blocks =
315 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
317 result = 0;
318 fail:
319 TALLOC_FREE(xattr_name);
320 return result;
323 static int streams_xattr_lstat(vfs_handle_struct *handle,
324 struct smb_filename *smb_fname)
326 NTSTATUS status;
327 int result = -1;
328 char *xattr_name = NULL;
330 if (!is_ntfs_stream_smb_fname(smb_fname)) {
331 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
334 /* If the default stream is requested, just stat the base file. */
335 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
336 return streams_xattr_stat_base(handle, smb_fname, false);
339 /* Populate the stat struct with info from the base file. */
340 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
341 return -1;
344 /* Derive the xattr name to lookup. */
345 status = streams_xattr_get_name(handle, talloc_tos(),
346 smb_fname->stream_name, &xattr_name);
347 if (!NT_STATUS_IS_OK(status)) {
348 errno = map_errno_from_nt_status(status);
349 return -1;
352 /* Augment the base file's stat information before returning. */
353 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
354 smb_fname->base_name,
355 xattr_name);
356 if (smb_fname->st.st_ex_size == -1) {
357 errno = ENOENT;
358 result = -1;
359 goto fail;
362 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
363 smb_fname->st.st_ex_mode &= ~S_IFMT;
364 smb_fname->st.st_ex_mode |= S_IFREG;
365 smb_fname->st.st_ex_blocks =
366 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
368 result = 0;
370 fail:
371 TALLOC_FREE(xattr_name);
372 return result;
375 static int streams_xattr_open(vfs_handle_struct *handle,
376 struct smb_filename *smb_fname,
377 files_struct *fsp, int flags, mode_t mode)
379 NTSTATUS status;
380 struct smb_filename *smb_fname_base = NULL;
381 struct stream_io *sio;
382 struct ea_struct ea;
383 char *xattr_name = NULL;
384 int baseflags;
385 int hostfd = -1;
387 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
388 smb_fname_str_dbg(smb_fname), flags));
390 if (!is_ntfs_stream_smb_fname(smb_fname)) {
391 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
394 /* If the default stream is requested, just open the base file. */
395 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
396 char *tmp_stream_name;
397 int ret;
399 tmp_stream_name = smb_fname->stream_name;
400 smb_fname->stream_name = NULL;
402 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
404 smb_fname->stream_name = tmp_stream_name;
406 return ret;
409 status = streams_xattr_get_name(handle, talloc_tos(),
410 smb_fname->stream_name, &xattr_name);
411 if (!NT_STATUS_IS_OK(status)) {
412 errno = map_errno_from_nt_status(status);
413 goto fail;
416 /* Create an smb_filename with stream_name == NULL. */
417 smb_fname_base = synthetic_smb_fname(
418 talloc_tos(), smb_fname->base_name, NULL, NULL);
419 if (smb_fname_base == NULL) {
420 errno = ENOMEM;
421 goto fail;
425 * We use baseflags to turn off nasty side-effects when opening the
426 * underlying file.
428 baseflags = flags;
429 baseflags &= ~O_TRUNC;
430 baseflags &= ~O_EXCL;
431 baseflags &= ~O_CREAT;
433 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
434 baseflags, mode);
436 TALLOC_FREE(smb_fname_base);
438 /* It is legit to open a stream on a directory, but the base
439 * fd has to be read-only.
441 if ((hostfd == -1) && (errno == EISDIR)) {
442 baseflags &= ~O_ACCMODE;
443 baseflags |= O_RDONLY;
444 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
445 mode);
448 if (hostfd == -1) {
449 goto fail;
452 status = get_ea_value(talloc_tos(), handle->conn, NULL,
453 smb_fname->base_name, xattr_name, &ea);
455 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
457 if (!NT_STATUS_IS_OK(status)
458 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
460 * The base file is not there. This is an error even if we got
461 * O_CREAT, the higher levels should have created the base
462 * file for us.
464 DEBUG(10, ("streams_xattr_open: base file %s not around, "
465 "returning ENOENT\n", smb_fname->base_name));
466 errno = ENOENT;
467 goto fail;
470 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
471 (flags & O_TRUNC)) {
473 * The attribute does not exist or needs to be truncated
477 * Darn, xattrs need at least 1 byte
479 char null = '\0';
481 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
482 xattr_name, smb_fname->base_name));
484 if (fsp->base_fsp->fh->fd != -1) {
485 if (SMB_VFS_FSETXATTR(
486 fsp->base_fsp, xattr_name,
487 &null, sizeof(null),
488 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
489 goto fail;
491 } else {
492 if (SMB_VFS_SETXATTR(
493 handle->conn, smb_fname->base_name,
494 xattr_name, &null, sizeof(null),
495 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
496 goto fail;
501 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
502 struct stream_io,
503 NULL);
504 if (sio == NULL) {
505 errno = ENOMEM;
506 goto fail;
509 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
510 xattr_name);
511 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
512 smb_fname->base_name);
513 sio->fsp_name_ptr = fsp->fsp_name;
514 sio->handle = handle;
515 sio->fsp = fsp;
517 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
518 errno = ENOMEM;
519 goto fail;
522 return hostfd;
524 fail:
525 if (hostfd >= 0) {
527 * BUGBUGBUG -- we would need to call fd_close_posix here, but
528 * we don't have a full fsp yet
530 fsp->fh->fd = hostfd;
531 SMB_VFS_CLOSE(fsp);
534 return -1;
537 static int streams_xattr_unlink(vfs_handle_struct *handle,
538 const struct smb_filename *smb_fname)
540 NTSTATUS status;
541 int ret = -1;
542 char *xattr_name = NULL;
544 if (!is_ntfs_stream_smb_fname(smb_fname)) {
545 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
548 /* If the default stream is requested, just open the base file. */
549 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
550 struct smb_filename *smb_fname_base = NULL;
552 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
553 if (smb_fname_base == NULL) {
554 errno = ENOMEM;
555 return -1;
558 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
560 TALLOC_FREE(smb_fname_base);
561 return ret;
564 status = streams_xattr_get_name(handle, talloc_tos(),
565 smb_fname->stream_name, &xattr_name);
566 if (!NT_STATUS_IS_OK(status)) {
567 errno = map_errno_from_nt_status(status);
568 goto fail;
571 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
573 if ((ret == -1) && (errno == ENOATTR)) {
574 errno = ENOENT;
575 goto fail;
578 ret = 0;
580 fail:
581 TALLOC_FREE(xattr_name);
582 return ret;
585 static int streams_xattr_rename(vfs_handle_struct *handle,
586 const struct smb_filename *smb_fname_src,
587 const struct smb_filename *smb_fname_dst)
589 NTSTATUS status;
590 int ret = -1;
591 char *src_xattr_name = NULL;
592 char *dst_xattr_name = NULL;
593 bool src_is_stream, dst_is_stream;
594 ssize_t oret;
595 ssize_t nret;
596 struct ea_struct ea;
598 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
599 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
601 if (!src_is_stream && !dst_is_stream) {
602 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
603 smb_fname_dst);
606 /* For now don't allow renames from or to the default stream. */
607 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
608 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
609 errno = ENOSYS;
610 goto done;
613 /* Don't rename if the streams are identical. */
614 if (strcasecmp_m(smb_fname_src->stream_name,
615 smb_fname_dst->stream_name) == 0) {
616 goto done;
619 /* Get the xattr names. */
620 status = streams_xattr_get_name(handle, talloc_tos(),
621 smb_fname_src->stream_name,
622 &src_xattr_name);
623 if (!NT_STATUS_IS_OK(status)) {
624 errno = map_errno_from_nt_status(status);
625 goto fail;
627 status = streams_xattr_get_name(handle, talloc_tos(),
628 smb_fname_dst->stream_name,
629 &dst_xattr_name);
630 if (!NT_STATUS_IS_OK(status)) {
631 errno = map_errno_from_nt_status(status);
632 goto fail;
635 /* read the old stream */
636 status = get_ea_value(talloc_tos(), handle->conn, NULL,
637 smb_fname_src->base_name, src_xattr_name, &ea);
638 if (!NT_STATUS_IS_OK(status)) {
639 errno = ENOENT;
640 goto fail;
643 /* (over)write the new stream */
644 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
645 dst_xattr_name, ea.value.data, ea.value.length,
647 if (nret < 0) {
648 if (errno == ENOATTR) {
649 errno = ENOENT;
651 goto fail;
654 /* remove the old stream */
655 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
656 src_xattr_name);
657 if (oret < 0) {
658 if (errno == ENOATTR) {
659 errno = ENOENT;
661 goto fail;
664 done:
665 errno = 0;
666 ret = 0;
667 fail:
668 TALLOC_FREE(src_xattr_name);
669 TALLOC_FREE(dst_xattr_name);
670 return ret;
673 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle, files_struct *fsp,
674 const char *fname,
675 bool (*fn)(struct ea_struct *ea,
676 void *private_data),
677 void *private_data)
679 NTSTATUS status;
680 char **names;
681 size_t i, num_names;
682 struct streams_xattr_config *config;
684 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
685 return NT_STATUS_UNSUCCESSFUL);
687 status = get_ea_names_from_file(talloc_tos(), handle->conn, fsp, fname,
688 &names, &num_names);
689 if (!NT_STATUS_IS_OK(status)) {
690 return status;
693 for (i=0; i<num_names; i++) {
694 struct ea_struct ea;
697 * We want to check with samba_private_attr_name()
698 * whether the xattr name is a private one,
699 * unfortunately it flags xattrs that begin with the
700 * default streams prefix as private.
702 * By only calling samba_private_attr_name() in case
703 * the xattr does NOT begin with the default prefix,
704 * we know that if it returns 'true' it definitely one
705 * of our internal xattr like "user.DOSATTRIB".
707 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
708 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
709 if (samba_private_attr_name(names[i])) {
710 continue;
714 if (strncmp(names[i], config->prefix,
715 config->prefix_len) != 0) {
716 continue;
719 status = get_ea_value(names, handle->conn, fsp, fname,
720 names[i], &ea);
721 if (!NT_STATUS_IS_OK(status)) {
722 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
723 names[i], fname, nt_errstr(status)));
724 continue;
727 ea.name = talloc_asprintf(
728 ea.value.data, ":%s%s",
729 names[i] + config->prefix_len,
730 config->store_stream_type ? "" : ":$DATA");
731 if (ea.name == NULL) {
732 DEBUG(0, ("talloc failed\n"));
733 continue;
736 if (!fn(&ea, private_data)) {
737 TALLOC_FREE(ea.value.data);
738 return NT_STATUS_OK;
741 TALLOC_FREE(ea.value.data);
744 TALLOC_FREE(names);
745 return NT_STATUS_OK;
748 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
749 struct stream_struct **streams,
750 const char *name, off_t size,
751 off_t alloc_size)
753 struct stream_struct *tmp;
755 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
756 (*num_streams)+1);
757 if (tmp == NULL) {
758 return false;
761 tmp[*num_streams].name = talloc_strdup(tmp, name);
762 if (tmp[*num_streams].name == NULL) {
763 return false;
766 tmp[*num_streams].size = size;
767 tmp[*num_streams].alloc_size = alloc_size;
769 *streams = tmp;
770 *num_streams += 1;
771 return true;
774 struct streaminfo_state {
775 TALLOC_CTX *mem_ctx;
776 vfs_handle_struct *handle;
777 unsigned int num_streams;
778 struct stream_struct *streams;
779 NTSTATUS status;
782 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
784 struct streaminfo_state *state =
785 (struct streaminfo_state *)private_data;
787 if (!add_one_stream(state->mem_ctx,
788 &state->num_streams, &state->streams,
789 ea->name, ea->value.length-1,
790 smb_roundup(state->handle->conn,
791 ea->value.length-1))) {
792 state->status = NT_STATUS_NO_MEMORY;
793 return false;
796 return true;
799 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
800 struct files_struct *fsp,
801 const char *fname,
802 TALLOC_CTX *mem_ctx,
803 unsigned int *pnum_streams,
804 struct stream_struct **pstreams)
806 SMB_STRUCT_STAT sbuf;
807 int ret;
808 NTSTATUS status;
809 struct streaminfo_state state;
811 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
812 ret = SMB_VFS_FSTAT(fsp, &sbuf);
814 else {
815 struct smb_filename *smb_fname = NULL;
816 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL,
817 NULL);
818 if (smb_fname == NULL) {
819 return NT_STATUS_NO_MEMORY;
821 if (lp_posix_pathnames()) {
822 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
823 } else {
824 ret = SMB_VFS_STAT(handle->conn, smb_fname);
826 sbuf = smb_fname->st;
827 TALLOC_FREE(smb_fname);
830 if (ret == -1) {
831 return map_nt_error_from_unix(errno);
834 state.streams = *pstreams;
835 state.num_streams = *pnum_streams;
836 state.mem_ctx = mem_ctx;
837 state.handle = handle;
838 state.status = NT_STATUS_OK;
840 if (S_ISLNK(sbuf.st_ex_mode)) {
842 * Currently we do't have SMB_VFS_LLISTXATTR
843 * inside the VFS which means there's no way
844 * to cope with a symlink when lp_posix_pathnames().
845 * returns true. For now ignore links.
846 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
848 status = NT_STATUS_OK;
849 } else {
850 status = walk_xattr_streams(handle, fsp, fname,
851 collect_one_stream, &state);
854 if (!NT_STATUS_IS_OK(status)) {
855 TALLOC_FREE(state.streams);
856 return status;
859 if (!NT_STATUS_IS_OK(state.status)) {
860 TALLOC_FREE(state.streams);
861 return state.status;
864 *pnum_streams = state.num_streams;
865 *pstreams = state.streams;
867 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
870 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
871 enum timestamp_set_resolution *p_ts_res)
873 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
876 static int streams_xattr_connect(vfs_handle_struct *handle,
877 const char *service, const char *user)
879 struct streams_xattr_config *config;
880 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
881 const char *prefix;
882 int rc;
884 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
885 if (rc != 0) {
886 return rc;
889 config = talloc_zero(handle->conn, struct streams_xattr_config);
890 if (config == NULL) {
891 DEBUG(1, ("talloc_zero() failed\n"));
892 errno = ENOMEM;
893 return -1;
896 prefix = lp_parm_const_string(SNUM(handle->conn),
897 "streams_xattr", "prefix",
898 default_prefix);
899 config->prefix = talloc_strdup(config, prefix);
900 if (config->prefix == NULL) {
901 DEBUG(1, ("talloc_strdup() failed\n"));
902 errno = ENOMEM;
903 return -1;
905 config->prefix_len = strlen(config->prefix);
906 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
908 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
909 "streams_xattr",
910 "store_stream_type",
911 true);
913 SMB_VFS_HANDLE_SET_DATA(handle, config,
914 NULL, struct stream_xattr_config,
915 return -1);
917 return 0;
920 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
921 files_struct *fsp, const void *data,
922 size_t n, off_t offset)
924 struct stream_io *sio =
925 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
926 struct ea_struct ea;
927 NTSTATUS status;
928 int ret;
930 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
932 if (sio == NULL) {
933 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
936 if (!streams_xattr_recheck(sio)) {
937 return -1;
940 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
941 sio->base, sio->xattr_name, &ea);
942 if (!NT_STATUS_IS_OK(status)) {
943 return -1;
946 if ((offset + n) > ea.value.length-1) {
947 uint8_t *tmp;
949 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
950 offset + n + 1);
952 if (tmp == NULL) {
953 TALLOC_FREE(ea.value.data);
954 errno = ENOMEM;
955 return -1;
957 ea.value.data = tmp;
958 ea.value.length = offset + n + 1;
959 ea.value.data[offset+n] = 0;
962 memcpy(ea.value.data + offset, data, n);
964 if (fsp->base_fsp->fh->fd != -1) {
965 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
966 sio->xattr_name,
967 ea.value.data, ea.value.length, 0);
968 } else {
969 ret = SMB_VFS_SETXATTR(fsp->conn,
970 fsp->base_fsp->fsp_name->base_name,
971 sio->xattr_name,
972 ea.value.data, ea.value.length, 0);
974 TALLOC_FREE(ea.value.data);
976 if (ret == -1) {
977 return -1;
980 return n;
983 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
984 files_struct *fsp, void *data,
985 size_t n, off_t offset)
987 struct stream_io *sio =
988 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
989 struct ea_struct ea;
990 NTSTATUS status;
991 size_t length, overlap;
993 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
994 (int)offset, (int)n));
996 if (sio == NULL) {
997 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1000 if (!streams_xattr_recheck(sio)) {
1001 return -1;
1004 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1005 sio->base, sio->xattr_name, &ea);
1006 if (!NT_STATUS_IS_OK(status)) {
1007 return -1;
1010 length = ea.value.length-1;
1012 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1013 (int)length));
1015 /* Attempt to read past EOF. */
1016 if (length <= offset) {
1017 return 0;
1020 overlap = (offset + n) > length ? (length - offset) : n;
1021 memcpy(data, ea.value.data + offset, overlap);
1023 TALLOC_FREE(ea.value.data);
1024 return overlap;
1027 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1028 struct files_struct *fsp,
1029 off_t offset)
1031 int ret;
1032 uint8_t *tmp;
1033 struct ea_struct ea;
1034 NTSTATUS status;
1035 struct stream_io *sio =
1036 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1038 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1039 fsp_str_dbg(fsp), (double)offset));
1041 if (sio == NULL) {
1042 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1045 if (!streams_xattr_recheck(sio)) {
1046 return -1;
1049 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1050 sio->base, sio->xattr_name, &ea);
1051 if (!NT_STATUS_IS_OK(status)) {
1052 return -1;
1055 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1056 offset + 1);
1058 if (tmp == NULL) {
1059 TALLOC_FREE(ea.value.data);
1060 errno = ENOMEM;
1061 return -1;
1064 /* Did we expand ? */
1065 if (ea.value.length < offset + 1) {
1066 memset(&tmp[ea.value.length], '\0',
1067 offset + 1 - ea.value.length);
1070 ea.value.data = tmp;
1071 ea.value.length = offset + 1;
1072 ea.value.data[offset] = 0;
1074 if (fsp->base_fsp->fh->fd != -1) {
1075 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1076 sio->xattr_name,
1077 ea.value.data, ea.value.length, 0);
1078 } else {
1079 ret = SMB_VFS_SETXATTR(fsp->conn,
1080 fsp->base_fsp->fsp_name->base_name,
1081 sio->xattr_name,
1082 ea.value.data, ea.value.length, 0);
1085 TALLOC_FREE(ea.value.data);
1087 if (ret == -1) {
1088 return -1;
1091 return 0;
1094 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1095 struct files_struct *fsp,
1096 uint32_t mode,
1097 off_t offset,
1098 off_t len)
1100 struct stream_io *sio =
1101 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1103 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1104 "len = %.0f\n",
1105 fsp_str_dbg(fsp), (double)offset, (double)len));
1107 if (sio == NULL) {
1108 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1111 if (!streams_xattr_recheck(sio)) {
1112 return -1;
1115 /* Let the pwrite code path handle it. */
1116 errno = ENOSYS;
1117 return -1;
1121 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1122 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1123 .connect_fn = streams_xattr_connect,
1124 .open_fn = streams_xattr_open,
1125 .stat_fn = streams_xattr_stat,
1126 .fstat_fn = streams_xattr_fstat,
1127 .lstat_fn = streams_xattr_lstat,
1128 .pread_fn = streams_xattr_pread,
1129 .pwrite_fn = streams_xattr_pwrite,
1130 .unlink_fn = streams_xattr_unlink,
1131 .rename_fn = streams_xattr_rename,
1132 .ftruncate_fn = streams_xattr_ftruncate,
1133 .fallocate_fn = streams_xattr_fallocate,
1134 .streaminfo_fn = streams_xattr_streaminfo,
1137 NTSTATUS vfs_streams_xattr_init(void);
1138 NTSTATUS vfs_streams_xattr_init(void)
1140 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1141 &vfs_streams_xattr_fns);