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;
417 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
420 DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
421 smb_fname_str_dbg(smb_fname), flags));
423 if (!is_ntfs_stream_smb_fname(smb_fname)) {
424 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
427 /* If the default stream is requested, just open the base file. */
428 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
429 char *tmp_stream_name;
431 tmp_stream_name = smb_fname->stream_name;
432 smb_fname->stream_name = NULL;
434 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
436 smb_fname->stream_name = tmp_stream_name;
441 status = streams_xattr_get_name(handle, talloc_tos(),
442 smb_fname->stream_name, &xattr_name);
443 if (!NT_STATUS_IS_OK(status)) {
444 errno = map_errno_from_nt_status(status);
449 * Return a valid fd, but ensure any attempt to use it returns an error
452 ret = pipe(pipe_fds);
459 fakefd = pipe_fds[0];
461 status = get_ea_value(talloc_tos(), handle->conn, NULL,
462 smb_fname, xattr_name, &ea);
464 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
466 if (!NT_STATUS_IS_OK(status)
467 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
469 * The base file is not there. This is an error even if we got
470 * O_CREAT, the higher levels should have created the base
473 DEBUG(10, ("streams_xattr_open: base file %s not around, "
474 "returning ENOENT\n", smb_fname->base_name));
479 if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
482 * The attribute does not exist or needs to be truncated
486 * Darn, xattrs need at least 1 byte
490 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
491 xattr_name, smb_fname->base_name));
493 ret = SMB_VFS_SETXATTR(fsp->conn,
497 flags & O_EXCL ? XATTR_CREATE : 0);
503 sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
509 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
511 if (sio->xattr_name == NULL) {
517 * so->base needs to be a copy of fsp->fsp_name->base_name,
518 * making it identical to streams_xattr_recheck(). If the
519 * open is changing directories, fsp->fsp_name->base_name
520 * will be the full path from the share root, whilst
521 * smb_fname will be relative to the $cwd.
523 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
524 fsp->fsp_name->base_name);
525 if (sio->base == NULL) {
530 sio->fsp_name_ptr = fsp->fsp_name;
531 sio->handle = handle;
545 static int streams_xattr_unlink(vfs_handle_struct *handle,
546 const struct smb_filename *smb_fname)
550 char *xattr_name = NULL;
552 if (!is_ntfs_stream_smb_fname(smb_fname)) {
553 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
556 /* If the default stream is requested, just open the base file. */
557 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
558 struct smb_filename *smb_fname_base = NULL;
560 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
561 if (smb_fname_base == NULL) {
566 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
568 TALLOC_FREE(smb_fname_base);
572 status = streams_xattr_get_name(handle, talloc_tos(),
573 smb_fname->stream_name, &xattr_name);
574 if (!NT_STATUS_IS_OK(status)) {
575 errno = map_errno_from_nt_status(status);
579 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
581 if ((ret == -1) && (errno == ENOATTR)) {
589 TALLOC_FREE(xattr_name);
593 static int streams_xattr_rename(vfs_handle_struct *handle,
594 const struct smb_filename *smb_fname_src,
595 const struct smb_filename *smb_fname_dst)
599 char *src_xattr_name = NULL;
600 char *dst_xattr_name = NULL;
601 bool src_is_stream, dst_is_stream;
606 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
607 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
609 if (!src_is_stream && !dst_is_stream) {
610 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
614 /* For now don't allow renames from or to the default stream. */
615 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
616 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
621 /* Don't rename if the streams are identical. */
622 if (strcasecmp_m(smb_fname_src->stream_name,
623 smb_fname_dst->stream_name) == 0) {
627 /* Get the xattr names. */
628 status = streams_xattr_get_name(handle, talloc_tos(),
629 smb_fname_src->stream_name,
631 if (!NT_STATUS_IS_OK(status)) {
632 errno = map_errno_from_nt_status(status);
635 status = streams_xattr_get_name(handle, talloc_tos(),
636 smb_fname_dst->stream_name,
638 if (!NT_STATUS_IS_OK(status)) {
639 errno = map_errno_from_nt_status(status);
643 /* read the old stream */
644 status = get_ea_value(talloc_tos(), handle->conn, NULL,
645 smb_fname_src, src_xattr_name, &ea);
646 if (!NT_STATUS_IS_OK(status)) {
651 /* (over)write the new stream */
652 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
653 dst_xattr_name, ea.value.data, ea.value.length,
656 if (errno == ENOATTR) {
662 /* remove the old stream */
663 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
666 if (errno == ENOATTR) {
676 TALLOC_FREE(src_xattr_name);
677 TALLOC_FREE(dst_xattr_name);
681 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
683 const struct smb_filename *smb_fname,
684 bool (*fn)(struct ea_struct *ea,
691 struct streams_xattr_config *config;
693 SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
694 return NT_STATUS_UNSUCCESSFUL);
696 status = get_ea_names_from_file(talloc_tos(),
702 if (!NT_STATUS_IS_OK(status)) {
706 for (i=0; i<num_names; i++) {
710 * We want to check with samba_private_attr_name()
711 * whether the xattr name is a private one,
712 * unfortunately it flags xattrs that begin with the
713 * default streams prefix as private.
715 * By only calling samba_private_attr_name() in case
716 * the xattr does NOT begin with the default prefix,
717 * we know that if it returns 'true' it definitely one
718 * of our internal xattr like "user.DOSATTRIB".
720 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
721 strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
722 if (samba_private_attr_name(names[i])) {
727 if (strncmp(names[i], config->prefix,
728 config->prefix_len) != 0) {
732 status = get_ea_value(names,
738 if (!NT_STATUS_IS_OK(status)) {
739 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
741 smb_fname->base_name,
746 ea.name = talloc_asprintf(
747 ea.value.data, ":%s%s",
748 names[i] + config->prefix_len,
749 config->store_stream_type ? "" : ":$DATA");
750 if (ea.name == NULL) {
751 DEBUG(0, ("talloc failed\n"));
755 if (!fn(&ea, private_data)) {
756 TALLOC_FREE(ea.value.data);
760 TALLOC_FREE(ea.value.data);
767 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
768 struct stream_struct **streams,
769 const char *name, off_t size,
772 struct stream_struct *tmp;
774 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
780 tmp[*num_streams].name = talloc_strdup(tmp, name);
781 if (tmp[*num_streams].name == NULL) {
785 tmp[*num_streams].size = size;
786 tmp[*num_streams].alloc_size = alloc_size;
793 struct streaminfo_state {
795 vfs_handle_struct *handle;
796 unsigned int num_streams;
797 struct stream_struct *streams;
801 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
803 struct streaminfo_state *state =
804 (struct streaminfo_state *)private_data;
806 if (!add_one_stream(state->mem_ctx,
807 &state->num_streams, &state->streams,
808 ea->name, ea->value.length-1,
809 smb_roundup(state->handle->conn,
810 ea->value.length-1))) {
811 state->status = NT_STATUS_NO_MEMORY;
818 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
819 struct files_struct *fsp,
820 const struct smb_filename *smb_fname,
822 unsigned int *pnum_streams,
823 struct stream_struct **pstreams)
825 SMB_STRUCT_STAT sbuf;
828 struct streaminfo_state state;
830 ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
832 return map_nt_error_from_unix(errno);
835 state.streams = *pstreams;
836 state.num_streams = *pnum_streams;
837 state.mem_ctx = mem_ctx;
838 state.handle = handle;
839 state.status = NT_STATUS_OK;
841 if (S_ISLNK(sbuf.st_ex_mode)) {
843 * Currently we do't have SMB_VFS_LLISTXATTR
844 * inside the VFS which means there's no way
845 * to cope with a symlink when lp_posix_pathnames().
846 * returns true. For now ignore links.
847 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
849 status = NT_STATUS_OK;
851 status = walk_xattr_streams(handle, fsp, smb_fname,
852 collect_one_stream, &state);
855 if (!NT_STATUS_IS_OK(status)) {
856 TALLOC_FREE(state.streams);
860 if (!NT_STATUS_IS_OK(state.status)) {
861 TALLOC_FREE(state.streams);
865 *pnum_streams = state.num_streams;
866 *pstreams = state.streams;
868 return SMB_VFS_NEXT_STREAMINFO(handle,
876 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
877 enum timestamp_set_resolution *p_ts_res)
879 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
882 static int streams_xattr_connect(vfs_handle_struct *handle,
883 const char *service, const char *user)
885 struct streams_xattr_config *config;
886 const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
890 rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
895 config = talloc_zero(handle->conn, struct streams_xattr_config);
896 if (config == NULL) {
897 DEBUG(1, ("talloc_zero() failed\n"));
902 prefix = lp_parm_const_string(SNUM(handle->conn),
903 "streams_xattr", "prefix",
905 config->prefix = talloc_strdup(config, prefix);
906 if (config->prefix == NULL) {
907 DEBUG(1, ("talloc_strdup() failed\n"));
911 config->prefix_len = strlen(config->prefix);
912 DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
914 config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
919 SMB_VFS_HANDLE_SET_DATA(handle, config,
920 NULL, struct stream_xattr_config,
926 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
927 files_struct *fsp, const void *data,
928 size_t n, off_t offset)
930 struct stream_io *sio =
931 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
934 struct smb_filename *smb_fname_base = NULL;
937 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
940 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
943 if (!streams_xattr_recheck(sio)) {
947 /* Create an smb_filename with stream_name == NULL. */
948 smb_fname_base = synthetic_smb_fname(talloc_tos(),
952 fsp->fsp_name->flags);
953 if (smb_fname_base == NULL) {
958 status = get_ea_value(talloc_tos(), handle->conn, NULL,
959 smb_fname_base, sio->xattr_name, &ea);
960 if (!NT_STATUS_IS_OK(status)) {
964 if ((offset + n) > ea.value.length-1) {
967 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
971 TALLOC_FREE(ea.value.data);
976 ea.value.length = offset + n + 1;
977 ea.value.data[offset+n] = 0;
980 memcpy(ea.value.data + offset, data, n);
982 ret = SMB_VFS_SETXATTR(fsp->conn,
985 ea.value.data, ea.value.length, 0);
986 TALLOC_FREE(ea.value.data);
995 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
996 files_struct *fsp, void *data,
997 size_t n, off_t offset)
999 struct stream_io *sio =
1000 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1001 struct ea_struct ea;
1003 size_t length, overlap;
1004 struct smb_filename *smb_fname_base = NULL;
1006 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1007 (int)offset, (int)n));
1010 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1013 if (!streams_xattr_recheck(sio)) {
1017 /* Create an smb_filename with stream_name == NULL. */
1018 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1022 fsp->fsp_name->flags);
1023 if (smb_fname_base == NULL) {
1028 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1029 smb_fname_base, sio->xattr_name, &ea);
1030 if (!NT_STATUS_IS_OK(status)) {
1034 length = ea.value.length-1;
1036 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1039 /* Attempt to read past EOF. */
1040 if (length <= offset) {
1044 overlap = (offset + n) > length ? (length - offset) : n;
1045 memcpy(data, ea.value.data + offset, overlap);
1047 TALLOC_FREE(ea.value.data);
1051 struct streams_xattr_pread_state {
1053 struct vfs_aio_state vfs_aio_state;
1056 static void streams_xattr_pread_done(struct tevent_req *subreq);
1058 static struct tevent_req *streams_xattr_pread_send(
1059 struct vfs_handle_struct *handle,
1060 TALLOC_CTX *mem_ctx,
1061 struct tevent_context *ev,
1062 struct files_struct *fsp,
1064 size_t n, off_t offset)
1066 struct tevent_req *req = NULL;
1067 struct tevent_req *subreq = NULL;
1068 struct streams_xattr_pread_state *state = NULL;
1069 struct stream_io *sio =
1070 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1072 req = tevent_req_create(mem_ctx, &state,
1073 struct streams_xattr_pread_state);
1079 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1081 if (tevent_req_nomem(req, subreq)) {
1082 return tevent_req_post(req, ev);
1084 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1088 state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1089 if (state->nread != n) {
1090 if (state->nread != -1) {
1093 tevent_req_error(req, errno);
1094 return tevent_req_post(req, ev);
1097 tevent_req_done(req);
1098 return tevent_req_post(req, ev);
1101 static void streams_xattr_pread_done(struct tevent_req *subreq)
1103 struct tevent_req *req = tevent_req_callback_data(
1104 subreq, struct tevent_req);
1105 struct streams_xattr_pread_state *state = tevent_req_data(
1106 req, struct streams_xattr_pread_state);
1108 state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1109 TALLOC_FREE(subreq);
1111 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1114 tevent_req_done(req);
1117 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1118 struct vfs_aio_state *vfs_aio_state)
1120 struct streams_xattr_pread_state *state = tevent_req_data(
1121 req, struct streams_xattr_pread_state);
1123 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1127 *vfs_aio_state = state->vfs_aio_state;
1128 return state->nread;
1131 struct streams_xattr_pwrite_state {
1133 struct vfs_aio_state vfs_aio_state;
1136 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1138 static struct tevent_req *streams_xattr_pwrite_send(
1139 struct vfs_handle_struct *handle,
1140 TALLOC_CTX *mem_ctx,
1141 struct tevent_context *ev,
1142 struct files_struct *fsp,
1144 size_t n, off_t offset)
1146 struct tevent_req *req = NULL;
1147 struct tevent_req *subreq = NULL;
1148 struct streams_xattr_pwrite_state *state = NULL;
1149 struct stream_io *sio =
1150 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1152 req = tevent_req_create(mem_ctx, &state,
1153 struct streams_xattr_pwrite_state);
1159 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1161 if (tevent_req_nomem(req, subreq)) {
1162 return tevent_req_post(req, ev);
1164 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1168 state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1169 if (state->nwritten != n) {
1170 if (state->nwritten != -1) {
1173 tevent_req_error(req, errno);
1174 return tevent_req_post(req, ev);
1177 tevent_req_done(req);
1178 return tevent_req_post(req, ev);
1181 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1183 struct tevent_req *req = tevent_req_callback_data(
1184 subreq, struct tevent_req);
1185 struct streams_xattr_pwrite_state *state = tevent_req_data(
1186 req, struct streams_xattr_pwrite_state);
1188 state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1189 TALLOC_FREE(subreq);
1191 if (tevent_req_error(req, state->vfs_aio_state.error)) {
1194 tevent_req_done(req);
1197 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1198 struct vfs_aio_state *vfs_aio_state)
1200 struct streams_xattr_pwrite_state *state = tevent_req_data(
1201 req, struct streams_xattr_pwrite_state);
1203 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1207 *vfs_aio_state = state->vfs_aio_state;
1208 return state->nwritten;
1211 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1212 struct files_struct *fsp,
1217 struct ea_struct ea;
1219 struct stream_io *sio =
1220 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1221 struct smb_filename *smb_fname_base = NULL;
1223 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1224 fsp_str_dbg(fsp), (double)offset));
1227 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1230 if (!streams_xattr_recheck(sio)) {
1234 /* Create an smb_filename with stream_name == NULL. */
1235 smb_fname_base = synthetic_smb_fname(talloc_tos(),
1239 fsp->fsp_name->flags);
1240 if (smb_fname_base == NULL) {
1245 status = get_ea_value(talloc_tos(), handle->conn, NULL,
1246 smb_fname_base, sio->xattr_name, &ea);
1247 if (!NT_STATUS_IS_OK(status)) {
1251 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1255 TALLOC_FREE(ea.value.data);
1260 /* Did we expand ? */
1261 if (ea.value.length < offset + 1) {
1262 memset(&tmp[ea.value.length], '\0',
1263 offset + 1 - ea.value.length);
1266 ea.value.data = tmp;
1267 ea.value.length = offset + 1;
1268 ea.value.data[offset] = 0;
1270 ret = SMB_VFS_SETXATTR(fsp->conn,
1273 ea.value.data, ea.value.length, 0);
1274 TALLOC_FREE(ea.value.data);
1283 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1284 struct files_struct *fsp,
1289 struct stream_io *sio =
1290 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1292 DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1294 fsp_str_dbg(fsp), (double)offset, (double)len));
1297 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1300 if (!streams_xattr_recheck(sio)) {
1304 /* Let the pwrite code path handle it. */
1309 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1310 uid_t uid, gid_t gid)
1312 struct stream_io *sio =
1313 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1316 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1322 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1326 struct stream_io *sio =
1327 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1330 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1336 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1337 struct files_struct *fsp,
1342 struct stream_io *sio =
1343 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1346 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1353 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1354 struct files_struct *fsp,
1358 struct stream_io *sio =
1359 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1362 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1369 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1370 struct files_struct *fsp,
1373 struct stream_io *sio =
1374 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1377 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1384 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1385 struct files_struct *fsp,
1391 struct stream_io *sio =
1392 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1395 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1403 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1405 TALLOC_CTX *mem_ctx)
1407 struct stream_io *sio =
1408 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1411 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1414 return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1415 handle, fsp->base_fsp->fsp_name,
1416 SMB_ACL_TYPE_ACCESS, mem_ctx);
1419 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1423 struct stream_io *sio =
1424 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1427 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1433 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1435 TALLOC_CTX *mem_ctx,
1436 char **blob_description,
1439 struct stream_io *sio =
1440 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1443 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1444 blob_description, blob);
1447 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1448 handle, fsp->base_fsp->fsp_name, mem_ctx,
1449 blob_description, blob);
1452 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1454 uint32_t security_info,
1455 TALLOC_CTX *mem_ctx,
1456 struct security_descriptor **ppdesc)
1458 struct stream_io *sio =
1459 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1462 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1466 return SMB_VFS_NEXT_GET_NT_ACL(handle, fsp->base_fsp->fsp_name,
1467 security_info, mem_ctx, ppdesc);
1470 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1472 uint32_t security_info_sent,
1473 const struct security_descriptor *psd)
1475 struct stream_io *sio =
1476 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1479 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1480 security_info_sent, psd);
1483 return NT_STATUS_OK;
1486 struct streams_xattr_fsync_state {
1488 struct vfs_aio_state vfs_aio_state;
1491 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1493 static struct tevent_req *streams_xattr_fsync_send(
1494 struct vfs_handle_struct *handle,
1495 TALLOC_CTX *mem_ctx,
1496 struct tevent_context *ev,
1497 struct files_struct *fsp)
1499 struct tevent_req *req = NULL;
1500 struct tevent_req *subreq = NULL;
1501 struct streams_xattr_fsync_state *state = NULL;
1502 struct stream_io *sio =
1503 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1505 req = tevent_req_create(mem_ctx, &state,
1506 struct streams_xattr_fsync_state);
1512 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1513 if (tevent_req_nomem(req, subreq)) {
1514 return tevent_req_post(req, ev);
1516 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1521 * There's no pathname based sync variant and we don't have access to
1522 * the basefile handle, so we can't do anything here.
1525 tevent_req_done(req);
1526 return tevent_req_post(req, ev);
1529 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1531 struct tevent_req *req = tevent_req_callback_data(
1532 subreq, struct tevent_req);
1533 struct streams_xattr_fsync_state *state = tevent_req_data(
1534 req, struct streams_xattr_fsync_state);
1536 state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1537 TALLOC_FREE(subreq);
1538 if (state->ret != 0) {
1539 tevent_req_error(req, errno);
1543 tevent_req_done(req);
1546 static int streams_xattr_fsync_recv(struct tevent_req *req,
1547 struct vfs_aio_state *vfs_aio_state)
1549 struct streams_xattr_fsync_state *state = tevent_req_data(
1550 req, struct streams_xattr_fsync_state);
1552 if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1556 *vfs_aio_state = state->vfs_aio_state;
1560 static bool streams_xattr_lock(vfs_handle_struct *handle,
1567 struct stream_io *sio =
1568 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1571 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1577 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1584 struct stream_io *sio =
1585 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1588 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1589 pcount, ptype, ppid);
1596 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1598 uint32_t share_mode,
1599 uint32_t access_mask)
1601 struct stream_io *sio =
1602 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1605 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1606 share_mode, access_mask);
1612 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1616 struct stream_io *sio =
1617 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1620 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1626 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1628 struct lock_struct *plock)
1630 struct stream_io *sio =
1631 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1634 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1640 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1641 .fs_capabilities_fn = streams_xattr_fs_capabilities,
1642 .connect_fn = streams_xattr_connect,
1643 .open_fn = streams_xattr_open,
1644 .stat_fn = streams_xattr_stat,
1645 .fstat_fn = streams_xattr_fstat,
1646 .lstat_fn = streams_xattr_lstat,
1647 .pread_fn = streams_xattr_pread,
1648 .pwrite_fn = streams_xattr_pwrite,
1649 .pread_send_fn = streams_xattr_pread_send,
1650 .pread_recv_fn = streams_xattr_pread_recv,
1651 .pwrite_send_fn = streams_xattr_pwrite_send,
1652 .pwrite_recv_fn = streams_xattr_pwrite_recv,
1653 .unlink_fn = streams_xattr_unlink,
1654 .rename_fn = streams_xattr_rename,
1655 .ftruncate_fn = streams_xattr_ftruncate,
1656 .fallocate_fn = streams_xattr_fallocate,
1657 .streaminfo_fn = streams_xattr_streaminfo,
1659 .fsync_send_fn = streams_xattr_fsync_send,
1660 .fsync_recv_fn = streams_xattr_fsync_recv,
1662 .lock_fn = streams_xattr_lock,
1663 .getlock_fn = streams_xattr_getlock,
1664 .kernel_flock_fn = streams_xattr_kernel_flock,
1665 .linux_setlease_fn = streams_xattr_linux_setlease,
1666 .strict_lock_check_fn = streams_xattr_strict_lock_check,
1668 .fchown_fn = streams_xattr_fchown,
1669 .fchmod_fn = streams_xattr_fchmod,
1671 .fgetxattr_fn = streams_xattr_fgetxattr,
1672 .flistxattr_fn = streams_xattr_flistxattr,
1673 .fremovexattr_fn = streams_xattr_fremovexattr,
1674 .fsetxattr_fn = streams_xattr_fsetxattr,
1676 .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1677 .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1678 .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1680 .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1681 .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1685 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1687 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1688 &vfs_streams_xattr_fns);