vfs_streams_xattr: use pathref in streams_xattr_unlink_internal()
[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                 vfs_fake_fd_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 = vfs_fake_fd_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         SMB_ASSERT(smb_fname->fsp != NULL);
543         SMB_ASSERT(smb_fname->fsp->base_fsp != NULL);
544
545         ret = SMB_VFS_FREMOVEXATTR(smb_fname->fsp->base_fsp, xattr_name);
546
547         if ((ret == -1) && (errno == ENOATTR)) {
548                 errno = ENOENT;
549                 goto fail;
550         }
551
552         ret = 0;
553
554  fail:
555         TALLOC_FREE(xattr_name);
556         return ret;
557 }
558
559 static int streams_xattr_unlinkat(vfs_handle_struct *handle,
560                         struct files_struct *dirfsp,
561                         const struct smb_filename *smb_fname,
562                         int flags)
563 {
564         int ret;
565         if (flags & AT_REMOVEDIR) {
566                 ret = SMB_VFS_NEXT_UNLINKAT(handle,
567                                 dirfsp,
568                                 smb_fname,
569                                 flags);
570         } else {
571                 ret = streams_xattr_unlink_internal(handle,
572                                 dirfsp,
573                                 smb_fname,
574                                 flags);
575         }
576         return ret;
577 }
578
579 static int streams_xattr_renameat(vfs_handle_struct *handle,
580                                 files_struct *srcfsp,
581                                 const struct smb_filename *smb_fname_src,
582                                 files_struct *dstfsp,
583                                 const struct smb_filename *smb_fname_dst)
584 {
585         NTSTATUS status;
586         int ret = -1;
587         char *src_xattr_name = NULL;
588         char *dst_xattr_name = NULL;
589         bool src_is_stream, dst_is_stream;
590         ssize_t oret;
591         ssize_t nret;
592         struct ea_struct ea;
593
594         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
595         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
596
597         if (!src_is_stream && !dst_is_stream) {
598                 return SMB_VFS_NEXT_RENAMEAT(handle,
599                                         srcfsp,
600                                         smb_fname_src,
601                                         dstfsp,
602                                         smb_fname_dst);
603         }
604
605         /* For now don't allow renames from or to the default stream. */
606         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
607             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
608                 errno = ENOSYS;
609                 goto done;
610         }
611
612         /* Don't rename if the streams are identical. */
613         if (strcasecmp_m(smb_fname_src->stream_name,
614                        smb_fname_dst->stream_name) == 0) {
615                 goto done;
616         }
617
618         /* Get the xattr names. */
619         status = streams_xattr_get_name(handle, talloc_tos(),
620                                         smb_fname_src->stream_name,
621                                         &src_xattr_name);
622         if (!NT_STATUS_IS_OK(status)) {
623                 errno = map_errno_from_nt_status(status);
624                 goto fail;
625         }
626         status = streams_xattr_get_name(handle, talloc_tos(),
627                                         smb_fname_dst->stream_name,
628                                         &dst_xattr_name);
629         if (!NT_STATUS_IS_OK(status)) {
630                 errno = map_errno_from_nt_status(status);
631                 goto fail;
632         }
633
634         /* read the old stream */
635         status = get_ea_value(talloc_tos(), handle->conn, NULL,
636                               smb_fname_src, src_xattr_name, &ea);
637         if (!NT_STATUS_IS_OK(status)) {
638                 errno = ENOENT;
639                 goto fail;
640         }
641
642         /* (over)write the new stream */
643         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
644                                 dst_xattr_name, ea.value.data, ea.value.length,
645                                 0);
646         if (nret < 0) {
647                 if (errno == ENOATTR) {
648                         errno = ENOENT;
649                 }
650                 goto fail;
651         }
652
653         /* remove the old stream */
654         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
655                                    src_xattr_name);
656         if (oret < 0) {
657                 if (errno == ENOATTR) {
658                         errno = ENOENT;
659                 }
660                 goto fail;
661         }
662
663  done:
664         errno = 0;
665         ret = 0;
666  fail:
667         TALLOC_FREE(src_xattr_name);
668         TALLOC_FREE(dst_xattr_name);
669         return ret;
670 }
671
672 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
673                                 files_struct *fsp,
674                                 const struct smb_filename *smb_fname,
675                                 bool (*fn)(struct ea_struct *ea,
676                                         void *private_data),
677                                 void *private_data)
678 {
679         NTSTATUS status;
680         char **names;
681         size_t i, num_names;
682         struct streams_xattr_config *config;
683
684         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
685                                 return NT_STATUS_UNSUCCESSFUL);
686
687         status = get_ea_names_from_file(talloc_tos(),
688                                 handle->conn,
689                                 fsp,
690                                 smb_fname,
691                                 &names,
692                                 &num_names);
693         if (!NT_STATUS_IS_OK(status)) {
694                 return status;
695         }
696
697         for (i=0; i<num_names; i++) {
698                 struct ea_struct ea;
699
700                 /*
701                  * We want to check with samba_private_attr_name()
702                  * whether the xattr name is a private one,
703                  * unfortunately it flags xattrs that begin with the
704                  * default streams prefix as private.
705                  *
706                  * By only calling samba_private_attr_name() in case
707                  * the xattr does NOT begin with the default prefix,
708                  * we know that if it returns 'true' it definitely one
709                  * of our internal xattr like "user.DOSATTRIB".
710                  */
711                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
712                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
713                         if (samba_private_attr_name(names[i])) {
714                                 continue;
715                         }
716                 }
717
718                 if (strncmp(names[i], config->prefix,
719                             config->prefix_len) != 0) {
720                         continue;
721                 }
722
723                 status = get_ea_value(names,
724                                         handle->conn,
725                                         NULL,
726                                         smb_fname,
727                                         names[i],
728                                         &ea);
729                 if (!NT_STATUS_IS_OK(status)) {
730                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
731                                 names[i],
732                                 smb_fname->base_name,
733                                 nt_errstr(status)));
734                         continue;
735                 }
736
737                 ea.name = talloc_asprintf(
738                         ea.value.data, ":%s%s",
739                         names[i] + config->prefix_len,
740                         config->store_stream_type ? "" : ":$DATA");
741                 if (ea.name == NULL) {
742                         DEBUG(0, ("talloc failed\n"));
743                         continue;
744                 }
745
746                 if (!fn(&ea, private_data)) {
747                         TALLOC_FREE(ea.value.data);
748                         return NT_STATUS_OK;
749                 }
750
751                 TALLOC_FREE(ea.value.data);
752         }
753
754         TALLOC_FREE(names);
755         return NT_STATUS_OK;
756 }
757
758 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
759                            struct stream_struct **streams,
760                            const char *name, off_t size,
761                            off_t alloc_size)
762 {
763         struct stream_struct *tmp;
764
765         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
766                                    (*num_streams)+1);
767         if (tmp == NULL) {
768                 return false;
769         }
770
771         tmp[*num_streams].name = talloc_strdup(tmp, name);
772         if (tmp[*num_streams].name == NULL) {
773                 return false;
774         }
775
776         tmp[*num_streams].size = size;
777         tmp[*num_streams].alloc_size = alloc_size;
778
779         *streams = tmp;
780         *num_streams += 1;
781         return true;
782 }
783
784 struct streaminfo_state {
785         TALLOC_CTX *mem_ctx;
786         vfs_handle_struct *handle;
787         unsigned int num_streams;
788         struct stream_struct *streams;
789         NTSTATUS status;
790 };
791
792 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
793 {
794         struct streaminfo_state *state =
795                 (struct streaminfo_state *)private_data;
796
797         if (!add_one_stream(state->mem_ctx,
798                             &state->num_streams, &state->streams,
799                             ea->name, ea->value.length-1,
800                             smb_roundup(state->handle->conn,
801                                         ea->value.length-1))) {
802                 state->status = NT_STATUS_NO_MEMORY;
803                 return false;
804         }
805
806         return true;
807 }
808
809 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
810                                          struct files_struct *fsp,
811                                          const struct smb_filename *smb_fname,
812                                          TALLOC_CTX *mem_ctx,
813                                          unsigned int *pnum_streams,
814                                          struct stream_struct **pstreams)
815 {
816         SMB_STRUCT_STAT sbuf;
817         int ret;
818         NTSTATUS status;
819         struct streaminfo_state state;
820
821         ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
822         if (ret == -1) {
823                 return map_nt_error_from_unix(errno);
824         }
825
826         state.streams = *pstreams;
827         state.num_streams = *pnum_streams;
828         state.mem_ctx = mem_ctx;
829         state.handle = handle;
830         state.status = NT_STATUS_OK;
831
832         if (S_ISLNK(sbuf.st_ex_mode)) {
833                 /*
834                  * Currently we do't have SMB_VFS_LLISTXATTR
835                  * inside the VFS which means there's no way
836                  * to cope with a symlink when lp_posix_pathnames().
837                  * returns true. For now ignore links.
838                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
839                  */
840                 status = NT_STATUS_OK;
841         } else {
842                 status = walk_xattr_streams(handle, fsp, smb_fname,
843                                     collect_one_stream, &state);
844         }
845
846         if (!NT_STATUS_IS_OK(status)) {
847                 TALLOC_FREE(state.streams);
848                 return status;
849         }
850
851         if (!NT_STATUS_IS_OK(state.status)) {
852                 TALLOC_FREE(state.streams);
853                 return state.status;
854         }
855
856         *pnum_streams = state.num_streams;
857         *pstreams = state.streams;
858
859         return SMB_VFS_NEXT_STREAMINFO(handle,
860                         fsp,
861                         smb_fname,
862                         mem_ctx,
863                         pnum_streams,
864                         pstreams);
865 }
866
867 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
868                         enum timestamp_set_resolution *p_ts_res)
869 {
870         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
871 }
872
873 static int streams_xattr_connect(vfs_handle_struct *handle,
874                                  const char *service, const char *user)
875 {
876         struct streams_xattr_config *config;
877         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
878         const char *prefix;
879         int rc;
880
881         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
882         if (rc != 0) {
883                 return rc;
884         }
885
886         config = talloc_zero(handle->conn, struct streams_xattr_config);
887         if (config == NULL) {
888                 DEBUG(1, ("talloc_zero() failed\n"));
889                 errno = ENOMEM;
890                 return -1;
891         }
892
893         prefix = lp_parm_const_string(SNUM(handle->conn),
894                                       "streams_xattr", "prefix",
895                                       default_prefix);
896         config->prefix = talloc_strdup(config, prefix);
897         if (config->prefix == NULL) {
898                 DEBUG(1, ("talloc_strdup() failed\n"));
899                 errno = ENOMEM;
900                 return -1;
901         }
902         config->prefix_len = strlen(config->prefix);
903         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
904
905         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
906                                                  "streams_xattr",
907                                                  "store_stream_type",
908                                                  true);
909
910         SMB_VFS_HANDLE_SET_DATA(handle, config,
911                                 NULL, struct stream_xattr_config,
912                                 return -1);
913
914         return 0;
915 }
916
917 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
918                                     files_struct *fsp, const void *data,
919                                     size_t n, off_t offset)
920 {
921         struct stream_io *sio =
922                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
923         struct ea_struct ea;
924         NTSTATUS status;
925         struct smb_filename *smb_fname_base = NULL;
926         int ret;
927
928         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
929
930         if (sio == NULL) {
931                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
932         }
933
934         if (!streams_xattr_recheck(sio)) {
935                 return -1;
936         }
937
938         /* Create an smb_filename with stream_name == NULL. */
939         smb_fname_base = synthetic_smb_fname(talloc_tos(),
940                                         sio->base,
941                                         NULL,
942                                         NULL,
943                                         fsp->fsp_name->twrp,
944                                         fsp->fsp_name->flags);
945         if (smb_fname_base == NULL) {
946                 errno = ENOMEM;
947                 return -1;
948         }
949
950         status = get_ea_value(talloc_tos(), handle->conn, NULL,
951                               smb_fname_base, sio->xattr_name, &ea);
952         if (!NT_STATUS_IS_OK(status)) {
953                 return -1;
954         }
955
956         if ((offset + n) > ea.value.length-1) {
957                 uint8_t *tmp;
958
959                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
960                                            offset + n + 1);
961
962                 if (tmp == NULL) {
963                         TALLOC_FREE(ea.value.data);
964                         errno = ENOMEM;
965                         return -1;
966                 }
967                 ea.value.data = tmp;
968                 ea.value.length = offset + n + 1;
969                 ea.value.data[offset+n] = 0;
970         }
971
972         memcpy(ea.value.data + offset, data, n);
973
974         ret = SMB_VFS_SETXATTR(fsp->conn,
975                                fsp->fsp_name,
976                                sio->xattr_name,
977                                ea.value.data, ea.value.length, 0);
978         TALLOC_FREE(ea.value.data);
979
980         if (ret == -1) {
981                 return -1;
982         }
983
984         return n;
985 }
986
987 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
988                                    files_struct *fsp, void *data,
989                                    size_t n, off_t offset)
990 {
991         struct stream_io *sio =
992                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
993         struct ea_struct ea;
994         NTSTATUS status;
995         size_t length, overlap;
996         struct smb_filename *smb_fname_base = NULL;
997
998         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
999                    (int)offset, (int)n));
1000
1001         if (sio == NULL) {
1002                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1003         }
1004
1005         if (!streams_xattr_recheck(sio)) {
1006                 return -1;
1007         }
1008
1009         /* Create an smb_filename with stream_name == NULL. */
1010         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1011                                         sio->base,
1012                                         NULL,
1013                                         NULL,
1014                                         fsp->fsp_name->twrp,
1015                                         fsp->fsp_name->flags);
1016         if (smb_fname_base == NULL) {
1017                 errno = ENOMEM;
1018                 return -1;
1019         }
1020
1021         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1022                               smb_fname_base, sio->xattr_name, &ea);
1023         if (!NT_STATUS_IS_OK(status)) {
1024                 return -1;
1025         }
1026
1027         length = ea.value.length-1;
1028
1029         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1030                    (int)length));
1031
1032         /* Attempt to read past EOF. */
1033         if (length <= offset) {
1034                 return 0;
1035         }
1036
1037         overlap = (offset + n) > length ? (length - offset) : n;
1038         memcpy(data, ea.value.data + offset, overlap);
1039
1040         TALLOC_FREE(ea.value.data);
1041         return overlap;
1042 }
1043
1044 struct streams_xattr_pread_state {
1045         ssize_t nread;
1046         struct vfs_aio_state vfs_aio_state;
1047 };
1048
1049 static void streams_xattr_pread_done(struct tevent_req *subreq);
1050
1051 static struct tevent_req *streams_xattr_pread_send(
1052         struct vfs_handle_struct *handle,
1053         TALLOC_CTX *mem_ctx,
1054         struct tevent_context *ev,
1055         struct files_struct *fsp,
1056         void *data,
1057         size_t n, off_t offset)
1058 {
1059         struct tevent_req *req = NULL;
1060         struct tevent_req *subreq = NULL;
1061         struct streams_xattr_pread_state *state = NULL;
1062         struct stream_io *sio =
1063                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1064
1065         req = tevent_req_create(mem_ctx, &state,
1066                                 struct streams_xattr_pread_state);
1067         if (req == NULL) {
1068                 return NULL;
1069         }
1070
1071         if (sio == NULL) {
1072                 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1073                                                  data, n, offset);
1074                 if (tevent_req_nomem(req, subreq)) {
1075                         return tevent_req_post(req, ev);
1076                 }
1077                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1078                 return req;
1079         }
1080
1081         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1082         if (state->nread != n) {
1083                 if (state->nread != -1) {
1084                         errno = EIO;
1085                 }
1086                 tevent_req_error(req, errno);
1087                 return tevent_req_post(req, ev);
1088         }
1089
1090         tevent_req_done(req);
1091         return tevent_req_post(req, ev);
1092 }
1093
1094 static void streams_xattr_pread_done(struct tevent_req *subreq)
1095 {
1096         struct tevent_req *req = tevent_req_callback_data(
1097                 subreq, struct tevent_req);
1098         struct streams_xattr_pread_state *state = tevent_req_data(
1099                 req, struct streams_xattr_pread_state);
1100
1101         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1102         TALLOC_FREE(subreq);
1103
1104         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1105                 return;
1106         }
1107         tevent_req_done(req);
1108 }
1109
1110 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1111                                         struct vfs_aio_state *vfs_aio_state)
1112 {
1113         struct streams_xattr_pread_state *state = tevent_req_data(
1114                 req, struct streams_xattr_pread_state);
1115
1116         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1117                 return -1;
1118         }
1119
1120         *vfs_aio_state = state->vfs_aio_state;
1121         return state->nread;
1122 }
1123
1124 struct streams_xattr_pwrite_state {
1125         ssize_t nwritten;
1126         struct vfs_aio_state vfs_aio_state;
1127 };
1128
1129 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1130
1131 static struct tevent_req *streams_xattr_pwrite_send(
1132         struct vfs_handle_struct *handle,
1133         TALLOC_CTX *mem_ctx,
1134         struct tevent_context *ev,
1135         struct files_struct *fsp,
1136         const void *data,
1137         size_t n, off_t offset)
1138 {
1139         struct tevent_req *req = NULL;
1140         struct tevent_req *subreq = NULL;
1141         struct streams_xattr_pwrite_state *state = NULL;
1142         struct stream_io *sio =
1143                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1144
1145         req = tevent_req_create(mem_ctx, &state,
1146                                 struct streams_xattr_pwrite_state);
1147         if (req == NULL) {
1148                 return NULL;
1149         }
1150
1151         if (sio == NULL) {
1152                 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1153                                                   data, n, offset);
1154                 if (tevent_req_nomem(req, subreq)) {
1155                         return tevent_req_post(req, ev);
1156                 }
1157                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1158                 return req;
1159         }
1160
1161         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1162         if (state->nwritten != n) {
1163                 if (state->nwritten != -1) {
1164                         errno = EIO;
1165                 }
1166                 tevent_req_error(req, errno);
1167                 return tevent_req_post(req, ev);
1168         }
1169
1170         tevent_req_done(req);
1171         return tevent_req_post(req, ev);
1172 }
1173
1174 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1175 {
1176         struct tevent_req *req = tevent_req_callback_data(
1177                 subreq, struct tevent_req);
1178         struct streams_xattr_pwrite_state *state = tevent_req_data(
1179                 req, struct streams_xattr_pwrite_state);
1180
1181         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1182         TALLOC_FREE(subreq);
1183
1184         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1185                 return;
1186         }
1187         tevent_req_done(req);
1188 }
1189
1190 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1191                                          struct vfs_aio_state *vfs_aio_state)
1192 {
1193         struct streams_xattr_pwrite_state *state = tevent_req_data(
1194                 req, struct streams_xattr_pwrite_state);
1195
1196         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1197                 return -1;
1198         }
1199
1200         *vfs_aio_state = state->vfs_aio_state;
1201         return state->nwritten;
1202 }
1203
1204 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1205                                         struct files_struct *fsp,
1206                                         off_t offset)
1207 {
1208         int ret;
1209         uint8_t *tmp;
1210         struct ea_struct ea;
1211         NTSTATUS status;
1212         struct stream_io *sio =
1213                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1214         struct smb_filename *smb_fname_base = NULL;
1215
1216         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1217                    fsp_str_dbg(fsp), (double)offset));
1218
1219         if (sio == NULL) {
1220                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1221         }
1222
1223         if (!streams_xattr_recheck(sio)) {
1224                 return -1;
1225         }
1226
1227         /* Create an smb_filename with stream_name == NULL. */
1228         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1229                                         sio->base,
1230                                         NULL,
1231                                         NULL,
1232                                         fsp->fsp_name->twrp,
1233                                         fsp->fsp_name->flags);
1234         if (smb_fname_base == NULL) {
1235                 errno = ENOMEM;
1236                 return -1;
1237         }
1238
1239         status = get_ea_value(talloc_tos(), handle->conn, NULL,
1240                               smb_fname_base, sio->xattr_name, &ea);
1241         if (!NT_STATUS_IS_OK(status)) {
1242                 return -1;
1243         }
1244
1245         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1246                                    offset + 1);
1247
1248         if (tmp == NULL) {
1249                 TALLOC_FREE(ea.value.data);
1250                 errno = ENOMEM;
1251                 return -1;
1252         }
1253
1254         /* Did we expand ? */
1255         if (ea.value.length < offset + 1) {
1256                 memset(&tmp[ea.value.length], '\0',
1257                         offset + 1 - ea.value.length);
1258         }
1259
1260         ea.value.data = tmp;
1261         ea.value.length = offset + 1;
1262         ea.value.data[offset] = 0;
1263
1264         ret = SMB_VFS_SETXATTR(fsp->conn,
1265                                fsp->fsp_name,
1266                                sio->xattr_name,
1267                                ea.value.data, ea.value.length, 0);
1268         TALLOC_FREE(ea.value.data);
1269
1270         if (ret == -1) {
1271                 return -1;
1272         }
1273
1274         return 0;
1275 }
1276
1277 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1278                                         struct files_struct *fsp,
1279                                         uint32_t mode,
1280                                         off_t offset,
1281                                         off_t len)
1282 {
1283         struct stream_io *sio =
1284                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1285
1286         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1287                 "len = %.0f\n",
1288                 fsp_str_dbg(fsp), (double)offset, (double)len));
1289
1290         if (sio == NULL) {
1291                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1292         }
1293
1294         if (!streams_xattr_recheck(sio)) {
1295                 return -1;
1296         }
1297
1298         /* Let the pwrite code path handle it. */
1299         errno = ENOSYS;
1300         return -1;
1301 }
1302
1303 static int streams_xattr_fchown(vfs_handle_struct *handle, files_struct *fsp,
1304                                 uid_t uid, gid_t gid)
1305 {
1306         struct stream_io *sio =
1307                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1308
1309         if (sio == NULL) {
1310                 return SMB_VFS_NEXT_FCHOWN(handle, fsp, uid, gid);
1311         }
1312
1313         return 0;
1314 }
1315
1316 static int streams_xattr_fchmod(vfs_handle_struct *handle,
1317                                 files_struct *fsp,
1318                                 mode_t mode)
1319 {
1320         struct stream_io *sio =
1321                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1322
1323         if (sio == NULL) {
1324                 return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
1325         }
1326
1327         return 0;
1328 }
1329
1330 static ssize_t streams_xattr_fgetxattr(struct vfs_handle_struct *handle,
1331                                        struct files_struct *fsp,
1332                                        const char *name,
1333                                        void *value,
1334                                        size_t size)
1335 {
1336         struct stream_io *sio =
1337                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1338
1339         if (sio == NULL) {
1340                 return SMB_VFS_NEXT_FGETXATTR(handle, fsp, name, value, size);
1341         }
1342
1343         errno = ENOTSUP;
1344         return -1;
1345 }
1346
1347 static ssize_t streams_xattr_flistxattr(struct vfs_handle_struct *handle,
1348                                         struct files_struct *fsp,
1349                                         char *list,
1350                                         size_t size)
1351 {
1352         struct stream_io *sio =
1353                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1354
1355         if (sio == NULL) {
1356                 return SMB_VFS_NEXT_FLISTXATTR(handle, fsp, list, size);
1357         }
1358
1359         errno = ENOTSUP;
1360         return -1;
1361 }
1362
1363 static int streams_xattr_fremovexattr(struct vfs_handle_struct *handle,
1364                                       struct files_struct *fsp,
1365                                       const char *name)
1366 {
1367         struct stream_io *sio =
1368                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1369
1370         if (sio == NULL) {
1371                 return SMB_VFS_NEXT_FREMOVEXATTR(handle, fsp, name);
1372         }
1373
1374         errno = ENOTSUP;
1375         return -1;
1376 }
1377
1378 static int streams_xattr_fsetxattr(struct vfs_handle_struct *handle,
1379                                    struct files_struct *fsp,
1380                                    const char *name,
1381                                    const void *value,
1382                                    size_t size,
1383                                    int flags)
1384 {
1385         struct stream_io *sio =
1386                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1387
1388         if (sio == NULL) {
1389                 return SMB_VFS_NEXT_FSETXATTR(handle, fsp, name, value,
1390                                               size, flags);
1391         }
1392
1393         errno = ENOTSUP;
1394         return -1;
1395 }
1396
1397 static SMB_ACL_T streams_xattr_sys_acl_get_fd(vfs_handle_struct *handle,
1398                                               files_struct *fsp,
1399                                               TALLOC_CTX *mem_ctx)
1400 {
1401         struct stream_io *sio =
1402                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1403
1404         if (sio == NULL) {
1405                 return SMB_VFS_NEXT_SYS_ACL_GET_FD(handle, fsp, mem_ctx);
1406         }
1407
1408         return SMB_VFS_NEXT_SYS_ACL_GET_FILE(
1409                 handle, fsp->base_fsp->fsp_name,
1410                 SMB_ACL_TYPE_ACCESS, mem_ctx);
1411 }
1412
1413 static int streams_xattr_sys_acl_set_fd(vfs_handle_struct *handle,
1414                                         files_struct *fsp,
1415                                         SMB_ACL_TYPE_T type,
1416                                         SMB_ACL_T theacl)
1417 {
1418         struct stream_io *sio =
1419                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1420
1421         if (sio == NULL) {
1422                 return SMB_VFS_NEXT_SYS_ACL_SET_FD(handle, fsp, type, theacl);
1423         }
1424
1425         return 0;
1426 }
1427
1428 static int streams_xattr_sys_acl_blob_get_fd(vfs_handle_struct *handle,
1429                                              files_struct *fsp,
1430                                              TALLOC_CTX *mem_ctx,
1431                                              char **blob_description,
1432                                              DATA_BLOB *blob)
1433 {
1434         struct stream_io *sio =
1435                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1436
1437         if (sio == NULL) {
1438                 return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FD(handle, fsp, mem_ctx,
1439                                                         blob_description, blob);
1440         }
1441
1442         return SMB_VFS_NEXT_SYS_ACL_BLOB_GET_FILE(
1443                 handle, fsp->base_fsp->fsp_name, mem_ctx,
1444                 blob_description, blob);
1445 }
1446
1447 static NTSTATUS streams_xattr_fget_nt_acl(vfs_handle_struct *handle,
1448                                           files_struct *fsp,
1449                                           uint32_t security_info,
1450                                           TALLOC_CTX *mem_ctx,
1451                                           struct security_descriptor **ppdesc)
1452 {
1453         struct stream_io *sio =
1454                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1455
1456         if (sio == NULL) {
1457                 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1458                                                 mem_ctx, ppdesc);
1459         }
1460
1461         return SMB_VFS_NEXT_GET_NT_ACL_AT(handle,
1462                                         handle->conn->cwd_fsp,
1463                                         fsp->base_fsp->fsp_name,
1464                                         security_info,
1465                                         mem_ctx,
1466                                         ppdesc);
1467 }
1468
1469 static NTSTATUS streams_xattr_fset_nt_acl(vfs_handle_struct *handle,
1470                                           files_struct *fsp,
1471                                           uint32_t security_info_sent,
1472                                           const struct security_descriptor *psd)
1473 {
1474         struct stream_io *sio =
1475                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1476
1477         if (sio == NULL) {
1478                 return SMB_VFS_NEXT_FSET_NT_ACL(handle, fsp,
1479                                                 security_info_sent, psd);
1480         }
1481
1482         return NT_STATUS_OK;
1483 }
1484
1485 struct streams_xattr_fsync_state {
1486         int ret;
1487         struct vfs_aio_state vfs_aio_state;
1488 };
1489
1490 static void streams_xattr_fsync_done(struct tevent_req *subreq);
1491
1492 static struct tevent_req *streams_xattr_fsync_send(
1493         struct vfs_handle_struct *handle,
1494         TALLOC_CTX *mem_ctx,
1495         struct tevent_context *ev,
1496         struct files_struct *fsp)
1497 {
1498         struct tevent_req *req = NULL;
1499         struct tevent_req *subreq = NULL;
1500         struct streams_xattr_fsync_state *state = NULL;
1501         struct stream_io *sio =
1502                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1503
1504         req = tevent_req_create(mem_ctx, &state,
1505                                 struct streams_xattr_fsync_state);
1506         if (req == NULL) {
1507                 return NULL;
1508         }
1509
1510         if (sio == NULL) {
1511                 subreq = SMB_VFS_NEXT_FSYNC_SEND(state, ev, handle, fsp);
1512                 if (tevent_req_nomem(req, subreq)) {
1513                         return tevent_req_post(req, ev);
1514                 }
1515                 tevent_req_set_callback(subreq, streams_xattr_fsync_done, req);
1516                 return req;
1517         }
1518
1519         /*
1520          * There's no pathname based sync variant and we don't have access to
1521          * the basefile handle, so we can't do anything here.
1522          */
1523
1524         tevent_req_done(req);
1525         return tevent_req_post(req, ev);
1526 }
1527
1528 static void streams_xattr_fsync_done(struct tevent_req *subreq)
1529 {
1530         struct tevent_req *req = tevent_req_callback_data(
1531                 subreq, struct tevent_req);
1532         struct streams_xattr_fsync_state *state = tevent_req_data(
1533                 req, struct streams_xattr_fsync_state);
1534
1535         state->ret = SMB_VFS_FSYNC_RECV(subreq, &state->vfs_aio_state);
1536         TALLOC_FREE(subreq);
1537         if (state->ret != 0) {
1538                 tevent_req_error(req, errno);
1539                 return;
1540         }
1541
1542         tevent_req_done(req);
1543 }
1544
1545 static int streams_xattr_fsync_recv(struct tevent_req *req,
1546                                     struct vfs_aio_state *vfs_aio_state)
1547 {
1548         struct streams_xattr_fsync_state *state = tevent_req_data(
1549                 req, struct streams_xattr_fsync_state);
1550
1551         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1552                 return -1;
1553         }
1554
1555         *vfs_aio_state = state->vfs_aio_state;
1556         return state->ret;
1557 }
1558
1559 static bool streams_xattr_lock(vfs_handle_struct *handle,
1560                                files_struct *fsp,
1561                                int op,
1562                                off_t offset,
1563                                off_t count,
1564                                int type)
1565 {
1566         struct stream_io *sio =
1567                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1568
1569         if (sio == NULL) {
1570                 return SMB_VFS_NEXT_LOCK(handle, fsp, op, offset, count, type);
1571         }
1572
1573         return true;
1574 }
1575
1576 static bool streams_xattr_getlock(vfs_handle_struct *handle,
1577                                   files_struct *fsp,
1578                                   off_t *poffset,
1579                                   off_t *pcount,
1580                                   int *ptype,
1581                                   pid_t *ppid)
1582 {
1583         struct stream_io *sio =
1584                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1585
1586         if (sio == NULL) {
1587                 return SMB_VFS_NEXT_GETLOCK(handle, fsp, poffset,
1588                                             pcount, ptype, ppid);
1589         }
1590
1591         errno = ENOTSUP;
1592         return false;
1593 }
1594
1595 static int streams_xattr_kernel_flock(vfs_handle_struct *handle,
1596                                       files_struct *fsp,
1597                                       uint32_t share_access,
1598                                       uint32_t access_mask)
1599 {
1600         struct stream_io *sio =
1601                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1602
1603         if (sio == NULL) {
1604                 return SMB_VFS_NEXT_KERNEL_FLOCK(handle, fsp,
1605                                                  share_access, access_mask);
1606         }
1607
1608         return 0;
1609 }
1610
1611 static int streams_xattr_linux_setlease(vfs_handle_struct *handle,
1612                                         files_struct *fsp,
1613                                         int leasetype)
1614 {
1615         struct stream_io *sio =
1616                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1617
1618         if (sio == NULL) {
1619                 return SMB_VFS_NEXT_LINUX_SETLEASE(handle, fsp, leasetype);
1620         }
1621
1622         return 0;
1623 }
1624
1625 static bool streams_xattr_strict_lock_check(struct vfs_handle_struct *handle,
1626                                             files_struct *fsp,
1627                                             struct lock_struct *plock)
1628 {
1629         struct stream_io *sio =
1630                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1631
1632         if (sio == NULL) {
1633                 return SMB_VFS_NEXT_STRICT_LOCK_CHECK(handle, fsp, plock);
1634         }
1635
1636         return true;
1637 }
1638
1639 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1640         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1641         .connect_fn = streams_xattr_connect,
1642         .openat_fn = streams_xattr_openat,
1643         .close_fn = streams_xattr_close,
1644         .stat_fn = streams_xattr_stat,
1645         .fstat_fn = streams_xattr_fstat,
1646         .lstat_fn = streams_xattr_lstat,
1647         .pread_fn = streams_xattr_pread,
1648         .pwrite_fn = streams_xattr_pwrite,
1649         .pread_send_fn = streams_xattr_pread_send,
1650         .pread_recv_fn = streams_xattr_pread_recv,
1651         .pwrite_send_fn = streams_xattr_pwrite_send,
1652         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1653         .unlinkat_fn = streams_xattr_unlinkat,
1654         .renameat_fn = streams_xattr_renameat,
1655         .ftruncate_fn = streams_xattr_ftruncate,
1656         .fallocate_fn = streams_xattr_fallocate,
1657         .streaminfo_fn = streams_xattr_streaminfo,
1658
1659         .fsync_send_fn = streams_xattr_fsync_send,
1660         .fsync_recv_fn = streams_xattr_fsync_recv,
1661
1662         .lock_fn = streams_xattr_lock,
1663         .getlock_fn = streams_xattr_getlock,
1664         .kernel_flock_fn = streams_xattr_kernel_flock,
1665         .linux_setlease_fn = streams_xattr_linux_setlease,
1666         .strict_lock_check_fn = streams_xattr_strict_lock_check,
1667
1668         .fchown_fn = streams_xattr_fchown,
1669         .fchmod_fn = streams_xattr_fchmod,
1670
1671         .fgetxattr_fn = streams_xattr_fgetxattr,
1672         .flistxattr_fn = streams_xattr_flistxattr,
1673         .fremovexattr_fn = streams_xattr_fremovexattr,
1674         .fsetxattr_fn = streams_xattr_fsetxattr,
1675
1676         .sys_acl_get_fd_fn = streams_xattr_sys_acl_get_fd,
1677         .sys_acl_blob_get_fd_fn = streams_xattr_sys_acl_blob_get_fd,
1678         .sys_acl_set_fd_fn = streams_xattr_sys_acl_set_fd,
1679
1680         .fget_nt_acl_fn = streams_xattr_fget_nt_acl,
1681         .fset_nt_acl_fn = streams_xattr_fset_nt_acl,
1682 };
1683
1684 static_decl_vfs;
1685 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1686 {
1687         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1688                                 &vfs_streams_xattr_fns);
1689 }