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