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