streams_depot: Only create the subdirectories with O_CREAT
[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 stat_ex *base_sbuf,
469                                  const struct smb_filename *smb_fname,
470                                  struct smb_filename **smb_fname_out,
471                                  bool create_dir)
472 {
473         char *dirname, *stream_fname;
474         const char *stype;
475         NTSTATUS status;
476
477         *smb_fname_out = NULL;
478
479         stype = strchr_m(smb_fname->stream_name + 1, ':');
480
481         if (stype) {
482                 if (strcasecmp_m(stype, ":$DATA") != 0) {
483                         return NT_STATUS_INVALID_PARAMETER;
484                 }
485         }
486
487         dirname = stream_dir(handle, smb_fname, base_sbuf, create_dir);
488
489         if (dirname == NULL) {
490                 status = map_nt_error_from_unix(errno);
491                 goto fail;
492         }
493
494         stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
495                                        smb_fname->stream_name);
496
497         if (stream_fname == NULL) {
498                 status = NT_STATUS_NO_MEMORY;
499                 goto fail;
500         }
501
502         if (stype == NULL) {
503                 /* Append an explicit stream type if one wasn't specified. */
504                 stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
505                                                stream_fname);
506                 if (stream_fname == NULL) {
507                         status = NT_STATUS_NO_MEMORY;
508                         goto fail;
509                 }
510         } else {
511                 /* Normalize the stream type to upercase. */
512                 if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
513                         status = NT_STATUS_INVALID_PARAMETER;
514                         goto fail;
515                 }
516         }
517
518         DEBUG(10, ("stream filename = %s\n", stream_fname));
519
520         /* Create an smb_filename with stream_name == NULL. */
521         *smb_fname_out = synthetic_smb_fname(talloc_tos(),
522                                         stream_fname,
523                                         NULL,
524                                         NULL,
525                                         smb_fname->twrp,
526                                         smb_fname->flags);
527         if (*smb_fname_out == NULL) {
528                 return NT_STATUS_NO_MEMORY;
529         }
530
531         return NT_STATUS_OK;
532
533  fail:
534         DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
535         TALLOC_FREE(*smb_fname_out);
536         return status;
537 }
538
539 static NTSTATUS walk_streams(vfs_handle_struct *handle,
540                              struct smb_filename *smb_fname_base,
541                              char **pdirname,
542                              bool (*fn)(const struct smb_filename *dirname,
543                                         const char *dirent,
544                                         void *private_data),
545                              void *private_data)
546 {
547         char *dirname;
548         char *rootdir = NULL;
549         char *orig_connectpath = NULL;
550         struct smb_filename *dir_smb_fname = NULL;
551         struct smb_Dir *dir_hnd = NULL;
552         const char *dname = NULL;
553         long offset = 0;
554         char *talloced = NULL;
555         NTSTATUS status;
556
557         dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
558                              false);
559
560         if (dirname == NULL) {
561                 if (errno == ENOENT) {
562                         /*
563                          * no stream around
564                          */
565                         return NT_STATUS_OK;
566                 }
567                 return map_nt_error_from_unix(errno);
568         }
569
570         DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
571
572         dir_smb_fname = synthetic_smb_fname(talloc_tos(),
573                                         dirname,
574                                         NULL,
575                                         NULL,
576                                         smb_fname_base->twrp,
577                                         smb_fname_base->flags);
578         if (dir_smb_fname == NULL) {
579                 TALLOC_FREE(dirname);
580                 return NT_STATUS_NO_MEMORY;
581         }
582
583         /*
584          * For OpenDir to succeed if the stream rootdir is outside
585          * the share path, we must temporarily swap out the connect
586          * path for this share. We're dealing with absolute paths
587          * here so we don't care about chdir calls.
588          */
589         rootdir = stream_rootdir(handle, talloc_tos());
590         if (rootdir == NULL) {
591                 TALLOC_FREE(dir_smb_fname);
592                 TALLOC_FREE(dirname);
593                 return NT_STATUS_NO_MEMORY;
594         }
595
596         orig_connectpath = handle->conn->connectpath;
597         handle->conn->connectpath = rootdir;
598
599         status = OpenDir(
600                 talloc_tos(), handle->conn, dir_smb_fname, NULL, 0, &dir_hnd);
601         if (!NT_STATUS_IS_OK(status)) {
602                 handle->conn->connectpath = orig_connectpath;
603                 TALLOC_FREE(rootdir);
604                 TALLOC_FREE(dir_smb_fname);
605                 TALLOC_FREE(dirname);
606                 return status;
607         }
608
609         while ((dname = ReadDirName(dir_hnd, &offset, NULL, &talloced))
610                != NULL)
611         {
612                 if (ISDOT(dname) || ISDOTDOT(dname)) {
613                         TALLOC_FREE(talloced);
614                         continue;
615                 }
616
617                 DBG_DEBUG("dirent=%s\n", dname);
618
619                 if (!fn(dir_smb_fname, dname, private_data)) {
620                         TALLOC_FREE(talloced);
621                         break;
622                 }
623                 TALLOC_FREE(talloced);
624         }
625
626         /* Restore the original connectpath. */
627         handle->conn->connectpath = orig_connectpath;
628         TALLOC_FREE(rootdir);
629         TALLOC_FREE(dir_smb_fname);
630         TALLOC_FREE(dir_hnd);
631
632         if (pdirname != NULL) {
633                 *pdirname = dirname;
634         }
635         else {
636                 TALLOC_FREE(dirname);
637         }
638
639         return NT_STATUS_OK;
640 }
641
642 static int streams_depot_stat(vfs_handle_struct *handle,
643                               struct smb_filename *smb_fname)
644 {
645         struct smb_filename *smb_fname_stream = NULL;
646         NTSTATUS status;
647         int ret = -1;
648
649         DEBUG(10, ("streams_depot_stat called for [%s]\n",
650                    smb_fname_str_dbg(smb_fname)));
651
652         if (!is_named_stream(smb_fname)) {
653                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
654         }
655
656         /* Stat the actual stream now. */
657         status = stream_smb_fname(
658                 handle, NULL, smb_fname, &smb_fname_stream, false);
659         if (!NT_STATUS_IS_OK(status)) {
660                 ret = -1;
661                 errno = map_errno_from_nt_status(status);
662                 goto done;
663         }
664
665         ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
666
667         /* Update the original smb_fname with the stat info. */
668         smb_fname->st = smb_fname_stream->st;
669  done:
670         TALLOC_FREE(smb_fname_stream);
671         return ret;
672 }
673
674
675
676 static int streams_depot_lstat(vfs_handle_struct *handle,
677                                struct smb_filename *smb_fname)
678 {
679         struct smb_filename *smb_fname_stream = NULL;
680         NTSTATUS status;
681         int ret = -1;
682
683         DEBUG(10, ("streams_depot_lstat called for [%s]\n",
684                    smb_fname_str_dbg(smb_fname)));
685
686         if (!is_named_stream(smb_fname)) {
687                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
688         }
689
690         /* Stat the actual stream now. */
691         status = stream_smb_fname(
692                 handle, NULL, smb_fname, &smb_fname_stream, false);
693         if (!NT_STATUS_IS_OK(status)) {
694                 ret = -1;
695                 errno = map_errno_from_nt_status(status);
696                 goto done;
697         }
698
699         ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
700
701  done:
702         TALLOC_FREE(smb_fname_stream);
703         return ret;
704 }
705
706 static int streams_depot_openat(struct vfs_handle_struct *handle,
707                                 const struct files_struct *dirfsp,
708                                 const struct smb_filename *smb_fname,
709                                 struct files_struct *fsp,
710                                 int flags,
711                                 mode_t mode)
712 {
713         struct smb_filename *smb_fname_stream = NULL;
714         struct files_struct *fspcwd = NULL;
715         NTSTATUS status;
716         bool create_it;
717         int ret = -1;
718
719         if (!is_named_stream(smb_fname)) {
720                 return SMB_VFS_NEXT_OPENAT(handle,
721                                            dirfsp,
722                                            smb_fname,
723                                            fsp,
724                                            flags,
725                                            mode);
726         }
727
728         SMB_ASSERT(fsp_is_alternate_stream(fsp));
729         SMB_ASSERT(VALID_STAT(fsp->base_fsp->fsp_name->st));
730
731         create_it = (mode & O_CREAT);
732
733         /* Determine the stream name, and then open it. */
734         status = stream_smb_fname(
735                 handle,
736                 &fsp->base_fsp->fsp_name->st,
737                 fsp->fsp_name,
738                 &smb_fname_stream,
739                 create_it);
740         if (!NT_STATUS_IS_OK(status)) {
741                 ret = -1;
742                 errno = map_errno_from_nt_status(status);
743                 goto done;
744         }
745
746         status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
747         if (!NT_STATUS_IS_OK(status)) {
748                 ret = -1;
749                 errno = map_errno_from_nt_status(status);
750                 goto done;
751         }
752
753         ret = SMB_VFS_NEXT_OPENAT(handle,
754                                   fspcwd,
755                                   smb_fname_stream,
756                                   fsp,
757                                   flags,
758                                   mode);
759
760  done:
761         TALLOC_FREE(smb_fname_stream);
762         TALLOC_FREE(fspcwd);
763         return ret;
764 }
765
766 static int streams_depot_unlink_internal(vfs_handle_struct *handle,
767                                 struct files_struct *dirfsp,
768                                 const struct smb_filename *smb_fname,
769                                 int flags)
770 {
771         struct smb_filename *full_fname = NULL;
772         char *dirname = NULL;
773         int ret = -1;
774
775         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
776                                                   dirfsp,
777                                                   smb_fname);
778         if (full_fname == NULL) {
779                 return -1;
780         }
781
782         DEBUG(10, ("streams_depot_unlink called for %s\n",
783                    smb_fname_str_dbg(full_fname)));
784
785         /* If there is a valid stream, just unlink the stream and return. */
786         if (is_named_stream(full_fname)) {
787                 struct smb_filename *smb_fname_stream = NULL;
788                 NTSTATUS status;
789
790                 status = stream_smb_fname(
791                         handle, NULL, full_fname, &smb_fname_stream, false);
792                 TALLOC_FREE(full_fname);
793                 if (!NT_STATUS_IS_OK(status)) {
794                         errno = map_errno_from_nt_status(status);
795                         return -1;
796                 }
797
798                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
799                                 dirfsp->conn->cwd_fsp,
800                                 smb_fname_stream,
801                                 0);
802
803                 TALLOC_FREE(smb_fname_stream);
804                 return ret;
805         }
806
807         /*
808          * We potentially need to delete the per-inode streams directory
809          */
810
811         if (full_fname->flags & SMB_FILENAME_POSIX_PATH) {
812                 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
813         } else {
814                 ret = SMB_VFS_NEXT_STAT(handle, full_fname);
815                 if (ret == -1 && (errno == ENOENT || errno == ELOOP)) {
816                         if (VALID_STAT(smb_fname->st) &&
817                                         S_ISLNK(smb_fname->st.st_ex_mode)) {
818                                 /*
819                                  * Original name was a link - Could be
820                                  * trying to remove a dangling symlink.
821                                  */
822                                 ret = SMB_VFS_NEXT_LSTAT(handle, full_fname);
823                         }
824                 }
825         }
826         if (ret == -1) {
827                 TALLOC_FREE(full_fname);
828                 return -1;
829         }
830
831         /*
832          * We know the unlink should succeed as the ACL
833          * check is already done in the caller. Remove the
834          * file *after* the streams.
835          */
836         dirname = stream_dir(handle,
837                              full_fname,
838                              &full_fname->st,
839                              false);
840         TALLOC_FREE(full_fname);
841         if (dirname != NULL) {
842                 struct smb_filename *smb_fname_dir = NULL;
843
844                 smb_fname_dir = synthetic_smb_fname(talloc_tos(),
845                                                     dirname,
846                                                     NULL,
847                                                     NULL,
848                                                     smb_fname->twrp,
849                                                     smb_fname->flags);
850                 if (smb_fname_dir == NULL) {
851                         TALLOC_FREE(dirname);
852                         errno = ENOMEM;
853                         return -1;
854                 }
855
856                 SMB_VFS_NEXT_UNLINKAT(handle,
857                                       dirfsp->conn->cwd_fsp,
858                                       smb_fname_dir,
859                                       AT_REMOVEDIR);
860                 TALLOC_FREE(smb_fname_dir);
861                 TALLOC_FREE(dirname);
862         }
863
864         ret = SMB_VFS_NEXT_UNLINKAT(handle,
865                                 dirfsp,
866                                 smb_fname,
867                                 flags);
868         return ret;
869 }
870
871 static int streams_depot_rmdir_internal(vfs_handle_struct *handle,
872                         struct files_struct *dirfsp,
873                         const struct smb_filename *smb_fname)
874 {
875         struct smb_filename *full_fname = NULL;
876         struct smb_filename *smb_fname_base = NULL;
877         int ret = -1;
878
879         full_fname = full_path_from_dirfsp_atname(talloc_tos(),
880                                                   dirfsp,
881                                                   smb_fname);
882         if (full_fname == NULL) {
883                 return -1;
884         }
885
886         DBG_DEBUG("called for %s\n", full_fname->base_name);
887
888         /*
889          * We potentially need to delete the per-inode streams directory
890          */
891
892         smb_fname_base = synthetic_smb_fname(talloc_tos(),
893                                 full_fname->base_name,
894                                 NULL,
895                                 NULL,
896                                 full_fname->twrp,
897                                 full_fname->flags);
898         TALLOC_FREE(full_fname);
899         if (smb_fname_base == NULL) {
900                 errno = ENOMEM;
901                 return -1;
902         }
903
904         if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
905                 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
906         } else {
907                 ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
908         }
909
910         if (ret == -1) {
911                 TALLOC_FREE(smb_fname_base);
912                 return -1;
913         }
914
915         /*
916          * We know the rmdir should succeed as the ACL
917          * check is already done in the caller. Remove the
918          * directory *after* the streams.
919          */
920         {
921                 char *dirname = stream_dir(handle, smb_fname_base,
922                                            &smb_fname_base->st, false);
923
924                 if (dirname != NULL) {
925                         struct smb_filename *smb_fname_dir =
926                                 synthetic_smb_fname(talloc_tos(),
927                                                 dirname,
928                                                 NULL,
929                                                 NULL,
930                                                 smb_fname->twrp,
931                                                 smb_fname->flags);
932                         if (smb_fname_dir == NULL) {
933                                 TALLOC_FREE(smb_fname_base);
934                                 TALLOC_FREE(dirname);
935                                 errno = ENOMEM;
936                                 return -1;
937                         }
938                         SMB_VFS_NEXT_UNLINKAT(handle,
939                                         dirfsp->conn->cwd_fsp,
940                                         smb_fname_dir,
941                                         AT_REMOVEDIR);
942                         TALLOC_FREE(smb_fname_dir);
943                 }
944                 TALLOC_FREE(dirname);
945         }
946
947         ret = SMB_VFS_NEXT_UNLINKAT(handle,
948                                 dirfsp,
949                                 smb_fname,
950                                 AT_REMOVEDIR);
951         TALLOC_FREE(smb_fname_base);
952         return ret;
953 }
954
955 static int streams_depot_unlinkat(vfs_handle_struct *handle,
956                         struct files_struct *dirfsp,
957                         const struct smb_filename *smb_fname,
958                         int flags)
959 {
960         int ret;
961         if (flags & AT_REMOVEDIR) {
962                 ret = streams_depot_rmdir_internal(handle,
963                                 dirfsp,
964                                 smb_fname);
965         } else {
966                 ret = streams_depot_unlink_internal(handle,
967                                 dirfsp,
968                                 smb_fname,
969                                 flags);
970         }
971         return ret;
972 }
973
974 static int streams_depot_renameat(vfs_handle_struct *handle,
975                                 files_struct *srcfsp,
976                                 const struct smb_filename *smb_fname_src,
977                                 files_struct *dstfsp,
978                                 const struct smb_filename *smb_fname_dst)
979 {
980         struct smb_filename *smb_fname_src_stream = NULL;
981         struct smb_filename *smb_fname_dst_stream = NULL;
982         struct smb_filename *full_src = NULL;
983         struct smb_filename *full_dst = NULL;
984         bool src_is_stream, dst_is_stream;
985         NTSTATUS status;
986         int ret = -1;
987
988         DEBUG(10, ("streams_depot_renameat called for %s => %s\n",
989                    smb_fname_str_dbg(smb_fname_src),
990                    smb_fname_str_dbg(smb_fname_dst)));
991
992         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
993         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
994
995         if (!src_is_stream && !dst_is_stream) {
996                 return SMB_VFS_NEXT_RENAMEAT(handle,
997                                         srcfsp,
998                                         smb_fname_src,
999                                         dstfsp,
1000                                         smb_fname_dst);
1001         }
1002
1003         /* for now don't allow renames from or to the default stream */
1004         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
1005             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
1006                 errno = ENOSYS;
1007                 goto done;
1008         }
1009
1010         full_src = full_path_from_dirfsp_atname(talloc_tos(),
1011                                                 srcfsp,
1012                                                 smb_fname_src);
1013         if (full_src == NULL) {
1014                 errno = ENOMEM;
1015                 goto done;
1016         }
1017
1018         full_dst = full_path_from_dirfsp_atname(talloc_tos(),
1019                                                 dstfsp,
1020                                                 smb_fname_dst);
1021         if (full_dst == NULL) {
1022                 errno = ENOMEM;
1023                 goto done;
1024         }
1025
1026         status = stream_smb_fname(
1027                 handle, NULL, full_src, &smb_fname_src_stream, false);
1028         if (!NT_STATUS_IS_OK(status)) {
1029                 errno = map_errno_from_nt_status(status);
1030                 goto done;
1031         }
1032
1033         status = stream_smb_fname(
1034                 handle, NULL, full_dst, &smb_fname_dst_stream, false);
1035         if (!NT_STATUS_IS_OK(status)) {
1036                 errno = map_errno_from_nt_status(status);
1037                 goto done;
1038         }
1039
1040         /*
1041          * We must use handle->conn->cwd_fsp as
1042          * srcfsp and dstfsp directory handles here
1043          * as we used the full pathname from the cwd dir
1044          * to calculate the streams directory and filename
1045          * within.
1046          */
1047         ret = SMB_VFS_NEXT_RENAMEAT(handle,
1048                                 handle->conn->cwd_fsp,
1049                                 smb_fname_src_stream,
1050                                 handle->conn->cwd_fsp,
1051                                 smb_fname_dst_stream);
1052
1053 done:
1054         TALLOC_FREE(smb_fname_src_stream);
1055         TALLOC_FREE(smb_fname_dst_stream);
1056         return ret;
1057 }
1058
1059 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
1060                            struct stream_struct **streams,
1061                            const char *name, off_t size,
1062                            off_t alloc_size)
1063 {
1064         struct stream_struct *tmp;
1065
1066         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
1067                                    (*num_streams)+1);
1068         if (tmp == NULL) {
1069                 return false;
1070         }
1071
1072         tmp[*num_streams].name = talloc_strdup(tmp, name);
1073         if (tmp[*num_streams].name == NULL) {
1074                 return false;
1075         }
1076
1077         tmp[*num_streams].size = size;
1078         tmp[*num_streams].alloc_size = alloc_size;
1079
1080         *streams = tmp;
1081         *num_streams += 1;
1082         return true;
1083 }
1084
1085 struct streaminfo_state {
1086         TALLOC_CTX *mem_ctx;
1087         vfs_handle_struct *handle;
1088         unsigned int num_streams;
1089         struct stream_struct *streams;
1090         NTSTATUS status;
1091 };
1092
1093 static bool collect_one_stream(const struct smb_filename *dirfname,
1094                                const char *dirent,
1095                                void *private_data)
1096 {
1097         const char *dirname = dirfname->base_name;
1098         struct streaminfo_state *state =
1099                 (struct streaminfo_state *)private_data;
1100         struct smb_filename *smb_fname = NULL;
1101         char *sname = NULL;
1102         bool ret;
1103
1104         sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
1105         if (sname == NULL) {
1106                 state->status = NT_STATUS_NO_MEMORY;
1107                 ret = false;
1108                 goto out;
1109         }
1110
1111         smb_fname = synthetic_smb_fname(talloc_tos(),
1112                                         sname,
1113                                         NULL,
1114                                         NULL,
1115                                         dirfname->twrp,
1116                                         0);
1117         if (smb_fname == NULL) {
1118                 state->status = NT_STATUS_NO_MEMORY;
1119                 ret = false;
1120                 goto out;
1121         }
1122
1123         if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
1124                 DEBUG(10, ("Could not stat %s: %s\n", sname,
1125                            strerror(errno)));
1126                 ret = true;
1127                 goto out;
1128         }
1129
1130         if (!add_one_stream(state->mem_ctx,
1131                             &state->num_streams, &state->streams,
1132                             dirent, smb_fname->st.st_ex_size,
1133                             SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
1134                                                    &smb_fname->st))) {
1135                 state->status = NT_STATUS_NO_MEMORY;
1136                 ret = false;
1137                 goto out;
1138         }
1139
1140         ret = true;
1141  out:
1142         TALLOC_FREE(sname);
1143         TALLOC_FREE(smb_fname);
1144         return ret;
1145 }
1146
1147 static NTSTATUS streams_depot_fstreaminfo(vfs_handle_struct *handle,
1148                                          struct files_struct *fsp,
1149                                          TALLOC_CTX *mem_ctx,
1150                                          unsigned int *pnum_streams,
1151                                          struct stream_struct **pstreams)
1152 {
1153         struct smb_filename *smb_fname_base = NULL;
1154         int ret;
1155         NTSTATUS status;
1156         struct streaminfo_state state;
1157
1158         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1159                                         fsp->fsp_name->base_name,
1160                                         NULL,
1161                                         NULL,
1162                                         fsp->fsp_name->twrp,
1163                                         fsp->fsp_name->flags);
1164         if (smb_fname_base == NULL) {
1165                 return NT_STATUS_NO_MEMORY;
1166         }
1167
1168         ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
1169         if (ret == -1) {
1170                 status = map_nt_error_from_unix(errno);
1171                 goto out;
1172         }
1173
1174         state.streams = *pstreams;
1175         state.num_streams = *pnum_streams;
1176         state.mem_ctx = mem_ctx;
1177         state.handle = handle;
1178         state.status = NT_STATUS_OK;
1179
1180         status = walk_streams(handle,
1181                                 smb_fname_base,
1182                                 NULL,
1183                                 collect_one_stream,
1184                                 &state);
1185
1186         if (!NT_STATUS_IS_OK(status)) {
1187                 TALLOC_FREE(state.streams);
1188                 goto out;
1189         }
1190
1191         if (!NT_STATUS_IS_OK(state.status)) {
1192                 TALLOC_FREE(state.streams);
1193                 status = state.status;
1194                 goto out;
1195         }
1196
1197         *pnum_streams = state.num_streams;
1198         *pstreams = state.streams;
1199         status = SMB_VFS_NEXT_FSTREAMINFO(handle,
1200                                 fsp->base_fsp ? fsp->base_fsp : fsp,
1201                                 mem_ctx,
1202                                 pnum_streams,
1203                                 pstreams);
1204
1205  out:
1206         TALLOC_FREE(smb_fname_base);
1207         return status;
1208 }
1209
1210 static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
1211                         enum timestamp_set_resolution *p_ts_res)
1212 {
1213         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
1214 }
1215
1216 static struct vfs_fn_pointers vfs_streams_depot_fns = {
1217         .fs_capabilities_fn = streams_depot_fs_capabilities,
1218         .openat_fn = streams_depot_openat,
1219         .stat_fn = streams_depot_stat,
1220         .lstat_fn = streams_depot_lstat,
1221         .unlinkat_fn = streams_depot_unlinkat,
1222         .renameat_fn = streams_depot_renameat,
1223         .fstreaminfo_fn = streams_depot_fstreaminfo,
1224 };
1225
1226 static_decl_vfs;
1227 NTSTATUS vfs_streams_depot_init(TALLOC_CTX *ctx)
1228 {
1229         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
1230                                 &vfs_streams_depot_fns);
1231 }