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