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