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