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