smbd: use vfs_stat() in more places
[samba.git] / source3 / modules / vfs_streams_xattr.c
1 /*
2  * Store streams in xattrs
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  *
6  * Partly based on James Peach's Darwin module, which is
7  *
8  * Copyright (C) James Peach 2006-2007
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "lib/util/tevent_unix.h"
28 #include "librpc/gen_ndr/ioctl.h"
29 #include "hash_inode.h"
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS DBGC_VFS
33
34 struct streams_xattr_config {
35         const char *prefix;
36         size_t prefix_len;
37         bool store_stream_type;
38 };
39
40 struct stream_io {
41         char *base;
42         char *xattr_name;
43         void *fsp_name_ptr;
44         files_struct *fsp;
45         vfs_handle_struct *handle;
46 };
47
48 static ssize_t get_xattr_size(connection_struct *conn,
49                                 const struct smb_filename *smb_fname,
50                                 const char *xattr_name)
51 {
52         NTSTATUS status;
53         struct ea_struct ea;
54         ssize_t result;
55
56         status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
57                               xattr_name, &ea);
58
59         if (!NT_STATUS_IS_OK(status)) {
60                 return -1;
61         }
62
63         result = ea.value.length-1;
64         TALLOC_FREE(ea.value.data);
65         return result;
66 }
67
68 /**
69  * Given a stream name, populate xattr_name with the xattr name to use for
70  * accessing the stream.
71  */
72 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
73                                        TALLOC_CTX *ctx,
74                                        const char *stream_name,
75                                        char **xattr_name)
76 {
77         char *sname;
78         char *stype;
79         struct streams_xattr_config *config;
80
81         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
82                                 return NT_STATUS_UNSUCCESSFUL);
83
84         sname = talloc_strdup(ctx, stream_name + 1);
85         if (sname == NULL) {
86                 return NT_STATUS_NO_MEMORY;
87         }
88
89         /*
90          * With vfs_fruit option "fruit:encoding = native" we're
91          * already converting stream names that contain illegal NTFS
92          * characters from their on-the-wire Unicode Private Range
93          * encoding to their native ASCII representation.
94          *
95          * As as result the name of xattrs storing the streams (via
96          * vfs_streams_xattr) may contain a colon, so we have to use
97          * strrchr_m() instead of strchr_m() for matching the stream
98          * type suffix.
99          *
100          * In check_path_syntax() we've already ensured the streamname
101          * we got from the client is valid.
102          */
103         stype = strrchr_m(sname, ':');
104
105         if (stype) {
106                 /*
107                  * We only support one stream type: "$DATA"
108                  */
109                 if (strcasecmp_m(stype, ":$DATA") != 0) {
110                         talloc_free(sname);
111                         return NT_STATUS_INVALID_PARAMETER;
112                 }
113
114                 /* Split name and type */
115                 stype[0] = '\0';
116         }
117
118         *xattr_name = talloc_asprintf(ctx, "%s%s%s",
119                                       config->prefix,
120                                       sname,
121                                       config->store_stream_type ? ":$DATA" : "");
122         if (*xattr_name == NULL) {
123                 talloc_free(sname);
124                 return NT_STATUS_NO_MEMORY;
125         }
126
127         DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
128                    stream_name));
129
130         talloc_free(sname);
131         return NT_STATUS_OK;
132 }
133
134 static bool streams_xattr_recheck(struct stream_io *sio)
135 {
136         NTSTATUS status;
137         char *xattr_name = NULL;
138
139         if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
140                 return true;
141         }
142
143         if (sio->fsp->fsp_name->stream_name == NULL) {
144                 /* how can this happen */
145                 errno = EINVAL;
146                 return false;
147         }
148
149         status = streams_xattr_get_name(sio->handle, talloc_tos(),
150                                         sio->fsp->fsp_name->stream_name,
151                                         &xattr_name);
152         if (!NT_STATUS_IS_OK(status)) {
153                 return false;
154         }
155
156         TALLOC_FREE(sio->xattr_name);
157         TALLOC_FREE(sio->base);
158         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
159                                         xattr_name);
160         if (sio->xattr_name == NULL) {
161                 DBG_DEBUG("sio->xattr_name==NULL\n");
162                 return false;
163         }
164         TALLOC_FREE(xattr_name);
165
166         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
167                                   sio->fsp->fsp_name->base_name);
168         if (sio->base == NULL) {
169                 DBG_DEBUG("sio->base==NULL\n");
170                 return false;
171         }
172
173         sio->fsp_name_ptr = sio->fsp->fsp_name;
174
175         return true;
176 }
177
178 /**
179  * Helper to stat/lstat the base file of an smb_fname.
180  */
181 static int streams_xattr_stat_base(vfs_handle_struct *handle,
182                                    struct smb_filename *smb_fname,
183                                    bool follow_links)
184 {
185         char *tmp_stream_name;
186         int result;
187
188         tmp_stream_name = smb_fname->stream_name;
189         smb_fname->stream_name = NULL;
190         if (follow_links) {
191                 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
192         } else {
193                 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
194         }
195         smb_fname->stream_name = tmp_stream_name;
196         return result;
197 }
198
199 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
200                                SMB_STRUCT_STAT *sbuf)
201 {
202         struct smb_filename *smb_fname_base = NULL;
203         int ret = -1;
204         struct stream_io *io = (struct stream_io *)
205                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
206
207         if (io == NULL || fsp->base_fsp == NULL) {
208                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
209         }
210
211         DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
212
213         if (!streams_xattr_recheck(io)) {
214                 return -1;
215         }
216
217         /* Create an smb_filename with stream_name == NULL. */
218         smb_fname_base = synthetic_smb_fname(talloc_tos(),
219                                         io->base,
220                                         NULL,
221                                         NULL,
222                                         fsp->fsp_name->twrp,
223                                         fsp->fsp_name->flags);
224         if (smb_fname_base == NULL) {
225                 errno = ENOMEM;
226                 return -1;
227         }
228
229         ret = vfs_stat(handle->conn, smb_fname_base);
230         *sbuf = smb_fname_base->st;
231
232         if (ret == -1) {
233                 TALLOC_FREE(smb_fname_base);
234                 return -1;
235         }
236
237         sbuf->st_ex_size = get_xattr_size(handle->conn,
238                                         smb_fname_base, io->xattr_name);
239         if (sbuf->st_ex_size == -1) {
240                 TALLOC_FREE(smb_fname_base);
241                 SET_STAT_INVALID(*sbuf);
242                 return -1;
243         }
244
245         DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
246
247         sbuf->st_ex_ino = hash_inode(sbuf, io->xattr_name);
248         sbuf->st_ex_mode &= ~S_IFMT;
249         sbuf->st_ex_mode &= ~S_IFDIR;
250         sbuf->st_ex_mode |= S_IFREG;
251         sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
252
253         TALLOC_FREE(smb_fname_base);
254         return 0;
255 }
256
257 static int streams_xattr_stat(vfs_handle_struct *handle,
258                               struct smb_filename *smb_fname)
259 {
260         NTSTATUS status;
261         int result = -1;
262         char *xattr_name = NULL;
263
264         if (!is_named_stream(smb_fname)) {
265                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
266         }
267
268         /* Note if lp_posix_paths() is true, we can never
269          * get here as is_named_stream() is
270          * always false. So we never need worry about
271          * not following links here. */
272
273         /* Populate the stat struct with info from the base file. */
274         if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
275                 return -1;
276         }
277
278         /* Derive the xattr name to lookup. */
279         status = streams_xattr_get_name(handle, talloc_tos(),
280                                         smb_fname->stream_name, &xattr_name);
281         if (!NT_STATUS_IS_OK(status)) {
282                 errno = map_errno_from_nt_status(status);
283                 return -1;
284         }
285
286         /* Augment the base file's stat information before returning. */
287         smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
288                                                   smb_fname,
289                                                   xattr_name);
290         if (smb_fname->st.st_ex_size == -1) {
291                 SET_STAT_INVALID(smb_fname->st);
292                 errno = ENOENT;
293                 result = -1;
294                 goto fail;
295         }
296
297         smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
298         smb_fname->st.st_ex_mode &= ~S_IFMT;
299         smb_fname->st.st_ex_mode &= ~S_IFDIR;
300         smb_fname->st.st_ex_mode |= S_IFREG;
301         smb_fname->st.st_ex_blocks =
302             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
303
304         result = 0;
305  fail:
306         TALLOC_FREE(xattr_name);
307         return result;
308 }
309
310 static int streams_xattr_lstat(vfs_handle_struct *handle,
311                                struct smb_filename *smb_fname)
312 {
313         NTSTATUS status;
314         int result = -1;
315         char *xattr_name = NULL;
316
317         if (!is_named_stream(smb_fname)) {
318                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
319         }
320
321         /* Populate the stat struct with info from the base file. */
322         if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
323                 return -1;
324         }
325
326         /* Derive the xattr name to lookup. */
327         status = streams_xattr_get_name(handle, talloc_tos(),
328                                         smb_fname->stream_name, &xattr_name);
329         if (!NT_STATUS_IS_OK(status)) {
330                 errno = map_errno_from_nt_status(status);
331                 return -1;
332         }
333
334         /* Augment the base file's stat information before returning. */
335         smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
336                                                   smb_fname,
337                                                   xattr_name);
338         if (smb_fname->st.st_ex_size == -1) {
339                 SET_STAT_INVALID(smb_fname->st);
340                 errno = ENOENT;
341                 result = -1;
342                 goto fail;
343         }
344
345         smb_fname->st.st_ex_ino = hash_inode(&smb_fname->st, xattr_name);
346         smb_fname->st.st_ex_mode &= ~S_IFMT;
347         smb_fname->st.st_ex_mode |= S_IFREG;
348         smb_fname->st.st_ex_blocks =
349             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
350
351         result = 0;
352
353  fail:
354         TALLOC_FREE(xattr_name);
355         return result;
356 }
357
358 static int streams_xattr_openat(struct vfs_handle_struct *handle,
359                                 const struct files_struct *dirfsp,
360                                 const struct smb_filename *smb_fname,
361                                 files_struct *fsp,
362                                 int flags,
363                                 mode_t mode)
364 {
365         NTSTATUS status;
366         struct streams_xattr_config *config = NULL;
367         struct stream_io *sio = NULL;
368         struct ea_struct ea;
369         char *xattr_name = NULL;
370         int fakefd = -1;
371         bool set_empty_xattr = false;
372         int ret;
373
374         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
375                                 return -1);
376
377         DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
378                    smb_fname_str_dbg(smb_fname), flags));
379
380         if (!is_named_stream(smb_fname)) {
381                 return SMB_VFS_NEXT_OPENAT(handle,
382                                            dirfsp,
383                                            smb_fname,
384                                            fsp,
385                                            flags,
386                                            mode);
387         }
388
389         /*
390          * For now assert this, so the below SMB_VFS_SETXATTR() works.
391          */
392         SMB_ASSERT(fsp_get_pathref_fd(dirfsp) == AT_FDCWD);
393
394         status = streams_xattr_get_name(handle, talloc_tos(),
395                                         smb_fname->stream_name, &xattr_name);
396         if (!NT_STATUS_IS_OK(status)) {
397                 errno = map_errno_from_nt_status(status);
398                 goto fail;
399         }
400
401         status = get_ea_value(talloc_tos(), handle->conn, NULL,
402                               smb_fname, xattr_name, &ea);
403
404         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
405
406         if (!NT_STATUS_IS_OK(status)) {
407                 if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
408                         /*
409                          * The base file is not there. This is an error even if
410                          * we got O_CREAT, the higher levels should have created
411                          * the base file for us.
412                          */
413                         DBG_DEBUG("streams_xattr_open: base file %s not around, "
414                                   "returning ENOENT\n", smb_fname->base_name);
415                         errno = ENOENT;
416                         goto fail;
417                 }
418
419                 if (!(flags & O_CREAT)) {
420                         errno = ENOATTR;
421                         goto fail;
422                 }
423
424                 set_empty_xattr = true;
425         }
426
427         if (flags & O_TRUNC) {
428                 set_empty_xattr = true;
429         }
430
431         if (set_empty_xattr) {
432                 /*
433                  * The attribute does not exist or needs to be truncated
434                  */
435
436                 /*
437                  * Darn, xattrs need at least 1 byte
438                  */
439                 char null = '\0';
440
441                 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
442                            xattr_name, smb_fname->base_name));
443
444                 ret = SMB_VFS_SETXATTR(fsp->conn,
445                                        smb_fname,
446                                        xattr_name,
447                                        &null, sizeof(null),
448                                        flags & O_EXCL ? XATTR_CREATE : 0);
449                 if (ret != 0) {
450                         goto fail;
451                 }
452         }
453
454         fakefd = vfs_fake_fd();
455
456         sio = VFS_ADD_FSP_EXTENSION(handle, fsp, struct stream_io, NULL);
457         if (sio == NULL) {
458                 errno = ENOMEM;
459                 goto fail;
460         }
461
462         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
463                                         xattr_name);
464         if (sio->xattr_name == NULL) {
465                 errno = ENOMEM;
466                 goto fail;
467         }
468
469         /*
470          * so->base needs to be a copy of fsp->fsp_name->base_name,
471          * making it identical to streams_xattr_recheck(). If the
472          * open is changing directories, fsp->fsp_name->base_name
473          * will be the full path from the share root, whilst
474          * smb_fname will be relative to the $cwd.
475          */
476         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
477                                   fsp->fsp_name->base_name);
478         if (sio->base == NULL) {
479                 errno = ENOMEM;
480                 goto fail;
481         }
482
483         sio->fsp_name_ptr = fsp->fsp_name;
484         sio->handle = handle;
485         sio->fsp = fsp;
486
487         return fakefd;
488
489  fail:
490         if (fakefd >= 0) {
491                 close(fakefd);
492                 fakefd = -1;
493         }
494
495         return -1;
496 }
497
498 static int streams_xattr_close(vfs_handle_struct *handle,
499                                files_struct *fsp)
500 {
501         int ret;
502         int fd;
503
504         fd = fsp_get_pathref_fd(fsp);
505
506         DBG_DEBUG("streams_xattr_close called [%s] fd [%d]\n",
507                         smb_fname_str_dbg(fsp->fsp_name), fd);
508
509         if (!is_named_stream(fsp->fsp_name)) {
510                 return SMB_VFS_NEXT_CLOSE(handle, fsp);
511         }
512
513         ret = close(fd);
514         fsp_set_fd(fsp, -1);
515
516         return ret;
517 }
518
519 static int streams_xattr_unlink_internal(vfs_handle_struct *handle,
520                         struct files_struct *dirfsp,
521                         const struct smb_filename *smb_fname,
522                         int flags)
523 {
524         NTSTATUS status;
525         int ret = -1;
526         char *xattr_name = NULL;
527
528         if (!is_named_stream(smb_fname)) {
529                 return SMB_VFS_NEXT_UNLINKAT(handle,
530                                         dirfsp,
531                                         smb_fname,
532                                         flags);
533         }
534
535         status = streams_xattr_get_name(handle, talloc_tos(),
536                                         smb_fname->stream_name, &xattr_name);
537         if (!NT_STATUS_IS_OK(status)) {
538                 errno = map_errno_from_nt_status(status);
539                 goto fail;
540         }
541
542         ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
543
544         if ((ret == -1) && (errno == ENOATTR)) {
545                 errno = ENOENT;
546                 goto fail;
547         }
548
549         ret = 0;
550
551  fail:
552         TALLOC_FREE(xattr_name);
553         return ret;
554 }
555
556 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
557                         struct files_struct *dirfsp,
558                         const struct smb_filename *smb_fname,
559                         int flags)
560 {
561         int ret;
562         if (flags & AT_REMOVEDIR) {
563                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
564                                 dirfsp,
565                                 smb_fname,
566                                 flags);
567         } else {
568                 ret = streams_xattr_unlink_internal(handle,
569                                 dirfsp,
570                                 smb_fname,
571                                 flags);
572         }
573         return ret;
574 }
575
576 static int streams_xattr_renameat(vfs_handle_struct *handle,
577                                 files_struct *srcfsp,
578                                 const struct smb_filename *smb_fname_src,
579                                 files_struct *dstfsp,
580                                 const struct smb_filename *smb_fname_dst)
581 {
582         NTSTATUS status;
583         int ret = -1;
584         char *src_xattr_name = NULL;
585         char *dst_xattr_name = NULL;
586         bool src_is_stream, dst_is_stream;
587         ssize_t oret;
588         ssize_t nret;
589         struct ea_struct ea;
590
591         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
592         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
593
594         if (!src_is_stream && !dst_is_stream) {
595                 return SMB_VFS_NEXT_RENAMEAT(handle,
596                                         srcfsp,
597                                         smb_fname_src,
598                                         dstfsp,
599                                         smb_fname_dst);
600         }
601
602         /* For now don't allow renames from or to the default stream. */
603         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
604             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
605                 errno = ENOSYS;
606                 goto done;
607         }
608
609         /* Don't rename if the streams are identical. */
610         if (strcasecmp_m(smb_fname_src->stream_name,
611                        smb_fname_dst->stream_name) == 0) {
612                 goto done;
613         }
614
615         /* Get the xattr names. */
616         status = streams_xattr_get_name(handle, talloc_tos(),
617                                         smb_fname_src->stream_name,
618                                         &src_xattr_name);
619         if (!NT_STATUS_IS_OK(status)) {
620                 errno = map_errno_from_nt_status(status);
621                 goto fail;
622         }
623         status = streams_xattr_get_name(handle, talloc_tos(),
624                                         smb_fname_dst->stream_name,
625                                         &dst_xattr_name);
626         if (!NT_STATUS_IS_OK(status)) {
627                 errno = map_errno_from_nt_status(status);
628                 goto fail;
629         }
630
631         /* read the old stream */
632         status = get_ea_value(talloc_tos(), handle->conn, NULL,
633                               smb_fname_src, src_xattr_name, &ea);
634         if (!NT_STATUS_IS_OK(status)) {
635                 errno = ENOENT;
636                 goto fail;
637         }
638
639         /* (over)write the new stream */
640         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
641                                 dst_xattr_name, ea.value.data, ea.value.length,
642                                 0);
643         if (nret < 0) {
644                 if (errno == ENOATTR) {
645                         errno = ENOENT;
646                 }
647                 goto fail;
648         }
649
650         /* remove the old stream */
651         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
652                                    src_xattr_name);
653         if (oret < 0) {
654                 if (errno == ENOATTR) {
655                         errno = ENOENT;
656                 }
657                 goto fail;
658         }
659
660  done:
661         errno = 0;
662         ret = 0;
663  fail:
664         TALLOC_FREE(src_xattr_name);
665         TALLOC_FREE(dst_xattr_name);
666         return ret;
667 }
668
669 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
670                                 files_struct *fsp,
671                                 const struct smb_filename *smb_fname,
672                                 bool (*fn)(struct ea_struct *ea,
673                                         void *private_data),
674                                 void *private_data)
675 {
676         NTSTATUS status;
677         char **names;
678         size_t i, num_names;
679         struct streams_xattr_config *config;
680
681         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
682                                 return NT_STATUS_UNSUCCESSFUL);
683
684         status = get_ea_names_from_file(talloc_tos(),
685                                 handle->conn,
686                                 fsp,
687                                 smb_fname,
688                                 &names,
689                                 &num_names);
690         if (!NT_STATUS_IS_OK(status)) {
691                 return status;
692         }
693
694         for (i=0; i<num_names; i++) {
695                 struct ea_struct ea;
696
697                 /*
698                  * We want to check with samba_private_attr_name()
699                  * whether the xattr name is a private one,
700                  * unfortunately it flags xattrs that begin with the
701                  * default streams prefix as private.
702                  *
703                  * By only calling samba_private_attr_name() in case
704                  * the xattr does NOT begin with the default prefix,
705                  * we know that if it returns 'true' it definitely one
706                  * of our internal xattr like "user.DOSATTRIB".
707                  */
708                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
709                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
710                         if (samba_private_attr_name(names[i])) {
711                                 continue;
712                         }
713                 }
714
715                 if (strncmp(names[i], config->prefix,
716                             config->prefix_len) != 0) {
717                         continue;
718                 }
719
720                 status = get_ea_value(names,
721                                         handle->conn,
722                                         NULL,
723                                         smb_fname,
724                                         names[i],
725                                         &ea);
726                 if (!NT_STATUS_IS_OK(status)) {
727                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
728                                 names[i],
729                                 smb_fname->base_name,
730                                 nt_errstr(status)));
731                         continue;
732                 }
733
734                 ea.name = talloc_asprintf(
735                         ea.value.data, ":%s%s",
736                         names[i] + config->prefix_len,
737                         config->store_stream_type ? "" : ":$DATA");
738                 if (ea.name == NULL) {
739                         DEBUG(0, ("talloc failed\n"));
740                         continue;
741                 }
742
743                 if (!fn(&ea, private_data)) {
744                         TALLOC_FREE(ea.value.data);
745                         return NT_STATUS_OK;
746                 }
747
748                 TALLOC_FREE(ea.value.data);
749         }
750
751         TALLOC_FREE(names);
752         return NT_STATUS_OK;
753 }
754
755 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
756                            struct stream_struct **streams,
757                            const char *name, off_t size,
758                            off_t alloc_size)
759 {
760         struct stream_struct *tmp;
761
762         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
763                                    (*num_streams)+1);
764         if (tmp == NULL) {
765                 return false;
766         }
767
768         tmp[*num_streams].name = talloc_strdup(tmp, name);
769         if (tmp[*num_streams].name == NULL) {
770                 return false;
771         }
772
773         tmp[*num_streams].size = size;
774         tmp[*num_streams].alloc_size = alloc_size;
775
776         *streams = tmp;
777         *num_streams += 1;
778         return true;
779 }
780
781 struct streaminfo_state {
782         TALLOC_CTX *mem_ctx;
783         vfs_handle_struct *handle;
784         unsigned int num_streams;
785         struct stream_struct *streams;
786         NTSTATUS status;
787 };
788
789 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
790 {
791         struct streaminfo_state *state =
792                 (struct streaminfo_state *)private_data;
793
794         if (!add_one_stream(state->mem_ctx,
795                             &state->num_streams, &state->streams,
796                             ea->name, ea->value.length-1,
797                             smb_roundup(state->handle->conn,
798                                         ea->value.length-1))) {
799                 state->status = NT_STATUS_NO_MEMORY;
800                 return false;
801         }
802
803         return true;
804 }
805
806 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
807                                          struct files_struct *fsp,
808                                          const struct smb_filename *smb_fname,
809                                          TALLOC_CTX *mem_ctx,
810                                          unsigned int *pnum_streams,
811                                          struct stream_struct **pstreams)
812 {
813         SMB_STRUCT_STAT sbuf;
814         int ret;
815         NTSTATUS status;
816         struct streaminfo_state state;
817
818         ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
819         if (ret == -1) {
820                 return map_nt_error_from_unix(errno);
821         }
822
823         state.streams = *pstreams;
824         state.num_streams = *pnum_streams;
825         state.mem_ctx = mem_ctx;
826         state.handle = handle;
827         state.status = NT_STATUS_OK;
828
829         if (S_ISLNK(sbuf.st_ex_mode)) {
830                 /*
831                  * Currently we do't have SMB_VFS_LLISTXATTR
832                  * inside the VFS which means there's no way
833                  * to cope with a symlink when lp_posix_pathnames().
834                  * returns true. For now ignore links.
835                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
836                  */
837                 status = NT_STATUS_OK;
838         } else {
839                 status = walk_xattr_streams(handle, fsp, smb_fname,
840                                     collect_one_stream, &state);
841         }
842
843         if (!NT_STATUS_IS_OK(status)) {
844                 TALLOC_FREE(state.streams);
845                 return status;
846         }
847
848         if (!NT_STATUS_IS_OK(state.status)) {
849                 TALLOC_FREE(state.streams);
850                 return state.status;
851         }
852
853         *pnum_streams = state.num_streams;
854         *pstreams = state.streams;
855
856         return SMB_VFS_NEXT_STREAMINFO(handle,
857                         fsp,
858                         smb_fname,
859                         mem_ctx,
860                         pnum_streams,
861                         pstreams);
862 }
863
864 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
865                         enum timestamp_set_resolution *p_ts_res)
866 {
867         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
868 }
869
870 static int streams_xattr_connect(vfs_handle_struct *handle,
871                                  const char *service, const char *user)
872 {
873         struct streams_xattr_config *config;
874         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
875         const char *prefix;
876         int rc;
877
878         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
879         if (rc != 0) {
880                 return rc;
881         }
882
883         config = talloc_zero(handle->conn, struct streams_xattr_config);
884         if (config == NULL) {
885                 DEBUG(1, ("talloc_zero() failed\n"));
886                 errno = ENOMEM;
887                 return -1;
888         }
889
890         prefix = lp_parm_const_string(SNUM(handle->conn),
891                                       "streams_xattr", "prefix",
892                                       default_prefix);
893         config->prefix = talloc_strdup(config, prefix);
894         if (config->prefix == NULL) {
895                 DEBUG(1, ("talloc_strdup() failed\n"));
896                 errno = ENOMEM;
897                 return -1;
898         }
899         config->prefix_len = strlen(config->prefix);
900         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
901
902         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
903                                                  "streams_xattr",
904                                                  "store_stream_type",
905                                                  true);
906
907         SMB_VFS_HANDLE_SET_DATA(handle, config,
908                                 NULL, struct stream_xattr_config,
909                                 return -1);
910
911         return 0;
912 }
913
914 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
915                                     files_struct *fsp, const void *data,
916                                     size_t n, off_t offset)
917 {
918         struct stream_io *sio =
919                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
920         struct ea_struct ea;
921         NTSTATUS status;
922         struct smb_filename *smb_fname_base = NULL;
923         int ret;
924
925         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
926
927         if (sio == NULL) {
928                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
929         }
930
931         if (!streams_xattr_recheck(sio)) {
932                 return -1;
933         }
934
935         /* Create an smb_filename with stream_name == NULL. */
936         smb_fname_base = synthetic_smb_fname(talloc_tos(),
937                                         sio->base,
938                                         NULL,
939                                         NULL,
940                                         fsp->fsp_name->twrp,
941                                         fsp->fsp_name->flags);
942         if (smb_fname_base == NULL) {
943                 errno = ENOMEM;
944                 return -1;
945         }
946
947         status = get_ea_value(talloc_tos(), handle->conn, NULL,
948                               smb_fname_base, sio->xattr_name, &ea);
949         if (!NT_STATUS_IS_OK(status)) {
950                 return -1;
951         }
952
953         if ((offset + n) > ea.value.length-1) {
954                 uint8_t *tmp;
955
956                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
957                                            offset + n + 1);
958
959                 if (tmp == NULL) {
960                         TALLOC_FREE(ea.value.data);
961                         errno = ENOMEM;
962                         return -1;
963                 }
964                 ea.value.data = tmp;
965                 ea.value.length = offset + n + 1;
966                 ea.value.data[offset+n] = 0;
967         }
968
969         memcpy(ea.value.data + offset, data, n);
970
971         ret = SMB_VFS_SETXATTR(fsp->conn,
972                                fsp->fsp_name,
973                                sio->xattr_name,
974                                ea.value.data, ea.value.length, 0);
975         TALLOC_FREE(ea.value.data);
976
977         if (ret == -1) {
978                 return -1;
979         }
980
981         return n;
982 }
983
984 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
985                                    files_struct *fsp, void *data,
986                                    size_t n, off_t offset)
987 {
988         struct stream_io *sio =
989                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
990         struct ea_struct ea;
991         NTSTATUS status;
992         size_t length, overlap;
993         struct smb_filename *smb_fname_base = NULL;
994
995         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
996                    (int)offset, (int)n));
997
998         if (sio == NULL) {
999                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1000         }
1001
1002         if (!streams_xattr_recheck(sio)) {
1003                 return -1;
1004         }
1005
1006         /* Create an smb_filename with stream_name == NULL. */
1007         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1008                                         sio->base,
1009                                         NULL,
1010                                         NULL,
1011                                         fsp->fsp_name->twrp,
1012                                         fsp->fsp_name->flags);
1013         if (smb_fname_base == NULL) {
1014                 errno = ENOMEM;
1015                 return -1;
1016         }
1017
1018         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1019                               smb_fname_base, sio->xattr_name, &ea);
1020         if (!NT_STATUS_IS_OK(status)) {
1021                 return -1;
1022         }
1023
1024         length = ea.value.length-1;
1025
1026         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1027                    (int)length));
1028
1029         /* Attempt to read past EOF. */
1030         if (length <= offset) {
1031                 return 0;
1032         }
1033
1034         overlap = (offset + n) > length ? (length - offset) : n;
1035         memcpy(data, ea.value.data + offset, overlap);
1036
1037         TALLOC_FREE(ea.value.data);
1038         return overlap;
1039 }
1040
1041 struct streams_xattr_pread_state {
1042         ssize_t nread;
1043         struct vfs_aio_state vfs_aio_state;
1044 };
1045
1046 static void streams_xattr_pread_done(struct tevent_req *subreq);
1047
1048 static struct tevent_req *streams_xattr_pread_send(
1049         struct vfs_handle_struct *handle,
1050         TALLOC_CTX *mem_ctx,
1051         struct tevent_context *ev,
1052         struct files_struct *fsp,
1053         void *data,
1054         size_t n, off_t offset)
1055 {
1056         struct tevent_req *req = NULL;
1057         struct tevent_req *subreq = NULL;
1058         struct streams_xattr_pread_state *state = NULL;
1059         struct stream_io *sio =
1060                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1061
1062         req = tevent_req_create(mem_ctx, &state,
1063                                 struct streams_xattr_pread_state);
1064         if (req == NULL) {
1065                 return NULL;
1066         }
1067
1068         if (sio == NULL) {
1069                 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1070                                                  data, n, offset);
1071                 if (tevent_req_nomem(req, subreq)) {
1072                         return tevent_req_post(req, ev);
1073                 }
1074                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1075                 return req;
1076         }
1077
1078         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1079         if (state->nread != n) {
1080                 if (state->nread != -1) {
1081                         errno = EIO;
1082                 }
1083                 tevent_req_error(req, errno);
1084                 return tevent_req_post(req, ev);
1085         }
1086
1087         tevent_req_done(req);
1088         return tevent_req_post(req, ev);
1089 }
1090
1091 static void streams_xattr_pread_done(struct tevent_req *subreq)
1092 {
1093         struct tevent_req *req = tevent_req_callback_data(
1094                 subreq, struct tevent_req);
1095         struct streams_xattr_pread_state *state = tevent_req_data(
1096                 req, struct streams_xattr_pread_state);
1097
1098         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1099         TALLOC_FREE(subreq);
1100
1101         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1102                 return;
1103         }
1104         tevent_req_done(req);
1105 }
1106
1107 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1108                                         struct vfs_aio_state *vfs_aio_state)
1109 {
1110         struct streams_xattr_pread_state *state = tevent_req_data(
1111                 req, struct streams_xattr_pread_state);
1112
1113         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1114                 return -1;
1115         }
1116
1117         *vfs_aio_state = state->vfs_aio_state;
1118         return state->nread;
1119 }
1120
1121 struct streams_xattr_pwrite_state {
1122         ssize_t nwritten;
1123         struct vfs_aio_state vfs_aio_state;
1124 };
1125
1126 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1127
1128 static struct tevent_req *streams_xattr_pwrite_send(
1129         struct vfs_handle_struct *handle,
1130         TALLOC_CTX *mem_ctx,
1131         struct tevent_context *ev,
1132         struct files_struct *fsp,
1133         const void *data,
1134         size_t n, off_t offset)
1135 {
1136         struct tevent_req *req = NULL;
1137         struct tevent_req *subreq = NULL;
1138         struct streams_xattr_pwrite_state *state = NULL;
1139         struct stream_io *sio =
1140                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1141
1142         req = tevent_req_create(mem_ctx, &state,
1143                                 struct streams_xattr_pwrite_state);
1144         if (req == NULL) {
1145                 return NULL;
1146         }
1147
1148         if (sio == NULL) {
1149                 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1150                                                   data, n, offset);
1151                 if (tevent_req_nomem(req, subreq)) {
1152                         return tevent_req_post(req, ev);
1153                 }
1154                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1155                 return req;
1156         }
1157
1158         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1159         if (state->nwritten != n) {
1160                 if (state->nwritten != -1) {
1161                         errno = EIO;
1162                 }
1163                 tevent_req_error(req, errno);
1164                 return tevent_req_post(req, ev);
1165         }
1166
1167         tevent_req_done(req);
1168         return tevent_req_post(req, ev);
1169 }
1170
1171 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1172 {
1173         struct tevent_req *req = tevent_req_callback_data(
1174                 subreq, struct tevent_req);
1175         struct streams_xattr_pwrite_state *state = tevent_req_data(
1176                 req, struct streams_xattr_pwrite_state);
1177
1178         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1179         TALLOC_FREE(subreq);
1180
1181         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1182                 return;
1183         }
1184         tevent_req_done(req);
1185 }
1186
1187 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1188                                          struct vfs_aio_state *vfs_aio_state)
1189 {
1190         struct streams_xattr_pwrite_state *state = tevent_req_data(
1191                 req, struct streams_xattr_pwrite_state);
1192
1193         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1194                 return -1;
1195         }
1196
1197         *vfs_aio_state = state->vfs_aio_state;
1198         return state->nwritten;
1199 }
1200
1201 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1202                                         struct files_struct *fsp,
1203                                         off_t offset)
1204 {
1205         int ret;
1206         uint8_t *tmp;
1207         struct ea_struct ea;
1208         NTSTATUS status;
1209         struct stream_io *sio =
1210                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1211         struct smb_filename *smb_fname_base = NULL;
1212
1213         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1214                    fsp_str_dbg(fsp), (double)offset));
1215
1216         if (sio == NULL) {
1217                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1218         }
1219
1220         if (!streams_xattr_recheck(sio)) {
1221                 return -1;
1222         }
1223
1224         /* Create an smb_filename with stream_name == NULL. */
1225         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1226                                         sio->base,
1227                                         NULL,
1228                                         NULL,
1229                                         fsp->fsp_name->twrp,
1230                                         fsp->fsp_name->flags);
1231         if (smb_fname_base == NULL) {
1232                 errno = ENOMEM;
1233                 return -1;
1234         }
1235
1236         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1237                               smb_fname_base, sio->xattr_name, &ea);
1238         if (!NT_STATUS_IS_OK(status)) {
1239                 return -1;
1240         }
1241
1242         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1243                                    offset + 1);
1244
1245         if (tmp == NULL) {
1246                 TALLOC_FREE(ea.value.data);
1247                 errno = ENOMEM;
1248                 return -1;
1249         }
1250
1251         /* Did we expand ? */
1252         if (ea.value.length < offset + 1) {
1253                 memset(&tmp[ea.value.length], '\0',
1254                         offset + 1 - ea.value.length);
1255         }
1256
1257         ea.value.data = tmp;
1258         ea.value.length = offset + 1;
1259         ea.value.data[offset] = 0;
1260
1261         ret = SMB_VFS_SETXATTR(fsp->conn,
1262                                fsp->fsp_name,
1263                                sio->xattr_name,
1264                                ea.value.data, ea.value.length, 0);
1265         TALLOC_FREE(ea.value.data);
1266
1267         if (ret == -1) {
1268                 return -1;
1269         }
1270
1271         return 0;
1272 }
1273
1274 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1275                                         struct files_struct *fsp,
1276                                         uint32_t mode,
1277                                         off_t offset,
1278                                         off_t len)
1279 {
1280         struct stream_io *sio =
1281                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1282
1283         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1284                 "len = %.0f\n",
1285                 fsp_str_dbg(fsp), (double)offset, (double)len));
1286
1287         if (sio == NULL) {
1288                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1289         }
1290
1291         if (!streams_xattr_recheck(sio)) {
1292                 return -1;
1293         }
1294
1295         /* Let the pwrite code path handle it. */
1296         errno = ENOSYS;
1297         return -1;
1298 }
1299
1300 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1301                                 uid_t uid, gid_t gid)
1302 {
1303         struct stream_io *sio =
1304                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1305
1306         if (sio == NULL) {
1307                 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1308         }
1309
1310         return 0;
1311 }
1312
1313 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1314                                 files_struct *fsp,
1315                                 mode_t mode)
1316 {
1317         struct stream_io *sio =
1318                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1319
1320         if (sio == NULL) {
1321                 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1322         }
1323
1324         return 0;
1325 }
1326
1327 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1328                                        struct files_struct *fsp,
1329                                        const char *name,
1330                                        void *value,
1331                                        size_t size)
1332 {
1333         struct stream_io *sio =
1334                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1335
1336         if (sio == NULL) {
1337                 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1338         }
1339
1340         errno = ENOTSUP;
1341         return -1;
1342 }
1343
1344 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1345                                         struct files_struct *fsp,
1346                                         char *list,
1347                                         size_t size)
1348 {
1349         struct stream_io *sio =
1350                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1351
1352         if (sio == NULL) {
1353                 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1354         }
1355
1356         errno = ENOTSUP;
1357         return -1;
1358 }
1359
1360 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1361                                       struct files_struct *fsp,
1362                                       const char *name)
1363 {
1364         struct stream_io *sio =
1365                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1366
1367         if (sio == NULL) {
1368                 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1369         }
1370
1371         errno = ENOTSUP;
1372         return -1;
1373 }
1374
1375 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1376                                    struct files_struct *fsp,
1377                                    const char *name,
1378                                    const void *value,
1379                                    size_t size,
1380                                    int flags)
1381 {
1382         struct stream_io *sio =
1383                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1384
1385         if (sio == NULL) {
1386                 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1387                                               size, flags);
1388         }
1389
1390         errno = ENOTSUP;
1391         return -1;
1392 }
1393
1394 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1395                                               files_struct *fsp,
1396                                               TALLOC_CTX *mem_ctx)
1397 {
1398         struct stream_io *sio =
1399                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1400
1401         if (sio == NULL) {
1402                 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1403         }
1404
1405         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1406                 handle, fsp->base_fsp->fsp_name,
1407                 SMB_ACL_TYPE_ACCESS, mem_ctx);
1408 }
1409
1410 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1411                                         files_struct *fsp,
1412                                         SMB_ACL_T theacl)
1413 {
1414         struct stream_io *sio =
1415                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1416
1417         if (sio == NULL) {
1418                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, theacl);
1419         }
1420
1421         return 0;
1422 }
1423
1424 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1425                                              files_struct *fsp,
1426                                              TALLOC_CTX *mem_ctx,
1427                                              char **blob_description,
1428                                              DATA_BLOB *blob)
1429 {
1430         struct stream_io *sio =
1431                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1432
1433         if (sio == NULL) {
1434                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1435                                                         blob_description, blob);
1436         }
1437
1438         return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1439                 handle, fsp->base_fsp->fsp_name, mem_ctx,
1440                 blob_description, blob);
1441 }
1442
1443 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1444                                           files_struct *fsp,
1445                                           uint32_t security_info,
1446                                           TALLOC_CTX *mem_ctx,
1447                                           struct security_descriptor **ppdesc)
1448 {
1449         struct stream_io *sio =
1450                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1451
1452         if (sio == NULL) {
1453                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1454                                                 mem_ctx, ppdesc);
1455         }
1456
1457         return SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
1458                                         handle->conn->cwd_fsp,
1459                                         fsp->base_fsp->fsp_name,
1460                                         security_info,
1461                                         mem_ctx,
1462                                         ppdesc);
1463 }
1464
1465 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1466                                           files_struct *fsp,
1467                                           uint32_t security_info_sent,
1468                                           const struct security_descriptor *psd)
1469 {
1470         struct stream_io *sio =
1471                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1472
1473         if (sio == NULL) {
1474                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1475                                                 security_info_sent, psd);
1476         }
1477
1478         return NT_STATUS_OK;
1479 }
1480
1481 struct streams_xattr_fsync_state {
1482         int ret;
1483         struct vfs_aio_state vfs_aio_state;
1484 };
1485
1486 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1487
1488 static struct tevent_req *streams_xattr_fsync_send(
1489         struct vfs_handle_struct *handle,
1490         TALLOC_CTX *mem_ctx,
1491         struct tevent_context *ev,
1492         struct files_struct *fsp)
1493 {
1494         struct tevent_req *req = NULL;
1495         struct tevent_req *subreq = NULL;
1496         struct streams_xattr_fsync_state *state = NULL;
1497         struct stream_io *sio =
1498                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1499
1500         req = tevent_req_create(mem_ctx, &state,
1501                                 struct streams_xattr_fsync_state);
1502         if (req == NULL) {
1503                 return NULL;
1504         }
1505
1506         if (sio == NULL) {
1507                 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1508                 if (tevent_req_nomem(req, subreq)) {
1509                         return tevent_req_post(req, ev);
1510                 }
1511                 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1512                 return req;
1513         }
1514
1515         /*
1516          * There's no pathname based sync variant and we don't have access to
1517          * the basefile handle, so we can't do anything here.
1518          */
1519
1520         tevent_req_done(req);
1521         return tevent_req_post(req, ev);
1522 }
1523
1524 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1525 {
1526         struct tevent_req *req = tevent_req_callback_data(
1527                 subreq, struct tevent_req);
1528         struct streams_xattr_fsync_state *state = tevent_req_data(
1529                 req, struct streams_xattr_fsync_state);
1530
1531         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1532         TALLOC_FREE(subreq);
1533         if (state->ret != 0) {
1534                 tevent_req_error(req, errno);
1535                 return;
1536         }
1537
1538         tevent_req_done(req);
1539 }
1540
1541 static int streams_xattr_fsync_recv(struct tevent_req *req,
1542                                     struct vfs_aio_state *vfs_aio_state)
1543 {
1544         struct streams_xattr_fsync_state *state = tevent_req_data(
1545                 req, struct streams_xattr_fsync_state);
1546
1547         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1548                 return -1;
1549         }
1550
1551         *vfs_aio_state = state->vfs_aio_state;
1552         return state->ret;
1553 }
1554
1555 static bool streams_xattr_lock(vfs_handle_struct *handle,
1556                                files_struct *fsp,
1557                                int op,
1558                                off_t offset,
1559                                off_t count,
1560                                int type)
1561 {
1562         struct stream_io *sio =
1563                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1564
1565         if (sio == NULL) {
1566                 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1567         }
1568
1569         return true;
1570 }
1571
1572 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1573                                   files_struct *fsp,
1574                                   off_t *poffset,
1575                                   off_t *pcount,
1576                                   int *ptype,
1577                                   pid_t *ppid)
1578 {
1579         struct stream_io *sio =
1580                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1581
1582         if (sio == NULL) {
1583                 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1584                                             pcount, ptype, ppid);
1585         }
1586
1587         errno = ENOTSUP;
1588         return false;
1589 }
1590
1591 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1592                                       files_struct *fsp,
1593                                       uint32_t share_access,
1594                                       uint32_t access_mask)
1595 {
1596         struct stream_io *sio =
1597                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1598
1599         if (sio == NULL) {
1600                 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1601                                                  share_access, access_mask);
1602         }
1603
1604         return 0;
1605 }
1606
1607 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1608                                         files_struct *fsp,
1609                                         int leasetype)
1610 {
1611         struct stream_io *sio =
1612                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1613
1614         if (sio == NULL) {
1615                 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1616         }
1617
1618         return 0;
1619 }
1620
1621 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1622                                             files_struct *fsp,
1623                                             struct lock_struct *plock)
1624 {
1625         struct stream_io *sio =
1626                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1627
1628         if (sio == NULL) {
1629                 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1630         }
1631
1632         return true;
1633 }
1634
1635 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1636         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1637         .connect_fn = streams_xattr_connect,
1638         .openat_fn = streams_xattr_openat,
1639         .close_fn = streams_xattr_close,
1640         .stat_fn = streams_xattr_stat,
1641         .fstat_fn = streams_xattr_fstat,
1642         .lstat_fn = streams_xattr_lstat,
1643         .pread_fn = streams_xattr_pread,
1644         .pwrite_fn = streams_xattr_pwrite,
1645         .pread_send_fn = streams_xattr_pread_send,
1646         .pread_recv_fn = streams_xattr_pread_recv,
1647         .pwrite_send_fn = streams_xattr_pwrite_send,
1648         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1649         .unlinkat_fn = streams_xattr_unlinkat,
1650         .renameat_fn = streams_xattr_renameat,
1651         .ftruncate_fn = streams_xattr_ftruncate,
1652         .fallocate_fn = streams_xattr_fallocate,
1653         .streaminfo_fn = streams_xattr_streaminfo,
1654
1655         .fsync_send_fn = streams_xattr_fsync_send,
1656         .fsync_recv_fn = streams_xattr_fsync_recv,
1657
1658         .lock_fn = streams_xattr_lock,
1659         .getlock_fn = streams_xattr_getlock,
1660         .kernel_flock_fn = streams_xattr_kernel_flock,
1661         .linux_setlease_fn = streams_xattr_linux_setlease,
1662         .strict_lock_check_fn = streams_xattr_strict_lock_check,
1663
1664         .fchown_fn = streams_xattr_fchown,
1665         .fchmod_fn = streams_xattr_fchmod,
1666
1667         .fgetxattr_fn = streams_xattr_fgetxattr,
1668         .flistxattr_fn = streams_xattr_flistxattr,
1669         .fremovexattr_fn = streams_xattr_fremovexattr,
1670         .fsetxattr_fn = streams_xattr_fsetxattr,
1671
1672         .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1673         .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1674         .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1675
1676         .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1677         .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1678 };
1679
1680 static_decl_vfs;
1681 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1682 {
1683         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1684                                 &vfs_streams_xattr_fns);
1685 }