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