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