s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source3 / modules / vfs_streams_xattr.c
blobb68fa02a354af98e4980108bd269e13e71327011
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"
26 #undef DBGC_CLASS
27 #define DBGC_CLASS DBGC_VFS
29 struct stream_io {
30 char *base;
31 char *xattr_name;
32 void *fsp_name_ptr;
33 files_struct *fsp;
34 vfs_handle_struct *handle;
37 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
39 struct MD5Context ctx;
40 unsigned char hash[16];
41 SMB_INO_T result;
42 char *upper_sname;
44 DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
45 (unsigned long)sbuf->st_ex_dev,
46 (unsigned long)sbuf->st_ex_ino, sname));
48 upper_sname = talloc_strdup_upper(talloc_tos(), sname);
49 SMB_ASSERT(upper_sname != NULL);
51 MD5Init(&ctx);
52 MD5Update(&ctx, (unsigned char *)&(sbuf->st_ex_dev),
53 sizeof(sbuf->st_ex_dev));
54 MD5Update(&ctx, (unsigned char *)&(sbuf->st_ex_ino),
55 sizeof(sbuf->st_ex_ino));
56 MD5Update(&ctx, (unsigned char *)upper_sname,
57 talloc_get_size(upper_sname)-1);
58 MD5Final(hash, &ctx);
60 TALLOC_FREE(upper_sname);
62 /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
63 memcpy(&result, hash, sizeof(result));
65 DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
67 return result;
70 static ssize_t get_xattr_size(connection_struct *conn,
71 files_struct *fsp,
72 const char *fname,
73 const char *xattr_name)
75 NTSTATUS status;
76 struct ea_struct ea;
77 ssize_t result;
79 status = get_ea_value(talloc_tos(), conn, fsp, fname,
80 xattr_name, &ea);
82 if (!NT_STATUS_IS_OK(status)) {
83 return -1;
86 result = ea.value.length-1;
87 TALLOC_FREE(ea.value.data);
88 return result;
91 /**
92 * Given a stream name, populate xattr_name with the xattr name to use for
93 * accessing the stream.
95 static NTSTATUS streams_xattr_get_name(TALLOC_CTX *ctx,
96 const char *stream_name,
97 char **xattr_name)
99 char *stype;
101 stype = strchr_m(stream_name + 1, ':');
103 *xattr_name = talloc_asprintf(ctx, "%s%s",
104 SAMBA_XATTR_DOSSTREAM_PREFIX,
105 stream_name + 1);
106 if (*xattr_name == NULL) {
107 return NT_STATUS_NO_MEMORY;
110 if (stype == NULL) {
111 /* Append an explicit stream type if one wasn't specified. */
112 *xattr_name = talloc_asprintf(ctx, "%s:$DATA",
113 *xattr_name);
114 if (*xattr_name == NULL) {
115 return NT_STATUS_NO_MEMORY;
117 } else {
118 /* Normalize the stream type to upercase. */
119 strupper_m(strrchr_m(*xattr_name, ':') + 1);
122 DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
123 stream_name));
125 return NT_STATUS_OK;
128 static bool streams_xattr_recheck(struct stream_io *sio)
130 NTSTATUS status;
131 char *xattr_name = NULL;
133 if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
134 return true;
137 if (sio->fsp->fsp_name->stream_name == NULL) {
138 /* how can this happen */
139 errno = EINVAL;
140 return false;
143 status = streams_xattr_get_name(talloc_tos(),
144 sio->fsp->fsp_name->stream_name,
145 &xattr_name);
146 if (!NT_STATUS_IS_OK(status)) {
147 return false;
150 TALLOC_FREE(sio->xattr_name);
151 TALLOC_FREE(sio->base);
152 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
153 xattr_name);
154 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
155 sio->fsp->fsp_name->base_name);
156 sio->fsp_name_ptr = sio->fsp->fsp_name;
158 TALLOC_FREE(xattr_name);
160 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
161 return false;
164 return true;
168 * Helper to stat/lstat the base file of an smb_fname.
170 static int streams_xattr_stat_base(vfs_handle_struct *handle,
171 struct smb_filename *smb_fname,
172 bool follow_links)
174 char *tmp_stream_name;
175 int result;
177 tmp_stream_name = smb_fname->stream_name;
178 smb_fname->stream_name = NULL;
179 if (follow_links) {
180 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
181 } else {
182 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
184 smb_fname->stream_name = tmp_stream_name;
185 return result;
188 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
189 SMB_STRUCT_STAT *sbuf)
191 struct smb_filename *smb_fname_base = NULL;
192 NTSTATUS status;
193 int ret = -1;
194 struct stream_io *io = (struct stream_io *)
195 VFS_FETCH_FSP_EXTENSION(handle, fsp);
197 DEBUG(10, ("streams_xattr_fstat called for %d\n", fsp->fh->fd));
199 if (io == NULL || fsp->base_fsp == NULL) {
200 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
203 if (!streams_xattr_recheck(io)) {
204 return -1;
207 /* Create an smb_filename with stream_name == NULL. */
208 status = create_synthetic_smb_fname(talloc_tos(),
209 io->base,
210 NULL, NULL,
211 &smb_fname_base);
212 if (!NT_STATUS_IS_OK(status)) {
213 errno = map_errno_from_nt_status(status);
214 return -1;
217 if (lp_posix_pathnames()) {
218 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
219 } else {
220 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
222 *sbuf = smb_fname_base->st;
223 TALLOC_FREE(smb_fname_base);
225 if (ret == -1) {
226 return -1;
229 sbuf->st_ex_size = get_xattr_size(handle->conn, fsp->base_fsp,
230 io->base, io->xattr_name);
231 if (sbuf->st_ex_size == -1) {
232 return -1;
235 DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
237 sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
238 sbuf->st_ex_mode &= ~S_IFMT;
239 sbuf->st_ex_mode |= S_IFREG;
240 sbuf->st_ex_blocks = sbuf->st_ex_size % STAT_ST_BLOCKSIZE + 1;
242 return 0;
245 static int streams_xattr_stat(vfs_handle_struct *handle,
246 struct smb_filename *smb_fname)
248 NTSTATUS status;
249 int result = -1;
250 char *xattr_name = NULL;
252 if (!is_ntfs_stream_smb_fname(smb_fname)) {
253 return SMB_VFS_NEXT_STAT(handle, smb_fname);
256 /* If the default stream is requested, just stat the base file. */
257 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
258 return streams_xattr_stat_base(handle, smb_fname, true);
261 /* Populate the stat struct with info from the base file. */
262 if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
263 return -1;
266 /* Derive the xattr name to lookup. */
267 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
268 &xattr_name);
269 if (!NT_STATUS_IS_OK(status)) {
270 errno = map_errno_from_nt_status(status);
271 return -1;
274 /* Augment the base file's stat information before returning. */
275 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
276 smb_fname->base_name,
277 xattr_name);
278 if (smb_fname->st.st_ex_size == -1) {
279 errno = ENOENT;
280 result = -1;
281 goto fail;
284 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
285 smb_fname->st.st_ex_mode &= ~S_IFMT;
286 smb_fname->st.st_ex_mode |= S_IFREG;
287 smb_fname->st.st_ex_blocks =
288 smb_fname->st.st_ex_size % STAT_ST_BLOCKSIZE + 1;
290 result = 0;
291 fail:
292 TALLOC_FREE(xattr_name);
293 return result;
296 static int streams_xattr_lstat(vfs_handle_struct *handle,
297 struct smb_filename *smb_fname)
299 NTSTATUS status;
300 int result = -1;
301 char *xattr_name = NULL;
303 if (!is_ntfs_stream_smb_fname(smb_fname)) {
304 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
307 /* If the default stream is requested, just stat the base file. */
308 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
309 return streams_xattr_stat_base(handle, smb_fname, false);
312 /* Populate the stat struct with info from the base file. */
313 if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
314 return -1;
317 /* Derive the xattr name to lookup. */
318 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
319 &xattr_name);
320 if (!NT_STATUS_IS_OK(status)) {
321 errno = map_errno_from_nt_status(status);
322 return -1;
325 /* Augment the base file's stat information before returning. */
326 smb_fname->st.st_ex_size = get_xattr_size(handle->conn, NULL,
327 smb_fname->base_name,
328 xattr_name);
329 if (smb_fname->st.st_ex_size == -1) {
330 errno = ENOENT;
331 result = -1;
332 goto fail;
335 smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
336 smb_fname->st.st_ex_mode &= ~S_IFMT;
337 smb_fname->st.st_ex_mode |= S_IFREG;
338 smb_fname->st.st_ex_blocks =
339 smb_fname->st.st_ex_size % STAT_ST_BLOCKSIZE + 1;
341 result = 0;
343 fail:
344 TALLOC_FREE(xattr_name);
345 return result;
348 static int streams_xattr_open(vfs_handle_struct *handle,
349 struct smb_filename *smb_fname,
350 files_struct *fsp, int flags, mode_t mode)
352 NTSTATUS status;
353 struct smb_filename *smb_fname_base = NULL;
354 struct stream_io *sio;
355 struct ea_struct ea;
356 char *xattr_name = NULL;
357 int baseflags;
358 int hostfd = -1;
360 DEBUG(10, ("streams_xattr_open called for %s\n",
361 smb_fname_str_dbg(smb_fname)));
363 if (!is_ntfs_stream_smb_fname(smb_fname)) {
364 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
367 /* If the default stream is requested, just open the base file. */
368 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
369 char *tmp_stream_name;
370 int ret;
372 tmp_stream_name = smb_fname->stream_name;
373 smb_fname->stream_name = NULL;
375 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
377 smb_fname->stream_name = tmp_stream_name;
379 return ret;
382 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
383 &xattr_name);
384 if (!NT_STATUS_IS_OK(status)) {
385 errno = map_errno_from_nt_status(status);
386 goto fail;
389 /* Create an smb_filename with stream_name == NULL. */
390 status = create_synthetic_smb_fname(talloc_tos(),
391 smb_fname->base_name,
392 NULL, NULL,
393 &smb_fname_base);
394 if (!NT_STATUS_IS_OK(status)) {
395 errno = map_errno_from_nt_status(status);
396 goto fail;
400 * We use baseflags to turn off nasty side-effects when opening the
401 * underlying file.
403 baseflags = flags;
404 baseflags &= ~O_TRUNC;
405 baseflags &= ~O_EXCL;
406 baseflags &= ~O_CREAT;
408 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname_base, fsp,
409 baseflags, mode);
411 TALLOC_FREE(smb_fname_base);
413 /* It is legit to open a stream on a directory, but the base
414 * fd has to be read-only.
416 if ((hostfd == -1) && (errno == EISDIR)) {
417 baseflags &= ~O_ACCMODE;
418 baseflags |= O_RDONLY;
419 hostfd = SMB_VFS_OPEN(handle->conn, smb_fname, fsp, baseflags,
420 mode);
423 if (hostfd == -1) {
424 goto fail;
427 status = get_ea_value(talloc_tos(), handle->conn, NULL,
428 smb_fname->base_name, xattr_name, &ea);
430 DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
432 if (!NT_STATUS_IS_OK(status)
433 && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
435 * The base file is not there. This is an error even if we got
436 * O_CREAT, the higher levels should have created the base
437 * file for us.
439 DEBUG(10, ("streams_xattr_open: base file %s not around, "
440 "returning ENOENT\n", smb_fname->base_name));
441 errno = ENOENT;
442 goto fail;
445 if (!NT_STATUS_IS_OK(status)) {
447 * The attribute does not exist
450 if (flags & O_CREAT) {
452 * Darn, xattrs need at least 1 byte
454 char null = '\0';
456 DEBUG(10, ("creating attribute %s on file %s\n",
457 xattr_name, smb_fname->base_name));
459 if (fsp->base_fsp->fh->fd != -1) {
460 if (SMB_VFS_FSETXATTR(
461 fsp->base_fsp, xattr_name,
462 &null, sizeof(null),
463 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
464 goto fail;
466 } else {
467 if (SMB_VFS_SETXATTR(
468 handle->conn, smb_fname->base_name,
469 xattr_name, &null, sizeof(null),
470 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
471 goto fail;
477 if (flags & O_TRUNC) {
478 char null = '\0';
479 if (fsp->base_fsp->fh->fd != -1) {
480 if (SMB_VFS_FSETXATTR(
481 fsp->base_fsp, xattr_name,
482 &null, sizeof(null),
483 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
484 goto fail;
486 } else {
487 if (SMB_VFS_SETXATTR(
488 handle->conn, smb_fname->base_name,
489 xattr_name, &null, sizeof(null),
490 flags & O_EXCL ? XATTR_CREATE : 0) == -1) {
491 goto fail;
496 sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
497 struct stream_io,
498 NULL);
499 if (sio == NULL) {
500 errno = ENOMEM;
501 goto fail;
504 sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
505 xattr_name);
506 sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
507 smb_fname->base_name);
508 sio->fsp_name_ptr = fsp->fsp_name;
509 sio->handle = handle;
510 sio->fsp = fsp;
512 if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
513 errno = ENOMEM;
514 goto fail;
517 return hostfd;
519 fail:
520 if (hostfd >= 0) {
522 * BUGBUGBUG -- we would need to call fd_close_posix here, but
523 * we don't have a full fsp yet
525 SMB_VFS_CLOSE(fsp);
528 return -1;
531 static int streams_xattr_unlink(vfs_handle_struct *handle,
532 const struct smb_filename *smb_fname)
534 NTSTATUS status;
535 int ret = -1;
536 char *xattr_name;
538 if (!is_ntfs_stream_smb_fname(smb_fname)) {
539 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
542 /* If the default stream is requested, just open the base file. */
543 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
544 struct smb_filename *smb_fname_base = NULL;
546 status = copy_smb_filename(talloc_tos(), smb_fname,
547 &smb_fname_base);
548 if (!NT_STATUS_IS_OK(status)) {
549 errno = map_errno_from_nt_status(status);
550 return -1;
553 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
555 TALLOC_FREE(smb_fname_base);
556 return ret;
559 status = streams_xattr_get_name(talloc_tos(), smb_fname->stream_name,
560 &xattr_name);
561 if (!NT_STATUS_IS_OK(status)) {
562 errno = map_errno_from_nt_status(status);
563 goto fail;
566 ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname->base_name, xattr_name);
568 if ((ret == -1) && (errno == ENOATTR)) {
569 errno = ENOENT;
570 goto fail;
573 ret = 0;
575 fail:
576 TALLOC_FREE(xattr_name);
577 return ret;
580 static int streams_xattr_rename(vfs_handle_struct *handle,
581 const struct smb_filename *smb_fname_src,
582 const struct smb_filename *smb_fname_dst)
584 NTSTATUS status;
585 int ret = -1;
586 char *src_xattr_name = NULL;
587 char *dst_xattr_name = NULL;
588 bool src_is_stream, dst_is_stream;
589 ssize_t oret;
590 ssize_t nret;
591 struct ea_struct ea;
593 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
594 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
596 if (!src_is_stream && !dst_is_stream) {
597 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
598 smb_fname_dst);
601 /* For now don't allow renames from or to the default stream. */
602 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
603 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
604 errno = ENOSYS;
605 goto done;
608 /* Don't rename if the streams are identical. */
609 if (StrCaseCmp(smb_fname_src->stream_name,
610 smb_fname_dst->stream_name) == 0) {
611 goto done;
614 /* Get the xattr names. */
615 status = streams_xattr_get_name(talloc_tos(),
616 smb_fname_src->stream_name,
617 &src_xattr_name);
618 if (!NT_STATUS_IS_OK(status)) {
619 errno = map_errno_from_nt_status(status);
620 goto fail;
622 status = streams_xattr_get_name(talloc_tos(),
623 smb_fname_dst->stream_name,
624 &dst_xattr_name);
625 if (!NT_STATUS_IS_OK(status)) {
626 errno = map_errno_from_nt_status(status);
627 goto fail;
630 /* read the old stream */
631 status = get_ea_value(talloc_tos(), handle->conn, NULL,
632 smb_fname_src->base_name, src_xattr_name, &ea);
633 if (!NT_STATUS_IS_OK(status)) {
634 errno = ENOENT;
635 goto fail;
638 /* (over)write the new stream */
639 nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src->base_name,
640 dst_xattr_name, ea.value.data, ea.value.length,
642 if (nret < 0) {
643 if (errno == ENOATTR) {
644 errno = ENOENT;
646 goto fail;
649 /* remove the old stream */
650 oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src->base_name,
651 src_xattr_name);
652 if (oret < 0) {
653 if (errno == ENOATTR) {
654 errno = ENOENT;
656 goto fail;
659 done:
660 errno = 0;
661 ret = 0;
662 fail:
663 TALLOC_FREE(src_xattr_name);
664 TALLOC_FREE(dst_xattr_name);
665 return ret;
668 static NTSTATUS walk_xattr_streams(connection_struct *conn, files_struct *fsp,
669 const char *fname,
670 bool (*fn)(struct ea_struct *ea,
671 void *private_data),
672 void *private_data)
674 NTSTATUS status;
675 char **names;
676 size_t i, num_names;
677 size_t prefix_len = strlen(SAMBA_XATTR_DOSSTREAM_PREFIX);
679 status = get_ea_names_from_file(talloc_tos(), conn, fsp, fname,
680 &names, &num_names);
681 if (!NT_STATUS_IS_OK(status)) {
682 return status;
685 for (i=0; i<num_names; i++) {
686 struct ea_struct ea;
688 if (strncmp(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
689 prefix_len) != 0) {
690 continue;
693 status = get_ea_value(names, conn, fsp, fname, names[i], &ea);
694 if (!NT_STATUS_IS_OK(status)) {
695 DEBUG(10, ("Could not get ea %s for file %s: %s\n",
696 names[i], fname, nt_errstr(status)));
697 continue;
700 ea.name = talloc_asprintf(ea.value.data, ":%s",
701 names[i] + prefix_len);
702 if (ea.name == NULL) {
703 DEBUG(0, ("talloc failed\n"));
704 continue;
707 if (!fn(&ea, private_data)) {
708 TALLOC_FREE(ea.value.data);
709 return NT_STATUS_OK;
712 TALLOC_FREE(ea.value.data);
715 TALLOC_FREE(names);
716 return NT_STATUS_OK;
719 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
720 struct stream_struct **streams,
721 const char *name, SMB_OFF_T size,
722 SMB_OFF_T alloc_size)
724 struct stream_struct *tmp;
726 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
727 (*num_streams)+1);
728 if (tmp == NULL) {
729 return false;
732 tmp[*num_streams].name = talloc_strdup(tmp, name);
733 if (tmp[*num_streams].name == NULL) {
734 return false;
737 tmp[*num_streams].size = size;
738 tmp[*num_streams].alloc_size = alloc_size;
740 *streams = tmp;
741 *num_streams += 1;
742 return true;
745 struct streaminfo_state {
746 TALLOC_CTX *mem_ctx;
747 vfs_handle_struct *handle;
748 unsigned int num_streams;
749 struct stream_struct *streams;
750 NTSTATUS status;
753 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
755 struct streaminfo_state *state =
756 (struct streaminfo_state *)private_data;
758 if (!add_one_stream(state->mem_ctx,
759 &state->num_streams, &state->streams,
760 ea->name, ea->value.length-1,
761 smb_roundup(state->handle->conn,
762 ea->value.length-1))) {
763 state->status = NT_STATUS_NO_MEMORY;
764 return false;
767 return true;
770 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
771 struct files_struct *fsp,
772 const char *fname,
773 TALLOC_CTX *mem_ctx,
774 unsigned int *pnum_streams,
775 struct stream_struct **pstreams)
777 SMB_STRUCT_STAT sbuf;
778 int ret;
779 NTSTATUS status;
780 struct streaminfo_state state;
782 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
783 ret = SMB_VFS_FSTAT(fsp, &sbuf);
785 else {
786 struct smb_filename *smb_fname = NULL;
787 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL,
788 NULL, &smb_fname);
789 if (!NT_STATUS_IS_OK(status)) {
790 return status;
792 if (lp_posix_pathnames()) {
793 ret = SMB_VFS_LSTAT(handle->conn, smb_fname);
794 } else {
795 ret = SMB_VFS_STAT(handle->conn, smb_fname);
797 sbuf = smb_fname->st;
798 TALLOC_FREE(smb_fname);
801 if (ret == -1) {
802 return map_nt_error_from_unix(errno);
805 state.streams = NULL;
806 state.num_streams = 0;
808 if (!S_ISDIR(sbuf.st_ex_mode)) {
809 if (!add_one_stream(mem_ctx,
810 &state.num_streams, &state.streams,
811 "::$DATA", sbuf.st_ex_size,
812 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
813 &sbuf))) {
814 return NT_STATUS_NO_MEMORY;
818 state.mem_ctx = mem_ctx;
819 state.handle = handle;
820 state.status = NT_STATUS_OK;
822 status = walk_xattr_streams(handle->conn, fsp, fname,
823 collect_one_stream, &state);
825 if (!NT_STATUS_IS_OK(status)) {
826 TALLOC_FREE(state.streams);
827 return status;
830 if (!NT_STATUS_IS_OK(state.status)) {
831 TALLOC_FREE(state.streams);
832 return state.status;
835 *pnum_streams = state.num_streams;
836 *pstreams = state.streams;
837 return NT_STATUS_OK;
840 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
841 enum timestamp_set_resolution *p_ts_res)
843 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
846 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
847 files_struct *fsp, const void *data,
848 size_t n, SMB_OFF_T offset)
850 struct stream_io *sio =
851 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
852 struct ea_struct ea;
853 NTSTATUS status;
854 int ret;
856 DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
858 if (sio == NULL) {
859 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
862 if (!streams_xattr_recheck(sio)) {
863 return -1;
866 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
867 sio->base, sio->xattr_name, &ea);
868 if (!NT_STATUS_IS_OK(status)) {
869 return -1;
872 if ((offset + n) > ea.value.length-1) {
873 uint8 *tmp;
875 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
876 offset + n + 1);
878 if (tmp == NULL) {
879 TALLOC_FREE(ea.value.data);
880 errno = ENOMEM;
881 return -1;
883 ea.value.data = tmp;
884 ea.value.length = offset + n + 1;
885 ea.value.data[offset+n] = 0;
888 memcpy(ea.value.data + offset, data, n);
890 if (fsp->base_fsp->fh->fd != -1) {
891 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
892 sio->xattr_name,
893 ea.value.data, ea.value.length, 0);
894 } else {
895 ret = SMB_VFS_SETXATTR(fsp->conn,
896 fsp->base_fsp->fsp_name->base_name,
897 sio->xattr_name,
898 ea.value.data, ea.value.length, 0);
900 TALLOC_FREE(ea.value.data);
902 if (ret == -1) {
903 return -1;
906 return n;
909 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
910 files_struct *fsp, void *data,
911 size_t n, SMB_OFF_T offset)
913 struct stream_io *sio =
914 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
915 struct ea_struct ea;
916 NTSTATUS status;
917 size_t length, overlap;
919 DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
920 (int)offset, (int)n));
922 if (sio == NULL) {
923 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
926 if (!streams_xattr_recheck(sio)) {
927 return -1;
930 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
931 sio->base, sio->xattr_name, &ea);
932 if (!NT_STATUS_IS_OK(status)) {
933 return -1;
936 length = ea.value.length-1;
938 DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
939 (int)length));
941 /* Attempt to read past EOF. */
942 if (length <= offset) {
943 return 0;
946 overlap = (offset + n) > length ? (length - offset) : n;
947 memcpy(data, ea.value.data + offset, overlap);
949 TALLOC_FREE(ea.value.data);
950 return overlap;
953 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
954 struct files_struct *fsp,
955 SMB_OFF_T offset)
957 int ret;
958 uint8 *tmp;
959 struct ea_struct ea;
960 NTSTATUS status;
961 struct stream_io *sio =
962 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
964 DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
965 fsp_str_dbg(fsp), (double)offset));
967 if (sio == NULL) {
968 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
971 if (!streams_xattr_recheck(sio)) {
972 return -1;
975 status = get_ea_value(talloc_tos(), handle->conn, fsp->base_fsp,
976 sio->base, sio->xattr_name, &ea);
977 if (!NT_STATUS_IS_OK(status)) {
978 return -1;
981 tmp = TALLOC_REALLOC_ARRAY(talloc_tos(), ea.value.data, uint8,
982 offset + 1);
984 if (tmp == NULL) {
985 TALLOC_FREE(ea.value.data);
986 errno = ENOMEM;
987 return -1;
990 /* Did we expand ? */
991 if (ea.value.length < offset + 1) {
992 memset(&tmp[ea.value.length], '\0',
993 offset + 1 - ea.value.length);
996 ea.value.data = tmp;
997 ea.value.length = offset + 1;
998 ea.value.data[offset] = 0;
1000 if (fsp->base_fsp->fh->fd != -1) {
1001 ret = SMB_VFS_FSETXATTR(fsp->base_fsp,
1002 sio->xattr_name,
1003 ea.value.data, ea.value.length, 0);
1004 } else {
1005 ret = SMB_VFS_SETXATTR(fsp->conn,
1006 fsp->base_fsp->fsp_name->base_name,
1007 sio->xattr_name,
1008 ea.value.data, ea.value.length, 0);
1011 TALLOC_FREE(ea.value.data);
1013 if (ret == -1) {
1014 return -1;
1017 return 0;
1020 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1021 .fs_capabilities = streams_xattr_fs_capabilities,
1022 .open = streams_xattr_open,
1023 .stat = streams_xattr_stat,
1024 .fstat = streams_xattr_fstat,
1025 .lstat = streams_xattr_lstat,
1026 .pread = streams_xattr_pread,
1027 .pwrite = streams_xattr_pwrite,
1028 .unlink = streams_xattr_unlink,
1029 .rename = streams_xattr_rename,
1030 .ftruncate = streams_xattr_ftruncate,
1031 .streaminfo = streams_xattr_streaminfo,
1034 NTSTATUS vfs_streams_xattr_init(void);
1035 NTSTATUS vfs_streams_xattr_init(void)
1037 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1038 &vfs_streams_xattr_fns);