smbd: add twrp arg to synthetic_smb_fname()
[samba.git] / source3 / modules / vfs_streams_depot.c
1 /*
2  * Store streams in a separate subdirectory
3  *
4  * Copyright (C) Volker Lendecke, 2007
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "system/filesys.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_VFS
26
27 /*
28  * Excerpt from a mail from tridge:
29  *
30  * Volker, what I'm thinking of is this:
31  * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream1
32  * /mount-point/.streams/XX/YY/aaaa.bbbb/namedstream2
33  *
34  * where XX/YY is a 2 level hash based on the fsid/inode. "aaaa.bbbb"
35  * is the fsid/inode. "namedstreamX" is a file named after the stream
36  * name.
37  */
38
39 static uint32_t hash_fn(DATA_BLOB key)
40 {
41         uint32_t value; /* Used to compute the hash value.  */
42         uint32_t i;     /* Used to cycle through random values. */
43
44         /* Set the initial value from the key size. */
45         for (value = 0x238F13AF * key.length, i=0; i < key.length; i++)
46                 value = (value + (key.data[i] << (i*5 % 24)));
47
48         return (1103515243 * value + 12345);
49 }
50
51 /*
52  * With the hashing scheme based on the inode we need to protect against
53  * streams showing up on files with re-used inodes. This can happen if we
54  * create a stream directory from within Samba, and a local process or NFS
55  * client deletes the file without deleting the streams directory. When the
56  * inode is re-used and the stream directory is still around, the streams in
57  * there would be show up as belonging to the new file.
58  *
59  * There are several workarounds for this, probably the easiest one is on
60  * systems which have a true birthtime stat element: When the file has a later
61  * birthtime than the streams directory, then we have to recreate the
62  * directory.
63  *
64  * The other workaround is to somehow mark the file as generated by Samba with
65  * something that a NFS client would not do. The closest one is a special
66  * xattr value being set. On systems which do not support xattrs, it might be
67  * an option to put in a special ACL entry for a non-existing group.
68  */
69
70 static bool file_is_valid(vfs_handle_struct *handle,
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_SETXATTR(handle->conn, smb_fname, 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         int ret;
134
135         check_valid = lp_parm_bool(SNUM(handle->conn),
136                       "streams_depot", "check_valid", true);
137
138         tmp = talloc_asprintf(talloc_tos(), "%s/.streams",
139                 handle->conn->connectpath);
140
141         if (tmp == NULL) {
142                 errno = ENOMEM;
143                 goto fail;
144         }
145
146         rootdir = lp_parm_substituted_string(talloc_tos(), lp_sub,
147                 SNUM(handle->conn), "streams_depot", "directory",
148                 tmp);
149         if (rootdir == NULL) {
150                 errno = ENOMEM;
151                 goto fail;
152         }
153
154         rootdir_fname = synthetic_smb_fname(talloc_tos(),
155                                         rootdir,
156                                         NULL,
157                                         NULL,
158                                         smb_fname->twrp,
159                                         smb_fname->flags);
160         if (rootdir_fname == NULL) {
161                 errno = ENOMEM;
162                 goto fail;
163         }
164
165         /* Stat the base file if it hasn't already been done. */
166         if (base_sbuf == NULL) {
167                 struct smb_filename *smb_fname_base;
168
169                 smb_fname_base = synthetic_smb_fname(
170                                         talloc_tos(),
171                                         smb_fname->base_name,
172                                         NULL,
173                                         NULL,
174                                         smb_fname->twrp,
175                                         smb_fname->flags);
176                 if (smb_fname_base == NULL) {
177                         errno = ENOMEM;
178                         goto fail;
179                 }
180                 if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
181                         TALLOC_FREE(smb_fname_base);
182                         goto fail;
183                 }
184                 base_sbuf_tmp = smb_fname_base->st;
185                 TALLOC_FREE(smb_fname_base);
186         } else {
187                 base_sbuf_tmp = *base_sbuf;
188         }
189
190         id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
191
192         push_file_id_16((char *)id_buf, &id);
193
194         hash = hash_fn(data_blob_const(id_buf, sizeof(id_buf)));
195
196         first = hash & 0xff;
197         second = (hash >> 8) & 0xff;
198
199         id_hex = hex_encode_talloc(talloc_tos(), id_buf, sizeof(id_buf));
200
201         if (id_hex == NULL) {
202                 errno = ENOMEM;
203                 goto fail;
204         }
205
206         result = talloc_asprintf(talloc_tos(), "%s/%2.2X/%2.2X/%s", rootdir,
207                                  first, second, id_hex);
208
209         TALLOC_FREE(id_hex);
210
211         if (result == NULL) {
212                 errno = ENOMEM;
213                 return NULL;
214         }
215
216         smb_fname_hash = synthetic_smb_fname(talloc_tos(),
217                                         result,
218                                         NULL,
219                                         NULL,
220                                         smb_fname->twrp,
221                                         smb_fname->flags);
222         if (smb_fname_hash == NULL) {
223                 errno = ENOMEM;
224                 goto fail;
225         }
226
227         if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
228                 struct smb_filename *smb_fname_new = NULL;
229                 char *newname;
230                 bool delete_lost;
231
232                 if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
233                         errno = EINVAL;
234                         goto fail;
235                 }
236
237                 if (!check_valid ||
238                     file_is_valid(handle, smb_fname)) {
239                         return result;
240                 }
241
242                 /*
243                  * Someone has recreated a file under an existing inode
244                  * without deleting the streams directory.
245                  * Move it away or remove if streams_depot:delete_lost is set.
246                  */
247
248         again:
249                 delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
250                                            "delete_lost", false);
251
252                 if (delete_lost) {
253                         DEBUG(3, ("Someone has recreated a file under an "
254                               "existing inode. Removing: %s\n",
255                               smb_fname_hash->base_name));
256                         recursive_rmdir(talloc_tos(), handle->conn,
257                                         smb_fname_hash);
258                         SMB_VFS_NEXT_UNLINKAT(handle,
259                                         handle->conn->cwd_fsp,
260                                         smb_fname_hash,
261                                         AT_REMOVEDIR);
262                 } else {
263                         newname = talloc_asprintf(talloc_tos(), "lost-%lu",
264                                                   random());
265                         DEBUG(3, ("Someone has recreated a file under an "
266                               "existing inode. Renaming: %s to: %s\n",
267                               smb_fname_hash->base_name,
268                               newname));
269                         if (newname == NULL) {
270                                 errno = ENOMEM;
271                                 goto fail;
272                         }
273
274                         smb_fname_new = synthetic_smb_fname(
275                                                 talloc_tos(),
276                                                 newname,
277                                                 NULL,
278                                                 NULL,
279                                                 smb_fname->twrp,
280                                                 smb_fname->flags);
281                         TALLOC_FREE(newname);
282                         if (smb_fname_new == NULL) {
283                                 errno = ENOMEM;
284                                 goto fail;
285                         }
286
287                         if (SMB_VFS_NEXT_RENAMEAT(handle,
288                                         handle->conn->cwd_fsp,
289                                         smb_fname_hash,
290                                         handle->conn->cwd_fsp,
291                                         smb_fname_new) == -1) {
292                                 TALLOC_FREE(smb_fname_new);
293                                 if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
294                                         goto again;
295                                 }
296                                 goto fail;
297                         }
298
299                         TALLOC_FREE(smb_fname_new);
300                 }
301         }
302
303         if (!create_it) {
304                 errno = ENOENT;
305                 goto fail;
306         }
307
308         ret = SMB_VFS_NEXT_MKDIRAT(handle,
309                                 handle->conn->cwd_fsp,
310                                 rootdir_fname,
311                                 0755);
312         if ((ret != 0) && (errno != EEXIST)) {
313                 goto fail;
314         }
315
316         tmp = talloc_asprintf(result, "%s/%2.2X", rootdir, first);
317         if (tmp == NULL) {
318                 errno = ENOMEM;
319                 goto fail;
320         }
321
322         tmp_fname = synthetic_smb_fname(talloc_tos(),
323                                         tmp,
324                                         NULL,
325                                         NULL,
326                                         smb_fname->twrp,
327                                         smb_fname->flags);
328         if (tmp_fname == NULL) {
329                 errno = ENOMEM;
330                 goto fail;
331         }
332
333         ret = SMB_VFS_NEXT_MKDIRAT(handle,
334                                 handle->conn->cwd_fsp,
335                                 tmp_fname,
336                                 0755);
337         if ((ret != 0) && (errno != EEXIST)) {
338                 goto fail;
339         }
340
341         TALLOC_FREE(tmp);
342         TALLOC_FREE(tmp_fname);
343
344         tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
345                               second);
346         if (tmp == NULL) {
347                 errno = ENOMEM;
348                 goto fail;
349         }
350
351         tmp_fname = synthetic_smb_fname(talloc_tos(),
352                                         tmp,
353                                         NULL,
354                                         NULL,
355                                         smb_fname->twrp,
356                                         smb_fname->flags);
357         if (tmp_fname == NULL) {
358                 errno = ENOMEM;
359                 goto fail;
360         }
361
362         ret = SMB_VFS_NEXT_MKDIRAT(handle,
363                         handle->conn->cwd_fsp,
364                         tmp_fname,
365                         0755);
366         if ((ret != 0) && (errno != EEXIST)) {
367                 goto fail;
368         }
369
370         TALLOC_FREE(tmp);
371         TALLOC_FREE(tmp_fname);
372
373         /* smb_fname_hash is the struct smb_filename version of 'result' */
374         ret = SMB_VFS_NEXT_MKDIRAT(handle,
375                         handle->conn->cwd_fsp,
376                         smb_fname_hash,
377                         0755);
378         if ((ret != 0) && (errno != EEXIST)) {
379                 goto fail;
380         }
381
382         if (check_valid && !mark_file_valid(handle, smb_fname)) {
383                 goto fail;
384         }
385
386         TALLOC_FREE(rootdir_fname);
387         TALLOC_FREE(rootdir);
388         TALLOC_FREE(tmp_fname);
389         TALLOC_FREE(smb_fname_hash);
390         return result;
391
392  fail:
393         TALLOC_FREE(rootdir_fname);
394         TALLOC_FREE(rootdir);
395         TALLOC_FREE(tmp_fname);
396         TALLOC_FREE(smb_fname_hash);
397         TALLOC_FREE(result);
398         return NULL;
399 }
400 /**
401  * Given a stream name, populate smb_fname_out with the actual location of the
402  * stream.
403  */
404 static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
405                                  const struct smb_filename *smb_fname,
406                                  struct smb_filename **smb_fname_out,
407                                  bool create_dir)
408 {
409         char *dirname, *stream_fname;
410         const char *stype;
411         NTSTATUS status;
412
413         *smb_fname_out = NULL;
414
415         stype = strchr_m(smb_fname->stream_name + 1, ':');
416
417         if (stype) {
418                 if (strcasecmp_m(stype, ":$DATA") != 0) {
419                         return NT_STATUS_INVALID_PARAMETER;
420                 }
421         }
422
423         dirname = stream_dir(handle, smb_fname, NULL, create_dir);
424
425         if (dirname == NULL) {
426                 status = map_nt_error_from_unix(errno);
427                 goto fail;
428         }
429
430         stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
431                                        smb_fname->stream_name);
432
433         if (stream_fname == NULL) {
434                 status = NT_STATUS_NO_MEMORY;
435                 goto fail;
436         }
437
438         if (stype == NULL) {
439                 /* Append an explicit stream type if one wasn't specified. */
440                 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
441                                                stream_fname);
442                 if (stream_fname == NULL) {
443                         status = NT_STATUS_NO_MEMORY;
444                         goto fail;
445                 }
446         } else {
447                 /* Normalize the stream type to upercase. */
448                 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
449                         status = NT_STATUS_INVALID_PARAMETER;
450                         goto fail;
451                 }
452         }
453
454         DEBUG(10, ("stream filename = %s\n", stream_fname));
455
456         /* Create an smb_filename with stream_name == NULL. */
457         *smb_fname_out = synthetic_smb_fname(talloc_tos(),
458                                         stream_fname,
459                                         NULL,
460                                         NULL,
461                                         smb_fname->twrp,
462                                         smb_fname->flags);
463         if (*smb_fname_out == NULL) {
464                 return NT_STATUS_NO_MEMORY;
465         }
466
467         return NT_STATUS_OK;
468
469  fail:
470         DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
471         TALLOC_FREE(*smb_fname_out);
472         return status;
473 }
474
475 static NTSTATUS walk_streams(vfs_handle_struct *handle,
476                              struct smb_filename *smb_fname_base,
477                              char **pdirname,
478                              bool (*fn)(const struct smb_filename *dirname,
479                                         const char *dirent,
480                                         void *private_data),
481                              void *private_data)
482 {
483         char *dirname;
484         struct smb_filename *dir_smb_fname = NULL;
485         struct smb_Dir *dir_hnd = NULL;
486         const char *dname = NULL;
487         long offset = 0;
488         char *talloced = NULL;
489
490         dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
491                              false);
492
493         if (dirname == NULL) {
494                 if (errno == ENOENT) {
495                         /*
496                          * no stream around
497                          */
498                         return NT_STATUS_OK;
499                 }
500                 return map_nt_error_from_unix(errno);
501         }
502
503         DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
504
505         dir_smb_fname = synthetic_smb_fname(talloc_tos(),
506                                         dirname,
507                                         NULL,
508                                         NULL,
509                                         smb_fname_base->twrp,
510                                         smb_fname_base->flags);
511         if (dir_smb_fname == NULL) {
512                 TALLOC_FREE(dirname);
513                 return NT_STATUS_NO_MEMORY;
514         }
515
516         dir_hnd = OpenDir(talloc_tos(), handle->conn, dir_smb_fname, NULL, 0);
517         if (dir_hnd == NULL) {
518                 TALLOC_FREE(dir_smb_fname);
519                 TALLOC_FREE(dirname);
520                 return map_nt_error_from_unix(errno);
521         }
522
523         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
524                != NULL)
525         {
526                 if (ISDOT(dname) || ISDOTDOT(dname)) {
527                         TALLOC_FREE(talloced);
528                         continue;
529                 }
530
531                 DBG_DEBUG("dirent=%s\n", dname);
532
533                 if (!fn(dir_smb_fname, dname, private_data)) {
534                         TALLOC_FREE(talloced);
535                         break;
536                 }
537                 TALLOC_FREE(talloced);
538         }
539
540         TALLOC_FREE(dir_smb_fname);
541         TALLOC_FREE(dir_hnd);
542
543         if (pdirname != NULL) {
544                 *pdirname = dirname;
545         }
546         else {
547                 TALLOC_FREE(dirname);
548         }
549
550         return NT_STATUS_OK;
551 }
552
553 static int streams_depot_stat(vfs_handle_struct *handle,
554                               struct smb_filename *smb_fname)
555 {
556         struct smb_filename *smb_fname_stream = NULL;
557         NTSTATUS status;
558         int ret = -1;
559
560         DEBUG(10, ("streams_depot_stat called for [%s]\n",
561                    smb_fname_str_dbg(smb_fname)));
562
563         if (!is_named_stream(smb_fname)) {
564                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
565         }
566
567         /* Stat the actual stream now. */
568         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
569                                   false);
570         if (!NT_STATUS_IS_OK(status)) {
571                 ret = -1;
572                 errno = map_errno_from_nt_status(status);
573                 goto done;
574         }
575
576         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
577
578         /* Update the original smb_fname with the stat info. */
579         smb_fname->st = smb_fname_stream->st;
580  done:
581         TALLOC_FREE(smb_fname_stream);
582         return ret;
583 }
584
585
586
587 static int streams_depot_lstat(vfs_handle_struct *handle,
588                                struct smb_filename *smb_fname)
589 {
590         struct smb_filename *smb_fname_stream = NULL;
591         NTSTATUS status;
592         int ret = -1;
593
594         DEBUG(10, ("streams_depot_lstat called for [%s]\n",
595                    smb_fname_str_dbg(smb_fname)));
596
597         if (!is_named_stream(smb_fname)) {
598                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
599         }
600
601         /* Stat the actual stream now. */
602         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
603                                   false);
604         if (!NT_STATUS_IS_OK(status)) {
605                 ret = -1;
606                 errno = map_errno_from_nt_status(status);
607                 goto done;
608         }
609
610         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
611
612  done:
613         TALLOC_FREE(smb_fname_stream);
614         return ret;
615 }
616
617 static int streams_depot_open(vfs_handle_struct *handle,
618                               struct smb_filename *smb_fname,
619                               files_struct *fsp, int flags, mode_t mode)
620 {
621         struct smb_filename *smb_fname_stream = NULL;
622         struct smb_filename *smb_fname_base = NULL;
623         NTSTATUS status;
624         int ret = -1;
625
626         if (!is_named_stream(smb_fname)) {
627                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
628         }
629
630         /* Ensure the base file still exists. */
631         smb_fname_base = synthetic_smb_fname(talloc_tos(),
632                                         smb_fname->base_name,
633                                         NULL,
634                                         NULL,
635                                         smb_fname->twrp,
636                                         smb_fname->flags);
637         if (smb_fname_base == NULL) {
638                 ret = -1;
639                 errno = ENOMEM;
640                 goto done;
641         }
642
643         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
644         if (ret == -1) {
645                 goto done;
646         }
647
648         /* Determine the stream name, and then open it. */
649         status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
650         if (!NT_STATUS_IS_OK(status)) {
651                 ret = -1;
652                 errno = map_errno_from_nt_status(status);
653                 goto done;
654         }
655
656         ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
657
658  done:
659         TALLOC_FREE(smb_fname_stream);
660         TALLOC_FREE(smb_fname_base);
661         return ret;
662 }
663
664 static int streams_depot_unlink_internal(vfs_handle_struct *handle,
665                                 struct files_struct *dirfsp,
666                                 const struct smb_filename *smb_fname,
667                                 int flags)
668 {
669         struct smb_filename *smb_fname_base = NULL;
670         int ret = -1;
671
672         DEBUG(10, ("streams_depot_unlink called for %s\n",
673                    smb_fname_str_dbg(smb_fname)));
674
675         /* If there is a valid stream, just unlink the stream and return. */
676         if (is_named_stream(smb_fname)) {
677                 struct smb_filename *smb_fname_stream = NULL;
678                 NTSTATUS status;
679
680                 status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
681                                           false);
682                 if (!NT_STATUS_IS_OK(status)) {
683                         errno = map_errno_from_nt_status(status);
684                         return -1;
685                 }
686
687                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
688                                 dirfsp,
689                                 smb_fname_stream,
690                                 0);
691
692                 TALLOC_FREE(smb_fname_stream);
693                 return ret;
694         }
695
696         /*
697          * We potentially need to delete the per-inode streams directory
698          */
699
700         smb_fname_base = synthetic_smb_fname(talloc_tos(),
701                                         smb_fname->base_name,
702                                         NULL,
703                                         NULL,
704                                         smb_fname->twrp,
705                                         smb_fname->flags);
706         if (smb_fname_base == NULL) {
707                 errno = ENOMEM;
708                 return -1;
709         }
710
711         if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
712                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
713         } else {
714                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
715         }
716
717         if (ret == -1) {
718                 TALLOC_FREE(smb_fname_base);
719                 return -1;
720         }
721
722         /*
723          * We know the unlink should succeed as the ACL
724          * check is already done in the caller. Remove the
725          * file *after* the streams.
726          */
727         {
728                 char *dirname = stream_dir(handle, smb_fname_base,
729                                            &smb_fname_base->st, false);
730
731                 if (dirname != NULL) {
732                         struct smb_filename *smb_fname_dir =
733                                 synthetic_smb_fname(talloc_tos(),
734                                                 dirname,
735                                                 NULL,
736                                                 NULL,
737                                                 smb_fname->twrp,
738                                                 smb_fname->flags);
739                         if (smb_fname_dir == NULL) {
740                                 TALLOC_FREE(smb_fname_base);
741                                 TALLOC_FREE(dirname);
742                                 errno = ENOMEM;
743                                 return -1;
744                         }
745                         SMB_VFS_NEXT_UNLINKAT(handle,
746                                 dirfsp,
747                                 smb_fname_dir,
748                                 AT_REMOVEDIR);
749                         TALLOC_FREE(smb_fname_dir);
750                 }
751                 TALLOC_FREE(dirname);
752         }
753
754         ret = SMB_VFS_NEXT_UNLINKAT(handle,
755                                 dirfsp,
756                                 smb_fname,
757                                 flags);
758         TALLOC_FREE(smb_fname_base);
759         return ret;
760 }
761
762 static int streams_depot_rmdir_internal(vfs_handle_struct *handle,
763                         struct files_struct *dirfsp,
764                         const struct smb_filename *smb_fname)
765 {
766         struct smb_filename *smb_fname_base = NULL;
767         int ret = -1;
768
769         DBG_DEBUG("called for %s\n", smb_fname->base_name);
770
771         /*
772          * We potentially need to delete the per-inode streams directory
773          */
774
775         smb_fname_base = synthetic_smb_fname(talloc_tos(),
776                                 smb_fname->base_name,
777                                 NULL,
778                                 NULL,
779                                 smb_fname->twrp,
780                                 smb_fname->flags);
781         if (smb_fname_base == NULL) {
782                 errno = ENOMEM;
783                 return -1;
784         }
785
786         if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
787                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
788         } else {
789                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
790         }
791
792         if (ret == -1) {
793                 TALLOC_FREE(smb_fname_base);
794                 return -1;
795         }
796
797         /*
798          * We know the rmdir should succeed as the ACL
799          * check is already done in the caller. Remove the
800          * directory *after* the streams.
801          */
802         {
803                 char *dirname = stream_dir(handle, smb_fname_base,
804                                            &smb_fname_base->st, false);
805
806                 if (dirname != NULL) {
807                         struct smb_filename *smb_fname_dir =
808                                 synthetic_smb_fname(talloc_tos(),
809                                                 dirname,
810                                                 NULL,
811                                                 NULL,
812                                                 smb_fname->twrp,
813                                                 smb_fname->flags);
814                         if (smb_fname_dir == NULL) {
815                                 TALLOC_FREE(smb_fname_base);
816                                 TALLOC_FREE(dirname);
817                                 errno = ENOMEM;
818                                 return -1;
819                         }
820                         SMB_VFS_NEXT_UNLINKAT(handle,
821                                         dirfsp,
822                                         smb_fname_dir,
823                                         AT_REMOVEDIR);
824                         TALLOC_FREE(smb_fname_dir);
825                 }
826                 TALLOC_FREE(dirname);
827         }
828
829         ret = SMB_VFS_NEXT_UNLINKAT(handle,
830                                 dirfsp,
831                                 smb_fname_base,
832                                 AT_REMOVEDIR);
833         TALLOC_FREE(smb_fname_base);
834         return ret;
835 }
836
837 static int streams_depot_unlinkat(vfs_handle_struct *handle,
838                         struct files_struct *dirfsp,
839                         const struct smb_filename *smb_fname,
840                         int flags)
841 {
842         int ret;
843         SMB_ASSERT(dirfsp == dirfsp->conn->cwd_fsp);
844         if (flags & AT_REMOVEDIR) {
845                 ret = streams_depot_rmdir_internal(handle,
846                                 dirfsp,
847                                 smb_fname);
848         } else {
849                 ret = streams_depot_unlink_internal(handle,
850                                 dirfsp,
851                                 smb_fname,
852                                 flags);
853         }
854         return ret;
855 }
856
857 static int streams_depot_renameat(vfs_handle_struct *handle,
858                                 files_struct *srcfsp,
859                                 const struct smb_filename *smb_fname_src,
860                                 files_struct *dstfsp,
861                                 const struct smb_filename *smb_fname_dst)
862 {
863         struct smb_filename *smb_fname_src_stream = NULL;
864         struct smb_filename *smb_fname_dst_stream = NULL;
865         bool src_is_stream, dst_is_stream;
866         NTSTATUS status;
867         int ret = -1;
868
869         DEBUG(10, ("streams_depot_renameat called for %s => %s\n",
870                    smb_fname_str_dbg(smb_fname_src),
871                    smb_fname_str_dbg(smb_fname_dst)));
872
873         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
874         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
875
876         if (!src_is_stream && !dst_is_stream) {
877                 return SMB_VFS_NEXT_RENAMEAT(handle,
878                                         srcfsp,
879                                         smb_fname_src,
880                                         dstfsp,
881                                         smb_fname_dst);
882         }
883
884         /* for now don't allow renames from or to the default stream */
885         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
886             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
887                 errno = ENOSYS;
888                 goto done;
889         }
890
891         status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
892                                   false);
893         if (!NT_STATUS_IS_OK(status)) {
894                 errno = map_errno_from_nt_status(status);
895                 goto done;
896         }
897
898         status = stream_smb_fname(handle, smb_fname_dst,
899                                   &smb_fname_dst_stream, false);
900         if (!NT_STATUS_IS_OK(status)) {
901                 errno = map_errno_from_nt_status(status);
902                 goto done;
903         }
904
905         ret = SMB_VFS_NEXT_RENAMEAT(handle,
906                                 srcfsp,
907                                 smb_fname_src_stream,
908                                 dstfsp,
909                                 smb_fname_dst_stream);
910
911 done:
912         TALLOC_FREE(smb_fname_src_stream);
913         TALLOC_FREE(smb_fname_dst_stream);
914         return ret;
915 }
916
917 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
918                            struct stream_struct **streams,
919                            const char *name, off_t size,
920                            off_t alloc_size)
921 {
922         struct stream_struct *tmp;
923
924         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
925                                    (*num_streams)+1);
926         if (tmp == NULL) {
927                 return false;
928         }
929
930         tmp[*num_streams].name = talloc_strdup(tmp, name);
931         if (tmp[*num_streams].name == NULL) {
932                 return false;
933         }
934
935         tmp[*num_streams].size = size;
936         tmp[*num_streams].alloc_size = alloc_size;
937
938         *streams = tmp;
939         *num_streams += 1;
940         return true;
941 }
942
943 struct streaminfo_state {
944         TALLOC_CTX *mem_ctx;
945         vfs_handle_struct *handle;
946         unsigned int num_streams;
947         struct stream_struct *streams;
948         NTSTATUS status;
949 };
950
951 static bool collect_one_stream(const struct smb_filename *dirfname,
952                                const char *dirent,
953                                void *private_data)
954 {
955         const char *dirname = dirfname->base_name;
956         struct streaminfo_state *state =
957                 (struct streaminfo_state *)private_data;
958         struct smb_filename *smb_fname = NULL;
959         char *sname = NULL;
960         bool ret;
961
962         sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
963         if (sname == NULL) {
964                 state->status = NT_STATUS_NO_MEMORY;
965                 ret = false;
966                 goto out;
967         }
968
969         smb_fname = synthetic_smb_fname(talloc_tos(),
970                                         sname,
971                                         NULL,
972                                         NULL,
973                                         dirfname->twrp,
974                                         0);
975         if (smb_fname == NULL) {
976                 state->status = NT_STATUS_NO_MEMORY;
977                 ret = false;
978                 goto out;
979         }
980
981         if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
982                 DEBUG(10, ("Could not stat %s: %s\n", sname,
983                            strerror(errno)));
984                 ret = true;
985                 goto out;
986         }
987
988         if (!add_one_stream(state->mem_ctx,
989                             &state->num_streams, &state->streams,
990                             dirent, smb_fname->st.st_ex_size,
991                             SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
992                                                    &smb_fname->st))) {
993                 state->status = NT_STATUS_NO_MEMORY;
994                 ret = false;
995                 goto out;
996         }
997
998         ret = true;
999  out:
1000         TALLOC_FREE(sname);
1001         TALLOC_FREE(smb_fname);
1002         return ret;
1003 }
1004
1005 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
1006                                          struct files_struct *fsp,
1007                                          const struct smb_filename *smb_fname,
1008                                          TALLOC_CTX *mem_ctx,
1009                                          unsigned int *pnum_streams,
1010                                          struct stream_struct **pstreams)
1011 {
1012         struct smb_filename *smb_fname_base = NULL;
1013         int ret;
1014         NTSTATUS status;
1015         struct streaminfo_state state;
1016
1017         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1018                                         smb_fname->base_name,
1019                                         NULL,
1020                                         NULL,
1021                                         smb_fname->twrp,
1022                                         smb_fname->flags);
1023         if (smb_fname_base == NULL) {
1024                 return NT_STATUS_NO_MEMORY;
1025         }
1026
1027         if ((fsp != NULL) && (fsp->fh->fd != -1)) {
1028                 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
1029         }
1030         else {
1031                 if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
1032                         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
1033                 } else {
1034                         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
1035                 }
1036         }
1037
1038         if (ret == -1) {
1039                 status = map_nt_error_from_unix(errno);
1040                 goto out;
1041         }
1042
1043         state.streams = *pstreams;
1044         state.num_streams = *pnum_streams;
1045         state.mem_ctx = mem_ctx;
1046         state.handle = handle;
1047         state.status = NT_STATUS_OK;
1048
1049         if (S_ISLNK(smb_fname_base->st.st_ex_mode)) {
1050                 /*
1051                  * Currently we do't have SMB_VFS_LLISTXATTR
1052                  * inside the VFS which means there's no way
1053                  * to cope with a symlink when lp_posix_pathnames().
1054                  * returns true. For now ignore links.
1055                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
1056                  */
1057                 status = NT_STATUS_OK;
1058         } else {
1059                 status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
1060                               &state);
1061         }
1062
1063         if (!NT_STATUS_IS_OK(status)) {
1064                 TALLOC_FREE(state.streams);
1065                 goto out;
1066         }
1067
1068         if (!NT_STATUS_IS_OK(state.status)) {
1069                 TALLOC_FREE(state.streams);
1070                 status = state.status;
1071                 goto out;
1072         }
1073
1074         *pnum_streams = state.num_streams;
1075         *pstreams = state.streams;
1076         status = SMB_VFS_NEXT_STREAMINFO(handle,
1077                                 fsp,
1078                                 smb_fname_base,
1079                                 mem_ctx,
1080                                 pnum_streams,
1081                                 pstreams);
1082
1083  out:
1084         TALLOC_FREE(smb_fname_base);
1085         return status;
1086 }
1087
1088 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
1089                         enum timestamp_set_resolution *p_ts_res)
1090 {
1091         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
1092 }
1093
1094 static struct vfs_fn_pointers vfs_streams_depot_fns = {
1095         .fs_capabilities_fn = streams_depot_fs_capabilities,
1096         .open_fn = streams_depot_open,
1097         .stat_fn = streams_depot_stat,
1098         .lstat_fn = streams_depot_lstat,
1099         .unlinkat_fn = streams_depot_unlinkat,
1100         .renameat_fn = streams_depot_renameat,
1101         .streaminfo_fn = streams_depot_streaminfo,
1102 };
1103
1104 static_decl_vfs;
1105 NTSTATUS vfs_streams_depot_init(TALLOC_CTX *ctx)
1106 {
1107         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
1108                                 &vfs_streams_depot_fns);
1109 }