s3:net: Fix a segfault in "net rpc trustdom list" for overlong domain names
[Samba.git] / source3 / modules / vfs_streams_depot.c
blob51cd56e8fb49a55b0e0715a1fa1277f81b08c0ed
1 /*
2 * Store streams in a separate subdirectory
4 * Copyright (C) Volker Lendecke, 2007
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_VFS
26 * Excerpt from a mail from tridge:
28 * Volker, what I'm thinking of is this:
29 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
30 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
32 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
33 * is the fsid/inode. "namedstreamX" is a file named after the stream
34 * name.
37 static uint32_t hash_fn(DATA_BLOB key)
39 uint32_t value; /* Used to compute the hash value. */
40 uint32_t i; /* Used to cycle through random values. */
42 /* Set the initial value from the key size. */
43 for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
44 value = (value + (key.data[i] << (i*5 % 24)));
46 return (1103515243 * value + 12345);
50 * With the hashing scheme based on the inode we need to protect against
51 * streams showing up on files with re-used inodes. This can happen if we
52 * create a stream directory from within Samba, and a local process or NFS
53 * client deletes the file without deleting the streams directory. When the
54 * inode is re-used and the stream directory is still around, the streams in
55 * there would be show up as belonging to the new file.
57 * There are several workarounds for this, probably the easiest one is on
58 * systems which have a true birthtime stat element: When the file has a later
59 * birthtime than the streams directory, then we have to recreate the
60 * directory.
62 * The other workaround is to somehow mark the file as generated by Samba with
63 * something that a NFS client would not do. The closest one is a special
64 * xattr value being set. On systems which do not support xattrs, it might be
65 * an option to put in a special ACL entry for a non-existing group.
68 #define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
70 static bool file_is_valid(vfs_handle_struct *handle, const char *path,
71 bool check_valid)
73 char buf;
75 if (!check_valid) {
76 return true;
79 DEBUG(10, ("file_is_valid (%s) called\n", path));
81 if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
82 &buf, sizeof(buf)) != sizeof(buf)) {
83 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
84 return false;
87 if (buf != '1') {
88 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
89 return false;
92 return true;
95 static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
96 bool check_valid)
98 char buf = '1';
99 int ret;
101 if (!check_valid) {
102 return true;
105 DEBUG(10, ("marking file %s as valid\n", path));
107 ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
108 &buf, sizeof(buf), 0);
110 if (ret == -1) {
111 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
112 return false;
115 return true;
119 * Given an smb_filename, determine the stream directory using the file's
120 * base_name.
122 static char *stream_dir(vfs_handle_struct *handle,
123 const struct smb_filename *smb_fname,
124 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
126 uint32_t hash;
127 struct smb_filename *smb_fname_hash = NULL;
128 char *result = NULL;
129 SMB_STRUCT_STAT base_sbuf_tmp;
130 uint8_t first, second;
131 char *tmp;
132 char *id_hex;
133 struct file_id id;
134 uint8 id_buf[16];
135 bool check_valid;
136 const char *rootdir;
137 NTSTATUS status;
139 check_valid = lp_parm_bool(SNUM(handle->conn),
140 "streams_depot", "check_valid", true);
142 tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
144 if (tmp == NULL) {
145 errno = ENOMEM;
146 goto fail;
149 rootdir = lp_parm_const_string(
150 SNUM(handle->conn), "streams_depot", "directory",
151 tmp);
153 /* Stat the base file if it hasn't already been done. */
154 if (base_sbuf == NULL) {
155 struct smb_filename *smb_fname_base = NULL;
157 status = create_synthetic_smb_fname(talloc_tos(),
158 smb_fname->base_name,
159 NULL, NULL,
160 &smb_fname_base);
161 if (!NT_STATUS_IS_OK(status)) {
162 errno = map_errno_from_nt_status(status);
163 goto fail;
165 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
166 TALLOC_FREE(smb_fname_base);
167 goto fail;
169 base_sbuf_tmp = smb_fname_base->st;
170 TALLOC_FREE(smb_fname_base);
171 } else {
172 base_sbuf_tmp = *base_sbuf;
175 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
177 push_file_id_16((char *)id_buf, &id);
179 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
181 first = hash & 0xff;
182 second = (hash >> 8) & 0xff;
184 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
186 if (id_hex == NULL) {
187 errno = ENOMEM;
188 goto fail;
191 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
192 first, second, id_hex);
194 TALLOC_FREE(id_hex);
196 if (result == NULL) {
197 errno = ENOMEM;
198 return NULL;
201 status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
202 &smb_fname_hash);
203 if (!NT_STATUS_IS_OK(status)) {
204 errno = map_errno_from_nt_status(status);
205 goto fail;
208 if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
209 struct smb_filename *smb_fname_new = NULL;
210 char *newname;
212 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
213 errno = EINVAL;
214 goto fail;
217 if (file_is_valid(handle, smb_fname->base_name, check_valid)) {
218 return result;
222 * Someone has recreated a file under an existing inode
223 * without deleting the streams directory. For now, just move
224 * it away.
227 again:
228 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
229 if (newname == NULL) {
230 errno = ENOMEM;
231 goto fail;
234 status = create_synthetic_smb_fname(talloc_tos(), newname,
235 NULL, NULL,
236 &smb_fname_new);
237 TALLOC_FREE(newname);
238 if (!NT_STATUS_IS_OK(status)) {
239 errno = map_errno_from_nt_status(status);
240 goto fail;
243 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
244 smb_fname_new) == -1) {
245 TALLOC_FREE(smb_fname_new);
246 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
247 goto again;
249 goto fail;
252 TALLOC_FREE(smb_fname_new);
255 if (!create_it) {
256 errno = ENOENT;
257 goto fail;
260 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
261 && (errno != EEXIST)) {
262 goto fail;
265 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
266 if (tmp == NULL) {
267 errno = ENOMEM;
268 goto fail;
271 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
272 && (errno != EEXIST)) {
273 goto fail;
276 TALLOC_FREE(tmp);
278 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
279 second);
280 if (tmp == NULL) {
281 errno = ENOMEM;
282 goto fail;
285 if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
286 && (errno != EEXIST)) {
287 goto fail;
290 TALLOC_FREE(tmp);
292 if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
293 && (errno != EEXIST)) {
294 goto fail;
297 if (!mark_file_valid(handle, smb_fname->base_name, check_valid)) {
298 goto fail;
301 TALLOC_FREE(smb_fname_hash);
302 return result;
304 fail:
305 TALLOC_FREE(smb_fname_hash);
306 TALLOC_FREE(result);
307 return NULL;
310 * Given a stream name, populate smb_fname_out with the actual location of the
311 * stream.
313 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
314 const struct smb_filename *smb_fname,
315 struct smb_filename **smb_fname_out,
316 bool create_dir)
318 char *dirname, *stream_fname;
319 const char *stype;
320 NTSTATUS status;
322 *smb_fname_out = NULL;
324 stype = strchr_m(smb_fname->stream_name + 1, ':');
326 if (stype) {
327 if (StrCaseCmp(stype, ":$DATA") != 0) {
328 return NT_STATUS_INVALID_PARAMETER;
332 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
334 if (dirname == NULL) {
335 status = map_nt_error_from_unix(errno);
336 goto fail;
339 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
340 smb_fname->stream_name);
342 if (stream_fname == NULL) {
343 status = NT_STATUS_NO_MEMORY;
344 goto fail;
347 if (stype == NULL) {
348 /* Append an explicit stream type if one wasn't specified. */
349 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
350 stream_fname);
351 if (stream_fname == NULL) {
352 status = NT_STATUS_NO_MEMORY;
353 goto fail;
355 } else {
356 /* Normalize the stream type to upercase. */
357 strupper_m(strrchr_m(stream_fname, ':') + 1);
360 DEBUG(10, ("stream filename = %s\n", stream_fname));
362 /* Create an smb_filename with stream_name == NULL. */
363 status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
364 NULL, smb_fname_out);
365 if (!NT_STATUS_IS_OK(status)) {
366 return status;
369 return NT_STATUS_OK;
371 fail:
372 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
373 TALLOC_FREE(*smb_fname_out);
374 return status;
377 static NTSTATUS walk_streams(vfs_handle_struct *handle,
378 struct smb_filename *smb_fname_base,
379 char **pdirname,
380 bool (*fn)(const char *dirname,
381 const char *dirent,
382 void *private_data),
383 void *private_data)
385 char *dirname;
386 SMB_STRUCT_DIR *dirhandle = NULL;
387 char *dirent = NULL;
389 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
390 false);
392 if (dirname == NULL) {
393 if (errno == ENOENT) {
395 * no stream around
397 return NT_STATUS_OK;
399 return map_nt_error_from_unix(errno);
402 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
404 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
406 if (dirhandle == NULL) {
407 TALLOC_FREE(dirname);
408 return map_nt_error_from_unix(errno);
411 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL)) != NULL) {
413 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
414 TALLOC_FREE(dirent);
415 continue;
418 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
420 if (!fn(dirname, dirent, private_data)) {
421 TALLOC_FREE(dirent);
422 break;
424 TALLOC_FREE(dirent);
427 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
429 if (pdirname != NULL) {
430 *pdirname = dirname;
432 else {
433 TALLOC_FREE(dirname);
436 return NT_STATUS_OK;
440 * Helper to stat/lstat the base file of an smb_fname. This will actually
441 * fills in the stat struct in smb_filename.
443 static int streams_depot_stat_base(vfs_handle_struct *handle,
444 struct smb_filename *smb_fname,
445 bool follow_links)
447 char *tmp_stream_name;
448 int result;
450 tmp_stream_name = smb_fname->stream_name;
451 smb_fname->stream_name = NULL;
452 if (follow_links) {
453 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
454 } else {
455 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
457 smb_fname->stream_name = tmp_stream_name;
458 return result;
461 static int streams_depot_stat(vfs_handle_struct *handle,
462 struct smb_filename *smb_fname)
464 struct smb_filename *smb_fname_stream = NULL;
465 NTSTATUS status;
466 int ret = -1;
468 DEBUG(10, ("streams_depot_stat called for [%s]\n",
469 smb_fname_str_dbg(smb_fname)));
471 if (!is_ntfs_stream_smb_fname(smb_fname)) {
472 return SMB_VFS_NEXT_STAT(handle, smb_fname);
475 /* If the default stream is requested, just stat the base file. */
476 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
477 return streams_depot_stat_base(handle, smb_fname, true);
480 /* Stat the actual stream now. */
481 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
482 false);
483 if (!NT_STATUS_IS_OK(status)) {
484 ret = -1;
485 errno = map_errno_from_nt_status(status);
486 goto done;
489 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
491 /* Update the original smb_fname with the stat info. */
492 smb_fname->st = smb_fname_stream->st;
493 done:
494 TALLOC_FREE(smb_fname_stream);
495 return ret;
500 static int streams_depot_lstat(vfs_handle_struct *handle,
501 struct smb_filename *smb_fname)
503 struct smb_filename *smb_fname_stream = NULL;
504 NTSTATUS status;
505 int ret = -1;
507 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
508 smb_fname_str_dbg(smb_fname)));
510 if (!is_ntfs_stream_smb_fname(smb_fname)) {
511 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
514 /* If the default stream is requested, just stat the base file. */
515 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
516 return streams_depot_stat_base(handle, smb_fname, false);
519 /* Stat the actual stream now. */
520 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
521 false);
522 if (!NT_STATUS_IS_OK(status)) {
523 ret = -1;
524 errno = map_errno_from_nt_status(status);
525 goto done;
528 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
530 done:
531 TALLOC_FREE(smb_fname_stream);
532 return ret;
535 static int streams_depot_open(vfs_handle_struct *handle,
536 struct smb_filename *smb_fname,
537 files_struct *fsp, int flags, mode_t mode)
539 struct smb_filename *smb_fname_stream = NULL;
540 struct smb_filename *smb_fname_base = NULL;
541 NTSTATUS status;
542 int ret = -1;
544 if (!is_ntfs_stream_smb_fname(smb_fname)) {
545 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
548 /* If the default stream is requested, just open the base file. */
549 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
550 char *tmp_stream_name;
552 tmp_stream_name = smb_fname->stream_name;
553 smb_fname->stream_name = NULL;
554 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
555 smb_fname->stream_name = tmp_stream_name;
557 return ret;
560 /* Ensure the base file still exists. */
561 status = create_synthetic_smb_fname(talloc_tos(),
562 smb_fname->base_name,
563 NULL, NULL,
564 &smb_fname_base);
565 if (!NT_STATUS_IS_OK(status)) {
566 ret = -1;
567 errno = map_errno_from_nt_status(status);
568 goto done;
571 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
572 if (ret == -1) {
573 goto done;
576 /* Determine the stream name, and then open it. */
577 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
578 if (!NT_STATUS_IS_OK(status)) {
579 ret = -1;
580 errno = map_errno_from_nt_status(status);
581 goto done;
584 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
586 done:
587 TALLOC_FREE(smb_fname_stream);
588 TALLOC_FREE(smb_fname_base);
589 return ret;
592 static int streams_depot_unlink(vfs_handle_struct *handle,
593 const struct smb_filename *smb_fname)
595 struct smb_filename *smb_fname_base = NULL;
596 NTSTATUS status;
597 int ret = -1;
599 DEBUG(10, ("streams_depot_unlink called for %s\n",
600 smb_fname_str_dbg(smb_fname)));
602 /* If there is a valid stream, just unlink the stream and return. */
603 if (is_ntfs_stream_smb_fname(smb_fname) &&
604 !is_ntfs_default_stream_smb_fname(smb_fname)) {
605 struct smb_filename *smb_fname_stream = NULL;
607 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
608 false);
609 if (!NT_STATUS_IS_OK(status)) {
610 errno = map_errno_from_nt_status(status);
611 return -1;
614 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
616 TALLOC_FREE(smb_fname_stream);
617 return ret;
621 * We potentially need to delete the per-inode streams directory
624 status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
625 NULL, NULL, &smb_fname_base);
626 if (!NT_STATUS_IS_OK(status)) {
627 errno = map_errno_from_nt_status(status);
628 return -1;
631 if (lp_posix_pathnames()) {
632 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
633 } else {
634 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
637 if (ret == -1) {
638 TALLOC_FREE(smb_fname_base);
639 return -1;
642 if (smb_fname_base->st.st_ex_nlink == 1) {
643 char *dirname = stream_dir(handle, smb_fname_base,
644 &smb_fname_base->st, false);
646 if (dirname != NULL) {
647 SMB_VFS_NEXT_RMDIR(handle, dirname);
649 TALLOC_FREE(dirname);
652 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
654 TALLOC_FREE(smb_fname_base);
655 return ret;
658 static int streams_depot_rename(vfs_handle_struct *handle,
659 const struct smb_filename *smb_fname_src,
660 const struct smb_filename *smb_fname_dst)
662 struct smb_filename *smb_fname_src_stream = NULL;
663 struct smb_filename *smb_fname_dst_stream = NULL;
664 struct smb_filename *smb_fname_dst_mod = NULL;
665 bool src_is_stream, dst_is_stream;
666 NTSTATUS status;
667 int ret = -1;
669 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
670 smb_fname_str_dbg(smb_fname_src),
671 smb_fname_str_dbg(smb_fname_dst)));
673 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
674 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
676 if (!src_is_stream && !dst_is_stream) {
677 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
678 smb_fname_dst);
681 /* for now don't allow renames from or to the default stream */
682 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
683 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
684 errno = ENOSYS;
685 goto done;
688 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
689 false);
690 if (!NT_STATUS_IS_OK(status)) {
691 errno = map_errno_from_nt_status(status);
692 goto done;
696 * Handle passing in a stream name without the base file. This is
697 * exercised by the NTRENAME streams rename path.
699 if (StrCaseCmp(smb_fname_dst->base_name, "./") == 0) {
700 status = create_synthetic_smb_fname(talloc_tos(),
701 smb_fname_src->base_name,
702 smb_fname_dst->stream_name,
703 NULL, &smb_fname_dst_mod);
704 if (!NT_STATUS_IS_OK(status)) {
705 errno = map_errno_from_nt_status(status);
706 goto done;
710 status = stream_smb_fname(handle, (smb_fname_dst_mod ?
711 smb_fname_dst_mod : smb_fname_dst),
712 &smb_fname_dst_stream, false);
713 if (!NT_STATUS_IS_OK(status)) {
714 errno = map_errno_from_nt_status(status);
715 goto done;
718 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
719 smb_fname_dst_stream);
721 done:
722 TALLOC_FREE(smb_fname_src_stream);
723 TALLOC_FREE(smb_fname_dst_stream);
724 TALLOC_FREE(smb_fname_dst_mod);
725 return ret;
728 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
729 struct stream_struct **streams,
730 const char *name, SMB_OFF_T size,
731 SMB_OFF_T alloc_size)
733 struct stream_struct *tmp;
735 tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
736 (*num_streams)+1);
737 if (tmp == NULL) {
738 return false;
741 tmp[*num_streams].name = talloc_strdup(tmp, name);
742 if (tmp[*num_streams].name == NULL) {
743 return false;
746 tmp[*num_streams].size = size;
747 tmp[*num_streams].alloc_size = alloc_size;
749 *streams = tmp;
750 *num_streams += 1;
751 return true;
754 struct streaminfo_state {
755 TALLOC_CTX *mem_ctx;
756 vfs_handle_struct *handle;
757 unsigned int num_streams;
758 struct stream_struct *streams;
759 NTSTATUS status;
762 static bool collect_one_stream(const char *dirname,
763 const char *dirent,
764 void *private_data)
766 struct streaminfo_state *state =
767 (struct streaminfo_state *)private_data;
768 struct smb_filename *smb_fname = NULL;
769 char *sname = NULL;
770 NTSTATUS status;
771 bool ret;
773 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
774 if (sname == NULL) {
775 state->status = NT_STATUS_NO_MEMORY;
776 ret = false;
777 goto out;
780 status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
781 NULL, &smb_fname);
782 if (!NT_STATUS_IS_OK(status)) {
783 state->status = status;
784 ret = false;
785 goto out;
788 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
789 DEBUG(10, ("Could not stat %s: %s\n", sname,
790 strerror(errno)));
791 ret = true;
792 goto out;
795 if (!add_one_stream(state->mem_ctx,
796 &state->num_streams, &state->streams,
797 dirent, smb_fname->st.st_ex_size,
798 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
799 &smb_fname->st))) {
800 state->status = NT_STATUS_NO_MEMORY;
801 ret = false;
802 goto out;
805 ret = true;
806 out:
807 TALLOC_FREE(sname);
808 TALLOC_FREE(smb_fname);
809 return ret;
812 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
813 struct files_struct *fsp,
814 const char *fname,
815 TALLOC_CTX *mem_ctx,
816 unsigned int *pnum_streams,
817 struct stream_struct **pstreams)
819 struct smb_filename *smb_fname_base = NULL;
820 int ret;
821 NTSTATUS status;
822 struct streaminfo_state state;
824 status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
825 &smb_fname_base);
826 if (!NT_STATUS_IS_OK(status)) {
827 return status;
830 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
831 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
833 else {
834 if (lp_posix_pathnames()) {
835 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
836 } else {
837 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
841 if (ret == -1) {
842 status = map_nt_error_from_unix(errno);
843 goto out;
846 state.streams = NULL;
847 state.num_streams = 0;
849 if (!S_ISDIR(smb_fname_base->st.st_ex_mode)) {
850 if (!add_one_stream(mem_ctx,
851 &state.num_streams, &state.streams,
852 "::$DATA", smb_fname_base->st.st_ex_size,
853 SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
854 &smb_fname_base->st))) {
855 status = NT_STATUS_NO_MEMORY;
856 goto out;
860 state.mem_ctx = mem_ctx;
861 state.handle = handle;
862 state.status = NT_STATUS_OK;
864 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
865 &state);
867 if (!NT_STATUS_IS_OK(status)) {
868 TALLOC_FREE(state.streams);
869 goto out;
872 if (!NT_STATUS_IS_OK(state.status)) {
873 TALLOC_FREE(state.streams);
874 status = state.status;
875 goto out;
878 *pnum_streams = state.num_streams;
879 *pstreams = state.streams;
880 status = NT_STATUS_OK;
882 out:
883 TALLOC_FREE(smb_fname_base);
884 return status;
887 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
888 enum timestamp_set_resolution *p_ts_res)
890 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
893 static struct vfs_fn_pointers vfs_streams_depot_fns = {
894 .fs_capabilities = streams_depot_fs_capabilities,
895 .open = streams_depot_open,
896 .stat = streams_depot_stat,
897 .lstat = streams_depot_lstat,
898 .unlink = streams_depot_unlink,
899 .rename = streams_depot_rename,
900 .streaminfo = streams_depot_streaminfo,
903 NTSTATUS vfs_streams_depot_init(void);
904 NTSTATUS vfs_streams_depot_init(void)
906 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
907 &vfs_streams_depot_fns);