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