]> git.samba.org - samba.git/blob - source3/modules/vfs_streams_depot.c
Start fixing the RAW-STREAMS test - ensure that the xattr
[samba.git] / source3 / modules / vfs_streams_depot.c
1 /*
2  * Store streams in a separate subdirectory
3  *
4  * Copyright (C) Volker Lendecke, 2007
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include "includes.h"
21
22 #undef DBGC_CLASS
23 #define DBGC_CLASS DBGC_VFS
24
25 /*
26  * Excerpt from a mail from tridge:
27  *
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
31  *
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.
35  */
36
37 static uint32_t hash_fn(DATA_BLOB key)
38 {
39         uint32_t value; /* Used to compute the hash value.  */
40         uint32_t i;     /* Used to cycle through random values. */
41
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)));
45
46         return (1103515243 * value + 12345);
47 }
48
49 /*
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.
56  *
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.
61  *
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.
66  */
67
68 static bool file_is_valid(vfs_handle_struct *handle, const char *path,
69                           bool check_valid)
70 {
71         char buf;
72
73         if (!check_valid) {
74                 return true;
75         }
76
77         DEBUG(10, ("file_is_valid (%s) called\n", path));
78
79         if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
80                                   &buf, sizeof(buf)) != sizeof(buf)) {
81                 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
82                 return false;
83         }
84
85         if (buf != '1') {
86                 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
87                 return false;
88         }
89
90         return true;
91 }
92
93 static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
94                             bool check_valid)
95 {
96         char buf = '1';
97         int ret;
98
99         if (!check_valid) {
100                 return true;
101         }
102
103         DEBUG(10, ("marking file %s as valid\n", path));
104
105         ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
106                                     &buf, sizeof(buf), 0);
107
108         if (ret == -1) {
109                 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
110                 return false;
111         }
112
113         return true;
114 }
115
116 /**
117  * Given an smb_filename, determine the stream directory using the file's
118  * base_name.
119  */
120 static char *stream_dir(vfs_handle_struct *handle,
121                         const struct smb_filename *smb_fname,
122                         const SMB_STRUCT_STAT *base_sbuf, bool create_it)
123 {
124         uint32_t hash;
125         struct smb_filename *smb_fname_hash = NULL;
126         char *result = NULL;
127         SMB_STRUCT_STAT base_sbuf_tmp;
128         uint8_t first, second;
129         char *tmp;
130         char *id_hex;
131         struct file_id id;
132         uint8 id_buf[16];
133         bool check_valid;
134         const char *rootdir;
135         NTSTATUS status;
136
137         check_valid = lp_parm_bool(SNUM(handle->conn),
138                       "streams_depot", "check_valid", true);
139
140         tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
141
142         if (tmp == NULL) {
143                 errno = ENOMEM;
144                 goto fail;
145         }
146
147         rootdir = lp_parm_const_string(
148                 SNUM(handle->conn), "streams_depot", "directory",
149                 tmp);
150
151         /* Stat the base file if it hasn't already been done. */
152         if (base_sbuf == NULL) {
153                 struct smb_filename *smb_fname_base = NULL;
154
155                 status = create_synthetic_smb_fname(talloc_tos(),
156                                                     smb_fname->base_name,
157                                                     NULL, NULL,
158                                                     &smb_fname_base);
159                 if (!NT_STATUS_IS_OK(status)) {
160                         errno = map_errno_from_nt_status(status);
161                         goto fail;
162                 }
163                 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
164                         TALLOC_FREE(smb_fname_base);
165                         goto fail;
166                 }
167                 base_sbuf_tmp = smb_fname_base->st;
168                 TALLOC_FREE(smb_fname_base);
169         } else {
170                 base_sbuf_tmp = *base_sbuf;
171         }
172
173         id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
174
175         push_file_id_16((char *)id_buf, &id);
176
177         hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
178
179         first = hash & 0xff;
180         second = (hash >> 8) & 0xff;
181
182         id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
183
184         if (id_hex == NULL) {
185                 errno = ENOMEM;
186                 goto fail;
187         }
188
189         result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
190                                  first, second, id_hex);
191
192         TALLOC_FREE(id_hex);
193
194         if (result == NULL) {
195                 errno = ENOMEM;
196                 return NULL;
197         }
198
199         status = create_synthetic_smb_fname(talloc_tos(), result, NULL, NULL,
200                                             &smb_fname_hash);
201         if (!NT_STATUS_IS_OK(status)) {
202                 errno = map_errno_from_nt_status(status);
203                 goto fail;
204         }
205
206         if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
207                 struct smb_filename *smb_fname_new = NULL;
208                 char *newname;
209
210                 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
211                         errno = EINVAL;
212                         goto fail;
213                 }
214
215                 if (file_is_valid(handle, smb_fname->base_name, check_valid)) {
216                         return result;
217                 }
218
219                 /*
220                  * Someone has recreated a file under an existing inode
221                  * without deleting the streams directory. For now, just move
222                  * it away.
223                  */
224
225         again:
226                 newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
227                 if (newname == NULL) {
228                         errno = ENOMEM;
229                         goto fail;
230                 }
231
232                 status = create_synthetic_smb_fname(talloc_tos(), newname,
233                                                     NULL, NULL,
234                                                     &smb_fname_new);
235                 TALLOC_FREE(newname);
236                 if (!NT_STATUS_IS_OK(status)) {
237                         errno = map_errno_from_nt_status(status);
238                         goto fail;
239                 }
240
241                 if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
242                                         smb_fname_new) == -1) {
243                         TALLOC_FREE(smb_fname_new);
244                         if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
245                                 goto again;
246                         }
247                         goto fail;
248                 }
249
250                 TALLOC_FREE(smb_fname_new);
251         }
252
253         if (!create_it) {
254                 errno = ENOENT;
255                 goto fail;
256         }
257
258         if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
259             && (errno != EEXIST)) {
260                 goto fail;
261         }
262
263         tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
264         if (tmp == NULL) {
265                 errno = ENOMEM;
266                 goto fail;
267         }
268
269         if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
270             && (errno != EEXIST)) {
271                 goto fail;
272         }
273
274         TALLOC_FREE(tmp);
275
276         tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
277                               second);
278         if (tmp == NULL) {
279                 errno = ENOMEM;
280                 goto fail;
281         }
282
283         if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
284             && (errno != EEXIST)) {
285                 goto fail;
286         }
287
288         TALLOC_FREE(tmp);
289
290         if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
291             && (errno != EEXIST)) {
292                 goto fail;
293         }
294
295         if (!mark_file_valid(handle, smb_fname->base_name, check_valid)) {
296                 goto fail;
297         }
298
299         TALLOC_FREE(smb_fname_hash);
300         return result;
301
302  fail:
303         TALLOC_FREE(smb_fname_hash);
304         TALLOC_FREE(result);
305         return NULL;
306 }
307 /**
308  * Given a stream name, populate smb_fname_out with the actual location of the
309  * stream.
310  */
311 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
312                                  const struct smb_filename *smb_fname,
313                                  struct smb_filename **smb_fname_out,
314                                  bool create_dir)
315 {
316         char *dirname, *stream_fname;
317         const char *stype;
318         NTSTATUS status;
319
320         *smb_fname_out = NULL;
321
322         stype = strchr_m(smb_fname->stream_name + 1, ':');
323
324         if (stype) {
325                 if (StrCaseCmp(stype, ":$DATA") != 0) {
326                         return NT_STATUS_INVALID_PARAMETER;
327                 }
328         }
329
330         dirname = stream_dir(handle, smb_fname, NULL, create_dir);
331
332         if (dirname == NULL) {
333                 status = map_nt_error_from_unix(errno);
334                 goto fail;
335         }
336
337         stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
338                                        smb_fname->stream_name);
339
340         if (stream_fname == NULL) {
341                 status = NT_STATUS_NO_MEMORY;
342                 goto fail;
343         }
344
345         if (stype == NULL) {
346                 /* Append an explicit stream type if one wasn't specified. */
347                 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
348                                                stream_fname);
349                 if (stream_fname == NULL) {
350                         status = NT_STATUS_NO_MEMORY;
351                         goto fail;
352                 }
353         } else {
354                 /* Normalize the stream type to upercase. */
355                 strupper_m(strrchr_m(stream_fname, ':') + 1);
356         }
357
358         DEBUG(10, ("stream filename = %s\n", stream_fname));
359
360         /* Create an smb_filename with stream_name == NULL. */
361         status = create_synthetic_smb_fname(talloc_tos(), stream_fname, NULL,
362                                             NULL, smb_fname_out);
363         if (!NT_STATUS_IS_OK(status)) {
364                 return status;
365         }
366
367         return NT_STATUS_OK;
368
369  fail:
370         DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
371         TALLOC_FREE(*smb_fname_out);
372         return status;
373 }
374
375 static NTSTATUS walk_streams(vfs_handle_struct *handle,
376                              struct smb_filename *smb_fname_base,
377                              char **pdirname,
378                              bool (*fn)(const char *dirname,
379                                         const char *dirent,
380                                         void *private_data),
381                              void *private_data)
382 {
383         char *dirname;
384         SMB_STRUCT_DIR *dirhandle = NULL;
385         char *dirent = NULL;
386
387         dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
388                              false);
389
390         if (dirname == NULL) {
391                 if (errno == ENOENT) {
392                         /*
393                          * no stream around
394                          */
395                         return NT_STATUS_OK;
396                 }
397                 return map_nt_error_from_unix(errno);
398         }
399
400         DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
401
402         dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
403
404         if (dirhandle == NULL) {
405                 TALLOC_FREE(dirname);
406                 return map_nt_error_from_unix(errno);
407         }
408
409         while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL)) != NULL) {
410
411                 if (ISDOT(dirent) || ISDOTDOT(dirent)) {
412                         TALLOC_FREE(dirent);
413                         continue;
414                 }
415
416                 DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
417
418                 if (!fn(dirname, dirent, private_data)) {
419                         TALLOC_FREE(dirent);
420                         break;
421                 }
422                 TALLOC_FREE(dirent);
423         }
424
425         SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
426
427         if (pdirname != NULL) {
428                 *pdirname = dirname;
429         }
430         else {
431                 TALLOC_FREE(dirname);
432         }
433
434         return NT_STATUS_OK;
435 }
436
437 /**
438  * Helper to stat/lstat the base file of an smb_fname. This will actually
439  * fills in the stat struct in smb_filename.
440  */
441 static int streams_depot_stat_base(vfs_handle_struct *handle,
442                                    struct smb_filename *smb_fname,
443                                    bool follow_links)
444 {
445         char *tmp_stream_name;
446         int result;
447
448         tmp_stream_name = smb_fname->stream_name;
449         smb_fname->stream_name = NULL;
450         if (follow_links) {
451                 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
452         } else {
453                 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
454         }
455         smb_fname->stream_name = tmp_stream_name;
456         return result;
457 }
458
459 static int streams_depot_stat(vfs_handle_struct *handle,
460                               struct smb_filename *smb_fname)
461 {
462         struct smb_filename *smb_fname_stream = NULL;
463         NTSTATUS status;
464         int ret = -1;
465
466         DEBUG(10, ("streams_depot_stat called for [%s]\n",
467                    smb_fname_str_dbg(smb_fname)));
468
469         if (!is_ntfs_stream_smb_fname(smb_fname)) {
470                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
471         }
472
473         /* If the default stream is requested, just stat the base file. */
474         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
475                 return streams_depot_stat_base(handle, smb_fname, true);
476         }
477
478         /* Stat the actual stream now. */
479         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
480                                   false);
481         if (!NT_STATUS_IS_OK(status)) {
482                 ret = -1;
483                 errno = map_errno_from_nt_status(status);
484                 goto done;
485         }
486
487         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
488
489         /* Update the original smb_fname with the stat info. */
490         smb_fname->st = smb_fname_stream->st;
491  done:
492         TALLOC_FREE(smb_fname_stream);
493         return ret;
494 }
495
496
497
498 static int streams_depot_lstat(vfs_handle_struct *handle,
499                                struct smb_filename *smb_fname)
500 {
501         struct smb_filename *smb_fname_stream = NULL;
502         NTSTATUS status;
503         int ret = -1;
504
505         DEBUG(10, ("streams_depot_lstat called for [%s]\n",
506                    smb_fname_str_dbg(smb_fname)));
507
508         if (!is_ntfs_stream_smb_fname(smb_fname)) {
509                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
510         }
511
512         /* If the default stream is requested, just stat the base file. */
513         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
514                 return streams_depot_stat_base(handle, smb_fname, false);
515         }
516
517         /* Stat the actual stream now. */
518         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
519                                   false);
520         if (!NT_STATUS_IS_OK(status)) {
521                 ret = -1;
522                 errno = map_errno_from_nt_status(status);
523                 goto done;
524         }
525
526         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
527
528  done:
529         TALLOC_FREE(smb_fname_stream);
530         return ret;
531 }
532
533 static int streams_depot_open(vfs_handle_struct *handle,
534                               struct smb_filename *smb_fname,
535                               files_struct *fsp, int flags, mode_t mode)
536 {
537         struct smb_filename *smb_fname_stream = NULL;
538         struct smb_filename *smb_fname_base = NULL;
539         NTSTATUS status;
540         int ret = -1;
541
542         if (!is_ntfs_stream_smb_fname(smb_fname)) {
543                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
544         }
545
546         /* If the default stream is requested, just open the base file. */
547         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
548                 char *tmp_stream_name;
549
550                 tmp_stream_name = smb_fname->stream_name;
551                 smb_fname->stream_name = NULL;
552                 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
553                 smb_fname->stream_name = tmp_stream_name;
554
555                 return ret;
556         }
557
558         /* Ensure the base file still exists. */
559         status = create_synthetic_smb_fname(talloc_tos(),
560                                             smb_fname->base_name,
561                                             NULL, NULL,
562                                             &smb_fname_base);
563         if (!NT_STATUS_IS_OK(status)) {
564                 ret = -1;
565                 errno = map_errno_from_nt_status(status);
566                 goto done;
567         }
568
569         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
570         if (ret == -1) {
571                 goto done;
572         }
573
574         /* Determine the stream name, and then open it. */
575         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
576         if (!NT_STATUS_IS_OK(status)) {
577                 ret = -1;
578                 errno = map_errno_from_nt_status(status);
579                 goto done;
580         }
581
582         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
583
584  done:
585         TALLOC_FREE(smb_fname_stream);
586         TALLOC_FREE(smb_fname_base);
587         return ret;
588 }
589
590 static int streams_depot_unlink(vfs_handle_struct *handle,
591                                 const struct smb_filename *smb_fname)
592 {
593         struct smb_filename *smb_fname_base = NULL;
594         NTSTATUS status;
595         int ret = -1;
596
597         DEBUG(10, ("streams_depot_unlink called for %s\n",
598                    smb_fname_str_dbg(smb_fname)));
599
600         /* If there is a valid stream, just unlink the stream and return. */
601         if (is_ntfs_stream_smb_fname(smb_fname) &&
602             !is_ntfs_default_stream_smb_fname(smb_fname)) {
603                 struct smb_filename *smb_fname_stream = NULL;
604
605                 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
606                                           false);
607                 if (!NT_STATUS_IS_OK(status)) {
608                         errno = map_errno_from_nt_status(status);
609                         return -1;
610                 }
611
612                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
613
614                 TALLOC_FREE(smb_fname_stream);
615                 return ret;
616         }
617
618         /*
619          * We potentially need to delete the per-inode streams directory
620          */
621
622         status = create_synthetic_smb_fname(talloc_tos(), smb_fname->base_name,
623                                             NULL, NULL, &smb_fname_base);
624         if (!NT_STATUS_IS_OK(status)) {
625                 errno = map_errno_from_nt_status(status);
626                 return -1;
627         }
628
629         if (lp_posix_pathnames()) {
630                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
631         } else {
632                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
633         }
634
635         if (ret == -1) {
636                 TALLOC_FREE(smb_fname_base);
637                 return -1;
638         }
639
640         if (smb_fname_base->st.st_ex_nlink == 1) {
641                 char *dirname = stream_dir(handle, smb_fname_base,
642                                            &smb_fname_base->st, false);
643
644                 if (dirname != NULL) {
645                         SMB_VFS_NEXT_RMDIR(handle, dirname);
646                 }
647                 TALLOC_FREE(dirname);
648         }
649
650         ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
651
652         TALLOC_FREE(smb_fname_base);
653         return ret;
654 }
655
656 static int streams_depot_rename(vfs_handle_struct *handle,
657                                 const struct smb_filename *smb_fname_src,
658                                 const struct smb_filename *smb_fname_dst)
659 {
660         struct smb_filename *smb_fname_src_stream = NULL;
661         struct smb_filename *smb_fname_dst_stream = NULL;
662         struct smb_filename *smb_fname_dst_mod = NULL;
663         bool src_is_stream, dst_is_stream;
664         NTSTATUS status;
665         int ret = -1;
666
667         DEBUG(10, ("streams_depot_rename called for %s => %s\n",
668                    smb_fname_str_dbg(smb_fname_src),
669                    smb_fname_str_dbg(smb_fname_dst)));
670
671         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
672         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
673
674         if (!src_is_stream && !dst_is_stream) {
675                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
676                                            smb_fname_dst);
677         }
678
679         /* for now don't allow renames from or to the default stream */
680         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
681             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
682                 errno = ENOSYS;
683                 goto done;
684         }
685
686         status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
687                                   false);
688         if (!NT_STATUS_IS_OK(status)) {
689                 errno = map_errno_from_nt_status(status);
690                 goto done;
691         }
692
693         /*
694          * Handle passing in a stream name without the base file.  This is
695          * exercised by the NTRENAME streams rename path.
696          */
697         if (StrCaseCmp(smb_fname_dst->base_name, "./") == 0) {
698                 status = create_synthetic_smb_fname(talloc_tos(),
699                                                     smb_fname_src->base_name,
700                                                     smb_fname_dst->stream_name,
701                                                     NULL, &smb_fname_dst_mod);
702                 if (!NT_STATUS_IS_OK(status)) {
703                         errno = map_errno_from_nt_status(status);
704                         goto done;
705                 }
706         }
707
708         status = stream_smb_fname(handle, (smb_fname_dst_mod ?
709                                            smb_fname_dst_mod : smb_fname_dst),
710                                   &smb_fname_dst_stream, false);
711         if (!NT_STATUS_IS_OK(status)) {
712                 errno = map_errno_from_nt_status(status);
713                 goto done;
714         }
715
716         ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
717                                   smb_fname_dst_stream);
718
719 done:
720         TALLOC_FREE(smb_fname_src_stream);
721         TALLOC_FREE(smb_fname_dst_stream);
722         TALLOC_FREE(smb_fname_dst_mod);
723         return ret;
724 }
725
726 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
727                            struct stream_struct **streams,
728                            const char *name, SMB_OFF_T size,
729                            SMB_OFF_T alloc_size)
730 {
731         struct stream_struct *tmp;
732
733         tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
734                                    (*num_streams)+1);
735         if (tmp == NULL) {
736                 return false;
737         }
738
739         tmp[*num_streams].name = talloc_strdup(tmp, name);
740         if (tmp[*num_streams].name == NULL) {
741                 return false;
742         }
743
744         tmp[*num_streams].size = size;
745         tmp[*num_streams].alloc_size = alloc_size;
746
747         *streams = tmp;
748         *num_streams += 1;
749         return true;
750 }
751
752 struct streaminfo_state {
753         TALLOC_CTX *mem_ctx;
754         vfs_handle_struct *handle;
755         unsigned int num_streams;
756         struct stream_struct *streams;
757         NTSTATUS status;
758 };
759
760 static bool collect_one_stream(const char *dirname,
761                                const char *dirent,
762                                void *private_data)
763 {
764         struct streaminfo_state *state =
765                 (struct streaminfo_state *)private_data;
766         struct smb_filename *smb_fname = NULL;
767         char *sname = NULL;
768         NTSTATUS status;
769         bool ret;
770
771         sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
772         if (sname == NULL) {
773                 state->status = NT_STATUS_NO_MEMORY;
774                 ret = false;
775                 goto out;
776         }
777
778         status = create_synthetic_smb_fname(talloc_tos(), sname, NULL,
779                                             NULL, &smb_fname);
780         if (!NT_STATUS_IS_OK(status)) {
781                 state->status = status;
782                 ret = false;
783                 goto out;
784         }
785
786         if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
787                 DEBUG(10, ("Could not stat %s: %s\n", sname,
788                            strerror(errno)));
789                 ret = true;
790                 goto out;
791         }
792
793         if (!add_one_stream(state->mem_ctx,
794                             &state->num_streams, &state->streams,
795                             dirent, smb_fname->st.st_ex_size,
796                             SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
797                                                    &smb_fname->st))) {
798                 state->status = NT_STATUS_NO_MEMORY;
799                 ret = false;
800                 goto out;
801         }
802
803         ret = true;
804  out:
805         TALLOC_FREE(sname);
806         TALLOC_FREE(smb_fname);
807         return ret;
808 }
809
810 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
811                                          struct files_struct *fsp,
812                                          const char *fname,
813                                          TALLOC_CTX *mem_ctx,
814                                          unsigned int *pnum_streams,
815                                          struct stream_struct **pstreams)
816 {
817         struct smb_filename *smb_fname_base = NULL;
818         int ret;
819         NTSTATUS status;
820         struct streaminfo_state state;
821
822         status = create_synthetic_smb_fname(talloc_tos(), fname, NULL, NULL,
823                                             &smb_fname_base);
824         if (!NT_STATUS_IS_OK(status)) {
825                 return status;
826         }
827
828         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
829                 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
830         }
831         else {
832                 if (lp_posix_pathnames()) {
833                         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
834                 } else {
835                         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
836                 }
837         }
838
839         if (ret == -1) {
840                 status = map_nt_error_from_unix(errno);
841                 goto out;
842         }
843
844         state.streams = NULL;
845         state.num_streams = 0;
846
847         if (!S_ISDIR(smb_fname_base->st.st_ex_mode)) {
848                 if (!add_one_stream(mem_ctx,
849                                     &state.num_streams, &state.streams,
850                                     "::$DATA", smb_fname_base->st.st_ex_size,
851                                     SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
852                                                        &smb_fname_base->st))) {
853                         status = NT_STATUS_NO_MEMORY;
854                         goto out;
855                 }
856         }
857
858         state.mem_ctx = mem_ctx;
859         state.handle = handle;
860         state.status = NT_STATUS_OK;
861
862         status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
863                               &state);
864
865         if (!NT_STATUS_IS_OK(status)) {
866                 TALLOC_FREE(state.streams);
867                 goto out;
868         }
869
870         if (!NT_STATUS_IS_OK(state.status)) {
871                 TALLOC_FREE(state.streams);
872                 status = state.status;
873                 goto out;
874         }
875
876         *pnum_streams = state.num_streams;
877         *pstreams = state.streams;
878         status = NT_STATUS_OK;
879
880  out:
881         TALLOC_FREE(smb_fname_base);
882         return status;
883 }
884
885 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
886                         enum timestamp_set_resolution *p_ts_res)
887 {
888         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
889 }
890
891 static struct vfs_fn_pointers vfs_streams_depot_fns = {
892         .fs_capabilities = streams_depot_fs_capabilities,
893         .open = streams_depot_open,
894         .stat = streams_depot_stat,
895         .lstat = streams_depot_lstat,
896         .unlink = streams_depot_unlink,
897         .rename = streams_depot_rename,
898         .streaminfo = streams_depot_streaminfo,
899 };
900
901 NTSTATUS vfs_streams_depot_init(void);
902 NTSTATUS vfs_streams_depot_init(void)
903 {
904         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
905                                 &vfs_streams_depot_fns);
906 }