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/>.
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
28 #include "lib/util/tevent_unix.h"
29 #include "librpc/gen_ndr/ioctl.h"
32 #define DBGC_CLASS DBGC_VFS
34 struct streams_xattr_config {
37 bool store_stream_type;
45 vfs_handle_struct *handle;
48 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
51 unsigned char hash[16];
55 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
56 (unsigned long)sbuf->st_ex_dev,
57 (unsigned long)sbuf->st_ex_ino, sname));
59 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
60 SMB_ASSERT(upper_sname != NULL);
63 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
64 sizeof(sbuf->st_ex_dev));
65 MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
66 sizeof(sbuf->st_ex_ino));
67 MD5Update(&ctx, (unsigned char *)upper_sname,
68 talloc_get_size(upper_sname)-1);
71 TALLOC_FREE(upper_sname);
73 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
74 memcpy(&result, hash, sizeof(result));
76 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
81 static ssize_t get_xattr_size(connection_struct *conn,
82 const struct smb_filename *smb_fname,
83 const char *xattr_name)
89 status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
92 if (!NT_STATUS_IS_OK(status)) {
96 result = ea.value.length-1;
97 TALLOC_FREE(ea.value.data);
102 * Given a stream name, populate xattr_name with the xattr name to use for
103 * accessing the stream.
105 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
107 const char *stream_name,
112 struct streams_xattr_config *config;
114 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
115 return NT_STATUS_UNSUCCESSFUL);
117 sname = talloc_strdup(ctx, stream_name + 1);
119 return NT_STATUS_NO_MEMORY;
123 * With vfs_fruit option "fruit:encoding = native" we're
124 * already converting stream names that contain illegal NTFS
125 * characters from their on-the-wire Unicode Private Range
126 * encoding to their native ASCII representation.
128 * As as result the name of xattrs storing the streams (via
129 * vfs_streams_xattr) may contain a colon, so we have to use
130 * strrchr_m() instead of strchr_m() for matching the stream
133 * In check_path_syntax() we've already ensured the streamname
134 * we got from the client is valid.
136 stype = strrchr_m(sname, ':');
140 * We only support one stream type: "$DATA"
142 if (strcasecmp_m(stype, ":$DATA") != 0) {
144 return NT_STATUS_INVALID_PARAMETER;
147 /* Split name and type */
151 *xattr_name = talloc_asprintf(ctx, "%s%s%s",
154 config->store_stream_type ? ":$DATA" : "");
155 if (*xattr_name == NULL) {
157 return NT_STATUS_NO_MEMORY;
160 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
167 static bool streams_xattr_recheck(struct stream_io *sio)
170 char *xattr_name = NULL;
172 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
176 if (sio->fsp->fsp_name->stream_name == NULL) {
177 /* how can this happen */
182 status = streams_xattr_get_name(sio->handle, talloc_tos(),
183 sio->fsp->fsp_name->stream_name,
185 if (!NT_STATUS_IS_OK(status)) {
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),
193 if (sio->xattr_name == NULL) {
194 DBG_DEBUG("sio->xattr_name==NULL\n");
197 TALLOC_FREE(xattr_name);
199 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
200 sio->fsp->fsp_name->base_name);
201 if (sio->base == NULL) {
202 DBG_DEBUG("sio->base==NULL\n");
206 sio->fsp_name_ptr = sio->fsp->fsp_name;
212 * Helper to stat/lstat the base file of an smb_fname.
214 static int streams_xattr_stat_base(vfs_handle_struct *handle,
215 struct smb_filename *smb_fname,
218 char *tmp_stream_name;
221 tmp_stream_name = smb_fname->stream_name;
222 smb_fname->stream_name = NULL;
224 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
226 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
228 smb_fname->stream_name = tmp_stream_name;
232 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
233 SMB_STRUCT_STAT *sbuf)
235 struct smb_filename *smb_fname_base = NULL;
237 struct stream_io *io = (struct stream_io *)
238 VFS_FETCH_FSP_EXTENSION(handle, fsp);
240 if (io == NULL || fsp->base_fsp == NULL) {
241 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
244 DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
246 if (!streams_xattr_recheck(io)) {
250 /* Create an smb_filename with stream_name == NULL. */
251 smb_fname_base = synthetic_smb_fname(talloc_tos(),
255 fsp->fsp_name->flags);
256 if (smb_fname_base == NULL) {
261 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
262 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
264 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
266 *sbuf = smb_fname_base->st;
269 TALLOC_FREE(smb_fname_base);
273 sbuf->st_ex_size = get_xattr_size(handle->conn,
274 smb_fname_base, io->xattr_name);
275 if (sbuf->st_ex_size == -1) {
276 TALLOC_FREE(smb_fname_base);
277 SET_STAT_INVALID(*sbuf);
281 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
283 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
284 sbuf->st_ex_mode &= ~S_IFMT;
285 sbuf->st_ex_mode &= ~S_IFDIR;
286 sbuf->st_ex_mode |= S_IFREG;
287 sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
289 TALLOC_FREE(smb_fname_base);
293 static int streams_xattr_stat(vfs_handle_struct *handle,
294 struct smb_filename *smb_fname)
298 char *xattr_name = NULL;
300 if (!is_ntfs_stream_smb_fname(smb_fname)) {
301 return SMB_VFS_NEXT_STAT(handle, smb_fname);
304 /* Note if lp_posix_paths() is true, we can never
305 * get here as is_ntfs_stream_smb_fname() is
306 * always false. So we never need worry about
307 * not following links here. */
309 /* If the default stream is requested, just stat the base file. */
310 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
311 return streams_xattr_stat_base(handle, smb_fname, true);
314 /* Populate the stat struct with info from the base file. */
315 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
319 /* Derive the xattr name to lookup. */
320 status = streams_xattr_get_name(handle, talloc_tos(),
321 smb_fname->stream_name, &xattr_name);
322 if (!NT_STATUS_IS_OK(status)) {
323 errno = map_errno_from_nt_status(status);
327 /* Augment the base file's stat information before returning. */
328 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
331 if (smb_fname->st.st_ex_size == -1) {
332 SET_STAT_INVALID(smb_fname->st);
338 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
339 smb_fname->st.st_ex_mode &= ~S_IFMT;
340 smb_fname->st.st_ex_mode &= ~S_IFDIR;
341 smb_fname->st.st_ex_mode |= S_IFREG;
342 smb_fname->st.st_ex_blocks =
343 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
347 TALLOC_FREE(xattr_name);
351 static int streams_xattr_lstat(vfs_handle_struct *handle,
352 struct smb_filename *smb_fname)
356 char *xattr_name = NULL;
358 if (!is_ntfs_stream_smb_fname(smb_fname)) {
359 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
362 /* If the default stream is requested, just stat the base file. */
363 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
364 return streams_xattr_stat_base(handle, smb_fname, false);
367 /* Populate the stat struct with info from the base file. */
368 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
372 /* Derive the xattr name to lookup. */
373 status = streams_xattr_get_name(handle, talloc_tos(),
374 smb_fname->stream_name, &xattr_name);
375 if (!NT_STATUS_IS_OK(status)) {
376 errno = map_errno_from_nt_status(status);
380 /* Augment the base file's stat information before returning. */
381 smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
384 if (smb_fname->st.st_ex_size == -1) {
385 SET_STAT_INVALID(smb_fname->st);
391 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
392 smb_fname->st.st_ex_mode &= ~S_IFMT;
393 smb_fname->st.st_ex_mode |= S_IFREG;
394 smb_fname->st.st_ex_blocks =
395 smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
400 TALLOC_FREE(xattr_name);
404 static int streams_xattr_open(vfs_handle_struct *handle,
405 struct smb_filename *smb_fname,
406 files_struct *fsp, int flags, mode_t mode)
409 struct streams_xattr_config *config = NULL;
410 struct stream_io *sio = NULL;
412 char *xattr_name = NULL;
415 bool set_empty_xattr = false;
418 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
421 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
422 smb_fname_str_dbg(smb_fname), flags));
424 if (!is_ntfs_stream_smb_fname(smb_fname)) {
425 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
428 /* If the default stream is requested, just open the base file. */
429 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
430 char *tmp_stream_name;
432 tmp_stream_name = smb_fname->stream_name;
433 smb_fname->stream_name = NULL;
435 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
437 smb_fname->stream_name = tmp_stream_name;
442 status = streams_xattr_get_name(handle, talloc_tos(),
443 smb_fname->stream_name, &xattr_name);
444 if (!NT_STATUS_IS_OK(status)) {
445 errno = map_errno_from_nt_status(status);
449 status = get_ea_value(talloc_tos(), handle->conn, NULL,
450 smb_fname, xattr_name, &ea);
452 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
454 if (!NT_STATUS_IS_OK(status)) {
455 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
457 * The base file is not there. This is an error even if
458 * we got O_CREAT, the higher levels should have created
459 * the base file for us.
461 DBG_DEBUG("streams_xattr_open: base file %s not around, "
462 "returning ENOENT\n", smb_fname->base_name);
467 if (!(flags & O_CREAT)) {
472 set_empty_xattr = true;
475 if (flags & O_TRUNC) {
476 set_empty_xattr = true;
479 if (set_empty_xattr) {
481 * The attribute does not exist or needs to be truncated
485 * Darn, xattrs need at least 1 byte
489 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
490 xattr_name, smb_fname->base_name));
492 ret = SMB_VFS_SETXATTR(fsp->conn,
496 flags & O_EXCL ? XATTR_CREATE : 0);
503 * Return a valid fd, but ensure any attempt to use it returns an error
506 ret = pipe(pipe_fds);
513 fakefd = pipe_fds[0];
515 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
521 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
523 if (sio->xattr_name == NULL) {
529 * so->base needs to be a copy of fsp->fsp_name->base_name,
530 * making it identical to streams_xattr_recheck(). If the
531 * open is changing directories, fsp->fsp_name->base_name
532 * will be the full path from the share root, whilst
533 * smb_fname will be relative to the $cwd.
535 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
536 fsp->fsp_name->base_name);
537 if (sio->base == NULL) {
542 sio->fsp_name_ptr = fsp->fsp_name;
543 sio->handle = handle;
557 static int streams_xattr_unlink(vfs_handle_struct *handle,
558 const struct smb_filename *smb_fname)
562 char *xattr_name = NULL;
564 if (!is_ntfs_stream_smb_fname(smb_fname)) {
565 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
568 /* If the default stream is requested, just open the base file. */
569 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
570 struct smb_filename *smb_fname_base = NULL;
572 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
573 if (smb_fname_base == NULL) {
578 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
580 TALLOC_FREE(smb_fname_base);
584 status = streams_xattr_get_name(handle, talloc_tos(),
585 smb_fname->stream_name, &xattr_name);
586 if (!NT_STATUS_IS_OK(status)) {
587 errno = map_errno_from_nt_status(status);
591 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
593 if ((ret == -1) && (errno == ENOATTR)) {
601 TALLOC_FREE(xattr_name);
605 static int streams_xattr_rename(vfs_handle_struct *handle,
606 const struct smb_filename *smb_fname_src,
607 const struct smb_filename *smb_fname_dst)
611 char *src_xattr_name = NULL;
612 char *dst_xattr_name = NULL;
613 bool src_is_stream, dst_is_stream;
618 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
619 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
621 if (!src_is_stream && !dst_is_stream) {
622 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
626 /* For now don't allow renames from or to the default stream. */
627 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
628 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
633 /* Don't rename if the streams are identical. */
634 if (strcasecmp_m(smb_fname_src->stream_name,
635 smb_fname_dst->stream_name) == 0) {
639 /* Get the xattr names. */
640 status = streams_xattr_get_name(handle, talloc_tos(),
641 smb_fname_src->stream_name,
643 if (!NT_STATUS_IS_OK(status)) {
644 errno = map_errno_from_nt_status(status);
647 status = streams_xattr_get_name(handle, talloc_tos(),
648 smb_fname_dst->stream_name,
650 if (!NT_STATUS_IS_OK(status)) {
651 errno = map_errno_from_nt_status(status);
655 /* read the old stream */
656 status = get_ea_value(talloc_tos(), handle->conn, NULL,
657 smb_fname_src, src_xattr_name, &ea);
658 if (!NT_STATUS_IS_OK(status)) {
663 /* (over)write the new stream */
664 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
665 dst_xattr_name, ea.value.data, ea.value.length,
668 if (errno == ENOATTR) {
674 /* remove the old stream */
675 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
678 if (errno == ENOATTR) {
688 TALLOC_FREE(src_xattr_name);
689 TALLOC_FREE(dst_xattr_name);
693 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
695 const struct smb_filename *smb_fname,
696 bool (*fn)(struct ea_struct *ea,
703 struct streams_xattr_config *config;
705 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
706 return NT_STATUS_UNSUCCESSFUL);
708 status = get_ea_names_from_file(talloc_tos(),
714 if (!NT_STATUS_IS_OK(status)) {
718 for (i=0; i<num_names; i++) {
722 * We want to check with samba_private_attr_name()
723 * whether the xattr name is a private one,
724 * unfortunately it flags xattrs that begin with the
725 * default streams prefix as private.
727 * By only calling samba_private_attr_name() in case
728 * the xattr does NOT begin with the default prefix,
729 * we know that if it returns 'true' it definitely one
730 * of our internal xattr like "user.DOSATTRIB".
732 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
733 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
734 if (samba_private_attr_name(names[i])) {
739 if (strncmp(names[i], config->prefix,
740 config->prefix_len) != 0) {
744 status = get_ea_value(names,
750 if (!NT_STATUS_IS_OK(status)) {
751 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
753 smb_fname->base_name,
758 ea.name = talloc_asprintf(
759 ea.value.data, ":%s%s",
760 names[i] + config->prefix_len,
761 config->store_stream_type ? "" : ":$DATA");
762 if (ea.name == NULL) {
763 DEBUG(0, ("talloc failed\n"));
767 if (!fn(&ea, private_data)) {
768 TALLOC_FREE(ea.value.data);
772 TALLOC_FREE(ea.value.data);
779 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
780 struct stream_struct **streams,
781 const char *name, off_t size,
784 struct stream_struct *tmp;
786 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
792 tmp[*num_streams].name = talloc_strdup(tmp, name);
793 if (tmp[*num_streams].name == NULL) {
797 tmp[*num_streams].size = size;
798 tmp[*num_streams].alloc_size = alloc_size;
805 struct streaminfo_state {
807 vfs_handle_struct *handle;
808 unsigned int num_streams;
809 struct stream_struct *streams;
813 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
815 struct streaminfo_state *state =
816 (struct streaminfo_state *)private_data;
818 if (!add_one_stream(state->mem_ctx,
819 &state->num_streams, &state->streams,
820 ea->name, ea->value.length-1,
821 smb_roundup(state->handle->conn,
822 ea->value.length-1))) {
823 state->status = NT_STATUS_NO_MEMORY;
830 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
831 struct files_struct *fsp,
832 const struct smb_filename *smb_fname,
834 unsigned int *pnum_streams,
835 struct stream_struct **pstreams)
837 SMB_STRUCT_STAT sbuf;
840 struct streaminfo_state state;
842 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
844 return map_nt_error_from_unix(errno);
847 state.streams = *pstreams;
848 state.num_streams = *pnum_streams;
849 state.mem_ctx = mem_ctx;
850 state.handle = handle;
851 state.status = NT_STATUS_OK;
853 if (S_ISLNK(sbuf.st_ex_mode)) {
855 * Currently we do't have SMB_VFS_LLISTXATTR
856 * inside the VFS which means there's no way
857 * to cope with a symlink when lp_posix_pathnames().
858 * returns true. For now ignore links.
859 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
861 status = NT_STATUS_OK;
863 status = walk_xattr_streams(handle, fsp, smb_fname,
864 collect_one_stream, &state);
867 if (!NT_STATUS_IS_OK(status)) {
868 TALLOC_FREE(state.streams);
872 if (!NT_STATUS_IS_OK(state.status)) {
873 TALLOC_FREE(state.streams);
877 *pnum_streams = state.num_streams;
878 *pstreams = state.streams;
880 return SMB_VFS_NEXT_STREAMINFO(handle,
888 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
889 enum timestamp_set_resolution *p_ts_res)
891 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
894 static int streams_xattr_connect(vfs_handle_struct *handle,
895 const char *service, const char *user)
897 struct streams_xattr_config *config;
898 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
902 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
907 config = talloc_zero(handle->conn, struct streams_xattr_config);
908 if (config == NULL) {
909 DEBUG(1, ("talloc_zero() failed\n"));
914 prefix = lp_parm_const_string(SNUM(handle->conn),
915 "streams_xattr", "prefix",
917 config->prefix = talloc_strdup(config, prefix);
918 if (config->prefix == NULL) {
919 DEBUG(1, ("talloc_strdup() failed\n"));
923 config->prefix_len = strlen(config->prefix);
924 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
926 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
931 SMB_VFS_HANDLE_SET_DATA(handle, config,
932 NULL, struct stream_xattr_config,
938 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
939 files_struct *fsp, const void *data,
940 size_t n, off_t offset)
942 struct stream_io *sio =
943 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
946 struct smb_filename *smb_fname_base = NULL;
949 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
952 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
955 if (!streams_xattr_recheck(sio)) {
959 /* Create an smb_filename with stream_name == NULL. */
960 smb_fname_base = synthetic_smb_fname(talloc_tos(),
964 fsp->fsp_name->flags);
965 if (smb_fname_base == NULL) {
970 status = get_ea_value(talloc_tos(), handle->conn, NULL,
971 smb_fname_base, sio->xattr_name, &ea);
972 if (!NT_STATUS_IS_OK(status)) {
976 if ((offset + n) > ea.value.length-1) {
979 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
983 TALLOC_FREE(ea.value.data);
988 ea.value.length = offset + n + 1;
989 ea.value.data[offset+n] = 0;
992 memcpy(ea.value.data + offset, data, n);
994 ret = SMB_VFS_SETXATTR(fsp->conn,
997 ea.value.data, ea.value.length, 0);
998 TALLOC_FREE(ea.value.data);
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;
1015 size_t length, overlap;
1016 struct smb_filename *smb_fname_base = NULL;
1018 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1019 (int)offset, (int)n));
1022 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1025 if (!streams_xattr_recheck(sio)) {
1029 /* Create an smb_filename with stream_name == NULL. */
1030 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1034 fsp->fsp_name->flags);
1035 if (smb_fname_base == NULL) {
1040 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1041 smb_fname_base, sio->xattr_name, &ea);
1042 if (!NT_STATUS_IS_OK(status)) {
1046 length = ea.value.length-1;
1048 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1051 /* Attempt to read past EOF. */
1052 if (length <= offset) {
1056 overlap = (offset + n) > length ? (length - offset) : n;
1057 memcpy(data, ea.value.data + offset, overlap);
1059 TALLOC_FREE(ea.value.data);
1063 struct streams_xattr_pread_state {
1065 struct vfs_aio_state vfs_aio_state;
1068 static void streams_xattr_pread_done(struct tevent_req *subreq);
1070 static struct tevent_req *streams_xattr_pread_send(
1071 struct vfs_handle_struct *handle,
1072 TALLOC_CTX *mem_ctx,
1073 struct tevent_context *ev,
1074 struct files_struct *fsp,
1076 size_t n, off_t offset)
1078 struct tevent_req *req = NULL;
1079 struct tevent_req *subreq = NULL;
1080 struct streams_xattr_pread_state *state = NULL;
1081 struct stream_io *sio =
1082 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1084 req = tevent_req_create(mem_ctx, &state,
1085 struct streams_xattr_pread_state);
1091 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1093 if (tevent_req_nomem(req, subreq)) {
1094 return tevent_req_post(req, ev);
1096 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1100 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1101 if (state->nread != n) {
1102 if (state->nread != -1) {
1105 tevent_req_error(req, errno);
1106 return tevent_req_post(req, ev);
1109 tevent_req_done(req);
1110 return tevent_req_post(req, ev);
1113 static void streams_xattr_pread_done(struct tevent_req *subreq)
1115 struct tevent_req *req = tevent_req_callback_data(
1116 subreq, struct tevent_req);
1117 struct streams_xattr_pread_state *state = tevent_req_data(
1118 req, struct streams_xattr_pread_state);
1120 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1121 TALLOC_FREE(subreq);
1123 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1126 tevent_req_done(req);
1129 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1130 struct vfs_aio_state *vfs_aio_state)
1132 struct streams_xattr_pread_state *state = tevent_req_data(
1133 req, struct streams_xattr_pread_state);
1135 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1139 *vfs_aio_state = state->vfs_aio_state;
1140 return state->nread;
1143 struct streams_xattr_pwrite_state {
1145 struct vfs_aio_state vfs_aio_state;
1148 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1150 static struct tevent_req *streams_xattr_pwrite_send(
1151 struct vfs_handle_struct *handle,
1152 TALLOC_CTX *mem_ctx,
1153 struct tevent_context *ev,
1154 struct files_struct *fsp,
1156 size_t n, off_t offset)
1158 struct tevent_req *req = NULL;
1159 struct tevent_req *subreq = NULL;
1160 struct streams_xattr_pwrite_state *state = NULL;
1161 struct stream_io *sio =
1162 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1164 req = tevent_req_create(mem_ctx, &state,
1165 struct streams_xattr_pwrite_state);
1171 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1173 if (tevent_req_nomem(req, subreq)) {
1174 return tevent_req_post(req, ev);
1176 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1180 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1181 if (state->nwritten != n) {
1182 if (state->nwritten != -1) {
1185 tevent_req_error(req, errno);
1186 return tevent_req_post(req, ev);
1189 tevent_req_done(req);
1190 return tevent_req_post(req, ev);
1193 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1195 struct tevent_req *req = tevent_req_callback_data(
1196 subreq, struct tevent_req);
1197 struct streams_xattr_pwrite_state *state = tevent_req_data(
1198 req, struct streams_xattr_pwrite_state);
1200 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1201 TALLOC_FREE(subreq);
1203 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1206 tevent_req_done(req);
1209 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1210 struct vfs_aio_state *vfs_aio_state)
1212 struct streams_xattr_pwrite_state *state = tevent_req_data(
1213 req, struct streams_xattr_pwrite_state);
1215 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1219 *vfs_aio_state = state->vfs_aio_state;
1220 return state->nwritten;
1223 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1224 struct files_struct *fsp,
1229 struct ea_struct ea;
1231 struct stream_io *sio =
1232 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1233 struct smb_filename *smb_fname_base = NULL;
1235 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1236 fsp_str_dbg(fsp), (double)offset));
1239 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1242 if (!streams_xattr_recheck(sio)) {
1246 /* Create an smb_filename with stream_name == NULL. */
1247 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1251 fsp->fsp_name->flags);
1252 if (smb_fname_base == NULL) {
1257 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1258 smb_fname_base, sio->xattr_name, &ea);
1259 if (!NT_STATUS_IS_OK(status)) {
1263 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1267 TALLOC_FREE(ea.value.data);
1272 /* Did we expand ? */
1273 if (ea.value.length < offset + 1) {
1274 memset(&tmp[ea.value.length], '\0',
1275 offset + 1 - ea.value.length);
1278 ea.value.data = tmp;
1279 ea.value.length = offset + 1;
1280 ea.value.data[offset] = 0;
1282 ret = SMB_VFS_SETXATTR(fsp->conn,
1285 ea.value.data, ea.value.length, 0);
1286 TALLOC_FREE(ea.value.data);
1295 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1296 struct files_struct *fsp,
1301 struct stream_io *sio =
1302 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1304 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1306 fsp_str_dbg(fsp), (double)offset, (double)len));
1309 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1312 if (!streams_xattr_recheck(sio)) {
1316 /* Let the pwrite code path handle it. */
1321 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1322 uid_t uid, gid_t gid)
1324 struct stream_io *sio =
1325 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1328 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1334 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1338 struct stream_io *sio =
1339 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1342 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1348 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1349 struct files_struct *fsp,
1354 struct stream_io *sio =
1355 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1358 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1365 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1366 struct files_struct *fsp,
1370 struct stream_io *sio =
1371 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1374 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1381 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1382 struct files_struct *fsp,
1385 struct stream_io *sio =
1386 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1389 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1396 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1397 struct files_struct *fsp,
1403 struct stream_io *sio =
1404 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1407 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1415 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1417 TALLOC_CTX *mem_ctx)
1419 struct stream_io *sio =
1420 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1423 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1426 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1427 handle, fsp->base_fsp->fsp_name,
1428 SMB_ACL_TYPE_ACCESS, mem_ctx);
1431 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1435 struct stream_io *sio =
1436 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1439 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1445 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1447 TALLOC_CTX *mem_ctx,
1448 char **blob_description,
1451 struct stream_io *sio =
1452 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1455 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1456 blob_description, blob);
1459 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1460 handle, fsp->base_fsp->fsp_name, mem_ctx,
1461 blob_description, blob);
1464 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1466 uint32_t security_info,
1467 TALLOC_CTX *mem_ctx,
1468 struct security_descriptor **ppdesc)
1470 struct stream_io *sio =
1471 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1474 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1478 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1479 security_info, mem_ctx, ppdesc);
1482 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1484 uint32_t security_info_sent,
1485 const struct security_descriptor *psd)
1487 struct stream_io *sio =
1488 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1491 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1492 security_info_sent, psd);
1495 return NT_STATUS_OK;
1498 struct streams_xattr_fsync_state {
1500 struct vfs_aio_state vfs_aio_state;
1503 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1505 static struct tevent_req *streams_xattr_fsync_send(
1506 struct vfs_handle_struct *handle,
1507 TALLOC_CTX *mem_ctx,
1508 struct tevent_context *ev,
1509 struct files_struct *fsp)
1511 struct tevent_req *req = NULL;
1512 struct tevent_req *subreq = NULL;
1513 struct streams_xattr_fsync_state *state = NULL;
1514 struct stream_io *sio =
1515 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1517 req = tevent_req_create(mem_ctx, &state,
1518 struct streams_xattr_fsync_state);
1524 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1525 if (tevent_req_nomem(req, subreq)) {
1526 return tevent_req_post(req, ev);
1528 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1533 * There's no pathname based sync variant and we don't have access to
1534 * the basefile handle, so we can't do anything here.
1537 tevent_req_done(req);
1538 return tevent_req_post(req, ev);
1541 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1543 struct tevent_req *req = tevent_req_callback_data(
1544 subreq, struct tevent_req);
1545 struct streams_xattr_fsync_state *state = tevent_req_data(
1546 req, struct streams_xattr_fsync_state);
1548 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1549 TALLOC_FREE(subreq);
1550 if (state->ret != 0) {
1551 tevent_req_error(req, errno);
1555 tevent_req_done(req);
1558 static int streams_xattr_fsync_recv(struct tevent_req *req,
1559 struct vfs_aio_state *vfs_aio_state)
1561 struct streams_xattr_fsync_state *state = tevent_req_data(
1562 req, struct streams_xattr_fsync_state);
1564 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1568 *vfs_aio_state = state->vfs_aio_state;
1572 static bool streams_xattr_lock(vfs_handle_struct *handle,
1579 struct stream_io *sio =
1580 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1583 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1589 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1596 struct stream_io *sio =
1597 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1600 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1601 pcount, ptype, ppid);
1608 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1610 uint32_t share_mode,
1611 uint32_t access_mask)
1613 struct stream_io *sio =
1614 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1617 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1618 share_mode, access_mask);
1624 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1628 struct stream_io *sio =
1629 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1632 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1638 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1640 struct lock_struct *plock)
1642 struct stream_io *sio =
1643 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1646 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1652 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1653 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1654 .connect_fn = streams_xattr_connect,
1655 .open_fn = streams_xattr_open,
1656 .stat_fn = streams_xattr_stat,
1657 .fstat_fn = streams_xattr_fstat,
1658 .lstat_fn = streams_xattr_lstat,
1659 .pread_fn = streams_xattr_pread,
1660 .pwrite_fn = streams_xattr_pwrite,
1661 .pread_send_fn = streams_xattr_pread_send,
1662 .pread_recv_fn = streams_xattr_pread_recv,
1663 .pwrite_send_fn = streams_xattr_pwrite_send,
1664 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1665 .unlink_fn = streams_xattr_unlink,
1666 .rename_fn = streams_xattr_rename,
1667 .ftruncate_fn = streams_xattr_ftruncate,
1668 .fallocate_fn = streams_xattr_fallocate,
1669 .streaminfo_fn = streams_xattr_streaminfo,
1671 .fsync_send_fn = streams_xattr_fsync_send,
1672 .fsync_recv_fn = streams_xattr_fsync_recv,
1674 .lock_fn = streams_xattr_lock,
1675 .getlock_fn = streams_xattr_getlock,
1676 .kernel_flock_fn = streams_xattr_kernel_flock,
1677 .linux_setlease_fn = streams_xattr_linux_setlease,
1678 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1680 .fchown_fn = streams_xattr_fchown,
1681 .fchmod_fn = streams_xattr_fchmod,
1683 .fgetxattr_fn = streams_xattr_fgetxattr,
1684 .flistxattr_fn = streams_xattr_flistxattr,
1685 .fremovexattr_fn = streams_xattr_fremovexattr,
1686 .fsetxattr_fn = streams_xattr_fsetxattr,
1688 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1689 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1690 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1692 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1693 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1697 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1699 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1700 &vfs_streams_xattr_fns);