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