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