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