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