gssapi: check for gss_acquire_cred_from
[Samba.git] / source3 / modules / vfs_streams_depot.c
blobaa54b8d9a2183e0a248cab2c39ce1f8ad4c27c3d
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"
21 #include "smbd/smbd.h"
22 #include "system/filesys.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_VFS
28 * Excerpt from a mail from tridge:
30 * Volker, what I'm thinking of is this:
31 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
32 * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
34 * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
35 * is the fsid/inode. "namedstreamX" is a file named after the stream
36 * name.
39 static uint32_t hash_fn(DATA_BLOB key)
41 uint32_t value; /* Used to compute the hash value. */
42 uint32_t i; /* Used to cycle through random values. */
44 /* Set the initial value from the key size. */
45 for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
46 value = (value + (key.data[i] << (i*5 % 24)));
48 return (1103515243 * value + 12345);
52 * With the hashing scheme based on the inode we need to protect against
53 * streams showing up on files with re-used inodes. This can happen if we
54 * create a stream directory from within Samba, and a local process or NFS
55 * client deletes the file without deleting the streams directory. When the
56 * inode is re-used and the stream directory is still around, the streams in
57 * there would be show up as belonging to the new file.
59 * There are several workarounds for this, probably the easiest one is on
60 * systems which have a true birthtime stat element: When the file has a later
61 * birthtime than the streams directory, then we have to recreate the
62 * directory.
64 * The other workaround is to somehow mark the file as generated by Samba with
65 * something that a NFS client would not do. The closest one is a special
66 * xattr value being set. On systems which do not support xattrs, it might be
67 * an option to put in a special ACL entry for a non-existing group.
70 static bool file_is_valid(vfs_handle_struct *handle, const char *path)
72 char buf;
74 DEBUG(10, ("file_is_valid (%s) called\n", path));
76 if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
77 &buf, sizeof(buf)) != sizeof(buf)) {
78 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
79 return false;
82 if (buf != '1') {
83 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
84 return false;
87 return true;
90 static bool mark_file_valid(vfs_handle_struct *handle, const char *path)
92 char buf = '1';
93 int ret;
95 DEBUG(10, ("marking file %s as valid\n", path));
97 ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
98 &buf, sizeof(buf), 0);
100 if (ret == -1) {
101 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
102 return false;
105 return true;
109 * Given an smb_filename, determine the stream directory using the file's
110 * base_name.
112 static char *stream_dir(vfs_handle_struct *handle,
113 const struct smb_filename *smb_fname,
114 const SMB_STRUCT_STAT *base_sbuf, bool create_it)
116 uint32_t hash;
117 struct smb_filename *smb_fname_hash = NULL;
118 char *result = NULL;
119 SMB_STRUCT_STAT base_sbuf_tmp;
120 uint8_t first, second;
121 char *tmp;
122 char *id_hex;
123 struct file_id id;
124 uint8_t id_buf[16];
125 bool check_valid;
126 char *rootdir = NULL;
127 struct smb_filename *rootdir_fname = NULL;
128 struct smb_filename *tmp_fname = NULL;
130 check_valid = lp_parm_bool(SNUM(handle->conn),
131 "streams_depot", "check_valid", true);
133 tmp = talloc_asprintf(talloc_tos(), "%s/.streams",
134 handle->conn->connectpath);
136 if (tmp == NULL) {
137 errno = ENOMEM;
138 goto fail;
141 rootdir = lp_parm_talloc_string(talloc_tos(),
142 SNUM(handle->conn), "streams_depot", "directory",
143 tmp);
144 if (rootdir == NULL) {
145 errno = ENOMEM;
146 goto fail;
149 rootdir_fname = synthetic_smb_fname(talloc_tos(),
150 rootdir,
151 NULL,
152 NULL,
153 smb_fname->flags);
154 if (rootdir_fname == NULL) {
155 errno = ENOMEM;
156 goto fail;
159 /* Stat the base file if it hasn't already been done. */
160 if (base_sbuf == NULL) {
161 struct smb_filename *smb_fname_base;
163 smb_fname_base = synthetic_smb_fname(
164 talloc_tos(),
165 smb_fname->base_name,
166 NULL,
167 NULL,
168 smb_fname->flags);
169 if (smb_fname_base == NULL) {
170 errno = ENOMEM;
171 goto fail;
173 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
174 TALLOC_FREE(smb_fname_base);
175 goto fail;
177 base_sbuf_tmp = smb_fname_base->st;
178 TALLOC_FREE(smb_fname_base);
179 } else {
180 base_sbuf_tmp = *base_sbuf;
183 id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
185 push_file_id_16((char *)id_buf, &id);
187 hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
189 first = hash & 0xff;
190 second = (hash >> 8) & 0xff;
192 id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
194 if (id_hex == NULL) {
195 errno = ENOMEM;
196 goto fail;
199 result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
200 first, second, id_hex);
202 TALLOC_FREE(id_hex);
204 if (result == NULL) {
205 errno = ENOMEM;
206 return NULL;
209 smb_fname_hash = synthetic_smb_fname(talloc_tos(),
210 result,
211 NULL,
212 NULL,
213 smb_fname->flags);
214 if (smb_fname_hash == NULL) {
215 errno = ENOMEM;
216 goto fail;
219 if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
220 struct smb_filename *smb_fname_new = NULL;
221 char *newname;
222 bool delete_lost;
224 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
225 errno = EINVAL;
226 goto fail;
229 if (!check_valid ||
230 file_is_valid(handle, smb_fname->base_name)) {
231 return result;
235 * Someone has recreated a file under an existing inode
236 * without deleting the streams directory.
237 * Move it away or remove if streams_depot:delete_lost is set.
240 again:
241 delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
242 "delete_lost", false);
244 if (delete_lost) {
245 DEBUG(3, ("Someone has recreated a file under an "
246 "existing inode. Removing: %s\n",
247 smb_fname_hash->base_name));
248 recursive_rmdir(talloc_tos(), handle->conn,
249 smb_fname_hash);
250 SMB_VFS_NEXT_RMDIR(handle, smb_fname_hash);
251 } else {
252 newname = talloc_asprintf(talloc_tos(), "lost-%lu",
253 random());
254 DEBUG(3, ("Someone has recreated a file under an "
255 "existing inode. Renaming: %s to: %s\n",
256 smb_fname_hash->base_name,
257 newname));
258 if (newname == NULL) {
259 errno = ENOMEM;
260 goto fail;
263 smb_fname_new = synthetic_smb_fname(
264 talloc_tos(),
265 newname,
266 NULL,
267 NULL,
268 smb_fname->flags);
269 TALLOC_FREE(newname);
270 if (smb_fname_new == NULL) {
271 errno = ENOMEM;
272 goto fail;
275 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
276 smb_fname_new) == -1) {
277 TALLOC_FREE(smb_fname_new);
278 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
279 goto again;
281 goto fail;
284 TALLOC_FREE(smb_fname_new);
288 if (!create_it) {
289 errno = ENOENT;
290 goto fail;
293 if ((SMB_VFS_NEXT_MKDIR(handle, rootdir_fname, 0755) != 0)
294 && (errno != EEXIST)) {
295 goto fail;
298 tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
299 if (tmp == NULL) {
300 errno = ENOMEM;
301 goto fail;
304 tmp_fname = synthetic_smb_fname(talloc_tos(),
305 tmp,
306 NULL,
307 NULL,
308 smb_fname->flags);
309 if (tmp_fname == NULL) {
310 errno = ENOMEM;
311 goto fail;
314 if ((SMB_VFS_NEXT_MKDIR(handle, tmp_fname, 0755) != 0)
315 && (errno != EEXIST)) {
316 goto fail;
319 TALLOC_FREE(tmp);
320 TALLOC_FREE(tmp_fname);
322 tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
323 second);
324 if (tmp == NULL) {
325 errno = ENOMEM;
326 goto fail;
329 tmp_fname = synthetic_smb_fname(talloc_tos(),
330 tmp,
331 NULL,
332 NULL,
333 smb_fname->flags);
334 if (tmp_fname == NULL) {
335 errno = ENOMEM;
336 goto fail;
339 if ((SMB_VFS_NEXT_MKDIR(handle, tmp_fname, 0755) != 0)
340 && (errno != EEXIST)) {
341 goto fail;
344 TALLOC_FREE(tmp);
345 TALLOC_FREE(tmp_fname);
347 /* smb_fname_hash is the struct smb_filename version of 'result' */
348 if ((SMB_VFS_NEXT_MKDIR(handle, smb_fname_hash, 0755) != 0)
349 && (errno != EEXIST)) {
350 goto fail;
353 if (check_valid && !mark_file_valid(handle, smb_fname->base_name)) {
354 goto fail;
357 TALLOC_FREE(rootdir_fname);
358 TALLOC_FREE(rootdir);
359 TALLOC_FREE(tmp_fname);
360 TALLOC_FREE(smb_fname_hash);
361 return result;
363 fail:
364 TALLOC_FREE(rootdir_fname);
365 TALLOC_FREE(rootdir);
366 TALLOC_FREE(tmp_fname);
367 TALLOC_FREE(smb_fname_hash);
368 TALLOC_FREE(result);
369 return NULL;
372 * Given a stream name, populate smb_fname_out with the actual location of the
373 * stream.
375 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
376 const struct smb_filename *smb_fname,
377 struct smb_filename **smb_fname_out,
378 bool create_dir)
380 char *dirname, *stream_fname;
381 const char *stype;
382 NTSTATUS status;
384 *smb_fname_out = NULL;
386 stype = strchr_m(smb_fname->stream_name + 1, ':');
388 if (stype) {
389 if (strcasecmp_m(stype, ":$DATA") != 0) {
390 return NT_STATUS_INVALID_PARAMETER;
394 dirname = stream_dir(handle, smb_fname, NULL, create_dir);
396 if (dirname == NULL) {
397 status = map_nt_error_from_unix(errno);
398 goto fail;
401 stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
402 smb_fname->stream_name);
404 if (stream_fname == NULL) {
405 status = NT_STATUS_NO_MEMORY;
406 goto fail;
409 if (stype == NULL) {
410 /* Append an explicit stream type if one wasn't specified. */
411 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
412 stream_fname);
413 if (stream_fname == NULL) {
414 status = NT_STATUS_NO_MEMORY;
415 goto fail;
417 } else {
418 /* Normalize the stream type to upercase. */
419 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
420 status = NT_STATUS_INVALID_PARAMETER;
421 goto fail;
425 DEBUG(10, ("stream filename = %s\n", stream_fname));
427 /* Create an smb_filename with stream_name == NULL. */
428 *smb_fname_out = synthetic_smb_fname(talloc_tos(),
429 stream_fname,
430 NULL,
431 NULL,
432 smb_fname->flags);
433 if (*smb_fname_out == NULL) {
434 return NT_STATUS_NO_MEMORY;
437 return NT_STATUS_OK;
439 fail:
440 DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
441 TALLOC_FREE(*smb_fname_out);
442 return status;
445 static NTSTATUS walk_streams(vfs_handle_struct *handle,
446 struct smb_filename *smb_fname_base,
447 char **pdirname,
448 bool (*fn)(const char *dirname,
449 const char *dirent,
450 void *private_data),
451 void *private_data)
453 char *dirname;
454 struct smb_filename *dir_smb_fname = NULL;
455 DIR *dirhandle = NULL;
456 const char *dirent = NULL;
457 char *talloced = NULL;
459 dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
460 false);
462 if (dirname == NULL) {
463 if (errno == ENOENT) {
465 * no stream around
467 return NT_STATUS_OK;
469 return map_nt_error_from_unix(errno);
472 DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
474 dir_smb_fname = synthetic_smb_fname(talloc_tos(),
475 dirname,
476 NULL,
477 NULL,
478 smb_fname_base->flags);
479 if (dir_smb_fname == NULL) {
480 TALLOC_FREE(dirname);
481 return NT_STATUS_NO_MEMORY;
484 dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dir_smb_fname, NULL, 0);
486 TALLOC_FREE(dir_smb_fname);
488 if (dirhandle == NULL) {
489 TALLOC_FREE(dirname);
490 return map_nt_error_from_unix(errno);
493 while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
494 &talloced)) != NULL) {
496 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
497 TALLOC_FREE(talloced);
498 continue;
501 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
503 if (!fn(dirname, dirent, private_data)) {
504 TALLOC_FREE(talloced);
505 break;
507 TALLOC_FREE(talloced);
510 SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
512 if (pdirname != NULL) {
513 *pdirname = dirname;
515 else {
516 TALLOC_FREE(dirname);
519 return NT_STATUS_OK;
523 * Helper to stat/lstat the base file of an smb_fname. This will actually
524 * fills in the stat struct in smb_filename.
526 static int streams_depot_stat_base(vfs_handle_struct *handle,
527 struct smb_filename *smb_fname,
528 bool follow_links)
530 char *tmp_stream_name;
531 int result;
533 tmp_stream_name = smb_fname->stream_name;
534 smb_fname->stream_name = NULL;
535 if (follow_links) {
536 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
537 } else {
538 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
540 smb_fname->stream_name = tmp_stream_name;
541 return result;
544 static int streams_depot_stat(vfs_handle_struct *handle,
545 struct smb_filename *smb_fname)
547 struct smb_filename *smb_fname_stream = NULL;
548 NTSTATUS status;
549 int ret = -1;
551 DEBUG(10, ("streams_depot_stat called for [%s]\n",
552 smb_fname_str_dbg(smb_fname)));
554 if (!is_ntfs_stream_smb_fname(smb_fname)) {
555 return SMB_VFS_NEXT_STAT(handle, smb_fname);
558 /* If the default stream is requested, just stat the base file. */
559 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
560 return streams_depot_stat_base(handle, smb_fname, true);
563 /* Stat the actual stream now. */
564 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
565 false);
566 if (!NT_STATUS_IS_OK(status)) {
567 ret = -1;
568 errno = map_errno_from_nt_status(status);
569 goto done;
572 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
574 /* Update the original smb_fname with the stat info. */
575 smb_fname->st = smb_fname_stream->st;
576 done:
577 TALLOC_FREE(smb_fname_stream);
578 return ret;
583 static int streams_depot_lstat(vfs_handle_struct *handle,
584 struct smb_filename *smb_fname)
586 struct smb_filename *smb_fname_stream = NULL;
587 NTSTATUS status;
588 int ret = -1;
590 DEBUG(10, ("streams_depot_lstat called for [%s]\n",
591 smb_fname_str_dbg(smb_fname)));
593 if (!is_ntfs_stream_smb_fname(smb_fname)) {
594 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
597 /* If the default stream is requested, just stat the base file. */
598 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
599 return streams_depot_stat_base(handle, smb_fname, false);
602 /* Stat the actual stream now. */
603 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
604 false);
605 if (!NT_STATUS_IS_OK(status)) {
606 ret = -1;
607 errno = map_errno_from_nt_status(status);
608 goto done;
611 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
613 done:
614 TALLOC_FREE(smb_fname_stream);
615 return ret;
618 static int streams_depot_open(vfs_handle_struct *handle,
619 struct smb_filename *smb_fname,
620 files_struct *fsp, int flags, mode_t mode)
622 struct smb_filename *smb_fname_stream = NULL;
623 struct smb_filename *smb_fname_base = NULL;
624 NTSTATUS status;
625 int ret = -1;
627 if (!is_ntfs_stream_smb_fname(smb_fname)) {
628 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
631 /* If the default stream is requested, just open the base file. */
632 if (is_ntfs_default_stream_smb_fname(smb_fname)) {
633 char *tmp_stream_name;
635 tmp_stream_name = smb_fname->stream_name;
636 smb_fname->stream_name = NULL;
637 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
638 smb_fname->stream_name = tmp_stream_name;
640 return ret;
643 /* Ensure the base file still exists. */
644 smb_fname_base = synthetic_smb_fname(talloc_tos(),
645 smb_fname->base_name,
646 NULL,
647 NULL,
648 smb_fname->flags);
649 if (smb_fname_base == NULL) {
650 ret = -1;
651 errno = ENOMEM;
652 goto done;
655 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
656 if (ret == -1) {
657 goto done;
660 /* Determine the stream name, and then open it. */
661 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
662 if (!NT_STATUS_IS_OK(status)) {
663 ret = -1;
664 errno = map_errno_from_nt_status(status);
665 goto done;
668 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
670 done:
671 TALLOC_FREE(smb_fname_stream);
672 TALLOC_FREE(smb_fname_base);
673 return ret;
676 static int streams_depot_unlink(vfs_handle_struct *handle,
677 const struct smb_filename *smb_fname)
679 struct smb_filename *smb_fname_base = NULL;
680 int ret = -1;
682 DEBUG(10, ("streams_depot_unlink called for %s\n",
683 smb_fname_str_dbg(smb_fname)));
685 /* If there is a valid stream, just unlink the stream and return. */
686 if (is_ntfs_stream_smb_fname(smb_fname) &&
687 !is_ntfs_default_stream_smb_fname(smb_fname)) {
688 struct smb_filename *smb_fname_stream = NULL;
689 NTSTATUS status;
691 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
692 false);
693 if (!NT_STATUS_IS_OK(status)) {
694 errno = map_errno_from_nt_status(status);
695 return -1;
698 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
700 TALLOC_FREE(smb_fname_stream);
701 return ret;
705 * We potentially need to delete the per-inode streams directory
708 smb_fname_base = synthetic_smb_fname(talloc_tos(),
709 smb_fname->base_name,
710 NULL,
711 NULL,
712 smb_fname->flags);
713 if (smb_fname_base == NULL) {
714 errno = ENOMEM;
715 return -1;
718 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
719 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
720 } else {
721 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
724 if (ret == -1) {
725 TALLOC_FREE(smb_fname_base);
726 return -1;
730 * We know the unlink should succeed as the ACL
731 * check is already done in the caller. Remove the
732 * file *after* the streams.
735 char *dirname = stream_dir(handle, smb_fname_base,
736 &smb_fname_base->st, false);
738 if (dirname != NULL) {
739 struct smb_filename *smb_fname_dir =
740 synthetic_smb_fname(talloc_tos(),
741 dirname,
742 NULL,
743 NULL,
744 smb_fname->flags);
745 if (smb_fname_dir == NULL) {
746 TALLOC_FREE(smb_fname_base);
747 TALLOC_FREE(dirname);
748 errno = ENOMEM;
749 return -1;
751 SMB_VFS_NEXT_RMDIR(handle, smb_fname_dir);
752 TALLOC_FREE(smb_fname_dir);
754 TALLOC_FREE(dirname);
757 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
758 TALLOC_FREE(smb_fname_base);
759 return ret;
762 static int streams_depot_rmdir(vfs_handle_struct *handle,
763 const struct smb_filename *smb_fname)
765 struct smb_filename *smb_fname_base = NULL;
766 int ret = -1;
768 DEBUG(10, ("streams_depot_rmdir called for %s\n",
769 smb_fname->base_name));
772 * We potentially need to delete the per-inode streams directory
775 smb_fname_base = synthetic_smb_fname(talloc_tos(),
776 smb_fname->base_name,
777 NULL,
778 NULL,
779 smb_fname->flags);
780 if (smb_fname_base == NULL) {
781 errno = ENOMEM;
782 return -1;
785 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
786 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
787 } else {
788 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
791 if (ret == -1) {
792 TALLOC_FREE(smb_fname_base);
793 return -1;
797 * We know the rmdir should succeed as the ACL
798 * check is already done in the caller. Remove the
799 * directory *after* the streams.
802 char *dirname = stream_dir(handle, smb_fname_base,
803 &smb_fname_base->st, false);
805 if (dirname != NULL) {
806 struct smb_filename *smb_fname_dir =
807 synthetic_smb_fname(talloc_tos(),
808 dirname,
809 NULL,
810 NULL,
811 smb_fname->flags);
812 if (smb_fname_dir == NULL) {
813 TALLOC_FREE(smb_fname_base);
814 TALLOC_FREE(dirname);
815 errno = ENOMEM;
816 return -1;
818 SMB_VFS_NEXT_RMDIR(handle, smb_fname_dir);
819 TALLOC_FREE(smb_fname_dir);
821 TALLOC_FREE(dirname);
824 ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname_base);
825 TALLOC_FREE(smb_fname_base);
826 return ret;
829 static int streams_depot_rename(vfs_handle_struct *handle,
830 const struct smb_filename *smb_fname_src,
831 const struct smb_filename *smb_fname_dst)
833 struct smb_filename *smb_fname_src_stream = NULL;
834 struct smb_filename *smb_fname_dst_stream = NULL;
835 bool src_is_stream, dst_is_stream;
836 NTSTATUS status;
837 int ret = -1;
839 DEBUG(10, ("streams_depot_rename called for %s => %s\n",
840 smb_fname_str_dbg(smb_fname_src),
841 smb_fname_str_dbg(smb_fname_dst)));
843 src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
844 dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
846 if (!src_is_stream && !dst_is_stream) {
847 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
848 smb_fname_dst);
851 /* for now don't allow renames from or to the default stream */
852 if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
853 is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
854 errno = ENOSYS;
855 goto done;
858 status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
859 false);
860 if (!NT_STATUS_IS_OK(status)) {
861 errno = map_errno_from_nt_status(status);
862 goto done;
865 status = stream_smb_fname(handle, smb_fname_dst,
866 &smb_fname_dst_stream, false);
867 if (!NT_STATUS_IS_OK(status)) {
868 errno = map_errno_from_nt_status(status);
869 goto done;
872 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
873 smb_fname_dst_stream);
875 done:
876 TALLOC_FREE(smb_fname_src_stream);
877 TALLOC_FREE(smb_fname_dst_stream);
878 return ret;
881 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
882 struct stream_struct **streams,
883 const char *name, off_t size,
884 off_t alloc_size)
886 struct stream_struct *tmp;
888 tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
889 (*num_streams)+1);
890 if (tmp == NULL) {
891 return false;
894 tmp[*num_streams].name = talloc_strdup(tmp, name);
895 if (tmp[*num_streams].name == NULL) {
896 return false;
899 tmp[*num_streams].size = size;
900 tmp[*num_streams].alloc_size = alloc_size;
902 *streams = tmp;
903 *num_streams += 1;
904 return true;
907 struct streaminfo_state {
908 TALLOC_CTX *mem_ctx;
909 vfs_handle_struct *handle;
910 unsigned int num_streams;
911 struct stream_struct *streams;
912 NTSTATUS status;
915 static bool collect_one_stream(const char *dirname,
916 const char *dirent,
917 void *private_data)
919 struct streaminfo_state *state =
920 (struct streaminfo_state *)private_data;
921 struct smb_filename *smb_fname = NULL;
922 char *sname = NULL;
923 bool ret;
925 sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
926 if (sname == NULL) {
927 state->status = NT_STATUS_NO_MEMORY;
928 ret = false;
929 goto out;
932 smb_fname = synthetic_smb_fname(talloc_tos(), sname, NULL, NULL, 0);
933 if (smb_fname == NULL) {
934 state->status = NT_STATUS_NO_MEMORY;
935 ret = false;
936 goto out;
939 if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
940 DEBUG(10, ("Could not stat %s: %s\n", sname,
941 strerror(errno)));
942 ret = true;
943 goto out;
946 if (!add_one_stream(state->mem_ctx,
947 &state->num_streams, &state->streams,
948 dirent, smb_fname->st.st_ex_size,
949 SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
950 &smb_fname->st))) {
951 state->status = NT_STATUS_NO_MEMORY;
952 ret = false;
953 goto out;
956 ret = true;
957 out:
958 TALLOC_FREE(sname);
959 TALLOC_FREE(smb_fname);
960 return ret;
963 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
964 struct files_struct *fsp,
965 const struct smb_filename *smb_fname,
966 TALLOC_CTX *mem_ctx,
967 unsigned int *pnum_streams,
968 struct stream_struct **pstreams)
970 struct smb_filename *smb_fname_base = NULL;
971 int ret;
972 NTSTATUS status;
973 struct streaminfo_state state;
975 smb_fname_base = synthetic_smb_fname(talloc_tos(),
976 smb_fname->base_name,
977 NULL,
978 NULL,
979 smb_fname->flags);
980 if (smb_fname_base == NULL) {
981 return NT_STATUS_NO_MEMORY;
984 if ((fsp != NULL) && (fsp->fh->fd != -1)) {
985 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
987 else {
988 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
989 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
990 } else {
991 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
995 if (ret == -1) {
996 status = map_nt_error_from_unix(errno);
997 goto out;
1000 state.streams = *pstreams;
1001 state.num_streams = *pnum_streams;
1002 state.mem_ctx = mem_ctx;
1003 state.handle = handle;
1004 state.status = NT_STATUS_OK;
1006 if (S_ISLNK(smb_fname_base->st.st_ex_mode)) {
1008 * Currently we do't have SMB_VFS_LLISTXATTR
1009 * inside the VFS which means there's no way
1010 * to cope with a symlink when lp_posix_pathnames().
1011 * returns true. For now ignore links.
1012 * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
1014 status = NT_STATUS_OK;
1015 } else {
1016 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
1017 &state);
1020 if (!NT_STATUS_IS_OK(status)) {
1021 TALLOC_FREE(state.streams);
1022 goto out;
1025 if (!NT_STATUS_IS_OK(state.status)) {
1026 TALLOC_FREE(state.streams);
1027 status = state.status;
1028 goto out;
1031 *pnum_streams = state.num_streams;
1032 *pstreams = state.streams;
1033 status = SMB_VFS_NEXT_STREAMINFO(handle,
1034 fsp,
1035 smb_fname_base,
1036 mem_ctx,
1037 pnum_streams,
1038 pstreams);
1040 out:
1041 TALLOC_FREE(smb_fname_base);
1042 return status;
1045 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
1046 enum timestamp_set_resolution *p_ts_res)
1048 return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
1051 static struct vfs_fn_pointers vfs_streams_depot_fns = {
1052 .fs_capabilities_fn = streams_depot_fs_capabilities,
1053 .open_fn = streams_depot_open,
1054 .stat_fn = streams_depot_stat,
1055 .lstat_fn = streams_depot_lstat,
1056 .unlink_fn = streams_depot_unlink,
1057 .rmdir_fn = streams_depot_rmdir,
1058 .rename_fn = streams_depot_rename,
1059 .streaminfo_fn = streams_depot_streaminfo,
1062 NTSTATUS vfs_streams_depot_init(void);
1063 NTSTATUS vfs_streams_depot_init(void)
1065 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
1066 &vfs_streams_depot_fns);