s3/modules: VFS: streams_depo: Remove SMB_VFS_STREAMINFO
[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,
71                         const struct smb_filename *smb_fname)
72 {
73         char buf;
74
75         DEBUG(10, ("file_is_valid (%s) called\n", smb_fname->base_name));
76
77         if (SMB_VFS_GETXATTR(handle->conn, smb_fname, SAMBA_XATTR_MARKER,
78                                   &buf, sizeof(buf)) != sizeof(buf)) {
79                 DEBUG(10, ("GETXATTR failed: %s\n", strerror(errno)));
80                 return false;
81         }
82
83         if (buf != '1') {
84                 DEBUG(10, ("got wrong buffer content: '%c'\n", buf));
85                 return false;
86         }
87
88         return true;
89 }
90
91 static bool mark_file_valid(vfs_handle_struct *handle,
92                                 const struct smb_filename *smb_fname)
93 {
94         char buf = '1';
95         int ret;
96
97         DEBUG(10, ("marking file %s as valid\n", smb_fname->base_name));
98
99         ret = SMB_VFS_FSETXATTR(smb_fname->fsp, SAMBA_XATTR_MARKER,
100                                     &buf, sizeof(buf), 0);
101
102         if (ret == -1) {
103                 DEBUG(10, ("SETXATTR failed: %s\n", strerror(errno)));
104                 return false;
105         }
106
107         return true;
108 }
109
110 /**
111  * Given an smb_filename, determine the stream directory using the file's
112  * base_name.
113  */
114 static char *stream_dir(vfs_handle_struct *handle,
115                         const struct smb_filename *smb_fname,
116                         const SMB_STRUCT_STAT *base_sbuf, bool create_it)
117 {
118         const struct loadparm_substitution *lp_sub =
119                 loadparm_s3_global_substitution();
120         uint32_t hash;
121         struct smb_filename *smb_fname_hash = NULL;
122         char *result = NULL;
123         SMB_STRUCT_STAT base_sbuf_tmp;
124         uint8_t first, second;
125         char *tmp;
126         char *id_hex;
127         struct file_id id;
128         uint8_t id_buf[16];
129         bool check_valid;
130         char *rootdir = NULL;
131         struct smb_filename *rootdir_fname = NULL;
132         struct smb_filename *tmp_fname = NULL;
133         struct smb_filename *tmpref = NULL;
134         const struct smb_filename *pathref = NULL;
135         int ret;
136
137         check_valid = lp_parm_bool(SNUM(handle->conn),
138                       "streams_depot", "check_valid", true);
139
140         tmp = talloc_asprintf(talloc_tos(), "%s/.streams",
141                 handle->conn->connectpath);
142
143         if (tmp == NULL) {
144                 errno = ENOMEM;
145                 goto fail;
146         }
147
148         rootdir = lp_parm_substituted_string(talloc_tos(), lp_sub,
149                 SNUM(handle->conn), "streams_depot", "directory",
150                 tmp);
151         if (rootdir == NULL) {
152                 errno = ENOMEM;
153                 goto fail;
154         }
155
156         rootdir_fname = synthetic_smb_fname(talloc_tos(),
157                                         rootdir,
158                                         NULL,
159                                         NULL,
160                                         smb_fname->twrp,
161                                         smb_fname->flags);
162         if (rootdir_fname == NULL) {
163                 errno = ENOMEM;
164                 goto fail;
165         }
166
167         /* Stat the base file if it hasn't already been done. */
168         if (base_sbuf == NULL) {
169                 struct smb_filename *smb_fname_base;
170
171                 smb_fname_base = synthetic_smb_fname(
172                                         talloc_tos(),
173                                         smb_fname->base_name,
174                                         NULL,
175                                         NULL,
176                                         smb_fname->twrp,
177                                         smb_fname->flags);
178                 if (smb_fname_base == NULL) {
179                         errno = ENOMEM;
180                         goto fail;
181                 }
182                 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
183                         TALLOC_FREE(smb_fname_base);
184                         goto fail;
185                 }
186                 base_sbuf_tmp = smb_fname_base->st;
187                 TALLOC_FREE(smb_fname_base);
188         } else {
189                 base_sbuf_tmp = *base_sbuf;
190         }
191
192         id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
193
194         push_file_id_16((char *)id_buf, &id);
195
196         hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
197
198         first = hash & 0xff;
199         second = (hash >> 8) & 0xff;
200
201         id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
202
203         if (id_hex == NULL) {
204                 errno = ENOMEM;
205                 goto fail;
206         }
207
208         result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
209                                  first, second, id_hex);
210
211         TALLOC_FREE(id_hex);
212
213         if (result == NULL) {
214                 errno = ENOMEM;
215                 return NULL;
216         }
217
218         smb_fname_hash = synthetic_smb_fname(talloc_tos(),
219                                         result,
220                                         NULL,
221                                         NULL,
222                                         smb_fname->twrp,
223                                         smb_fname->flags);
224         if (smb_fname_hash == NULL) {
225                 errno = ENOMEM;
226                 goto fail;
227         }
228
229         if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
230                 struct smb_filename *smb_fname_new = NULL;
231                 char *newname;
232                 bool delete_lost;
233
234                 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
235                         errno = EINVAL;
236                         goto fail;
237                 }
238
239                 if (!check_valid ||
240                     file_is_valid(handle, smb_fname)) {
241                         return result;
242                 }
243
244                 /*
245                  * Someone has recreated a file under an existing inode
246                  * without deleting the streams directory.
247                  * Move it away or remove if streams_depot:delete_lost is set.
248                  */
249
250         again:
251                 delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
252                                            "delete_lost", false);
253
254                 if (delete_lost) {
255                         DEBUG(3, ("Someone has recreated a file under an "
256                               "existing inode. Removing: %s\n",
257                               smb_fname_hash->base_name));
258                         recursive_rmdir(talloc_tos(), handle->conn,
259                                         smb_fname_hash);
260                         SMB_VFS_NEXT_UNLINKAT(handle,
261                                         handle->conn->cwd_fsp,
262                                         smb_fname_hash,
263                                         AT_REMOVEDIR);
264                 } else {
265                         newname = talloc_asprintf(talloc_tos(), "lost-%lu",
266                                                   random());
267                         DEBUG(3, ("Someone has recreated a file under an "
268                               "existing inode. Renaming: %s to: %s\n",
269                               smb_fname_hash->base_name,
270                               newname));
271                         if (newname == NULL) {
272                                 errno = ENOMEM;
273                                 goto fail;
274                         }
275
276                         smb_fname_new = synthetic_smb_fname(
277                                                 talloc_tos(),
278                                                 newname,
279                                                 NULL,
280                                                 NULL,
281                                                 smb_fname->twrp,
282                                                 smb_fname->flags);
283                         TALLOC_FREE(newname);
284                         if (smb_fname_new == NULL) {
285                                 errno = ENOMEM;
286                                 goto fail;
287                         }
288
289                         if (SMB_VFS_NEXT_RENAMEAT(handle,
290                                         handle->conn->cwd_fsp,
291                                         smb_fname_hash,
292                                         handle->conn->cwd_fsp,
293                                         smb_fname_new) == -1) {
294                                 TALLOC_FREE(smb_fname_new);
295                                 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
296                                         goto again;
297                                 }
298                                 goto fail;
299                         }
300
301                         TALLOC_FREE(smb_fname_new);
302                 }
303         }
304
305         if (!create_it) {
306                 errno = ENOENT;
307                 goto fail;
308         }
309
310         ret = SMB_VFS_NEXT_MKDIRAT(handle,
311                                 handle->conn->cwd_fsp,
312                                 rootdir_fname,
313                                 0755);
314         if ((ret != 0) && (errno != EEXIST)) {
315                 goto fail;
316         }
317
318         tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
319         if (tmp == NULL) {
320                 errno = ENOMEM;
321                 goto fail;
322         }
323
324         tmp_fname = synthetic_smb_fname(talloc_tos(),
325                                         tmp,
326                                         NULL,
327                                         NULL,
328                                         smb_fname->twrp,
329                                         smb_fname->flags);
330         if (tmp_fname == NULL) {
331                 errno = ENOMEM;
332                 goto fail;
333         }
334
335         ret = SMB_VFS_NEXT_MKDIRAT(handle,
336                                 handle->conn->cwd_fsp,
337                                 tmp_fname,
338                                 0755);
339         if ((ret != 0) && (errno != EEXIST)) {
340                 goto fail;
341         }
342
343         TALLOC_FREE(tmp);
344         TALLOC_FREE(tmp_fname);
345
346         tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
347                               second);
348         if (tmp == NULL) {
349                 errno = ENOMEM;
350                 goto fail;
351         }
352
353         tmp_fname = synthetic_smb_fname(talloc_tos(),
354                                         tmp,
355                                         NULL,
356                                         NULL,
357                                         smb_fname->twrp,
358                                         smb_fname->flags);
359         if (tmp_fname == NULL) {
360                 errno = ENOMEM;
361                 goto fail;
362         }
363
364         ret = SMB_VFS_NEXT_MKDIRAT(handle,
365                         handle->conn->cwd_fsp,
366                         tmp_fname,
367                         0755);
368         if ((ret != 0) && (errno != EEXIST)) {
369                 goto fail;
370         }
371
372         TALLOC_FREE(tmp);
373         TALLOC_FREE(tmp_fname);
374
375         /* smb_fname_hash is the struct smb_filename version of 'result' */
376         ret = SMB_VFS_NEXT_MKDIRAT(handle,
377                         handle->conn->cwd_fsp,
378                         smb_fname_hash,
379                         0755);
380         if ((ret != 0) && (errno != EEXIST)) {
381                 goto fail;
382         }
383         pathref = smb_fname;
384         if (smb_fname->fsp == NULL) {
385                 NTSTATUS status;
386                 status = synthetic_pathref(talloc_tos(),
387                                         handle->conn->cwd_fsp,
388                                         smb_fname->base_name,
389                                         NULL,
390                                         NULL,
391                                         smb_fname->twrp,
392                                         smb_fname->flags,
393                                         &tmpref);
394                 if (!NT_STATUS_IS_OK(status)) {
395                         goto fail;
396                 }
397                 pathref = tmpref;
398         }
399         if (check_valid && !mark_file_valid(handle, pathref)) {
400                 goto fail;
401         }
402
403         TALLOC_FREE(tmpref);
404         TALLOC_FREE(rootdir_fname);
405         TALLOC_FREE(rootdir);
406         TALLOC_FREE(tmp_fname);
407         TALLOC_FREE(smb_fname_hash);
408         return result;
409
410  fail:
411         TALLOC_FREE(tmpref);
412         TALLOC_FREE(rootdir_fname);
413         TALLOC_FREE(rootdir);
414         TALLOC_FREE(tmp_fname);
415         TALLOC_FREE(smb_fname_hash);
416         TALLOC_FREE(result);
417         return NULL;
418 }
419 /**
420  * Given a stream name, populate smb_fname_out with the actual location of the
421  * stream.
422  */
423 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
424                                  const struct smb_filename *smb_fname,
425                                  struct smb_filename **smb_fname_out,
426                                  bool create_dir)
427 {
428         char *dirname, *stream_fname;
429         const char *stype;
430         NTSTATUS status;
431
432         *smb_fname_out = NULL;
433
434         stype = strchr_m(smb_fname->stream_name + 1, ':');
435
436         if (stype) {
437                 if (strcasecmp_m(stype, ":$DATA") != 0) {
438                         return NT_STATUS_INVALID_PARAMETER;
439                 }
440         }
441
442         dirname = stream_dir(handle, smb_fname, NULL, create_dir);
443
444         if (dirname == NULL) {
445                 status = map_nt_error_from_unix(errno);
446                 goto fail;
447         }
448
449         stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
450                                        smb_fname->stream_name);
451
452         if (stream_fname == NULL) {
453                 status = NT_STATUS_NO_MEMORY;
454                 goto fail;
455         }
456
457         if (stype == NULL) {
458                 /* Append an explicit stream type if one wasn't specified. */
459                 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
460                                                stream_fname);
461                 if (stream_fname == NULL) {
462                         status = NT_STATUS_NO_MEMORY;
463                         goto fail;
464                 }
465         } else {
466                 /* Normalize the stream type to upercase. */
467                 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
468                         status = NT_STATUS_INVALID_PARAMETER;
469                         goto fail;
470                 }
471         }
472
473         DEBUG(10, ("stream filename = %s\n", stream_fname));
474
475         /* Create an smb_filename with stream_name == NULL. */
476         *smb_fname_out = synthetic_smb_fname(talloc_tos(),
477                                         stream_fname,
478                                         NULL,
479                                         NULL,
480                                         smb_fname->twrp,
481                                         smb_fname->flags);
482         if (*smb_fname_out == NULL) {
483                 return NT_STATUS_NO_MEMORY;
484         }
485
486         return NT_STATUS_OK;
487
488  fail:
489         DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
490         TALLOC_FREE(*smb_fname_out);
491         return status;
492 }
493
494 static NTSTATUS walk_streams(vfs_handle_struct *handle,
495                              struct smb_filename *smb_fname_base,
496                              char **pdirname,
497                              bool (*fn)(const struct smb_filename *dirname,
498                                         const char *dirent,
499                                         void *private_data),
500                              void *private_data)
501 {
502         char *dirname;
503         struct smb_filename *dir_smb_fname = NULL;
504         struct smb_Dir *dir_hnd = NULL;
505         const char *dname = NULL;
506         long offset = 0;
507         char *talloced = NULL;
508
509         dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
510                              false);
511
512         if (dirname == NULL) {
513                 if (errno == ENOENT) {
514                         /*
515                          * no stream around
516                          */
517                         return NT_STATUS_OK;
518                 }
519                 return map_nt_error_from_unix(errno);
520         }
521
522         DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
523
524         dir_smb_fname = synthetic_smb_fname(talloc_tos(),
525                                         dirname,
526                                         NULL,
527                                         NULL,
528                                         smb_fname_base->twrp,
529                                         smb_fname_base->flags);
530         if (dir_smb_fname == NULL) {
531                 TALLOC_FREE(dirname);
532                 return NT_STATUS_NO_MEMORY;
533         }
534
535         dir_hnd = OpenDir(talloc_tos(), handle->conn, dir_smb_fname, NULL, 0);
536         if (dir_hnd == NULL) {
537                 TALLOC_FREE(dir_smb_fname);
538                 TALLOC_FREE(dirname);
539                 return map_nt_error_from_unix(errno);
540         }
541
542         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
543                != NULL)
544         {
545                 if (ISDOT(dname) || ISDOTDOT(dname)) {
546                         TALLOC_FREE(talloced);
547                         continue;
548                 }
549
550                 DBG_DEBUG("dirent=%s\n", dname);
551
552                 if (!fn(dir_smb_fname, dname, private_data)) {
553                         TALLOC_FREE(talloced);
554                         break;
555                 }
556                 TALLOC_FREE(talloced);
557         }
558
559         TALLOC_FREE(dir_smb_fname);
560         TALLOC_FREE(dir_hnd);
561
562         if (pdirname != NULL) {
563                 *pdirname = dirname;
564         }
565         else {
566                 TALLOC_FREE(dirname);
567         }
568
569         return NT_STATUS_OK;
570 }
571
572 static int streams_depot_stat(vfs_handle_struct *handle,
573                               struct smb_filename *smb_fname)
574 {
575         struct smb_filename *smb_fname_stream = NULL;
576         NTSTATUS status;
577         int ret = -1;
578
579         DEBUG(10, ("streams_depot_stat called for [%s]\n",
580                    smb_fname_str_dbg(smb_fname)));
581
582         if (!is_named_stream(smb_fname)) {
583                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
584         }
585
586         /* Stat the actual stream now. */
587         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
588                                   false);
589         if (!NT_STATUS_IS_OK(status)) {
590                 ret = -1;
591                 errno = map_errno_from_nt_status(status);
592                 goto done;
593         }
594
595         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
596
597         /* Update the original smb_fname with the stat info. */
598         smb_fname->st = smb_fname_stream->st;
599  done:
600         TALLOC_FREE(smb_fname_stream);
601         return ret;
602 }
603
604
605
606 static int streams_depot_lstat(vfs_handle_struct *handle,
607                                struct smb_filename *smb_fname)
608 {
609         struct smb_filename *smb_fname_stream = NULL;
610         NTSTATUS status;
611         int ret = -1;
612
613         DEBUG(10, ("streams_depot_lstat called for [%s]\n",
614                    smb_fname_str_dbg(smb_fname)));
615
616         if (!is_named_stream(smb_fname)) {
617                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
618         }
619
620         /* Stat the actual stream now. */
621         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
622                                   false);
623         if (!NT_STATUS_IS_OK(status)) {
624                 ret = -1;
625                 errno = map_errno_from_nt_status(status);
626                 goto done;
627         }
628
629         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
630
631  done:
632         TALLOC_FREE(smb_fname_stream);
633         return ret;
634 }
635
636 static int streams_depot_openat(struct vfs_handle_struct *handle,
637                                 const struct files_struct *dirfsp,
638                                 const struct smb_filename *smb_fname,
639                                 struct files_struct *fsp,
640                                 int flags,
641                                 mode_t mode)
642 {
643         struct smb_filename *smb_fname_stream = NULL;
644         struct smb_filename *smb_fname_base = NULL;
645         struct files_struct *fspcwd = NULL;
646         NTSTATUS status;
647         int ret = -1;
648
649         if (!is_named_stream(smb_fname)) {
650                 return SMB_VFS_NEXT_OPENAT(handle,
651                                            dirfsp,
652                                            smb_fname,
653                                            fsp,
654                                            flags,
655                                            mode);
656         }
657
658         /*
659          * For now assert this so the below SMB_VFS_STAT() is ok.
660          */
661         SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
662
663         /* Ensure the base file still exists. */
664         smb_fname_base = synthetic_smb_fname(talloc_tos(),
665                                         smb_fname->base_name,
666                                         NULL,
667                                         NULL,
668                                         smb_fname->twrp,
669                                         smb_fname->flags);
670         if (smb_fname_base == NULL) {
671                 ret = -1;
672                 errno = ENOMEM;
673                 goto done;
674         }
675
676         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
677         if (ret == -1) {
678                 goto done;
679         }
680
681         /* Determine the stream name, and then open it. */
682         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
683         if (!NT_STATUS_IS_OK(status)) {
684                 ret = -1;
685                 errno = map_errno_from_nt_status(status);
686                 goto done;
687         }
688
689         status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
690         if (!NT_STATUS_IS_OK(status)) {
691                 ret = -1;
692                 errno = map_errno_from_nt_status(status);
693                 goto done;
694         }
695
696         ret = SMB_VFS_NEXT_OPENAT(handle,
697                                   fspcwd,
698                                   smb_fname_stream,
699                                   fsp,
700                                   flags,
701                                   mode);
702
703  done:
704         TALLOC_FREE(smb_fname_stream);
705         TALLOC_FREE(smb_fname_base);
706         TALLOC_FREE(fspcwd);
707         return ret;
708 }
709
710 static int streams_depot_unlink_internal(vfs_handle_struct *handle,
711                                 struct files_struct *dirfsp,
712                                 const struct smb_filename *smb_fname,
713                                 int flags)
714 {
715         struct smb_filename *full_fname = NULL;
716         char *dirname = NULL;
717         int ret = -1;
718
719         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
720                                                   dirfsp,
721                                                   smb_fname);
722         if (full_fname == NULL) {
723                 return -1;
724         }
725
726         DEBUG(10, ("streams_depot_unlink called for %s\n",
727                    smb_fname_str_dbg(full_fname)));
728
729         /* If there is a valid stream, just unlink the stream and return. */
730         if (is_named_stream(full_fname)) {
731                 struct smb_filename *smb_fname_stream = NULL;
732                 NTSTATUS status;
733
734                 status = stream_smb_fname(handle, full_fname, &smb_fname_stream,
735                                           false);
736                 TALLOC_FREE(full_fname);
737                 if (!NT_STATUS_IS_OK(status)) {
738                         errno = map_errno_from_nt_status(status);
739                         return -1;
740                 }
741
742                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
743                                 dirfsp->conn->cwd_fsp,
744                                 smb_fname_stream,
745                                 0);
746
747                 TALLOC_FREE(smb_fname_stream);
748                 return ret;
749         }
750
751         /*
752          * We potentially need to delete the per-inode streams directory
753          */
754
755         if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
756                 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
757         } else {
758                 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
759         }
760         if (ret == -1) {
761                 TALLOC_FREE(full_fname);
762                 return -1;
763         }
764
765         /*
766          * We know the unlink should succeed as the ACL
767          * check is already done in the caller. Remove the
768          * file *after* the streams.
769          */
770         dirname = stream_dir(handle,
771                              full_fname,
772                              &full_fname->st,
773                              false);
774         TALLOC_FREE(full_fname);
775         if (dirname != NULL) {
776                 struct smb_filename *smb_fname_dir = NULL;
777
778                 smb_fname_dir = synthetic_smb_fname(talloc_tos(),
779                                                     dirname,
780                                                     NULL,
781                                                     NULL,
782                                                     smb_fname->twrp,
783                                                     smb_fname->flags);
784                 if (smb_fname_dir == NULL) {
785                         TALLOC_FREE(dirname);
786                         errno = ENOMEM;
787                         return -1;
788                 }
789
790                 SMB_VFS_NEXT_UNLINKAT(handle,
791                                       dirfsp->conn->cwd_fsp,
792                                       smb_fname_dir,
793                                       AT_REMOVEDIR);
794                 TALLOC_FREE(smb_fname_dir);
795                 TALLOC_FREE(dirname);
796         }
797
798         ret = SMB_VFS_NEXT_UNLINKAT(handle,
799                                 dirfsp,
800                                 smb_fname,
801                                 flags);
802         return ret;
803 }
804
805 static int streams_depot_rmdir_internal(vfs_handle_struct *handle,
806                         struct files_struct *dirfsp,
807                         const struct smb_filename *smb_fname)
808 {
809         struct smb_filename *full_fname = NULL;
810         struct smb_filename *smb_fname_base = NULL;
811         int ret = -1;
812
813         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
814                                                   dirfsp,
815                                                   smb_fname);
816         if (full_fname == NULL) {
817                 return -1;
818         }
819
820         DBG_DEBUG("called for %s\n", full_fname->base_name);
821
822         /*
823          * We potentially need to delete the per-inode streams directory
824          */
825
826         smb_fname_base = synthetic_smb_fname(talloc_tos(),
827                                 full_fname->base_name,
828                                 NULL,
829                                 NULL,
830                                 full_fname->twrp,
831                                 full_fname->flags);
832         TALLOC_FREE(full_fname);
833         if (smb_fname_base == NULL) {
834                 errno = ENOMEM;
835                 return -1;
836         }
837
838         if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
839                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
840         } else {
841                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
842         }
843
844         if (ret == -1) {
845                 TALLOC_FREE(smb_fname_base);
846                 return -1;
847         }
848
849         /*
850          * We know the rmdir should succeed as the ACL
851          * check is already done in the caller. Remove the
852          * directory *after* the streams.
853          */
854         {
855                 char *dirname = stream_dir(handle, smb_fname_base,
856                                            &smb_fname_base->st, false);
857
858                 if (dirname != NULL) {
859                         struct smb_filename *smb_fname_dir =
860                                 synthetic_smb_fname(talloc_tos(),
861                                                 dirname,
862                                                 NULL,
863                                                 NULL,
864                                                 smb_fname->twrp,
865                                                 smb_fname->flags);
866                         if (smb_fname_dir == NULL) {
867                                 TALLOC_FREE(smb_fname_base);
868                                 TALLOC_FREE(dirname);
869                                 errno = ENOMEM;
870                                 return -1;
871                         }
872                         SMB_VFS_NEXT_UNLINKAT(handle,
873                                         dirfsp->conn->cwd_fsp,
874                                         smb_fname_dir,
875                                         AT_REMOVEDIR);
876                         TALLOC_FREE(smb_fname_dir);
877                 }
878                 TALLOC_FREE(dirname);
879         }
880
881         ret = SMB_VFS_NEXT_UNLINKAT(handle,
882                                 dirfsp,
883                                 smb_fname,
884                                 AT_REMOVEDIR);
885         TALLOC_FREE(smb_fname_base);
886         return ret;
887 }
888
889 static int streams_depot_unlinkat(vfs_handle_struct *handle,
890                         struct files_struct *dirfsp,
891                         const struct smb_filename *smb_fname,
892                         int flags)
893 {
894         int ret;
895         if (flags & AT_REMOVEDIR) {
896                 ret = streams_depot_rmdir_internal(handle,
897                                 dirfsp,
898                                 smb_fname);
899         } else {
900                 ret = streams_depot_unlink_internal(handle,
901                                 dirfsp,
902                                 smb_fname,
903                                 flags);
904         }
905         return ret;
906 }
907
908 static int streams_depot_renameat(vfs_handle_struct *handle,
909                                 files_struct *srcfsp,
910                                 const struct smb_filename *smb_fname_src,
911                                 files_struct *dstfsp,
912                                 const struct smb_filename *smb_fname_dst)
913 {
914         struct smb_filename *smb_fname_src_stream = NULL;
915         struct smb_filename *smb_fname_dst_stream = NULL;
916         bool src_is_stream, dst_is_stream;
917         NTSTATUS status;
918         int ret = -1;
919
920         DEBUG(10, ("streams_depot_renameat called for %s => %s\n",
921                    smb_fname_str_dbg(smb_fname_src),
922                    smb_fname_str_dbg(smb_fname_dst)));
923
924         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
925         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
926
927         if (!src_is_stream && !dst_is_stream) {
928                 return SMB_VFS_NEXT_RENAMEAT(handle,
929                                         srcfsp,
930                                         smb_fname_src,
931                                         dstfsp,
932                                         smb_fname_dst);
933         }
934
935         /* for now don't allow renames from or to the default stream */
936         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
937             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
938                 errno = ENOSYS;
939                 goto done;
940         }
941
942         status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
943                                   false);
944         if (!NT_STATUS_IS_OK(status)) {
945                 errno = map_errno_from_nt_status(status);
946                 goto done;
947         }
948
949         status = stream_smb_fname(handle, smb_fname_dst,
950                                   &smb_fname_dst_stream, false);
951         if (!NT_STATUS_IS_OK(status)) {
952                 errno = map_errno_from_nt_status(status);
953                 goto done;
954         }
955
956         ret = SMB_VFS_NEXT_RENAMEAT(handle,
957                                 srcfsp,
958                                 smb_fname_src_stream,
959                                 dstfsp,
960                                 smb_fname_dst_stream);
961
962 done:
963         TALLOC_FREE(smb_fname_src_stream);
964         TALLOC_FREE(smb_fname_dst_stream);
965         return ret;
966 }
967
968 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
969                            struct stream_struct **streams,
970                            const char *name, off_t size,
971                            off_t alloc_size)
972 {
973         struct stream_struct *tmp;
974
975         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
976                                    (*num_streams)+1);
977         if (tmp == NULL) {
978                 return false;
979         }
980
981         tmp[*num_streams].name = talloc_strdup(tmp, name);
982         if (tmp[*num_streams].name == NULL) {
983                 return false;
984         }
985
986         tmp[*num_streams].size = size;
987         tmp[*num_streams].alloc_size = alloc_size;
988
989         *streams = tmp;
990         *num_streams += 1;
991         return true;
992 }
993
994 struct streaminfo_state {
995         TALLOC_CTX *mem_ctx;
996         vfs_handle_struct *handle;
997         unsigned int num_streams;
998         struct stream_struct *streams;
999         NTSTATUS status;
1000 };
1001
1002 static bool collect_one_stream(const struct smb_filename *dirfname,
1003                                const char *dirent,
1004                                void *private_data)
1005 {
1006         const char *dirname = dirfname->base_name;
1007         struct streaminfo_state *state =
1008                 (struct streaminfo_state *)private_data;
1009         struct smb_filename *smb_fname = NULL;
1010         char *sname = NULL;
1011         bool ret;
1012
1013         sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
1014         if (sname == NULL) {
1015                 state->status = NT_STATUS_NO_MEMORY;
1016                 ret = false;
1017                 goto out;
1018         }
1019
1020         smb_fname = synthetic_smb_fname(talloc_tos(),
1021                                         sname,
1022                                         NULL,
1023                                         NULL,
1024                                         dirfname->twrp,
1025                                         0);
1026         if (smb_fname == NULL) {
1027                 state->status = NT_STATUS_NO_MEMORY;
1028                 ret = false;
1029                 goto out;
1030         }
1031
1032         if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
1033                 DEBUG(10, ("Could not stat %s: %s\n", sname,
1034                            strerror(errno)));
1035                 ret = true;
1036                 goto out;
1037         }
1038
1039         if (!add_one_stream(state->mem_ctx,
1040                             &state->num_streams, &state->streams,
1041                             dirent, smb_fname->st.st_ex_size,
1042                             SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
1043                                                    &smb_fname->st))) {
1044                 state->status = NT_STATUS_NO_MEMORY;
1045                 ret = false;
1046                 goto out;
1047         }
1048
1049         ret = true;
1050  out:
1051         TALLOC_FREE(sname);
1052         TALLOC_FREE(smb_fname);
1053         return ret;
1054 }
1055
1056 static NTSTATUS streams_depot_fstreaminfo(vfs_handle_struct *handle,
1057                                          struct files_struct *fsp,
1058                                          TALLOC_CTX *mem_ctx,
1059                                          unsigned int *pnum_streams,
1060                                          struct stream_struct **pstreams)
1061 {
1062         struct smb_filename *smb_fname_base = NULL;
1063         int ret;
1064         NTSTATUS status;
1065         struct streaminfo_state state;
1066
1067         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1068                                         fsp->fsp_name->base_name,
1069                                         NULL,
1070                                         NULL,
1071                                         fsp->fsp_name->twrp,
1072                                         fsp->fsp_name->flags);
1073         if (smb_fname_base == NULL) {
1074                 return NT_STATUS_NO_MEMORY;
1075         }
1076
1077         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
1078         if (ret == -1) {
1079                 status = map_nt_error_from_unix(errno);
1080                 goto out;
1081         }
1082
1083         state.streams = *pstreams;
1084         state.num_streams = *pnum_streams;
1085         state.mem_ctx = mem_ctx;
1086         state.handle = handle;
1087         state.status = NT_STATUS_OK;
1088
1089         status = walk_streams(handle,
1090                                 smb_fname_base,
1091                                 NULL,
1092                                 collect_one_stream,
1093                                 &state);
1094
1095         if (!NT_STATUS_IS_OK(status)) {
1096                 TALLOC_FREE(state.streams);
1097                 goto out;
1098         }
1099
1100         if (!NT_STATUS_IS_OK(state.status)) {
1101                 TALLOC_FREE(state.streams);
1102                 status = state.status;
1103                 goto out;
1104         }
1105
1106         *pnum_streams = state.num_streams;
1107         *pstreams = state.streams;
1108         status = SMB_VFS_NEXT_FSTREAMINFO(handle,
1109                                 fsp->base_fsp ? fsp->base_fsp : fsp,
1110                                 mem_ctx,
1111                                 pnum_streams,
1112                                 pstreams);
1113
1114  out:
1115         TALLOC_FREE(smb_fname_base);
1116         return status;
1117 }
1118
1119 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
1120                         enum timestamp_set_resolution *p_ts_res)
1121 {
1122         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
1123 }
1124
1125 static struct vfs_fn_pointers vfs_streams_depot_fns = {
1126         .fs_capabilities_fn = streams_depot_fs_capabilities,
1127         .openat_fn = streams_depot_openat,
1128         .stat_fn = streams_depot_stat,
1129         .lstat_fn = streams_depot_lstat,
1130         .unlinkat_fn = streams_depot_unlinkat,
1131         .renameat_fn = streams_depot_renameat,
1132         .fstreaminfo_fn = streams_depot_fstreaminfo,
1133 };
1134
1135 static_decl_vfs;
1136 NTSTATUS vfs_streams_depot_init(TALLOC_CTX *ctx)
1137 {
1138         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
1139                                 &vfs_streams_depot_fns);
1140 }