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