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