vfs_streams_xattr: remove fsp argument from get_xattr_size()
[samba.git] / source3 / modules / vfs_streams_xattr.c
1 /*
2  * Store streams in xattrs
3  *
4  * Copyright (C) Volker Lendecke, 2008
5  *
6  * Partly based on James Peach's Darwin module, which is
7  *
8  * Copyright (C) James Peach 2006-2007
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "system/filesys.h"
27 #include "../lib/crypto/md5.h"
28 #include "lib/util/tevent_unix.h"
29
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_VFS
32
33 struct streams_xattr_config {
34         const char *prefix;
35         size_t prefix_len;
36         bool store_stream_type;
37 };
38
39 struct stream_io {
40         char *base;
41         char *xattr_name;
42         void *fsp_name_ptr;
43         files_struct *fsp;
44         vfs_handle_struct *handle;
45 };
46
47 static SMB_INO_T stream_inode(const SMB_STRUCT_STAT *sbuf, const char *sname)
48 {
49         MD5_CTX ctx;
50         unsigned char hash[16];
51         SMB_INO_T result;
52         char *upper_sname;
53
54         DEBUG(10, ("stream_inode called for %lu/%lu [%s]\n",
55                    (unsigned long)sbuf->st_ex_dev,
56                    (unsigned long)sbuf->st_ex_ino, sname));
57
58         upper_sname = talloc_strdup_upper(talloc_tos(), sname);
59         SMB_ASSERT(upper_sname != NULL);
60
61         MD5Init(&ctx);
62         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_dev),
63                   sizeof(sbuf->st_ex_dev));
64         MD5Update(&ctx, (const unsigned char *)&(sbuf->st_ex_ino),
65                   sizeof(sbuf->st_ex_ino));
66         MD5Update(&ctx, (unsigned char *)upper_sname,
67                   talloc_get_size(upper_sname)-1);
68         MD5Final(hash, &ctx);
69
70         TALLOC_FREE(upper_sname);
71
72         /* Hopefully all the variation is in the lower 4 (or 8) bytes! */
73         memcpy(&result, hash, sizeof(result));
74
75         DEBUG(10, ("stream_inode returns %lu\n", (unsigned long)result));
76
77         return result;
78 }
79
80 static ssize_t get_xattr_size(connection_struct *conn,
81                                 const struct smb_filename *smb_fname,
82                                 const char *xattr_name)
83 {
84         NTSTATUS status;
85         struct ea_struct ea;
86         ssize_t result;
87
88         status = get_ea_value(talloc_tos(), conn, NULL, smb_fname,
89                               xattr_name, &ea);
90
91         if (!NT_STATUS_IS_OK(status)) {
92                 return -1;
93         }
94
95         result = ea.value.length-1;
96         TALLOC_FREE(ea.value.data);
97         return result;
98 }
99
100 /**
101  * Given a stream name, populate xattr_name with the xattr name to use for
102  * accessing the stream.
103  */
104 static NTSTATUS streams_xattr_get_name(vfs_handle_struct *handle,
105                                        TALLOC_CTX *ctx,
106                                        const char *stream_name,
107                                        char **xattr_name)
108 {
109         char *sname;
110         char *stype;
111         struct streams_xattr_config *config;
112
113         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
114                                 return NT_STATUS_UNSUCCESSFUL);
115
116         sname = talloc_strdup(ctx, stream_name + 1);
117         if (sname == NULL) {
118                 return NT_STATUS_NO_MEMORY;
119         }
120
121         /*
122          * With vfs_fruit option "fruit:encoding = native" we're
123          * already converting stream names that contain illegal NTFS
124          * characters from their on-the-wire Unicode Private Range
125          * encoding to their native ASCII representation.
126          *
127          * As as result the name of xattrs storing the streams (via
128          * vfs_streams_xattr) may contain a colon, so we have to use
129          * strrchr_m() instead of strchr_m() for matching the stream
130          * type suffix.
131          *
132          * In check_path_syntax() we've already ensured the streamname
133          * we got from the client is valid.
134          */
135         stype = strrchr_m(sname, ':');
136
137         if (stype) {
138                 /*
139                  * We only support one stream type: "$DATA"
140                  */
141                 if (strcasecmp_m(stype, ":$DATA") != 0) {
142                         talloc_free(sname);
143                         return NT_STATUS_INVALID_PARAMETER;
144                 }
145
146                 /* Split name and type */
147                 stype[0] = '\0';
148         }
149
150         *xattr_name = talloc_asprintf(ctx, "%s%s%s",
151                                       config->prefix,
152                                       sname,
153                                       config->store_stream_type ? ":$DATA" : "");
154         if (*xattr_name == NULL) {
155                 talloc_free(sname);
156                 return NT_STATUS_NO_MEMORY;
157         }
158
159         DEBUG(10, ("xattr_name: %s, stream_name: %s\n", *xattr_name,
160                    stream_name));
161
162         talloc_free(sname);
163         return NT_STATUS_OK;
164 }
165
166 static bool streams_xattr_recheck(struct stream_io *sio)
167 {
168         NTSTATUS status;
169         char *xattr_name = NULL;
170
171         if (sio->fsp->fsp_name == sio->fsp_name_ptr) {
172                 return true;
173         }
174
175         if (sio->fsp->fsp_name->stream_name == NULL) {
176                 /* how can this happen */
177                 errno = EINVAL;
178                 return false;
179         }
180
181         status = streams_xattr_get_name(sio->handle, talloc_tos(),
182                                         sio->fsp->fsp_name->stream_name,
183                                         &xattr_name);
184         if (!NT_STATUS_IS_OK(status)) {
185                 return false;
186         }
187
188         TALLOC_FREE(sio->xattr_name);
189         TALLOC_FREE(sio->base);
190         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
191                                         xattr_name);
192         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(sio->handle, sio->fsp),
193                                   sio->fsp->fsp_name->base_name);
194         sio->fsp_name_ptr = sio->fsp->fsp_name;
195
196         TALLOC_FREE(xattr_name);
197
198         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
199                 return false;
200         }
201
202         return true;
203 }
204
205 /**
206  * Helper to stat/lstat the base file of an smb_fname.
207  */
208 static int streams_xattr_stat_base(vfs_handle_struct *handle,
209                                    struct smb_filename *smb_fname,
210                                    bool follow_links)
211 {
212         char *tmp_stream_name;
213         int result;
214
215         tmp_stream_name = smb_fname->stream_name;
216         smb_fname->stream_name = NULL;
217         if (follow_links) {
218                 result = SMB_VFS_NEXT_STAT(handle, smb_fname);
219         } else {
220                 result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
221         }
222         smb_fname->stream_name = tmp_stream_name;
223         return result;
224 }
225
226 static int streams_xattr_fstat(vfs_handle_struct *handle, files_struct *fsp,
227                                SMB_STRUCT_STAT *sbuf)
228 {
229         struct smb_filename *smb_fname_base = NULL;
230         int ret = -1;
231         struct stream_io *io = (struct stream_io *)
232                 VFS_FETCH_FSP_EXTENSION(handle, fsp);
233
234         DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
235
236         if (io == NULL || fsp->base_fsp == NULL) {
237                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
238         }
239
240         if (!streams_xattr_recheck(io)) {
241                 return -1;
242         }
243
244         /* Create an smb_filename with stream_name == NULL. */
245         smb_fname_base = synthetic_smb_fname(talloc_tos(),
246                                         io->base,
247                                         NULL,
248                                         NULL,
249                                         fsp->fsp_name->flags);
250         if (smb_fname_base == NULL) {
251                 errno = ENOMEM;
252                 return -1;
253         }
254
255         if (smb_fname_base->flags & SMB_FILENAME_POSIX_PATH) {
256                 ret = SMB_VFS_LSTAT(handle->conn, smb_fname_base);
257         } else {
258                 ret = SMB_VFS_STAT(handle->conn, smb_fname_base);
259         }
260         *sbuf = smb_fname_base->st;
261
262         if (ret == -1) {
263                 TALLOC_FREE(smb_fname_base);
264                 return -1;
265         }
266
267         sbuf->st_ex_size = get_xattr_size(handle->conn,
268                                         smb_fname_base, io->xattr_name);
269         if (sbuf->st_ex_size == -1) {
270                 TALLOC_FREE(smb_fname_base);
271                 SET_STAT_INVALID(*sbuf);
272                 return -1;
273         }
274
275         DEBUG(10, ("sbuf->st_ex_size = %d\n", (int)sbuf->st_ex_size));
276
277         sbuf->st_ex_ino = stream_inode(sbuf, io->xattr_name);
278         sbuf->st_ex_mode &= ~S_IFMT;
279         sbuf->st_ex_mode |= S_IFREG;
280         sbuf->st_ex_blocks = sbuf->st_ex_size / STAT_ST_BLOCKSIZE + 1;
281
282         TALLOC_FREE(smb_fname_base);
283         return 0;
284 }
285
286 static int streams_xattr_stat(vfs_handle_struct *handle,
287                               struct smb_filename *smb_fname)
288 {
289         NTSTATUS status;
290         int result = -1;
291         char *xattr_name = NULL;
292
293         if (!is_ntfs_stream_smb_fname(smb_fname)) {
294                 return SMB_VFS_NEXT_STAT(handle, smb_fname);
295         }
296
297         /* Note if lp_posix_paths() is true, we can never
298          * get here as is_ntfs_stream_smb_fname() is
299          * always false. So we never need worry about
300          * not following links here. */
301
302         /* If the default stream is requested, just stat the base file. */
303         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
304                 return streams_xattr_stat_base(handle, smb_fname, true);
305         }
306
307         /* Populate the stat struct with info from the base file. */
308         if (streams_xattr_stat_base(handle, smb_fname, true) == -1) {
309                 return -1;
310         }
311
312         /* Derive the xattr name to lookup. */
313         status = streams_xattr_get_name(handle, talloc_tos(),
314                                         smb_fname->stream_name, &xattr_name);
315         if (!NT_STATUS_IS_OK(status)) {
316                 errno = map_errno_from_nt_status(status);
317                 return -1;
318         }
319
320         /* Augment the base file's stat information before returning. */
321         smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
322                                                   smb_fname,
323                                                   xattr_name);
324         if (smb_fname->st.st_ex_size == -1) {
325                 SET_STAT_INVALID(smb_fname->st);
326                 errno = ENOENT;
327                 result = -1;
328                 goto fail;
329         }
330
331         smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
332         smb_fname->st.st_ex_mode &= ~S_IFMT;
333         smb_fname->st.st_ex_mode |= S_IFREG;
334         smb_fname->st.st_ex_blocks =
335             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
336
337         result = 0;
338  fail:
339         TALLOC_FREE(xattr_name);
340         return result;
341 }
342
343 static int streams_xattr_lstat(vfs_handle_struct *handle,
344                                struct smb_filename *smb_fname)
345 {
346         NTSTATUS status;
347         int result = -1;
348         char *xattr_name = NULL;
349
350         if (!is_ntfs_stream_smb_fname(smb_fname)) {
351                 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
352         }
353
354         /* If the default stream is requested, just stat the base file. */
355         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
356                 return streams_xattr_stat_base(handle, smb_fname, false);
357         }
358
359         /* Populate the stat struct with info from the base file. */
360         if (streams_xattr_stat_base(handle, smb_fname, false) == -1) {
361                 return -1;
362         }
363
364         /* Derive the xattr name to lookup. */
365         status = streams_xattr_get_name(handle, talloc_tos(),
366                                         smb_fname->stream_name, &xattr_name);
367         if (!NT_STATUS_IS_OK(status)) {
368                 errno = map_errno_from_nt_status(status);
369                 return -1;
370         }
371
372         /* Augment the base file's stat information before returning. */
373         smb_fname->st.st_ex_size = get_xattr_size(handle->conn,
374                                                   smb_fname,
375                                                   xattr_name);
376         if (smb_fname->st.st_ex_size == -1) {
377                 SET_STAT_INVALID(smb_fname->st);
378                 errno = ENOENT;
379                 result = -1;
380                 goto fail;
381         }
382
383         smb_fname->st.st_ex_ino = stream_inode(&smb_fname->st, xattr_name);
384         smb_fname->st.st_ex_mode &= ~S_IFMT;
385         smb_fname->st.st_ex_mode |= S_IFREG;
386         smb_fname->st.st_ex_blocks =
387             smb_fname->st.st_ex_size / STAT_ST_BLOCKSIZE + 1;
388
389         result = 0;
390
391  fail:
392         TALLOC_FREE(xattr_name);
393         return result;
394 }
395
396 static int streams_xattr_open(vfs_handle_struct *handle,
397                               struct smb_filename *smb_fname,
398                               files_struct *fsp, int flags, mode_t mode)
399 {
400         NTSTATUS status;
401         struct smb_filename *smb_fname_base = NULL;
402         struct stream_io *sio;
403         struct ea_struct ea;
404         char *xattr_name = NULL;
405         int baseflags;
406         int hostfd = -1;
407         int ret;
408
409         DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
410                    smb_fname_str_dbg(smb_fname), flags));
411
412         if (!is_ntfs_stream_smb_fname(smb_fname)) {
413                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
414         }
415
416         /* If the default stream is requested, just open the base file. */
417         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
418                 char *tmp_stream_name;
419
420                 tmp_stream_name = smb_fname->stream_name;
421                 smb_fname->stream_name = NULL;
422
423                 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
424
425                 smb_fname->stream_name = tmp_stream_name;
426
427                 return ret;
428         }
429
430         status = streams_xattr_get_name(handle, talloc_tos(),
431                                         smb_fname->stream_name, &xattr_name);
432         if (!NT_STATUS_IS_OK(status)) {
433                 errno = map_errno_from_nt_status(status);
434                 goto fail;
435         }
436
437         /* Create an smb_filename with stream_name == NULL. */
438         smb_fname_base = synthetic_smb_fname(talloc_tos(),
439                                 smb_fname->base_name,
440                                 NULL,
441                                 NULL,
442                                 smb_fname->flags);
443         if (smb_fname_base == NULL) {
444                 errno = ENOMEM;
445                 goto fail;
446         }
447
448         /*
449          * We use baseflags to turn off nasty side-effects when opening the
450          * underlying file.
451          */
452         baseflags = flags;
453         baseflags &= ~O_TRUNC;
454         baseflags &= ~O_EXCL;
455         baseflags &= ~O_CREAT;
456
457         hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
458                                    baseflags, mode);
459
460         /* It is legit to open a stream on a directory, but the base
461          * fd has to be read-only.
462          */
463         if ((hostfd == -1) && (errno == EISDIR)) {
464                 baseflags &= ~O_ACCMODE;
465                 baseflags |= O_RDONLY;
466                 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp, baseflags,
467                                            mode);
468         }
469
470         TALLOC_FREE(smb_fname_base);
471
472         if (hostfd == -1) {
473                 goto fail;
474         }
475
476         status = get_ea_value(talloc_tos(), handle->conn, NULL,
477                               smb_fname, xattr_name, &ea);
478
479         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
480
481         if (!NT_STATUS_IS_OK(status)
482             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
483                 /*
484                  * The base file is not there. This is an error even if we got
485                  * O_CREAT, the higher levels should have created the base
486                  * file for us.
487                  */
488                 DEBUG(10, ("streams_xattr_open: base file %s not around, "
489                            "returning ENOENT\n", smb_fname->base_name));
490                 errno = ENOENT;
491                 goto fail;
492         }
493
494         if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
495             (flags & O_TRUNC)) {
496                 /*
497                  * The attribute does not exist or needs to be truncated
498                  */
499
500                 /*
501                  * Darn, xattrs need at least 1 byte
502                  */
503                 char null = '\0';
504
505                 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
506                            xattr_name, smb_fname->base_name));
507
508                 ret = SMB_VFS_SETXATTR(fsp->conn,
509                                        smb_fname,
510                                        xattr_name,
511                                        &null, sizeof(null),
512                                        flags & O_EXCL ? XATTR_CREATE : 0);
513                 if (ret != 0) {
514                         goto fail;
515                 }
516         }
517
518         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
519                                                         struct stream_io,
520                                                         NULL);
521         if (sio == NULL) {
522                 errno = ENOMEM;
523                 goto fail;
524         }
525
526         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
527                                         xattr_name);
528         /*
529          * so->base needs to be a copy of fsp->fsp_name->base_name,
530          * making it identical to streams_xattr_recheck(). If the
531          * open is changing directories, fsp->fsp_name->base_name
532          * will be the full path from the share root, whilst
533          * smb_fname will be relative to the $cwd.
534          */
535         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
536                                   fsp->fsp_name->base_name);
537         sio->fsp_name_ptr = fsp->fsp_name;
538         sio->handle = handle;
539         sio->fsp = fsp;
540
541         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
542                 errno = ENOMEM;
543                 goto fail;
544         }
545
546         return hostfd;
547
548  fail:
549         if (hostfd >= 0) {
550                 /*
551                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
552                  * we don't have a full fsp yet
553                  */
554                 fsp->fh->fd = hostfd;
555                 SMB_VFS_NEXT_CLOSE(handle, fsp);
556         }
557
558         return -1;
559 }
560
561 static int streams_xattr_unlink(vfs_handle_struct *handle,
562                                 const struct smb_filename *smb_fname)
563 {
564         NTSTATUS status;
565         int ret = -1;
566         char *xattr_name = NULL;
567
568         if (!is_ntfs_stream_smb_fname(smb_fname)) {
569                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
570         }
571
572         /* If the default stream is requested, just open the base file. */
573         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
574                 struct smb_filename *smb_fname_base = NULL;
575
576                 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
577                 if (smb_fname_base == NULL) {
578                         errno = ENOMEM;
579                         return -1;
580                 }
581
582                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
583
584                 TALLOC_FREE(smb_fname_base);
585                 return ret;
586         }
587
588         status = streams_xattr_get_name(handle, talloc_tos(),
589                                         smb_fname->stream_name, &xattr_name);
590         if (!NT_STATUS_IS_OK(status)) {
591                 errno = map_errno_from_nt_status(status);
592                 goto fail;
593         }
594
595         ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
596
597         if ((ret == -1) && (errno == ENOATTR)) {
598                 errno = ENOENT;
599                 goto fail;
600         }
601
602         ret = 0;
603
604  fail:
605         TALLOC_FREE(xattr_name);
606         return ret;
607 }
608
609 static int streams_xattr_rename(vfs_handle_struct *handle,
610                                 const struct smb_filename *smb_fname_src,
611                                 const struct smb_filename *smb_fname_dst)
612 {
613         NTSTATUS status;
614         int ret = -1;
615         char *src_xattr_name = NULL;
616         char *dst_xattr_name = NULL;
617         bool src_is_stream, dst_is_stream;
618         ssize_t oret;
619         ssize_t nret;
620         struct ea_struct ea;
621
622         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
623         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
624
625         if (!src_is_stream && !dst_is_stream) {
626                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
627                                            smb_fname_dst);
628         }
629
630         /* For now don't allow renames from or to the default stream. */
631         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
632             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
633                 errno = ENOSYS;
634                 goto done;
635         }
636
637         /* Don't rename if the streams are identical. */
638         if (strcasecmp_m(smb_fname_src->stream_name,
639                        smb_fname_dst->stream_name) == 0) {
640                 goto done;
641         }
642
643         /* Get the xattr names. */
644         status = streams_xattr_get_name(handle, talloc_tos(),
645                                         smb_fname_src->stream_name,
646                                         &src_xattr_name);
647         if (!NT_STATUS_IS_OK(status)) {
648                 errno = map_errno_from_nt_status(status);
649                 goto fail;
650         }
651         status = streams_xattr_get_name(handle, talloc_tos(),
652                                         smb_fname_dst->stream_name,
653                                         &dst_xattr_name);
654         if (!NT_STATUS_IS_OK(status)) {
655                 errno = map_errno_from_nt_status(status);
656                 goto fail;
657         }
658
659         /* read the old stream */
660         status = get_ea_value(talloc_tos(), handle->conn, NULL,
661                               smb_fname_src, src_xattr_name, &ea);
662         if (!NT_STATUS_IS_OK(status)) {
663                 errno = ENOENT;
664                 goto fail;
665         }
666
667         /* (over)write the new stream */
668         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
669                                 dst_xattr_name, ea.value.data, ea.value.length,
670                                 0);
671         if (nret < 0) {
672                 if (errno == ENOATTR) {
673                         errno = ENOENT;
674                 }
675                 goto fail;
676         }
677
678         /* remove the old stream */
679         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
680                                    src_xattr_name);
681         if (oret < 0) {
682                 if (errno == ENOATTR) {
683                         errno = ENOENT;
684                 }
685                 goto fail;
686         }
687
688  done:
689         errno = 0;
690         ret = 0;
691  fail:
692         TALLOC_FREE(src_xattr_name);
693         TALLOC_FREE(dst_xattr_name);
694         return ret;
695 }
696
697 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
698                                 files_struct *fsp,
699                                 const struct smb_filename *smb_fname,
700                                 bool (*fn)(struct ea_struct *ea,
701                                         void *private_data),
702                                 void *private_data)
703 {
704         NTSTATUS status;
705         char **names;
706         size_t i, num_names;
707         struct streams_xattr_config *config;
708
709         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
710                                 return NT_STATUS_UNSUCCESSFUL);
711
712         status = get_ea_names_from_file(talloc_tos(),
713                                 handle->conn,
714                                 fsp,
715                                 smb_fname,
716                                 &names,
717                                 &num_names);
718         if (!NT_STATUS_IS_OK(status)) {
719                 return status;
720         }
721
722         for (i=0; i<num_names; i++) {
723                 struct ea_struct ea;
724
725                 /*
726                  * We want to check with samba_private_attr_name()
727                  * whether the xattr name is a private one,
728                  * unfortunately it flags xattrs that begin with the
729                  * default streams prefix as private.
730                  *
731                  * By only calling samba_private_attr_name() in case
732                  * the xattr does NOT begin with the default prefix,
733                  * we know that if it returns 'true' it definitely one
734                  * of our internal xattr like "user.DOSATTRIB".
735                  */
736                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
737                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
738                         if (samba_private_attr_name(names[i])) {
739                                 continue;
740                         }
741                 }
742
743                 if (strncmp(names[i], config->prefix,
744                             config->prefix_len) != 0) {
745                         continue;
746                 }
747
748                 status = get_ea_value(names,
749                                         handle->conn,
750                                         fsp,
751                                         smb_fname,
752                                         names[i],
753                                         &ea);
754                 if (!NT_STATUS_IS_OK(status)) {
755                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
756                                 names[i],
757                                 smb_fname->base_name,
758                                 nt_errstr(status)));
759                         continue;
760                 }
761
762                 ea.name = talloc_asprintf(
763                         ea.value.data, ":%s%s",
764                         names[i] + config->prefix_len,
765                         config->store_stream_type ? "" : ":$DATA");
766                 if (ea.name == NULL) {
767                         DEBUG(0, ("talloc failed\n"));
768                         continue;
769                 }
770
771                 if (!fn(&ea, private_data)) {
772                         TALLOC_FREE(ea.value.data);
773                         return NT_STATUS_OK;
774                 }
775
776                 TALLOC_FREE(ea.value.data);
777         }
778
779         TALLOC_FREE(names);
780         return NT_STATUS_OK;
781 }
782
783 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
784                            struct stream_struct **streams,
785                            const char *name, off_t size,
786                            off_t alloc_size)
787 {
788         struct stream_struct *tmp;
789
790         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
791                                    (*num_streams)+1);
792         if (tmp == NULL) {
793                 return false;
794         }
795
796         tmp[*num_streams].name = talloc_strdup(tmp, name);
797         if (tmp[*num_streams].name == NULL) {
798                 return false;
799         }
800
801         tmp[*num_streams].size = size;
802         tmp[*num_streams].alloc_size = alloc_size;
803
804         *streams = tmp;
805         *num_streams += 1;
806         return true;
807 }
808
809 struct streaminfo_state {
810         TALLOC_CTX *mem_ctx;
811         vfs_handle_struct *handle;
812         unsigned int num_streams;
813         struct stream_struct *streams;
814         NTSTATUS status;
815 };
816
817 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
818 {
819         struct streaminfo_state *state =
820                 (struct streaminfo_state *)private_data;
821
822         if (!add_one_stream(state->mem_ctx,
823                             &state->num_streams, &state->streams,
824                             ea->name, ea->value.length-1,
825                             smb_roundup(state->handle->conn,
826                                         ea->value.length-1))) {
827                 state->status = NT_STATUS_NO_MEMORY;
828                 return false;
829         }
830
831         return true;
832 }
833
834 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
835                                          struct files_struct *fsp,
836                                          const struct smb_filename *smb_fname,
837                                          TALLOC_CTX *mem_ctx,
838                                          unsigned int *pnum_streams,
839                                          struct stream_struct **pstreams)
840 {
841         SMB_STRUCT_STAT sbuf;
842         int ret;
843         NTSTATUS status;
844         struct streaminfo_state state;
845
846         ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
847         if (ret == -1) {
848                 return map_nt_error_from_unix(errno);
849         }
850
851         state.streams = *pstreams;
852         state.num_streams = *pnum_streams;
853         state.mem_ctx = mem_ctx;
854         state.handle = handle;
855         state.status = NT_STATUS_OK;
856
857         if (S_ISLNK(sbuf.st_ex_mode)) {
858                 /*
859                  * Currently we do't have SMB_VFS_LLISTXATTR
860                  * inside the VFS which means there's no way
861                  * to cope with a symlink when lp_posix_pathnames().
862                  * returns true. For now ignore links.
863                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
864                  */
865                 status = NT_STATUS_OK;
866         } else {
867                 status = walk_xattr_streams(handle, fsp, smb_fname,
868                                     collect_one_stream, &state);
869         }
870
871         if (!NT_STATUS_IS_OK(status)) {
872                 TALLOC_FREE(state.streams);
873                 return status;
874         }
875
876         if (!NT_STATUS_IS_OK(state.status)) {
877                 TALLOC_FREE(state.streams);
878                 return state.status;
879         }
880
881         *pnum_streams = state.num_streams;
882         *pstreams = state.streams;
883
884         return SMB_VFS_NEXT_STREAMINFO(handle,
885                         fsp,
886                         smb_fname,
887                         mem_ctx,
888                         pnum_streams,
889                         pstreams);
890 }
891
892 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
893                         enum timestamp_set_resolution *p_ts_res)
894 {
895         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
896 }
897
898 static int streams_xattr_connect(vfs_handle_struct *handle,
899                                  const char *service, const char *user)
900 {
901         struct streams_xattr_config *config;
902         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
903         const char *prefix;
904         int rc;
905
906         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
907         if (rc != 0) {
908                 return rc;
909         }
910
911         config = talloc_zero(handle->conn, struct streams_xattr_config);
912         if (config == NULL) {
913                 DEBUG(1, ("talloc_zero() failed\n"));
914                 errno = ENOMEM;
915                 return -1;
916         }
917
918         prefix = lp_parm_const_string(SNUM(handle->conn),
919                                       "streams_xattr", "prefix",
920                                       default_prefix);
921         config->prefix = talloc_strdup(config, prefix);
922         if (config->prefix == NULL) {
923                 DEBUG(1, ("talloc_strdup() failed\n"));
924                 errno = ENOMEM;
925                 return -1;
926         }
927         config->prefix_len = strlen(config->prefix);
928         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
929
930         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
931                                                  "streams_xattr",
932                                                  "store_stream_type",
933                                                  true);
934
935         SMB_VFS_HANDLE_SET_DATA(handle, config,
936                                 NULL, struct stream_xattr_config,
937                                 return -1);
938
939         return 0;
940 }
941
942 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
943                                     files_struct *fsp, const void *data,
944                                     size_t n, off_t offset)
945 {
946         struct stream_io *sio =
947                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
948         struct ea_struct ea;
949         NTSTATUS status;
950         struct smb_filename *smb_fname_base = NULL;
951         int ret;
952
953         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
954
955         if (sio == NULL) {
956                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
957         }
958
959         if (!streams_xattr_recheck(sio)) {
960                 return -1;
961         }
962
963         /* Create an smb_filename with stream_name == NULL. */
964         smb_fname_base = synthetic_smb_fname(talloc_tos(),
965                                         sio->base,
966                                         NULL,
967                                         NULL,
968                                         fsp->fsp_name->flags);
969         if (smb_fname_base == NULL) {
970                 errno = ENOMEM;
971                 return -1;
972         }
973
974         status = get_ea_value(talloc_tos(), handle->conn, fsp,
975                               smb_fname_base, sio->xattr_name, &ea);
976         if (!NT_STATUS_IS_OK(status)) {
977                 return -1;
978         }
979
980         if ((offset + n) > ea.value.length-1) {
981                 uint8_t *tmp;
982
983                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
984                                            offset + n + 1);
985
986                 if (tmp == NULL) {
987                         TALLOC_FREE(ea.value.data);
988                         errno = ENOMEM;
989                         return -1;
990                 }
991                 ea.value.data = tmp;
992                 ea.value.length = offset + n + 1;
993                 ea.value.data[offset+n] = 0;
994         }
995
996         memcpy(ea.value.data + offset, data, n);
997
998         ret = SMB_VFS_SETXATTR(fsp->conn,
999                                fsp->fsp_name,
1000                                sio->xattr_name,
1001                                ea.value.data, ea.value.length, 0);
1002         TALLOC_FREE(ea.value.data);
1003
1004         if (ret == -1) {
1005                 return -1;
1006         }
1007
1008         return n;
1009 }
1010
1011 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1012                                    files_struct *fsp, void *data,
1013                                    size_t n, off_t offset)
1014 {
1015         struct stream_io *sio =
1016                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1017         struct ea_struct ea;
1018         NTSTATUS status;
1019         size_t length, overlap;
1020         struct smb_filename *smb_fname_base = NULL;
1021
1022         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1023                    (int)offset, (int)n));
1024
1025         if (sio == NULL) {
1026                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1027         }
1028
1029         if (!streams_xattr_recheck(sio)) {
1030                 return -1;
1031         }
1032
1033         /* Create an smb_filename with stream_name == NULL. */
1034         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1035                                         sio->base,
1036                                         NULL,
1037                                         NULL,
1038                                         fsp->fsp_name->flags);
1039         if (smb_fname_base == NULL) {
1040                 errno = ENOMEM;
1041                 return -1;
1042         }
1043
1044         status = get_ea_value(talloc_tos(), handle->conn, fsp,
1045                               smb_fname_base, sio->xattr_name, &ea);
1046         if (!NT_STATUS_IS_OK(status)) {
1047                 return -1;
1048         }
1049
1050         length = ea.value.length-1;
1051
1052         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1053                    (int)length));
1054
1055         /* Attempt to read past EOF. */
1056         if (length <= offset) {
1057                 return 0;
1058         }
1059
1060         overlap = (offset + n) > length ? (length - offset) : n;
1061         memcpy(data, ea.value.data + offset, overlap);
1062
1063         TALLOC_FREE(ea.value.data);
1064         return overlap;
1065 }
1066
1067 struct streams_xattr_pread_state {
1068         ssize_t nread;
1069         struct vfs_aio_state vfs_aio_state;
1070 };
1071
1072 static void streams_xattr_pread_done(struct tevent_req *subreq);
1073
1074 static struct tevent_req *streams_xattr_pread_send(
1075         struct vfs_handle_struct *handle,
1076         TALLOC_CTX *mem_ctx,
1077         struct tevent_context *ev,
1078         struct files_struct *fsp,
1079         void *data,
1080         size_t n, off_t offset)
1081 {
1082         struct tevent_req *req = NULL;
1083         struct tevent_req *subreq = NULL;
1084         struct streams_xattr_pread_state *state = NULL;
1085         struct stream_io *sio =
1086                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1087
1088         req = tevent_req_create(mem_ctx, &state,
1089                                 struct streams_xattr_pread_state);
1090         if (req == NULL) {
1091                 return NULL;
1092         }
1093
1094         if (sio == NULL) {
1095                 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1096                                                  data, n, offset);
1097                 if (tevent_req_nomem(req, subreq)) {
1098                         return tevent_req_post(req, ev);
1099                 }
1100                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1101                 return req;
1102         }
1103
1104         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1105         if (state->nread != n) {
1106                 if (state->nread != -1) {
1107                         errno = EIO;
1108                 }
1109                 tevent_req_error(req, errno);
1110                 return tevent_req_post(req, ev);
1111         }
1112
1113         tevent_req_done(req);
1114         return tevent_req_post(req, ev);
1115 }
1116
1117 static void streams_xattr_pread_done(struct tevent_req *subreq)
1118 {
1119         struct tevent_req *req = tevent_req_callback_data(
1120                 subreq, struct tevent_req);
1121         struct streams_xattr_pread_state *state = tevent_req_data(
1122                 req, struct streams_xattr_pread_state);
1123
1124         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1125         TALLOC_FREE(subreq);
1126
1127         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1128                 return;
1129         }
1130         tevent_req_done(req);
1131 }
1132
1133 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1134                                         struct vfs_aio_state *vfs_aio_state)
1135 {
1136         struct streams_xattr_pread_state *state = tevent_req_data(
1137                 req, struct streams_xattr_pread_state);
1138
1139         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1140                 return -1;
1141         }
1142
1143         *vfs_aio_state = state->vfs_aio_state;
1144         return state->nread;
1145 }
1146
1147 struct streams_xattr_pwrite_state {
1148         ssize_t nwritten;
1149         struct vfs_aio_state vfs_aio_state;
1150 };
1151
1152 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1153
1154 static struct tevent_req *streams_xattr_pwrite_send(
1155         struct vfs_handle_struct *handle,
1156         TALLOC_CTX *mem_ctx,
1157         struct tevent_context *ev,
1158         struct files_struct *fsp,
1159         const void *data,
1160         size_t n, off_t offset)
1161 {
1162         struct tevent_req *req = NULL;
1163         struct tevent_req *subreq = NULL;
1164         struct streams_xattr_pwrite_state *state = NULL;
1165         struct stream_io *sio =
1166                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1167
1168         req = tevent_req_create(mem_ctx, &state,
1169                                 struct streams_xattr_pwrite_state);
1170         if (req == NULL) {
1171                 return NULL;
1172         }
1173
1174         if (sio == NULL) {
1175                 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1176                                                   data, n, offset);
1177                 if (tevent_req_nomem(req, subreq)) {
1178                         return tevent_req_post(req, ev);
1179                 }
1180                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1181                 return req;
1182         }
1183
1184         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1185         if (state->nwritten != n) {
1186                 if (state->nwritten != -1) {
1187                         errno = EIO;
1188                 }
1189                 tevent_req_error(req, errno);
1190                 return tevent_req_post(req, ev);
1191         }
1192
1193         tevent_req_done(req);
1194         return tevent_req_post(req, ev);
1195 }
1196
1197 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1198 {
1199         struct tevent_req *req = tevent_req_callback_data(
1200                 subreq, struct tevent_req);
1201         struct streams_xattr_pwrite_state *state = tevent_req_data(
1202                 req, struct streams_xattr_pwrite_state);
1203
1204         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1205         TALLOC_FREE(subreq);
1206
1207         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1208                 return;
1209         }
1210         tevent_req_done(req);
1211 }
1212
1213 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1214                                          struct vfs_aio_state *vfs_aio_state)
1215 {
1216         struct streams_xattr_pwrite_state *state = tevent_req_data(
1217                 req, struct streams_xattr_pwrite_state);
1218
1219         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1220                 return -1;
1221         }
1222
1223         *vfs_aio_state = state->vfs_aio_state;
1224         return state->nwritten;
1225 }
1226
1227 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1228                                         struct files_struct *fsp,
1229                                         off_t offset)
1230 {
1231         int ret;
1232         uint8_t *tmp;
1233         struct ea_struct ea;
1234         NTSTATUS status;
1235         struct stream_io *sio =
1236                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1237         struct smb_filename *smb_fname_base = NULL;
1238
1239         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1240                    fsp_str_dbg(fsp), (double)offset));
1241
1242         if (sio == NULL) {
1243                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1244         }
1245
1246         if (!streams_xattr_recheck(sio)) {
1247                 return -1;
1248         }
1249
1250         /* Create an smb_filename with stream_name == NULL. */
1251         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1252                                         sio->base,
1253                                         NULL,
1254                                         NULL,
1255                                         fsp->fsp_name->flags);
1256         if (smb_fname_base == NULL) {
1257                 errno = ENOMEM;
1258                 return -1;
1259         }
1260
1261         status = get_ea_value(talloc_tos(), handle->conn, fsp,
1262                               smb_fname_base, sio->xattr_name, &ea);
1263         if (!NT_STATUS_IS_OK(status)) {
1264                 return -1;
1265         }
1266
1267         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1268                                    offset + 1);
1269
1270         if (tmp == NULL) {
1271                 TALLOC_FREE(ea.value.data);
1272                 errno = ENOMEM;
1273                 return -1;
1274         }
1275
1276         /* Did we expand ? */
1277         if (ea.value.length < offset + 1) {
1278                 memset(&tmp[ea.value.length], '\0',
1279                         offset + 1 - ea.value.length);
1280         }
1281
1282         ea.value.data = tmp;
1283         ea.value.length = offset + 1;
1284         ea.value.data[offset] = 0;
1285
1286         ret = SMB_VFS_SETXATTR(fsp->conn,
1287                                fsp->fsp_name,
1288                                sio->xattr_name,
1289                                ea.value.data, ea.value.length, 0);
1290         TALLOC_FREE(ea.value.data);
1291
1292         if (ret == -1) {
1293                 return -1;
1294         }
1295
1296         return 0;
1297 }
1298
1299 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1300                                         struct files_struct *fsp,
1301                                         uint32_t mode,
1302                                         off_t offset,
1303                                         off_t len)
1304 {
1305         struct stream_io *sio =
1306                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1307
1308         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1309                 "len = %.0f\n",
1310                 fsp_str_dbg(fsp), (double)offset, (double)len));
1311
1312         if (sio == NULL) {
1313                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1314         }
1315
1316         if (!streams_xattr_recheck(sio)) {
1317                 return -1;
1318         }
1319
1320         /* Let the pwrite code path handle it. */
1321         errno = ENOSYS;
1322         return -1;
1323 }
1324
1325
1326 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1327         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1328         .connect_fn = streams_xattr_connect,
1329         .open_fn = streams_xattr_open,
1330         .stat_fn = streams_xattr_stat,
1331         .fstat_fn = streams_xattr_fstat,
1332         .lstat_fn = streams_xattr_lstat,
1333         .pread_fn = streams_xattr_pread,
1334         .pwrite_fn = streams_xattr_pwrite,
1335         .pread_send_fn = streams_xattr_pread_send,
1336         .pread_recv_fn = streams_xattr_pread_recv,
1337         .pwrite_send_fn = streams_xattr_pwrite_send,
1338         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1339         .unlink_fn = streams_xattr_unlink,
1340         .rename_fn = streams_xattr_rename,
1341         .ftruncate_fn = streams_xattr_ftruncate,
1342         .fallocate_fn = streams_xattr_fallocate,
1343         .streaminfo_fn = streams_xattr_streaminfo,
1344 };
1345
1346 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *);
1347 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1348 {
1349         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1350                                 &vfs_streams_xattr_fns);
1351 }