nfs4acls: Use talloc_realloc()
[Samba.git] / source3 / modules / vfs_streams_xattr.c
blob92bd1c9bce75979dc2715cde21da1533a1f3fd56
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);
116 * With vfs_fruit option "fruit:encoding = native" we're
117 * already converting stream names that contain illegal NTFS
118 * characters from their on-the-wire Unicode Private Range
119 * encoding to their native ASCII representation.
121 * As as result the name of xattrs storing the streams (via
122 * vfs_streams_xattr) may contain a colon, so we have to use
123 * strrchr_m() instead of strchr_m() for matching the stream
124 * type suffix.
126 * In check_path_syntax() we've already ensured the streamname
127 * we got from the client is valid.
129 stype = strrchr_m(stream_name + 1, ':');
131 if (stype) {
132 if (strcasecmp_m(stype, ":$DATA") != 0) {
133 return NT_STATUS_INVALID_PARAMETER;
137 *xattr_name = talloc_asprintf(ctx, "%s%s",
138 config->prefix,
139 stream_name + 1);
140 if (*xattr_name == NULL) {
141 return NT_STATUS_NO_MEMORY;
144 if (stype != NULL) {
145 /* Normalize the stream type to upercase. */
146 if (!strupper_m(strrchr_m(*xattr_name, ':') + 1)) {
147 return NT_STATUS_INVALID_PARAMETER;
149 } else if (config->store_stream_type) {
151 * Append an explicit stream type if one wasn't
152 * specified.
154 *xattr_name = talloc_asprintf(ctx, "%s%s",
155 *xattr_name, ":$DATA");
156 if (*xattr_name == NULL) {
157 return NT_STATUS_NO_MEMORY;
161 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
162 stream_name));
164 return NT_STATUS_OK;
167 static bool streams_xattr_recheck(struct stream_io *sio)
169 NTSTATUS status;
170 char *xattr_name = NULL;
172 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
173 return true;
176 if (sio->fsp->fsp_name->stream_name == NULL) {
177 /* how can this happen */
178 errno = EINVAL;
179 return false;
182 status = streams_xattr_get_name(sio->handle, talloc_tos(),
183 sio->fsp->fsp_name->stream_name,
184 &xattr_name);
185 if (!NT_STATUS_IS_OK(status)) {
186 return false;
189 TALLOC_FREE(sio->xattr_name);
190 TALLOC_FREE(sio->base);
191 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
192 xattr_name);
193 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
194 sio->fsp->fsp_name->base_name);
195 sio->fsp_name_ptr = sio->fsp->fsp_name;
197 TALLOC_FREE(xattr_name);
199 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
200 return false;
203 return true;
207 * Helper to stat/lstat the base file of an smb_fname.
209 static int streams_xattr_stat_base(vfs_handle_struct *handle,
210 struct smb_filename *smb_fname,
211 bool follow_links)
213 char *tmp_stream_name;
214 int result;
216 tmp_stream_name = smb_fname->stream_name;
217 smb_fname->stream_name = NULL;
218 if (follow_links) {
219 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
220 } else {
221 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
223 smb_fname->stream_name = tmp_stream_name;
224 return result;
227 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
228 SMB_STRUCT_STAT *sbuf)
230 struct smb_filename *smb_fname_base = NULL;
231 int ret = -1;
232 struct stream_io *io = (struct stream_io *)
233 VFS_FETCH_FSP_EXTENSION(handle, fsp);
235 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
237 if (io == NULL || fsp->base_fsp == NULL) {
238 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
241 if (!streams_xattr_recheck(io)) {
242 return -1;
245 /* Create an smb_filename with stream_name == NULL. */
246 smb_fname_base = synthetic_smb_fname(talloc_tos(), io->base,
247 NULL, NULL);
248 if (smb_fname_base == NULL) {
249 errno = ENOMEM;
250 return -1;
253 if (lp_posix_pathnames()) {
254 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
255 } else {
256 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
258 *sbuf = smb_fname_base->st;
259 TALLOC_FREE(smb_fname_base);
261 if (ret == -1) {
262 return -1;
265 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
266 io->base, io->xattr_name);
267 if (sbuf->st_ex_size == -1) {
268 return -1;
271 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
273 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
274 sbuf->st_ex_mode &= ~S_IFMT;
275 sbuf->st_ex_mode |= S_IFREG;
276 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
278 return 0;
281 static int streams_xattr_stat(vfs_handle_struct *handle,
282 struct smb_filename *smb_fname)
284 NTSTATUS status;
285 int result = -1;
286 char *xattr_name = NULL;
288 if (!is_ntfs_stream_smb_fname(smb_fname)) {
289 return SMB_VFS_NEXT_STAT(handle, smb_fname);
292 /* Note if lp_posix_paths() is true, we can never
293 * get here as is_ntfs_stream_smb_fname() is
294 * always false. So we never need worry about
295 * not following links here. */
297 /* If the default stream is requested, just stat the base file. */
298 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
299 return streams_xattr_stat_base(handle, smb_fname, true);
302 /* Populate the stat struct with info from the base file. */
303 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
304 return -1;
307 /* Derive the xattr name to lookup. */
308 status = streams_xattr_get_name(handle, talloc_tos(),
309 smb_fname->stream_name, &xattr_name);
310 if (!NT_STATUS_IS_OK(status)) {
311 errno = map_errno_from_nt_status(status);
312 return -1;
315 /* Augment the base file's stat information before returning. */
316 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
317 smb_fname->base_name,
318 xattr_name);
319 if (smb_fname->st.st_ex_size == -1) {
320 errno = ENOENT;
321 result = -1;
322 goto fail;
325 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
326 smb_fname->st.st_ex_mode &= ~S_IFMT;
327 smb_fname->st.st_ex_mode |= S_IFREG;
328 smb_fname->st.st_ex_blocks =
329 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
331 result = 0;
332 fail:
333 TALLOC_FREE(xattr_name);
334 return result;
337 static int streams_xattr_lstat(vfs_handle_struct *handle,
338 struct smb_filename *smb_fname)
340 NTSTATUS status;
341 int result = -1;
342 char *xattr_name = NULL;
344 if (!is_ntfs_stream_smb_fname(smb_fname)) {
345 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
348 /* If the default stream is requested, just stat the base file. */
349 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
350 return streams_xattr_stat_base(handle, smb_fname, false);
353 /* Populate the stat struct with info from the base file. */
354 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
355 return -1;
358 /* Derive the xattr name to lookup. */
359 status = streams_xattr_get_name(handle, talloc_tos(),
360 smb_fname->stream_name, &xattr_name);
361 if (!NT_STATUS_IS_OK(status)) {
362 errno = map_errno_from_nt_status(status);
363 return -1;
366 /* Augment the base file's stat information before returning. */
367 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
368 smb_fname->base_name,
369 xattr_name);
370 if (smb_fname->st.st_ex_size == -1) {
371 errno = ENOENT;
372 result = -1;
373 goto fail;
376 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
377 smb_fname->st.st_ex_mode &= ~S_IFMT;
378 smb_fname->st.st_ex_mode |= S_IFREG;
379 smb_fname->st.st_ex_blocks =
380 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
382 result = 0;
384 fail:
385 TALLOC_FREE(xattr_name);
386 return result;
389 static int streams_xattr_open(vfs_handle_struct *handle,
390 struct smb_filename *smb_fname,
391 files_struct *fsp, int flags, mode_t mode)
393 NTSTATUS status;
394 struct smb_filename *smb_fname_base = NULL;
395 struct stream_io *sio;
396 struct ea_struct ea;
397 char *xattr_name = NULL;
398 int baseflags;
399 int hostfd = -1;
401 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
402 smb_fname_str_dbg(smb_fname), flags));
404 if (!is_ntfs_stream_smb_fname(smb_fname)) {
405 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
408 /* If the default stream is requested, just open the base file. */
409 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
410 char *tmp_stream_name;
411 int ret;
413 tmp_stream_name = smb_fname->stream_name;
414 smb_fname->stream_name = NULL;
416 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
418 smb_fname->stream_name = tmp_stream_name;
420 return ret;
423 status = streams_xattr_get_name(handle, talloc_tos(),
424 smb_fname->stream_name, &xattr_name);
425 if (!NT_STATUS_IS_OK(status)) {
426 errno = map_errno_from_nt_status(status);
427 goto fail;
430 /* Create an smb_filename with stream_name == NULL. */
431 smb_fname_base = synthetic_smb_fname(
432 talloc_tos(), smb_fname->base_name, NULL, NULL);
433 if (smb_fname_base == NULL) {
434 errno = ENOMEM;
435 goto fail;
439 * We use baseflags to turn off nasty side-effects when opening the
440 * underlying file.
442 baseflags = flags;
443 baseflags &= ~O_TRUNC;
444 baseflags &= ~O_EXCL;
445 baseflags &= ~O_CREAT;
447 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
448 baseflags, mode);
450 TALLOC_FREE(smb_fname_base);
452 /* It is legit to open a stream on a directory, but the base
453 * fd has to be read-only.
455 if ((hostfd == -1) && (errno == EISDIR)) {
456 baseflags &= ~O_ACCMODE;
457 baseflags |= O_RDONLY;
458 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
459 mode);
462 if (hostfd == -1) {
463 goto fail;
466 status = get_ea_value(talloc_tos(), handle->conn, NULL,
467 smb_fname->base_name, xattr_name, &ea);
469 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
471 if (!NT_STATUS_IS_OK(status)
472 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
474 * The base file is not there. This is an error even if we got
475 * O_CREAT, the higher levels should have created the base
476 * file for us.
478 DEBUG(10, ("streams_xattr_open: base file %s not around, "
479 "returning ENOENT\n", smb_fname->base_name));
480 errno = ENOENT;
481 goto fail;
484 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
485 (flags & O_TRUNC)) {
487 * The attribute does not exist or needs to be truncated
491 * Darn, xattrs need at least 1 byte
493 char null = '\0';
495 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
496 xattr_name, smb_fname->base_name));
498 if (fsp->base_fsp->fh->fd != -1) {
499 if (SMB_VFS_FSETXATTR(
500 fsp->base_fsp, xattr_name,
501 &null, sizeof(null),
502 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
503 goto fail;
505 } else {
506 if (SMB_VFS_SETXATTR(
507 handle->conn, smb_fname->base_name,
508 xattr_name, &null, sizeof(null),
509 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
510 goto fail;
515 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
516 struct stream_io,
517 NULL);
518 if (sio == NULL) {
519 errno = ENOMEM;
520 goto fail;
523 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
524 xattr_name);
525 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
526 smb_fname->base_name);
527 sio->fsp_name_ptr = fsp->fsp_name;
528 sio->handle = handle;
529 sio->fsp = fsp;
531 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
532 errno = ENOMEM;
533 goto fail;
536 return hostfd;
538 fail:
539 if (hostfd >= 0) {
541 * BUGBUGBUG -- we would need to call fd_close_posix here, but
542 * we don't have a full fsp yet
544 fsp->fh->fd = hostfd;
545 SMB_VFS_CLOSE(fsp);
548 return -1;
551 static int streams_xattr_unlink(vfs_handle_struct *handle,
552 const struct smb_filename *smb_fname)
554 NTSTATUS status;
555 int ret = -1;
556 char *xattr_name = NULL;
558 if (!is_ntfs_stream_smb_fname(smb_fname)) {
559 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
562 /* If the default stream is requested, just open the base file. */
563 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
564 struct smb_filename *smb_fname_base = NULL;
566 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
567 if (smb_fname_base == NULL) {
568 errno = ENOMEM;
569 return -1;
572 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
574 TALLOC_FREE(smb_fname_base);
575 return ret;
578 status = streams_xattr_get_name(handle, talloc_tos(),
579 smb_fname->stream_name, &xattr_name);
580 if (!NT_STATUS_IS_OK(status)) {
581 errno = map_errno_from_nt_status(status);
582 goto fail;
585 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
587 if ((ret == -1) && (errno == ENOATTR)) {
588 errno = ENOENT;
589 goto fail;
592 ret = 0;
594 fail:
595 TALLOC_FREE(xattr_name);
596 return ret;
599 static int streams_xattr_rename(vfs_handle_struct *handle,
600 const struct smb_filename *smb_fname_src,
601 const struct smb_filename *smb_fname_dst)
603 NTSTATUS status;
604 int ret = -1;
605 char *src_xattr_name = NULL;
606 char *dst_xattr_name = NULL;
607 bool src_is_stream, dst_is_stream;
608 ssize_t oret;
609 ssize_t nret;
610 struct ea_struct ea;
612 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
613 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
615 if (!src_is_stream && !dst_is_stream) {
616 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
617 smb_fname_dst);
620 /* For now don't allow renames from or to the default stream. */
621 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
622 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
623 errno = ENOSYS;
624 goto done;
627 /* Don't rename if the streams are identical. */
628 if (strcasecmp_m(smb_fname_src->stream_name,
629 smb_fname_dst->stream_name) == 0) {
630 goto done;
633 /* Get the xattr names. */
634 status = streams_xattr_get_name(handle, talloc_tos(),
635 smb_fname_src->stream_name,
636 &src_xattr_name);
637 if (!NT_STATUS_IS_OK(status)) {
638 errno = map_errno_from_nt_status(status);
639 goto fail;
641 status = streams_xattr_get_name(handle, talloc_tos(),
642 smb_fname_dst->stream_name,
643 &dst_xattr_name);
644 if (!NT_STATUS_IS_OK(status)) {
645 errno = map_errno_from_nt_status(status);
646 goto fail;
649 /* read the old stream */
650 status = get_ea_value(talloc_tos(), handle->conn, NULL,
651 smb_fname_src->base_name, src_xattr_name, &ea);
652 if (!NT_STATUS_IS_OK(status)) {
653 errno = ENOENT;
654 goto fail;
657 /* (over)write the new stream */
658 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
659 dst_xattr_name, ea.value.data, ea.value.length,
661 if (nret < 0) {
662 if (errno == ENOATTR) {
663 errno = ENOENT;
665 goto fail;
668 /* remove the old stream */
669 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
670 src_xattr_name);
671 if (oret < 0) {
672 if (errno == ENOATTR) {
673 errno = ENOENT;
675 goto fail;
678 done:
679 errno = 0;
680 ret = 0;
681 fail:
682 TALLOC_FREE(src_xattr_name);
683 TALLOC_FREE(dst_xattr_name);
684 return ret;
687 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle, files_struct *fsp,
688 const char *fname,
689 bool (*fn)(struct ea_struct *ea,
690 void *private_data),
691 void *private_data)
693 NTSTATUS status;
694 char **names;
695 size_t i, num_names;
696 struct streams_xattr_config *config;
698 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
699 return NT_STATUS_UNSUCCESSFUL);
701 status = get_ea_names_from_file(talloc_tos(), handle->conn, fsp, fname,
702 &names, &num_names);
703 if (!NT_STATUS_IS_OK(status)) {
704 return status;
707 for (i=0; i<num_names; i++) {
708 struct ea_struct ea;
711 * We want to check with samba_private_attr_name()
712 * whether the xattr name is a private one,
713 * unfortunately it flags xattrs that begin with the
714 * default streams prefix as private.
716 * By only calling samba_private_attr_name() in case
717 * the xattr does NOT begin with the default prefix,
718 * we know that if it returns 'true' it definitely one
719 * of our internal xattr like "user.DOSATTRIB".
721 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
722 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
723 if (samba_private_attr_name(names[i])) {
724 continue;
728 if (strncmp(names[i], config->prefix,
729 config->prefix_len) != 0) {
730 continue;
733 status = get_ea_value(names, handle->conn, fsp, fname,
734 names[i], &ea);
735 if (!NT_STATUS_IS_OK(status)) {
736 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
737 names[i], fname, nt_errstr(status)));
738 continue;
741 ea.name = talloc_asprintf(
742 ea.value.data, ":%s%s",
743 names[i] + config->prefix_len,
744 config->store_stream_type ? "" : ":$DATA");
745 if (ea.name == NULL) {
746 DEBUG(0, ("talloc failed\n"));
747 continue;
750 if (!fn(&ea, private_data)) {
751 TALLOC_FREE(ea.value.data);
752 return NT_STATUS_OK;
755 TALLOC_FREE(ea.value.data);
758 TALLOC_FREE(names);
759 return NT_STATUS_OK;
762 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
763 struct stream_struct **streams,
764 const char *name, off_t size,
765 off_t alloc_size)
767 struct stream_struct *tmp;
769 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
770 (*num_streams)+1);
771 if (tmp == NULL) {
772 return false;
775 tmp[*num_streams].name = talloc_strdup(tmp, name);
776 if (tmp[*num_streams].name == NULL) {
777 return false;
780 tmp[*num_streams].size = size;
781 tmp[*num_streams].alloc_size = alloc_size;
783 *streams = tmp;
784 *num_streams += 1;
785 return true;
788 struct streaminfo_state {
789 TALLOC_CTX *mem_ctx;
790 vfs_handle_struct *handle;
791 unsigned int num_streams;
792 struct stream_struct *streams;
793 NTSTATUS status;
796 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
798 struct streaminfo_state *state =
799 (struct streaminfo_state *)private_data;
801 if (!add_one_stream(state->mem_ctx,
802 &state->num_streams, &state->streams,
803 ea->name, ea->value.length-1,
804 smb_roundup(state->handle->conn,
805 ea->value.length-1))) {
806 state->status = NT_STATUS_NO_MEMORY;
807 return false;
810 return true;
813 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
814 struct files_struct *fsp,
815 const char *fname,
816 TALLOC_CTX *mem_ctx,
817 unsigned int *pnum_streams,
818 struct stream_struct **pstreams)
820 SMB_STRUCT_STAT sbuf;
821 int ret;
822 NTSTATUS status;
823 struct streaminfo_state state;
825 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
826 ret = SMB_VFS_FSTAT(fsp, &sbuf);
828 else {
829 struct smb_filename *smb_fname = NULL;
830 smb_fname = synthetic_smb_fname(talloc_tos(), fname, NULL,
831 NULL);
832 if (smb_fname == NULL) {
833 return NT_STATUS_NO_MEMORY;
835 if (lp_posix_pathnames()) {
836 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
837 } else {
838 ret = SMB_VFS_STAT(handle->conn, smb_fname);
840 sbuf = smb_fname->st;
841 TALLOC_FREE(smb_fname);
844 if (ret == -1) {
845 return map_nt_error_from_unix(errno);
848 state.streams = *pstreams;
849 state.num_streams = *pnum_streams;
850 state.mem_ctx = mem_ctx;
851 state.handle = handle;
852 state.status = NT_STATUS_OK;
854 if (S_ISLNK(sbuf.st_ex_mode)) {
856 * Currently we do't have SMB_VFS_LLISTXATTR
857 * inside the VFS which means there's no way
858 * to cope with a symlink when lp_posix_pathnames().
859 * returns true. For now ignore links.
860 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
862 status = NT_STATUS_OK;
863 } else {
864 status = walk_xattr_streams(handle, fsp, fname,
865 collect_one_stream, &state);
868 if (!NT_STATUS_IS_OK(status)) {
869 TALLOC_FREE(state.streams);
870 return status;
873 if (!NT_STATUS_IS_OK(state.status)) {
874 TALLOC_FREE(state.streams);
875 return state.status;
878 *pnum_streams = state.num_streams;
879 *pstreams = state.streams;
881 return SMB_VFS_NEXT_STREAMINFO(handle, fsp, fname, mem_ctx, pnum_streams, pstreams);
884 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
885 enum timestamp_set_resolution *p_ts_res)
887 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
890 static int streams_xattr_connect(vfs_handle_struct *handle,
891 const char *service, const char *user)
893 struct streams_xattr_config *config;
894 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
895 const char *prefix;
896 int rc;
898 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
899 if (rc != 0) {
900 return rc;
903 config = talloc_zero(handle->conn, struct streams_xattr_config);
904 if (config == NULL) {
905 DEBUG(1, ("talloc_zero() failed\n"));
906 errno = ENOMEM;
907 return -1;
910 prefix = lp_parm_const_string(SNUM(handle->conn),
911 "streams_xattr", "prefix",
912 default_prefix);
913 config->prefix = talloc_strdup(config, prefix);
914 if (config->prefix == NULL) {
915 DEBUG(1, ("talloc_strdup() failed\n"));
916 errno = ENOMEM;
917 return -1;
919 config->prefix_len = strlen(config->prefix);
920 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
922 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
923 "streams_xattr",
924 "store_stream_type",
925 true);
927 SMB_VFS_HANDLE_SET_DATA(handle, config,
928 NULL, struct stream_xattr_config,
929 return -1);
931 return 0;
934 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
935 files_struct *fsp, const void *data,
936 size_t n, off_t offset)
938 struct stream_io *sio =
939 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
940 struct ea_struct ea;
941 NTSTATUS status;
942 int ret;
944 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
946 if (sio == NULL) {
947 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
950 if (!streams_xattr_recheck(sio)) {
951 return -1;
954 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
955 sio->base, sio->xattr_name, &ea);
956 if (!NT_STATUS_IS_OK(status)) {
957 return -1;
960 if ((offset + n) > ea.value.length-1) {
961 uint8_t *tmp;
963 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
964 offset + n + 1);
966 if (tmp == NULL) {
967 TALLOC_FREE(ea.value.data);
968 errno = ENOMEM;
969 return -1;
971 ea.value.data = tmp;
972 ea.value.length = offset + n + 1;
973 ea.value.data[offset+n] = 0;
976 memcpy(ea.value.data + offset, data, n);
978 if (fsp->base_fsp->fh->fd != -1) {
979 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
980 sio->xattr_name,
981 ea.value.data, ea.value.length, 0);
982 } else {
983 ret = SMB_VFS_SETXATTR(fsp->conn,
984 fsp->base_fsp->fsp_name->base_name,
985 sio->xattr_name,
986 ea.value.data, ea.value.length, 0);
988 TALLOC_FREE(ea.value.data);
990 if (ret == -1) {
991 return -1;
994 return n;
997 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
998 files_struct *fsp, void *data,
999 size_t n, off_t offset)
1001 struct stream_io *sio =
1002 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1003 struct ea_struct ea;
1004 NTSTATUS status;
1005 size_t length, overlap;
1007 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1008 (int)offset, (int)n));
1010 if (sio == NULL) {
1011 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1014 if (!streams_xattr_recheck(sio)) {
1015 return -1;
1018 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1019 sio->base, sio->xattr_name, &ea);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 return -1;
1024 length = ea.value.length-1;
1026 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1027 (int)length));
1029 /* Attempt to read past EOF. */
1030 if (length <= offset) {
1031 return 0;
1034 overlap = (offset + n) > length ? (length - offset) : n;
1035 memcpy(data, ea.value.data + offset, overlap);
1037 TALLOC_FREE(ea.value.data);
1038 return overlap;
1041 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1042 struct files_struct *fsp,
1043 off_t offset)
1045 int ret;
1046 uint8_t *tmp;
1047 struct ea_struct ea;
1048 NTSTATUS status;
1049 struct stream_io *sio =
1050 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1052 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1053 fsp_str_dbg(fsp), (double)offset));
1055 if (sio == NULL) {
1056 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1059 if (!streams_xattr_recheck(sio)) {
1060 return -1;
1063 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
1064 sio->base, sio->xattr_name, &ea);
1065 if (!NT_STATUS_IS_OK(status)) {
1066 return -1;
1069 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1070 offset + 1);
1072 if (tmp == NULL) {
1073 TALLOC_FREE(ea.value.data);
1074 errno = ENOMEM;
1075 return -1;
1078 /* Did we expand ? */
1079 if (ea.value.length < offset + 1) {
1080 memset(&tmp[ea.value.length], '\0',
1081 offset + 1 - ea.value.length);
1084 ea.value.data = tmp;
1085 ea.value.length = offset + 1;
1086 ea.value.data[offset] = 0;
1088 if (fsp->base_fsp->fh->fd != -1) {
1089 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1090 sio->xattr_name,
1091 ea.value.data, ea.value.length, 0);
1092 } else {
1093 ret = SMB_VFS_SETXATTR(fsp->conn,
1094 fsp->base_fsp->fsp_name->base_name,
1095 sio->xattr_name,
1096 ea.value.data, ea.value.length, 0);
1099 TALLOC_FREE(ea.value.data);
1101 if (ret == -1) {
1102 return -1;
1105 return 0;
1108 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1109 struct files_struct *fsp,
1110 uint32_t mode,
1111 off_t offset,
1112 off_t len)
1114 struct stream_io *sio =
1115 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1117 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1118 "len = %.0f\n",
1119 fsp_str_dbg(fsp), (double)offset, (double)len));
1121 if (sio == NULL) {
1122 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1125 if (!streams_xattr_recheck(sio)) {
1126 return -1;
1129 /* Let the pwrite code path handle it. */
1130 errno = ENOSYS;
1131 return -1;
1135 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1136 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1137 .connect_fn = streams_xattr_connect,
1138 .open_fn = streams_xattr_open,
1139 .stat_fn = streams_xattr_stat,
1140 .fstat_fn = streams_xattr_fstat,
1141 .lstat_fn = streams_xattr_lstat,
1142 .pread_fn = streams_xattr_pread,
1143 .pwrite_fn = streams_xattr_pwrite,
1144 .unlink_fn = streams_xattr_unlink,
1145 .rename_fn = streams_xattr_rename,
1146 .ftruncate_fn = streams_xattr_ftruncate,
1147 .fallocate_fn = streams_xattr_fallocate,
1148 .streaminfo_fn = streams_xattr_streaminfo,
1151 NTSTATUS vfs_streams_xattr_init(void);
1152 NTSTATUS vfs_streams_xattr_init(void)
1154 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1155 &vfs_streams_xattr_fns);