vfs_streams_xattr: remove all uses of fd, use name based functions
[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                                 files_struct *fsp,
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, fsp, 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         DBG_DEBUG("streams_xattr_fstat called for %s\n", fsp_str_dbg(io->fsp));
236
237         if (io == NULL || fsp->base_fsp == NULL) {
238                 return SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
239         }
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, fsp,
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, NULL,
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, NULL,
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 smb_filename *smb_fname_base = NULL;
403         struct stream_io *sio;
404         struct ea_struct ea;
405         char *xattr_name = NULL;
406         int baseflags;
407         int hostfd = -1;
408         int ret;
409
410         DEBUG(10, ("streams_xattr_open called for %s with flags 0x%x\n",
411                    smb_fname_str_dbg(smb_fname), flags));
412
413         if (!is_ntfs_stream_smb_fname(smb_fname)) {
414                 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
415         }
416
417         /* If the default stream is requested, just open the base file. */
418         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
419                 char *tmp_stream_name;
420
421                 tmp_stream_name = smb_fname->stream_name;
422                 smb_fname->stream_name = NULL;
423
424                 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
425
426                 smb_fname->stream_name = tmp_stream_name;
427
428                 return ret;
429         }
430
431         status = streams_xattr_get_name(handle, talloc_tos(),
432                                         smb_fname->stream_name, &xattr_name);
433         if (!NT_STATUS_IS_OK(status)) {
434                 errno = map_errno_from_nt_status(status);
435                 goto fail;
436         }
437
438         /* Create an smb_filename with stream_name == NULL. */
439         smb_fname_base = synthetic_smb_fname(talloc_tos(),
440                                 smb_fname->base_name,
441                                 NULL,
442                                 NULL,
443                                 smb_fname->flags);
444         if (smb_fname_base == NULL) {
445                 errno = ENOMEM;
446                 goto fail;
447         }
448
449         /*
450          * We use baseflags to turn off nasty side-effects when opening the
451          * underlying file.
452          */
453         baseflags = flags;
454         baseflags &= ~O_TRUNC;
455         baseflags &= ~O_EXCL;
456         baseflags &= ~O_CREAT;
457
458         hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp,
459                                    baseflags, mode);
460
461         /* It is legit to open a stream on a directory, but the base
462          * fd has to be read-only.
463          */
464         if ((hostfd == -1) && (errno == EISDIR)) {
465                 baseflags &= ~O_ACCMODE;
466                 baseflags |= O_RDONLY;
467                 hostfd = SMB_VFS_NEXT_OPEN(handle, smb_fname_base, fsp, baseflags,
468                                            mode);
469         }
470
471         TALLOC_FREE(smb_fname_base);
472
473         if (hostfd == -1) {
474                 goto fail;
475         }
476
477         status = get_ea_value(talloc_tos(), handle->conn, NULL,
478                               smb_fname, xattr_name, &ea);
479
480         DEBUG(10, ("get_ea_value returned %s\n", nt_errstr(status)));
481
482         if (!NT_STATUS_IS_OK(status)
483             && !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
484                 /*
485                  * The base file is not there. This is an error even if we got
486                  * O_CREAT, the higher levels should have created the base
487                  * file for us.
488                  */
489                 DEBUG(10, ("streams_xattr_open: base file %s not around, "
490                            "returning ENOENT\n", smb_fname->base_name));
491                 errno = ENOENT;
492                 goto fail;
493         }
494
495         if ((!NT_STATUS_IS_OK(status) && (flags & O_CREAT)) ||
496             (flags & O_TRUNC)) {
497                 /*
498                  * The attribute does not exist or needs to be truncated
499                  */
500
501                 /*
502                  * Darn, xattrs need at least 1 byte
503                  */
504                 char null = '\0';
505
506                 DEBUG(10, ("creating or truncating attribute %s on file %s\n",
507                            xattr_name, smb_fname->base_name));
508
509                 ret = SMB_VFS_SETXATTR(fsp->conn,
510                                        smb_fname,
511                                        xattr_name,
512                                        &null, sizeof(null),
513                                        flags & O_EXCL ? XATTR_CREATE : 0);
514                 if (ret != 0) {
515                         goto fail;
516                 }
517         }
518
519         sio = (struct stream_io *)VFS_ADD_FSP_EXTENSION(handle, fsp,
520                                                         struct stream_io,
521                                                         NULL);
522         if (sio == NULL) {
523                 errno = ENOMEM;
524                 goto fail;
525         }
526
527         sio->xattr_name = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
528                                         xattr_name);
529         /*
530          * so->base needs to be a copy of fsp->fsp_name->base_name,
531          * making it identical to streams_xattr_recheck(). If the
532          * open is changing directories, fsp->fsp_name->base_name
533          * will be the full path from the share root, whilst
534          * smb_fname will be relative to the $cwd.
535          */
536         sio->base = talloc_strdup(VFS_MEMCTX_FSP_EXTENSION(handle, fsp),
537                                   fsp->fsp_name->base_name);
538         sio->fsp_name_ptr = fsp->fsp_name;
539         sio->handle = handle;
540         sio->fsp = fsp;
541
542         if ((sio->xattr_name == NULL) || (sio->base == NULL)) {
543                 errno = ENOMEM;
544                 goto fail;
545         }
546
547         return hostfd;
548
549  fail:
550         if (hostfd >= 0) {
551                 /*
552                  * BUGBUGBUG -- we would need to call fd_close_posix here, but
553                  * we don't have a full fsp yet
554                  */
555                 fsp->fh->fd = hostfd;
556                 SMB_VFS_NEXT_CLOSE(handle, fsp);
557         }
558
559         return -1;
560 }
561
562 static int streams_xattr_unlink(vfs_handle_struct *handle,
563                                 const struct smb_filename *smb_fname)
564 {
565         NTSTATUS status;
566         int ret = -1;
567         char *xattr_name = NULL;
568
569         if (!is_ntfs_stream_smb_fname(smb_fname)) {
570                 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
571         }
572
573         /* If the default stream is requested, just open the base file. */
574         if (is_ntfs_default_stream_smb_fname(smb_fname)) {
575                 struct smb_filename *smb_fname_base = NULL;
576
577                 smb_fname_base = cp_smb_filename(talloc_tos(), smb_fname);
578                 if (smb_fname_base == NULL) {
579                         errno = ENOMEM;
580                         return -1;
581                 }
582
583                 ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_base);
584
585                 TALLOC_FREE(smb_fname_base);
586                 return ret;
587         }
588
589         status = streams_xattr_get_name(handle, talloc_tos(),
590                                         smb_fname->stream_name, &xattr_name);
591         if (!NT_STATUS_IS_OK(status)) {
592                 errno = map_errno_from_nt_status(status);
593                 goto fail;
594         }
595
596         ret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname, xattr_name);
597
598         if ((ret == -1) && (errno == ENOATTR)) {
599                 errno = ENOENT;
600                 goto fail;
601         }
602
603         ret = 0;
604
605  fail:
606         TALLOC_FREE(xattr_name);
607         return ret;
608 }
609
610 static int streams_xattr_rename(vfs_handle_struct *handle,
611                                 const struct smb_filename *smb_fname_src,
612                                 const struct smb_filename *smb_fname_dst)
613 {
614         NTSTATUS status;
615         int ret = -1;
616         char *src_xattr_name = NULL;
617         char *dst_xattr_name = NULL;
618         bool src_is_stream, dst_is_stream;
619         ssize_t oret;
620         ssize_t nret;
621         struct ea_struct ea;
622
623         src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
624         dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
625
626         if (!src_is_stream && !dst_is_stream) {
627                 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
628                                            smb_fname_dst);
629         }
630
631         /* For now don't allow renames from or to the default stream. */
632         if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
633             is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
634                 errno = ENOSYS;
635                 goto done;
636         }
637
638         /* Don't rename if the streams are identical. */
639         if (strcasecmp_m(smb_fname_src->stream_name,
640                        smb_fname_dst->stream_name) == 0) {
641                 goto done;
642         }
643
644         /* Get the xattr names. */
645         status = streams_xattr_get_name(handle, talloc_tos(),
646                                         smb_fname_src->stream_name,
647                                         &src_xattr_name);
648         if (!NT_STATUS_IS_OK(status)) {
649                 errno = map_errno_from_nt_status(status);
650                 goto fail;
651         }
652         status = streams_xattr_get_name(handle, talloc_tos(),
653                                         smb_fname_dst->stream_name,
654                                         &dst_xattr_name);
655         if (!NT_STATUS_IS_OK(status)) {
656                 errno = map_errno_from_nt_status(status);
657                 goto fail;
658         }
659
660         /* read the old stream */
661         status = get_ea_value(talloc_tos(), handle->conn, NULL,
662                               smb_fname_src, src_xattr_name, &ea);
663         if (!NT_STATUS_IS_OK(status)) {
664                 errno = ENOENT;
665                 goto fail;
666         }
667
668         /* (over)write the new stream */
669         nret = SMB_VFS_SETXATTR(handle->conn, smb_fname_src,
670                                 dst_xattr_name, ea.value.data, ea.value.length,
671                                 0);
672         if (nret < 0) {
673                 if (errno == ENOATTR) {
674                         errno = ENOENT;
675                 }
676                 goto fail;
677         }
678
679         /* remove the old stream */
680         oret = SMB_VFS_REMOVEXATTR(handle->conn, smb_fname_src,
681                                    src_xattr_name);
682         if (oret < 0) {
683                 if (errno == ENOATTR) {
684                         errno = ENOENT;
685                 }
686                 goto fail;
687         }
688
689  done:
690         errno = 0;
691         ret = 0;
692  fail:
693         TALLOC_FREE(src_xattr_name);
694         TALLOC_FREE(dst_xattr_name);
695         return ret;
696 }
697
698 static NTSTATUS walk_xattr_streams(vfs_handle_struct *handle,
699                                 files_struct *fsp,
700                                 const struct smb_filename *smb_fname,
701                                 bool (*fn)(struct ea_struct *ea,
702                                         void *private_data),
703                                 void *private_data)
704 {
705         NTSTATUS status;
706         char **names;
707         size_t i, num_names;
708         struct streams_xattr_config *config;
709
710         SMB_VFS_HANDLE_GET_DATA(handle, config, struct streams_xattr_config,
711                                 return NT_STATUS_UNSUCCESSFUL);
712
713         status = get_ea_names_from_file(talloc_tos(),
714                                 handle->conn,
715                                 fsp,
716                                 smb_fname,
717                                 &names,
718                                 &num_names);
719         if (!NT_STATUS_IS_OK(status)) {
720                 return status;
721         }
722
723         for (i=0; i<num_names; i++) {
724                 struct ea_struct ea;
725
726                 /*
727                  * We want to check with samba_private_attr_name()
728                  * whether the xattr name is a private one,
729                  * unfortunately it flags xattrs that begin with the
730                  * default streams prefix as private.
731                  *
732                  * By only calling samba_private_attr_name() in case
733                  * the xattr does NOT begin with the default prefix,
734                  * we know that if it returns 'true' it definitely one
735                  * of our internal xattr like "user.DOSATTRIB".
736                  */
737                 if (strncasecmp_m(names[i], SAMBA_XATTR_DOSSTREAM_PREFIX,
738                                   strlen(SAMBA_XATTR_DOSSTREAM_PREFIX)) != 0) {
739                         if (samba_private_attr_name(names[i])) {
740                                 continue;
741                         }
742                 }
743
744                 if (strncmp(names[i], config->prefix,
745                             config->prefix_len) != 0) {
746                         continue;
747                 }
748
749                 status = get_ea_value(names,
750                                         handle->conn,
751                                         fsp,
752                                         smb_fname,
753                                         names[i],
754                                         &ea);
755                 if (!NT_STATUS_IS_OK(status)) {
756                         DEBUG(10, ("Could not get ea %s for file %s: %s\n",
757                                 names[i],
758                                 smb_fname->base_name,
759                                 nt_errstr(status)));
760                         continue;
761                 }
762
763                 ea.name = talloc_asprintf(
764                         ea.value.data, ":%s%s",
765                         names[i] + config->prefix_len,
766                         config->store_stream_type ? "" : ":$DATA");
767                 if (ea.name == NULL) {
768                         DEBUG(0, ("talloc failed\n"));
769                         continue;
770                 }
771
772                 if (!fn(&ea, private_data)) {
773                         TALLOC_FREE(ea.value.data);
774                         return NT_STATUS_OK;
775                 }
776
777                 TALLOC_FREE(ea.value.data);
778         }
779
780         TALLOC_FREE(names);
781         return NT_STATUS_OK;
782 }
783
784 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
785                            struct stream_struct **streams,
786                            const char *name, off_t size,
787                            off_t alloc_size)
788 {
789         struct stream_struct *tmp;
790
791         tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
792                                    (*num_streams)+1);
793         if (tmp == NULL) {
794                 return false;
795         }
796
797         tmp[*num_streams].name = talloc_strdup(tmp, name);
798         if (tmp[*num_streams].name == NULL) {
799                 return false;
800         }
801
802         tmp[*num_streams].size = size;
803         tmp[*num_streams].alloc_size = alloc_size;
804
805         *streams = tmp;
806         *num_streams += 1;
807         return true;
808 }
809
810 struct streaminfo_state {
811         TALLOC_CTX *mem_ctx;
812         vfs_handle_struct *handle;
813         unsigned int num_streams;
814         struct stream_struct *streams;
815         NTSTATUS status;
816 };
817
818 static bool collect_one_stream(struct ea_struct *ea, void *private_data)
819 {
820         struct streaminfo_state *state =
821                 (struct streaminfo_state *)private_data;
822
823         if (!add_one_stream(state->mem_ctx,
824                             &state->num_streams, &state->streams,
825                             ea->name, ea->value.length-1,
826                             smb_roundup(state->handle->conn,
827                                         ea->value.length-1))) {
828                 state->status = NT_STATUS_NO_MEMORY;
829                 return false;
830         }
831
832         return true;
833 }
834
835 static NTSTATUS streams_xattr_streaminfo(vfs_handle_struct *handle,
836                                          struct files_struct *fsp,
837                                          const struct smb_filename *smb_fname,
838                                          TALLOC_CTX *mem_ctx,
839                                          unsigned int *pnum_streams,
840                                          struct stream_struct **pstreams)
841 {
842         SMB_STRUCT_STAT sbuf;
843         int ret;
844         NTSTATUS status;
845         struct streaminfo_state state;
846
847         ret = vfs_stat_smb_basename(handle->conn, smb_fname, &sbuf);
848         if (ret == -1) {
849                 return map_nt_error_from_unix(errno);
850         }
851
852         state.streams = *pstreams;
853         state.num_streams = *pnum_streams;
854         state.mem_ctx = mem_ctx;
855         state.handle = handle;
856         state.status = NT_STATUS_OK;
857
858         if (S_ISLNK(sbuf.st_ex_mode)) {
859                 /*
860                  * Currently we do't have SMB_VFS_LLISTXATTR
861                  * inside the VFS which means there's no way
862                  * to cope with a symlink when lp_posix_pathnames().
863                  * returns true. For now ignore links.
864                  * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
865                  */
866                 status = NT_STATUS_OK;
867         } else {
868                 status = walk_xattr_streams(handle, fsp, smb_fname,
869                                     collect_one_stream, &state);
870         }
871
872         if (!NT_STATUS_IS_OK(status)) {
873                 TALLOC_FREE(state.streams);
874                 return status;
875         }
876
877         if (!NT_STATUS_IS_OK(state.status)) {
878                 TALLOC_FREE(state.streams);
879                 return state.status;
880         }
881
882         *pnum_streams = state.num_streams;
883         *pstreams = state.streams;
884
885         return SMB_VFS_NEXT_STREAMINFO(handle,
886                         fsp,
887                         smb_fname,
888                         mem_ctx,
889                         pnum_streams,
890                         pstreams);
891 }
892
893 static uint32_t streams_xattr_fs_capabilities(struct vfs_handle_struct *handle,
894                         enum timestamp_set_resolution *p_ts_res)
895 {
896         return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
897 }
898
899 static int streams_xattr_connect(vfs_handle_struct *handle,
900                                  const char *service, const char *user)
901 {
902         struct streams_xattr_config *config;
903         const char *default_prefix = SAMBA_XATTR_DOSSTREAM_PREFIX;
904         const char *prefix;
905         int rc;
906
907         rc = SMB_VFS_NEXT_CONNECT(handle, service, user);
908         if (rc != 0) {
909                 return rc;
910         }
911
912         config = talloc_zero(handle->conn, struct streams_xattr_config);
913         if (config == NULL) {
914                 DEBUG(1, ("talloc_zero() failed\n"));
915                 errno = ENOMEM;
916                 return -1;
917         }
918
919         prefix = lp_parm_const_string(SNUM(handle->conn),
920                                       "streams_xattr", "prefix",
921                                       default_prefix);
922         config->prefix = talloc_strdup(config, prefix);
923         if (config->prefix == NULL) {
924                 DEBUG(1, ("talloc_strdup() failed\n"));
925                 errno = ENOMEM;
926                 return -1;
927         }
928         config->prefix_len = strlen(config->prefix);
929         DEBUG(10, ("streams_xattr using stream prefix: %s\n", config->prefix));
930
931         config->store_stream_type = lp_parm_bool(SNUM(handle->conn),
932                                                  "streams_xattr",
933                                                  "store_stream_type",
934                                                  true);
935
936         SMB_VFS_HANDLE_SET_DATA(handle, config,
937                                 NULL, struct stream_xattr_config,
938                                 return -1);
939
940         return 0;
941 }
942
943 static ssize_t streams_xattr_pwrite(vfs_handle_struct *handle,
944                                     files_struct *fsp, const void *data,
945                                     size_t n, off_t offset)
946 {
947         struct stream_io *sio =
948                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
949         struct ea_struct ea;
950         NTSTATUS status;
951         struct smb_filename *smb_fname_base = NULL;
952         int ret;
953
954         DEBUG(10, ("streams_xattr_pwrite called for %d bytes\n", (int)n));
955
956         if (sio == NULL) {
957                 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
958         }
959
960         if (!streams_xattr_recheck(sio)) {
961                 return -1;
962         }
963
964         /* Create an smb_filename with stream_name == NULL. */
965         smb_fname_base = synthetic_smb_fname(talloc_tos(),
966                                         sio->base,
967                                         NULL,
968                                         NULL,
969                                         fsp->fsp_name->flags);
970         if (smb_fname_base == NULL) {
971                 errno = ENOMEM;
972                 return -1;
973         }
974
975         status = get_ea_value(talloc_tos(), handle->conn, fsp,
976                               smb_fname_base, sio->xattr_name, &ea);
977         if (!NT_STATUS_IS_OK(status)) {
978                 return -1;
979         }
980
981         if ((offset + n) > ea.value.length-1) {
982                 uint8_t *tmp;
983
984                 tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
985                                            offset + n + 1);
986
987                 if (tmp == NULL) {
988                         TALLOC_FREE(ea.value.data);
989                         errno = ENOMEM;
990                         return -1;
991                 }
992                 ea.value.data = tmp;
993                 ea.value.length = offset + n + 1;
994                 ea.value.data[offset+n] = 0;
995         }
996
997         memcpy(ea.value.data + offset, data, n);
998
999         ret = SMB_VFS_SETXATTR(fsp->conn,
1000                                fsp->fsp_name,
1001                                sio->xattr_name,
1002                                ea.value.data, ea.value.length, 0);
1003         TALLOC_FREE(ea.value.data);
1004
1005         if (ret == -1) {
1006                 return -1;
1007         }
1008
1009         return n;
1010 }
1011
1012 static ssize_t streams_xattr_pread(vfs_handle_struct *handle,
1013                                    files_struct *fsp, void *data,
1014                                    size_t n, off_t offset)
1015 {
1016         struct stream_io *sio =
1017                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1018         struct ea_struct ea;
1019         NTSTATUS status;
1020         size_t length, overlap;
1021         struct smb_filename *smb_fname_base = NULL;
1022
1023         DEBUG(10, ("streams_xattr_pread: offset=%d, size=%d\n",
1024                    (int)offset, (int)n));
1025
1026         if (sio == NULL) {
1027                 return SMB_VFS_NEXT_PREAD(handle, fsp, data, n, offset);
1028         }
1029
1030         if (!streams_xattr_recheck(sio)) {
1031                 return -1;
1032         }
1033
1034         /* Create an smb_filename with stream_name == NULL. */
1035         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1036                                         sio->base,
1037                                         NULL,
1038                                         NULL,
1039                                         fsp->fsp_name->flags);
1040         if (smb_fname_base == NULL) {
1041                 errno = ENOMEM;
1042                 return -1;
1043         }
1044
1045         status = get_ea_value(talloc_tos(), handle->conn, fsp,
1046                               smb_fname_base, sio->xattr_name, &ea);
1047         if (!NT_STATUS_IS_OK(status)) {
1048                 return -1;
1049         }
1050
1051         length = ea.value.length-1;
1052
1053         DEBUG(10, ("streams_xattr_pread: get_ea_value returned %d bytes\n",
1054                    (int)length));
1055
1056         /* Attempt to read past EOF. */
1057         if (length <= offset) {
1058                 return 0;
1059         }
1060
1061         overlap = (offset + n) > length ? (length - offset) : n;
1062         memcpy(data, ea.value.data + offset, overlap);
1063
1064         TALLOC_FREE(ea.value.data);
1065         return overlap;
1066 }
1067
1068 struct streams_xattr_pread_state {
1069         ssize_t nread;
1070         struct vfs_aio_state vfs_aio_state;
1071 };
1072
1073 static void streams_xattr_pread_done(struct tevent_req *subreq);
1074
1075 static struct tevent_req *streams_xattr_pread_send(
1076         struct vfs_handle_struct *handle,
1077         TALLOC_CTX *mem_ctx,
1078         struct tevent_context *ev,
1079         struct files_struct *fsp,
1080         void *data,
1081         size_t n, off_t offset)
1082 {
1083         struct tevent_req *req = NULL;
1084         struct tevent_req *subreq = NULL;
1085         struct streams_xattr_pread_state *state = NULL;
1086         struct stream_io *sio =
1087                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1088
1089         req = tevent_req_create(mem_ctx, &state,
1090                                 struct streams_xattr_pread_state);
1091         if (req == NULL) {
1092                 return NULL;
1093         }
1094
1095         if (sio == NULL) {
1096                 subreq = SMB_VFS_NEXT_PREAD_SEND(state, ev, handle, fsp,
1097                                                  data, n, offset);
1098                 if (tevent_req_nomem(req, subreq)) {
1099                         return tevent_req_post(req, ev);
1100                 }
1101                 tevent_req_set_callback(subreq, streams_xattr_pread_done, req);
1102                 return req;
1103         }
1104
1105         state->nread = SMB_VFS_PREAD(fsp, data, n, offset);
1106         if (state->nread != n) {
1107                 if (state->nread != -1) {
1108                         errno = EIO;
1109                 }
1110                 tevent_req_error(req, errno);
1111                 return tevent_req_post(req, ev);
1112         }
1113
1114         tevent_req_done(req);
1115         return tevent_req_post(req, ev);
1116 }
1117
1118 static void streams_xattr_pread_done(struct tevent_req *subreq)
1119 {
1120         struct tevent_req *req = tevent_req_callback_data(
1121                 subreq, struct tevent_req);
1122         struct streams_xattr_pread_state *state = tevent_req_data(
1123                 req, struct streams_xattr_pread_state);
1124
1125         state->nread = SMB_VFS_PREAD_RECV(subreq, &state->vfs_aio_state);
1126         TALLOC_FREE(subreq);
1127
1128         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1129                 return;
1130         }
1131         tevent_req_done(req);
1132 }
1133
1134 static ssize_t streams_xattr_pread_recv(struct tevent_req *req,
1135                                         struct vfs_aio_state *vfs_aio_state)
1136 {
1137         struct streams_xattr_pread_state *state = tevent_req_data(
1138                 req, struct streams_xattr_pread_state);
1139
1140         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1141                 return -1;
1142         }
1143
1144         *vfs_aio_state = state->vfs_aio_state;
1145         return state->nread;
1146 }
1147
1148 struct streams_xattr_pwrite_state {
1149         ssize_t nwritten;
1150         struct vfs_aio_state vfs_aio_state;
1151 };
1152
1153 static void streams_xattr_pwrite_done(struct tevent_req *subreq);
1154
1155 static struct tevent_req *streams_xattr_pwrite_send(
1156         struct vfs_handle_struct *handle,
1157         TALLOC_CTX *mem_ctx,
1158         struct tevent_context *ev,
1159         struct files_struct *fsp,
1160         const void *data,
1161         size_t n, off_t offset)
1162 {
1163         struct tevent_req *req = NULL;
1164         struct tevent_req *subreq = NULL;
1165         struct streams_xattr_pwrite_state *state = NULL;
1166         struct stream_io *sio =
1167                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1168
1169         req = tevent_req_create(mem_ctx, &state,
1170                                 struct streams_xattr_pwrite_state);
1171         if (req == NULL) {
1172                 return NULL;
1173         }
1174
1175         if (sio == NULL) {
1176                 subreq = SMB_VFS_NEXT_PWRITE_SEND(state, ev, handle, fsp,
1177                                                   data, n, offset);
1178                 if (tevent_req_nomem(req, subreq)) {
1179                         return tevent_req_post(req, ev);
1180                 }
1181                 tevent_req_set_callback(subreq, streams_xattr_pwrite_done, req);
1182                 return req;
1183         }
1184
1185         state->nwritten = SMB_VFS_PWRITE(fsp, data, n, offset);
1186         if (state->nwritten != n) {
1187                 if (state->nwritten != -1) {
1188                         errno = EIO;
1189                 }
1190                 tevent_req_error(req, errno);
1191                 return tevent_req_post(req, ev);
1192         }
1193
1194         tevent_req_done(req);
1195         return tevent_req_post(req, ev);
1196 }
1197
1198 static void streams_xattr_pwrite_done(struct tevent_req *subreq)
1199 {
1200         struct tevent_req *req = tevent_req_callback_data(
1201                 subreq, struct tevent_req);
1202         struct streams_xattr_pwrite_state *state = tevent_req_data(
1203                 req, struct streams_xattr_pwrite_state);
1204
1205         state->nwritten = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
1206         TALLOC_FREE(subreq);
1207
1208         if (tevent_req_error(req, state->vfs_aio_state.error)) {
1209                 return;
1210         }
1211         tevent_req_done(req);
1212 }
1213
1214 static ssize_t streams_xattr_pwrite_recv(struct tevent_req *req,
1215                                          struct vfs_aio_state *vfs_aio_state)
1216 {
1217         struct streams_xattr_pwrite_state *state = tevent_req_data(
1218                 req, struct streams_xattr_pwrite_state);
1219
1220         if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
1221                 return -1;
1222         }
1223
1224         *vfs_aio_state = state->vfs_aio_state;
1225         return state->nwritten;
1226 }
1227
1228 static int streams_xattr_ftruncate(struct vfs_handle_struct *handle,
1229                                         struct files_struct *fsp,
1230                                         off_t offset)
1231 {
1232         int ret;
1233         uint8_t *tmp;
1234         struct ea_struct ea;
1235         NTSTATUS status;
1236         struct stream_io *sio =
1237                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1238         struct smb_filename *smb_fname_base = NULL;
1239
1240         DEBUG(10, ("streams_xattr_ftruncate called for file %s offset %.0f\n",
1241                    fsp_str_dbg(fsp), (double)offset));
1242
1243         if (sio == NULL) {
1244                 return SMB_VFS_NEXT_FTRUNCATE(handle, fsp, offset);
1245         }
1246
1247         if (!streams_xattr_recheck(sio)) {
1248                 return -1;
1249         }
1250
1251         /* Create an smb_filename with stream_name == NULL. */
1252         smb_fname_base = synthetic_smb_fname(talloc_tos(),
1253                                         sio->base,
1254                                         NULL,
1255                                         NULL,
1256                                         fsp->fsp_name->flags);
1257         if (smb_fname_base == NULL) {
1258                 errno = ENOMEM;
1259                 return -1;
1260         }
1261
1262         status = get_ea_value(talloc_tos(), handle->conn, fsp,
1263                               smb_fname_base, sio->xattr_name, &ea);
1264         if (!NT_STATUS_IS_OK(status)) {
1265                 return -1;
1266         }
1267
1268         tmp = talloc_realloc(talloc_tos(), ea.value.data, uint8_t,
1269                                    offset + 1);
1270
1271         if (tmp == NULL) {
1272                 TALLOC_FREE(ea.value.data);
1273                 errno = ENOMEM;
1274                 return -1;
1275         }
1276
1277         /* Did we expand ? */
1278         if (ea.value.length < offset + 1) {
1279                 memset(&tmp[ea.value.length], '\0',
1280                         offset + 1 - ea.value.length);
1281         }
1282
1283         ea.value.data = tmp;
1284         ea.value.length = offset + 1;
1285         ea.value.data[offset] = 0;
1286
1287         ret = SMB_VFS_SETXATTR(fsp->conn,
1288                                fsp->fsp_name,
1289                                sio->xattr_name,
1290                                ea.value.data, ea.value.length, 0);
1291         TALLOC_FREE(ea.value.data);
1292
1293         if (ret == -1) {
1294                 return -1;
1295         }
1296
1297         return 0;
1298 }
1299
1300 static int streams_xattr_fallocate(struct vfs_handle_struct *handle,
1301                                         struct files_struct *fsp,
1302                                         uint32_t mode,
1303                                         off_t offset,
1304                                         off_t len)
1305 {
1306         struct stream_io *sio =
1307                 (struct stream_io *)VFS_FETCH_FSP_EXTENSION(handle, fsp);
1308
1309         DEBUG(10, ("streams_xattr_fallocate called for file %s offset %.0f"
1310                 "len = %.0f\n",
1311                 fsp_str_dbg(fsp), (double)offset, (double)len));
1312
1313         if (sio == NULL) {
1314                 return SMB_VFS_NEXT_FALLOCATE(handle, fsp, mode, offset, len);
1315         }
1316
1317         if (!streams_xattr_recheck(sio)) {
1318                 return -1;
1319         }
1320
1321         /* Let the pwrite code path handle it. */
1322         errno = ENOSYS;
1323         return -1;
1324 }
1325
1326
1327 static struct vfs_fn_pointers vfs_streams_xattr_fns = {
1328         .fs_capabilities_fn = streams_xattr_fs_capabilities,
1329         .connect_fn = streams_xattr_connect,
1330         .open_fn = streams_xattr_open,
1331         .stat_fn = streams_xattr_stat,
1332         .fstat_fn = streams_xattr_fstat,
1333         .lstat_fn = streams_xattr_lstat,
1334         .pread_fn = streams_xattr_pread,
1335         .pwrite_fn = streams_xattr_pwrite,
1336         .pread_send_fn = streams_xattr_pread_send,
1337         .pread_recv_fn = streams_xattr_pread_recv,
1338         .pwrite_send_fn = streams_xattr_pwrite_send,
1339         .pwrite_recv_fn = streams_xattr_pwrite_recv,
1340         .unlink_fn = streams_xattr_unlink,
1341         .rename_fn = streams_xattr_rename,
1342         .ftruncate_fn = streams_xattr_ftruncate,
1343         .fallocate_fn = streams_xattr_fallocate,
1344         .streaminfo_fn = streams_xattr_streaminfo,
1345 };
1346
1347 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *);
1348 NTSTATUS vfs_streams_xattr_init(TALLOC_CTX *ctx)
1349 {
1350         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_xattr",
1351                                 &vfs_streams_xattr_fns);
1352 }