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