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