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